public function __construct($params)
 {
     parent::__construct($params);
     $this->init(array('id' => uniqid('wpadm_method_backup__'), 'stime' => time(), 'type' => $params['type']));
     $name = get_option('siteurl');
     $name = str_replace("http://", '', $name);
     $name = str_replace("https://", '', $name);
     $name = preg_replace("|\\W|", "_", $name);
     $name .= '-' . $this->type . '-' . date("Y_m_d_H_i");
     $this->name = $name;
     // папка для бэкапа
     $this->dir = WPADM_DIR_BACKUP . '/' . $this->name;
     $error = WPAdm_Core::mkdir(WPADM_DIR_BACKUP);
     if (!empty($error)) {
         $this->result->setError($error);
         $this->result->setResult(WPAdm_Result::WPADM_RESULT_ERROR);
     }
     $error = WPAdm_Core::mkdir($this->dir);
     if (!empty($error)) {
         $this->result->setError($error);
         $this->result->setResult(WPAdm_Result::WPADM_RESULT_ERROR);
     }
 }
 public function getResult()
 {
     $errors = array();
     $this->result->setResult(WPAdm_Result::WPADM_RESULT_SUCCESS);
     $this->result->setError('');
     WPAdm_Core::log(langWPADM::get('Start Backup process...', false));
     # create db dump
     if (in_array('db', $this->params['types'])) {
         WPAdm_Core::log(langWPADM::get('Creating Database Dump', false));
         $error = WPAdm_Core::mkdir(WPADM_DIR_BACKUP);
         if (!empty($error)) {
             $this->result->setError($error);
             $this->result->setResult(WPAdm_Result::WPADM_RESULT_ERROR);
             return $this->result;
         }
         $mysql_dump_file = WPADM_DIR_BACKUP . '/mysqldump.sql';
         if (file_exists($mysql_dump_file)) {
             unlink($mysql_dump_file);
         }
         $wp_mysql_params = $this->getWpMysqlParams();
         if (isset($this->params['optimize']) && $this->params['optimize'] == 1) {
             WPAdm_Core::log(langWPADM::get('Optimize Database Tables', false));
             $commandContext = new WPAdm_Command_Context();
             $commandContext->addParam('command', 'mysqloptimize')->addParam('host', $wp_mysql_params['host'])->addParam('db', $wp_mysql_params['db'])->addParam('user', $wp_mysql_params['user'])->addParam('password', $wp_mysql_params['password']);
             $this->queue->clear()->add($commandContext);
             unset($commandContext);
         }
         $commandContext = new WPAdm_Command_Context();
         $commandContext->addParam('command', 'mysqldump')->addParam('host', $wp_mysql_params['host'])->addParam('db', $wp_mysql_params['db'])->addParam('user', $wp_mysql_params['user'])->addParam('password', $wp_mysql_params['password'])->addParam('tables', '')->addParam('to_file', $mysql_dump_file);
         $res = $this->queue->add($commandContext)->save()->execute();
         if (!$res) {
             $log = langWPADM::get('Website "%d" returned an error during database dump creation: \'Dump of Database wasn\'t created: "%s"\'. To solve this problem, please check your database system logs or send to us your FTP access data. You can send to us support request using "Help" button on plugin page.', false, array('%d', '%s'), array(SITE_HOME, $this->queue->getError()));
             WPAdm_Core::log($log);
             $errors[] = $log;
         } elseif (0 == (int) filesize($mysql_dump_file)) {
             $log = langWPADM::get('Website "%d" returned an error during database dump creation: Database-Dump file is emplty. To solve this problem, please check permissions to folder: "%dir".', false, array('%d', '%dir'), array(SITE_HOME, WPADM_DIR_BACKUP));
             $errors[] = $log;
             WPAdm_Core::log($log);
         } else {
             $size_dump = round(filesize($mysql_dump_file) / 1024 / 1024, 2);
             $log = str_replace("%s", $size_dump, langWPADM::get('Database Dump was successfully created ( %s Mb) : ', false));
             WPAdm_Core::log($log . $mysql_dump_file);
         }
         unset($commandContext);
     }
     if (count($errors) == 0) {
         if (in_array('files', $this->params['types'])) {
             WPAdm_Core::log(langWPADM::get('Create a list of files for Backup', false));
             $files = $this->createListFilesForArchive();
         }
         if (isset($mysql_dump_file) && file_exists($mysql_dump_file) && filesize($mysql_dump_file) > 0) {
             $files[] = $mysql_dump_file;
         }
         if (empty($files)) {
             $errors[] = langWPADM::get('Website "%d" returned an error during creation of the list of files for a backup: list of files for a backup is empty. To solve this problem, please check files and folders permissions for website "%d".', false, array('%d'), array(SITE_HOME));
         }
         // split the file list by 170kbayt lists, To break one big task into smaller
         $files2 = array();
         $files2[0] = array();
         $i = 0;
         $size = 0;
         foreach ($files as $f) {
             if ($size > 170000) {
                 //~170kbyte
                 $i++;
                 $size = 0;
                 $files2[$i] = array();
             }
             $f_size = (int) @filesize($f);
             if ($f_size == 0 || $f_size > 1000000) {
                 WPAdm_Core::log('File ' . $f . ' Size ' . $f_size);
             }
             $size += $f_size;
             $files2[$i][] = $f;
         }
         WPAdm_Core::log(langWPADM::get('List of Backup-Files was successfully created', false));
         $this->queue->clear();
         foreach ($files2 as $files) {
             $commandContext = new WPAdm_Command_Context();
             $commandContext->addParam('command', 'archive')->addParam('files', $files)->addParam('to_file', $this->dir . '/' . $this->name)->addParam('max_file_size', 900000)->addParam('remove_path', ABSPATH);
             $this->queue->add($commandContext);
             unset($commandContext);
         }
         WPAdm_Core::log(langWPADM::get('Backup of Files was started', false));
         $this->queue->save()->execute();
         WPAdm_Core::log(langWPADM::get('End of File Backup', false));
         $files = glob($this->dir . '/' . $this->name . '*');
         $urls = array();
         $totalSize = 0;
         foreach ($files as $file) {
             $urls[] = str_replace(ABSPATH, '', $file);
             $totalSize += @intval(filesize($file));
         }
         $this->result->setData($urls);
         $this->result->setSize($totalSize);
         $size = $totalSize / 1024 / 1024;
         /// MByte
         $size = round($size, 2);
         $log = str_replace("%s", $size, langWPADM::get('Backup Size %s Mb', false));
         WPAdm_Core::log($log);
         $remove_from_server = 0;
         #Removing TMP-files
         WPAdm_Core::rmdir($mysql_dump_file);
         #Removind old backups(if limit the number of stored backups)
         if ($this->params['limit'] != 0) {
             WPAdm_Core::log(langWPADM::get('Limits of Backups ', false) . $this->params['limit']);
             WPAdm_Core::log(langWPADM::get('Removing of old Backups was started', false));
             $files = glob(WPADM_DIR_BACKUP . '/*');
             if (count($files) > $this->params['limit']) {
                 $files2 = array();
                 foreach ($files as $f) {
                     $fa = explode('-', $f);
                     if (count($fa) != 3) {
                         continue;
                     }
                     $files2[$fa[2]] = $f;
                 }
                 ksort($files2);
                 $d = count($files2) - $this->params['limit'];
                 $del = array_slice($files2, 0, $d);
                 foreach ($del as $d) {
                     WPAdm_Core::rmdir($d);
                 }
             }
             WPAdm_Core::log(langWPADM::get('Removing of old Backups was Finished', false));
         }
     }
     wpadm_class::setBackup(1);
     if (!empty($errors)) {
         $this->result->setError(implode("\n", $errors));
         $this->result->setResult(WPAdm_Result::WPADM_RESULT_ERROR);
         WPAdm_Core::rmdir($this->dir);
         wpadm_class::setStatus(0);
         wpadm_class::setErrors(implode(", ", $errors));
     } else {
         wpadm_class::setStatus(1);
         WPAdm_Core::log(langWPADM::get('Backup creation was complete successfully!', false));
     }
     wpadm_class::backupSend();
     return $this->result;
 }
 public static function wpadm_restore_dropbox()
 {
     require_once DRBBACKUP_BASE_DIR . "/modules/class-wpadm-core.php";
     @session_write_close();
     $log_class = new WPAdm_Core(array('method' => "local"), 'full_backup_dropbox', WPAdm_Core::$pl_dir);
     if (file_exists(WPAdm_Core::getTmpDir() . "/logs2")) {
         @unlink(WPAdm_Core::getTmpDir() . "/logs2");
     }
     if (file_exists(WPAdm_Core::getTmpDir() . "/log.log")) {
         @unlink(WPAdm_Core::getTmpDir() . "/log.log");
     }
     WPAdm_Core::log(langWPADM::get('Start Restore from Dropbox cloud', false));
     $dropbox_options = get_option(PREFIX_BACKUP_ . 'dropbox-setting');
     if ($dropbox_options) {
         require_once DRBBACKUP_BASE_DIR . "/modules/dropbox.class.php";
         $dropbox_options = unserialize(base64_decode($dropbox_options));
         $folder_project = self::getNameProject();
         $dropbox = new dropbox($dropbox_options['app_key'], $dropbox_options['app_secret'], $dropbox_options['auth_token_secret']);
         if ($dropbox->isAuth()) {
             WPAdm_Core::mkdir(DROPBOX_BACKUP_DIR_BACKUP);
             $name_backup = isset($_POST['name']) ? trim($_POST['name']) : "";
             $dir_backup = DROPBOX_BACKUP_DIR_BACKUP . "/{$name_backup}";
             $error = WPAdm_Core::mkdir($dir_backup);
             if (!empty($error)) {
                 WPAdm_Core::log($error);
                 $res['result'] = WPAdm_Result::WPADM_RESULT_ERROR;
                 $res['error'] = $error;
                 $res['data'] = array();
                 $res['size'] = 0;
             } else {
                 $files = $dropbox->listing("{$folder_project}/{$name_backup}");
                 if (isset($files['items'])) {
                     $n = count($files['items']);
                     for ($i = 0; $i < $n; $i++) {
                         $res = $dropbox->downloadFile("{$folder_project}/{$name_backup}/{$files['items'][$i]['name']}", "{$dir_backup}/{$files['items'][$i]['name']}");
                         if ($res != "{$dir_backup}/{$files['items'][$i]['name']}" && isset($res['text'])) {
                             WPAdm_Core::log(langWPADM::get('Error: ', false) . $res['text']);
                         } else {
                             $log = str_replace('%s', $files['items'][$i]['name'], langWPADM::get('Download file (%s) with Dropbox', false));
                             WPAdm_Core::log($log);
                         }
                     }
                     parent::$type = 'full';
                     $backup = new WPAdm_Core(array('method' => "local_restore", 'params' => array('types' => array('files', 'db'), 'name_backup' => $name_backup)), 'full_backup_dropbox', WPAdm_Core::$pl_dir);
                     $res = $backup->getResult()->toArray();
                     WPAdm_Core::rmdir($dir_backup);
                 }
             }
         } else {
             WPAdm_Core::log(langWPADM::get('Website "%d" can\'t authorize on Dropbox with using of "app key: %k" and "app secret: %s"', false, array('%d', '%k', '%s'), array(SITE_HOME, $dropbox_options['app_key'], $dropbox_options['app_secret'])));
         }
     } else {
         WPAdm_Core::log(langWPADM::get('Website "%d" returned an error during connection to Dropbox: "app key" and "app secret" wasn\'t found. Please, check your Dropbox settings.', false, '%d', SITE_HOME));
     }
     @session_start();
     echo json_encode($res);
     wp_die();
 }
 public static function restore_backup()
 {
     require_once dirname(__FILE__) . "/class-wpadm-core.php";
     @session_write_close();
     $log = new WPAdm_Core(array('method' => "local"), 'db_backup_s3', dirname(__FILE__));
     if (file_exists(WPAdm_Core::getTmpDir() . "/logs2")) {
         unlink(WPAdm_Core::getTmpDir() . "/logs2");
     }
     if (file_exists(WPAdm_Core::getTmpDir() . "/log.log")) {
         unlink(WPAdm_Core::getTmpDir() . "/log.log");
     }
     $res['error'] = 'Error';
     $res['result'] = 'error';
     parent::$type = 'db';
     if (isset($_POST['type'])) {
         $name_backup = isset($_POST['name']) ? trim($_POST['name']) : "";
         $dir = ABSPATH . 'wpadm_backups/' . $name_backup;
         if ($_POST['type'] == 'local') {
             $backup = new WPAdm_Core(array('method' => "local_restore", 'params' => array('types' => array('db'), 'name_backup' => $name_backup)), 'db_backup_s3', dirname(__FILE__));
             $res = $backup->getResult()->toArray();
         } elseif ($_POST['type'] == 's3') {
             $amazon_option = self::getAmazonOptions();
             if ($amazon_option) {
                 require_once dirname(__FILE__) . '/modules/aws-autoloader.php';
                 try {
                     $credentials = new Aws\Common\Credentials\Credentials($amazon_option['access_key_id'], $amazon_option['secret_access_key']);
                     $client = Aws\S3\S3Client::factory(array('credentials' => $credentials));
                     $project = self::getNameProject();
                     WPAdm_Core::log("Get Files for Resore Backup");
                     $keys = $client->listObjects(array('Bucket' => $amazon_option['bucket'], 'Prefix' => $name_backup))->getIterator();
                     //->getPath('Contents/*/Key');
                     if (isset($keys['Contents'])) {
                         $n = count($keys['Contents']);
                         WPAdm_Core::mkdir($dir);
                         WPAdm_Core::log("Start Downloads files with Amazon S3");
                         for ($i = 0; $i < $n; $i++) {
                             $path = explode("/", $keys['Contents'][$i]['Key']);
                             if (isset($path[0]) && isset($path[1]) && !empty($path[1])) {
                                 $result = $client->getObject(array('Bucket' => $amazon_option['bucket'], 'Key' => $keys['Contents'][$i]['Key'], 'SaveAs' => ABSPATH . 'wpadm_backups/' . $keys['Contents'][$i]['Key']));
                                 WPAdm_Core::log("Download file - {$keys['Contents'][$i]['Key']}");
                             }
                         }
                         WPAdm_Core::log("End downloads files with Amazon S3");
                         $restore = new WPAdm_Core(array('method' => "local_restore", 'params' => array('log-delete' => 0, 'types' => array('db'), 'name_backup' => $name_backup)), 'db_backup_s3', dirname(__FILE__));
                         $res = $restore->getResult()->toArray();
                         if (is_dir($dir)) {
                             WPAdm_Core::rmdir($dir);
                         }
                     } else {
                         $res['error'] = "Error, in downloads with Amazon S3";
                         $res['result'] = 'error';
                     }
                 } catch (Exception $e) {
                     $res['error'] = $e->getMessage();
                     $res['result'] = 'error';
                 } catch (S3Exception $e) {
                     $res['error'] = $e->getMessage();
                     $res['result'] = 'error';
                 }
             } else {
                 $res['error'] = 'Error: Data is not exist for send backup files to Amazon S3. Please type your Data in the Settings form';
                 $res['result'] = 'error';
             }
         }
     }
     @session_start();
     echo json_encode($res);
     wp_die();
 }
 public function getResult()
 {
     $errors = array();
     $this->result->setResult(WPAdm_Result::WPADM_RESULT_SUCCESS);
     $this->result->setError('');
     WPAdm_Core::log(langWPADM::get('Start backup', false));
     # create db dump
     WPAdm_Core::log(langWPADM::get('Start create db dump', false));
     $error = WPAdm_Core::mkdir(WPADM_DIR_BACKUP);
     if (!empty($error)) {
         $this->result->setError($error);
         $this->result->setResult(WPAdm_Result::WPADM_RESULT_ERROR);
         return $this->result;
     }
     $mysql_dump_file = WPADM_DIR_BACKUP . '/mysqldump.sql';
     if (file_exists($mysql_dump_file)) {
         unlink($mysql_dump_file);
     }
     $wp_mysql_params = $this->getWpMysqlParams();
     if (isset($this->params['optimize']) && $this->params['optimize'] == 1) {
         WPAdm_Core::log(langWPADM::get('Table optimization', false));
         $commandContext = new WPAdm_Command_Context();
         $commandContext->addParam('command', 'mysqloptimize')->addParam('host', $wp_mysql_params['host'])->addParam('db', $wp_mysql_params['db'])->addParam('user', $wp_mysql_params['user'])->addParam('password', $wp_mysql_params['password']);
         $this->queue->clear()->add($commandContext);
         unset($commandContext);
     }
     $commandContext = new WPAdm_Command_Context();
     $commandContext->addParam('command', 'mysqldump')->addParam('host', $wp_mysql_params['host'])->addParam('db', $wp_mysql_params['db'])->addParam('user', $wp_mysql_params['user'])->addParam('password', $wp_mysql_params['password'])->addParam('tables', '')->addParam('to_file', $mysql_dump_file);
     $res = $this->queue->add($commandContext)->save()->execute();
     if (!$res) {
         $log = str_replace('%s', $this->queue->getError(), langWPADM::get('Error: Dump of Database wasn\'t created (%s)', false));
         WPAdm_Core::log($log);
         $errors[] = $log;
     } elseif (0 == (int) filesize($mysql_dump_file)) {
         $errors[] = langWPADM::get('MySQL Error: Database-Dump File is empty', false);
         WPAdm_Core::log(langWPADM::get('Dump of Database wasn\'t created (File of Database-Dump is empty!)', false));
     } else {
         $size_dump = round(filesize($mysql_dump_file) / 1024 / 1024, 2);
         $log = str_replace("%s", $size_dump, langWPADM::get('Database Dump was successfully created ( %s Mb) : ', false));
         WPAdm_Core::log($log . $mysql_dump_file);
     }
     unset($commandContext);
     #ЗАРХИВИРУЕМ ФАЙЛЫ
     WPAdm_Core::log(langWPADM::get('Create a list of files for Backup', false));
     $files = $this->createListFilesForArchive();
     if (file_exists($mysql_dump_file) && filesize($mysql_dump_file) > 0) {
         $files[] = $mysql_dump_file;
     }
     if (empty($files)) {
         $errors[] = langWPADM::get('Error: the list of Backup files is empty', false);
     }
     // split the file list by 170kbayt lists, To break one big task into smaller
     $files2 = array();
     $files2[0] = array();
     $i = 0;
     $size = 0;
     foreach ($files as $f) {
         if ($size > 170000) {
             //~170kbyte
             $i++;
             $size = 0;
             $files2[$i] = array();
         }
         $f_size = (int) filesize($f);
         if ($f_size == 0 || $f_size > 1000000) {
             WPAdm_Core::log('file ' . $f . ' size ' . $f_size);
         }
         $size += $f_size;
         $files2[$i][] = $f;
     }
     WPAdm_Core::log(langWPADM::get('List of Backup-Files was successfully created', false));
     $this->queue->clear();
     foreach ($files2 as $files) {
         $commandContext = new WPAdm_Command_Context();
         $commandContext->addParam('command', 'archive')->addParam('files', $files)->addParam('to_file', $this->dir . '/' . $this->name)->addParam('max_file_size', 900000)->addParam('remove_path', ABSPATH);
         $this->queue->add($commandContext);
         unset($commandContext);
     }
     WPAdm_Core::log(langWPADM::get('Backup of Files was started', false));
     $this->queue->save()->execute();
     WPAdm_Core::log(langWPADM::get('End of File Backup', false));
     $files = glob($this->dir . '/' . $this->name . '*');
     $urls = array();
     $totalSize = 0;
     foreach ($files as $file) {
         $urls[] = str_replace(ABSPATH, '', $file);
         $totalSize += @intval(filesize($file));
     }
     $this->result->setData($urls);
     $this->result->setSize($totalSize);
     $remove_from_server = 0;
     if (isset($this->params['storage'])) {
         foreach ($this->params['storage'] as $storage) {
             if ($storage['type'] == 'ftp') {
                 WPAdm_Core::log(langWPADM::get('Begin copying files to FTP', false));
                 $this->queue->clear();
                 $files = glob($this->dir . '/' . $this->name . '*');
                 //$this->getResult()->setData($files);
                 $ad = $storage['access_details'];
                 $dir = isset($ad['dir']) ? $ad['dir'] : '/';
                 $dir = trim($dir, '/') . '/' . $this->name;
                 foreach ($files as $file) {
                     $commandContext = new WPAdm_Command_Context();
                     $commandContext->addParam('command', 'send_to_ftp')->addParam('file', $file)->addParam('host', $ad['host'])->addParam('port', isset($ad['port']) ? $ad['port'] : 21)->addParam('user', $ad['user'])->addParam('password', $ad['password'])->addParam('dir', $dir)->addParam('http_host', isset($ad['http_host']) ? $ad['http_host'] : '');
                     $this->queue->add($commandContext);
                     unset($commandContext);
                 }
                 $res = $this->queue->save()->execute();
                 if (!$res) {
                     $log = langWPADM::get('FTP: ', false);
                     WPAdm_Core::log($log . $this->queue->getError());
                     $errors[] = $log . $this->queue->getError();
                 }
                 WPAdm_Core::log(langWPADM::get('Finished copying files to FTP', false));
                 if (isset($storage['remove_from_server']) && $storage['remove_from_server'] == 1) {
                     $remove_from_server = $storage['remove_from_server'];
                 }
             } elseif ($storage['type'] == 's3') {
                 WPAdm_Core::log(langWPADM::get('Begin coping files to S3', false));
                 $this->queue->clear();
                 $files = glob($this->dir . '/' . $this->name . '*');
                 //$this->getResult()->setData($files);
                 $ad = $storage['access_details'];
                 $dir = isset($ad['dir']) ? $ad['dir'] : '/';
                 $dir = trim($dir, '/') . '/' . $this->name;
                 foreach ($files as $file) {
                     $commandContext = new WPAdm_Command_Context();
                     $commandContext->addParam('command', 'send_to_s3')->addParam('file', $file)->addParam('bucket', $ad['bucket'])->addParam('AccessKeyId', $ad['AccessKeyId'])->addParam('SecretAccessKey', $ad['SecretAccessKey'])->addParam('SessionToken', $ad['SessionToken']);
                     $this->queue->add($commandContext);
                     unset($commandContext);
                 }
                 $res = $this->queue->save()->execute();
                 if (!$res) {
                     WPAdm_Core::log('S3: ' . $this->queue->getError());
                     $errors[] = 'S3: ' . $this->queue->getError();
                 }
                 WPAdm_Core::log(langWPADM::get('Finished copying files to S3', false));
                 if (isset($storage['remove_from_server']) && $storage['remove_from_server'] == 1) {
                     $remove_from_server = $storage['remove_from_server'];
                 }
             }
         }
         if ($remove_from_server) {
             // удаляем файлы на сервере
             WPAdm_Core::log(langWPADM::get('Remove the backup server', false));
             WPAdm_Core::rmdir($this->dir);
         }
     }
     if (isset($this->params['gd']) && isset($this->params['gd']['key']) && isset($this->params['gd']['secret'])) {
         $this->queue->clear();
         $files = glob($this->dir . '/' . $this->name . '*');
         $files = array_merge_recursive(array($mysql_dump_file), $files);
         WPAdm_Core::log(langWPADM::get('files to google: ', false) . print_r($files, true));
         $n = count($files);
         for ($i = 0; $i < $n; $i++) {
             $commandContext = new WPAdm_Command_Context();
             $commandContext->addParam('command', 'send_to_google_drive')->addParam('key', $this->params['gd']['key'])->addParam('secret', $this->params['gd']['secret'])->addParam('token', $this->params['gd']['token'])->addParam('folder_project', $this->params['gd']['folder'])->addParam('folder', $this->name)->addParam('files', $files[$i]);
             $this->queue->add($commandContext);
             unset($commandContext);
         }
         $res = $this->queue->save()->execute();
         if (!$res) {
             WPAdm_Core::log(langWPADM::get('Google drive: ', false) . $this->queue->getError());
         }
         //WPAdm_Core::log('google drive' . print_r($this->params, true));
     }
     if (isset($this->params['dropbox']) && isset($this->params['dropbox']['key']) && isset($this->params['dropbox']['secret'])) {
         $this->queue->clear();
         $files = glob($this->dir . '/' . $this->name . '*');
         $files = array_merge_recursive(array($mysql_dump_file), $files);
         WPAdm_Core::log(langWPADM::get('files to dropbox: ', false) . print_r($files, true));
         $n = count($files);
         for ($i = 0; $i < $n; $i++) {
             $commandContext = new WPAdm_Command_Context();
             $commandContext->addParam('command', 'send_to_dropbox')->addParam('key', $this->params['dropbox']['key'])->addParam('secret', $this->params['dropbox']['secret'])->addParam('token', $this->params['dropbox']['token'])->addParam('folder_project', $this->params['dropbox']['folder'])->addParam('folder', $this->name)->addParam('files', $files[$i]);
             $this->queue->add($commandContext);
             unset($commandContext);
         }
         $this->queue->save()->execute();
         if (!$res) {
             WPAdm_Core::log(langWPADM::get('Dropbox: ', false) . $this->queue->getError());
         }
     }
     #Removing TMP-files
     WPAdm_Core::rmdir(WPADM_DIR_BACKUP . '/mysqldump.sql');
     #Removind old backups(if limit the number of stored backups)
     WPAdm_Core::log(langWPADM::get('Start removing old backups', false));
     if ($this->params['limit'] != 0) {
         $files = glob(WPADM_DIR_BACKUP . '/*');
         if (count($files) > $this->params['limit']) {
             $files2 = array();
             foreach ($files as $f) {
                 $fa = explode('-', $f);
                 if (count($fa) != 3) {
                     continue;
                 }
                 $files2[$fa[2]] = $f;
             }
             ksort($files2);
             $d = count($files2) - $this->params['limit'];
             $del = array_slice($files2, 0, $d);
             foreach ($del as $d) {
                 WPAdm_Core::rmdir($d);
             }
         }
     }
     WPAdm_Core::log(langWPADM::get('Finished removing old backups', false));
     WPAdm_Core::log(langWPADM::get('Creating a backup is completed', false));
     wpadm_class::setBackup(2);
     if (!empty($errors)) {
         $this->result->setError(implode("\n", $errors));
         $this->result->setResult(WPAdm_Result::WPADM_RESULT_ERROR);
         wpadm_class::setStatus(0);
         wpadm_class::setErrors(implode(", ", $errors));
     } else {
         wpadm_class::setStatus(1);
     }
     wpadm_class::backupSend();
     return $this->result;
 }
 public function getResult()
 {
     if ($this->start === false) {
         $this->result->setResult(WPAdm_Result::WPADM_RESULT_ERROR);
         $this->result->setError('Backup process was started, please, wait a few minutes...');
         return $this->result;
     }
     $errors = array();
     $this->result->setResult(WPAdm_Result::WPADM_RESULT_SUCCESS);
     $this->result->setError('');
     WPAdm_Core::log('Start Backup process...');
     # create db dump
     if (in_array('db', $this->params['types'])) {
         WPAdm_Core::log('Creating Database Dump');
         WPAdm_Core::mkdir(ABSPATH . 'wpadm_backup');
         $mysql_dump_file = ABSPATH . 'wpadm_backup/mysqldump.sql';
         if (file_exists($mysql_dump_file)) {
             unlink($mysql_dump_file);
         }
         $wp_mysql_params = $this->getWpMysqlParams();
         if (isset($this->params['optimize']) && $this->params['optimize'] == 1) {
             WPAdm_Core::log('Optimize Database Tables');
             $commandContext = new WPAdm_Command_Context();
             $commandContext->addParam('command', 'mysqloptimize')->addParam('host', $wp_mysql_params['host'])->addParam('db', $wp_mysql_params['db'])->addParam('user', $wp_mysql_params['user'])->addParam('password', $wp_mysql_params['password']);
             $this->queue->clear()->add($commandContext);
             unset($commandContext);
         }
         $commandContext = new WPAdm_Command_Context();
         $commandContext->addParam('command', 'mysqldump')->addParam('host', $wp_mysql_params['host'])->addParam('db', $wp_mysql_params['db'])->addParam('user', $wp_mysql_params['user'])->addParam('password', $wp_mysql_params['password'])->addParam('tables', '')->addParam('to_file', $mysql_dump_file);
         $res = $this->queue->add($commandContext)->save()->execute();
         if (!$res) {
             WPAdm_Core::log('Error: Dump of Database wasn\'t created(' . $this->queue->getError() . ')');
             $errors[] = 'MySQL Error: ' . $this->queue->getError();
         } elseif (0 == (int) filesize($mysql_dump_file)) {
             $errors[] = 'MySQL Error: Database-Dump File is empty';
             WPAdm_Core::log('Dump of Database wasn\'t created (File of Database-Dump is empty!)');
         } else {
             $size_dump = round(filesize($mysql_dump_file) / 1024 / 1024, 2);
             WPAdm_Core::log('Database Dump was successfully created(' . $size_dump . 'Mb):' . $mysql_dump_file);
         }
         unset($commandContext);
     }
     $files = array();
     if (in_array('files', $this->params['types'])) {
         WPAdm_Core::log('Create a list of files for Backup');
         $files = $this->createListFilesForArchive();
     }
     if (isset($mysql_dump_file) && file_exists($mysql_dump_file) && filesize($mysql_dump_file) > 0) {
         $files[] = $mysql_dump_file;
     }
     if (empty($files)) {
         $errors[] = 'Error: the list of Backup files is empty';
     }
     // split the file list by 170kbayt lists, To break one big task into smaller
     $files2 = array();
     $files2[0] = array();
     $i = 0;
     $size = 0;
     foreach ($files as $f) {
         if ($size > 170000) {
             //~170kbyte
             $i++;
             $size = 0;
             $files2[$i] = array();
         }
         $f_size = (int) @filesize($f);
         if ($f_size == 0 || $f_size > 1000000) {
             WPAdm_Core::log('File ' . $f . ' Size ' . $f_size);
         }
         $size += $f_size;
         $files2[$i][] = $f;
     }
     WPAdm_Core::log('List of Backup-Files was successfully created');
     $this->queue->clear();
     foreach ($files2 as $files) {
         $commandContext = new WPAdm_Command_Context();
         $commandContext->addParam('command', 'archive')->addParam('files', $files)->addParam('to_file', $this->dir . '/' . $this->name)->addParam('max_file_size', 900000)->addParam('remove_path', ABSPATH);
         $this->queue->add($commandContext);
         unset($commandContext);
     }
     WPAdm_Core::log('Backup of Files was started');
     $this->queue->save()->execute();
     WPAdm_Core::log('End of File Backup');
     $files = glob($this->dir . '/' . $this->name . '*');
     $urls = array();
     $totalSize = 0;
     foreach ($files as $file) {
         $urls[] = str_replace(ABSPATH, '', $file);
         $totalSize += @intval(filesize($file));
     }
     $this->result->setData($urls);
     $this->result->setSize($totalSize);
     $size = $totalSize / 1024 / 1024;
     /// MByte
     $size = round($size, 2);
     WPAdm_Core::log('Backup Size ' . $size . " Mb");
     $remove_from_server = 0;
     #Removing TMP-files
     WPAdm_Core::rmdir(ABSPATH . 'wpadm_backup');
     #Removind old backups(if limit the number of stored backups)
     if ($this->params['limit'] != 0) {
         WPAdm_Core::log('Limits of Backups ' . $this->params['limit']);
         WPAdm_Core::log('Removing of old Backups was started');
         $files = glob(ABSPATH . 'wpadm_backups/*');
         if (count($files) > $this->params['limit']) {
             $files2 = array();
             foreach ($files as $f) {
                 $fa = explode('-', $f);
                 if (count($fa) != 3) {
                     continue;
                 }
                 $files2[$fa[2]] = $f;
             }
             ksort($files2);
             $d = count($files2) - $this->params['limit'];
             $del = array_slice($files2, 0, $d);
             foreach ($del as $d) {
                 WPAdm_Core::rmdir($d);
             }
         }
         WPAdm_Core::log('Removing of old Backups was Finished');
     }
     wpadm_class::setBackup(1);
     if (!empty($errors)) {
         $this->result->setError(implode("\n", $errors));
         $this->result->setResult(WPAdm_Result::WPADM_RESULT_ERROR);
         wpadm_class::setStatus(0);
         wpadm_class::setErrors(implode(", ", $errors));
     } else {
         wpadm_class::setStatus(1);
         WPAdm_Core::log('Backup creating is completed successfully!');
     }
     wpadm_class::backupSend();
     return $this->result;
 }
 public function getResult()
 {
     $errors = array();
     $this->result->setResult(WPAdm_Result::WPADM_RESULT_SUCCESS);
     $this->result->setError('');
     WPAdm_Core::log('Start backup');
     WPAdm_Core::mkdir(ABSPATH . 'wpadm_backup');
     $mysql_dump_file = ABSPATH . 'wpadm_backup/mysqldump.sql';
     if (file_exists($mysql_dump_file)) {
         unlink($mysql_dump_file);
     }
     $wp_mysql_params = $this->getWpMysqlParams();
     if (isset($this->params['optimize']) && $this->params['optimize'] == 1) {
         WPAdm_Core::log('Optimize database tables');
         $commandContext = new WPAdm_Command_Context();
         $commandContext->addParam('command', 'mysqloptimize')->addParam('host', $wp_mysql_params['host'])->addParam('db', $wp_mysql_params['db'])->addParam('user', $wp_mysql_params['user'])->addParam('password', $wp_mysql_params['password']);
         $this->queue->clear()->add($commandContext);
         unset($commandContext);
     }
     $commandContext = new WPAdm_Command_Context();
     $commandContext->addParam('command', 'mysqldump')->addParam('host', $wp_mysql_params['host'])->addParam('db', $wp_mysql_params['db'])->addParam('user', $wp_mysql_params['user'])->addParam('password', $wp_mysql_params['password'])->addParam('tables', '')->addParam('to_file', $mysql_dump_file);
     $res = $this->queue->add($commandContext)->save()->execute();
     if (!$res) {
         WPAdm_Core::log('Database dump was not created(' . $this->queue->getError() . ')');
         $errors[] = 'MySQL error: ' . $this->queue->getError();
     } elseif (0 == (int) filesize($mysql_dump_file)) {
         $errors[] = 'MySQL error: empty dump-file';
         WPAdm_Core::log('Database dump was not created(empty file)');
     } else {
         WPAdm_Core::log('Database dump created(' . filesize($mysql_dump_file) . 'b):' . $mysql_dump_file);
     }
     unset($commandContext);
     WPAdm_Core::log('Start preparing a list of files');
     $files = array();
     if (file_exists($mysql_dump_file) && filesize($mysql_dump_file) > 0) {
         $files[] = $mysql_dump_file;
     }
     if (empty($files)) {
         $errors[] = 'Empty list files';
     }
     // Divide the list of files on the lists from 170 kbytes to break one big task into smaller
     $files2 = array();
     $files2[0] = array();
     $i = 0;
     $size = 0;
     foreach ($files as $f) {
         if ($size > 170000) {
             //~170kbyte
             $i++;
             $size = 0;
             $files2[$i] = array();
         }
         $f_size = (int) filesize($f);
         if ($f_size == 0 || $f_size > 1000000) {
             WPAdm_Core::log('file ' . $f . ' size ' . $f_size);
         }
         $size += $f_size;
         $files2[$i][] = $f;
     }
     WPAdm_Core::log('List of files prepared');
     $this->queue->clear();
     foreach ($files2 as $files) {
         $commandContext = new WPAdm_Command_Context();
         $commandContext->addParam('command', 'archive')->addParam('files', $files)->addParam('to_file', $this->dir . '/' . $this->name)->addParam('max_file_size', 900000)->addParam('remove_path', ABSPATH);
         $this->queue->add($commandContext);
         unset($commandContext);
     }
     WPAdm_Core::log('Getting back up files');
     $this->queue->save()->execute();
     WPAdm_Core::log('Ended up files');
     $files = glob($this->dir . '/' . $this->name . '*');
     $urls = array();
     $totalSize = 0;
     foreach ($files as $file) {
         $urls[] = str_replace(ABSPATH, '', $file);
         $totalSize += @intval(filesize($file));
     }
     $this->result->setData($urls);
     $this->result->setSize($totalSize);
     $remove_from_server = 0;
     if (isset($this->params['storage'])) {
         foreach ($this->params['storage'] as $storage) {
             if ($storage['type'] == 's3') {
                 WPAdm_Core::log('Begin coping files to S3');
                 $this->queue->clear();
                 $files = glob($this->dir . '/' . $this->name . '*');
                 //$this->getResult()->setData($files);
                 $ad = $storage['access_details'];
                 $dir = isset($ad['dir']) ? $ad['dir'] : '/';
                 $dir = trim($dir, '/') . '/' . $this->name;
                 foreach ($files as $file) {
                     $commandContext = new WPAdm_Command_Context();
                     $commandContext->addParam('command', 'send_to_s3')->addParam('file', $file)->addParam('bucket', $ad['bucket'])->addParam('AccessKeyId', $ad['AccessKeyId'])->addParam('SecretAccessKey', $ad['SecretAccessKey'])->addParam('SessionToken', $ad['SessionToken']);
                     if (isset($ad['mkdir_for_backup']) && $ad['mkdir_for_backup'] == 1) {
                         $commandContext->addParam('dir', $this->name);
                     }
                     $this->queue->add($commandContext);
                     unset($commandContext);
                 }
                 $res = $this->queue->save()->execute();
                 if (!$res) {
                     WPAdm_Core::log('S3: ' . $this->queue->getError());
                     $errors[] = 'S3: ' . $this->queue->getError();
                 }
                 WPAdm_Core::log('Finished copying files to S3');
                 if (isset($storage['remove_from_server']) && $storage['remove_from_server'] == 1) {
                     $remove_from_server = $storage['remove_from_server'];
                 }
             }
         }
         if ($remove_from_server) {
             // удаляем файлы на сервере
             WPAdm_Core::log('Remove the backup server');
             WPAdm_Core::rmdir($this->dir);
         }
     }
     # delete tmpf-files
     WPAdm_Core::rmdir(ABSPATH . 'wpadm_backup');
     #Remove old archives (over limit)
     WPAdm_Core::log('Start removing old files');
     if ($this->params['limit'] != 0) {
         $files = glob(ABSPATH . 'wpadm_backups/*');
         if (count($files) > $this->params['limit']) {
             $files2 = array();
             foreach ($files as $f) {
                 $fa = explode('-', $f);
                 if (count($fa) != 3) {
                     continue;
                 }
                 $files2[$fa[2]] = $f;
             }
             ksort($files2);
             $d = count($files2) - $this->params['limit'];
             $del = array_slice($files2, 0, $d);
             foreach ($del as $d) {
                 WPAdm_Core::rmdir($d);
             }
         }
     }
     WPAdm_Core::log('Finished delete the old files');
     WPAdm_Core::log('backup completed');
     if (!empty($errors)) {
         $this->result->setError(implode("\n", $errors));
         $this->result->setResult(WPAdm_Result::WPADM_RESULT_ERROR);
     }
     return $this->result;
 }