Skip to content

Instantly share code, notes, and snippets.

@stephencroberts
Last active December 27, 2015 16:59
Show Gist options
  • Save stephencroberts/7358466 to your computer and use it in GitHub Desktop.
Save stephencroberts/7358466 to your computer and use it in GitHub Desktop.
Setup dev Joomla! site after pulling database from somewhere else
<?php
// Load Joomla! config
if (!file_exists('httpdocs/configuration.php')) {
echo "Couldn't find configuration.php!\n";
exit();
}
include('httpdocs/configuration.php');
$config = new JConfig();
$pfx = $config->dbprefix;
$dbname = $config->db;
$dbhost = $config->host;
$dbuser = $config->user;
$dbpass = $config->password;
$path = realpath(dirname($_SERVER['SCRIPT_FILENAME']));
// Get the hostname
$vpath = str_replace('/', '\/', $path);
$vhosts = file_get_contents('/etc/apache2/other/vhosts.conf');
preg_match("/<VirtualHost((?!VirtualHost).)*$vpath.*?VirtualHost>/s", $vhosts, $matches);
if (!isset($matches[0])) {
echo "No VirtualHost found!\n";
exit();
}
preg_match('/ServerName(.*)/', $matches[0], $matches);
$host = trim($matches[1]);
if (!$host) {
echo "Couldn't get the hostname!\n";
exit();
}
$mysql = trim(shell_exec("/usr/bin/which mysql"));
if (!$mysql) {
echo "Couldn't find mysql binary!\n";
exit();
}
// Import current.sql
$result = shell_exec("$mysql --user=$dbuser ".(empty($dbpass)?'':"--password=$dbpass ")."$dbname < $path/database/current.sql && echo ' '");
if ($result == null) {
echo "Failed to import current.sql!\n";
exit();
} else {
echo "Successfully imported current.sql!\n";
}
// Import develop.sql
$result = shell_exec("$mysql --user=$dbuser ".(empty($dbpass)?'':"--password=$dbpass ")."$dbname < $path/database/develop.sql && echo ' '");
if ($result == null) {
echo "Failed to import develop.sql!\n";
} else {
echo "Successfully imported develop.sql!\n";
}
// Connect to database
$mysqli = new mysqli($dbhost, $dbuser, $dbpass, $dbname);
if ($mysqli->connect_errno) {
printf("Connect failed: %s\n", $mysqli->connect_error);
exit();
}
// Update DigiToll Sync config
$query = str_replace('#__', $pfx, "SELECT `params` FROM `#__extensions` WHERE `element`='digitoll' AND `folder`='system';");
if ($result = $mysqli->query($query)) {
$params = $result->fetch_assoc();
$result->close();
if (isset($params['params'])) {
// Decode params
$params = json_decode($params['params']);
if (isset($params->sync_site) && isset($params->sync_path)) {
// Update values
$params->sync_site = $host;
$params->sync_path = "$path/database/develop.sql";
$params = $mysqli->real_escape_string(json_encode($params));
// Store values back in DB
$query = str_replace('#__', $pfx, "UPDATE `#__extensions` SET `params`='$params' WHERE `element`='digitoll' AND `folder`='system';");
if ($result = $mysqli->query($query)) {
echo "Updated DigiToll Sync!\n";
} else {
echo "Failed to update DigiToll Sync!\n";
}
}
}
}
// Turn of SSL
$query = str_replace('#__', $pfx, "UPDATE `#__extensions` SET `enabled`=0 WHERE `element`='sslredirect' AND `folder`='system';");
if ($result = $mysqli->query($query)) {
echo "SSL is OFF!\n";
} else {
echo "SSL is ON!\n";
}
$mysqli->close();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment