/** * Expand archive * * @param bool $cleanup Whether or not to clean up after expansion (i.e. removing known OS files, etc...) * @return bool */ public function expand($cleanup = true) { // Create local tmp copy of the archive that's being expanded $temp = Manager::getTempPath($this->getName()); $this->copy($temp); $archive = new \PharData($temp->getAbsolutePath()); foreach ($archive as $file) { // Add 7 to the length for the 'phar://' prefix to the file $path = substr($file, strlen($temp->getAbsolutePath()) + 7); $entity = Entity::fromPath($this->getParent() . $path, $this->getAdapter()); if ($entity->isFile()) { // Open $item = fopen($file, 'r'); // Write stream $entity->putStream($item); // Close fclose($item); } else { // Create the directory $entity->create(); } } // Clean up $temp->delete(); return parent::expand($cleanup); }
/** * Expand archive * * @param bool $cleanup Whether or not to clean up after expansion (i.e. removing known OS files, etc...) * @return bool */ public function expand($cleanup = true) { // Create local tmp copy of the archive that's being expanded $temp = Manager::getTempPath($this->getName()); $this->copy($temp); $zip = new \ZipArchive(); // Open the temp archive (we use the absolute path because we're on the local filesystem) if ($zip->open($temp->getAbsolutePath()) === true) { // We don't actually have to extract the archive, we can just read out of it and copy over to the original location for ($i = 0; $i < $zip->numFiles; $i++) { $filename = $zip->getNameIndex($i); $entity = Entity::fromPath($this->getParent() . '/' . $filename, $this->getAdapter()); if ($entity->isFile()) { // Open $item = fopen('zip://' . $temp->getAbsolutePath() . '#' . $filename, 'r'); // Write stream $entity->putStream($item); // Close fclose($item); } else { // Create the directory $entity->create(); } } // Clean up $zip->close(); $temp->delete(); return parent::expand($cleanup); } return false; }
/** * Browser within publications (Curation) * * @return string */ protected function _select() { // Incoming $props = Request::getVar('p', ''); $ajax = Request::getInt('ajax', 0); $pid = Request::getInt('pid', 0); $vid = Request::getInt('vid', 0); $filter = urldecode(Request::getVar('filter', '')); $directory = urldecode(Request::getVar('directory', '')); // Parse props for curation $parts = explode('-', $props); $block = isset($parts[0]) && in_array($parts[0], array('content', 'extras')) ? $parts[0] : 'content'; $step = isset($parts[1]) && is_numeric($parts[1]) && $parts[1] > 0 ? $parts[1] : 1; $element = isset($parts[2]) && is_numeric($parts[2]) && $parts[2] > 0 ? $parts[2] : 1; // Output HTML $view = new \Hubzero\Plugin\View(array('folder' => 'projects', 'element' => 'files', 'name' => 'selector')); $view->publication = new \Components\Publications\Models\Publication($pid, NULL, $vid); // On error if (!$view->publication->exists()) { // Output error $view = new \Hubzero\Plugin\View(array('folder' => 'projects', 'element' => 'files', 'name' => 'error')); $view->title = ''; $view->option = $this->_option; $view->setError(Lang::txt('PLG_PROJECTS_FILES_SELECTOR_ERROR_NO_PUBID')); return $view->loadTemplate(); } $view->publication->attachments(); // Get curation model $view->publication->setCuration(); // Make sure block exists, else use default $view->publication->_curationModel->setBlock($block, $step); // Get file list $view->items = NULL; if ($this->model->get('id')) { // Set params $params = array('sortby' => 'localpath', 'showFullMetadata' => false, 'subdir' => $directory); // Retrieve items if (($cid = Request::getInt('cid')) && $cid > 0) { // Get directory that we're interested in $con = Connection::oneOrFail($cid); $dir = \Hubzero\Filesystem\Entity::fromPath($directory != '.' ? $directory : '', $con->adapter()); $view->items = $dir->listContents(); } else { $view->items = $this->repo->filelist($params); } $view->directory = $directory; // Get directories $params = array('subdir' => NULL, 'sortby' => 'localpath', 'showFullMetadata' => false, 'dirsOnly' => true); $view->folders = $this->repo->filelist($params); } $view->option = $this->model->isProvisioned() ? 'com_publications' : $this->_option; $view->database = $this->_database; $view->model = $this->model; $view->repo = $this->repo; $view->uid = $this->_uid; $view->ajax = $ajax; $view->task = $this->_task; $view->element = $element; $view->block = $block; $view->step = $step; $view->props = $props; $view->filter = $filter; $view->sizelimit = $this->params->get('maxUpload', '104857600'); $view->showCons = $this->params->get('default_action', 'browse') == 'connections' ? true : false; // Get messages and errors $view->msg = $this->_msg; if ($this->getError()) { $view->setError($this->getError()); } return $view->loadTemplate(); }
/** * Sorts incoming file/folder data * * @return array */ private function getCollection() { // Incoming $files = $this->prune((array) Request::getVar('asset', [])); $directories = $this->prune((array) Request::getVar('folder', [])); $collection = new Collection(); $entities = array_merge($files, $directories); if (!empty($entities) && is_array($entities)) { foreach ($entities as $entity) { $path = trim($this->subdir, '/') . '/' . urldecode($entity); $collection->add(Entity::fromPath($path, $this->connection->adapter())); } } return $collection; }
/** * Compresses/archives entities in collection * * @param bool $structure Whether or not to retain directory location of files being zipped * @param bool $upload Whether or not to reupload compressed to filesystem location * @return string|bool */ public function compress($structure = false, $upload = false) { if (!extension_loaded('zip')) { return false; } // Get temp directory $adapter = null; $temp = sys_get_temp_dir(); $tarname = uniqid() . '.zip'; $zip = new \ZipArchive(); if ($zip->open($temp . DS . $tarname, \ZipArchive::OVERWRITE) === true) { foreach ($this->_data as $entity) { if ($entity->isFile()) { $zip->addFromString($structure ? $entity->getPath() : $entity->getName(), $entity->read()); } else { if ($entity->isDir() && $structure) { $zip->addEmptyDir($entity->getPath()); } } // Set some vars in case we need them later $adapter = $adapter ?: $entity->getAdapter(); } $zip->close(); $local = Manager::getTempPath(); if ($upload) { // @FIXME: use manager copy? $entity = Entity::fromPath($tarname, $adapter); $entity->put($local->readAndDelete()); return $entity; } else { return $local; } } else { return false; } }
/** * Build breadcrumbs inside files plugin * * @param string $dir * @param string $url * @return string */ public static function buildFileBrowserCrumbs($dir = '', $url = '', &$parent = NULL, $linkit = true, $adapter = NULL, $seperator = '»') { $bc = NULL; $href = ''; $desectPath = explode(DS, $dir); if ($dir && count($desectPath) > 0) { for ($p = 0; $p < count($desectPath); $p++) { $parent = count($desectPath) > 1 && $p != count($desectPath) ? $href : ''; $href .= DS . $desectPath[$p]; $name = $desectPath[$p]; if ($adapter) { $temp = Entity::fromPath($desectPath[$p], $adapter); if ($temp) { $name = $temp->displayName(); } } if ($linkit) { if (strpos($url, '?') !== false) { $currentUrl = Route::url($url . '&subdir=' . urlencode($href)); $bc .= ' » <span><a href="' . $currentUrl . '" class="folder">' . $name . '</a></span> '; } else { $bc .= ' » <span><a href="' . $url . '/?subdir='; $bc .= urlencode($href) . '" class="folder">' . $name . '</a></span> '; } } else { $bc .= ' <span class="folder">' . $name . '</span> ' . $seperator; } } } return $bc; }
/** * Grabs the item name, without extension * * @return string **/ public function getDisplayName() { return str_replace('.' . $this->getExtension(), '', parent::getDisplayName()); }
/** * Save incoming file selection * * @return boolean */ public function save($element, $elementId, $pub, $blockParams, $toAttach = array()) { // Incoming selections if (empty($toAttach)) { $selections = Request::getVar('selecteditems', ''); $toAttach = explode(',', $selections); } // Get configs $configs = $this->getConfigs($element, $elementId, $pub, $blockParams); // Cannot make changes if ($configs->freeze) { return false; } // Nothing to change if (empty($toAttach)) { return false; } // Git helper include_once PATH_CORE . DS . 'components' . DS . 'com_projects' . DS . 'helpers' . DS . 'githelper.php'; $this->_git = new \Components\Projects\Helpers\Git($configs->path); // Counter $i = 0; $a = 0; // Attach/refresh each selected item foreach ($toAttach as $identifier) { if (!trim($identifier)) { continue; } $identifier = urldecode($identifier); // Catch items coming in from connections if (preg_match('/^([0-9]*):\\/\\//', $identifier, $matches)) { if (isset($matches[1])) { require_once PATH_CORE . DS . 'components' . DS . 'com_projects' . DS . 'models' . DS . 'orm' . DS . 'connection.php'; // Grab the connection id $connection = $matches[1]; // Reset identifier $identifier = str_replace($matches[0], '', $identifier); $connection = Connection::oneOrFail($connection); // Create file objects $conFile = Entity::fromPath($identifier, $connection->adapter()); if (!$conFile->isLocal()) { // Create a temp file and write to it $tempFile = Manager::getTempPath($conFile->getName()); Manager::copy($conFile, $tempFile); } else { $tempFile = $conFile; } // Insert the file into the repo $result = $pub->_project->repo()->insert(['subdir' => $conFile->getParent(), 'dataPath' => $tempFile->getAbsolutePath(), 'update' => false]); if (!$conFile->isLocal()) { $tempFile->delete(); } } } $a++; $ordering = $i + 1; if ($this->addAttachment($identifier, $pub, $configs, User::get('id'), $elementId, $element, $ordering)) { $i++; } } // Success if ($i > 0 && $i == $a) { Event::trigger('filesystem.onAfterSaveFileAttachments', [$pub, $configs, $elementId, $element]); $message = $this->get('_message') ? $this->get('_message') : Lang::txt('Selection successfully saved'); $this->set('_message', $message); } return true; }
/** * Sorts incoming file/folder data * * @return array */ private function getCollection() { // Incoming $files = $this->prune((array) Request::getVar('asset', [])); $directories = $this->prune((array) Request::getVar('folder', [])); $collection = new Collection(); $entities = array_merge($files, $directories); if (!empty($entities) && is_array($entities)) { foreach ($entities as $entity) { if (count(explode('/', $entity)) > 1) { $path = urldecode($entity); } else { $path = trim($this->subdir, '/') . '/' . urldecode($entity); } $file = Entity::fromPath($path, $this->connection->adapter()); if (!$file->exists()) { $view->setError(Lang::txt('Failed to find the file at ' . $path)); return $collection; } $collection->add(Entity::fromPath($path, $this->connection->adapter())); } } return $collection; }