Beispiel #1
0
 /**
  * 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;
 }