/** ====================================================================================================================================================
  * Create the zip file
  *
  * @return boolean if it works
  */
 public function create_zip($type_backup)
 {
     global $blog_id;
     global $wpdb;
     // We create the folder for the backup files
     $blog_fold = "";
     if (is_multisite()) {
         $blog_fold = $blog_id . "/";
     }
     // Security issue browsing the backups file
     if (!is_dir(WP_CONTENT_DIR . "/sedlex/backup-scheduler/" . $blog_fold)) {
         @mkdir(WP_CONTENT_DIR . "/sedlex/backup-scheduler/" . $blog_fold, 0777, true);
     }
     if (is_file(WP_CONTENT_DIR . "/sedlex/backup-scheduler/" . $blog_fold . ".htaccess")) {
         @unlink(WP_CONTENT_DIR . "/sedlex/backup-scheduler/" . $blog_fold . ".htaccess");
     }
     if (!is_file(WP_CONTENT_DIR . "/sedlex/backup-scheduler/" . $blog_fold . "index.php")) {
         @file_put_contents(WP_CONTENT_DIR . "/sedlex/backup-scheduler/" . $blog_fold . "index.php", "You are not allowed here!");
         SLFramework_Debug::log(get_class(), "Create the index.php file in the " . WP_CONTENT_DIR . "/sedlex/backup-scheduler/" . $blog_fold . " to avoid any listing of the directory.", 5);
     }
     // Memory limit upgrade
     $current_use = ceil(memory_get_usage() / (1024 * 1024));
     $limit = (int) ini_get('memory_limit');
     if ($current_use + $this->get_param('max_allocated') + 20 >= $limit) {
         @ini_set('memory_limit', sprintf('%dM', $current_use + $this->get_param('max_allocated') + 20));
     }
     // Avoid plurality of processes
     // ----------------------------------------------
     if ($this->get_param('process_running') === true) {
         $state = $this->get_param('process_state');
         $starttime = 0;
         if (isset($state['start'])) {
             $starttime = $state['start'];
         }
         if (time() - $starttime > $this->get_param('max_time') * 3) {
             // on reset ce qui doi 괲e reseter
             if (isset($state['step']) && $state['step'] == 'SQL') {
                 SL_Database::reset(WP_CONTENT_DIR . "/sedlex/backup-scheduler/" . $blog_fold);
             }
             if (isset($state['step']) && $state['step'] == 'ZIP') {
                 SL_Zip::reset(WP_CONTENT_DIR . "/sedlex/backup-scheduler/" . $blog_fold);
             }
         } else {
             return array('finished' => false, 'error' => sprintf(__("Please wait, a backup is in progress for %s seconds! Wait until %s seconds for an automatic restart.", $this->pluginID), time() - $starttime, $this->get_param('max_time') * 3) . "<br/>" . __("This error message may also be generated if the chunk size is too big: try first to set the chunk size to 1Mo in order to avoid any memory saturation of your server and then increase it slowly...", $this->pluginID));
         }
     }
     $this->set_param('process_running', true);
     // Retrieve process parameters
     $state = $this->get_param('process_state');
     if (!is_array($state) || !isset($state['step'])) {
         SL_Database::reset(WP_CONTENT_DIR . "/sedlex/backup-scheduler/" . $blog_fold);
         SL_Zip::reset(WP_CONTENT_DIR . "/sedlex/backup-scheduler/" . $blog_fold);
         // If the state is not set, it means that we just start the processes
         $state = array("rand" => date_i18n("YmdHis") . "_" . SLFramework_Utils::rand_str(10, "abcdefghijklmnopqrstuvwxyz0123456789"), "step" => "SQL");
         $summary['start'] = time();
         $this->set_param('info_process', $summary);
     }
     $state['start'] = time();
     $this->set_param('process_state', $state);
     // STEP SQL
     if ($state['step'] == "SQL") {
         if ($this->get_param('save_db') || $this->get_param('save_db_all')) {
             // We create the SQL file
             if (!is_multisite()) {
                 $sql = new SL_Database();
             } else {
                 if (is_multisite() && $blog_id == 1) {
                     if ($this->get_param('save_db_all')) {
                         if ($this->get_param('save_db_diff_blog')) {
                             $sql = new SL_Database("", $wpdb->prefix);
                         } else {
                             $sql = new SL_Database();
                         }
                     } else {
                         if (is_multisite() && $blog_id == 1 && $this->get_param('save_db_diff_blog')) {
                             $sql = new SL_Database($wpdb->prefix, $wpdb->prefix);
                         } else {
                             $sql = new SL_Database($wpdb->prefix);
                         }
                     }
                 } else {
                     $sql = new SL_Database($wpdb->prefix);
                 }
             }
             $res = $sql->createSQL(WP_CONTENT_DIR . "/sedlex/backup-scheduler/" . $blog_fold . "BackupScheduler" . $this->get_param('add_name') . "_" . $state['rand'], $this->get_param('max_time'), ceil(($this->get_param('max_allocated') - 0.5) * 1024 * 1024));
             // We remove 0.5Mo to ensure that the sql file will be included in the backup
             // Check if the step should be modified
             if ($res['finished'] == true) {
                 SLFramework_Debug::log(get_class(), "SQL extraction finished", 4);
                 $state['step'] = "ZIP";
                 $this->set_param('process_state', $state);
                 $state['sqlfile'] = $res['path'];
                 $this->set_param('process_state', $state);
                 $summary = $this->get_param('info_process');
                 $summary['sql']['total_entries'] = $res['nb_finished'];
                 $summary['sql']['end'] = time();
                 $summary['sql']['start'] = $res['start'];
                 $summary['sql']['files'] = $state['sqlfile'];
                 $this->set_param('info_process', $summary);
                 return array('text' => __('(SQL extraction - ending)', $this->pluginID));
             } else {
                 $res['text'] = ' ' . __('(SQL extraction)', $this->pluginID);
                 return $res;
             }
         } else {
             // Nothing should be done, thus we go directly at the next step
             $state['step'] = "ZIP";
             $this->set_param('process_state', $state);
             return array('text' => __('(SQL extraction - nothing to be done)', $this->pluginID));
         }
         // STEP ZIP
     } else {
         if ($state['step'] == "ZIP") {
             $z = new SL_Zip();
             $ip = SL_Zip::is_inProgress(WP_CONTENT_DIR . "/sedlex/backup-scheduler/" . $blog_fold);
             if ($ip['step'] == "nothing") {
                 $summary = $this->get_param('info_process');
                 $summary['zip']['start'] = time();
                 $this->set_param('info_process', $summary);
                 $upload_dir = wp_upload_dir();
                 $upload_dir = $upload_dir['basedir'] . "/";
                 if ((is_multisite() && $blog_id == 1 || !is_multisite()) && $this->get_param('save_all')) {
                     SLFramework_Debug::log(get_class(), "ZIP backup of " . ABSPATH, 4);
                     $exclu = explode("\n", $this->get_param('exclude_folder'));
                     $exclu = array_map('trim', $exclu);
                     $exclu[] = WP_CONTENT_DIR . "/sedlex";
                     $z->addDir(ABSPATH, ABSPATH, "backup_" . date_i18n("Ymd") . "/", $exclu);
                 } else {
                     if ((is_multisite() && $blog_id == 1 || !is_multisite()) && $this->get_param('save_plugin')) {
                         SLFramework_Debug::log(get_class(), "ZIP backup of " . WP_CONTENT_DIR . "/plugins/", 4);
                         $exclu = explode("\n", $this->get_param('exclude_folder'));
                         $exclu = array_map('trim', $exclu);
                         $z->addDir(WP_CONTENT_DIR . "/plugins/", WP_CONTENT_DIR . "/", "backup_" . date_i18n("Ymd") . "/", $exclu);
                     }
                     if ((is_multisite() && $blog_id == 1 || !is_multisite()) && $this->get_param('save_theme')) {
                         SLFramework_Debug::log(get_class(), "ZIP backup of " . WP_CONTENT_DIR . "/themes/", 4);
                         $exclu = explode("\n", $this->get_param('exclude_folder'));
                         $exclu = array_map('trim', $exclu);
                         $z->addDir(WP_CONTENT_DIR . "/themes/", WP_CONTENT_DIR . "/", "backup_" . date_i18n("Ymd") . "/", $exclu);
                     }
                     if (!is_multisite() && $this->get_param('save_upload')) {
                         $upload_dir = wp_upload_dir();
                         $upload_dir = $upload_dir['basedir'] . "/";
                         SLFramework_Debug::log(get_class(), "ZIP backup of " . $upload_dir, 4);
                         $exclu = explode("\n", $this->get_param('exclude_folder'));
                         $exclu = array_map('trim', $exclu);
                         $z->addDir($upload_dir, WP_CONTENT_DIR . "/", "backup_" . date_i18n("Ymd") . "/", $exclu);
                     }
                     if (is_multisite() && $this->get_param('save_upload')) {
                         // blogs.dir n'est plus utilisé pour les nouveaux blogs MU
                         if (is_dir(WP_CONTENT_DIR . "/blogs.dir/" . $blog_id)) {
                             SLFramework_Debug::log(get_class(), "ZIP backup of " . WP_CONTENT_DIR . "/blogs.dir/" . $blog_id . "/", 4);
                             $exclu = explode("\n", $this->get_param('exclude_folder'));
                             $exclu = array_map('trim', $exclu);
                             $z->addDir(WP_CONTENT_DIR . "/blogs.dir/" . $blog_id . "/", WP_CONTENT_DIR . "/", "backup_" . date_i18n("Ymd") . "/", $exclu);
                         }
                         if ($blog_id != 1) {
                             SLFramework_Debug::log(get_class(), "ZIP backup of " . $upload_dir, 4);
                             $exclu = explode("\n", $this->get_param('exclude_folder'));
                             $exclu = array_map('trim', $exclu);
                             $z->addDir($upload_dir, WP_CONTENT_DIR . "/", "backup_" . date_i18n("Ymd") . "/", $exclu);
                         } else {
                             $root = scandir($upload_dir);
                             foreach ($root as $value) {
                                 if ($value !== "sites" && $value !== "." && $value !== "..") {
                                     SLFramework_Debug::log(get_class(), "ZIP backup of " . $upload_dir . $value . "/", 4);
                                     $exclu = explode("\n", $this->get_param('exclude_folder'));
                                     $exclu = array_map('trim', $exclu);
                                     $z->addDir($upload_dir . $value . "/", WP_CONTENT_DIR . "/", "backup_" . date_i18n("Ymd") . "/", $exclu);
                                 }
                             }
                         }
                     }
                     if (is_multisite() && $blog_id == 1 && $this->get_param('save_upload_all')) {
                         // blogs.dir n'est plus utilisé pour les nouveaux blogs MU
                         $exclu = explode("\n", $this->get_param('exclude_folder'));
                         $exclu = array_map('trim', $exclu);
                         if (is_dir(WP_CONTENT_DIR . "/blogs.dir/")) {
                             SLFramework_Debug::log(get_class(), "ZIP backup of " . WP_CONTENT_DIR . "/blogs.dir/" . $blog_id . "/", 4);
                             $z->addDir(WP_CONTENT_DIR . "/blogs.dir/", WP_CONTENT_DIR . "/", "backup_" . date_i18n("Ymd") . "/", $exclu);
                         }
                         SLFramework_Debug::log(get_class(), "ZIP backup of " . $upload_dir, 4);
                         $z->addDir($upload_dir, $upload_dir, "backup_" . date_i18n("Ymd") . "/", $exclu);
                     }
                     if (is_multisite() && $blog_id == 1 || !is_multisite()) {
                         SLFramework_Debug::log(get_class(), "ZIP backup of " . ABSPATH . "/wp-config.php", 4);
                         $z->addFile(ABSPATH . "/wp-config.php", ABSPATH, "backup_" . date_i18n("Ymd") . "/");
                     }
                 }
                 if ($this->get_param('save_db') || $this->get_param('save_db_all')) {
                     foreach ($state['sqlfile'] as $f => $t) {
                         $z->addFile($f, WP_CONTENT_DIR . "/sedlex/backup-scheduler/" . $blog_fold, "backup_" . date_i18n("Ymd") . "/");
                         SLFramework_Debug::log(get_class(), "ZIP backup of " . $f, 4);
                     }
                 }
             }
             $path = $z->createZip(WP_CONTENT_DIR . "/sedlex/backup-scheduler/" . $blog_fold . "BackupScheduler" . $this->get_param('add_name') . "_" . $state['rand'] . ".zip", $this->get_param('chunk') * 1024 * 1024, $this->get_param('max_time'), $this->get_param('max_allocated') * 1024 * 1024);
             // Check if the step should be modified
             if ($path['finished'] == true) {
                 //new log
                 $summary = $this->get_param('info_process');
                 $summary['zip']['end'] = time();
                 $summary['zip']['total_entries'] = $path['nb_finished'];
                 $summary['zip']['excluded_entries'] = $path['not_included'];
                 $summary['zip']['files'] = $path['path'];
                 $this->set_param('info_process', $summary);
                 $state['step'] = "FTP";
                 $this->set_param('process_state', $state);
                 $files_to_sent = $path['path'];
                 // Reset this variable to avoid any conflicts
                 $this->set_param('ftp_to_be_sent', $files_to_sent);
                 $this->set_param('mail_to_be_sent', $files_to_sent);
                 $this->set_param('ftp_sent', array());
                 $this->set_param('mail_sent', array());
             }
             $path['text'] = ' ' . __('(ZIP creation)', $this->pluginID);
             return $path;
             // STEP FTP
         } else {
             if ($state['step'] == "FTP") {
                 if ($this->get_param('ftp') && $type_backup == "external") {
                     // On envoie le premier fichier en FTP
                     $files_to_sent = $this->get_param('ftp_to_be_sent');
                     $files_sent = $this->get_param('ftp_sent');
                     $file_to_sent = array_pop($files_to_sent);
                     //new log
                     $summary = $this->get_param('info_process');
                     if (is_file($file_to_sent)) {
                         SLFramework_Debug::log(get_class(), "FTP file to be sent: " . $file_to_sent, 4);
                         file_put_contents($file_to_sent . ".ftp.progress", "in progress");
                         $res = $this->sendFTP(array($file_to_sent));
                         if ($res['transfer']) {
                             $res['text'] = ' ' . __('(FTP sending)', $this->pluginID);
                             $res['nb_finished'] = count($files_sent);
                             $res['nb_to_finished'] = count($files_to_sent);
                             array_push($files_sent, $file_to_sent);
                             // Store result
                             $temp_truc = array('file' => $file_to_sent, 'date' => time(), 'error' => false, 'error_msg' => '');
                             // On supprime le fichier d'attente
                             @unlink($file_to_sent . ".ftp.progress");
                             file_put_contents($file_to_sent . ".ftp.ok", "ok");
                             $summary['ftp'][] = $temp_truc;
                             $this->set_param('info_process', $summary);
                         } else {
                             array_push($files_sent, $res['error'] . ": " . $file_to_sent);
                             // Store result
                             $temp_truc = array('file' => $file_to_sent, 'date' => time(), 'error' => true, 'error_msg' => $res['error']);
                             // On supprime le fichier d'attente
                             @unlink($file_to_sent . ".ftp.progress");
                             file_put_contents($file_to_sent . ".ftp.ko", $res['error'] . " ");
                             $summary['ftp'][] = $temp_truc;
                             $this->set_param('info_process', $summary);
                         }
                         // Mise a jour
                         $this->set_param('ftp_to_be_sent', $files_to_sent);
                         $this->set_param('ftp_sent', $files_sent);
                         return $res;
                     } else {
                         // Mise a jour
                         $this->set_param('ftp_to_be_sent', $files_to_sent);
                         $this->set_param('ftp_sent', $files_sent);
                         $state['step'] = "MAIL";
                         $this->set_param('process_state', $state);
                         return array('text' => __('(FTP sending - ending)', $this->pluginID));
                     }
                 } else {
                     // Nothing should be done, thus we go directly at the next step
                     $state['step'] = "MAIL";
                     $this->set_param('process_state', $state);
                     return array('text' => __('(FTP sending - nothing to be done)', $this->pluginID));
                 }
                 // STEP MAIL
             } else {
                 if ($state['step'] == "MAIL") {
                     if ($this->get_param('email_check') && $type_backup == "external") {
                         // On envoie le premier fichier en mail
                         $files_to_sent = $this->get_param('mail_to_be_sent');
                         $files_sent = $this->get_param('mail_sent');
                         $file_to_sent = array_pop($files_to_sent);
                         //new log
                         $summary = $this->get_param('info_process');
                         $temp_truc = array('file' => $file_to_sent, 'date' => time());
                         $summary['mail'][] = $temp_truc;
                         $this->set_param('info_process', $summary);
                         if (is_file($file_to_sent)) {
                             SLFramework_Debug::log(get_class(), "Email the backup file: " . $file_to_sent, 4);
                             $subject = sprintf(__("Backup of %s on %s (%s)", $this->pluginID), get_bloginfo('name'), date_i18n('Y-m-d'), count($files_sent) . "/" . (count($files_to_sent) + count($files_sent)));
                             $res = $this->sendEmail(array($file_to_sent), $subject);
                             if ($res === true) {
                                 $path['text'] = ' ' . __('(MAIL sending)', $this->pluginID);
                                 $path['nb_finished'] = count($files_sent);
                                 $path['nb_to_finished'] = count($files_to_sent);
                                 SLFramework_Debug::log(get_class(), "Email sent.", 4);
                                 array_push($files_sent, $file_to_sent);
                             } else {
                                 SLFramework_Debug::log(get_class(), "Email failed to be sent.", 2);
                                 $path['error'] = __("Your Wordpress installation cannot send emails (with heavy attachments)!", $this->pluginID);
                                 array_push($files_sent, __("Your Wordpress installation cannot send emails (with heavy attachments)!", $this->pluginID) . ": " . $file_to_sent);
                             }
                             // Mise a jour
                             $this->set_param('mail_to_be_sent', $files_to_sent);
                             $this->set_param('mail_sent', $files_sent);
                             return $path;
                         } else {
                             // Mise a jour
                             $this->set_param('mail_to_be_sent', $files_to_sent);
                             $this->set_param('mail_sent', $files_sent);
                             $state['step'] = "END";
                             $this->set_param('process_state', $state);
                             return array('text' => __('(MAIL sending - ending)', $this->pluginID));
                         }
                     } else {
                         // Nothing should be done, thus we go directly at the next step
                         $state['step'] = "END";
                         $this->set_param('process_state', $state);
                         return array('text' => __('(MAIL sending - nothing to be done)', $this->pluginID));
                     }
                     // STEP END
                 } else {
                     if ($state['step'] == "END") {
                         SLFramework_Debug::log(get_class(), "Email to summarize the backup process.", 4);
                         //new log
                         $summary = $this->get_param('info_process');
                         $summary['end'] = time();
                         $this->set_param('info_process', $summary);
                         $this->sendSummaryEmail();
                         // We delete the possible SQL file and config file
                         $num_i = 1;
                         while (true) {
                             if (is_file(WP_CONTENT_DIR . "/sedlex/backup-scheduler/" . $blog_fold . "BackupScheduler" . $this->get_param('add_name') . "_" . $state['rand'] . ".sql" . $num_i)) {
                                 @unlink(WP_CONTENT_DIR . "/sedlex/backup-scheduler/" . $blog_fold . "BackupScheduler" . $this->get_param('add_name') . "_" . $state['rand'] . ".sql" . $num_i);
                                 SLFramework_Debug::log(get_class(), "SQL file is deleted: " . WP_CONTENT_DIR . "/sedlex/backup-scheduler/" . $blog_fold . "BackupScheduler" . $this->get_param('add_name') . "_" . $state['rand'] . ".sql" . $num_i, 4);
                                 $num_i++;
                             } else {
                                 break;
                             }
                         }
                         @file_put_contents(WP_CONTENT_DIR . "/sedlex/backup-scheduler/" . $blog_fold . "last_backup", date_i18n("Y-m-d"));
                         $state = array();
                         $this->set_param('process_state', $state);
                         return array('text' => __('(END - ending)', $this->pluginID));
                     }
                 }
             }
         }
     }
     SLFramework_Debug::log(get_class(), "An unknown error occured!", 2);
     return array('finished' => false, 'error' => __("An unknown error occured!", $this->pluginID));
 }
Esempio n. 2
0
 /** ====================================================================================================================================================
  * To add an external link in the 
  * 
  * @return the short_url
  */
 function add_external_link($link, $comment)
 {
     global $wpdb;
     $table_name = $this->table_name;
     // We add the shortlink
     $car_minus = $this->get_param('low_char');
     $car_maxus = $this->get_param('upp_char');
     $car_nombr = $this->get_param('num_char');
     $car_longu = $this->get_param('length');
     $temp_url = "";
     $url_ext = addslashes(str_replace("'", "", $link));
     $comment = addslashes(str_replace("'", "", $comment));
     $char = ($car_maxus ? "ABCDEFGHIJKLMNOPQRSTUVWXYZ" : "") . ($car_minus ? "abcdefghijklmnopqrstuvwxyz" : "") . ($car_nombr ? "1234567890" : "");
     $ok = false;
     // Search for existing short link
     $short = $wpdb->get_var("SELECT short_url FROM {$table_name} WHERE id_post=0 AND url_externe='" . $url_ext . "'");
     if ($short != "") {
         return $short;
     } else {
         while (!$ok) {
             $result = $this->get_param('prefix') . SLFramework_Utils::rand_str($car_longu, $char);
             $select = "SELECT id_post FROM {$table_name} WHERE short_url='" . $result . "'";
             $temp_id = $wpdb->get_var($select);
             if ($temp_id == null || $temp_id === false || !is_numeric($temp_id)) {
                 $ok = true;
                 $sql = "DELETE FROM {$table_name} WHERE url_externe='" . $url_ext . "'";
                 $wpdb->query($sql);
                 $sql = "INSERT INTO {$table_name} (id_post, short_url, url_externe, comment) VALUES ('0', '" . $result . "', '" . $url_ext . "', '" . $comment . "')";
                 $wpdb->query($sql);
                 return $result;
             }
         }
     }
 }