Пример #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;
 }
Пример #2
0
 /**
  * Converts a Zotero_Item object to a SimpleXMLElement Atom object
  *
  * @param	object				$item		Zotero_Item object
  * @param	string				$content
  * @return	SimpleXMLElement					Item data as SimpleXML element
  */
 public static function convertItemToAtom(Zotero_Item $item, $queryParams, $apiVersion = null, $permissions = null, $sharedData = null)
 {
     $content = $queryParams['content'];
     $contentIsHTML = sizeOf($content) == 1 && $content[0] == 'html';
     $contentParamString = urlencode(implode(',', $content));
     $style = $queryParams['style'];
     $entry = '<entry xmlns="' . Zotero_Atom::$nsAtom . '" xmlns:zapi="' . Zotero_Atom::$nsZoteroAPI . '"/>';
     $xml = new SimpleXMLElement($entry);
     $title = $item->getDisplayTitle(true);
     $title = $title ? $title : '[Untitled]';
     $xml->title = $title;
     $author = $xml->addChild('author');
     $createdByUserID = null;
     switch (Zotero_Libraries::getType($item->libraryID)) {
         case 'group':
             $createdByUserID = $item->createdByUserID;
             break;
     }
     if ($createdByUserID) {
         $author->name = Zotero_Users::getUsername($createdByUserID);
         $author->uri = Zotero_URI::getUserURI($createdByUserID);
     } else {
         $author->name = Zotero_Libraries::getName($item->libraryID);
         $author->uri = Zotero_URI::getLibraryURI($item->libraryID);
     }
     $id = Zotero_URI::getItemURI($item);
     /*if (!$contentIsHTML) {
     			$id .= "?content=$content";
     		}*/
     $xml->id = $id;
     $xml->published = Zotero_Date::sqlToISO8601($item->getField('dateAdded'));
     $xml->updated = Zotero_Date::sqlToISO8601($item->getField('dateModified'));
     $link = $xml->addChild("link");
     $link['rel'] = "self";
     $link['type'] = "application/atom+xml";
     $href = Zotero_Atom::getItemURI($item);
     if (!$contentIsHTML) {
         $href .= "?content={$contentParamString}";
     }
     $link['href'] = $href;
     $parent = $item->getSource();
     if ($parent) {
         // TODO: handle group items?
         $parentItem = Zotero_Items::get($item->libraryID, $parent);
         $link = $xml->addChild("link");
         $link['rel'] = "up";
         $link['type'] = "application/atom+xml";
         $href = Zotero_Atom::getItemURI($parentItem);
         if (!$contentIsHTML) {
             $href .= "?content={$contentParamString}";
         }
         $link['href'] = $href;
     }
     $link = $xml->addChild('link');
     $link['rel'] = 'alternate';
     $link['type'] = 'text/html';
     $link['href'] = Zotero_URI::getItemURI($item);
     // If appropriate permissions and the file is stored in ZFS, get file request link
     if ($permissions && $permissions->canAccess($item->libraryID, 'files')) {
         $details = Zotero_S3::getDownloadDetails($item);
         if ($details) {
             $link = $xml->addChild('link');
             $link['rel'] = 'enclosure';
             $type = $item->attachmentMIMEType;
             if ($type) {
                 $link['type'] = $type;
             }
             $link['href'] = $details['url'];
             if (!empty($details['filename'])) {
                 $link['title'] = $details['filename'];
             }
             if (!empty($details['size'])) {
                 $link['length'] = $details['size'];
             }
         }
     }
     $xml->addChild('zapi:key', $item->key, Zotero_Atom::$nsZoteroAPI);
     $xml->addChild('zapi:itemType', Zotero_ItemTypes::getName($item->itemTypeID), Zotero_Atom::$nsZoteroAPI);
     if ($item->isRegularItem()) {
         $val = $item->creatorSummary;
         if ($val !== '') {
             $xml->addChild('zapi:creatorSummary', htmlspecialchars($val), Zotero_Atom::$nsZoteroAPI);
         }
         $val = substr($item->getField('date', true, true, true), 0, 4);
         if ($val !== '' && $val !== '0000') {
             $xml->addChild('zapi:year', $val, Zotero_Atom::$nsZoteroAPI);
         }
     }
     if (!$parent && $item->isRegularItem()) {
         if ($permissions && !$permissions->canAccess($item->libraryID, 'notes')) {
             $numChildren = $item->numAttachments();
         } else {
             $numChildren = $item->numChildren();
         }
         $xml->addChild('zapi:numChildren', $numChildren, Zotero_Atom::$nsZoteroAPI);
     }
     $xml->addChild('zapi:numTags', $item->numTags(), Zotero_Atom::$nsZoteroAPI);
     $xml->content = '';
     //
     // DOM XML from here on out
     //
     $contentNode = dom_import_simplexml($xml->content);
     $domDoc = $contentNode->ownerDocument;
     $multiFormat = sizeOf($content) > 1;
     // Create a root XML document for multi-format responses
     if ($multiFormat) {
         $contentNode->setAttribute('type', 'application/xml');
         /*$multicontent = $domDoc->createElementNS(
         			Zotero_Atom::$nsZoteroAPI, 'multicontent'
         		);
         		$contentNode->appendChild($multicontent);*/
     }
     foreach ($content as $type) {
         // Set the target to either the main <content>
         // or a <multicontent> <content>
         if (!$multiFormat) {
             $target = $contentNode;
         } else {
             $target = $domDoc->createElementNS(Zotero_Atom::$nsZoteroAPI, 'subcontent');
             $contentNode->appendChild($target);
         }
         $target->setAttributeNS(Zotero_Atom::$nsZoteroAPI, "zapi:type", $type);
         if ($type == 'html') {
             if (!$multiFormat) {
                 $target->setAttribute('type', 'xhtml');
             }
             $div = $domDoc->createElement('div');
             $div->setAttribute('xmlns', Zotero_Atom::$nsXHTML);
             $target->appendChild($div);
             $html = $item->toHTML(true);
             $subNode = dom_import_simplexml($html);
             $importedNode = $domDoc->importNode($subNode, true);
             $div->appendChild($importedNode);
         } else {
             if ($type == 'citation') {
                 if (!$multiFormat) {
                     $target->setAttribute('type', 'xhtml');
                 }
                 if (isset($sharedData[$type][$item->libraryID . "/" . $item->key])) {
                     $html = $sharedData[$type][$item->libraryID . "/" . $item->key];
                 } else {
                     if ($sharedData !== null) {
                         error_log("Citation not found in sharedData -- retrieving individually");
                     }
                     $html = Zotero_Cite::getCitationFromCiteServer($item, $style);
                 }
                 $html = new SimpleXMLElement($html);
                 $html['xmlns'] = Zotero_Atom::$nsXHTML;
                 $subNode = dom_import_simplexml($html);
                 $importedNode = $domDoc->importNode($subNode, true);
                 $target->appendChild($importedNode);
             } else {
                 if ($type == 'bib') {
                     if (!$multiFormat) {
                         $target->setAttribute('type', 'xhtml');
                     }
                     if (isset($sharedData[$type][$item->libraryID . "/" . $item->key])) {
                         $html = $sharedData[$type][$item->libraryID . "/" . $item->key];
                     } else {
                         if ($sharedData !== null) {
                             error_log("Bibliography not found in sharedData -- retrieving individually");
                         }
                         $html = Zotero_Cite::getBibliographyFromCitationServer(array($item), $style);
                     }
                     $html = new SimpleXMLElement($html);
                     $html['xmlns'] = Zotero_Atom::$nsXHTML;
                     $subNode = dom_import_simplexml($html);
                     $importedNode = $domDoc->importNode($subNode, true);
                     $target->appendChild($importedNode);
                 } else {
                     if ($type == 'json') {
                         $target->setAttributeNS(Zotero_Atom::$nsZoteroAPI, "zapi:etag", $item->etag);
                         $textNode = $domDoc->createTextNode($item->toJSON(false, $queryParams['pprint'], true));
                         $target->appendChild($textNode);
                     } else {
                         if ($type == 'csljson') {
                             $arr = $item->toCSLItem();
                             $mask = JSON_HEX_TAG | JSON_HEX_AMP;
                             if ($queryParams['pprint']) {
                                 $json = Zotero_Utilities::json_encode_pretty($arr, $mask);
                             } else {
                                 $json = json_encode($arr, $mask);
                             }
                             // Until JSON_UNESCAPED_SLASHES is available
                             $json = str_replace('\\/', '/', $json);
                             $textNode = $domDoc->createTextNode($json);
                             $target->appendChild($textNode);
                         } else {
                             if ($type == 'full') {
                                 if (!$multiFormat) {
                                     $target->setAttribute('type', 'xhtml');
                                 }
                                 $fullXML = Zotero_Items::convertItemToXML($item, array(), $apiVersion);
                                 $fullXML->addAttribute("xmlns", Zotero_Atom::$nsZoteroTransfer);
                                 $subNode = dom_import_simplexml($fullXML);
                                 $importedNode = $domDoc->importNode($subNode, true);
                                 $target->appendChild($importedNode);
                             } else {
                                 if (in_array($type, Zotero_Translate::$exportFormats)) {
                                     $export = Zotero_Translate::doExport(array($item), $type);
                                     $target->setAttribute('type', $export['mimeType']);
                                     // Insert XML into document
                                     if (preg_match('/\\+xml$/', $export['mimeType'])) {
                                         // Strip prolog
                                         $body = preg_replace('/^<\\?xml.+\\n/', "", $export['body']);
                                         $subNode = $domDoc->createDocumentFragment();
                                         $subNode->appendXML($body);
                                         $target->appendChild($subNode);
                                     } else {
                                         $textNode = $domDoc->createTextNode($export['body']);
                                         $target->appendChild($textNode);
                                     }
                                 }
                             }
                         }
                     }
                 }
             }
         }
     }
     return $xml;
 }
Пример #3
0
 public function storagetransferbucket()
 {
     // DISABLED
     $this->e404();
     if (!$this->permissions->isSuper()) {
         $this->e404();
     }
     $this->allowMethods(array('POST'));
     Zotero_S3::transferBucket('zoterofilestorage', 'zoterofilestoragetest');
     exit;
 }
Пример #4
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;
 }