function resursiveZip($directory) { try { $folderPath = duplicator_safe_path($directory); if (!($dh = opendir($folderPath))) { return false; } //EXCLUDE: Snapshot directory if (strstr($folderPath, DUPLICATOR_SSDIR_PATH)) { return; } //EXCLUDE: Directory Exclusions List if ($GLOBALS['duplicator_bypass-array'] != null) { foreach ($GLOBALS['duplicator_bypass-array'] as $val) { if (duplicator_safe_path($val) == $folderPath) { duplicator_log("path filter found: {$val}", 2); return; } } } /* Legacy: Excludes empty diretories and files with no extention while (false !== ($file = @readdir($dh))) { if ($file != '.' && $file != '..') { $fullpath = "{$folderPath}/{$file}"; if(is_dir($fullpath)) { duplicator_fcgi_flush(); $this->resursiveZip($fullpath); } else if(is_file($fullpath) && is_readable($fullpath)) { //Check filter extensions if(!in_array(@pathinfo($fullpath, PATHINFO_EXTENSION), $this->skipNames)) { $localpath = str_replace($this->rootFolder, '', $folderPath); $localname = empty($localpath) ? '' : ltrim("{$localpath}/", '/'); $this->zipArchive->addFile("{$folderPath}/{$file}", "{$localname}{$file}"); } } $this->limitItems++; } } */ while (false !== ($file = @readdir($dh))) { if ($file != '.' && $file != '..') { $fullpath = "{$folderPath}/{$file}"; $localpath = str_replace($this->rootFolder, '', $folderPath); $localname = empty($localpath) ? '' : ltrim("{$localpath}/", '/'); if (is_dir($fullpath)) { duplicator_fcgi_flush(); $this->zipArchive->addEmptyDir("{$localname}{$file}"); $this->resursiveZip($fullpath); } else { if (is_file($fullpath) && is_readable($fullpath)) { //Check filter extensions $ext = @pathinfo($fullpath, PATHINFO_EXTENSION); if ($ext == '' || !in_array($ext, $this->skipNames)) { $this->zipArchive->addFile("{$folderPath}/{$file}", "{$localname}{$file}"); } } } $this->limitItems++; } } //Check if were over our count if ($this->limitItems > $this->limit) { duplicator_log("log:class.zip=>new open handle {$this->zipArchive->numFiles}"); $this->zipArchive->close(); $this->zipArchive->open($this->zipFilePath, ZIPARCHIVE::CREATE); $this->limitItems = 0; duplicator_fcgi_flush(); } closedir($dh); } catch (Exception $e) { duplicator_log("log:class.zip=>runtime error: " . $e); } }
/** * DUPLICATOR_CREATE_DBSCRIPT * Create the SQL DataDump File * @param string $destination The full path and filname where the sql script will be written */ function duplicator_create_dbscript($destination) { try { global $wpdb; $time_start = DuplicatorUtils::GetMicrotime(); $handle = fopen($destination, 'w+'); $tables = $wpdb->get_col('SHOW TABLES'); $sql_header = "/* DUPLICATOR MYSQL SCRIPT CREATED ON : " . @date("F j, Y, g:i a") . " */\n\n"; $sql_header .= "SET FOREIGN_KEY_CHECKS = 0;\n\n"; @fwrite($handle, $sql_header); //BUILD CREATES: //All creates must be created before inserts do to foreign key constraints foreach ($tables as $table) { //$sql_del = ($GLOBALS['duplicator_opts']['dbadd_drop']) ? "DROP TABLE IF EXISTS {$table};\n\n" : ""; //@fwrite($handle, $sql_del); $create = $wpdb->get_row("SHOW CREATE TABLE `{$table}`", ARRAY_N); @fwrite($handle, "{$create[1]};\n\n"); } //BUILD INSERTS: //Create Insert in 100 row increments to better handle memory foreach ($tables as $table) { $row_count = $wpdb->get_var("SELECT Count(*) FROM `{$table}`"); duplicator_log("{$table} ({$row_count})"); if ($row_count > 100) { $row_count = ceil($row_count / 100); } else { if ($row_count > 0) { $row_count = 1; } } if ($row_count >= 1) { @fwrite($handle, "\n/* INSERT TABLE DATA: {$table} */\n"); } for ($i = 0; $i < $row_count; $i++) { $sql = ""; $limit = $i * 100; $query = "SELECT * FROM `{$table}` LIMIT {$limit}, 100"; $rows = $wpdb->get_results($query, ARRAY_A); if (is_array($rows)) { foreach ($rows as $row) { $sql .= "INSERT INTO `{$table}` VALUES("; $num_values = count($row); $num_counter = 1; foreach ($row as $value) { if (is_null($value) || !isset($value)) { $num_values == $num_counter ? $sql .= 'NULL' : ($sql .= 'NULL, '); } else { $num_values == $num_counter ? $sql .= '"' . @mysql_real_escape_string($value) . '"' : ($sql .= '"' . @mysql_real_escape_string($value) . '", '); } $num_counter++; } $sql .= ");\n"; } @fwrite($handle, $sql); duplicator_fcgi_flush(); } } } unset($sql); $sql_footer = "\nSET FOREIGN_KEY_CHECKS = 1;"; @fwrite($handle, $sql_footer); duplicator_log("SQL CREATED: {$destination}"); fclose($handle); $wpdb->flush(); $time_end = DuplicatorUtils::GetMicrotime(); $time_sum = DuplicatorUtils::ElapsedTime($time_end, $time_start); $sql_file_size = filesize($destination); if ($sql_file_size <= 0) { duplicator_error("ERROR: SQL file generated zero bytes. \nERROR INFO: No data was written to the sql file. Check permission on file and parent directory at [{$destination}]"); } duplicator_log("SQL FILE SIZE: " . duplicator_bytesize($sql_file_size)); duplicator_log("SQL RUNTIME: {$time_sum}"); } catch (Exception $e) { duplicator_error("ERROR: Runtime error in duplicator_create_dbscript \nERROR INFO: '{$e}'"); } }
/** * DUPLICATOR ZIP * Creates the zip file * * @param string $zipFilePath The full path to the zip file that will be made * @param string $folderPath The folder that will be zipped * @param string $sqlfilepath The path to the database file to include in the package */ function __construct($zipFilePath, $folderPath, $sqlfilepath) { try { $total_time_start = DuplicatorUtils::GetMicrotime(); duplicator_log("PACKAGE DIR: {$folderPath}"); duplicator_log("PACKAGE FILE: {$zipFilePath}"); $this->zipArchive = new ZipArchive(); $this->zipFilePath = duplicator_safe_path($zipFilePath); $this->rootFolder = rtrim(duplicator_safe_path($folderPath), '/'); $this->skipNames = $GLOBALS['duplicator_skip_ext-array']; $this->countDirs = 0; $this->countFiles = 0; $this->countLinks = 0; $exts_list = implode(";", $this->skipNames); $path_list = implode(";", $GLOBALS['duplicator_bypass-array']); $this->fileExtActive = strlen($exts_list); duplicator_log("FILTER EXTENSIONS: '{$exts_list}'"); duplicator_log("FILTER DIRECTORIES: '{$path_list}'"); duplicator_log($GLOBALS['DUPLICATOR_SEPERATOR2']); //CREATE ZIP FILE if ($this->zipArchive->open($this->zipFilePath, ZIPARCHIVE::CREATE) === TRUE) { duplicator_log("STARTING PACKAGE BUILD"); } else { duplicator_error("ERROR: Cannot open zip file with PHP ZipArchive. \nERROR INFO: Path location [{$this->zipFilePath}]"); } //ADD SQL File $sql_in_zip = $this->zipArchive->addFile($sqlfilepath, "/database.sql"); if ($sql_in_zip) { duplicator_log("ADDED=>SQL: {$sqlfilepath}"); } else { duplicator_error("ERROR: Unable to add database.sql file to package from. \nERROR INFO: SQL File Path [{$sqlfilepath}]"); } //RECURSIVE CALL TO ALL FILES $list_time_start = DuplicatorUtils::GetMicrotime(); duplicator_log("BUILDING FILE LIST"); $this->resursiveZip($this->rootFolder); $list_time_end = DuplicatorUtils::GetMicrotime(); $list_time_sum = DuplicatorUtils::ElapsedTime($list_time_end, $list_time_start); duplicator_log("FILE LIST COMPLETE: {$list_time_sum}"); duplicator_log("FILE LIST STATS: Dirs {$this->countDirs} | Files {$this->countFiles} | Links {$this->countLinks} | "); duplicator_log("\nPACKAGE INFO: " . print_r($this->zipArchive, true)); //LOG FINAL RESULTS duplicator_log("CREATING PACKAGE"); duplicator_fcgi_flush(); @set_time_limit(0); $zip_close_result = $this->zipArchive->close(); if ($zip_close_result) { duplicator_log("CLOSING PACKAGE RESULT: '{$zip_close_result}'"); } else { $err_info = 'This server or hosted segement might have a disk quota limit.\\nPlease check your disk space usage to make sure you can store this zip file successfully.'; duplicator_error("ERROR: ZipArchive Class did not close successfully. \nERROR INFO: {$err_info}"); } $total_time_end = DuplicatorUtils::GetMicrotime(); $total_time_sum = DuplicatorUtils::ElapsedTime($total_time_end, $total_time_start); $this->zipFileSize = @filesize($this->zipFilePath); duplicator_log("PACKAGE FILE SIZE: " . duplicator_bytesize($this->zipFileSize)); duplicator_log("PACKAGE RUNTIME: {$total_time_sum}"); } catch (Exception $e) { duplicator_error("ERROR: Runtime error in class.zip.php constructor. \nERROR INFO: {$e}"); } }
/** * DUPLICATOR_CREATE_DBSCRIPT * Create the SQL DataDump File * @param string $destination The full path and filname where the sql script will be written */ function duplicator_create_dbscript($destination) { try { global $wpdb; //$dbiconv = ($GLOBALS['duplicator_opts']['dbiconv'] == "0" && function_exists("iconv")) ? false : true; $handle = fopen($destination, 'w+'); $tables = $wpdb->get_col('SHOW TABLES'); duplicator_log("log:fun__create_dbscript=>started"); //if ($dbiconv) { //duplicator_log("log:fun__create_dbscript=>dbiconv enabled"); //} foreach ($tables as $table) { //Generate Drop Statement //$sql_del = ($GLOBALS['duplicator_opts']['dbadd_drop']) ? "DROP TABLE IF EXISTS {$table};\n\n" : ""; //@fwrite($handle, $sql_del); //Generate Create Statement $row_count = $wpdb->get_var("SELECT Count(*) FROM `{$table}`"); duplicator_log("start: {$table} ({$row_count})"); $create = $wpdb->get_row("SHOW CREATE TABLE `{$table}`", ARRAY_N); $sql_crt = "{$create[1]};\n\n"; @fwrite($handle, $sql_crt); if ($row_count > 100) { $row_count = ceil($row_count / 100); } else { if ($row_count > 0) { $row_count = 1; } } //PERFORM ICONV ROUTINE //Chunck the query results to avoid memory issues /*if ($dbiconv) { for ($i = 0; $i < $row_count; $i++) { $sql = ""; $limit = $i * 100; $query = "SELECT * FROM `{$table}` LIMIT {$limit}, 100"; $rows = $wpdb->get_results($query, ARRAY_A); if (is_array($rows)) { foreach ($rows as $row) { $sql .= "INSERT INTO `{$table}` VALUES("; $num_values = count($row); $num_counter = 1; foreach ($row as $value) { $value = @iconv(DUPLICATOR_DB_ICONV_IN, DUPLICATOR_DB_ICONV_OUT, $value); ($num_values == $num_counter) ? $sql .= '"' . @mysql_real_escape_string($value) . '"' : $sql .= '"' . @mysql_real_escape_string($value) . '", '; $num_counter++; } $sql .= ");\n"; } @fwrite($handle, $sql); duplicator_fcgi_flush(); } } //DO NOT PERFORM ICONV } else {*/ for ($i = 0; $i < $row_count; $i++) { $sql = ""; $limit = $i * 100; $query = "SELECT * FROM `{$table}` LIMIT {$limit}, 100"; $rows = $wpdb->get_results($query, ARRAY_A); if (is_array($rows)) { foreach ($rows as $row) { $sql .= "INSERT INTO `{$table}` VALUES("; $num_values = count($row); $num_counter = 1; foreach ($row as $value) { $num_values == $num_counter ? $sql .= '"' . @mysql_real_escape_string($value) . '"' : ($sql .= '"' . @mysql_real_escape_string($value) . '", '); $num_counter++; } $sql .= ");\n"; } @fwrite($handle, $sql); duplicator_fcgi_flush(); } } //} @fwrite($handle, "\n\n"); duplicator_log("done: {$table}"); } duplicator_log("log:fun__create_dbscript=>sql file written to {$destination}"); fclose($handle); $wpdb->flush(); duplicator_log("log:fun__create_dbscript=>ended"); } catch (Exception $e) { duplicator_log("log:fun__create_dbscript=>runtime error: " . $e); } }
function resursiveZip($directory) { try { $folderPath = duplicator_safe_path($directory); //EXCLUDE: Snapshot directory if (strstr($folderPath, DUPLICATOR_SSDIR_PATH) || empty($folderPath)) { return; } //EXCLUDE: Directory Exclusions List if (is_array($GLOBALS['duplicator_bypass-array'])) { foreach ($GLOBALS['duplicator_bypass-array'] as $val) { if (duplicator_safe_path($val) == $folderPath) { duplicator_log("path filter found: {$val}", 2); return; } } } $dh = new DirectoryIterator($folderPath); foreach ($dh as $file) { if (!$file->isDot()) { $fullpath = "{$folderPath}/{$file}"; $localpath = str_replace($this->rootFolder, '', $folderPath); $localname = empty($localpath) ? '' : ltrim("{$localpath}/", '/'); $filename = $file->getFilename(); if ($file->isDir()) { if (!in_array($fullpath, $GLOBALS['duplicator_bypass-array'])) { $this->zipArchive->addEmptyDir("{$localname}{$filename}"); @set_time_limit(0); duplicator_fcgi_flush(); } $this->resursiveZip($fullpath); } else { //Check filter extensions $ext = @pathinfo($fullpath, PATHINFO_EXTENSION); if ($ext == '' || !in_array($ext, $this->skipNames)) { $this->zipArchive->addFile("{$folderPath}/{$filename}", "{$localname}{$filename}"); } } $this->limitItems++; } } //Check if were over our count if ($this->limitItems > $this->limit) { duplicator_log("log:class.zip=>new open handle {$this->zipArchive->numFiles}"); $this->zipArchive->close(); $this->zipArchive->open($this->zipFilePath, ZIPARCHIVE::CREATE); $this->limitItems = 0; duplicator_fcgi_flush(); } @closedir($dh); } catch (Exception $e) { duplicator_log("log:class.zip=>runtime error: " . $e); } }