Esempio n. 1
0
 /**
  * Method to delete referenced data of an item.
  *
  * @param     mixed      $pk    An primary key value to delete.
  *
  * @return    boolean
  */
 public function deleteReferences($pk = null)
 {
     if (empty($this->id) || $this->id != $pk) {
         if (!$this->load($pk)) {
             return false;
         }
     }
     // Delete the physical file
     $path = PFrepoHelper::getFilePath($this->file_name, $this->dir_id);
     if (!empty($path)) {
         JFile::delete($path . '/' . $this->file_name);
     }
     // Delete the revisions folder
     $path = PFrepoHelper::getBasePath($this->project_id) . '/_revs/file_' . (int) $pk;
     if (JFolder::exists($path)) {
         JFolder::delete($path);
     }
     // Delete revisions
     $query = $this->_db->getQuery(true);
     $query->clear()->delete('#__pf_repo_file_revs')->where('parent_id = ' . (int) $pk);
     $this->_db->setQuery($query);
     $this->_db->execute();
 }
Esempio n. 2
0
 /**
  * Method to get item data.
  *
  * @param     integer    The id of the item.
  * @return    mixed      Menu item data object on success, false on failure.
  */
 public function &getItem($pk = null)
 {
     // Initialise variables.
     $pk = !empty($pk) ? $pk : (int) $this->getState($this->getName() . '.id');
     if ($this->_item === null) {
         $this->_item = array();
     }
     // Check cache
     if (isset($this->_item[$pk])) {
         return $this->_item[$pk];
     }
     try {
         $db = $this->getDbo();
         $query = $db->getQuery(true);
         $query->select($this->getState('item.select', 'a.id, a.asset_id, a.project_id, a.title, a.alias, a.description, ' . 'a.created, a.created_by, a.modified, a.modified_by, a.checked_out, a.checked_out_time, ' . 'a.attribs, a.access, a.file_name, a.file_extension, a.file_size, a.dir_id'));
         $query->from('#__pf_repo_files AS a');
         // Join on project table.
         $query->select('p.title AS project_title, p.alias AS project_alias');
         $query->join('LEFT', '#__pf_projects AS p on p.id = a.project_id');
         // Join on directories table.
         $query->select('d.title AS dir_title, d.alias AS dir_alias, d.path');
         $query->join('LEFT', '#__pf_repo_dirs AS d on d.id = a.dir_id');
         // Join on user table.
         $query->select('u.name AS author');
         $query->join('LEFT', '#__users AS u on u.id = a.created_by');
         $query->where('a.id = ' . (int) $pk);
         $db->setQuery($query);
         $item = $db->loadObject();
         if ($error = $db->getErrorMsg()) {
             throw new Exception($error);
         }
         if (empty($item)) {
             return JError::raiseError(404, JText::_('COM_PROJECTFORK_ERROR_FILE_NOT_FOUND'));
         }
         // Convert parameter fields to objects.
         $registry = new JRegistry();
         $registry->loadString($item->attribs);
         $params = $this->getState('params');
         if ($params) {
             $item->params = clone $this->getState('params');
             $item->params->merge($registry);
         } else {
             $item->params = $registry;
         }
         // Get the pyhsical location
         $item->physical_path = PFrepoHelper::getFilePath($item->file_name, $item->dir_id);
         // Generate slugs
         $item->slug = $item->alias ? $item->id . ':' . $item->alias : $item->id;
         $item->project_slug = $item->project_alias ? $item->project_id . ':' . $item->project_alias : $item->project_id;
         $item->dir_slug = $item->dir_alias ? $item->dir_id . ':' . $item->dir_alias : $item->dir_id;
         // Compute selected asset permissions.
         $user = JFactory::getUser();
         $uid = $user->get('id');
         $access = PFrepoHelper::getActions('file', $item->id);
         $view_access = true;
         if ($item->access && !$user->authorise('core.admin')) {
             $view_access = in_array($item->access, $user->getAuthorisedViewLevels());
         }
         $item->params->set('access-view', $view_access);
         if (!$view_access) {
             $item->params->set('access-edit', false);
             $item->params->set('access-change', false);
         } else {
             // Check general edit permission first.
             if ($access->get('core.edit')) {
                 $item->params->set('access-edit', true);
             } elseif (!empty($uid) && $access->get('core.edit.own')) {
                 // Check for a valid user and that they are the owner.
                 if ($uid == $item->created_by) {
                     $item->params->set('access-edit', true);
                 }
             }
             // Check edit state permission.
             $item->params->set('access-change', $access->get('core.edit.state'));
         }
         $this->_item[$pk] = $item;
     } catch (JException $e) {
         if ($e->getCode() == 404) {
             // Need to go thru the error handler to allow Redirect to work.
             JError::raiseError(404, $e->getMessage());
         } else {
             $this->setError($e);
             $this->_item[$pk] = false;
         }
     }
     return $this->_item[$pk];
 }
Esempio n. 3
0
 public function download()
 {
     $id = JRequest::getUInt('id');
     $rev = JRequest::getUInt('rev');
     $link_base = 'index.php?option=' . $this->option . '&view=';
     $link_list = $link_base . $this->view_list . $this->getRedirectToListAppend();
     $user = JFactory::getUser();
     $levels = $user->getAuthorisedViewLevels();
     $admin = $user->authorise('core.admin', 'com_pfrepo');
     $file_model = $this->getModel();
     $file = $file_model->getItem($id);
     if (empty($id) || !$file || empty($file->id)) {
         $this->setError(JText::_('COM_PROJECTFORK_ERROR_FILE_NOT_FOUND'));
         $this->setMessage($this->getError(), 'error');
         $this->setRedirect(JRoute::_($link_list, false));
         return false;
     }
     // Check file access
     if (!$admin && !in_array($file->access, $levels)) {
         $this->setError(JText::_('JERROR_ALERTNOAUTHOR'));
         $this->setMessage($this->getError(), 'error');
         $this->setRedirect(JRoute::_($link_list, false));
         return false;
     }
     if ($rev) {
         $rev_model = $this->getModel('FileRevision');
         $file_rev = $rev_model->getItem($rev);
         if (!$file_rev || empty($file_rev->id)) {
             $this->setError(JText::_('COM_PROJECTFORK_ERROR_FILE_NOT_FOUND'));
             $this->setMessage($this->getError(), 'error');
             $this->setRedirect(JRoute::_($link_list, false));
             return false;
         }
         // Check access
         if ($file_rev->parent_id != $file->id) {
             $this->setError(JText::_('JERROR_ALERTNOAUTHOR'));
             $this->setMessage($this->getError(), 'error');
             $this->setRedirect(JRoute::_($link_list, false));
             return false;
         }
         $filepath = PFrepoHelper::getBasePath($file->project_id) . '/_revs/file_' . $file->id;
         $filename = $file_rev->file_name;
     } else {
         $filepath = PFrepoHelper::getFilePath($file->file_name, $file->dir_id);
         $filename = $file->file_name;
     }
     // Check if the file exists
     if (empty($filepath) || !JFile::exists($filepath . '/' . $filename)) {
         $this->setError(JText::_('COM_PROJECTFORK_ERROR_FILE_NOT_FOUND'));
         $this->setMessage($this->getError(), 'error');
         $this->setRedirect(JRoute::_($link_list, false));
         return false;
     }
     if (headers_sent($f, $line)) {
         $this->setError(JText::sprintf('COM_PROJECTFORK_WARNING_FILE_DL_ERROR_HEADERS_SENT', $f, $line));
         $this->setMessage($this->getError(), 'error');
         $this->setRedirect(JRoute::_($link_list, false));
         return false;
     }
     ob_end_clean();
     header("Content-Type: APPLICATION/OCTET-STREAM");
     header("Content-Length: " . filesize($filepath . '/' . $filename));
     header("Content-Disposition: attachment; filename=\"" . $filename . "\";");
     header("Content-Transfer-Encoding: Binary");
     if (function_exists('readfile')) {
         readfile($filepath . '/' . $filename);
     } else {
         echo file_get_contents($filepath . '/' . $filename);
     }
     jexit();
 }
Esempio n. 4
0
 /**
  * Method to delete a file
  *
  * @param     string     $name    The file name
  * @param     integer    $dir     The dir id to which the file belongs to
  *
  * @return    boolean             True on success, otherwise False
  */
 public function deleteFile($name, $dir = 0)
 {
     $path = PFrepoHelper::getFilePath($name, $dir);
     if (empty($path)) {
         return false;
     }
     if (JFile::delete($path . '/' . $name) !== true) {
         return false;
     }
     return true;
 }