예제 #1
0
 /**
  * Generates ZIP archive
  * @return string|WP_Error
  */
 protected function _generateArchive()
 {
     global $blog_id;
     set_time_limit(0);
     // Prepare archive directory
     $uploadDir = wp_upload_dir();
     $exporter = wp_get_current_user();
     $archiveName = $uploadDir['path'] . '/' . self::HOOK . '-' . $blog_id . '-' . time() . '-' . $exporter->user_login;
     $archiveDir = $archiveName . '/';
     if (!file_exists($archiveDir)) {
         wp_mkdir_p($archiveDir);
     }
     // Prepare queue
     $baseUrl = untrailingslashit(home_url());
     $newBaseUrl = untrailingslashit($this->_options->getOption('baseUrl'));
     $urlsQueue = array_unique(array_merge(array(trailingslashit($baseUrl)), $this->_getListOfLocalFilesByUrl(array(get_template_directory_uri())), $this->_getListOfLocalFilesByUrl(explode("\n", $this->_options->getOption('additionalUrls')))));
     // Process queue
     $this->_exportLog = array();
     while (count($urlsQueue)) {
         $currentUrl = array_shift($urlsQueue);
         //echo "Processing ". $currentUrl."<br />";
         $urlResponse = new StaticHtmlOutput_UrlRequest($currentUrl);
         $urlResponse->cleanup();
         // Add current url to the list of processed urls
         $this->_exportLog[$currentUrl] = true;
         // Add new urls to the queue
         foreach ($urlResponse->extractAllUrls($baseUrl) as $newUrl) {
             if (!isset($this->_exportLog[$newUrl]) && $newUrl != $currentUrl && !in_array($newUrl, $urlsQueue)) {
                 //echo "Adding ".$newUrl." to the list<br />";
                 $urlsQueue[] = $newUrl;
             }
         }
         // Save url data
         $urlResponse->replaceBaseUlr($baseUrl, $newBaseUrl);
         $this->_saveUrlData($urlResponse, $archiveDir);
     }
     // Create archive object
     $tempZip = $archiveName . '.tmp';
     $zipArchive = new ZipArchive();
     if ($zipArchive->open($tempZip, ZIPARCHIVE::CREATE) !== true) {
         return new WP_Error('Could not create archive');
     }
     $iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($archiveDir));
     foreach ($iterator as $fileName => $fileObject) {
         $baseName = basename($fileName);
         if ($baseName != '.' && $baseName != '..') {
             if (!$zipArchive->addFile(realpath($fileName), str_replace($archiveDir, '', $fileName))) {
                 return new WP_Error('Could not add file: ' . $fileName);
             }
         }
     }
     $zipArchive->close();
     rename($tempZip, $archiveName . '.zip');
     if ($this->_options->getOption('sendViaFTP') == 1) {
         //crude FTP addition
         require_once '/home/leon/leonwp/wp-content/plugins/static-html-output-plugin/library/FTP/ftp.php';
         $config = array();
         //keys[passive_mode(true|false)|transfer_mode(FTP_ASCII|FTP_BINARY)|reattempts(int)|log_path|verbose(true|false)|create_mask(default:0777)]
         $ftp = new ftp($config);
         $ftp->conn($this->_options->getOption('ftpServer'), $this->_options->getOption('ftpUsername'), filter_input(INPUT_POST, 'ftpPassword'));
         //Crude FTP
         $ftp->put($this->_options->getOption('ftpRemotePath'), $archiveName . '/');
         unset($ftp);
     }
     // Remove temporary files unless user requested to keep or needed for FTP transfer
     if ($this->_options->getOption('retainStaticFiles') != 1) {
         $iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($archiveDir), RecursiveIteratorIterator::CHILD_FIRST);
         foreach ($iterator as $fileName => $fileObject) {
             // Remove file
             if ($fileObject->isDir()) {
                 // Ignore special dirs
                 $dirName = basename($fileName);
                 if ($dirName != '.' && $dirName != '..') {
                     rmdir($fileName);
                 }
             } else {
                 unlink($fileName);
             }
         }
         rmdir($archiveDir);
     }
     return str_replace(ABSPATH, trailingslashit(home_url()), $archiveName . '.zip');
 }