)
*
* @todo BOM could be used for charset autodetection
*/
if ($offset == $size) {
// UTF-8
if (strncmp($result, "\xEF\xBB\xBF", 3) == 0) {
$result = substr($result, 3);
// UTF-16 BE, LE
} elseif (strncmp($result, "\xFE\xFF", 2) == 0 || strncmp($result, "\xFF\xFE", 2) == 0) {
$result = substr($result, 2);
}
}
return $result;
}
function untar_archive($tarname, $untar_path, $file_list = array(), $handle_remote = false){
global $globals, $can_write, $ftp;
// Create directory if not there
if(!is_dir($untar_path)){
@mkdir($untar_path);
}
$tar_archive = new softtar($tarname, '', $handle_remote);
if(empty($file_list)){
$res = $tar_archive->extractModify($untar_path, '');
}else{
$res = $tar_archive->extractList($file_list, $untar_path);
}
if(!$res){
return false;
}
return true;
}
function backuply_optPOST($name, $default = ''){
global $error;
//Check the GETED NAME was GETed
if(isset($_POST[$name])){
return $_POST[$name];
}else{
return $default;
}
}
function backuply_entity_check($string){
//Convert Hexadecimal to Decimal
$num = ((substr($string, 0, 1) === 'x') ? hexdec(substr($string, 1)) : (int) $string);
//Squares and Spaces - return nothing
$string = (($num > 0x10FFFF || ($num >= 0xD800 && $num <= 0xDFFF) || $num < 0x20) ? '' : ''.$num.';');
return $string;
}
function backuply_rmdir_recursive_fn($path){
$path = (substr($path, -1) == '/' || substr($path, -1) == '\\' ? $path : $path.'/');
backuply_resetfilelist();
$files = backuply_filelist_fn($path, 1, 0, 'all');
$files = (!is_array($files) ? array() : $files);
//First delete the files only
foreach($files as $k => $v){
@chmod($k, 0777);
if(file_exists($k) && is_file($k) && @filetype($k) == "file"){
@unlink($k);
}
}
@clearstatcache();
$folders = backuply_filelist_fn($path, 1, 1, 'all');
$folders = (!is_array($folders) ? array() : $folders);
@krsort($folders);
//Now Delete the FOLDERS
foreach($folders as $k => $v){
@chmod($k, 0777);
if(is_dir($k)){
@rmdir($k);
}
}
@rmdir($path);
@clearstatcache();
}
function backuply_filelist_fn($startdir="./", $searchSubdirs=1, $directoriesonly=0, $maxlevel="all", $level=1, $reset = 1) {
//list the directory/file names that you want to ignore
$ignoredDirectory[] = ".";
$ignoredDirectory[] = "..";
$ignoredDirectory[] = "_vti_cnf";
global $directorylist; //initialize global array
if(substr($startdir, -1) != '/'){
$startdir = $startdir.'/';
}
if (is_dir($startdir)) {
if ($dh = opendir($startdir)) {
while (($file = readdir($dh)) !== false) {
if (!(array_search($file,$ignoredDirectory) > -1)) {
if (@filetype($startdir . $file) == "dir") {
//build your directory array however you choose;
//add other file details that you want.
$directorylist[$startdir . $file]['level'] = $level;
$directorylist[$startdir . $file]['dir'] = 1;
$directorylist[$startdir . $file]['name'] = $file;
$directorylist[$startdir . $file]['path'] = $startdir;
if ($searchSubdirs) {
if ((($maxlevel) == "all") or ($maxlevel > $level)) {
backuply_filelist_fn($startdir . $file . "/", $searchSubdirs, $directoriesonly, $maxlevel, ($level + 1), 0);
}
}
} else {
if (!$directoriesonly) {
// echo substr(strrchr($file, "."), 1);
//if you want to include files; build your file array
//however you choose; add other file details that you want.
$directorylist[$startdir . $file]['level'] = $level;
$directorylist[$startdir . $file]['dir'] = 0;
$directorylist[$startdir . $file]['name'] = $file;
$directorylist[$startdir . $file]['path'] = $startdir;
}}}}
closedir($dh);
}}
if(!empty($reset)){
$r = $directorylist;
$directorylist = array();
return($r);
}
}
function backuply_resetfilelist(){
global $directorylist;
$directorylist = array();
}
function backuply_die($txt, $l_file = '', $backuly_backup_dir = ''){
global $data, $can_write;
$array = array();
$array['result'] = $txt;
$array['data'] = $GLOBALS['data'];
$globals = ['l_readbytes', 'import_i', 'import_len', 'import_offset', 'status', 'current_status', 'import_spos', 'part_no', 'backuply_version', 'remote_data'];
// Updating data with $GLOBALS
foreach($globals as $global){
if(!empty($GLOBALS[$global])){
$data[$global] = $GLOBALS[$global];
}
}
// Add last backed up file to the array if the process is still INCOMPLETE
if(!empty($l_file)){
$array['l_file'] = $l_file;
}
// Send the current status of the operation performed according to which the next operation will be performed.
if(!empty($GLOBALS['current_status'])){
$array['current_status'] = $GLOBALS['current_status'];
}
// Was there an error ?
if(!empty($GLOBALS['error'])){
$array['local_tarname'] = $data['local_tar'];
$array['restore_error'] = $GLOBALS['error'];
//backuply_log(' restore error : '. var_export($array['restore_error'], 1));
restore_clean(1);
restore_curl($array);
die();
}
if($txt == 'DONE'){
$array = array();
$array['status'] = 0;
$array['backuly_backup_dir'] = $backuly_backup_dir;
$array['softpath'] = $data['softpath'];
$array['fname'] = $data['fname'];
$array['dbexist'] = $data['dbexist'];
$array['is_migrating'] = $data['is_migrating'];
$array['not_writable'] = isset($data['not_writable']) ? $data['not_writable'] : false;
$data = $array;
restore_clean(1);
restore_curl($data);
die();
}
// In case the restore is incomplete we call clean without force so it will delete files that are not required anymore
//restore_clean();
restore_curl($data);
}
// Copy from source to destination
function backuply_copydir_fn($source, $destination){
$source = (substr($source, -1) == '/' || substr($source, -1) == '\\' ? $source : $source.'/');
$destination = (substr($destination, -1) == '/' || substr($destination, -1) == '\\' ? $destination : $destination.'/');
$source_ = substr($source, 0, -1);
$destination_ = substr($destination, 0, -1);
if(!is_dir($destination)){
mkdir($destination);
}
backuply_resetfilelist();
$files = backuply_filelist_fn($source, 1, 1, 'all');
$files = (!is_array($files) ? array() : $files);
// Make the folders
foreach($files as $k => $v){
mkdir(str_replace($source_, $destination_, $k), $globals['dirchmod'], 1);
@chmod(str_replace($source_, $destination_, $k), fileperms($k));
}
@clearstatcache();
backuply_resetfilelist();
$files = backuply_filelist_fn($source, 1, 0, 'all');
$files = (!is_array($files) ? array() : $files);
// Copy the files
foreach($files as $k => $v){
if(file_exists($k) && is_file($k) && @filetype($k) == "file"){
if(!empty($GLOBALS['last_file']) && $GLOBALS['start'] == 0){
if(preg_match('#^'.$GLOBALS['last_file'].'$#', $k)){
$GLOBALS['start'] = 1; // give a jump start once the last backed up file is found..
}
continue; //return true to skip files
}
copy($k, str_replace($source_, $destination_, $k));
@chmod(str_replace($source_, $destination_, $k), fileperms($k));
// We can run the scripts for the end time already set
if(time() >= $GLOBALS['end']){
$GLOBALS['end_file'] = $last_file; // set end file so that we know where to start from
break;
}
}
}
@clearstatcache();
backuply_resetfilelist();
return true;
}
function backuply_mysql_connect($host, $user, $pass, $newlink = false){
if(extension_loaded('mysqli')){
//echo 'mysqli';
// To handle connection if user passes a custom port along with the host as localhost:6446
$exh = explode(':', $host);
if(!empty($exh[1])){
$sock = null;
$port = $exh[1];
// To handle connection if user passes a socket like localhost:usr/mysql/mysql.sock
if(!is_numeric($exh[1])){
$sock = $exh[1];
$port = null;
}
$sconn = @mysqli_connect($exh[0], $user, $pass, '', $port, $sock);
}else{
$sconn = @mysqli_connect($host, $user, $pass);
}
}else{
//echo 'mysql';
$sconn = @mysql_connect($host, $user, $pass, $newlink);
}
return $sconn;
}
function backuply_mysql_select_db($db, $conn){
if(extension_loaded('mysqli')){
$return = @mysqli_select_db($conn, $db);
}else{
$return = @mysql_select_db($db, $conn);
}
return $return;
}
/**
* This function is taken from WordPress https://developer.wordpress.org/reference/classes/wpdb/set_sql_mode/
* It sets the SQL mode because there could be some incompatible modes set so that can break database restore.
*/
function backuply_set_sql_mode($modes, $conn){
if(empty($modes)){
if(extension_loaded('mysqli')){
$res = mysqli_query($conn, 'SELECT @@SESSION.sql_mode');
} else {
$res = mysql_query('SELECT @@SESSION.sql_mode', $conn);
}
if(empty($res)){
backuply_log('Query to get SQL Mode returned empty');
return;
}
if(extension_loaded('mysqli')){
$modes_array = mysqli_fetch_array($res);
if (empty($modes_array[0])){
return;
}
$modes_str = $modes_array[0];
} else {
$modes_str = mysql_result($res, 0);
}
if(empty($modes_str)){
return;
}
$modes = explode(',', $modes_str);
}
$modes = array_change_key_case($modes, CASE_UPPER);
$incompatible_modes = array(
'NO_ZERO_DATE',
'ONLY_FULL_GROUP_BY',
'STRICT_TRANS_TABLES',
'STRICT_ALL_TABLES',
'TRADITIONAL',
'ANSI',
);
foreach($modes as $i => $mode){
if(in_array($mode, $incompatible_modes, true)){
unset($modes[$i]);
}
}
$modes_str = implode(',', $modes);
if(extension_loaded('mysqli')){
mysqli_query($conn, "SET SESSION sql_mode='$modes_str'");
} else {
mysql_query("SET SESSION sql_mode='$modes_str'", $conn);
}
}
function backuply_mysql_query($query, $conn){
$return = '';
if(extension_loaded('mysqli')){
try{
$return = @mysqli_query($conn, $query);
}catch(mysqli_sql_exception $e){
//
}
}else{
$return = @mysql_query($query, $conn);
}
return $return;
}
function backuply_mysql_error($conn){
if(extension_loaded('mysqli')){
$return = @mysqli_error($conn);
// In mysqli if connection is not made then we will get connection error using the following function.
if(empty($conn)){
$return = @mysqli_connect_error();
}
}else{
$return = @mysql_error($conn);
}
return $return;
}
function backuply_mysql_num_rows($result){
if(extension_loaded('mysqli')){
$return = @mysqli_num_rows($result);
}else{
$return = @mysql_num_rows($result);
}
return $return;
}
function backuply_mysql_fetch_array($result){
if(extension_loaded('mysqli')){
$return = @mysqli_fetch_array($result);
}else{
$return = @mysql_fetch_array($result);
}
return $return;
}
function backuply_stream_wrapper_register($protocol, $classname){
$protocols = array('dropbox', 'aws', 'caws', 'bcloud', 'gdrive', 'softftpes', 'softsftp', 'ftp', 'webdav', 'onedrive');
if(!in_array($protocol, $protocols)){
return false;
}
backuply_include_lib($protocol);
if(!stream_wrapper_register($protocol, $classname)){
return false;
}
return true;
}
function backuply_include_lib($protocol) {
if(!class_exists($protocol)){
if(file_exists(__DIR__ .'/lib/'.$protocol.'.php')) {
include_once(__DIR__ .'/lib/'.$protocol.'.php');
return;
}
if(file_exists(dirname(dirname(__FILE__)) . '/backuply-pro/lib/' .$protocol . '.php')) {
include_once(dirname(dirname(__FILE__)) . '/backuply-pro/lib/' .$protocol . '.php');
return;
}
return false;
}
return true;
}
function soft_preg_replace($pattern, $file, &$var, $valuenum, $stripslashes = ''){
preg_match($pattern, $file, $matches);
if(empty($stripslashes)){
$var = @trim($matches[$valuenum]);
}else{
$var = @stripslashes(trim($matches[$valuenum]));
}
}
function backuply_print($array){
echo '';
print_r($array);
echo '
';
}
// Create or updates the log file
function backuply_status_log($log, $status = 'working', $percentage = 0 ){
$log_file = dirname(__FILE__, 3).'/backuply/backuply_log.php';
if(!file_exists($log_file) || 0 == filesize($log_file)) {
$log = "\n" . $log; //Prepend php exit
}
$this_log = $log . '|' . $status . '|' . $percentage . "\n";
file_put_contents($log_file, $this_log, FILE_APPEND);
}
function restore_clean($force = 0){
global $data;
//Delete the temporarily downloaded archive if some error occur in the restore
if($GLOBALS['current_status'] >= 4 || !empty($force)){
@unlink($data['local_tar']);
}
// Delete these always, we will unzip these files everytime
@unlink($data['softpath'].'/softperms.txt');
@unlink($data['softpath'].'/softver.txt');
$info_file = str_replace('.tar.gz', '.php', $data['fname']);
// To delete the info file which gets extracted too.
if(file_exists($data['softpath'] .'/'. $info_file)){
@unlink($data['softpath'] .'/'. $info_file);
}
//Delete the softsql.sql file
if((file_exists($data['softpath'].'/'.$data['dbexist']) && $GLOBALS['current_status'] >= 2) || !empty($force)){
if(file_exists($data['softpath'].'/'.$data['dbexist']) && !is_dir($data['softpath'].'/'.$data['dbexist'])){
@unlink($data['softpath'].'/'.$data['dbexist']);
}
if(file_exists($data['softpath'].'/'.$data['fname']) && !is_dir($data['softpath'].'/'.$data['fname'])){
@unlink($data['softpath'].'/'.$data['fname']);
}
}
}
function handle_restore_status($output) {
if(!empty($output['restore_error'])){
// Send restore failure mail
$error = $output['restore_error'];
$error_string = 'Below are the error(s) :
';
foreach($error as $ek => $ev){
$error_string .= '* '.$ev.'
';
}
backuply_status_log($error_string, 'info');
backuply_status_log('Restore of your WordPress installation failed.', 'error', 100);
restore_clean(1);
final_restore_response($output, $error_string);
return false;
}
if($output['status'] == 0) {
backuply_log('Restore complete called !');
final_restore_response($output, '');
}
}
function final_restore_response($output, $error_str = '') {
global $data;
$config = backuply_get_config();
$url = $output['ajax_url'];
if(empty($config['RESTORE_KEY'])) {
backuply_status_log('Unable to find security key', 'error');
backuply_kill_process();
return;
}
$url .= '?action=backuply_restore_response&security='. $config['RESTORE_KEY'].'&user_id='.$output['user_id']. '&sess_key='.$output['sess_key'];
if(!empty($output['restore_db'])){
$url .= '&restore_db=true';
}
if(!empty($output['is_migrating'])){
$url .= '&is_migrating=true';
}
if(!empty($output['not_writable'])){
$url .= '¬_writable=true';
}
if(!empty($error_str)) {
$url .= '&error=1&error_string=' . htmlentities($error_str);
}
$curl = new Curl();
$curl->setConnectTimeout(5);
$curl->setTimeout(5);
$curl->setOpt(CURLOPT_SSL_VERIFYPEER, FALSE);
$curl->setOpt(CURLOPT_SSL_VERIFYHOST, FALSE);
$curl->setUserAgent('Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36');
$curl->get($url);
die();
}
// CURL call to call self to prevent timeout
function restore_curl($data) {
$config = backuply_get_config();
if(empty($config['RESTORE_KEY'])) {
backuply_kill_process();
return;
}
$data['restore_key'] = urlencode($config['RESTORE_KEY']);
$data['site_url'] = backuply_optPOST('site_url');
$data['backup_site_url'] = backuply_optPOST('backup_site_url');
$data['backup_site_path'] = backuply_optPOST('backup_site_path');
$data['ajax_url'] = backuply_optPOST('ajax_url');
$data['backuly_backup_dir'] = backuply_optPOST('backuly_backup_dir');
$data['restore_db'] = backuply_optPOST('restore_db');
$data['debug_mode'] = backuply_optPOST('debug_mode');
$data['sess_key'] = backuply_optPOST('sess_key');
$data['user_id'] = backuply_optPOST('user_id');
$data['exclude_db'] = backuply_optPOST('exclude_db');
handle_restore_status($data);
$curl = new Curl();
$curl->setConnectTimeout(5);
$curl->setTimeout(5);
$curl->setOpt(CURLOPT_SSL_VERIFYPEER, FALSE);
$curl->setOpt(CURLOPT_SSL_VERIFYHOST, FALSE);
$curl->setReferer((!empty($_SERVER['REQUEST_SCHEME']) ? $_SERVER['REQUEST_SCHEME'] : 'http') .'://'. $_SERVER['SERVER_NAME']);
$curl->setUserAgent('Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36');
$curl->post(backuply_optPOST('restore_curl_url'), $data);
die();
}
// Download the remote archive
function remote_archive_download_loop(){
global $data;
// Do we have a remote file ?
if(empty($data['remote_tar'])){
return false;
}
clearstatcache();
restore_got_killed(); //stops process if restore gets killed
// For untar and backward compatibiility of .tar files
// if(substr($this->_orig_tar, -2) == 'gz'){
// $this->_compress = true;
// $this->_compress_type = 'gz';
// }
$url = parse_url($data['remote_tar']);
if(!class_exists($url['scheme'])){
$did_register = backuply_stream_wrapper_register($url['scheme'], $url['scheme']);
if(empty($did_register)){
backuply_include_lib($url['scheme']);
}
}
//Create tmp directory if not present.
$localdir = pathinfo($data['local_tar'], PATHINFO_DIRNAME);
if(!is_dir($localdir)){
//create recursively
@mkdir($localdir, 0755, 1);
}
// Lets ensure file data is proper
if($data['restore_loop'] == 1){
// $data['size'] = filesize($data['remote_tar']);
// backuply_log('Size : ' . $data['size']);
@unlink($data['local_tar']);
$data['l_readbytes'] = 0;
}
if(empty($data['l_readbytes']) && $data['restore_loop'] == 1) {
backuply_status_log('Downloading the backup(' . $data['fname'] . ') from remote location.', 'info', 13);
}
if(method_exists($url['scheme'], 'download_file_loop')){
$obj = new $url['scheme'];
//Delete the local file if the process is starting afresh and the file already exists
if(file_exists($data['local_tar']) && empty($data['l_readbytes'])){
@unlink($data['local_tar']);
}
//backuply_log('invoked download function, org_tar : '.$this->_orig_tar.' , local : '.$this->_local_tar);
$obj->download_file_loop($data['remote_tar'], $data['local_tar'], $data['l_readbytes']);
}else{
// Open the file pointer if not opened
$remote_fp = @fopen($data['remote_tar'], 'rb');
$fp = @fopen($data['local_tar'], 'ab');
$GLOBALS['l_readbytes'] = $data['l_readbytes'];
//backuply_log($data['remote_tar']);
if(empty($remote_fp)){
$error[] = 'Unable to open remote file for reading';
backuply_log('Unable to open remote file for reading');
backuply_die('download_error');
}
$meta = stream_get_meta_data($remote_fp);
$chunk = 1048576;
// Seek to the location
if(!fseek($remote_fp, $GLOBALS['l_readbytes'])){
$error[] = 'Unable to seek file pointer';
backuply_die('download_error');
}
while($GLOBALS['l_readbytes'] < $data['size']){
restore_got_killed();
if(time() + 5 >= $GLOBALS['end']){
$GLOBALS['l_readbytes'] = filesize($data['local_tar']);
break;
}
if(($data['size'] - $GLOBALS['l_readbytes']) < $chunk){
$chunk = (int) $data['size'] - $GLOBALS['l_readbytes'];
backuply_log('Last Chunk '. $chunk);
}
// Read a block
$block = fread($remote_fp, $chunk);
$GLOBALS['l_readbytes'] += strlen($block);
backuply_log('Downloaded (L'.$data['restore_loop'].') : '.$GLOBALS['l_readbytes'].' / '.$data['size']);
// Write the block to the local file
fwrite($fp, $block);
$percentage = (filesize($data['local_tar']) / $data['size']) * 100;
backuply_status_log(''.round($percentage).'%
', 'downloading', 22);
}
// Close
@fclose($remote_fp);
@fclose($fp);
}
backuply_log('File Size ---->' . $data['size']);
if($GLOBALS['l_readbytes'] <= $data['size']){
$data['status'] = 1;
backuply_die('INCOMPLETE');
die();
} else {
backuply_status_log('TAR has been successfully downloaded, now we will untar the file', 'info', 20);
}
}
// Updates current time
function update_active_time($bak_dir) {
$ret = touch($bak_dir . '/restoration/restoration.php');
@touch($bak_dir . '/status.lock');
if($ret === FALSE) {
$error[] = 'Unable to create a restore session';
backuply_die('restoreerror');
}
}
// Updates Site Config file if its migration
function updating_config_file(){
global $data;
if(!file_exists($data['softpath'] . '/wp-config.php')){
backuply_log('Updating Wp-Config File: Unable to find wp-config.php');
return false;
}
// wp-config.php is required to be writable, especially in case of migration
// so that the database values could be updated to the new one.
if(!is_writable($data['softpath'] . '/wp-config.php')){
$old_permission = fileperms($data['softpath'] . '/wp-config.php');
chmod($data['softpath'] . '/wp-config.php', 0644);
}
if(!is_writable($data['softpath'] . '/wp-config.php')){
backuply_log('Updating Wp-Config File: wp-config.php is not writable!');
return false;
}
$config_cont = file_get_contents($data['softpath'] . '/wp-config.php');
$replace_list = [
'DB_NAME' => $data['softdb'],
'DB_USER' => $data['softdbuser'],
'DB_PASSWORD' => $data['softdbpass'],
'DB_HOST' => $data['softdbhost']
];
$matches = [];
foreach($replace_list as $con => $val){
preg_match_all('/\ndefine\((\s*?)("|\')'.preg_quote($con).'("|\')(\s*?),(\s*?)("|\')(.*?)("|\')(\s*?)\);/is', $config_cont, $match);
$replacement = str_replace($match[7], $val, $match[0]);
$config_cont = str_replace($match[0], $replacement, $config_cont);
}
file_put_contents($data['softpath'] . '/wp-config.php', $config_cont);
// Restoring the older permission of the wp-config.php file.
if(isset($old_permission) && is_numeric($old_permission)){
chmod($data['softpath'] . '/wp-config.php', $old_permission);
}
return true;
}
function update_urls_in_db($sql){
global $data;
// We dont need to change the url if it's just a restore
if(empty($data['is_migrating'])){
return $sql;
}
// What is the data to be replaced ?
$replace_data[$data['backup_site_path']] = $data['softpath'];
// Removing protocols
if(preg_match('/^http:\/\/www\./is', $data['backup_site_url'])){
$source_url = preg_replace('/^http:\/\/www\./is', '', $data['backup_site_url']);
}elseif(preg_match('/^http:\/\//is', $data['backup_site_url'])){
$source_url = preg_replace('/^http:\/\//is', '', $data['backup_site_url']);
}elseif(preg_match('/^https:\/\/www\./is', $data['backup_site_url'])){
$source_url = preg_replace('/^https:\/\/www\./is', '', $data['backup_site_url']);
}elseif(preg_match('/^https:\/\//is', $data['backup_site_url'])){
$source_url = preg_replace('/^https:\/\//is', '', $data['backup_site_url']);
}
$replace_data['https?:\/\/[www.]?'. preg_quote($source_url, '/')] = $data['site_url'];
if(!preg_match('/' . 'https?:\/\/[www.]?'.preg_quote($source_url, '\/?$/'). '/is', $data['backup_site_url'])){
$replace_data[$data['backup_site_url']] = $data['site_url'];
}
//Replacing protocols
if(preg_match('/^http:\/\/www\./is', $data['site_url'])){
$dest_url = preg_replace('/^http:\/\/www\./is', '', $data['site_url']);
}elseif(preg_match('/^http:\/\//is', $data['site_url'])){
$dest_url = preg_replace('/^http:\/\//is', '', $data['site_url']);
}elseif(preg_match('/^https:\/\/www\./is', $data['site_url'])){
$dest_url = preg_replace('/^https:\/\/www\./is', '', $data['site_url']);
}elseif(preg_match('/^https:\/\//is', $data['site_url'])){
$dest_url = preg_replace('/^https:\/\//is', '', $data['site_url']);
}
$replace_data['//www.'.$source_url] = '//www.'.$dest_url;
$replace_data['//'.$source_url] = '//'.$dest_url;
//Replace encoded softurls
$replace_data[rawurlencode('http://'.$source_url)] = rawurlencode($data['site_url']);
$replace_data[rawurlencode('http://www.'.$source_url)] = rawurlencode($data['site_url']);
$replace_data[rawurlencode('https://'.$source_url)] = rawurlencode($data['site_url']);
$replace_data[rawurlencode('https://www.'.$source_url)] = rawurlencode($data['site_url']);
//If enabled then it will replace all directory characters in database e.g if user has installation in subdir e. "/a" then the anchor and every word which has "a" will be replaced in database.
//$replace_data[$data['relativeurl']] = $__settings['relativeurl'];
// Just to be safe
foreach($replace_data as $rk => $rv){
if(empty($rk) || empty($rv)){
unset($replace_data[$rk]);
}
if(strpos($rk, '//') === 0){
$rk = '(? $data['l_readbytes']) {
remote_archive_download_loop();
}
}
// Restore files
if(!empty($data['restore_dir']) && empty($GLOBALS['current_status'])){
//backuply_log('restore files start');
// Store the progress
backuply_status_log('Restoring directories', 'working', 27);
// Set default values
$GLOBALS['start'] = 0;
$GLOBALS['end_file'] = '';
if(!untar_archive($local_file, $data['softpath'], array(), true)){
$error[] = 'There was some error while unzipping the backup files';
backuply_die('restoreerror');
}
// Is the backup process INCOMPLETE ?
if(!empty($GLOBALS['end_file'])){
$data['last_file'] = $GLOBALS['end_file'];
$data['last_byte'] = $GLOBALS['end_byte'];
$data['status'] = 1;
backuply_status_log('Restoring: ' .$GLOBALS['end_file']);
backuply_die('INCOMPLETE', $GLOBALS['end_file']);
///echo serialize($data);
}
// See if a permission list is there ?
$perms = @file($data['softpath'].'/softperms.txt');
if(is_array($perms)){
foreach($perms as $k => $v){
$link = $target = $dest_file = '';
$v = trim($v);
$perm = substr($v, -4);
// Do this only if the restore of files is already completed
if(empty($GLOBALS['end_file'])){
if(preg_match('/(.*?)linkto=(.*?)('.$perm.')/', $v, $out)){
$link = trim($out[1]);
$target = trim($out[2]);
if (substr($link, 0, 1) == '/'){
$link = $data['softpath'].$link;
}else{
$link = $data['softpath'].'/'.$link;
}
if(!empty($target)){
if(!@symlink($target, $link)){
$error[] = 'Unable to extract symbolic link {' . $link . '}';
backuply_die('restoresymlink');
}
}
}
}
// We are not going to touch the permission of the install directory.
if(trim(substr($v, 0, -5)) == '/'){
continue;
}
if(is_numeric($perm)){
if(empty($link)){
$dest_file = $data['softpath'].'/'.substr($v, 0, -5);
}else{
$dest_file = $link;
}
@chmod($dest_file, octdec($perm));
}
}
}
$GLOBALS['current_status'] = 1;
//backuply_log('restore files ends');
}
// Restore Database
if(!empty($data['restore_db']) && $GLOBALS['current_status'] < 2){
//backuply_log('restore db start');
// Updating wp-config
updating_config_file(); // Will need to update config on every restore because, we can't identify if a restore is migration or not unless there is a change in URL, and migration can happen even if there URL is same.
// Store the progress
backuply_status_log('Working on restoring Database', 'working', !empty($data['restore_dir']) ? 67 : 24);
// --- Enabling maintenance mode ---
$maintenance_string = '';
file_put_contents(cleanpath($data['backup_site_path']) . '/.maintenance', $maintenance_string); // We need to update everytime the script calls itself, becuase .maintenance gets deleted after 10 minutes of inactivity with the file.
backuply_status_log('Maintainance mode enabled', 'info', !empty($data['restore_dir']) ? 68 : 25);
$dbuser = $data['softdbuser'];
$dbpass = $data['softdbpass'];
// Does the USER exist ?
$mysql = @backuply_mysql_connect($data['softdbhost'], $dbuser, $dbpass, true);
// Try to select the DB if connection was successful
if($mysql){
$_mysql = @backuply_mysql_select_db($data['softdb'], $mysql);
}
// Untaring the backup file
if(!file_exists($data['softpath'].'/'.$data['dbexist'])){
backuply_status_log('Untaring the backup file', 'working', !empty($data['restore_dir']) ? 67 : 24);
if(!untar_archive($local_file, $data['softpath'], array($data['dbexist'], 'softver.txt'), true)){
$error[] = 'There was some error while unzipping the backup files';
//backuply_log('There was some error while unzipping the backup files');
backuply_die('restoreerror');
}
}
//$sql_data = implode('', file($data['softpath'].'/'.$data['dbexist']));
//Make the Connection
$__conn = @backuply_mysql_connect($data['softdbhost'], $dbuser, $dbpass, true);
backuply_mysql_query('SET CHARACTER SET utf8mb4', $__conn);
backuply_set_sql_mode(array(), $__conn);
//CHECK Errors and SELECT DATABASE
if(!empty($__conn)){
backuply_status_log('Successfully connected to the database L('. $data['restore_loop'] .')', 'working', !empty($data['restore_dir']) ? 68 : 25);
if(!(@backuply_mysql_select_db($data['softdb'], $__conn))){
//$softpanel->deldb($dbuser, $dbpass);
$error[] = 'Could not select the database to restore'.'
'.backuply_mysql_error($__conn);
backuply_die('res_err_selectmy');
//backuply_log('Could not select the database to restore');
}
}else{
$error[] = 'Could not connect to the database'.'
'.backuply_mysql_error($__conn);
backuply_die('err_myconn');
//backuply_log('Could not connect to the database');
}
// We did not create the database ! So just backup the tables required for this database
if(!empty($data['exclude_db'])){
// Do we have to get the tables list from backup info ?
$thisdb_tables = $data['exclude_db'];
if(!is_array($data['exclude_db'])){
$thisdb_tables = unserialize($data['exclude_db']);
}
// This is just to remove the ` since we are not getting it in $tables below
foreach($thisdb_tables as $tk => $tv){
$_thisdb_tables[trim($tk, '`')] = trim($tv, '`');
}
}
if(empty($GLOBALS['import_len'])) {
$res = backuply_mysql_query("SHOW TABLES", $__conn);
for($i=0; $i < backuply_mysql_num_rows($res); $i++){
$row = backuply_mysql_fetch_array($res);
// We do not need to backup this table
if(isset($_thisdb_tables) && is_array($_thisdb_tables) && in_array($row[0], $_thisdb_tables)){
continue;
}
$tables[] = $row[0];
}
// Some tables cause problem
$res = backuply_mysql_query("SET foreign_key_checks = 0", $__conn);
$backup_version = 0;
if(file_exists($data['softpath'].'/softver.txt')){
$backup_version = file_get_contents($data['softpath'].'/softver.txt');
$backup_version = trim($backup_version);
}
if(version_compare($backup_version, '1.1.7', '<=')){
foreach($tables as $k => $v){
backuply_status_log('Dropping old table : '.$v, 'writing');
$res = backuply_mysql_query("DROP TABLE `$v`", $__conn);
}
}
//Softaculous Function to import Data
backuply_status_log('Starting to import database', 'working', !empty($data['restore_dir']) ? 69 : 26);
}
backuply_import($data['softpath'].'/'.$data['dbexist'], $__conn);
//backuply_log('restore db complete');
if(!empty($error)){
return false;
}
backuply_status_log('Import has been completed', 'working', !empty($data['restore_dir']) ? 70 : 28);
// --- Disabling Maintenance mode ---
if(file_exists($data['backup_site_path'] . '/.maintenance')){
unlink($data['backup_site_path'] . '/.maintenance');
backuply_status_log('Maintainance mode disabled', 'info', !empty($data['restore_dir']) ? 72 : 29);
}
@unlink($data['softpath'].'/'.$data['dbexist']);
if(file_exists($data['softpath'].'/'.$data['fname'])){
@unlink($data['softpath'].'/'.$data['fname']);
}
$GLOBALS['current_status'] = 2;
}
$GLOBALS['current_status'] = 5;
// The process is complete. Send a NULL Value
backuply_die('DONE','',$data['backuly_backup_dir']);