Example #1
0
 protected static function findItem($needles = null)
 {
     $app = JFactory::getApplication();
     $menus = $app->getMenu('site');
     // Prepare the reverse lookup array.
     // Collect all menu items and creat an array that contains
     // the ID from the query string of the menu item as a key,
     // and the menu item id (Itemid) as a value
     // Example:
     // array( "category" =>
     //     1(id) => 100 (Itemid),
     //     2(id) => 101 (Itemid)
     // );
     if (self::$lookup === null) {
         self::$lookup = array();
         $component = JComponentHelper::getComponent(self::$component);
         $items = $menus->getItems('component_id', $component->id);
         if ($items) {
             foreach ($items as $item) {
                 if (isset($item->query) && isset($item->query['view'])) {
                     $view = $item->query['view'];
                     if (!isset(self::$lookup[$view])) {
                         self::$lookup[$view] = array();
                     }
                     if (isset($item->query['id'])) {
                         self::$lookup[$view][$item->query['id']] = $item->id;
                     } else {
                         // If it is a root element that have no a request parameter ID ( categories, authors ), we set 0 for an key
                         self::$lookup[$view][0] = $item->id;
                     }
                 }
             }
         }
     }
     if ($needles) {
         foreach ($needles as $view => $ids) {
             if (isset(self::$lookup[$view])) {
                 foreach ($ids as $id) {
                     if (isset(self::$lookup[$view][(int) $id])) {
                         return self::$lookup[$view][(int) $id];
                     }
                 }
             }
         }
     } else {
         $active = $menus->getActive();
         if ($active) {
             return $active->id;
         }
     }
     return null;
 }
Example #2
0
 public function download()
 {
     // Check for request forgeries.
     JSession::checkToken("post") or jexit(JText::_('JINVALID_TOKEN'));
     $user = JFactory::getUser();
     $data = $this->input->post->get("jform", array(), "array");
     $fileId = JArrayHelper::getValue($data, "file_id", 0, "int");
     $userId = $user->get("id");
     // Validate the user.
     if (!$userId) {
         $this->setRedirect(JRoute::_('index.php?option=com_users&view=login', false), JText::_('COM_IDENTITYPROOF_ERROR_NOT_LOG_IN'));
         return;
     }
     // Validate the item owner.
     jimport("identityproof.validator.file.owner");
     $validator = new IdentityProofValidatorFileOwner(JFactory::getDbo(), $fileId, $userId);
     if (!$validator->isValid()) {
         $this->setRedirect(JRoute::_(IdentityProofHelperRoute::getProofRoute(), false), JText::_('COM_IDENTITYPROOF_ERROR_INVALID_ITEM'));
         return;
     }
     // Validate the password.
     $password = JArrayHelper::getValue($data, "password", null, "string");
     $match = JUserHelper::verifyPassword($password, $user->get("password"), $userId);
     if (!$match) {
         $this->setRedirect(JRoute::_(IdentityProofHelperRoute::getProofRoute(), false), JText::_('COM_IDENTITYPROOF_ERROR_INVALID_ITEM'));
         return;
     }
     $params = JComponentHelper::getParams("com_identityproof");
     /** @var  $params Joomla\Registry\Registry */
     try {
         // Load file data.
         jimport("identityproof.file");
         $file = new IdentityProofFile(JFactory::getDbo());
         $keys = array("id" => $fileId, "user_id" => $userId);
         $file->load($keys);
         // Prepare keys.
         $keys = array("private" => $file->getPrivate(), "public" => $file->getPublic());
         // Prepare meta data
         $fileSize = $file->getMetaData("filesize");
         $mimeType = $file->getMetaData("mime_type");
         // Decrypt the file.
         $filePath = JPath::clean($params->get("files_path") . DIRECTORY_SEPARATOR . $file->getFilename());
         $output = file_get_contents($filePath);
         $output = IdentityProofHelper::decrypt($keys, $output);
     } catch (Exception $e) {
         JLog::add($e->getMessage());
         throw new Exception(JText::_('COM_IDENTITYPROOF_ERROR_SYSTEM'));
     }
     $app = JFactory::getApplication();
     $app->setHeader('Content-Type', $mimeType, true);
     $app->setHeader('Cache-Control', 'must-revalidate, post-check=0, pre-check=0', true);
     $app->setHeader('Content-Transfer-Encoding', 'binary', true);
     $app->setHeader('Pragma', 'no-cache', true);
     $app->setHeader('Expires', '0', true);
     $app->setHeader('Content-Disposition', 'attachment; filename=' . $file->getFilename(), true);
     $app->setHeader('Content-Length', $fileSize, true);
     $doc = JFactory::getDocument();
     $doc->setMimeEncoding($mimeType);
     $app->sendHeaders();
     echo $output;
     $app->close();
 }