Exemplo n.º 1
0
 function uploadFile($file, $offset = 0)
 {
     $dirs = MainWP_Helper::getMainWPDir('backup');
     $backupdir = $dirs[0];
     header('Content-Description: File Transfer');
     header('Content-Description: File Transfer');
     if (MainWP_Helper::endsWith($file, '.tar.gz')) {
         header('Content-Type: application/x-gzip');
         header("Content-Encoding: gzip'");
     } else {
         header('Content-Type: application/octet-stream');
     }
     header('Content-Disposition: attachment; filename="' . basename($file) . '"');
     header('Expires: 0');
     header('Cache-Control: must-revalidate');
     header('Pragma: public');
     header('Content-Length: ' . filesize($backupdir . $file));
     while (@ob_end_flush()) {
     }
     $this->readfile_chunked($backupdir . $file, $offset);
 }
Exemplo n.º 2
0
 private function addFile($path, $entryName)
 {
     if ('.' == basename($path) || '..' == basename($path)) {
         return false;
     }
     if ($this->excludeZip && MainWP_Helper::endsWith($path, '.zip')) {
         $this->log('Skipping ' . $path);
         return false;
     }
     $this->log('Adding ' . $path);
     //        if ($this->limit)
     //        {
     //            $this->cnt++;
     //
     //            if ($this->cnt > 250) throw new Exception('Some error..' . $this->archivePath);
     //        }
     $this->updatePidFile();
     $rslt = false;
     if ($this->mode == self::APPEND) {
         $rslt = $this->checkBeforeAppend($entryName);
         if (true === $rslt) {
             return true;
         }
     }
     if (time() - $this->lastRun > 60) {
         @set_time_limit(20 * 60 * 60);
         /*20 minutes*/
         $this->lastRun = time();
     }
     $this->gcCnt++;
     if ($this->gcCnt > 20) {
         if (function_exists('gc_enable')) {
             @gc_enable();
         }
         if (function_exists('gc_collect_cycles')) {
             @gc_collect_cycles();
         }
         $this->gcCnt = 0;
     }
     $stat = @stat($path);
     $fp = @fopen($path, 'rb');
     if (!$fp) {
         //todo: add some error feedback!
         return;
     }
     $prefix = '';
     if (strlen($entryName) > 99) {
         $prefix = substr($entryName, 0, strpos($entryName, '/', strlen($entryName) - 100) + 1);
         $entryName = substr($entryName, strlen($prefix));
         if (strlen($prefix) > 154 || strlen($entryName) > 99) {
             $entryName = $prefix . $entryName;
             $prefix = '';
             $block = pack('a100a8a8a8a12a12a8a1a100a6a2a32a32a8a8a155a12', '././@LongLink', sprintf('%07o', 0), sprintf('%07o', 0), sprintf('%07o', 0), sprintf('%011o', strlen($entryName)), sprintf('%011o', 0), '        ', 'L', '', 'ustar', ' ', '', '', '', '', '', '');
             $checksum = 0;
             for ($i = 0; $i < 512; $i++) {
                 $checksum += ord(substr($block, $i, 1));
             }
             $checksum = pack('a8', sprintf('%07o', $checksum));
             $block = substr_replace($block, $checksum, 148, 8);
             if (!isset($rslt['bytesRead'])) {
                 $this->addData($block);
             }
             if (!isset($rslt['bytesRead'])) {
                 $this->addData(pack('a512', $entryName));
             }
             $entryName = substr($entryName, 0, 100);
         }
     }
     $this->block = pack('a100a8a8a8a12a12a8a1a100a6a2a32a32a8a8a155a12', $entryName, sprintf('%07o', $stat['mode']), sprintf('%07o', $stat['uid']), sprintf('%07o', $stat['gid']), sprintf('%011o', $stat['size']), sprintf('%011o', $stat['mtime']), '        ', 0, '', 'ustar', ' ', 'Unknown', 'Unknown', '', '', $prefix, '');
     $checksum = 0;
     for ($i = 0; $i < 512; $i++) {
         $checksum += ord(substr($this->block, $i, 1));
     }
     $checksum = pack('a8', sprintf('%07o', $checksum));
     $this->block = substr_replace($this->block, $checksum, 148, 8);
     if (!isset($rslt['bytesRead'])) {
         $this->addData($this->block);
     }
     if (isset($rslt['bytesRead'])) {
         @fseek($fp, $rslt['bytesRead']);
         $alreadyRead = $rslt['bytesRead'] % 512;
         $toRead = 512 - $alreadyRead;
         if ($toRead > 0) {
             $this->tempContent = fread($fp, $toRead);
             $this->addData($this->tempContent);
             $remainder = 512 - (strlen($this->tempContent) + $alreadyRead);
             $this->log('DEBUG-Added ' . strlen($this->tempContent) . '(before: ' . $alreadyRead . ') will pack: ' . $remainder . ' (packed: ' . strlen(pack('a' . $remainder, '')));
             if ($remainder > 0) {
                 $this->addData(pack('a' . $remainder), '');
             }
         }
     }
     while (!feof($fp)) {
         //0.1MB = 1024 000
         $this->tempContent = fread($fp, 1024000 * 5);
         $read = strlen($this->tempContent);
         $divide = $read % 512;
         $this->addData(substr($this->tempContent, 0, $read - $divide));
         if ($divide > 0) {
             $this->addData(pack('a512', substr($this->tempContent, -1 * $divide)));
         }
         $this->updatePidFile();
         //            if ($this->limit) throw new Exception('Some error..' . $entryName);
     }
     @fclose($fp);
     return true;
 }
Exemplo n.º 3
0
 public static function inExcludes($excludes, $value)
 {
     if (empty($value)) {
         return false;
     }
     if (null != $excludes) {
         foreach ($excludes as $exclude) {
             if (MainWP_Helper::endsWith($exclude, '*')) {
                 if (MainWP_Helper::startsWith($value, substr($exclude, 0, strlen($exclude) - 1))) {
                     return true;
                 }
             } else {
                 if ($value == $exclude) {
                     return true;
                 } else {
                     if (MainWP_Helper::startsWith($value, $exclude . '/')) {
                         return true;
                     }
                 }
             }
         }
     }
     return false;
 }
Exemplo n.º 4
0
 function addFileToZip($path, $zipEntryName)
 {
     if (time() - $this->lastRun > 20) {
         // @codingStandardsIgnoreStart
         @set_time_limit($this->timeout);
         // @codingStandardsIgnoreEnd
         $this->lastRun = time();
     }
     if ($this->excludeZip && MainWP_Helper::endsWith($path, '.zip')) {
         return false;
     }
     // this would fail with status ZIPARCHIVE::ER_OPEN
     // after certain number of files is added since
     // ZipArchive internally stores the file descriptors of all the
     // added files and only on close writes the contents to the ZIP file
     // see: http://bugs.php.net/bug.php?id=40494
     // and: http://pecl.php.net/bugs/bug.php?id=9443
     // return $zip->addFile( $path, $zipEntryName );
     $this->zipArchiveSizeCount += filesize($path);
     $this->gcCnt++;
     //5 mb limit!
     if (!$this->loadFilesBeforeZip || filesize($path) > 5 * 1024 * 1024) {
         $this->zipArchiveFileCount++;
         $added = $this->zip->addFile($path, $zipEntryName);
     } else {
         $this->zipArchiveFileCount++;
         $this->testContent = file_get_contents($path);
         if ($this->testContent === false) {
             return false;
         }
         $added = $this->zip->addFromString($zipEntryName, $this->testContent);
     }
     if ($this->gcCnt > 20) {
         // @codingStandardsIgnoreStart
         if (function_exists('gc_enable')) {
             @gc_enable();
         }
         if (function_exists('gc_collect_cycles')) {
             @gc_collect_cycles();
         }
         // @codingStandardsIgnoreEnd
         $this->gcCnt = 0;
     }
     //Over limits?
     if ($this->file_descriptors > 0 && $this->zipArchiveFileCount > $this->file_descriptors) {
         // || $this->zipArchiveSizeCount >= (31457280 * 2))
         $this->zip->close();
         $this->zip = null;
         unset($this->zip);
         // @codingStandardsIgnoreStart
         if (function_exists('gc_enable')) {
             @gc_enable();
         }
         if (function_exists('gc_collect_cycles')) {
             @gc_collect_cycles();
         }
         // @codingStandardsIgnoreEnd
         $this->zip = new ZipArchive();
         $this->zip->open($this->zipArchiveFileName);
         $this->zipArchiveFileCount = 0;
         $this->zipArchiveSizeCount = 0;
     }
     return $added;
 }