public static function getSourceNodes($paths, $root, $contentType = null)
 {
     $paths = static::splitMultipath($paths);
     if (is_string($root)) {
         $root = Site::splitPath($root);
     }
     $sourceFiles = array();
     foreach ($paths as $path) {
         $path = array_merge($root, $path);
         list($filename) = array_slice($path, -1);
         if ($filename == '*') {
             array_pop($path);
             Emergence_FS::cacheTree($path);
             foreach (Emergence_FS::getTreeFiles($path, false, $contentType ? array('Type' => $contentType) : null) as $path => $fileData) {
                 $sourceFiles[$path] = $fileData;
             }
         } else {
             $node = Site::resolvePath($path);
             if (!$node || !is_a($node, 'SiteFile')) {
                 throw new Exception('Source file "' . implode('/', $path) . '" does not exist', self::ERROR_NOT_FOUND);
             }
             if ($node->Type != $contentType) {
                 throw new Exception('Source file "' . implode('/', $path) . '" does not match requested content type "' . $contentType . '"', self::ERROR_TYPE_MISMATCH);
             }
             $sourceFiles[join('/', $path)] = array('ID' => $node->ID, 'SHA1' => $node->SHA1);
         }
     }
     return $sourceFiles;
 }
 public static function getByEmergenceVFS($path)
 {
     $vfsPath = static::get_virtual_path(str_replace('vfs://', '', $path));
     $templateNode = false;
     $streamPath = Site::splitPath($vfsPath);
     if (!empty($streamPath[0])) {
         $topFolder = array_shift($streamPath);
         $localRoot = Site::getRootCollection($topFolder);
         $searchStack = array_filter($streamPath);
         while (true) {
             $searchPath = $searchStack;
             if ($templateNode = $localRoot->resolvePath($searchPath)) {
                 break;
             }
             if ($templateNode = Emergence::resolveFileFromParent($topFolder, $searchPath)) {
                 break;
             }
             if (count($searchStack)) {
                 array_pop($searchStack);
             } else {
                 break;
             }
         }
     }
     return $templateNode;
 }
Ejemplo n.º 3
0
 public function httpGetInterceptor($method, $uri)
 {
     if ($method !== 'GET' || DevelopRequestHandler::getResponseMode() != 'json') {
         return true;
     }
     $pathParts = \Site::splitPath($uri);
     $rootPath = RootCollection::filterName($pathParts[0]);
     if (count($pathParts) && array_key_exists($rootPath, RootCollection::$siteDirectories)) {
         $className = RootCollection::$siteDirectories[$rootPath];
         $node = new $className($pathParts[0]);
         if (count($pathParts) > 1) {
             $node = $node->resolvePath(array_splice($pathParts, 1));
         }
     } else {
         $node = \Site::resolvePath($uri, false);
     }
     if (!$node) {
         throw new \Sabre\DAV\Exception\FileNotFound();
     }
     $children = array();
     foreach ($node->getChildren() as $child) {
         $children[] = $child->getData();
     }
     $this->server->httpResponse->sendStatus(200);
     $this->server->httpResponse->setHeader('Content-Type', 'application/json');
     $this->server->httpResponse->sendBody(json_encode(array('path' => $uri, 'children' => $children)));
     return false;
 }
 function export($path = null, $destination = null)
 {
     set_time_limit(0);
     if (!is_array($path) && !empty($path)) {
         $path = Site::splitPath($path);
     }
     if (empty($path)) {
         $files = DB::allRecords('SELECT MAX(`ID`) as `RID`' . ' FROM `_e_files`' . ' GROUP BY  `Handle`,`CollectionID`');
         foreach ($files as $file) {
             $SiteFile = SiteFile::getByID($file['RID'])->getData();
             if (strpos($SiteFile['FullPath'], '_parent') !== 0 && $SiteFile['Status'] != 'Deleted') {
                 $SiteFiles[$SiteFile['FullPath']] = Site::$rootPath . '/data/' . $SiteFile['ID'];
             }
         }
     } else {
         throw new Exception('Sub directories not yet supported.');
     }
     if (count($SiteFiles)) {
         $zip = new ZipArchive();
         $tmp = $destination ? $destination : tempnam("/tmp", "emr");
         if ($zip->open($tmp) === TRUE) {
             foreach ($SiteFiles as $virtualPath => $realPath) {
                 $zip->addFromString($virtualPath, file_get_contents($realPath));
             }
         }
         $zip->close();
         return $tmp;
     } else {
         throw new Exception('Nothing to compress found.');
     }
 }
Ejemplo n.º 5
0
 public function resolvePath($path)
 {
     if (!is_array($path)) {
         $path = \Site::splitPath($path);
     }
     $node = $this;
     while ($childHandle = array_shift($path)) {
         if (method_exists($node, 'getChild') && ($nextNode = $node->getChild($childHandle))) {
             $node = $nextNode;
         } else {
             $node = false;
             break;
         }
     }
     return $node;
 }
Ejemplo n.º 6
0
 public static function createFromPath($path, $data = null, $ancestorID = null)
 {
     if (!is_array($path)) {
         $path = Site::splitPath($path);
     }
     $parentCollection = null;
     // create collections
     while (count($path) > 1) {
         $parentCollection = SiteCollection::getOrCreateCollection(array_shift($path), $parentCollection);
     }
     return static::create($parentCollection->ID, $path[0], $data, $ancestorID);
 }
 public static function getCollectionLayers($path, $localOnly = false)
 {
     // split path into array
     if (is_string($path)) {
         $path = Site::splitPath($path);
     }
     // resolve local and remote collections
     $rootHandle = array_shift($path);
     $localCollection = SiteCollection::getByHandle($rootHandle, null, false);
     if (!$localOnly) {
         $remoteCollection = SiteCollection::getByHandle($rootHandle, null, true);
     }
     while ($handle = array_shift($path)) {
         if ($localCollection) {
             $localCollection = SiteCollection::getByHandle($handle, $localCollection->ID, false);
         }
         if ($remoteCollection) {
             $remoteCollection = SiteCollection::getByHandle($handle, $remoteCollection->ID, true);
         }
     }
     return array_filter(array('remote' => $remoteCollection, 'local' => $localCollection));
 }
 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;
 }
Ejemplo n.º 9
0
 public static function getOrCreatePath($path, SiteCollection $root = null)
 {
     if (!is_array($path)) {
         $path = Site::splitPath($path);
     }
     $collection = $root;
     // create collections
     while (count($path)) {
         $collection = static::getOrCreateCollection(array_shift($path), $collection);
     }
     return $collection;
 }
Ejemplo n.º 10
0
 public function getVersionedPath($filePath, $useCache = false)
 {
     if (is_string($filePath)) {
         $filePath = Site::splitPath($filePath);
     }
     $Asset = $this->getAsset($filePath);
     $assetPath = Sencha_RequestHandler::$externalRoot . '/' . $this->getName() . '/' . implode('/', $filePath);
     if ($Asset) {
         return $assetPath . '?_sha1=' . $Asset->SHA1;
     } else {
         return $assetPath;
     }
 }
 public function createFile($path, $data = null, $ancestorID = null)
 {
     if (!is_array($path)) {
         $path = Site::splitPath($path);
     }
     $parentCollection = $this;
     // create collections
     while (count($path) > 1) {
         $parentCollection = static::getOrCreateCollection(array_shift($path), $parentCollection);
     }
     $fileClass = static::$fileClass;
     $fileClass::create($parentCollection->ID, $path[0], $data, $ancestorID);
 }
Ejemplo n.º 12
0
 public static function getVersionedLibraryPath($filePath)
 {
     if (is_string($filePath)) {
         $filePath = Site::splitPath($filePath);
     }
     $assetPath = Sencha_RequestHandler::$externalRoot . '/x/' . implode('/', $filePath);
     array_unshift($filePath, 'ext-library');
     $Asset = Site::resolvePath($filePath);
     if ($Asset) {
         return $assetPath . '?_sha1=' . $Asset->SHA1;
     } else {
         return $assetPath;
     }
 }