function display_from_archive($content_map = array())
 {
     // Set default values
     $this->params = array_merge(array('pattern' => '@^[/]([^/]+)[/](.*)$@', 'vpath' => $_SERVER["PATH_INFO"], 'archive_subdir' => true, 'zipfile' => true, 'fix_dir' => false, 'archive_dir' => ARCHIVE_DIR, 'archive_file_prefix' => ARCHIVE_FILE_PREFIX, 'use_http_expire_date' => false, 'override_extractor' => null, 'title' => NULL, 'charset' => NULL, 'content' => NULL, 'error' => false), $this->params);
     $this->get_archive_location();
     // Only use a permanent redirect for releases (beta or full).
     $redirect_status_code = $this->params['version'] && $this->params['version']->is_numbered_release() ? 301 : 302;
     // Calculate expiry date if requested.
     $expires = null;
     if ($this->params['use_http_expire_date']) {
         if (!$this->params['version']) {
             $expires = "+1 week";
         } else {
             $compare_version = BoostVersion::from($this->params['version'])->compare(BoostVersion::current());
             $expires = $compare_version === -1 ? "+1 year" : ($compare_version === 0 ? "+1 week" : "+1 day");
         }
     }
     // Check file exists.
     if ($this->params['zipfile']) {
         $check_file = $this->params['archive'];
         if (!is_readable($check_file)) {
             error_page($this->params, 'Unable to find zipfile.');
             return;
         }
     } else {
         $check_file = $this->params['file'];
         if (is_dir($check_file)) {
             if (substr($check_file, -1) != '/') {
                 $redirect = resolve_url(basename($check_file) . '/');
                 header("Location: {$redirect}", TRUE, $redirect_status_code);
                 return;
             }
             $found_file = NULL;
             if (is_readable("{$check_file}/index.html")) {
                 $found_file = 'index.html';
             } else {
                 if (is_readable("{$check_file}/index.htm")) {
                     $found_file = 'index.htm';
                 }
             }
             if ($found_file) {
                 $this->params['file'] = $check_file = $check_file . $found_file;
                 $this->params['key'] = $this->params['key'] . $found_file;
             } else {
                 if (!http_headers('text/html', filemtime($check_file), $expires)) {
                     return;
                 }
                 $display_dir = new BoostDisplayDir($this->params);
                 return $display_dir->display($check_file);
             }
         } else {
             if (!is_readable($check_file)) {
                 error_page($this->params, 'Unable to find file.');
                 return;
             }
         }
     }
     // Choose filter to use
     $info_map = array_merge($content_map, array(array('', '@[.](txt|py|rst|jam|v2|bat|sh|xml|xsl|toyxml)$@i', 'text', 'text/plain'), array('', '@[.](qbk|quickbook)$@i', 'qbk', 'text/plain'), array('', '@[.](c|h|cpp|hpp)$@i', 'cpp', 'text/plain'), array('', '@[.]png$@i', 'raw', 'image/png'), array('', '@[.]gif$@i', 'raw', 'image/gif'), array('', '@[.](jpg|jpeg|jpe)$@i', 'raw', 'image/jpeg'), array('', '@[.]svg$@i', 'raw', 'image/svg+xml'), array('', '@[.]css$@i', 'raw', 'text/css'), array('', '@[.]js$@i', 'raw', 'application/x-javascript'), array('', '@[.]pdf$@i', 'raw', 'application/pdf'), array('', '@[.](html|htm)$@i', 'raw', 'text/html'), array('', '@(/|^)(Jamroot|Jamfile|ChangeLog|configure)$@i', 'text', 'text/plain'), array('', '@[.]dtd$@i', 'raw', 'application/xml-dtd')));
     $preprocess = null;
     $extractor = null;
     $type = null;
     foreach ($info_map as $i) {
         if (preg_match($i[1], $this->params['key'])) {
             if ($i[0]) {
                 $version = BoostVersion::from($i[0]);
                 if ($version->compare(BoostVersion::page()) > 0) {
                     // This is after the current version.
                     continue;
                 }
             }
             $extractor = $i[2];
             $type = $i[3];
             $preprocess = isset($i[4]) ? $i[4] : NULL;
             break;
         }
     }
     if ($this->params['override_extractor']) {
         $extractor = $this->params['override_extractor'];
     }
     if (!$extractor) {
         if (strpos($_SERVER['HTTP_HOST'], 'www.boost.org') === false) {
             error_page($this->params, "No extractor found for filename.");
         } else {
             error_page($this->params);
         }
         return;
     }
     // Handle ETags and Last-Modified HTTP headers.
     // Output raw files.
     if ($extractor == 'raw') {
         if (!http_headers($type, filemtime($check_file), $expires)) {
             return;
         }
         display_raw_file($this->params, $_SERVER['REQUEST_METHOD'], $type);
     } else {
         // Read file from hard drive or zipfile
         // Note: this sets $this->params['content'] with either the
         // content or an error message.
         if (!extract_file($this->params)) {
             error_page($this->params, $this->params['content']);
             return;
         }
         // Check if the file contains a redirect.
         if ($type == 'text/html') {
             if ($redirect = detect_redirect($this->params['content'])) {
                 http_headers('text/html', null, "+1 day");
                 header("Location: {$redirect}", TRUE, $redirect_status_code);
                 if ($_SERVER['REQUEST_METHOD'] != 'HEAD') {
                     echo $this->params['content'];
                 }
                 return;
             }
         }
         if (!http_headers('text/html', filemtime($check_file), $expires)) {
             return;
         }
         // Finally process the file and display it.
         if ($_SERVER['REQUEST_METHOD'] != 'HEAD') {
             if ($preprocess) {
                 $this->params['content'] = call_user_func($preprocess, $this->params['content']);
             }
             echo_filtered($extractor, $this->params);
         }
     }
 }
Example #2
0
{
    while (!$file_comp->eof()) {
        $byte = $file_comp->fread(1);
        $count = $file_comp->fread(1);
        display_percent($file_comp);
        if ($file_comp->eof()) {
            break;
        }
        $count = unpack('C', $count);
        $count = array_shift($count);
        // $s = bin2hex($byte);
        // echo "{$s}:{$count}\n";
        for ($i = 0; $i < $count; $i++) {
            $file_target->fwrite($byte);
        }
    }
}
$mode = $argv[1];
// c, x
$comp_file = $argv[2];
$target_file = $argv[3];
if ($mode == 'c') {
    $file_comp = new SplFileObject($comp_file, 'w');
    $file_target = new SplFileObject($target_file);
    compress_file($file_comp, $file_target);
} elseif ($mode == 'x') {
    $file_comp = new SplFileObject($comp_file);
    $file_target = new SplFileObject($target_file, 'w');
    extract_file($file_comp, $file_target);
}
echo "\n";