/**
  * Download XSD
  *
  * @return  void
  */
 public function xsdTask()
 {
     $path = $this->getSchema();
     if (file_exists($path)) {
         $server = new \Hubzero\Content\Server();
         $server->filename($path);
         $server->disposition('attachment');
         $server->acceptranges(true);
         $server->saveas(basename($path));
         if (!$server->serve()) {
             // Should only get here on error
             throw new Exception(Lang::txt('COM_PUBLICATIONS_SERVER_ERROR'), 404);
         } else {
             exit;
         }
     }
     App::redirect(Route::url('index.php?option=' . $this->_option . '&controller=' . $this->_controller, false), Lang::txt('COM_PUBLICATIONS_BATCH_ERROR_NO_XSD'), 'error');
 }
Exemple #2
0
 /**
  * Unlock sync and view sync log for project
  *
  * @return  void
  */
 public function fixsyncTask()
 {
     $id = Request::getVar('id', 0);
     $service = 'google';
     // Initiate extended database class
     $obj = new Tables\Project($this->database);
     if (!$id or !$obj->loadProject($id)) {
         App::redirect(Route::url('index.php?option=' . $this->_option, false), Lang::txt('COM_PROJECTS_NOTICE_ID_NOT_FOUND'), 'error');
         return;
     }
     // Unlock sync
     $obj->saveParam($id, $service . '_sync_lock', '');
     // Get log file
     $repodir = Helpers\Html::getProjectRepoPath($obj->alias, 'logs');
     $sfile = $repodir . DS . 'sync.' . Date::format('Y-m') . '.log';
     if (file_exists($sfile)) {
         // Serve up file
         $server = new \Hubzero\Content\Server();
         $server->filename($sfile);
         $server->disposition('attachment');
         $server->acceptranges(false);
         $server->saveas('sync.' . Date::format('Y-m') . '.txt');
         $result = $server->serve_attachment($sfile, 'sync.' . Date::format('Y-m') . '.txt', false);
         exit;
     }
     // Redirect
     App::redirect(Route::url('index.php?option=' . $this->_option . '&task=edit&id=' . $id, false), Lang::txt('Sync log unavailable'));
 }
Exemple #3
0
 /**
  * Compile PDF/image preview for any kind of file
  *
  *
  * @return  mixed  array or false
  */
 protected function _compile()
 {
     // Combine file and folder data
     $items = $this->_sortIncoming();
     // Get stored remote connections
     $remotes = $this->_getRemoteConnections();
     // Params for repo call
     $params = array('subdir' => $this->subdir, 'remoteConnections' => $remotes);
     // Incoming
     $commit = Request::getInt('commit', 0);
     $download = Request::getInt('download', 0);
     // Check that we have compile enabled
     if (!$this->params->get('latex')) {
         $this->setError(Lang::txt('PLG_PROJECTS_FILES_COMPILE_NOTALLOWED'));
         return;
     }
     // Output HTML
     $view = new \Hubzero\Plugin\View(array('folder' => 'projects', 'element' => 'files', 'name' => 'compiled'));
     // Get selected item
     if (!$items) {
         $view->setError(Lang::txt('PLG_PROJECTS_FILES_ERROR_NO_FILES_TO_COMPILE'));
         $view->loadTemplate();
         return;
     } else {
         foreach ($items as $element) {
             foreach ($element as $type => $item) {
                 // Get our metadata
                 $file = $this->repo->getMetadata($item, 'file', $params);
                 break;
             }
         }
     }
     // We need a file
     if (empty($file)) {
         $view->setError(Lang::txt('PLG_PROJECTS_FILES_ERROR_NO_FILES_TO_COMPILE'));
         $view->loadTemplate();
         return;
     }
     // Path for storing temp previews
     $imagePath = trim($this->model->config()->get('imagepath', '/site/projects'), DS);
     $outputDir = DS . $imagePath . DS . strtolower($this->model->get('alias')) . DS . 'compiled';
     // Make sure output dir exists
     if (!is_dir(PATH_APP . $outputDir)) {
         if (!Filesystem::makeDirectory(PATH_APP . $outputDir)) {
             $this->setError(Lang::txt('PLG_PROJECTS_FILES_UNABLE_TO_CREATE_UPLOAD_PATH'));
             return;
         }
     }
     // Get LaTeX helper
     $compiler = new \Components\Projects\Helpers\Compiler();
     // Tex compiler path
     $texPath = DS . trim($this->params->get('texpath'), DS);
     $view->file = $file;
     $view->oWidth = '780';
     $view->oHeight = '460';
     $view->url = Route::url($this->model->link('files'));
     $cExt = 'pdf';
     // Take out Google native extension if present
     $fileName = $file->get('name');
     if (in_array($file->get('ext'), \Components\Projects\Helpers\Google::getGoogleNativeExts())) {
         $fileName = preg_replace("/." . $file->get('ext') . "\\z/", "", $file->get('name'));
     }
     // Tex file?
     $tex = $compiler->isTexFile($fileName);
     // Build temp name
     $tempBase = $tex ? 'temp__' . \Components\Projects\Helpers\Html::takeOutExt($fileName) : $fileName;
     $tempBase = str_replace(' ', '_', $tempBase);
     // Get file contents
     if (!empty($this->_remoteService) && $file->get('converted')) {
         // 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'));
         $cExt = $tex ? 'tex' : \Components\Projects\Helpers\Google::getGoogleImportExt($resource['mimeType']);
         $cExt = in_array($cExt, array('tex', 'jpeg')) ? $cExt : 'pdf';
         $url = \Components\Projects\Helpers\Google::getDownloadUrl($resource, $cExt);
         // Get data
         $view->data = $this->_connect->sendHttpRequest($this->_remoteService, $this->model->get('owned_by_user'), $url);
     } elseif ($file->exists()) {
         $view->data = $file->isImage() ? NULL : $file->contents();
     } else {
         $this->setError(Lang::txt('PLG_PROJECTS_FILES_ERROR_COMPILE_NO_DATA'));
     }
     // LaTeX file?
     if ($tex && !empty($view->data)) {
         // Clean up data from Windows characters - important!
         $view->data = preg_replace('/[^(\\x20-\\x7F)\\x0A]*/', '', $view->data);
         // Compile and get path to PDF
         $contentFile = $compiler->compileTex($file->get('fullPath'), $view->data, $texPath, PATH_APP . $outputDir, 1, $tempBase);
         // Read log (to show in case of error)
         $logFile = $tempBase . '.log';
         if (file_exists(PATH_APP . $outputDir . DS . $logFile)) {
             $view->log = Filesystem::read(PATH_APP . $outputDir . DS . $logFile);
         }
         if (!$contentFile) {
             $this->setError(Lang::txt('PLG_PROJECTS_FILES_ERROR_COMPILE_TEX_FAILED'));
         }
     } elseif ($file->get('converted') && !empty($view->data)) {
         $tempBase = \Components\Projects\Helpers\Google::getImportFilename($file->get('name'), $cExt);
         // Write content to temp file
         $this->_connect->fetchFile($view->data, $tempBase, PATH_APP . $outputDir);
         $contentFile = $tempBase;
     } elseif (!$this->getError()) {
         // Make sure we can handle preview of this type of file
         if ($file->get('ext') == 'pdf' || $file->isImage() || !$file->isBinary()) {
             Filesystem::copy($file->get('fullPath'), PATH_APP . $outputDir . DS . $tempBase);
             $contentFile = $tempBase;
         }
     }
     $url = $this->model->link('files');
     $url .= $this->repo->isLocal() ? '' : '&repo=' . $this->repo->get('name');
     $url .= $this->subdir ? '&subdir=' . urlencode($this->subdir) : '';
     // Parse output
     if (!empty($contentFile) && file_exists(PATH_APP . $outputDir . DS . $contentFile)) {
         // Get compiled content mimetype
         $cType = Filesystem::mimetype(PATH_APP . $outputDir . DS . $contentFile);
         // Is image?
         if (strpos($cType, 'image/') !== false) {
             // Fix up object width & height
             list($width, $height, $type, $attr) = getimagesize(PATH_APP . $outputDir . DS . $contentFile);
             $xRatio = $view->oWidth / $width;
             $yRatio = $view->oHeight / $height;
             if ($xRatio * $height < $view->oHeight) {
                 // Resize the image based on width
                 $view->oHeight = ceil($xRatio * $height);
             } else {
                 // Resize the image based on height
                 $view->oWidth = ceil($yRatio * $width);
             }
         }
         // Download compiled file?
         if ($download) {
             $pdfName = $tex ? str_replace('temp__', '', basename($contentFile)) : basename($contentFile);
             // Serve up file
             $server = new \Hubzero\Content\Server();
             $server->filename(PATH_APP . $outputDir . DS . $contentFile);
             $server->disposition('attachment');
             $server->acceptranges(false);
             $server->saveas($pdfName);
             $result = $server->serve_attachment(PATH_APP . $outputDir . DS . $contentFile, $pdfName, false);
             if (!$result) {
                 // Should only get here on error
                 App::abort(404, Lang::txt('PLG_PROJECTS_FILES_SERVER_ERROR'));
             } else {
                 exit;
             }
         }
         // Add compiled PDF to repository?
         if ($commit && $tex) {
             $pdfName = str_replace('temp__', '', basename($contentFile));
             $where = $this->subdir ? $this->subdir . DS . $pdfName : $pdfName;
             if (Filesystem::copy(PATH_APP . $outputDir . DS . $contentFile, $this->_path . DS . $where)) {
                 // Checkin into repo
                 $params = array('subdir' => $this->subdir);
                 $params['file'] = $this->repo->getMetadata($pdfName, 'file', $params);
                 $this->repo->call('checkin', $params);
                 if ($this->repo->isLocal()) {
                     $this->model->saveParam('google_sync_queue', 1);
                 }
                 \Notify::message(Lang::txt('PLG_PROJECTS_FILES_SUCCESS_COMPILED'), 'success', 'projects');
                 // Redirect to file list
                 App::redirect(Route::url($url));
                 return;
             }
         }
         // Generate preview image for browsers that cannot embed pdf
         if ($cType == 'application/pdf') {
             // GS path
             $gspath = trim($this->params->get('gspath'), DS);
             if ($gspath && file_exists(DS . $gspath . DS . 'gs')) {
                 $gspath = DS . $gspath . DS;
                 $pdfName = $tex ? str_replace('temp__', '', basename($contentFile)) : basename($contentFile);
                 $pdfPath = PATH_APP . $outputDir . DS . $contentFile;
                 $exportPath = PATH_APP . $outputDir . DS . $tempBase . '%d.jpg';
                 exec($gspath . "gs -dNOPAUSE -sDEVICE=jpeg -r300 -dFirstPage=1 -dLastPage=1 -sOutputFile={$exportPath} {$pdfPath} 2>&1", $out);
                 if (is_file(PATH_APP . $outputDir . DS . $tempBase . '1.jpg')) {
                     $hi = new \Hubzero\Image\Processor(PATH_APP . $outputDir . DS . $tempBase . '1.jpg');
                     if (count($hi->getErrors()) == 0) {
                         $hi->resize($view->oWidth, false, false, true);
                         $hi->save(PATH_APP . $outputDir . DS . $tempBase . '1.jpg');
                     } else {
                         return false;
                     }
                 }
                 if (is_file(PATH_APP . $outputDir . DS . $tempBase . '1.jpg')) {
                     $image = $tempBase . '1.jpg';
                 }
             }
         }
     } elseif (!$this->getError()) {
         $this->setError(Lang::txt('PLG_PROJECTS_FILES_ERROR_COMPILE_PREVIEW_FAILED'));
     }
     $view->file = $file;
     $view->outputDir = $outputDir;
     $view->embed = $contentFile;
     $view->cType = $cType;
     $view->subdir = $this->subdir;
     $view->option = $this->_option;
     $view->image = !empty($image) ? $image : NULL;
     $view->model = $this->model;
     $view->repo = $this->repo;
     if ($this->getError()) {
         $view->setError($this->getError());
     }
     return $view->loadTemplate();
 }
 /**
  * Compiles PDF/image preview for any kind of file
  *
  * @return  string
  */
 public function compile()
 {
     // Combine file and folder data
     $items = $this->getCollection();
     // Incoming
     $download = Request::getInt('download', 0);
     // Check that we have compile enabled
     // @FIXME: why are latex and compiled preview tied together?
     //         presumedly we are also 'compiling' pdfs?
     if (!$this->params->get('latex')) {
         $this->setError(Lang::txt('PLG_PROJECTS_FILES_COMPILE_NOTALLOWED'));
         return;
     }
     // Output HTML
     $view = new \Hubzero\Plugin\View(['folder' => 'projects', 'element' => 'files', 'name' => 'connected', 'layout' => 'compiled']);
     // Make sure we have an item
     if (count($items) == 0) {
         $view->setError(Lang::txt('PLG_PROJECTS_FILES_ERROR_NO_FILES_TO_COMPILE'));
         $view->loadTemplate();
         return;
     }
     // We can only handle one file at a time
     $file = $items->first();
     // Build path for storing temp previews
     $imagePath = trim($this->model->config()->get('imagepath', '/site/projects'), DS);
     $outputDir = DS . $imagePath . DS . strtolower($this->model->get('alias')) . DS . 'compiled';
     // Make sure output dir exists
     if (!is_dir(PATH_APP . $outputDir)) {
         if (!Filesystem::makeDirectory(PATH_APP . $outputDir)) {
             $this->setError(Lang::txt('PLG_PROJECTS_FILES_UNABLE_TO_CREATE_UPLOAD_PATH'));
             return;
         }
     }
     // Get LaTeX helper
     $compiler = new \Components\Projects\Helpers\Compiler();
     // Tex compiler path
     $texPath = DS . trim($this->params->get('texpath'), DS);
     // Set view args and defaults
     $view->file = $file;
     $view->oWidth = '780';
     $view->oHeight = '460';
     $view->url = $this->model->link('files');
     $cExt = 'pdf';
     // Tex file?
     $tex = $compiler->isTexFile($file->getName());
     // Build temp name
     $tempBase = $tex ? 'temp__' . \Components\Projects\Helpers\Html::takeOutExt($file->getName()) : $file->getName();
     $tempBase = str_replace(' ', '_', $tempBase);
     $view->data = $file->isImage() ? NULL : $file->read();
     // LaTeX file?
     if ($tex && !empty($view->data)) {
         // Clean up data from Windows characters - important!
         $view->data = preg_replace('/[^(\\x20-\\x7F)\\x0A]*/', '', $view->data);
         // Store file locally
         $tmpfile = PATH_APP . $outputDir . DS . $tempBase;
         file_put_contents($tmpfile, $view->data);
         // Compile and get path to PDF
         $contentFile = $compiler->compileTex($tmpfile, $view->data, $texPath, PATH_APP . $outputDir, 1, $tempBase);
         // Read log (to show in case of error)
         $logFile = $tempBase . '.log';
         if (file_exists(PATH_APP . $outputDir . DS . $logFile)) {
             $view->log = Filesystem::read(PATH_APP . $outputDir . DS . $logFile);
         }
         if (!$contentFile) {
             $this->setError(Lang::txt('PLG_PROJECTS_FILES_ERROR_COMPILE_TEX_FAILED'));
         }
         $cType = Filesystem::mimetype(PATH_APP . $outputDir . DS . $contentFile);
     } else {
         // Make sure we can handle preview of this type of file
         if ($file->hasExtension('pdf') || $file->isImage() || !$file->isBinary()) {
             $origin = $this->connection->provider->alias . '://' . $file->getPath();
             $dest = 'compiled://' . $tempBase;
             // Do the copy
             Manager::adapter('local', ['path' => PATH_APP . $outputDir . DS], 'compiled');
             Manager::copy($origin, $dest);
             $contentFile = $tempBase;
         }
     }
     // Parse output
     if (!empty($contentFile) && file_exists(PATH_APP . $outputDir . DS . $contentFile)) {
         // Get compiled content mimetype
         $cType = Filesystem::mimetype(PATH_APP . $outputDir . DS . $contentFile);
         // Is image?
         if (strpos($cType, 'image/') !== false) {
             // Fix up object width & height
             list($width, $height, $type, $attr) = getimagesize(PATH_APP . $outputDir . DS . $contentFile);
             $xRatio = $view->oWidth / $width;
             $yRatio = $view->oHeight / $height;
             if ($xRatio * $height < $view->oHeight) {
                 // Resize the image based on width
                 $view->oHeight = ceil($xRatio * $height);
             } else {
                 // Resize the image based on height
                 $view->oWidth = ceil($yRatio * $width);
             }
         }
         // Download compiled file?
         if ($download) {
             $pdfName = $tex ? str_replace('temp__', '', basename($contentFile)) : basename($contentFile);
             // Serve up file
             $server = new \Hubzero\Content\Server();
             $server->filename(PATH_APP . $outputDir . DS . $contentFile);
             $server->disposition('attachment');
             $server->acceptranges(false);
             $server->saveas($pdfName);
             $result = $server->serve();
             if (!$result) {
                 // Should only get here on error
                 throw new Exception(Lang::txt('PLG_PROJECTS_FILES_SERVER_ERROR'), 404);
             } else {
                 exit;
             }
         }
         // Generate preview image for browsers that cannot embed pdf
         if ($cType == 'application/pdf') {
             // GS path
             $gspath = trim($this->params->get('gspath'), DS);
             if ($gspath && file_exists(DS . $gspath . DS . 'gs')) {
                 $gspath = DS . $gspath . DS;
                 $pdfName = $tex ? str_replace('temp__', '', basename($contentFile)) : basename($contentFile);
                 $pdfPath = PATH_APP . $outputDir . DS . $contentFile;
                 $exportPath = PATH_APP . $outputDir . DS . $tempBase . '%d.jpg';
                 exec($gspath . "gs -dNOPAUSE -sDEVICE=jpeg -r300 -dFirstPage=1 -dLastPage=1 -sOutputFile={$exportPath} {$pdfPath} 2>&1", $out);
                 if (is_file(PATH_APP . $outputDir . DS . $tempBase . '1.jpg')) {
                     $hi = new \Hubzero\Image\Processor(PATH_APP . $outputDir . DS . $tempBase . '1.jpg');
                     if (count($hi->getErrors()) == 0) {
                         $hi->resize($view->oWidth, false, false, true);
                         $hi->save(PATH_APP . $outputDir . DS . $tempBase . '1.jpg');
                     } else {
                         return false;
                     }
                 }
                 if (is_file(PATH_APP . $outputDir . DS . $tempBase . '1.jpg')) {
                     $image = $tempBase . '1.jpg';
                 }
             }
         }
     } elseif (!$this->getError()) {
         $this->setError(Lang::txt('PLG_PROJECTS_FILES_ERROR_COMPILE_PREVIEW_FAILED'));
     }
     $view->file = $file;
     $view->outputDir = $outputDir;
     $view->embed = $contentFile;
     $view->cType = $cType;
     $view->subdir = $this->subdir;
     $view->option = $this->_option;
     $view->image = !empty($image) ? $image : NULL;
     $view->model = $this->model;
     $view->repo = $this->repo;
     $view->connection = $this->connection;
     if ($this->getError()) {
         $view->setError($this->getError());
     }
     return $view->loadTemplate();
 }
Exemple #5
0
 /**
  * Serves up the file to the web
  *
  * @param   string  $as  What to serve the file as
  * @return  bool
  **/
 public function serve($as = null)
 {
     // Initiate a new content server
     $server = new \Hubzero\Content\Server();
     $server->disposition('attachment');
     $server->acceptranges(false);
     if (!$this->isLocal()) {
         // Create a temp file and write to it
         $temp = tmpfile();
         fwrite($temp, $this->read());
         $server->filename(stream_get_meta_data($temp)['uri']);
     } else {
         $server->filename($this->getAbsolutePath());
     }
     $server->saveas($as ?: $this->getName());
     // Serve up the file
     $result = $server->serve();
     // Clean up after serving
     if (isset($temp) && is_resource($temp)) {
         fclose($temp);
     }
     return $result;
 }
Exemple #6
0
 /**
  * Download a file
  *
  * @param   object  $member
  * @return  void
  */
 protected function _download($member)
 {
     $database = App::get('db');
     // Ensure we have a database object
     if (!$database) {
         App::abort(500, Lang::txt('DATABASE_NOT_FOUND'));
         return;
     }
     // Incoming
     $uid = $member->get('id');
     // Load the resume
     $resume = new \Components\Jobs\Tables\Resume($database);
     $file = '';
     $path = $this->build_path($uid);
     if ($resume->loadResume($uid)) {
         $file = PATH_APP . $path . DS . $resume->filename;
     }
     if (!is_file($file)) {
         App::abort(404, Lang::txt('FILE_NOT_FOUND'));
         return;
     }
     // Use user name as file name
     $default_title = $member->get('givenName') ? $member->get('givenName') . ' ' . $member->get('surname') . ' ' . ucfirst(Lang::txt('Resume')) : $member->get('name') . ' ' . ucfirst(Lang::txt('Resume'));
     $default_title .= substr($resume->filename, strripos($resume->filename, '.'));
     // Initiate a new content server and serve up the file
     $xserver = new \Hubzero\Content\Server();
     $xserver->filename($file);
     // record view
     $stats = new \Components\Jobs\Tables\JobStats($database);
     if (User::get('id') != $uid) {
         $stats->saveView($uid, 'seeker');
     }
     $xserver->disposition('attachment');
     $xserver->acceptranges(false);
     // @TODO fix byte range support
     $xserver->saveas(stripslashes($resume->title));
     $result = $xserver->serve_attachment($file, stripslashes($default_title), false);
     // @TODO fix byte range support
     if (!$result) {
         App::abort(500, Lang::txt('SERVER_ERROR'));
     }
     exit;
 }
 /**
  * Generate detailed responses CSV files and zip and offer up as download
  *
  * @return void
  **/
 private function downloadresponses()
 {
     require_once PATH_CORE . DS . 'components' . DS . 'com_courses' . DS . 'models' . DS . 'formReport.php';
     // Only allow for instructors
     if (!$this->course->offering()->section()->access('manage')) {
         App::abort(403, 'Sorry, you don\'t have permission to do this');
     }
     if (!($asset_ids = Request::getVar('assets', false))) {
         App::abort(422, 'Sorry, we don\'t know what results you\'re trying to retrieve');
     }
     $protected = 'site' . DS . 'protected';
     $tmp = $protected . DS . 'tmp';
     // We're going to temporarily house this in PATH_APP/site/protected/tmp
     if (!Filesystem::exists($protected)) {
         App::abort(500, 'Missing temporary directory');
     }
     // Make sure tmp folder exists
     if (!Filesystem::exists($tmp)) {
         Filesystem::makeDirectory($tmp);
     } else {
         // Folder was already there - do a sanity check and make sure no old responses zips are lying around
         $files = Filesystem::files($tmp);
         if ($files && count($files) > 0) {
             foreach ($files as $file) {
                 if (strstr($file, 'responses.zip') !== false) {
                     Filesystem::delete($tmp . DS . $file);
                 }
             }
         }
     }
     // Get the individual asset ids
     $asset_ids = explode('-', $asset_ids);
     // Set up our zip archive
     $zip = new ZipArchive();
     $path = PATH_APP . DS . $tmp . DS . time() . '.responses.zip';
     $zip->open($path, ZipArchive::CREATE);
     // Loop through the assets
     foreach ($asset_ids as $asset_id) {
         // Is it a number?
         if (!is_numeric($asset_id)) {
             continue;
         }
         // Get the rest of the asset row
         $asset = new \Components\Courses\Tables\Asset($this->db);
         $asset->load($asset_id);
         // Make sure asset is a part of this course
         if ($asset->get('course_id') != $this->course->get('id')) {
             continue;
         }
         if ($details = \Components\Courses\Models\FormReport::getLetterResponsesForAssetId($this->db, $asset_id, true, $this->course->offering()->section()->get('id'))) {
             $output = implode(',', $details['headers']) . "\n";
             if (isset($details['responses']) && count($details['responses']) > 0) {
                 foreach ($details['responses'] as $response) {
                     $output .= implode(',', $response) . "\n";
                 }
             }
             $zip->addFromString($asset_id . '.responses.csv', $output);
         } else {
             continue;
         }
     }
     // Close the zip archive handler
     $zip->close();
     if (is_file($path)) {
         // Set up the server
         $xserver = new \Hubzero\Content\Server();
         $xserver->filename($path);
         $xserver->saveas('responses.zip');
         $xserver->disposition('attachment');
         $xserver->acceptranges(false);
         // Serve the file
         $xserver->serve();
         // Now delete the file
         Filesystem::delete($path);
     }
     // All done!
     exit;
 }
 /**
  * Serve publication content
  * Determine how to render depending on master type, attachment type and user choice
  * Defaults to download
  *
  * @return  void
  */
 public function serveTask()
 {
     // Incoming
     $aid = Request::getInt('a', 0);
     // Attachment id
     $elementId = Request::getInt('el', 1);
     // Element id
     $render = Request::getVar('render', '');
     $vid = Request::getInt('vid', '');
     $file = Request::getVar('file', '');
     $disp = Request::getVar('disposition');
     $disp = in_array($disp, array('inline', 'attachment')) ? $disp : 'attachment';
     // Get our model and load publication data
     $this->model = new Models\Publication($this->_identifier, $this->_version, $vid);
     if (!$this->model->exists() || $this->model->isDeleted()) {
         App::redirect(Route::url('index.php?option=' . $this->_option), Lang::txt('COM_PUBLICATIONS_RESOURCE_NOT_FOUND'), 'error');
         return;
     }
     // Is the visitor authorized to view content?
     if (!$this->model->access('view-all')) {
         $this->_blockAccess();
         return true;
     }
     // Set curation
     $this->model->setCuration();
     // Bundle requested?
     if ($render == 'archive') {
         // Produce archival package
         if ($this->model->_curationModel->package()) {
             // Log access
             if ($this->model->isPublished()) {
                 $this->model->logAccess('primary');
             }
             $this->model->_curationModel->serveBundle();
             return;
         } else {
             throw new Exception(Lang::txt('COM_PUBLICATIONS_ERROR_FINDING_ATTACHMENTS'), 404);
             return;
         }
     }
     // Bundle requested?
     if ($render == 'showcontents') {
         // Produce archival package
         if ($this->model->_curationModel->package()) {
             // Build the HTML of the "about" tab
             $view = new \Hubzero\Component\View(['name' => 'view', 'layout' => '_contents']);
             $view->model = $this->model;
             $view->option = $this->_option;
             $view->display();
             return;
         } else {
             throw new Exception(Lang::txt('COM_PUBLICATIONS_ERROR_FINDING_ATTACHMENTS'), 404);
             return;
         }
     }
     // Serving data file (dataview)
     if ($file) {
         // Ensure the file exist
         if (!file_exists($this->model->path('data', true) . DS . trim($file))) {
             throw new Exception(Lang::txt('COM_PUBLICATIONS_ERROR_FINDING_ATTACHMENTS'), 404);
             return;
         }
         // Initiate a new content server and serve up the file
         $server = new \Hubzero\Content\Server();
         $server->filename($this->model->path('data', true) . DS . trim($file));
         $server->disposition($disp);
         $server->acceptranges(true);
         $server->saveas(basename($file));
         if (!$server->serve()) {
             // Should only get here on error
             throw new Exception(Lang::txt('COM_PUBLICATIONS_SERVER_ERROR'), 404);
         } else {
             exit;
         }
     }
     $this->model->attachments();
     // Individual attachment is requested? Find element ID
     if ($aid) {
         $elementId = $this->model->_curationModel->getElementIdByAttachment($aid);
     }
     // We do need attachments
     if (!isset($this->model->_attachments['elements'][$elementId]) || empty($this->model->_attachments['elements'][$elementId])) {
         throw new Exception(Lang::txt('COM_PUBLICATIONS_ERROR_FINDING_ATTACHMENTS'), 404);
         return;
     }
     // Get element manifest to deliver content as intended
     $curation = $this->model->_curationModel->getElementManifest($elementId);
     // We do need manifest!
     if (!$curation || !isset($curation->element) || !$curation->element) {
         return false;
     }
     // Get attachment type model
     $attModel = new Models\Attachments($this->database);
     // Log access
     if ($this->model->isPublished()) {
         $aType = $curation->element->params->role == 1 ? 'primary' : 'support';
         $this->model->logAccess($aType);
     }
     // Serve content
     $content = $attModel->serve($curation->element->params->type, $curation->element, $elementId, $this->model, $curation->block->params, $aid);
     // No content served
     if ($content === NULL || $content == false) {
         throw new Exception(Lang::txt('COM_PUBLICATIONS_ERROR_FINDING_ATTACHMENTS'), 404);
     }
     // Do we need to redirect to content?
     if ($attModel->get('redirect')) {
         App::redirect($attModel->get('redirect'));
         return;
     }
     return $content;
 }
 /**
  * Serve publication-related file (via public link)
  *
  * @param   int  	$projectid
  * @return  void
  */
 public function serve($type = '', $projectid = 0, $query = '')
 {
     $this->_area = $this->onProjectAreas();
     if ($type != $this->_area['name']) {
         return false;
     }
     $data = json_decode($query);
     if (!isset($data->pid) || !$projectid) {
         return false;
     }
     $disp = isset($data->disp) ? $data->disp : 'inline';
     $type = isset($data->type) ? $data->type : 'file';
     $folder = isset($data->folder) ? $data->folder : 'wikicontent';
     $fpath = isset($data->path) ? $data->path : 'inline';
     $limited = isset($data->limited) ? $data->limited : 0;
     if ($type != 'file') {
         return false;
     }
     $database = App::get('db');
     // Instantiate a project
     $model = new \Components\Projects\Models\Project($projectid);
     if (!$model->exists() || $limited == 1 && !$model->access('member')) {
         // Throw error
         throw new Exception(Lang::txt('COM_PROJECTS_ERROR_ACTION_NOT_AUTHORIZED'), 403);
         return;
     }
     // Get referenced path
     $pubconfig = Component::params('com_publications');
     $base_path = $pubconfig->get('webpath');
     $pubPath = \Components\Publications\Helpers\Html::buildPubPath($data->pid, $data->vid, $base_path, $folder, $root = 0);
     $serve = PATH_APP . $pubPath . DS . $fpath;
     // Ensure the file exist
     if (!file_exists($serve)) {
         // Throw error
         throw new Exception(Lang::txt('COM_PROJECTS_FILE_NOT_FOUND'), 404);
         return;
     }
     // Initiate a new content server and serve up the file
     $server = new \Hubzero\Content\Server();
     $server->filename($serve);
     $server->disposition($disp);
     $server->acceptranges(false);
     // @TODO fix byte range support
     $server->saveas(basename($fpath));
     if (!$server->serve()) {
         // Should only get here on error
         throw new Exception(Lang::txt('COM_PUBLICATIONS_SERVER_ERROR'), 404);
     } else {
         exit;
     }
     return;
 }
Exemple #10
0
 /**
  * Batch resume download
  *
  * @return     void
  */
 public function batchTask()
 {
     // Login required
     if (User::isGuest()) {
         \Notify::warning(Lang::txt('COM_JOBS_PLEASE_LOGIN_ACCESS_EMPLOYER'));
         $this->login();
         return;
     }
     // Check authorization
     if (!$this->_admin && !$this->_emp) {
         App::redirect(Route::url('index.php?option=com_jobs&task=subscribe'));
     }
     // Incoming
     $pile = Request::getVar('pile', 'all');
     // Zip the requested resumes
     $archive = $this->_archiveResumes($pile);
     if ($archive) {
         // Initiate a new content server and serve up the file
         $xserver = new \Hubzero\Content\Server();
         $xserver->filename($archive['path']);
         $xserver->disposition('attachment');
         $xserver->acceptranges(false);
         $xserver->saveas(Lang::txt('JOBS_RESUME_BATCH=Resume Batch'));
         $result = $xserver->serve_attachment($archive['path'], $archive['name'], false);
         // Delete downloaded zip
         \Filesystem::delete($archive['path']);
         if (!$result) {
             throw new Exception(Lang::txt('COM_JOBS_ERROR_ARCHIVE_FAILED'), 500);
         } else {
             exit;
         }
     } else {
         App::redirect(Route::url('index.php?option=com_jobs&task=dashboard'), Lang::txt('COM_JOBS_ERROR_ARCHIVE_FAILED'), 'error');
     }
 }
Exemple #11
0
 /**
  * Serve
  *
  * @return  boolean
  */
 public function serve($element, $elementId, $pub, $blockParams, $itemId = 0)
 {
     // Incoming
     $forceDownload = Request::getInt('download', 0);
     // Force downlaod action?
     // Get configs
     $configs = $this->getConfigs($element->params, $elementId, $pub, $blockParams);
     $attachments = $pub->_attachments;
     $attachments = isset($attachments['elements'][$elementId]) ? $attachments['elements'][$elementId] : NULL;
     // Sort out attachments for this element
     $attachments = $this->_parent->getElementAttachments($elementId, $attachments, $this->_name);
     if (!$forceDownload && $configs->handler) {
         // serve through handler
         // TBD
     } else {
         // Default action - download
         // Build download path
         $download = NULL;
         // Default serve - download
         if ($itemId) {
             foreach ($attachments as $attach) {
                 if ($attach->id == $itemId) {
                     $download = $this->getFilePath($attach->path, $attach->id, $configs, $attach->params);
                     break;
                 }
             }
         } elseif (count($attachments) > 1) {
             $overwrite = $pub->state == 1 ? false : true;
             $download = $this->bundle($attachments, $configs, $overwrite);
         } elseif (count($attachments) == 1) {
             $download = $this->getFilePath($attachments[0]->path, $attachments[0]->id, $configs, $attachments[0]->params);
         }
         // Perform download
         if ($download && is_file($download)) {
             // Initiate a new content server and serve up the file
             $server = new \Hubzero\Content\Server();
             $server->filename($download);
             $server->disposition('attachment');
             $server->acceptranges(true);
             $server->saveas(basename($download));
             if (!$server->serve()) {
                 // Should only get here on error
                 throw new Exception(Lang::txt('PLG_PROJECTS_PUBLICATIONS_ERROR_SERVE'), 404);
             } else {
                 exit;
             }
         } else {
             $this->setError(Lang::txt('PLG_PROJECTS_PUBLICATIONS_ERROR_DOWNLOAD'));
             return false;
         }
     }
     return false;
 }
 /**
  * Download source code for a tool
  *
  * @return     void
  */
 public function sourcecodeTask()
 {
     // Get tool instance
     $tool = Request::getVar('tool', 0);
     // Ensure we have a tool
     if (!$tool) {
         App::abort(404, Lang::txt('COM_RESOURCES_RESOURCE_NOT_FOUND'));
         return;
     }
     // Load the tool version
     $tv = new \Components\Tools\Tables\Version($this->database);
     $tv->loadFromInstance($tool);
     // Concat tarball name for this version
     $tarname = $tv->toolname . '-r' . $tv->revision . '.tar.gz';
     // Get contribtool params
     $tparams = Component::params('com_tools');
     $tarball_path = $tparams->get('sourcecodePath');
     if (empty($tarball_path)) {
         $tarball_path = "site/protected/source";
     }
     if ($tarball_path[0] != DS) {
         $tarball_path = rtrim(PATH_APP . DS . $tarball_path, DS);
     }
     $tarpath = $tarball_path . DS . $tv->toolname . DS;
     $opencode = $tv->codeaccess == '@OPEN' ? 1 : 0;
     // Is a tarball available?
     if (!file_exists($tarpath . $tarname)) {
         // File not found
         App::abort(404, Lang::txt('COM_RESOURCES_FILE_NOT_FOUND'));
         return;
     }
     if (!$opencode) {
         // This tool is not open source
         App::abort(403, Lang::txt('COM_RESOURCES_ALERTNOTAUTH'));
         return;
     }
     // Serve up the file
     $xserver = new \Hubzero\Content\Server();
     $xserver->filename($tarpath . $tarname);
     $xserver->disposition('attachment');
     $xserver->acceptranges(false);
     // @TODO fix byte range support
     $xserver->saveas($tarname);
     if (!$xserver->serve_attachment($tarpath . $tarname, $tarname, false)) {
         // @TODO fix byte range support
         throw new Exception(Lang::txt('COM_RESOURCES_SERVER_ERROR'), 500);
     } else {
         exit;
     }
     return;
 }
Exemple #13
0
 /**
  * Serve publication package
  *
  * @return     boolean
  */
 public function serveBundle()
 {
     if (empty($this->_pub)) {
         throw new Exception(Lang::txt('COM_PUBLICATIONS_FILE_NOT_FOUND'), 404);
         return;
     }
     $bundle = $this->_pub->path('base', true) . DS . $this->getBundleName();
     $serveas = $this->_pub->version->get('title') . ' v.' . $this->_pub->version->get('version_label') . '.zip';
     if (!is_file($bundle)) {
         throw new Exception(Lang::txt('COM_PUBLICATIONS_FILE_NOT_FOUND'), 404);
         return;
     }
     // Initiate a new content server and serve up the file
     $server = new \Hubzero\Content\Server();
     $server->filename($bundle);
     $server->disposition('download');
     $server->acceptranges(true);
     $server->saveas($serveas);
     if (!$server->serve()) {
         // Should only get here on error
         throw new Exception(Lang::txt('COM_PUBLICATIONS_SERVER_ERROR'), 404);
     } else {
         exit;
     }
     return;
 }
Exemple #14
0
 /**
  * Serve publication package
  *
  * @return     boolean
  */
 public function serveBundle()
 {
     if (empty($this->_pub)) {
         throw new Exception(Lang::txt('COM_PUBLICATIONS_FILE_NOT_FOUND'), 404);
         return;
     }
     $bundle = $this->_pub->path('base', true) . DS . $this->getBundleName();
     $doi = $this->_pub->version->get('doi');
     if ($doi != '') {
         $doi = str_replace('.', '_', $doi);
         $doi = str_replace('/', '_', $doi);
         $serveas = $doi . '.zip';
     } else {
         // Already contains a '.zip' on the end.
         $serveas = $this->getBundleName();
     }
     if (!is_file($bundle)) {
         throw new Exception(Lang::txt('COM_PUBLICATIONS_FILE_NOT_FOUND'), 404);
         return;
     }
     // Initiate a new content server and serve up the file
     $server = new \Hubzero\Content\Server();
     $server->filename($bundle);
     $server->disposition('download');
     $server->acceptranges(true);
     $server->saveas($serveas);
     if (!$server->serve()) {
         // Should only get here on error
         throw new Exception(Lang::txt('COM_PUBLICATIONS_SERVER_ERROR'), 404);
     } else {
         exit;
     }
     return;
 }