public function backup_items($items)
 {
     $dir = SNS_BACKUPS_PATH . $this->filename;
     if (!class_exists('Zip', false)) {
         require_once SNS_LIB_PATH . 'Zip.php';
     }
     $warns = array('not_readable' => array());
     $zip = new Zip();
     $zip->setZipFile($dir . '.zip');
     self::$zipFile = $zip;
     foreach ($items as $name => $path) {
         if ($name == Sns_Option::DB) {
             $sql_file = SNS_BACKUPS_PATH . 'wp_dump.sql';
             Sns_Log::log_action('Exporting DB');
             Sns_Backup::export_db($sql_file);
             Sns_Log::log_action('Exporting DB', SNS_LOG_END);
             $stream = @fopen($sql_file, 'rb');
             if ($stream) {
                 $zip->addLargeFile($stream, 'wp_dump.sql');
             }
             @unlink($sql_file);
             continue;
         }
         Sns_Log::log_action('Backup item - ' . $name);
         $itemZip = new Zip();
         $itemZip->setZipFile(SNS_BACKUPS_PATH . $name . '.zip');
         $path = realpath($path);
         $iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path, FilesystemIterator::SKIP_DOTS));
         $exclude = array(realpath(SNS_BACKUP_ROOT), realpath(SNS_BACKUPS_PATH), realpath(WP_CONTENT_DIR . SNS_DS . 'debug.log'));
         for ($iterator->rewind(); $iterator->valid(); $iterator->next()) {
             $file = $iterator->current();
             $continue = false;
             foreach ($exclude as $excludeFile) {
                 if (strpos($file, $excludeFile) !== false) {
                     $continue = true;
                 }
             }
             if ($continue) {
                 continue;
             }
             if (file_exists($file) && is_readable($file)) {
                 $stream = @fopen($file, 'rb');
                 if ($stream) {
                     $file = substr($file, strlen($path));
                     $itemZip->addLargeFile($stream, $file);
                 }
             } else {
                 $warns['not_readable'][] = $file;
             }
         }
         $itemZip->finalize();
         $stream = @fopen(SNS_BACKUPS_PATH . $name . '.zip', 'rb');
         if ($stream) {
             $zip->addLargeFile($stream, $name . '.zip');
         }
         @unlink(SNS_BACKUPS_PATH . $name . '.zip');
         Sns_Log::log_action('Backup item - ' . $name, SNS_LOG_END);
     }
     Sns_Log::log_action('Summarize item backups');
     $zip->finalize();
     Sns_Log::log_action('Summarize item backups', SNS_LOG_END);
     $this->hash;
     $this->filename;
     $this->save();
     return $warns;
 }
 public static function saveExternalZip()
 {
     $externalFolders = scandir(WP_CONTENT_DIR . SNS_DS . SNS_BACKUP_ROOT_FOLDER_NAME);
     $get_new_hash = Sns_Backup::get_new_hash();
     foreach ($externalFolders as $externalFolder) {
         if (substr($externalFolder, 0, 4) == 'sns_') {
             global $wpdb;
             $externalFolder = str_replace('.zip', '', $externalFolder);
             $table = SNS_DB_PREFIX . 'backups';
             $query = $wpdb->prepare("SELECT  id from {$table} WHERE filename = %s", $externalFolder);
             $fileNames = $wpdb->get_row($query);
             if (!$fileNames) {
                 $sql = $wpdb->prepare("INSERT INTO " . SNS_DB_PREFIX . "backups (type, info, backup_date, restore_date, hash, filename) VALUES (%s,%s,%s,%s,%s,%s)", 'manual', '{"options":[],"destinations":[]}', date('Y-m-d H:i:s'), '0000-00-00 00:00:00', $get_new_hash, $externalFolder);
                 $res = $wpdb->query($sql);
             }
         }
     }
 }
function sns_backup_action()
{
    $proc = Sns_State::get_status();
    $scheduleState = Sns_State::get_status(Sns_Backup::BACKUP_MODE_SCHEDULE);
    if ($proc['status'] != Sns_State::STATUS_ACTIVE && empty($scheduleState)) {
        $backup = new Sns_Backup('schedule');
        $data = array('status' => Sns_State::STATUS_ACTIVE);
        Sns_State::update($data);
        try {
            $backup->backup();
        } catch (Exception $e) {
            Sns_Log::log_exception_obj($e);
        }
        $data = array('status' => Sns_State::STATUS_FINISHED);
        Sns_State::update($data);
    }
}
function sns_backup_manual_backup()
{
    @set_time_limit(0);
    register_shutdown_function('sns_register_shutdown');
    try {
        Sns_Log::log_msg('########PROCESS STARTED########' . PHP_EOL);
        $state = Sns_State::get_status();
        if ($state['status'] == Sns_State::STATUS_ACTIVE) {
            throw new Sns_Exception_Unavailable_Operation('There is an existing active process.Please wait.');
        }
        $stateData = array('status' => Sns_State::STATUS_ACTIVE, 'type' => Sns_State::TYPE_BACKUP, 'start_date' => date('Y-m-d H:i:s'));
        Sns_State::update($stateData);
        Sns_Checker::check();
        $locations = isset($_POST['locations']) ? $_POST['locations'] : array();
        $destination = new Sns_Destination(Sns_Backup::BACKUP_MODE_MANUAL);
        $destination->set_destinations($locations);
        $destination->save();
        $backup = new Sns_Backup(Sns_Backup::BACKUP_MODE_MANUAL);
        Sns_State::$currentBackupFilename = $backup->getFilename();
        $dest = 'local';
        if (count($locations)) {
            $dest .= ',' . implode(',', array_keys($locations));
        }
        $options = Sns_Option::get_options(true);
        foreach ($options as $option => $data) {
            if ($option == Sns_Option::FULL) {
                $option_list = array($option);
                break;
            }
            if ($option != Sns_Option::COUNT) {
                $option_list[] = $option;
            }
        }
        $options = implode(',', array_values($option_list));
        $engLogDecription = '----------------------------------------------------';
        $engLogDecription .= "\n" . ucfirst($stateData['type']) . ": " . $options;
        $engLogDecription .= "\nDestination: " . $dest . "\n";
        $engLogDecription .= "----------------------------------------------------\n";
        Sns_Log::log_msg($engLogDecription);
        Sns_Log::log_action('Backing up');
        $warns = $backup->backup();
        Sns_Log::log_action('Backing up', SNS_LOG_END);
        Sns_Log::log_msg("\n");
        $skipped_files = '';
        if (!empty($warns['not_readable'])) {
            $skipped_files .= '*********WARNING**********' . PHP_EOL;
            $skipped_files .= 'The following files are not readable and were excluded from backup package' . PHP_EOL;
            $i = 1;
            foreach ($warns['not_readable'] as $file) {
                $skipped_files .= $i . '. ' . $file . PHP_EOL;
                $i++;
            }
            Sns_Log::log_msg($skipped_files);
        }
        $stateData = array('status' => Sns_State::STATUS_FINISHED, 'type' => Sns_State::TYPE_BACKUP);
        Sns_State::update($stateData);
        Sns_Log::log_msg('########PROCESS ENDED########' . PHP_EOL);
    } catch (Exception $e) {
        Sns_Log::log_exception_obj($e);
        $ex_data = Sns_Exception_Handler::get_exception_data($e);
        $stateData = array('status' => Sns_State::STATUS_FAILED, 'type' => Sns_State::TYPE_BACKUP, 'msg' => $ex_data['status'] . ' : ' . $ex_data['msg']);
        Sns_State::update($stateData);
        Sns_Log::log_msg('########PROCESS ENDED########' . PHP_EOL);
        Sns_Log::report('backup');
    }
    die;
}