function display($tpl = null) { $user = JFactory::getUser(); $this->item = $this->get('Item'); $this->state = $this->get('State'); // Check for errors. if (count($errors = $this->get('Errors'))) { JError::raiseError(500, implode("\n", $errors)); return false; } // Check access if ($this->item->params->get('access-view') != true) { JError::raiseWarning(403, JText::_('JERROR_ALERTNOAUTHOR')); return false; } $rev = JRequest::getUInt('rev'); if ($rev) { $rev_model = JModelLegacy::getInstance('FileRevision', 'PFrepoModel', $c = array('ignore_request' => true)); $file_rev = $rev_model->getItem($rev); if (!$file_rev || empty($file_rev->id)) { JError::raiseError(404, JText::_('COM_PROJECTFORK_ERROR_FILE_NOT_FOUND')); return false; } // Check access if ($file_rev->parent_id != $this->item->id) { JError::raiseWarning(403, JText::_('JERROR_ALERTNOAUTHOR')); return false; } $filepath = PFrepoHelper::getBasePath($this->item->project_id) . '/_revs/file_' . $this->item->id; $filename = $file_rev->file_name; } else { $filepath = $this->item->physical_path; $filename = $this->item->file_name; } // Check if the file exists if (empty($filepath) || !JFile::exists($filepath . '/' . $filename)) { JError::raiseError(404, JText::_('COM_PROJECTFORK_ERROR_FILE_NOT_FOUND')); return false; } if (headers_sent($file, $line)) { JError::raiseError(500, JText::sprintf('COM_PROJECTFORK_WARNING_FILE_DL_ERROR_HEADERS_SENT', $file, $line)); return false; } while (ob_get_level()) { 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(); }
/** * 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(); }
/** * Method to physically copy directory * * @param array $data The directory data * * @return boolean True on success */ protected function copyPhysical($project, $path, $dest) { if (!$project) { return false; } $base = PFrepoHelper::getBasePath(); $path_exists = JFolder::exists($base . '/' . $path); $dest_exists = JFolder::exists($base . '/' . $dest); // Do nothing if the path does not exist or if the destination already exists if (!$path_exists || $dest_exists) { return true; } return JFolder::copy($base . '/' . $path, $base . '/' . $dest); }
/** * Method to get the pyhsical path location of a file * * @param string $name The file name * @param integer $dir The directory id in which the file is stored * * @return string The path */ public static function getFilePath($name, $dir) { $db = JFactory::getDbo(); $query = $db->getQuery(true); $query->select('project_id, path')->from('#__pf_repo_dirs')->where('id = ' . (int) $dir); $db->setQuery($query); $dir = $db->loadObject(); if (empty($dir)) { return ''; } $base = PFrepoHelper::getBasePath(); $file = $base . '/' . $dir->path . '/' . $name; // Look in the directory if (JFile::exists($file)) { return $base . '/' . $dir->path; } // Look in the base dir (4.0 backwards compat) $file = $base . '/' . $dir->project_id . '/' . $name; if (JFile::exists($file)) { return $base . '/' . $dir->project_id; } // Look in the base dir (3.0 backwards compat) $file = $base . '/project_' . $dir->project_id . '/' . $name; if (JFile::exists($file)) { return $base . '/project_' . $dir->project_id; } return ''; }
/** * Method to delete one or more records. * * @param array An array of record primary keys. * * @return boolean True if successful, false if an error occurs. */ public function delete(&$pks) { $pks = (array) $pks; $table = $this->getTable(); $query = $this->_db->getQuery(true); $active_id = PFApplicationHelper::getActiveProjectId(); $repo_exists = PFApplicationHelper::exists('com_pfrepo'); if ($repo_exists) { $base_path = PFrepoHelper::getBasePath(); } // Include the content plugins for the on delete events. $dispatcher = JDispatcher::getInstance(); JPluginHelper::importPlugin('content'); // Iterate the items to delete each one. foreach ($pks as $i => $pk) { // Try to load from the db if ($table->load($pk) === false) { $this->setError($table->getError()); return false; } // Check delete permission if (!$this->canDelete($table)) { unset($pks[$i]); $error = $this->getError(); if ($error) { JError::raiseWarning(500, $error); } else { JError::raiseWarning(403, JText::_('JLIB_APPLICATION_ERROR_DELETE_NOT_PERMITTED')); } return false; } // Trigger the onContentBeforeDelete event. $context = $this->option . '.' . $this->name; $result = $dispatcher->trigger($this->event_before_delete, array($context, $table)); if (in_array(false, $result, true)) { $this->setError($table->getError()); return false; } if ($repo_exists) { $params = new JRegistry(); $params->loadString($table->attribs); $repo_dir = (int) $params->get('repo_dir'); $query->clear()->select('path')->from('#__pf_repo_dirs')->where('id = ' . $repo_dir); $this->_db->setQuery($query); $repo_path = $this->_db->loadResult(); } // Delete the item if (!$table->delete($pk)) { $this->setError($table->getError()); return false; } // Delete the repo directory if ($repo_exists) { if ($repo_path && $repo_dir) { // Delete repo 4.1 $repo = $base_path . '/' . $repo_path; if (JFolder::exists($repo) && $repo != $base_path) { JFolder::delete($repo); } // Delete repo 4.0 $repo = $base_path . '/' . $pk; if (JFolder::exists($repo)) { JFolder::delete($repo); } // Delete repo 3.0 $repo = $base_path . '/project_' . $pk; if (JFolder::exists($repo)) { JFolder::delete($repo); } } } // Delete the logo $this->deleteLogo($pk); // Check if the currently active project is being deleted. // If so, clear it from the session if ($active_id == $pk) { $this->setActive(array('id' => 0)); } // Trigger the onContentAfterDelete event. $dispatcher->trigger($this->event_after_delete, array($context, $table)); } // Clear the component's cache $this->cleanCache(); return true; }
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(); }
/** * Method for uploading a file * * @param array $file The file information * @param integer $dir The directory id * @param boolean $stream If set to true, use data stream * @param integer $parent_id If set, will try to move the original file to the revs folder * * @return mixed Array with file info on success, otherwise False */ public function upload($file = NULL, $dir = 0, $stream = false, $parent_id = 0) { // Dont allow upload to root dir if ((int) $dir <= 1) { $this->setError(JText::_('COM_PROJECTFORK_WARNING_SELECT_DIRECTORY')); return false; } // Check allowed file extension $allowed = PFrepoHelper::getAllowedFileExtensions(); $config = JComponentHelper::getParams('com_pfrepo'); $user = JFactory::getUser(); $filter_admin = $config->get('filter_ext_admin'); $is_admin = $user->authorise('core.admin'); if ($is_admin && !$filter_admin) { $allowed = array(); } if (count($allowed)) { $ext = strtolower(JFile::getExt($file['name'])); if (!in_array($ext, $allowed)) { $this->setError(JText::_('COM_PROJECTFORK_WARNING_INVALID_FILE_EXT')); return false; } } $query = $this->_db->getQuery(true); $query->select('project_id, path')->from('#__pf_repo_dirs')->where('id = ' . (int) $dir); $this->_db->setQuery($query); $dir = $this->_db->loadObject(); if (empty($dir)) { $this->setError(JText::_('COM_PROJECTFORK_WARNING_SELECT_DIRECTORY')); return false; } $project = $dir->project_id; $uploadpath = PFrepoHelper::getBasePath() . '/' . $dir->path; if (!is_array($file) || !isset($file['tmp_name'])) { $this->setError(JText::_('COM_PROJECTFORK_WARNING_NO_FILE_SELECTED')); return false; } // Try to create the upload path destination if (!JFolder::exists($uploadpath)) { if (!JFolder::create($uploadpath)) { return false; } } $errnum = (int) $file['error']; if ($errnum > 0) { $errmsg = PFrepoHelper::getFileErrorMsg($errnum, $file['name'], $file['size']); $this->setError($errmsg); return false; } // If we have a parent id, move it to the revisions folder first if ($parent_id) { $query->clear()->select('project_id, dir_id, file_name')->from('#__pf_repo_files')->where('id = ' . (int) $parent_id); $this->_db->setQuery($query); $head = $this->_db->loadObject(); if (empty($head)) { $this->setError(JText::_('COM_PROJECTFORK_WARNING_FILE_HEAD_NOT_FOUND')); return false; } // Prepare file paths $head_dest = PFrepoHelper::getBasePath($head->project_id) . '/_revs/file_' . (int) $parent_id; $head_path = PFrepoHelper::getFilePath($head->file_name, $head->dir_id); if (empty($head_path)) { $this->setError(JText::_('COM_PROJECTFORK_WARNING_FILE_HEAD_FILE_NOT_FOUND')); return false; } if (!JFolder::exists($head_dest)) { if (JFolder::create($head_dest) !== true) { return false; } } $head_path .= '/' . $head->file_name; $head_name = $this->generateNewFileName($head_dest, $head->file_name); $head_dest .= '/' . $head_name; // Move the file $move = JFile::move($head_path, $head_dest); if ($move !== true) { if (!is_bool($move)) { $this->setError($move); } return false; } } $name = $this->generateNewFileName($uploadpath, $file['name']); $ext = JFile::getExt($name); if ($stream) { // Check file size $flimit = PFrepoHelper::getMaxUploadSize(); $plimit = PFrepoHelper::getMaxPostSize(); $size = isset($_SERVER["CONTENT_LENGTH"]) ? (int) $_SERVER["CONTENT_LENGTH"] : 0; if ($flimit < $size) { $msg = JText::sprintf('COM_PROJECTFORK_WARNING_FILE_UPLOAD_ERROR_1', $name, $flimit); $this->setError($msg); if ($parent_id) { JFile::move($head_dest, $head_path); } return false; } elseif ($plimit < $size) { $msg = JText::sprintf('COM_PROJECTFORK_WARNING_FILE_UPLOAD_ERROR_9', $name, $plimit); $this->setError($msg); if ($parent_id) { JFile::move($head_dest, $head_path); } return false; } $fp = fopen("php://input", "r"); $temp = tmpfile(); if ($fp === false) { $this->setError(JText::_('COM_PROJECTFORK_WARNING_FILE_STREAM_ERROR_1')); if ($parent_id) { JFile::move($head_dest, $head_path); } return false; } if ($temp === false) { $this->setError(JText::_('COM_PROJECTFORK_WARNING_FILE_STREAM_ERROR_2')); if ($parent_id) { JFile::move($head_dest, $head_path); } return false; } $check = stream_copy_to_stream($fp, $temp); fclose($fp); if ($check != $size || empty($size)) { $this->setError(JText::_('COM_PROJECTFORK_WARNING_FILE_STREAM_ERROR_3')); if ($parent_id) { JFile::move($head_dest, $head_path); } return false; } $dest = fopen($uploadpath . '/' . $name, "w"); if ($dest === false) { $this->setError(JText::_('COM_PROJECTFORK_WARNING_FILE_STREAM_ERROR_4')); if ($parent_id) { JFile::move($head_dest, $head_path); } return false; } fseek($temp, 0, SEEK_SET); $check = stream_copy_to_stream($temp, $dest); fclose($dest); if ($check != $size) { $this->setError(JText::_('COM_PROJECTFORK_WARNING_FILE_STREAM_ERROR_5')); if ($parent_id) { JFile::move($head_dest, $head_path); } return false; } $file['size'] = $size; if ($parent_id) { // Rename the file name in the db if ($head_name != $head->file_name) { $query->clear()->update('#__pf_repo_files')->set('file_name = ' . $this->_db->quote($head_name))->where('id = ' . $parent_id); $this->_db->setQuery($query); $this->_db->execute(); } } return array('name' => $name, 'size' => $file['size'], 'extension' => $ext); } else { if (JFile::upload($file['tmp_name'], $uploadpath . '/' . $name) === true) { if ($parent_id) { // Rename the file name in the db if ($head_name != $head->file_name) { $query->clear()->update('#__pf_repo_files')->set('file_name = ' . $this->_db->quote($head_name))->where('id = ' . $parent_id); $this->_db->setQuery($query); $this->_db->execute(); } } return array('name' => $name, 'size' => $file['size'], 'extension' => $ext); } } if ($parent_id) { JFile::move($head_dest, $head_path); } return false; }