strlen($p_file_list[$i]))
&& (substr($v_header['filename'], 0, strlen($p_file_list[$i]))
== $p_file_list[$i])) {
$v_extract_file = true;
break;
}
}
// ----- It is a file, so compare the file names
elseif ($p_file_list[$i] == $v_header['filename']) {
$v_extract_file = true;
break;
}
}
} else {
$v_extract_file = true;
}
// ----- Look if this file need to be extracted
if (($v_extract_file) && (!$v_listing))
{
if (($p_remove_path != '')
&& (substr($v_header['filename'], 0, $p_remove_path_size)
== $p_remove_path))
$v_header['filename'] = substr($v_header['filename'],
$p_remove_path_size);
if (($p_path != './') && ($p_path != '/')) {
while (substr($p_path, -1) == '/')
$p_path = substr($p_path, 0, strlen($p_path)-1);
if (substr($v_header['filename'], 0, 1) == '/')
$v_header['filename'] = $p_path.$v_header['filename'];
else
$v_header['filename'] = $p_path.'/'.$v_header['filename'];
}
if (file_exists($v_header['filename'])) {
if ( (@is_dir($v_header['filename']))
&& ($v_header['typeflag'] == '')) {
$this->_error('File '.$v_header['filename']
.' already exists as a directory');
return false;
}
if ( ($this->_isArchive($v_header['filename']))
&& ($v_header['typeflag'] == "5")) {
$this->_error('Directory '.$v_header['filename']
.' already exists as a file');
return false;
}
if (!is_writeable($v_header['filename'])) {
//We cannot use $globals['ofc'] here and after restoring the files we are anyways changing the file's permissions according to the perms file. Therefore, using 0644/0755 directly here shouldn't be an issue.
if(is_dir($v_header['filename'])){
$chmod = chmod($v_header['filename'], 0755);
}else{
$chmod = chmod($v_header['filename'], 0644);
}
if (!is_writeable($v_header['filename'])) {
$this->_error('File '.$v_header['filename']
.' already exists and is write protected');
return false;
}
}
if (filemtime($v_header['filename']) > $v_header['mtime']) {
// To be completed : An error or silent no replace ?
}
}
// ----- Check the directory availability and create it if necessary
elseif (($v_result
= $this->_dirCheck(($v_header['typeflag'] == "5"
?$v_header['filename']
:dirname($v_header['filename'])))) != 1) {
$this->_error('Unable to create path for '.$v_header['filename']);
return false;
}
if ($v_extract_file) {
if ($v_header['typeflag'] == "5") {
if (!@file_exists($v_header['filename'])) {
if (!@mkdir($v_header['filename'], 0777)) {
$this->_error('Unable to create directory {'
.$v_header['filename'].'}');
return false;
}
}
} elseif ($v_header['typeflag'] == "2") {
if (@file_exists($v_header['filename'])) {
@unlink($v_header['filename']);
}
if (!@symlink($v_header['link'], $v_header['filename'])) {
$this->_error('Unable to extract symbolic link {'
.$v_header['filename'].'}');
return false;
}
} else {
if (($v_dest_file = @fopen($v_header['filename'], "wb")) == 0) {
$this->_error('Error while opening {'.$v_header['filename']
.'} in write binary mode');
return false;
} else {
$n = floor($v_header['size']/512);
for ($i=0; $i<$n; $i++) {
$v_content = $this->_readBlock();
fwrite($v_dest_file, $v_content, 512);
}
if (($v_header['size'] % 512) != 0) {
$v_content = $this->_readBlock();
fwrite($v_dest_file, $v_content, ($v_header['size'] % 512));
}
@fclose($v_dest_file);
if ($p_preserve) {
@chown($v_header['filename'], $v_header['uid']);
@chgrp($v_header['filename'], $v_header['gid']);
}
// ----- Change the file mode, mtime
@touch($v_header['filename'], $v_header['mtime']);
if ($v_header['mode'] & 0111) {
// make file executable, obey umask
$mode = fileperms($v_header['filename']) | (~umask() & 0111);
@chmod($v_header['filename'], $mode);
}
}
// ----- Check the file size
clearstatcache();
if (!is_file($v_header['filename'])) {
$this->_error('Extracted file '.$v_header['filename']
.'does not exist. Archive may be corrupted.');
return false;
}
$filesize = filesize($v_header['filename']);
if ($filesize != $v_header['size']) {
$this->_error('Extracted file '.$v_header['filename']
.' does not have the correct file size \''
.$filesize
.'\' ('.$v_header['size']
.' expected). Archive may be corrupted.');
return false;
}
}
} else {
$this->_jumpBlock(ceil(($v_header['size']/512)));
}
} else {
$this->_jumpBlock(ceil(($v_header['size']/512)));
}
if ($v_listing || $v_extract_file || $v_extraction_stopped) {
// ----- Log extracted files
if (($v_file_dir = dirname($v_header['filename']))
== $v_header['filename'])
$v_file_dir = '';
if ((substr($v_header['filename'], 0, 1) == '/') && ($v_file_dir == ''))
$v_file_dir = '/';
// Only if we are to return the list i.e. in listContent() then we fill full $v_header else we just need the count
$p_list_detail[$v_nb++] = (!empty($v_listing) ? $v_header : '');
if (is_array($p_file_list) && (count($p_list_detail) == count($p_file_list))) {
return true;
}
}
}
return true;
}
// }}}
// {{{ _openAppend()
function _openAppend()
{
if (filesize($this->_tarname) == 0)
return $this->_openWrite();
if ($this->_compress) {
$this->_close();
if (!@rename($this->_tarname, $this->_tarname.".tmp")) {
$this->_error('Error while renaming \''.$this->_tarname
.'\' to temporary file \''.$this->_tarname
.'.tmp\'');
return false;
}
if ($this->_compress_type == 'gz')
$v_temp_tar = @gzopen($this->_tarname.".tmp", "rb");
elseif ($this->_compress_type == 'bz2')
$v_temp_tar = @bzopen($this->_tarname.".tmp", "r");
if ($v_temp_tar == 0) {
$this->_error('Unable to open file \''.$this->_tarname
.'.tmp\' in binary read mode');
@rename($this->_tarname.".tmp", $this->_tarname);
return false;
}
if (!$this->_openWrite()) {
@rename($this->_tarname.".tmp", $this->_tarname);
return false;
}
if ($this->_compress_type == 'gz') {
$end_blocks = 0;
while (!@gzeof($v_temp_tar)) {
$v_buffer = @gzread($v_temp_tar, 512);
if ($v_buffer == ARCHIVE_TAR_END_BLOCK || strlen($v_buffer) == 0) {
$end_blocks++;
// do not copy end blocks, we will re-make them
// after appending
continue;
} elseif ($end_blocks > 0) {
for ($i = 0; $i < $end_blocks; $i++) {
if(!$this->_writeBlock(ARCHIVE_TAR_END_BLOCK)){
return false;
}
}
$end_blocks = 0;
}
$v_binary_data = pack("a512", $v_buffer);
if(!$this->_writeBlock($v_binary_data)){
return false;
}
}
@gzclose($v_temp_tar);
}
elseif ($this->_compress_type == 'bz2') {
$end_blocks = 0;
while (strlen($v_buffer = @bzread($v_temp_tar, 512)) > 0) {
if ($v_buffer == ARCHIVE_TAR_END_BLOCK || strlen($v_buffer) == 0) {
$end_blocks++;
// do not copy end blocks, we will re-make them
// after appending
continue;
} elseif ($end_blocks > 0) {
for ($i = 0; $i < $end_blocks; $i++) {
if(!$this->_writeBlock(ARCHIVE_TAR_END_BLOCK)){
return false;
}
}
$end_blocks = 0;
}
$v_binary_data = pack("a512", $v_buffer);
if(!$this->_writeBlock($v_binary_data)){
return false;
}
}
@bzclose($v_temp_tar);
}
if (!@unlink($this->_tarname.".tmp")) {
$this->_error('Error while deleting temporary file \''
.$this->_tarname.'.tmp\'');
}
} else {
// ----- For not compressed tar, just add files before the last
// one or two 512 bytes block
if (!$this->_openReadWrite())
return false;
clearstatcache();
$v_size = filesize($this->_tarname);
// We might have zero, one or two end blocks.
// The standard is two, but we should try to handle
// other cases.
fseek($this->_file, $v_size - 1024);
if (fread($this->_file, 512) == ARCHIVE_TAR_END_BLOCK) {
fseek($this->_file, $v_size - 1024);
}
elseif (fread($this->_file, 512) == ARCHIVE_TAR_END_BLOCK) {
fseek($this->_file, $v_size - 512);
}
}
return true;
}
// }}}
// {{{ _append()
function _append($p_filelist, $p_add_dir='', $p_remove_dir='')
{
if (!$this->_openAppend())
return false;
if ($this->_addList($p_filelist, $p_add_dir, $p_remove_dir))
$this->_writeFooter();
$this->_close();
return true;
}
// }}}
// {{{ _dirCheck()
function _dirCheck($p_dir)
{
clearstatcache();
if ((@is_dir($p_dir)) || ($p_dir == ''))
return true;
$p_parent_dir = dirname($p_dir);
if (($p_parent_dir != $p_dir) &&
($p_parent_dir != '') &&
(!$this->_dirCheck($p_parent_dir)))
return false;
if (!@mkdir($p_dir, 0777)) {
$this->_error("Unable to create directory '$p_dir'");
return false;
}
return true;
}
// }}}
// {{{ _pathReduction()
function _pathReduction($p_dir)
{
$v_result = '';
// ----- Look for not empty path
if ($p_dir != '') {
// ----- Explode path by directory names
$v_list = explode('/', $p_dir);
// ----- Study directories from last to first
for ($i=sizeof($v_list)-1; $i>=0; $i--) {
// ----- Look for current path
if ($v_list[$i] == ".") {
// ----- Ignore this directory
// Should be the first $i=0, but no check is done
}
else if ($v_list[$i] == "..") {
// ----- Ignore it and ignore the $i-1
$i--;
}
else if ( ($v_list[$i] == '')
&& ($i!=(sizeof($v_list)-1))
&& ($i!=0)) {
// ----- Ignore only the double '//' in path,
// but not the first and last /
} else {
$v_result = $v_list[$i].($i!=(sizeof($v_list)-1)?'/'
.$v_result:'');
}
}
}
if (defined('OS_WINDOWS') && OS_WINDOWS) {
$v_result = strtr($v_result, '\\', '/');
}
return $v_result;
}
// }}}
// {{{ _translateWinPath()
function _translateWinPath($p_path, $p_remove_disk_letter=true)
{
if (defined('OS_WINDOWS') && OS_WINDOWS) {
// ----- Look for potential disk letter
if ( ($p_remove_disk_letter)
&& (($v_position = strpos($p_path, ':')) != false)) {
$p_path = substr($p_path, $v_position+1);
}
// ----- Change potential windows directory separator
if ((strpos($p_path, '\\') > 0) || (substr($p_path, 0,1) == '\\')) {
$p_path = strtr($p_path, '\\', '/');
}
}
return $p_path;
}
// }}}
}
error_reporting(E_ALL);
function rmdir_recursive_fn($path){
$path = (substr($path, -1) == '/' || substr($path, -1) == '\\' ? $path : $path.'/');
resetfilelist();
$files = 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 = 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 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)) {
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 tar_archive($tarname, $file_list, $handle_remote = false){
global $globals;
$tar_archive = new softtar($tarname, '', $handle_remote);
$res = $tar_archive->createModify($file_list, '', '');
if(!$res){
return false;
}
return true;
}
function resetfilelist(){
global $directorylist;
$directorylist = array();
}
function softdie($txt, $l_file = ''){
global $data, $can_write;
$array = array();
$array['settings'] = $GLOBALS['settings'];
$array['result'] = $txt;
$array['data'] = $GLOBALS['data'];
$array['backup_status'] = $GLOBALS['backup_status'];
// Add last backed up file to the array if the process is still INCOMPLETE
if(!empty($l_file)){
$array['l_file'] = $l_file;
if(!empty($GLOBALS['init_data'])){
$array['init_data'] = $GLOBALS['init_data'];
}
if(!empty($GLOBALS['start_pos'])){
$array['init_pos'] = $GLOBALS['start_pos'];
}
// AWS stuff
if(!empty($GLOBALS['awss3_part_no'])){
$array['awss3_part_no'] = $GLOBALS['awss3_part_no'];
}
// AWS stuff
if(!empty($GLOBALS['awss3_etags'])){
$array['awss3_etags'] = $GLOBALS['awss3_etags'];
}
}
if($txt == 'DONE'){
$array['size'] = filesize($GLOBALS['successfile']);
/////Temp solution for non-suphp servers
if(!$can_write){
$array['cant_write'] = 1;
}
}
// Was there an error ?
if(!empty($GLOBALS['error'])){
$array['error'] = $GLOBALS['error'];
}
echo ''.base64_encode(serialize($array)).'';die();
}
// Clean the Backup files
function backup_clean($data){
foreach($GLOBALS['bfh'] as $v){
@fclose($v);
}
// Delete tmp/ folder only if the process was completed
if($GLOBALS['backup_status'] == BACKUP_COMPLETE){
rmdir_recursive_fn($data['path'].'/tmp/'.$data['name']);
}
if(file_exists($data['path'].'/tmp/'.$data['name'].'/softver.txt')){
@unlink($data['path'].'/tmp/'.$data['name'].'/softver.txt');
}
return false;
}
// Delete old failed backups
function delete_failed_backups(){
global $data, $tmpdir;
$start_time = time();
$backup_path = $data['path'].'/';
$raw_files = filelist_fn($backup_path, 0);
// r_print($raw_files);
if(!empty($raw_files)){
//for dot'.' file
$dotfile_deleted_count = 0;
$dot_delete_limit = 100; // set max 100
//for 'tmp' folder
$tmpfile_deleted_count = 0;
$tmp_delete_limit = 100; // set max 100
$one_hr = time() - 3600; // 1 hour
$delete_time_limit = $start_time + 10;
foreach($raw_files as $raw_k => $raw_v){
if(($dotfile_deleted_count >= $dot_delete_limit) || (time() > $delete_time_limit)){
break;
}
if(empty($raw_v['dir']) && strpos($raw_v['name'], '.') === 0 && preg_match('/^\..*\.tar\.gz$/', $raw_v['name'])){
$filetime = filemtime($raw_k);
if($filetime < $one_hr){
// echo 'deleting - '.$raw_k.'
';
unlink($raw_k);
$dotfile_deleted_count++;
}
}
}
if((time() > $delete_time_limit)){
return true;
}
//to delete tmp folder data
$raw_files_tmp = filelist_fn($backup_path.'tmp/', 0);
// r_print($raw_files_tmp);
if(!empty($raw_files_tmp)){
foreach($raw_files_tmp as $tmp_k => $tmp_v){
if(($tmpfile_deleted_count >= $tmp_delete_limit) || (time() > $delete_time_limit)){
break;
}
if(empty($tmp_v['name'])){
continue;
}
$tmp_filetime = filemtime($tmp_k);
if($tmp_filetime < $one_hr){
// echo 'deleting tmp files - '.$tmp_k.' => '.$tmp_filetime.'
';
if(empty($tmp_v['dir'])){
unlink($tmp_k);
}else{
rmdir_recursive_fn($tmp_k);
}
$tmpfile_deleted_count++;
}
}
}
}
}
function optGET($name, $default = ''){
global $error;
//Check the GETED NAME was GETed
if(isset($_GET[$name])){
return inputsec(htmlizer(trim($_GET[$name])));
}else{
return $default;
}
}
function optREQ($name, $default = ''){
global $error;
//Check the POSTED NAME was posted
if(isset($_REQUEST[$name])){
return inputsec(htmlizer(trim($_REQUEST[$name])));
}else{
return $default;
}
}
function inputsec($string){
//get_magic_quotes_gpc is depricated in php 7.4
if(version_compare(PHP_VERSION, '7.4', '<')){
if(!get_magic_quotes_gpc()){
$string = addslashes($string);
}else{
$string = stripslashes($string);
$string = addslashes($string);
}
}else{
$string = addslashes($string);
}
// This is to replace ` which can cause the command to be executed in exec()
$string = str_replace('`', '\`', $string);
return $string;
}
function htmlizer($string){
global $globals;
$string = htmlentities($string, ENT_QUOTES, @$globals['charset']);
preg_match_all('/(&#(\d{1,7}|x[0-9a-fA-F]{1,6});)/', $string, $matches);//r_print($matches);
foreach($matches[1] as $mk => $mv){
$tmp_m = entity_check($matches[2][$mk]);
$string = str_replace($matches[1][$mk], $tmp_m, $string);
}
return $string;
}
function 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 cleanpath($path){
// This was made for Locaweb since they have network paths starting with \\
/* if(function_exists('override_cleanpath')){
return override_cleanpath($path);
} */
$path = str_replace('\\\\', '/', $path);
$path = str_replace('\\', '/', $path);
return rtrim($path, '/');
}
function soft_stream_wrapper_register($protocol, $classname){
$protocols = array('dropbox', 'gdrive', 'softftpes', 'softsftp', 'webdav','softaws', 'onedrive');
if(!in_array($protocol, $protocols)){
return true;
}
@include_once('_'.$protocol.'.php');
if(!stream_wrapper_register($protocol, $classname)){
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 r_print($array){
echo '';
print_r($array);
echo '
';
}
@unlink(__FILE__); // More has to be done here !
// Do we need to rename the .htaccess back ?
if(file_exists('soft_htaccess')){
@rename('soft_htaccess', '.htaccess');
}
// The settings
$settings = unserialize(base64_decode('[[[settings]]]'));
$data = unserialize(base64_decode('[[[data]]]'));
$software = unserialize(base64_decode('[[[software]]]'));
$app = unserialize(base64_decode('[[[app]]]'));
define('APP', $app);
// Check the progress
define('BACKUP_SQL', 1);
define('BACKUP_DIR', 2);
define('BACKUP_DATADIR', 3);
define('BACKUP_WWWDIR', 4);
define('BACKUP_COMPLETE', 5);
$remote_location = unserialize(base64_decode('[[[remote_location]]]'));
global $user, $globals, $theme, $softpanel, $settings, $iscripts, $catwise, $error, $can_write;
$can_write = can_create_file(); // Check if we can write
$name = $data['name'];
//$path = $data['path'];
$tmpdir = $data['path'].'/tmp';
if(!empty($remote_location)){
$path = $remote_location['full_backup_loc'];
$zipfile = $path.'/.'.$name.'.tar';
$successfile = $path.'/'.$name.'.tar';
// For OneDrive and AWS
$GLOBALS['local_dest'] = $data['path'];
soft_stream_wrapper_register($remote_location['protocol'], $remote_location['protocol']);
}else{
/////Temp solution for non-suphp servers
$path = ((!$can_write) ? $tmpdir : $data['path']);
$zipfile = $path.'/.'.$name.'.tar.gz';
$successfile = $path.'/'.$name.'.tar.gz';
}
$f_list = array(); // Files/Folder which has to be added to the tar.gz
// We need to stop execution in 25 secs.. We will be called again if the process is incomplete
//Increased the exec time to 500 temporarily as Backup on remote in aefer is creating incorrectly compressed file when the backups executes in loop
// Is custom keep alive time defained ?
if(!empty($settings['SOFTACULOUS_KEEP_ALIVE'])){
$keepalive = (int) $settings['SOFTACULOUS_KEEP_ALIVE'];
}
// Set default value
if(empty($keepalive)){
$keepalive = 25;
}
$GLOBALS['end'] = (int) time() + $keepalive;
// Empty last file everytime as a precaution
$GLOBALS['last_file'] = '';
$GLOBALS['last_file'] = optREQ('last_file');
if(!empty($GLOBALS['last_file'])){
$GLOBALS['last_file'] = rawurldecode($GLOBALS['last_file']);
}
$GLOBALS['backup_status'] = (int) optREQ('backup_status');
if($GLOBALS['backup_status'] < 1){
delete_failed_backups();
}
$GLOBALS['sinit_data'] = '';
$GLOBALS['sinit_data'] = optGET('init_data');
if(!empty($GLOBALS['sinit_data'])){
$GLOBALS['sinit_data'] = base64_decode($GLOBALS['sinit_data']);
}
$GLOBALS['init_data'] = $GLOBALS['sinit_data'];
$GLOBALS['init_pos'] = 0;
$GLOBALS['init_pos'] = (int) optGET('init_pos');
$GLOBALS['start_pos'] = $GLOBALS['init_pos'];
// AWS Stuff
$GLOBALS['awss3_part_no'] = (int) optGET('awss3_part_no');
if(empty($GLOBALS['awss3_part_no'])){
$GLOBALS['awss3_part_no'] = 1;
}
// AWS Stuff
$GLOBALS['awss3_etags'] = optGET('awss3_etags');
if(!empty($GLOBALS['awss3_etags'])){
$GLOBALS['awss3_etags'] = unserialize(base64_decode($GLOBALS['awss3_etags']));
}else{
$GLOBALS['awss3_etags'] = [];
}
$GLOBALS['doing_soft_files'] = false;
//Backup the DATABASE
// If $GLOBALS['last_file'] is not empty that means the database has been already exported and added to tar
if(!empty($data['backup_db']) && !empty($data['softdb']) && $GLOBALS['backup_status'] < BACKUP_SQL){
// Store the progress
//soft_progress($data['ssk'], 15, $l['backingup_db']);
$GLOBALS['progress'][1] = 15;
$dbfile = $tmpdir.'/'.$name.'/softsql.sql';
$f_list['BACKUP_SOFT_FILES_1'] = '';
$f_list[] = $dbfile;
$f_list['BACKUP_SQL'] = '';
$f_list['BACKUP_SOFT_FILES_2'] = '';
$GLOBALS['replace']['from']['softsql'] = $dbfile;
$GLOBALS['replace']['to']['softsql'] = 'softsql.sql';
$dbuser = (empty($softpanel->backupmysql['user']) ? $data['softdbuser'] : $softpanel->backupmysql['user']);
$dbpass = (empty($softpanel->backupmysql['pass']) ? $data['softdbpass'] : $softpanel->backupmysql['pass']);
$__settings['softurl'] = 'http://'.$data['softdomain'];
if(!empty($data['mysql_dump']) && function_exists('exec')){
exec('mysqldump -h '.escapeshellarg($data['softdbhost']).' -u '.escapeshellarg($dbuser).' -p'.escapeshellarg($dbpass).' '.escapeshellarg($data['softdb']).' > '.escapeshellarg($dbfile));
}
if(!file_exists($dbfile) || empty(filesize($dbfile)) ){
$sql_conn = soft_mysql_connect($data['softdbhost'], $dbuser, $dbpass);
if(!$sql_conn){
$error['mysql_connect'] = 'Cannot connect mysql.';
softdie('conn');
}
$sel = soft_mysql_select_db($data['softdb'], $sql_conn);
if(!$sel){
$error['mysql_sel_db'] = 'Could not select the database';
softdie('conn');
}
$host = $data['softdbhost'];
$user = $data['softdbuser'];
$pass = $data['softdbpass'];
$db = $data['softdb'];
//include_once('mysql_functions.php');
if(!backup_mysql_fn($host, $user, $pass, $db, $dbfile)){
$error[] = 'Back up was not successful';
softdie('conn');
}
}
if(!file_exists($dbfile)){
$error['backup_db'] = 'Could not create sql file from database.';
return backup_clean($data);
}
}
//Backup the DIRECTORY
if(!empty($data['backup_dir']) && $GLOBALS['backup_status'] < BACKUP_DIR){
// Store the progress
//soft_progress($data['ssk'], 45, $progress_data);
$GLOBALS['progress'][2] = 45;
// Save the version
$GLOBALS['bfh']['softver'] = @fopen($data['path'].'/tmp/'.$data['name'].'/softver.txt','a');
@fwrite($GLOBALS['bfh']['softver'], $data['ver']);
$GLOBALS['replace']['from']['softver'] = $data['path'].'/tmp/'.$data['name'].'/softver.txt';
$GLOBALS['replace']['to']['softver'] = 'softver.txt';
if(!empty($data['select_files_backup']) && !empty($data['fileindex'])){
$_root_filelist = filelist_fn(cleanpath($data['softpath']), 0);
$root_filelist = array();
// Lets get the full paths in fileindex
$full_fileindex = array();
foreach($data['fileindex'] as $sfk => $sfv){
$full_fileindex[] = trim(cleanpath($data['softpath'])).'/'.$sfv;
}
foreach($_root_filelist as $rk => $rv){
$tmp_rk = cleanpath($rk);
$tmp_rv = $rv;
// Do we need to exclude the files ?
if(!empty($data['select_files_backup']) && !in_array(trim($tmp_rk), $full_fileindex)){
continue;
}
$tmp_rv['path'] = cleanpath($rv['path']);
$root_filelist[$tmp_rk] = $tmp_rv;
}
$final_filelist = array_keys($root_filelist);
//r_print($final_filelist);
foreach($final_filelist as $fk => $fv){
$f_list[] = $fv;
}
}else{
// Adding the directory in $f_list to add to tar
$f_list[] = $data['softpath'].'/';
}
$f_list['BACKUP_DIR'] = '';
// File Permission
$GLOBALS['bfh']['softperms'] = @fopen($data['path'].'/tmp/'.$data['name'].'/softperms.txt','a');
$GLOBALS['replace']['from']['softperms'] = $data['path'].'/tmp/'.$data['name'].'/softperms.txt';
$GLOBALS['replace']['to']['softperms'] = 'softperms.txt';
//Did it open the File Stream
if(!$GLOBALS['bfh']['softperms']){
$error[] = 'There were errors while trying to make a file of permissions';
backup_clean($data);
softdie('permdir');
}
// The directory itself
@fwrite($GLOBALS['bfh']['softperms'], '/ '.@substr(sprintf('%o', fileperms($data['softpath'])), -4)."\n");
}
// Backup the datadir as well
if(!empty($data['backup_datadir']) && !empty($data['softdatadir']) && $GLOBALS['backup_status'] < BACKUP_DATADIR){
// Store the progress
//soft_progress($data['ssk'], 85, $l['backingup_datadir']);
$GLOBALS['progress'][3] = 85;
// Adding the directory in $f_list to add to tar
$f_list[] = $data['softdatadir'].'/';
$f_list['BACKUP_DATADIR'] = '';
$GLOBALS['replace']['from']['softdatadir'] = $data['softdatadir'].'/';
$GLOBALS['replace']['to']['softdatadir'] = 'softdatadir/';
// We now create the file for storing file permission of datadir. We create here handler for the file.
// Data will be written in class file of tar.
$GLOBALS['bfh']['datadir_softperms'] = fopen($data['path'].'/tmp/'.$data['name'].'/datadir_softperms.txt','a');
$GLOBALS['replace']['from']['datadir_softperms'] = $data['path'].'/tmp/'.$data['name'].'/datadir_softperms.txt';
$GLOBALS['replace']['to']['datadir_softperms'] = 'softdatadir/softperms.txt';
//Did it open the File Stream
if(!$GLOBALS['bfh']['datadir_softperms']){
$error['err_dataperm_file'] = 'There were errors while trying to make a file of permissions of the data directory';
backup_clean($data);
softdie('permdatadir');
}
// The directory itself
@fwrite($GLOBALS['bfh']['datadir_softperms'], '/ '.@substr(sprintf('%o', fileperms($data['softdatadir'])), -4)."\n");
}
// Backup the Web directory as well
if(!empty($data['backup_wwwdir']) && !empty($data['wwwdir']) && $GLOBALS['backup_status'] < BACKUP_WWWDIR){
// Store the progress
//soft_progress($data['ssk'], 90, $l['backingup_datadir']);
$GLOBALS['progress'][4] = 90;
// Adding the directory in $f_list to add to tar
$f_list[] = $data['wwwdir'].'/';
$f_list['BACKUP_WWWDIR'] = '';
$GLOBALS['replace']['from']['wwwdir'] = $data['wwwdir'].'/';
$GLOBALS['replace']['to']['wwwdir'] = 'wwwdir/';
// We now create the file for storing file permission of datadir. We create here handler for the file.
// Data will be written in class file of tar.
$GLOBALS['bfh']['wwwdir_softperms'] = fopen($data['path'].'/tmp/'.$data['name'].'/wwwdir_softperms.txt','a');
$GLOBALS['replace']['from']['wwwdir_softperms'] = $data['path'].'/tmp/'.$data['name'].'/wwwdir_softperms.txt';
$GLOBALS['replace']['to']['wwwdir_softperms'] = 'wwwdir/softperms.txt';
//Did it open the File Stream
if(!$GLOBALS['bfh']['wwwdir_softperms']){
$error['err_wwwperm_file'] = 'There were errors while trying to make a file of permissions of the WEB directory';
backup_clean($data);
softdie('permwebdir');
}
// The directory itself
@fwrite($GLOBALS['bfh']['wwwdir_softperms'], '/ '.@substr(sprintf('%o', fileperms($data['wwwdir'])), -4)."\n");
}
// This is done at the end to make sure we have added all possible replace paths before the softpath
if(!empty($data['backup_dir'])){
$GLOBALS['replace']['from']['softpath'] = $data['softpath'].'/';
$GLOBALS['replace']['to']['softpath'] = '';
}
$f_list['BACKUP_SOFT_FILES_3'] = '';
// Now we will have to add the permission file to the end os an array of directory list.
if(!empty($GLOBALS['bfh']['softperms'])){
$f_list[] = $data['path'].'/tmp/'.$data['name'].'/softperms.txt';
}
if(!empty($GLOBALS['bfh']['datadir_softperms'])){
$f_list[] = $data['path'].'/tmp/'.$data['name'].'/datadir_softperms.txt';
}
if(!empty($GLOBALS['bfh']['wwwdir_softperms'])){
$f_list[] = $data['path'].'/tmp/'.$data['name'].'/wwwdir_softperms.txt';
}
if(!empty($GLOBALS['bfh']['softver'])){
$f_list[] = $data['path'].'/tmp/'.$data['name'].'/softver.txt';
}
$f_list['BACKUP_SOFT_FILES_4'] = '';
$f_list['BACKUP_COMPLETE'] = '';
if(empty($error) && !empty($f_list)){
// Set default values
$GLOBALS['start'] = 0;
$GLOBALS['end_file'] = '';
if(!tar_archive($zipfile, $f_list, true)){
backup_clean($data);
$error['backup_dir'] = 'The backup utility could not back up the files.';
@unlink($zipfile);
softdie('failbackup');
}
}
if(!empty($error)){
@unlink($zipfile);
backup_clean($data);
softdie('failbackup');
}
@print_r($error);
// CHMOD it to something Safe
@chmod($zipfile, 0600);
backup_clean($data);
// Is the backup process INCOMPLETE ?
if($GLOBALS['backup_status'] != BACKUP_COMPLETE){
// This is to notify that the backup is in progress and change the tmp folder's mtime
$mtime_file = $data['path'].'/tmp/'.$data['name'].'/'.$data['name'].'.txt';
file_put_contents($mtime_file, '');
unlink($mtime_file);
softdie('INCOMPLETE-'.$GLOBALS['progress'][$GLOBALS['backup_status']], $GLOBALS['end_file']);
}
// Rename the ZIP file
@rename($zipfile, $successfile);
softdie('DONE');