Ejemplo n.º 1
0
 public static function patchFile($item, $info, $algorithm, $patch)
 {
     switch ($algorithm) {
         case 'bsdiff':
         case 'xdelta':
         case 'vcdiff':
             break;
         case 'xdiff':
             if (!function_exists('xdiff_file_patch_binary')) {
                 throw new Exception("=xdiff not available");
             }
             break;
         default:
             throw new Exception("Invalid algorithm '{$algorithm}'", Z_ERROR_INVALID_INPUT);
     }
     $originalInfo = Zotero_S3::getLocalFileItemInfo($item);
     $basePath = "/tmp/zfsupload/";
     $path = $basePath . $info->hash . "_" . uniqid() . "/";
     mkdir($path, 0777, true);
     $cleanup = function () use($basePath, $path) {
         unlink("original");
         unlink("patch");
         unlink("new");
         chdir($basePath);
         rmdir($path);
     };
     $e = null;
     try {
         // Download file from S3 to temp directory
         if (!Zotero_S3::downloadFile($originalInfo, $path, "original")) {
             throw new Exception("Error downloading original file");
         }
         chdir($path);
         // Save body to temp file
         file_put_contents("patch", $patch);
         // Patch file
         switch ($algorithm) {
             case 'bsdiff':
                 exec('bspatch original new patch 2>&1', $output, $ret);
                 if ($ret) {
                     throw new Exception("Error applying patch ({$ret}): " . implode("\n", $output));
                 }
                 if (!file_exists("new")) {
                     throw new Exception("Error applying patch ({$ret})");
                 }
                 break;
             case 'xdelta':
             case 'vcdiff':
                 exec('xdelta3 -d -s original patch new 2>&1', $output, $ret);
                 if ($ret) {
                     if ($ret == 2) {
                         Z_HTTP::e400("Invalid delta");
                     }
                     throw new Exception("Error applying patch ({$ret}): " . implode("\n", $output));
                 }
                 if (!file_exists("new")) {
                     throw new Exception("Error applying patch ({$ret})");
                 }
                 break;
             case 'xdiff':
                 $ret = xdiff_file_patch_binary("original", "patch", "new");
                 if (!$ret) {
                     throw new Exception("Error applying patch");
                 }
                 break;
         }
         // Check MD5 hash
         if (md5_file("new") != $info->hash) {
             $cleanup();
             Z_HTTP::e409("Patched file does not match hash");
         }
         // Check file size
         if (filesize("new") != $info->size) {
             $cleanup();
             Z_HTTP::e409("Patched file size does not match (" . filesize("new") . " != {$info->size})");
         }
         // If ZIP, make sure it's a ZIP
         if ($info->zip && file_get_contents("new", false, null, 0, 4) != "PK" . chr(03) . chr(04)) {
             $cleanup();
             Z_HTTP::e409("Patched file is not a ZIP file");
         }
         // Upload to S3
         $t = $info->contentType . ($info->contentType && $info->charset ? "; charset={$info->charset}" : "");
         $storageFileID = Zotero_S3::uploadFile($info, "new", $t);
     } catch (Exception $e) {
         //$cleanup();
         throw $e;
     }
     return $storageFileID;
 }
Ejemplo n.º 2
0
 /**
  * Download ZIP file from S3, extract it, and return a temporary URL
  * pointing to the main file
  */
 public static function getTemporaryURL(Zotero_Item $item, $localOnly = false)
 {
     $extURLPrefix = Z_CONFIG::$ATTACHMENT_SERVER_URL;
     if ($extURLPrefix[strlen($extURLPrefix) - 1] != "/") {
         $extURLPrefix .= "/";
     }
     $info = Zotero_S3::getLocalFileItemInfo($item);
     $storageFileID = $info['storageFileID'];
     $filename = $info['filename'];
     $mtime = $info['mtime'];
     if (!$info['zip']) {
         throw new Exception("Not a zip attachment");
     }
     $realFilename = preg_replace("/^storage:/", "", $item->attachmentPath);
     $realFilename = self::decodeRelativeDescriptorString($realFilename);
     $realFilename = urlencode($realFilename);
     $docroot = Z_CONFIG::$ATTACHMENT_SERVER_DOCROOT;
     //Z_Core::$debug = true;
     // Check memcached to see if file is already extracted
     $key = "attachmentServerString_" . $storageFileID . "_" . $mtime;
     if ($randomStr = Z_Core::$MC->get($key)) {
         Z_Core::debug("Got attachment path '{$randomStr}/{$realFilename}' from memcached");
         return $extURLPrefix . "{$randomStr}/{$realFilename}";
     }
     $localAddr = gethostbyname(gethostname());
     // See if this is an attachment host
     $index = false;
     $skipHost = false;
     for ($i = 0, $len = sizeOf(Z_CONFIG::$ATTACHMENT_SERVER_HOSTS); $i < $len; $i++) {
         $hostAddr = gethostbyname(Z_CONFIG::$ATTACHMENT_SERVER_HOSTS[$i]);
         if ($hostAddr == $localAddr) {
             // Make a HEAD request on the local static port to make sure
             // this host is actually functional
             $url = "http://" . Z_CONFIG::$ATTACHMENT_SERVER_HOSTS[$i] . ":" . Z_CONFIG::$ATTACHMENT_SERVER_STATIC_PORT . "/";
             Z_Core::debug("Making HEAD request to {$url}");
             $ch = curl_init($url);
             curl_setopt($ch, CURLOPT_NOBODY, 1);
             curl_setopt($ch, CURLOPT_HTTPHEADER, array("Expect:"));
             curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 1);
             curl_setopt($ch, CURLOPT_TIMEOUT, 2);
             curl_setopt($ch, CURLOPT_HEADER, 0);
             // do not return HTTP headers
             curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
             $response = curl_exec($ch);
             $code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
             if ($code != 200) {
                 $skipHost = Z_CONFIG::$ATTACHMENT_SERVER_HOSTS[$i];
                 if ($code == 0) {
                     Z_Core::logError("Error connecting to local attachments server");
                 } else {
                     Z_Core::logError("Local attachments server returned {$code}");
                 }
                 break;
             }
             $index = $i + 1;
             break;
         }
     }
     // If not, make an internal root request to trigger the extraction on
     // one of them and retrieve the temporary URL
     if ($index === false) {
         // Prevent redirect madness if target server doesn't think it's an
         // attachment server
         if ($localOnly) {
             throw new Exception("Internal attachments request hit a non-attachment server");
         }
         $prefix = 'http://' . Z_CONFIG::$API_SUPER_USERNAME . ":" . Z_CONFIG::$API_SUPER_PASSWORD . "@";
         $path = Zotero_API::getItemURI($item) . "/file/view?int=1";
         $path = preg_replace('/^[^:]+:\\/\\/[^\\/]+/', '', $path);
         $context = stream_context_create(array('http' => array('follow_location' => 0)));
         $url = false;
         $hosts = Z_CONFIG::$ATTACHMENT_SERVER_HOSTS;
         // Try in random order
         shuffle($hosts);
         foreach ($hosts as $host) {
             // Don't try the local host again if we know it's not working
             if ($host == $skipHost) {
                 continue;
             }
             $intURL = $prefix . $host . ":" . Z_CONFIG::$ATTACHMENT_SERVER_DYNAMIC_PORT . $path;
             Z_Core::debug("Making GET request to {$host}");
             if (file_get_contents($intURL, false, $context) !== false) {
                 foreach ($http_response_header as $header) {
                     if (preg_match('/^Location:\\s*(.+)$/', $header, $matches)) {
                         if (strpos($matches[1], $extURLPrefix) !== 0) {
                             throw new Exception("Redirect location '" . $matches[1] . "'" . " does not begin with {$extURLPrefix}");
                         }
                         return $matches[1];
                     }
                 }
             }
         }
         return false;
     }
     // If this is an attachment host, do the extraction inline
     // and generate a random number with an embedded host id.
     //
     // The reverse proxy routes incoming file requests to the proper hosts
     // using the embedded id.
     //
     // A cron job deletes old attachment directories
     $randomStr = rand(1000000, 2147483647);
     // Seventh number is the host id
     $randomStr = substr($randomStr, 0, 6) . $index . substr($randomStr, 6);
     // Download and extract file
     $dir = $docroot . $randomStr . "/";
     $tmpDir = $dir . "ztmp/";
     if (!mkdir($tmpDir, 0777, true)) {
         throw new Exception("Unable to create directory '{$tmpDir}'");
     }
     Z_Core::debug("Downloading attachment to {$dir}");
     $response = Zotero_S3::downloadFile($info, $tmpDir);
     $success = self::extractZip($tmpDir . $info['filename'], $dir);
     unlink($tmpDir . $info['filename']);
     rmdir($tmpDir);
     if (!$success) {
         return false;
     }
     Z_Core::$MC->set($key, $randomStr, self::$cacheTime);
     return $extURLPrefix . "{$randomStr}/" . $realFilename;
 }