public function append_zip(ZipArchive $zip, $root, $local_prefix, FW_Backup_Interface_Feedback $feedback, $exclude = array())
 {
     /**
      * @var FW_Extension_Backup $backup
      */
     $feedback->set_task(__('Scanning file system...', 'fw'));
     $fs = new FW_Backup_Helper_File_System();
     $backup = fw()->extensions->get('backup');
     $exclude[] = $backup->get_backup_dir();
     $upload_dir = wp_upload_dir();
     $exclude[] = $upload_dir['basedir'] . '/fw-style.css';
     list($file_list, $file_size) = $fs->file_list_exclude($root, $exclude);
     $feedback->set_task(sprintf(__('%d file(s) found [%s]', 'fw'), count($file_list), $fs->format_bytes(array_sum($file_size))));
     $fs->append_zip($zip, $file_list, $root, $local_prefix, $feedback);
 }
 public function move($file, FW_Backup_Interface_Feedback $feedback, $name_prefix = false)
 {
     if (!$name_prefix) {
         $name_prefix = 'backup';
     }
     $ext = pathinfo($file, PATHINFO_EXTENSION);
     $date = date('Y_m_d-H_i_s', filemtime($file));
     // Make name guessing a little bit harder
     $random = bin2hex(fw_secure_rand(6));
     $target = $this->get_backup_dir() . DIRECTORY_SEPARATOR . "{$name_prefix}-{$date}-{$random}.{$ext}";
     $feedback->set_task(sprintf(__('Storing as %s', 'fw'), $target));
     if (!@rename($file, $target)) {
         $error = error_get_last();
         throw new FW_Backup_Exception(sprintf(__('rename(%s, %s) failed with message "%s"', 'fw'), $file, $target, $error['message']));
     }
     return new FW_Backup_Storage_File_Local(realpath($target));
 }
 public function export(FW_Backup_Interface_Feedback $feedback)
 {
     $time_start = microtime(true);
     $time_end = microtime(true) + $this->seconds;
     $sleep = 0.1;
     // Do not localize task names, they are used for debugging
     $feedback->set_task('Debugging Export Layer');
     while (microtime(true) < $time_end) {
         usleep($sleep * 1000000);
         $total = $time_end - $time_start;
         $complete = microtime(true) - $time_start;
         $feedback->set_progress(min(100, number_format($complete / $total * 100)) . '% -- ' . number_format($complete, 2) . 'sec');
     }
     $feedback->set_progress('100%');
     $feedback->set_task('Debugging Export Layer Done');
     $filename = sprintf('%s/backup-debug-%s.txt', get_temp_dir(), date('Y_m_d-H_i_s'));
     file_put_contents($filename, __FILE__);
     return $filename;
 }
 public function append_zip(ZipArchive $zip, $file_list, $root, $local_prefix, FW_Backup_Interface_Feedback $feedback)
 {
     $feedback->set_task(__('Adding files to archive...', 'fw'), count($file_list));
     foreach ($file_list as $index => $file) {
         $local_name = substr($file, strlen($root));
         // $root = /var/www
         // $file = /var/www/index.php
         // $local_name = /index.php
         $local_name = ltrim($local_name, DIRECTORY_SEPARATOR);
         $local_name = $local_prefix . $local_name;
         // fix for Windows
         $local_name = str_replace('\\', '/', $local_name);
         if (is_dir($file)) {
             $zip->addEmptyDir($local_name);
         } else {
             if (is_readable($file)) {
                 $zip->addFile($file, $local_name);
             } else {
                 // $log->append("error: could not read file $file");
             }
         }
         $feedback->set_progress($index);
     }
 }
 public function export_sql(FW_Backup_Interface_Feedback $feedback, $options_where = "WHERE option_name NOT LIKE 'fw_backup.%%'", $exclude_tables = array())
 {
     /**
      * @var FW_Extension_Backup $backup
      */
     global $wpdb, $table_prefix;
     $db = new FW_Backup_Helper_Database();
     $backup = fw()->extensions->get('backup');
     $filename = sprintf('%s/backup-database-%s.sql', get_temp_dir(), date('Y_m_d-H_i_s'));
     $fp = fopen($filename, 'w');
     $feedback->set_task(__('Querying database...', 'fw'));
     list($table_list, $view_list) = $db->query_schema();
     $feedback->set_task(sprintf(__('%d tables found', 'fw'), count($table_list)));
     $feedback->set_task(sprintf(__('%d views found', 'fw'), count($view_list)));
     $create_table_list = array_map(array($db, 'show_create_table'), $table_list);
     $create_view_list = array_map(array($db, 'show_create_view'), $view_list);
     $feedback->set_task(__('Writing meta info...', 'fw'));
     // After all headers should come empty line. This is necessary
     // for be able to run the following code with expected result:
     //
     //     $headers = $this->get_headers_fp()
     //     $db->foreach_statement($fp, FW_Backup_Callable::make(array($this, '_query'), $headers, $db));
     //
     fwrite($fp, '-- date: ' . current_time('mysql') . PHP_EOL);
     fwrite($fp, '-- table_prefix: ' . $table_prefix . PHP_EOL);
     fwrite($fp, PHP_EOL . PHP_EOL);
     $a = array_combine($table_list, $create_table_list);
     $a = array_diff_key($a, array_flip($exclude_tables));
     $feedback->set_task(__('Dumping tables...', 'fw'), count($a));
     $first = true;
     $index = 0;
     foreach ($a as $table => $create_table) {
         $feedback->set_progress($index, $table);
         fwrite($fp, $first ? $first = false : PHP_EOL . PHP_EOL);
         fwrite($fp, $db->close_mysql_statement($create_table));
         // get rid of backup history as well as backup settings
         switch ($table) {
             case $wpdb->posts:
                 $query = sprintf('SELECT * FROM %s WHERE post_type != %s', $db->escape_mysql_identifier($table), $db->escape_mysql_value($backup->post_type()->get_post_type()));
                 break;
             case $wpdb->postmeta:
                 $query = sprintf('SELECT a.* FROM %s AS a INNER JOIN %s AS b WHERE a.post_id = b.ID AND b.post_type != %s', $db->escape_mysql_identifier($table), $db->escape_mysql_identifier($wpdb->posts), $db->escape_mysql_value($backup->post_type()->get_post_type()));
                 break;
             case $wpdb->options:
                 $query = sprintf("SELECT * FROM %s {$options_where}", $db->escape_mysql_identifier($table));
                 break;
             default:
                 $query = sprintf('SELECT * FROM %s', $db->escape_mysql_identifier($table));
                 break;
         }
         $db->dump_query($fp, $query, $table);
         $feedback->set_progress(++$index, $table);
     }
     $feedback->set_task(__('Dumping views...', 'fw'));
     foreach ($create_view_list as $create_view) {
         fwrite($fp, $first ? $first = false : PHP_EOL . PHP_EOL);
         fwrite($fp, $db->close_mysql_statement($create_view));
     }
     fclose($fp);
     return $filename;
 }
 public function set_progress(FW_Backup_Interface_Feedback $feedback)
 {
     $this->update('cron.progress', array('task_title' => $feedback->get_task_title(), 'task_progress' => $feedback->get_task_progress(), 'task_progress_title' => $feedback->get_task_progress_title()));
 }