Last active
September 20, 2017 12:58
-
-
Save bimlas/5b7c9b1fde2fc0d61ad5ef3709185dd4 to your computer and use it in GitHub Desktop.
How to clone Prestashop to localhost (or any other server)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// https://gist.github.com/bimlas/5b7c9b1fde2fc0d61ad5ef3709185dd4 | |
$include_after_setup = 'include-after-setup.php'; | |
?> | |
<html> | |
<body> | |
<h1>Prestashop: Setup for your clone</h1> | |
<b>This script will prepare your clone to work on the target server</b> | |
<ul> | |
<li>Change your cloned Prestashop URI to the directory of the script (for example http://localhost/presta-dev/)</li> | |
<li>Change the clone's SQL authentication settings (login information)</li> | |
<li>Execute code in <code><?php echo $include_after_setup ?></code> (database is available as <code>$db</code>, constants of <i>settings.inc.php</i> (<code>_DB_PREFIX_</code> for example) are defined)</li> | |
</ul> | |
<b>Install</b> | |
<ul> | |
<li>Run a database backup on your live server</li> | |
<li>Copy the whole Prestashop directory from live server to the target server</li> | |
<li>Update the target SQL database with the latest backup</li> | |
<li>Copy this file to the top directory of the target Prestashop</li> | |
<li>Open this script through your browser (for example: http://localhost/presta-dev/name-of-this-file.php)</li> | |
</ul> | |
<?php | |
function update_uri_in_database($db) | |
{ | |
$physical_uri = dirname($_SERVER['REQUEST_URI']) . '/'; | |
$domain = $_SERVER['SERVER_NAME']; | |
$update_query = " | |
UPDATE " . _DB_PREFIX_ . "shop_url | |
SET | |
domain = '$domain', | |
domain_ssl = '$domain', | |
physical_uri = '$physical_uri';\n"; | |
if (!mysqli_query($db, $update_query)) { | |
die('<b>Could not update the database:</b> ' . mysqli_error($db)); | |
} | |
} | |
function connect_to_database($sql_auth) | |
{ | |
$db = mysqli_connect($sql_auth['_DB_SERVER_'], $sql_auth['_DB_USER_'], $sql_auth['_DB_PASSWD_'], $sql_auth['_DB_NAME_']); | |
if (mysqli_connect_errno()) { | |
die('<b>Could not connect:</b> ' . mysqli_connect_error()); | |
} | |
return $db; | |
} | |
function update_database_auth_in_config($sql_auth) | |
{ | |
$config_path = 'config/settings.inc.php'; | |
$config = file($config_path); | |
$updated_config = preg_replace( | |
array_map(function ($key) {return "/(.*'$key', ')[^']*('.*)/";}, array_keys($sql_auth)), | |
array_map(function ($val) {return "$1$val$2";}, array_values($sql_auth)), | |
$config | |
); | |
file_put_contents($config_path, implode('', $updated_config)); | |
include $config_path; | |
} | |
if (!empty($_POST)) { | |
$db = connect_to_database($_POST['sql_auth']); | |
update_database_auth_in_config($_POST['sql_auth']); | |
update_uri_in_database($db); | |
echo '<b>Settings updated</b><br>'; | |
if (file_exists($include_after_setup)) { | |
echo "<b>Executing <code>$include_after_setup</code></b><br>"; | |
include $include_after_setup; | |
echo '<br>'; | |
} | |
mysqli_close($db); | |
exit('<b>Setup finished</b><br>'); | |
} | |
?> | |
<form method="post"> | |
<table> | |
<thead><b>SQL settings of the target (clone)</b></thead> | |
<tr> | |
<td><label for="sql_auth[_DB_SERVER_]">Server address</label> | |
</td> | |
<td><input name="sql_auth[_DB_SERVER_]" | |
type="text" | |
value="<?php echo $_SERVER['SERVER_NAME']; ?>"></td> | |
</tr> | |
<tr> | |
<td><label for="sql_auth[_DB_NAME_]">Database name</label></td> | |
<td><input name="sql_auth[_DB_NAME_]" | |
type="text"></td> | |
</tr> | |
<tr> | |
<td><label for="sql_auth[_DB_USER_]">User</label></td> | |
<td><input name="sql_auth[_DB_USER_]" | |
type="text"></td> | |
</tr> | |
<tr> | |
<td><label for="sql_auth[_DB_PASSWD_]">Password</label></td> | |
<td><input name="sql_auth[_DB_PASSWD_]" | |
type="password"></td> | |
</tr> | |
</table> | |
<input type="submit" value="Update settings"> | |
</form> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example for include-after-setup.php: