function import($inputFile, $targetDirectory = null) { if (!$targetDirectory) { $targetDirectory = ''; } $finfo = finfo_open(FILEINFO_MIME_TYPE); $type = finfo_file($finfo, $inputFile); finfo_close($finfo); switch ($type) { case 'application/zip': $zip = new ZipArchive(); $zip->open($inputFile); $i = 0; while ($i < $zip->numFiles) { $file = $zip->statIndex($i); $contents = ''; $fp = $zip->getStream($file['name']); while (!feof($fp)) { $contents .= fread($fp, 2); } SiteCollection::createFile($targetDirectory . $file['name'], $contents); fclose($fp); $i++; } break; default: throw new Exception('MIME type ' . $type . ' unsupported.'); } }
public function getChild($handle, $record = null) { if ($child = parent::getChild($handle, $record)) { return $child; } else { throw new \Sabre\DAV\Exception\FileNotFound('The file with name: ' . $handle . ' could not be found'); } }
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; }
public function beforeCreateFile($uri, $data) { list($dir, $name) = \Sabre\DAV\URLUtil::splitPath($uri); $currentNode = null; foreach (explode('/', trim($dir, '/')) as $pathPart) { $parentNode = $currentNode; $currentNode = \SiteCollection::getByHandle($pathPart, $parentNode ? $parentNode->ID : null); if (!$currentNode) { $currentNode = \SiteCollection::create($pathPart, $parentNode); } } }
public static function getRootCollection($handle) { if (!empty(static::$_rootCollections[$handle])) { return static::$_rootCollections[$handle]; } return static::$_rootCollections[$handle] = SiteCollection::getOrCreateRootCollection($handle); }
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 getNodesFromPattern($patterns, $localOnly = false) { $matchedNodes = array(); if (!is_array($patterns)) { $patterns = array($patterns); } $rootCollections = SiteCollection::getAllRootCollections(); if (!$localOnly) { $rootCollections = array_merge($rootCollections, SiteCollection::getAllRootCollections(true)); } $_findMatchingNodes = function ($patternStack, $nodes) use(&$_findMatchingNodes, &$matchedNodes) { $pattern = array_shift($patternStack); foreach ($nodes as $node) { if (preg_match("{^{$pattern}\$}i", $node->Handle)) { if (!count($patternStack)) { $matchedNodes[] = $node; } elseif ($node->Class == 'SiteCollection') { $_findMatchingNodes($patternStack, $node->getChildren()); } } } }; foreach ($patterns as $pattern) { $_findMatchingNodes(explode('/', $pattern), $rootCollections); } return $matchedNodes; }
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; }
public static function handleActivityRequest() { $GLOBALS['Session']->requireAccountLevel('Developer'); if (static::peekPath() == 'all') { static::$activityPageSize = false; } $activity = array(); $openFiles = array(); $editResults = DB::query('SELECT f.*' . ' FROM _e_files f' . ' JOIN _e_file_collections c ON(c.ID = f.CollectionID)' . ' WHERE c.Site = "Local"' . ' ORDER BY ID DESC'); $closeFile = function ($path) use(&$activity, &$openFiles) { list($authorID, $collectionID, $handle) = explode('/', $path, 3); $Collection = SiteCollection::getByID($collectionID); $Author = Person::getByID($authorID); $activity[] = array('EventType' => 'save', 'Author' => $Author ? $Author->getData() : null, 'Collection' => $Collection->getData(), 'Handle' => $handle, 'CollectionPath' => $Collection->FullPath, 'Timestamp' => $openFiles[$path][count($openFiles[$path]) - 1]['Timestamp'], 'FirstTimestamp' => $openFiles[$path][0]['Timestamp'], 'RevisionID' => $openFiles[$path][count($openFiles[$path]) - 1]['ID'], 'FirstRevisionID' => $openFiles[$path][0]['ID'], 'FirstAncestorID' => $openFiles[$path][0]['AncestorID'], 'revisions' => $openFiles[$path]); unset($openFiles[$path]); }; while ((!static::$activityPageSize || count($activity) + count($openFiles) < static::$activityPageSize) && ($editRecord = $editResults->fetch_assoc())) { $editRecord['Timestamp'] = strtotime($editRecord['Timestamp']); $path = $editRecord['AuthorID'] . '/' . $editRecord['CollectionID'] . '/' . $editRecord['Handle']; if ($editRecord['Status'] == 'Deleted') { if (array_key_exists($path, $openFiles)) { $closeFile($path); } $Author = Person::getByID($editRecord['AuthorID']); $Collection = SiteCollection::getByID($editRecord['CollectionID']); $lastActivity = count($activity) ? $activity[count($activity) - 1] : null; if ($lastActivity && $lastActivity['EventType'] == 'delete' && $lastActivity['Author']['ID'] == $Author->ID) { // compound into last activity entry if it was a delete by the same person $activity[count($activity) - 1]['FirstTimestamp'] = $editRecord['Timestamp']; $activity[count($activity) - 1]['files'][] = array('Collection' => $Collection->getData(), 'Handle' => $editRecord['Handle'], 'CollectionPath' => $Collection->FullPath, 'Timestamp' => $editRecord['Timestamp']); } else { // push new activity $activity[] = array('EventType' => 'delete', 'Author' => $Author ? $Author->getData() : null, 'Timestamp' => $editRecord['Timestamp'], 'files' => array(array('Collection' => $Collection->getData(), 'Handle' => $editRecord['Handle'], 'CollectionPath' => $Collection->FullPath, 'Timestamp' => $editRecord['Timestamp']))); } } elseif (array_key_exists($path, $openFiles)) { if ($editRecord['Timestamp'] < $openFiles[$path][0]['Timestamp'] - static::$activitySessionThreshold) { $closeFile($path); $openFiles[$path] = array($editRecord); } else { array_unshift($openFiles[$path], $editRecord); } } else { $openFiles[$path] = array($editRecord); } } // close any files still open $openFileKeys = array_keys($openFiles); array_walk($openFileKeys, $closeFile); // sort activity by last edit usort($activity, function ($a, $b) { return $a['Timestamp'] > $b['Timestamp'] ? -1 : 1; }); return static::respond('activity', array('success' => true, 'data' => $activity)); }