Current File : //var/softaculous/presta17/_clone.php
<?php

//////////////////////////////////////////////////////////////
//===========================================================
// clone.php
//===========================================================
// SOFTACULOUS 
// Version : 4.2.8
// Inspired by the DESIRE to be the BEST OF ALL
// ----------------------------------------------------------
// Started by: Alons
// Date:       10th Jan 2009
// Time:       21:00 hrs
// Site:       http://www.softaculous.com/ (SOFTACULOUS)
// ----------------------------------------------------------
// Please Read the Terms of use at http://www.softaculous.com
// ----------------------------------------------------------
//===========================================================
// (c)Softaculous Inc.
//===========================================================
//////////////////////////////////////////////////////////////

if(!defined('SOFTACULOUS')){

	die('Hacking Attempt');

}

//The Install process
function __clone(){

global $__settings, $error, $software, $globals, $replace_data, $source_data, $notes;
	
	//Do we meet the Minimum software requirements
	__requirements();

	if(!empty($error)){
		return false;
	}
	
	$temp = parse_url($source_data['softurl']);
	$source_data['relativeurl'] = $temp['path'];
	$source_data['domhost'] = $temp['host'];
	
	$temp = parse_url($__settings['softurl']);
	$__settings['relativeurl'] = $temp['path'];
	$__settings['domhost'] = $temp['host'];
	
	$__settings['punycode_softurl'] = check_punycode($__settings['softurl']);
	
	if(empty($__settings['punycode_softurl'])){
		$__settings['punycode_softurl'] = $__settings['softurl'];
	}
	
	//shop_url table require only punycode domain and not url
	$temp_punycode = parse_url($__settings['punycode_softurl']);
	
	$__settings['punycode_relativeurl'] = $temp_punycode['path'];
	
	$source_data['punycode_softurl'] = check_punycode($source_data['softurl']);
	
	if(empty($source_data['punycode_softurl'])){
		$source_data['punycode_softurl'] = $source_data['softurl'];
	}
	
	//shop_url table require only punycode domain and not url
	$temp_punycode = parse_url($source_data['punycode_softurl']);
	
	$source_data['punycode_relativeurl'] = $temp_punycode['path'];
	
	// To get Admin Folder
	$folder = sfilelist($__settings['softpath'], 0, 1, 1, 1);
	foreach ($folder as $list) {
		if(sfile_exists($__settings['softpath'].'/'.$list['name'].'/get-file-admin.php')){
			$software['adminurl'] = $__settings['admin_folder'] = $list['name'];
		}
	}
	
	// We need the Database Prefix to upgrade
	$file = sfile($__settings['softpath'].'/app/config/parameters.php');
	
	if(empty($file)){
		$error[] = 'Could not read the config file to get the database prefix and the other values.';
		return false;
	}
	
	// Lets get the prefix
	soft_preg_replace('/("|\')database_prefix("|\')(\s*?)=>(\s*?)("|\')(.*?)("|\'),/is', $file, $dbprefix, 6);
	
	if(empty($dbprefix)){
		$error[] = 'Unable to get database prefix.';
		return false;
	}
	
	$__settings['secret'] = srandstr(56);
	
	//This is to handle cookie_key and secret length for Prestashop 8+
	if(!sversion_compare($__settings['ver'], '8.0.0', '<')){
		// Lets get the cookie_key
		soft_preg_replace('/("|\')cookie_key("|\')(\s*?)=>(\s*?)("|\')(.*?)("|\'),/is', $file, $cookie_key, 6);
		$__settings['cookie_key'] = srandstr(64);
		$replace_data[$cookie_key] = $__settings['cookie_key'];
		
		$__settings['secret'] = srandstr(64);
	}
	
	// Lets get the cookie_iv
	soft_preg_replace('/("|\')cookie_iv("|\')(\s*?)=>(\s*?)("|\')(.*?)("|\'),/is', $file, $cookie_iv, 6);
	$__settings['cookie_iv'] = srandstr(32);
	$replace_data[$cookie_iv] = $__settings['cookie_iv'];
	
	// Lets get the secret
	soft_preg_replace('/("|\')secret("|\')(\s*?)=>(\s*?)("|\')(.*?)("|\'),/is', $file, $secret, 6);
	$replace_data[$secret] = $__settings['secret'];
	
	sclone_replace($replace_data, $__settings['softpath'].'/app/config/parameters.php', true);
	sclone_replace($replace_data, $__settings['softpath'].'/mails/en/order_conf.html', true);
	sclone_replace($replace_data, $__settings['softpath'].'/modules/ps_emailalerts/mails/en/new_order.html', true);
	sclone_replace($replace_data, $__settings['softpath'].'/modules/ps_emailalerts/mails/en/return_slip.html', true);
	
	$replace_data[str_replace("/", "\\/", $source_data['softpath'])] = str_replace("/", "\\/", $__settings['softpath']);
	
	sclone_replace($replace_data, $__settings['softpath'].'/config/themes/classic/shop1.json', true);
	
	$replace_data[$source_data['domhost']] = $__settings['domhost'];
	
	####################### TO REPLACE RELATIVE URL IN ROBOTS.TXT FILE ##############################	

	$replace_data['Disallow: '.$source_data['relativeurl'].'/app/'] = 'Disallow: '.$__settings['relativeurl'].'/app/';
	$replace_data['Disallow: '.$source_data['relativeurl'].'/cache/'] = 'Disallow: '.$__settings['relativeurl'].'/cache/';
	$replace_data['Disallow: '.$source_data['relativeurl'].'/classes/'] = 'Disallow: '.$__settings['relativeurl'].'/classes/';
	$replace_data['Disallow: '.$source_data['relativeurl'].'/config/'] = 'Disallow: '.$__settings['relativeurl'].'/config/';
	$replace_data['Disallow: '.$source_data['relativeurl'].'/controllers/'] = 'Disallow: '.$__settings['relativeurl'].'/controllers/';
	$replace_data['Disallow: '.$source_data['relativeurl'].'/download/'] = 'Disallow: '.$__settings['relativeurl'].'/download/';
	$replace_data['Disallow: '.$source_data['relativeurl'].'/js/'] = 'Disallow: '.$__settings['relativeurl'].'/js/';
	$replace_data['Disallow: '.$source_data['relativeurl'].'/localization/'] = 'Disallow: '.$__settings['relativeurl'].'/localization/';
	$replace_data['Disallow: '.$source_data['relativeurl'].'/log/'] = 'Disallow: '.$__settings['relativeurl'].'/log/';
	$replace_data['Disallow: '.$source_data['relativeurl'].'/mails/'] = 'Disallow: '.$__settings['relativeurl'].'/mails/';
	$replace_data['Disallow: '.$source_data['relativeurl'].'/modules/'] = 'Disallow: '.$__settings['relativeurl'].'/modules/';
	$replace_data['Disallow: '.$source_data['relativeurl'].'/override/'] = 'Disallow: '.$__settings['relativeurl'].'/override/';
	$replace_data['Disallow: '.$source_data['relativeurl'].'/pdf/'] = 'Disallow: '.$__settings['relativeurl'].'/pdf/';
	$replace_data['Disallow: '.$source_data['relativeurl'].'/src/'] = 'Disallow: '.$__settings['relativeurl'].'/src/';
	$replace_data['Disallow: '.$source_data['relativeurl'].'/tools/'] = 'Disallow: '.$__settings['relativeurl'].'/tools/';
	$replace_data['Disallow: '.$source_data['relativeurl'].'/translations/'] = 'Disallow: '.$__settings['relativeurl'].'/translations/';
	$replace_data['Disallow: '.$source_data['relativeurl'].'/upload/'] = 'Disallow: '.$__settings['relativeurl'].'/upload/';
	$replace_data['Disallow: '.$source_data['relativeurl'].'/var/'] = 'Disallow: '.$__settings['relativeurl'].'/var/';
	$replace_data['Disallow: '.$source_data['relativeurl'].'/vendor/'] = 'Disallow: '.$__settings['relativeurl'].'/vendor/';
	$replace_data['Disallow: '.$source_data['relativeurl'].'/webservice/'] = 'Disallow: '.$__settings['relativeurl'].'/webservice/';
	
	// From 1.8
	$replace_data['Disallow: '.$source_data['relativeurl'].'/password-recovery/'] = 'Disallow: '.$__settings['relativeurl'].'/password-recovery/';
	$replace_data['Disallow: '.$source_data['relativeurl'].'/address/'] = 'Disallow: '.$__settings['relativeurl'].'/address/';
	$replace_data['Disallow: '.$source_data['relativeurl'].'/addresses/'] = 'Disallow: '.$__settings['relativeurl'].'/addresses/';
	$replace_data['Disallow: '.$source_data['relativeurl'].'/login/'] = 'Disallow: '.$__settings['relativeurl'].'/login/';
	$replace_data['Disallow: '.$source_data['relativeurl'].'/registration/'] = 'Disallow: '.$__settings['relativeurl'].'/registration/';
	$replace_data['Disallow: '.$source_data['relativeurl'].'/cart/'] = 'Disallow: '.$__settings['relativeurl'].'/cart/';
	$replace_data['Disallow: '.$source_data['relativeurl'].'/discount/'] = 'Disallow: '.$__settings['relativeurl'].'/discount/';
	$replace_data['Disallow: '.$source_data['relativeurl'].'/order-history/'] = 'Disallow: '.$__settings['relativeurl'].'/order-history/';
	$replace_data['Disallow: '.$source_data['relativeurl'].'/identity/'] = 'Disallow: '.$__settings['relativeurl'].'/identity/';
	$replace_data['Disallow: '.$source_data['relativeurl'].'/my-account/'] = 'Disallow: '.$__settings['relativeurl'].'/my-account/';
	$replace_data['Disallow: '.$source_data['relativeurl'].'/order-follow/'] = 'Disallow: '.$__settings['relativeurl'].'/order-follow/';
	$replace_data['Disallow: '.$source_data['relativeurl'].'/credit-slip/'] = 'Disallow: '.$__settings['relativeurl'].'/credit-slip/';
	$replace_data['Disallow: '.$source_data['relativeurl'].'/order/'] = 'Disallow: '.$__settings['relativeurl'].'/order/';
	$replace_data['Disallow: '.$source_data['relativeurl'].'/search/'] = 'Disallow: '.$__settings['relativeurl'].'/search/';
	$replace_data['Disallow: '.$source_data['relativeurl'].'/guest-tracking/'] = 'Disallow: '.$__settings['relativeurl'].'/guest-tracking/';
	$replace_data['Disallow: '.$source_data['relativeurl'].'/order-confirmation/'] = 'Disallow: '.$__settings['relativeurl'].'/order-confirmation/';

	sclone_replace($replace_data, $__settings['softpath'].'/robots.txt', true);

	####################### END OF REPLACING RELATIVE URL IN ROBOTS.TXT FILE ##############################	
	
	@schmod($__settings['softpath'].'/app/cache/', $globals['odc'], 1);
	@schmod($__settings['softpath'].'/var/cache/', $globals['odc'], 1);
	
	$dom_database = "UPDATE ".$dbprefix."shop_url SET domain='".$__settings['domhost']."', domain_ssl='".$__settings['domhost']."', physical_uri='".$__settings['relativeurl']."/' WHERE id_shop_url='1';";
	sdb_query($dom_database, $__settings['softdbhost'], $__settings['softdbuser'], $__settings['softdbpass'], $__settings['softdb']);

	$dom_database1 = "UPDATE ".$dbprefix."configuration SET value='".$__settings['domhost']."' WHERE name='PS_SHOP_DOMAIN' OR name='PS_SHOP_DOMAIN_SSL';";
	sdb_query($dom_database1, $__settings['softdbhost'], $__settings['softdbuser'], $__settings['softdbpass'], $__settings['softdb']);

	$__settings['NW_SALT'] = srandstr(16);
	$query = "UPDATE ".$dbprefix."configuration SET value='".$__settings['NW_SALT']."' WHERE name='NW_SALT';";
	sdb_query($query, $__settings['softdbhost'], $__settings['softdbuser'], $__settings['softdbpass'], $__settings['softdb']);
	
	$__settings['PS_NEWSLETTER_RAND'] = rand().rand();
	$query1 = "UPDATE ".$dbprefix."configuration SET value='".$__settings['PS_NEWSLETTER_RAND']."' WHERE name='PS_NEWSLETTER_RAND';";
	sdb_query($query1, $__settings['softdbhost'], $__settings['softdbuser'], $__settings['softdbpass'], $__settings['softdb']);
	
	$truncate = "TRUNCATE TABLE ".$dbprefix."connections_source;";
	sdb_query($truncate, $__settings['softdbhost'], $__settings['softdbuser'], $__settings['softdbpass'], $__settings['softdb']);
	
	$__settings['PS_CHECKOUT_SHOP_UUID_V4'] = __getUUID();
	$query1 = "UPDATE ".$dbprefix."configuration SET value='".$__settings['PS_CHECKOUT_SHOP_UUID_V4']."' WHERE name='PS_CHECKOUT_SHOP_UUID_V4';";
	sdb_query($query1, $__settings['softdbhost'], $__settings['softdbuser'], $__settings['softdbpass'], $__settings['softdb']);
	
	if(empty($source_data['punycode_relativeurl'])){
		
		$query1 = "UPDATE ".$dbprefix."psreassurance SET icon=CONCAT('".$__settings['punycode_relativeurl']."',icon);";
		
		$query2 = "UPDATE ".$dbprefix."pagenotfound SET request_uri=CONCAT('".$__settings['punycode_relativeurl']."',request_uri);";
		
	}else{
		
		$query1 = "UPDATE ".$dbprefix."psreassurance SET icon=REPLACE(icon, '".$source_data['punycode_relativeurl']."', '".$__settings['punycode_relativeurl']."');";
		
		$query2 = "UPDATE ".$dbprefix."pagenotfound SET request_uri=REPLACE(request_uri, '".$source_data['punycode_relativeurl']."', '".$__settings['punycode_relativeurl']."');";
		
	}
	
	sdb_query($query1, $__settings['softdbhost'], $__settings['softdbuser'], $__settings['softdbpass'], $__settings['softdb']);
	
	sdb_query($query2, $__settings['softdbhost'], $__settings['softdbuser'], $__settings['softdbpass'], $__settings['softdb'], '', 1);
	
	if(sfile_exists($__settings['softpath'].'/.htaccess')){
	
		// If the installation is on root domain we need to change the below relative URL
		if(is_dom_root($source_data['softpath'])){
			$replace_data['RewriteBase \'/\''] = 'RewriteBase \''.$__settings['relativeurl'].'\'';
			$replace_data['RewriteBase /'] = 'RewriteBase '.$__settings['relativeurl'];
			//for friendly url
			$replace_data['RewriteRule . - [E=REWRITEBASE:/]'] = 'RewriteRule . - [E=REWRITEBASE:'.$__settings['relativeurl'].'/]';
			$replace_data['ErrorDocument 404 /index.php?controller=404'] = 'ErrorDocument 404 '.$__settings['relativeurl'].'/index.php?controller=404';
			
		}elseif(is_dom_root($__settings['softpath'])){
			$replace_data['RewriteBase \''.$source_data['relativeurl'].'\''] = 'RewriteBase \'/\'';
			$replace_data['RewriteBase '.$source_data['relativeurl']] = 'RewriteBase /';
			//for friendly url
			$replace_data['RewriteRule . - [E=REWRITEBASE:'.$source_data['relativeurl'].'/]'] = 'RewriteRule . - [E=REWRITEBASE:/]';
			$replace_data['ErrorDocument 404 '.$source_data['relativeurl'].'/index.php?controller=404'] = 'ErrorDocument 404 /index.php?controller=404';
		}else{
			//There was a bug of softaculous where the relativeurl was not passed from subdirectory to subdirectory. Fixed in softaculous 5.7.6
			$replace_data['RewriteBase \''.$source_data['relativeurl'].'\''] = 'RewriteBase \''.$__settings['relativeurl'].'\'';
			$replace_data['RewriteBase '.$source_data['relativeurl']] = 'RewriteBase '.$__settings['relativeurl'];
			// for friendly url
			$replace_data['RewriteRule . - [E=REWRITEBASE:'.$source_data['relativeurl'].'/]'] = 'RewriteRule . - [E=REWRITEBASE:'.$__settings['relativeurl'].'/]';
			$replace_data['ErrorDocument 404 '.$source_data['relativeurl'].'/index.php?controller=404'] = 'ErrorDocument 404 '.$__settings['relativeurl'].'/index.php?controller=404';
		}
	
		sclone_replace($replace_data, $__settings['softpath'].'/.htaccess', true);		
		
	}
	
	################# TO GENERATE NEW_COOCKIE_KEY ################################
	
	// Copy clone version of clone_bootstrap.php to unset old 'new_cookie_key'.
	@scopy($software['path'].'/clone_bootstrap.php', $__settings['softpath'].'/config/bootstrap.php');
	
	// If friendly url is enabled softurl redirects to particular language code. Hence we have appended $software['adminurl'] to generate new_cookie_key
	$resp = curl_call($__settings['softurl'].'/'.$software['adminurl'], 0, 5, array(), '', 1);
	
	if(!empty($resp)){
		if(preg_match('/<new_cookie_key>(.*?)<\/new_cookie_key>/is', $resp, $matches)){
			if(!empty($matches[1])){
				$new_cookie_key = $matches[1];
			}
		}
	}
	
	// Some users do not have domain resolving correctly so lets just use the source installation cookie key if we do not get the key from curl call
	if(!empty($new_cookie_key)){
		
		soft_preg_replace('/("|\')new_cookie_key("|\')(\s*?)=>(\s*?)("|\')(.*?)("|\'),/is', $file, $old_cookie_key, 6);
		$__settings['new_cookie_key'] = $new_cookie_key;
		$replace_data[$old_cookie_key] = $__settings['new_cookie_key'];
		
	}
	
	// Revert the bootstrap.php
	@scopy($software['path'].'/bootstrap.php', $__settings['softpath'].'/config/bootstrap.php');
	
	################# END OF GENERATING NEW_COOCKIE_KEY ################################
	
	############### TO GENERATE API KEYS #################
	
	// TO generate API KEYS
	@scopy($software['path'].'/update_appkey.php', $__settings['softpath'].'/update_appkey.php');
	
	$resp = swget($__settings['softurl'].'/update_appkey.php');
	
	//Retrieve public and private key pair
	if(preg_match('/<publickey>(.*?)<\/publickey>/is', $resp, $matches1)){
		$__settings['public_key'] = $matches1[1];
	}
	
	if(preg_match('/<privatekey>(.*?)<\/privatekey>/is', $resp, $matches1)){
		$__settings['private_key'] = $matches1[1];
	}
	
	sunlink($__settings['softpath'].'/update_appkey.php');
	
	######################################################
	
	sclone_replace($replace_data, $__settings['softpath'].'/app/config/parameters.php', true);
	
	srm($__settings['softpath'].'/app/cache/prod/');
	srm($__settings['softpath'].'/var/cache/prod/');
	
	if(!aefer() && (sis_dir($__settings['softpath'].'/app/cache/prod/') || sis_dir($__settings['softpath'].'/var/cache/prod/'))){
		sconfigure('soft_delete.php', false, 0, 1);
		swget($__settings['softurl'].'/soft_delete.php');
		sunlink($__settings['softpath'].'/soft_delete.php');
	}
	
	if(sis_dir($__settings['softpath'].'/app/cache/prod/') && sis_dir($__settings['softpath'].'/var/cache/prod/')){
		$notes = 'Please delete <b>'.$__settings['softpath'].'/app/cache/prod </b> and <b>'.$__settings['softpath'].'/var/cache/prod </b> directory before visiting database upgrade links.';
	}

}

//Function to generate UUID
function __getUUID(){
	return sprintf(
		'%04x%04x-%04x-%04x-%04x-%04x%04x%04x',

		// 32 bits for "time_low"
		mt_rand(0, 0xffff), mt_rand(0, 0xffff),

		// 16 bits for "time_mid"
		mt_rand(0, 0xffff),

		// 16 bits for "time_hi_and_version",
		// four most significant bits holds version number 4
		mt_rand(0, 0x0fff) | 0x4000,

		// 16 bits, 8 bits for "clk_seq_hi_res",
		// 8 bits for "clk_seq_low",
		// two most significant bits holds zero and one for variant DCE1.1
		mt_rand(0, 0x3fff) | 0x8000,

		// 48 bits for "node"
		mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff)
	);
}

function __requirements(){
	global $user, $__settings, $insid, $error, $software;
	
	// Not compatible on ampps
	if($globals['softpanel'] == 'ampps'){
		$error[] = $software['name'].' can not be cloned on AMPPS.';
	}
	
	if(!empty($__settings['softdirectory'])){
		//subdirectory installations are supported from 8.1.5
		if ($__settings['ver'] >= '8.1.1' && $__settings['ver'] < '8.1.5'){
			$error[] = 'Prestashop 8.1.1 to 8.1.4 can not be cloned in a sub-directory.';
		}
	}
	
	$folder = sfilelist($user['ins'][$insid]['softpath'], 0, 1, 1, 1);
	
	$src_admin_folder = $user['ins'][$insid]['admin_folder'];
	
	// Set the actual admin folder.
	foreach ($folder as $list) {
		
		if(aefer()){
			$list['name'] = end(explode("/", $list['name']));
		}

		if(sfile_exists($user['ins'][$insid]['softpath'].'/'.$list['name'].'/get-file-admin.php')){
			
			$user['ins'][$insid]['admin_folder'] = $list['name'];
			$software['adminurl'] = $__settings['admin_folder'] = $list['name'];
			$user['ins'][$insid]['fileindex'][] = $list['name'];
			
			// Save the source installation with correct admin folder (if it is not correct in our records)
			if($src_admin_folder != $user['ins'][$insid]['admin_folder']){
				saveinstallations($user['ins'][$insid]);
			}
			
			break;
		}
	}

}

?>