public function execute(WPAdm_Command_Context $context)
 {
     ini_set("memory_limit", "256M");
     require_once WPAdm_Core::getPluginDir() . '/modules/class-wpadm-archive.php';
     $af = $this->getArchiveName($context->get('to_file'));
     ini_set("memory_limit", "256M");
     $archive = new WPAdm_Archive($af, $context->get('to_file') . '.md5');
     $archive->setRemovePath($context->get('remove_path'));
     $files = $context->get('files');
     if (!file_exists($af)) {
         WPAdm_Core::log(langWPADM::get('Create part ', false) . basename($af));
     }
     if (file_exists($af) && filesize($af) > $context->get('max_file_size')) {
         $af = $this->getNextArchiveName($context->get('to_file'));
         unset($archive);
         if (!file_exists($af)) {
             WPAdm_Core::log(langWPADM::get('Create part ', false) . basename($af));
         }
         $archive = new WPAdm_Archive($af, $context->get('to_file') . '.md5');
         $archive->setRemovePath($context->get('remove_path'));
     }
     $files_str = implode(',', $files);
     $files_archive = WPAdm_Running::getCommandResultData('archive');
     if (!in_array($files_str, $files_archive)) {
         $archive->add($files_str);
         $files_archive = WPAdm_Running::getCommandResultData('archive');
         $files_archive[] = $files_str;
         if (!empty($files_archive)) {
             WPAdm_Running::setCommandResultData('archive', $files_archive);
         }
     }
     return true;
 }
 private function saveDataCommand($file)
 {
     $data_command = WPAdm_Running::getCommandResultData('command_dropbox');
     $data_command[] = $file;
     WPAdm_Running::setCommandResultData('command_dropbox', $data_command);
 }
 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'])) {
         $mysql_dump_file = DROPBOX_BACKUP_DIR_BACKUP . '/mysqldump.sql';
         if (!WPAdm_Running::getCommandResult('db')) {
             WPAdm_Running::setCommandResult('db');
             WPAdm_Core::log(langWPADM::get('Creating Database Dump', false));
             $error = WPAdm_Core::mkdir(DROPBOX_BACKUP_DIR_BACKUP);
             if (!empty($error)) {
                 $this->result->setError($error);
                 $this->result->setResult(WPAdm_Result::WPADM_RESULT_ERROR);
                 return $this->result;
             }
             if (file_exists($mysql_dump_file) && !file_exists(WPAdm_Core::getTmpDir() . "/db")) {
                 unlink($mysql_dump_file);
             }
             $wp_mysql_params = $this->getWpMysqlParams();
             if (isset($this->params['optimize']) && $this->params['optimize'] == 1) {
                 $opt_db = WPAdm_Running::getCommandResultData('db', $proc_data);
                 if (!isset($opt_db['optimize'])) {
                     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 "%domain" 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('%domain', '%s'), array(SITE_HOME, $this->queue->getError()));
                 WPAdm_Core::log($log);
                 $errors[] = $log;
             } elseif (0 == (int) filesize($mysql_dump_file)) {
                 $log = langWPADM::get('Website "%domain" returned an error during database dump creation: Database-Dump file is emplty. To solve this problem, please check permissions to folder: "%dir".', false, array('%domain', '%dir'), array(SITE_HOME, DROPBOX_BACKUP_DIR_BACKUP));
                 $errors[] = $log;
                 WPAdm_Core::log($log);
             } else {
                 $size_dump = round(filesize($mysql_dump_file) / 1024 / 1024, 2);
                 $log = str_replace("%size", $size_dump, langWPADM::get('Database Dump was successfully created ( %size Mb) : ', false));
                 WPAdm_Core::log($log . $mysql_dump_file);
             }
             unset($commandContext);
             WPAdm_Running::setCommandResult('db', true);
         }
     }
     if (count($errors) == 0) {
         $command_files_list = WPAdm_Running::getCommandResultData('files');
         if (in_array('files', $this->params['types']) && empty($command_files_list)) {
             $files = $this->createListFilesForArchive();
             WPAdm_Running::setCommandResultData('files', $files);
         } else {
             $files = $command_files_list;
         }
         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 = WPAdm_Running::getCommandResultData('files2');
         if (empty($files2)) {
             $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_Running::setCommandResultData('files2', $files2);
         }
         WPAdm_Core::log(langWPADM::get('List of Backup-Files was successfully created', false));
         $this->queue->clear();
         if (!WPAdm_Running::getCommandResult('archive')) {
             WPAdm_Running::setCommandResult('archive');
             $files_archive = WPAdm_Running::getCommandResultData('archive');
             foreach ($files2 as $files) {
                 $files_str = implode(',', $files);
                 if (!in_array($files_str, $files_archive)) {
                     $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));
             WPAdm_Running::setCommandResult('archive', true);
         }
         $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);
         $this->result->setValue('md5_data', md5(print_r($this->result->toArray(), 1)));
         $this->result->setValue('name', $this->name);
         $this->result->setValue('time', $this->time);
         $this->result->setValue('type', 'local');
         $this->result->setValue('counts', count($urls));
         $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(DROPBOX_BACKUP_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 function mysqldump($db, $filename)
 {
     $proc_data = WPAdm_Running::getCommandResultData('db');
     if (!isset($proc_data['mysqldump'])) {
         $link = $this->connect($db);
         WPAdm_Core::log(langWPADM::get('MySQL of Dump was started', false));
         $tables = array();
         $n = $link->query('SHOW TABLES');
         $result = $link->last_result;
         if (!empty($link->last_error)) {
             $this->setError($link->last_error);
             return false;
         }
         if ($link->last_result === null) {
             /* foreach($link->error->errors as $key => $errors) {
                if ($key == db_connect_fail)
                }*/
             $this->setError(print_r(implode("\n", $link->error->errors), 1));
             return false;
         }
         for ($i = 0; $i < $n; $i++) {
             $row = array_values(get_object_vars($result[$i]));
             $tables[] = $row[0];
         }
         foreach ($tables as $table) {
             $return = '';
             $proc_data = WPAdm_Running::getCommandResultData('db');
             if (!isset($proc_data['mysqldump_table'][$table])) {
                 $result = $link->last_result;
                 if (!empty($link->last_error) && $n > 0) {
                     $this->setError($link->last_error);
                 }
                 $return .= 'DROP TABLE IF EXISTS ' . $table . ';';
                 $ress = $link->query('SHOW CREATE TABLE ' . $table);
                 $result2 = $link->last_result;
                 if (!empty($link->last_error) && $n > 0) {
                     $this->setError($link->last_error);
                 }
                 $row2 = array_values(get_object_vars($result2[0]));
                 $return .= "\n\n" . $row2[1] . ";\n\n";
                 file_put_contents($filename, $return, FILE_APPEND);
                 $proc_data = WPAdm_Running::getCommandResultData('db');
                 $proc_data['mysqldump_table'][$table] = 1;
                 WPAdm_Running::setCommandResultData('db', $proc_data);
                 $log = str_replace('%s', $table, langWPADM::get('Add a table "%s" in the database dump', false));
                 WPAdm_Core::log($log);
             }
             $while = true;
             while ($while) {
                 $table_db = WPAdm_Running::getCommandResultData('tabledb');
                 if (isset($table_db[$table])) {
                     if (isset($table_db[$table]['work']) && $table_db[$table]['work'] == true) {
                         $from = $table_db[$table]['from'];
                         // value from
                         $to = $table_db[$table]['to'];
                         // value to
                     }
                 } else {
                     $from = 0;
                     $to = $this->rows;
                 }
                 if (isset($from) && !empty($to) && $from >= 0 && $to >= 0) {
                     unset($link);
                     $link = $this->connect($db);
                     $num_fields = $link->query('SELECT * FROM ' . $table . " LIMIT {$from}, {$to}");
                     if ($num_fields > 0) {
                         $result2 = $link->last_result;
                         for ($i = 0; $i < $num_fields; $i++) {
                             $return = '';
                             $row = array_values(get_object_vars($result2[$i]));
                             //WPAdm_Core::log('row' . print_r($row, 1));
                             $rows_num = count($row);
                             if ($rows_num > 0) {
                                 $return .= 'INSERT INTO ' . $table . ' VALUES(';
                                 for ($j = 0; $j < $rows_num; $j++) {
                                     $row[$j] = addslashes($row[$j]);
                                     $row[$j] = str_replace("\n", "\\n", $row[$j]);
                                     if (isset($row[$j])) {
                                         $return .= '"' . $row[$j] . '"';
                                     } else {
                                         $return .= '""';
                                     }
                                     if ($j < $rows_num - 1) {
                                         $return .= ',';
                                     }
                                 }
                                 $return .= ");\n";
                                 file_put_contents($filename, $return, FILE_APPEND);
                                 $from += 1;
                                 $table_db[$table]['from'] = $from;
                                 $table_db[$table]['to'] = $to;
                                 $table_db[$table]['work'] = true;
                                 WPAdm_Running::setCommandResultData('tabledb', $table_db);
                             }
                         }
                         $log = str_replace(array('%s', '%from%', '%to%'), array($table, $from, $to), langWPADM::get('Add a table rows "%s" in the database dump from %from% to %to%', false));
                         WPAdm_Core::log($log);
                     } else {
                         $while = false;
                         $table_db[$table]['work'] = false;
                         WPAdm_Running::setCommandResultData('tabledb', $table_db);
                     }
                 } else {
                     $while = false;
                     $table_db[$table]['work'] = false;
                     WPAdm_Running::setCommandResultData('tabledb', $table_db);
                 }
             }
             if (!isset($proc_data['mysqldump_table'][$table])) {
                 $return = "\n\n\n";
                 file_put_contents($filename, $return, FILE_APPEND);
             }
         }
         unset($link);
         WPAdm_Core::log(langWPADM::get('MySQL of Dump was finished', false));
         $proc_data = WPAdm_Running::getCommandResultData('db');
         $proc_data['mysqldump'] = true;
         WPAdm_Running::setCommandResultData('db', $proc_data);
         return true;
     } else {
         return false;
     }
 }