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 ?