1

I have a system Joomla 3.x plugin with a restore its settings option. For this I use XMLHttpRequest to load a backup file. No problem this side. The problem is in the php file used to update the database. I cannot access the global variables of Joomla I know it's normal because the file isn't called from Joomla itself.

my script:

function uploadFile() {
var fd = new FormData();
fd.append("fileToUpload",  document.getElementById('fileToUpload').files[0]);
var xhr = new XMLHttpRequest();
xhr.upload.addEventListener("progress", uploadProgress, false);
xhr.addEventListener("load", uploadComplete, false);
xhr.addEventListener("error", uploadFailed, false);
xhr.addEventListener("abort", uploadCanceled, false);
xhr.open("POST", "restore.php");
xhr.send(fd);
}

restore.php file:

function import($_FILES)
{
if ($_FILES['fileToUpload']['error'] > 0) {
    echo "<p class='alert alert-error'>An error occurred while uploading!</p>";     
} else {
    $file_name = $_FILES['fileToUpload']['name'];
    $file_ext  = strtolower(end(explode(".", $file_name)));
    $file_size = $_FILES['fileToUpload']['size'];
    if (($file_ext == "json") && ($file_size < 500000)) {
        $options = file_get_contents($_FILES['fileToUpload']['tmp_name']);

        // Get a database object. 
        $db = JFactory::getDbo();

        // Create a new query object.
        $query = $db->getQuery(true);

        // Fields to update.
        $fields = array(
            $db->quoteName('field_to_update') . ' = ' . $db->quote($options)
        );

        // Conditions for which records should be updated.
        $conditions = array(
            $db->quoteName('element') . ' = ' . $db->quote('plugin_name'), 
            $db->quoteName('folder') . ' = ' . $db->quote('system')
        );

        // Set the query and execute it.
        $query->update($db->quoteName('#__extensions'))->set($fields)->where($conditions);
        $db->setQuery($query);
        $result = $db->execute();

        echo "<p class='alert alert-success'>Backup file successfully restored!</p>";
    } else {
        if ($file_ext <> "json") {
            echo "<p class='alert alert-error'>Invalid file type! Must be a JSON file.</p>";
        } elseif ($file_size > 500000) {
            echo "<p class='alert alert-error'>File too big!</p>";
        }
    }
}
return;
}

Is it possible to access the database global variables of Joomla in my called php file ?

1

You have to bootstrap a whole Joomla app to get CMS stack. Something along the line should work in your file:

define('_JEXEC', 1);

error_reporting(E_ALL | E_NOTICE);
ini_set('display_errors', 1);

define('JPATH_BASE', dirname(dirname(dirname(__FILE__))));
require_once JPATH_BASE . DS . 'includes' . DS . 'defines.php';
require_once JPATH_BASE . DS . 'includes' . DS . 'framework.php';

JLoader::import('joomla.user.authentication');
JLoader::import('joomla.application.component.helper');

class MyJoomlaServer extends JApplicationWeb {

    public function doExecute() {
        // Get a database object. 
        $db = JFactory::getDbo();

        // Create a new query object.
        $query = $db->getQuery(true);

        // Fields to update.
        $fields = array(
            $db->quoteName('field_to_update') . ' = ' . $db->quote($options)
        );

        ...
    }

    public function isAdmin() {
        return false;
    }
}

$app = JApplicationWeb::getInstance('MyJoomlaServer');
JFactory::$application = $app;
$app->execute();
  • Thank you for your reply. I tried your code but I have got a '500 internal server error'. Maybe due to path? I also tried following code and it worked. What do you think about it? define( '_JEXEC', 1 ); define( 'JPATH_BASE', realpath(dirname(__FILE__).'/../../../../../../' )); require_once ( JPATH_BASE .'/includes/defines.php' ); require_once ( JPATH_BASE .'/includes/framework.php' ); /* Create the Application */ $app = JFactory::getApplication('site'); $app->initialise(); – n.h. Jan 19 '15 at 14:33
  • Yes JPATH_BASE needs to point to the directory of your file. Guess some debug statements will help to determine if the path is correct. Basically it should point to the root folder of your Joomla installation. – Laoneo Jan 19 '15 at 14:36
  • What do you think about the code in my comment? What is the better approach? – n.h. Jan 19 '15 at 14:42
  • Saw just now that you want to run the whole site. Can be that it works, never tried it. Let us know if it works, or do you get any error? – Laoneo Jan 19 '15 at 14:50
  • It's working and for now i haven't got any error. Basically i have a Joomla system plugin with some certain features and i included a call to my script which in turn calls my restore.php file. – n.h. Jan 19 '15 at 15:01
0

Cannot execute a php file in administrator/component directory Cannot execute a php file in administrator/component directory

Difficulties in calling ajax from joomla 2.5 module Difficulties in calling ajax from joomla 2.5 module

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.