public static function resolveFileFromParent($collectionHandle, $path)
 {
     if (!($parentConfig = Site::getParentHostConfig())) {
         return false;
     }
     // get collection for parent site
     $collection = SiteCollection::getOrCreateRootCollection($collectionHandle, $parentConfig['ID']);
     $fileNode = $collection->resolvePath($path);
     // try to download from parent site
     if (!$fileNode) {
         $remoteURL = 'http://' . $parentConfig['Hostname'] . '/emergence/';
         $remoteURL .= $collectionHandle . '/';
         $remoteURL .= join('/', $path);
         $remoteURL .= '?accessKey=' . $parentConfig['AccessKey'];
         $cache = apc_fetch($remoteURL);
         if ($cache == '404') {
             return false;
         }
         //if(isset(self::$cache[$cacheKey])) {
         //    $fp = self::$cache[$cacheKey];
         //}
         $fp = fopen('php://memory', 'w+');
         //print("Retrieving: <a href='$remoteURL' target='_blank'>$remoteURL</a><br>\n");
         $ch = curl_init($remoteURL);
         curl_setopt($ch, CURLOPT_FILE, $fp);
         curl_setopt($ch, CURLOPT_HEADER, true);
         if (!curl_exec($ch)) {
             throw new Exception('Failed to query parent site for file');
         }
         if (curl_errno($ch)) {
             die("curl error:" . curl_error($ch));
         }
         // write file to parent site collection
         fseek($fp, 0);
         // read status
         $statusLine = trim(fgetss($fp));
         list($protocol, $status, $message) = explode(' ', $statusLine);
         if ($status != '200') {
             apc_store($remoteURL, '404');
             return false;
         }
         // read headers
         while ($header = trim(fgetss($fp))) {
             if (!$header) {
                 break;
             }
             list($key, $value) = preg_split('/:\\s*/', $header, 2);
             //print "$key=$value<br>";
         }
         $collection->createFile($path, $fp);
         $fileNode = $collection->resolvePath($path);
     }
     return $fileNode;
 }
示例#2
0
 public static function getRootCollection($handle)
 {
     if (!empty(static::$_rootCollections[$handle])) {
         return static::$_rootCollections[$handle];
     }
     return static::$_rootCollections[$handle] = SiteCollection::getOrCreateRootCollection($handle);
 }
 public static function resolveFileFromParent($collection, $path, $forceRemote = false, $params = array())
 {
     if (!Site::getConfig('parent_hostname')) {
         return false;
     }
     // get collection for parent site
     if (is_string($collection)) {
         $collection = SiteCollection::getOrCreateRootCollection($collection, true);
     }
     if (is_string($path)) {
         $path = Site::splitPath($path);
     }
     // try to get existing cached file
     $fileNode = $collection->resolvePath($path);
     // try to download from parent site
     if ($forceRemote || !$fileNode) {
         if (!Site::$autoPull) {
             return false;
         }
         $remoteURL = static::buildUrl(array_merge($collection->getFullPath(null, false), $path), $params);
         $cachedStatus = Cache::rawFetch($remoteURL);
         if ($cachedStatus) {
             return false;
         }
         $fp = fopen('php://memory', 'w+');
         $ch = curl_init($remoteURL);
         curl_setopt($ch, CURLOPT_FILE, $fp);
         curl_setopt($ch, CURLOPT_HEADER, true);
         if (curl_exec($ch) === false || curl_errno($ch)) {
             throw new Exception('Failed to query parent site for file: ' . curl_error($ch));
         }
         // read response
         fseek($fp, 0);
         // read and check status
         list($protocol, $status, $message) = explode(' ', trim(fgetss($fp)));
         if ($status != '200') {
             fclose($fp);
             Cache::rawStore($remoteURL, (int) $status);
             return false;
         }
         // read headers until a blank line is found
         while ($header = trim(fgetss($fp))) {
             if (!$header) {
                 break;
             }
             list($key, $value) = preg_split('/:\\s*/', $header, 2);
             $key = strtolower($key);
             // if etag found, use it to skip write if existing file matches
             if ($key == 'etag' && $fileNode && $fileNode->SHA1 == $value) {
                 fclose($fp);
                 return $fileNode;
             }
         }
         // write remaining buffer to file
         $fileRecord = $collection->createFile($path, $fp);
         $fileNode = new SiteFile($fileRecord['Handle'], $fileRecord);
     }
     return $fileNode;
 }