Example #1
0
 public function exec_restore($params = false)
 {
     ignore_user_abort(true);
     ini_set('memory_limit', '512M');
     if (!strstr(INI_SYSTEM_CHECK_DISABLED, 'set_time_limit')) {
         set_time_limit(0);
     }
     $loc = $this->backup_file;
     // Get the provided arg
     if (isset($params['id'])) {
         $id = $params['id'];
     } elseif (isset($_GET['filename'])) {
         $id = $params['filename'];
     } elseif (isset($_GET['file'])) {
         $id = $params['file'];
     } elseif ($loc != false) {
         $id = $loc;
     }
     if ($id == null) {
         return array('error' => 'You have not provided a backup to restore.');
     }
     $here = $this->get_bakup_location();
     $filename = $here . $id;
     $ext = get_file_extension($filename);
     $ext_error = false;
     $sql_file = false;
     if (!is_file($filename)) {
         return array('error' => 'You have not provided a existing backup to restore.');
         die;
     }
     $temp_dir_restore = false;
     switch ($ext) {
         case 'zip':
             $back_log_action = 'Unzipping userfiles';
             $this->log_action($back_log_action);
             $exract_folder = md5(basename($filename));
             $unzip = new \Microweber\Utils\Unzip();
             $target_dir = mw_cache_path() . 'backup_restore' . DS . $exract_folder . DS;
             if (!is_dir($target_dir)) {
                 mkdir_recursive($target_dir);
             }
             $result = $unzip->extract($filename, $target_dir, $preserve_filepath = true);
             $temp_dir_restore = $target_dir;
             $sql_restore = $target_dir . 'mw_sql_restore.sql';
             if (is_file($sql_restore)) {
                 $sql_file = $sql_restore;
             }
             break;
         case 'sql':
             $sql_file = $filename;
             break;
         default:
             $ext_error = true;
             break;
     }
     if ($ext_error == true) {
         return array('error' => 'Invalid file extension. The restore file must be .sql or .zip');
         die;
     }
     if ($sql_file != false) {
         $back_log_action = 'Restoring database';
         $this->log_action($back_log_action);
         $filename = $sql_file;
         $sqlErrorText = '';
         $sqlErrorCode = 0;
         $sqlStmt = '';
         $sqlFile = file_get_contents($filename);
         $sqlArray = explode($this->file_q_sep, $sqlFile);
         if (!isset($sqlArray[1])) {
             $sqlArray = explode("\n", $sqlFile);
         }
         // Process the sql file by statements
         $engine = mw()->database_manager->get_sql_engine();
         foreach ($sqlArray as $stmt) {
             $stmt = str_replace('/* MW_TABLE_SEP */', ' ', $stmt);
             $stmt = str_ireplace($this->prefix_placeholder, get_table_prefix(), $stmt);
             $stmt = str_replace("", '', $stmt);
             //                $stmt = str_replace("\x0D", '', $stmt);
             //                $stmt = str_replace("\x09", '', $stmt);
             ////
             //                $stmt = preg_replace('/[\x00-\x08\x0B\x0C\x0E-\x1F\x80-\x9F]/u', '', $stmt);
             if ($engine == 'sqlite') {
                 $stmt = str_replace("\\'", "''", $stmt);
             }
             if ($this->debug) {
                 d($stmt);
             }
             if (strlen(trim($stmt)) > 3) {
                 try {
                     @mw()->database_manager->q($stmt, true);
                     // mw()->database_manager->q($stmt);
                 } catch (QueryException $e) {
                     echo 'Caught exception: ' . $e->getMessage() . "\n";
                     $sqlErrorCode = 1;
                 } catch (Exception $e) {
                     echo 'Caught exception: ' . $e->getMessage() . "\n";
                     $sqlErrorCode = 1;
                 }
             }
         }
         // Print message (error or success)
         if ($sqlErrorCode == 0) {
             $back_log_action = 'Database restored!';
             $this->log_action($back_log_action);
             echo "Database restored!\n";
             echo 'Backup used: ' . $filename . "\n";
         } else {
             echo "An error occurred while restoring backup!<br><br>\n";
             echo "Error code: {$sqlErrorCode}<br>\n";
             echo "Error text: {$sqlErrorText}<br>\n";
             echo "Statement:<br/> {$sqlStmt}<br>";
         }
         $back_log_action = 'Database restored!';
         $this->log_action($back_log_action);
         echo "Files restored successfully!<br>\n";
         echo 'Backup used: ' . $filename . "<br>\n";
         if ($temp_dir_restore != false) {
             @unlink($filename);
         }
     }
     if (userfiles_path()) {
         if (!is_dir(userfiles_path())) {
             mkdir_recursive(userfiles_path());
         }
     }
     if (media_base_path()) {
         if (!is_dir(media_base_path())) {
             mkdir_recursive(media_base_path());
         }
     }
     if ($temp_dir_restore != false and is_dir($temp_dir_restore)) {
         echo "Media restored!<br>\n";
         $srcDir = $temp_dir_restore;
         $destDir = userfiles_path();
         $copy = $this->copyr($srcDir, $destDir);
     }
     if (function_exists('mw_post_update')) {
         mw_post_update();
     }
     $back_log_action = 'Cleaning up cache';
     $this->log_action($back_log_action);
     mw()->cache_manager->clear();
     $this->log_action(false);
 }
 private function install_from_remote($url)
 {
     $fname = basename($url);
     $dir_c = mw_cache_path() . 'downloads' . DS;
     if (!is_dir($dir_c)) {
         mkdir_recursive($dir_c);
     }
     $dl_file = $dir_c . $fname;
     if (!is_file($dl_file)) {
         $get = $this->app->url_manager->download($url, $post_params = false, $save_to_file = $dl_file);
     }
     if (is_file($dl_file)) {
         $unzip = new \Microweber\Utils\Unzip();
         $target_dir = MW_ROOTPATH;
         $result = $unzip->extract($dl_file, $target_dir, $preserve_filepath = true);
         return $result;
     }
 }