/** * Download file from S3, extract it if necessary, 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_Storage::getLocalFileItemInfo($item); $storageFileID = $info['storageFileID']; $filename = $info['filename']; $mtime = $info['mtime']; $zip = $info['zip']; $realFilename = preg_replace("/^storage:/", "", $item->attachmentPath); $realFilename = self::decodeRelativeDescriptorString($realFilename); $realEncodedFilename = rawurlencode($realFilename); $docroot = Z_CONFIG::$ATTACHMENT_SERVER_DOCROOT; // 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}/{$realEncodedFilename}' from memcached"); return $extURLPrefix . "{$randomStr}/{$realEncodedFilename}"; } $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) { continue; } // 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 download/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 file $dir = $docroot . $randomStr . "/"; $downloadDir = $zip ? $dir . "ztmp/" : $dir; Z_Core::debug("Downloading attachment to {$dir}"); if (!mkdir($downloadDir, 0777, true)) { throw new Exception("Unable to create directory '{$downloadDir}'"); } if ($zip) { $response = Zotero_Storage::downloadFile($info, $downloadDir); } else { $response = Zotero_Storage::downloadFile($info, $downloadDir, $realFilename); } if ($response) { if ($zip) { $success = self::extractZip($downloadDir . $info['filename'], $dir); unlink($downloadDir . $info['filename']); rmdir($downloadDir); // Make sure charset is just a string with no spaces or newlines if (preg_match('/^[^\\s]+/', trim($item->attachmentCharset), $matches)) { $charset = $matches[0]; } else { $charset = 'Off'; } file_put_contents($dir . ".htaccess", "AddDefaultCharset " . $charset); } else { $success = true; if (preg_match('/^[^\\s]+/', trim($item->attachmentContentType), $matches)) { $contentType = $matches[0]; $charset = trim($item->attachmentCharset); if (substr($charset, 0, 5) == 'text/' && preg_match('/^[^\\s]+/', $charset, $matches)) { $contentType .= '; ' . $matches[0]; } file_put_contents($dir . ".htaccess", "ForceType " . $contentType); } } } if (!$response || !$success) { return false; } Z_Core::$MC->set($key, $randomStr, self::$cacheTime); return $extURLPrefix . "{$randomStr}/" . $realEncodedFilename; }
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_Storage::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_Storage::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) { throw new Exception("Invalid delta", Z_ERROR_INVALID_INPUT); } 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(); throw new HTTPException("Patched file does not match hash", 409); } // Check file size if (filesize("new") != $info->size) { $cleanup(); throw new HTTPException("Patched file size does not match " . "(" . filesize("new") . " != {$info->size})", 409); } // 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(); throw new HTTPException("Patched file is not a ZIP file", 409); } // Upload to S3 $t = $info->contentType . ($info->contentType && $info->charset ? "; charset={$info->charset}" : ""); $storageFileID = Zotero_Storage::uploadFile($info, "new", $t); } catch (Exception $e) { //$cleanup(); throw $e; } return $storageFileID; }