Example #1
0
 protected function __construct(Menu $xMenu)
 {
     $this->menu = $xMenu;
     $rewp = new ReWP(WP_VAGRANTIZE_HOME . COMPOSER_DIR . '/amekusa/ReWP');
     $this->actions = array(new AjaxAction('saveSettings', function () use($rewp) {
         if (!$_POST) {
             wp_send_json_error();
         }
         if (!array_key_exists('data', $_POST)) {
             wp_send_json_error();
         }
         $data = array();
         parse_str($_POST['data'], $data);
         $rewp->setData($data);
         $dest = $rewp->exportData();
         if (!$dest) {
             wp_send_json_error();
         } else {
             $time = filemtime($dest);
             wp_send_json_success(array('file' => $dest, 'date' => date(get_option('time_format') . ', ' . get_option('date_format'), $time), 'datetime' => date(DATE_W3C, $time)));
         }
     }), new AjaxAction('resetSettings', function () use($rewp) {
         if (!$rewp->reset()) {
             wp_send_json_error();
         } else {
             wp_send_json_success();
         }
     }), new AjaxAction('renderSettingsTable', function () use($rewp) {
         $data = $rewp->getData();
         ob_start();
         include __DIR__ . '/view/SettingsTable.php';
         wp_send_json_success(ob_get_clean());
     }), new AjaxAction('download', function () use($rewp) {
         $name = $rewp->getData('hostname');
         if (!$name) {
             $name = $rewp->getData('ip');
         }
         if (!$name) {
             $name = 'vagrantme.up';
         }
         $name = preg_replace('/^[^a-zA-Z0-9_-]+/', '', $name);
         $name = preg_replace('/[^a-zA-Z0-9_-]+$/', '', $name);
         $name = preg_replace('/[^a-zA-Z0-9_.-]/', '_', $name);
         $dest = WP_VAGRANTIZE_HOME . ".exports/{$name}." . date('YmdHis') . '.zip';
         $zip = new \ZipArchiveEx();
         $zip->open($dest, \ZipArchive::OVERWRITE);
         $zip->addDirContents($rewp->getPath());
         $db = '';
         if ($rewp->getData('import_sql')) {
             $db = $rewp->exportDB(WP_VAGRANTIZE_HOME . '.exports');
             $zip->addFile($db, basename($db));
         }
         $zip->close();
         if ($db) {
             unlink($db);
         }
         $time = filemtime($dest);
         wp_send_json_success(array('file' => $dest, 'fileUrl' => WP_VAGRANTIZE_URL . 'download.php?file=' . basename($dest), 'date' => date(get_option('time_format') . ', ' . get_option('date_format'), $time), 'datetime' => date(DATE_W3C, $time)));
     }));
     // @formatter:on
     foreach ($this->actions as $iAct) {
         $iAct->register();
     }
     add_action('load-' . $this->getId(), array($this, 'setup'));
 }
 /**
  * Helper to create a ZIP out of a data file and the configuration file
  */
 protected function DoZip($aFiles, $sZipArchiveFile)
 {
     foreach ($aFiles as $aFile) {
         $sFile = $aFile['source'];
         if (!is_file($sFile) && !is_dir($sFile)) {
             throw new BackupException("File '{$sFile}' does not exist or could not be read");
         }
     }
     // Make sure the target path exists
     $sZipDir = dirname($sZipArchiveFile);
     SetupUtils::builddir($sZipDir);
     $oZip = new ZipArchiveEx();
     $res = $oZip->open($sZipArchiveFile, ZipArchive::CREATE | ZipArchive::OVERWRITE);
     if ($res === TRUE) {
         foreach ($aFiles as $aFile) {
             if (is_dir($aFile['source'])) {
                 $oZip->addDir($aFile['source'], $aFile['dest']);
             } else {
                 $oZip->addFile($aFile['source'], $aFile['dest']);
             }
         }
         if ($oZip->close()) {
             $this->LogInfo("Archive: {$sZipArchiveFile} created");
         } else {
             $this->LogError("Failed to save zip archive: {$sZipArchiveFile}");
             throw new BackupException("Failed to save zip archive: {$sZipArchiveFile}");
         }
     } else {
         $this->LogError("Failed to create zip archive: {$sZipArchiveFile}.");
         throw new BackupException("Failed to create zip archive: {$sZipArchiveFile}.");
     }
 }
 public function downloadZip($content_id, $file_id)
 {
     $file = File::find($file_id);
     if (!$file || !in_array($content_id, $file->content_ids)) {
         return redirect('/');
     }
     $photo_dir = $file->server_path;
     if (!\File::isDirectory(storage_path() . '/download_content/')) {
         \File::makeDirectory(storage_path() . '/download_content/', 0777);
     }
     $zipfile = $file_id . ".zip";
     $zip = new \ZipArchiveEx();
     $zip->open(storage_path() . '/download_content/' . $zipfile, \ZIPARCHIVE::CREATE);
     $source = $photo_dir;
     $files = \File::files($source);
     foreach ($files as $file) {
         $file = str_replace($source . '/', '', $file);
         $imagepath = $source . "/" . $file;
         $zip->addFile($imagepath, $file);
     }
     $zip->close();
     $ext = ".zip";
     $pathToFile = storage_path() . '/download_content/' . $zipfile;
     $name = $this->createFileName($content_id, $zipfile);
     $this->saveDownloadInfo($content_id, $file_id, 'zip', $pathToFile);
     return response()->download($pathToFile, $name)->deleteFileAfterSend(true);
 }