/** * 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)); }
public function renderlatexTask() { $expression = Request::getVar('expression', ''); $dir = PATH_APP . DS . 'cache' . DS . 'ckeditor' . DS . 'hubzeroequation' . DS; $filename = uniqid("equation_"); $error = null; //build tex document $doc = '\\documentclass[12pt]{article}' . "\n"; $doc .= '\\usepackage[utf8]{inputenc}' . "\n"; $doc .= '\\usepackage{amssymb,amsmath}' . "\n"; $doc .= '\\usepackage{color}' . "\n"; $doc .= '\\usepackage{amsfonts}' . "\n"; $doc .= '\\usepackage{amssymb}' . "\n"; $doc .= '\\usepackage{pst-plot}' . "\n"; $doc .= '\\begin{document}' . "\n"; $doc .= '\\pagestyle{empty}' . "\n"; $doc .= '\\begin{displaymath}' . "\n"; $doc .= $expression . "\n"; $doc .= '\\end{displaymath}' . "\n"; $doc .= '\\end{document}' . "\n"; //if cache doesn't exist, create it if (!is_dir($dir)) { \Hubzero\Filesystem::makeDirectory($dir); } if (file_put_contents($dir . DS . $filename . '.tex', $doc) === false) { throw new \Exception('Failed to open target file'); } try { //execute latex to build dvi $command = 'cd ' . $dir . '; /usr/bin/latex ' . $filename . '.tex < /dev/null |grep ^!|grep -v Emergency > ' . $dir . DS . $filename . '.error 2> /dev/null 2>&1'; exec($command, $output_lines, $exit_status); //execute dvi2png to build png $command = "/usr/bin/dvipng -bg 'transparent' -q -T tight -D 100 -o " . $dir . DS . $filename . '.png ' . $dir . DS . $filename . '.dvi 2>&1'; exec($command, $output_lines, $exit_status); if ($exit_status != 0) { throw new \Exception("dvi2png failed"); } } catch (\Exception $e) { $error = $e->getMessage(); } //build response $object = new stdClass(); if ($error) { $object->error = $error; $object->img = 'data:image/png;base64,'; } else { //no errors - send base64 encoded image $object->error = ""; $imgbinary = fread(fopen($dir . DS . $filename . '.png', 'r'), filesize($dir . DS . $filename . '.png')); $base64img = 'data:image/png;base64,' . base64_encode($imgbinary); $object->img = $base64img; } $object->expression = $expression; //clean up our cache mess shell_exec('rm ' . $dir . $filename . '.*'); $this->send($object); }