/** * Show images within projects * * @return void */ public function mediaTask() { // Incoming $media = trim(Request::getVar('media', 'thumb')); $source = NULL; $redirect = false; $dir = 'preview'; if (!$this->model->exists()) { return false; } $uri = Request::getVar('SCRIPT_URL', '', 'server'); if (strstr($uri, 'Compiled:')) { $media = str_replace('Compiled:', '', strstr($uri, 'Compiled:')); $dir = 'compiled'; } if (strstr($uri, 'Tool:')) { $media = str_replace('Tool:', '', strstr($uri, 'Tool:')); $media = $media ? $media : 'default'; $dir = 'tools'; } // Show project thumbnail if ($media == 'thumb') { $source = $this->getThumbSrc(); } elseif ($media) { if ($media == 'master') { // Public picture $source = $this->getProjectImageSrc(); } elseif ($dir == 'tools') { $path = trim($this->config->get('imagepath', '/site/projects'), DS); $source = PATH_APP . DS . $path . DS . $this->model->get('alias') . DS . $dir . DS . $media . '.png'; if (!is_file($source)) { // Get default tool image $source = PATH_CORE . DS . trim($this->config->get('toolpic', 'plugins/projects/tools/images/default.gif'), DS); } } else { // Authorization required if (!$this->model->access('member')) { return; } $path = trim($this->config->get('imagepath', '/site/projects'), DS); $source = PATH_APP . DS . $path . DS . $this->model->get('alias') . DS . $dir . DS . $media; $redirect = true; } } if (is_file($source)) { $server = new \Hubzero\Content\Server(); $server->filename($source); $server->serve_inline($source); exit; } elseif ($redirect) { App::redirect(Route::url('index.php?option=' . $this->_option)); } return; }
/** * Download file(s) * * @return void redirect */ protected function _download() { // Incoming $render = Request::getVar('render', 'download'); $hash = Request::getVar('hash', ''); // Metadata collector $collector = array(); // Combine file and folder data $items = $this->_sortIncoming(); // Get stored remote connections $remotes = $this->_getRemoteConnections(false); // Params for repo call $params = array('subdir' => $this->subdir, 'remoteConnections' => $remotes); // Collect items if (!$items) { $this->setError(Lang::txt('PLG_PROJECTS_FILES_ERROR_NO_FILES_TO_SHOW_HISTORY')); } else { foreach ($items as $element) { foreach ($element as $type => $item) { // Get type and item name break; } // Must have a name if (trim($item) == '') { continue; } // Build metadata object $collector[] = $this->repo->getMetadata($item, $type, $params); } } // Check that we have item(s) to download if (empty($collector)) { // Throw error App::abort(404, Lang::txt('PLG_PROJECTS_FILES_FILE_NOT_FOUND')); } // File preview? if ($render == 'preview') { // Output HTML $view = new \Hubzero\Plugin\View(array('folder' => 'projects', 'element' => 'files', 'name' => 'preview')); $view->file = isset($collector[0]) ? $collector[0] : NULL; // Get last revision if (!$view->file->get('converted') && !$hash) { $params['file'] = $view->file; $hash = $this->repo->getLastRevision($params); $view->file->set('hash', $hash); } $view->option = $this->_option; $view->model = $this->model; if (!$view->file instanceof \Components\Projects\Models\File) { $view->setError(Lang::txt('PLG_PROJECTS_FILES_ERROR_FILE_INFO_NOT_FOUND')); } return $view->loadTemplate(); } // Other rendering? if ($render == 'thumb' || $render == 'inline' || $render == 'medium') { $file = isset($collector[0]) ? $collector[0] : NULL; if (!$file instanceof \Components\Projects\Models\File) { App::abort(404, Lang::txt('PLG_PROJECTS_FILES_FILE_NOT_FOUND')); } // Get last revision if (!$file->get('converted') && !$hash) { $params['file'] = $file; $hash = $this->repo->getLastRevision($params); } $image = $file->getPreview($this->model, $hash, 'fullPath', $render); // Serve image if ($image && is_file($image)) { $server = new \Hubzero\Content\Server(); $server->filename($image); $server->serve_inline($image); exit; } } // File download if (count($items) > 1) { $archive = $this->_archiveFiles($items); if (!$archive) { $this->setError($this->getError() . ' ' . Lang::txt('PLG_PROJECTS_FILES_ARCHIVE_ERROR')); } else { $downloadPath = $archive['path']; $serveas = 'Project Files ' . Date::toSql() . '.zip'; } } else { $file = isset($collector[0]) ? $collector[0] : NULL; if (!$file instanceof \Components\Projects\Models\File) { App::abort(404, Lang::txt('PLG_PROJECTS_FILES_FILE_NOT_FOUND')); } $serveas = $file->get('name'); // Open converted file if (!empty($this->_remoteService) && $file->get('converted') && $this->_task == 'open') { // Is user connected? $connected = $this->_connect->getStoredParam($this->_remoteService . '_token', $this->_uid); if (!$connected) { // Redirect to connect screen \Notify::message(Lang::txt('PLG_PROJECTS_FILES_REMOTE_PLEASE_CONNECT'), 'success', 'projects'); // Redirect App::redirect(Route::url($this->model->link('files') . '&action=connect')); } // Load remote resource $this->_connect->setUser($this->model->get('owned_by_user')); $resource = $this->_connect->loadRemoteResource($this->_remoteService, $this->model->get('owned_by_user'), $file->get('remoteId')); $openLink = $resource && isset($resource['alternateLink']) ? $resource['alternateLink'] : ''; if (!$openLink) { // Throw error App::abort(404, Lang::txt('PLG_PROJECTS_FILES_FILE_NOT_FOUND') . ' ' . $file->get('name')); } // Redirect App::redirect($openLink); } // Import & download converted file if (!empty($this->_remoteService) && $file->get('converted')) { $temp_path = sys_get_temp_dir(); // Load remote resource $this->_connect->setUser($this->model->get('owned_by_user')); $resource = $this->_connect->loadRemoteResource($this->_remoteService, $this->model->get('owned_by_user'), $file->get('remoteId')); // Tex file? $tex = Components\Projects\Helpers\Compiler::isTexFile($file->get('remoteTitle'), $file->get('originalFormat')); $cExt = $tex ? 'tex' : \Components\Projects\Helpers\Google::getGoogleImportExt($file->get('mimeType')); $url = \Components\Projects\Helpers\Google::getDownloadUrl($resource, $cExt); $data = $this->_connect->sendHttpRequest($this->_remoteService, $this->model->get('owned_by_user'), $url); // Clean up data from Windows characters - important! $data = $tex ? preg_replace('/[^(\\x20-\\x7F)\\x0A]*/', '', $data) : $data; $ftname = \Components\Projects\Helpers\Google::getImportFilename($file->get('remoteTitle'), $cExt); $serveas = $ftname; $this->_connect->fetchFile($data, $ftname, $temp_path); $downloadPath = $temp_path . DS . $ftname; } elseif ($hash) { $tempPath = 'temp-' . \Components\Projects\Helpers\Html::generateCode(4, 4, 0, 1, 0) . $serveas; $downloadPath = sys_get_temp_dir() . DS . $tempPath; // Get file content $params = array('fileName' => $file->get('localPath'), 'hash' => $hash, 'target' => $downloadPath); $this->repo->getFileContent($params); } else { // Viewing current file $serveas = urldecode(Request::getVar('serveas', $file->get('name'))); $downloadPath = $file->get('fullPath'); } } // Now we can actually download if (!empty($downloadPath)) { // Ensure the file exist if (!file_exists($downloadPath)) { // Throw error App::abort(404, Lang::txt('PLG_PROJECTS_FILES_FILE_NOT_FOUND')); } // Cannot download zero byte files if (filesize($downloadPath) == 0) { $this->setError(Lang::txt('PLG_PROJECTS_FILES_ERROR_ZERO_BYTE')); } // Proceed with download if (!$this->getError()) { // Initiate a new content server and serve up the file $server = new \Hubzero\Content\Server(); $server->filename($downloadPath); $server->disposition('attachment'); $server->acceptranges(false); $server->saveas($serveas); $result = $server->serve_attachment($downloadPath, $serveas, false); if (!$result) { // Should only get here on error App::abort(404, Lang::txt('PLG_PROJECTS_FILES_SERVER_ERROR')); } else { // Clean up the /tmp directory from zip files (download multiple files) $temp_path = sys_get_temp_dir(); $matches = array(); preg_match('/^(\\/tmp.*?\\.zip)/is', $downloadPath, $matches); if (!empty($matches)) { \Hubzero\Filesystem::delete($downloadPath); } exit; } } } // Pass success or error message if ($this->getError()) { \Notify::message($this->getError(), 'error', 'projects'); } // Redirect to file list $url = $this->model->link('files') . '&action=browse'; $url .= $this->repo->isLocal() ? '' : '&repo=' . $this->repo->get('name'); $url .= $this->subdir ? '&subdir=' . urlencode($this->subdir) : ''; // Redirect App::redirect(Route::url($url)); }
/** * Download a file * * @return void */ public function downloadTask() { // Incoming $pid = Request::getInt('id', 0); $vid = Request::getInt('v', 0); $source = NULL; // Need pub and version ID if (!$pid || $pid == 0 || !$vid) { return; } // Get the file name $uri = Request::getVar('REQUEST_URI', '', 'server'); if (strstr($uri, 'Image:')) { $file = str_replace('Image:', '', strstr($uri, 'Image:')); } elseif (strstr($uri, 'File:')) { $file = str_replace('File:', '', strstr($uri, 'File:')); } //decode file name $file = urldecode($file); if (strtolower($file) == 'thumb') { // Get publication thumbnail $source = Helpers\Html::getThumb($pid, $vid, $this->config); } else { // Build publication path $path = Helpers\Html::buildPubPath($pid, $vid, $this->config->get('webpath')); if (strtolower($file) == 'master') { // Get master image $source = $path . DS . 'master.png'; // Default image if (!is_file(PATH_APP . DS . $source)) { // Grab first bigger image in gallery if (is_dir(PATH_APP . DS . $path . DS . 'gallery')) { $file_list = scandir(PATH_APP . DS . $path . DS . 'gallery'); foreach ($file_list as $file) { list($width, $height, $type, $attr) = getimagesize(PATH_APP . DS . $path . DS . 'gallery' . DS . $file); if ($width > 200) { $source = $path . DS . 'gallery' . DS . $file; break; } } } if (!is_file(PATH_APP . DS . $source)) { $source = PATH_CORE . DS . trim($this->config->get('masterimage', 'components/com_publications/site/assets/img/master.png'), DS); } } } else { // Load from gallery $source = PATH_APP . DS . $path . DS . 'gallery' . DS . $file; // Default image if (!is_file($source)) { $source = PATH_CORE . DS . trim($this->config->get('gallery_thumb', 'components/com_publications/site/assets/img/gallery_thumb.gif'), DS); } } } if (is_file($source)) { $server = new \Hubzero\Content\Server(); $server->filename($source); $server->serve_inline($source); exit; } return; }