protected function _prepareData() { $this->token = JSession::getFormToken(); $this->session = JFactory::getSession(); $this->item->changelogs = JUDownloadFrontHelper::getChangeLogField($this->item->id); $this->item->versions = $this->model->getVersions($this->item->id); $this->item->related_documents = $this->model->getRelatedDocuments($this->item->id); if (count($this->item->related_documents)) { foreach ($this->item->related_documents AS $documentRelated) { $documentRelated->link = JRoute::_(JUDownloadHelperRoute::getDocumentRoute($documentRelated->id)); $documentRelated->icon = JUDownloadHelper::getDocumentIcon($documentRelated->icon); } } $this->item->files = JUDownloadFrontHelperDocument::getFilesByDocumentId($this->item->id); $this->can_download_document = JUDownloadFrontHelperPermission::canDownloadDocument($this->item->id, false); $this->display_download_rule_msg = $this->params->get('show_rule_messages', 'modal'); $additionFields = array(); if (trim($this->item->external_link)) { $additionFields[] = 'external_link'; } if ($this->item->license_id && $this->item->confirm_license) { $additionFields[] = 'license_id'; $additionFields[] = 'confirm_license'; } $this->item->fieldGallery = JUDownloadFrontHelperField::getField('gallery', $this->item); $this->item->fields = JUDownloadFrontHelperField::getFields($this->item, 2, array(), array('gallery'), $additionFields); $this->item->license = JUDownloadFrontHelper::getLicense($this->item->license_id); $user = JFactory::getUser(); $uri = JUri::getInstance(); if ($this->item->checked_out > 0 && $this->item->checked_out != $user->get('id')) { if (JUDownloadFrontHelperPermission::canCheckInDocument($this->item->id)) { $this->item->checkin_link = JRoute::_('index.php?option=com_judownload&task=forms.checkin&id=' . $this->item->id . '&' . JSession::getFormToken() . '=1' . '&return=' . base64_encode(urlencode($uri))); } } else { $this->item->edit_link = JRoute::_('index.php?option=com_judownload&task=form.edit&id=' . $this->item->id . '&Itemid=' . JUDownloadHelperRoute::findItemIdOfDocument($this->item->id)); if ($this->item->published == 1) { $this->item->editstate_link = JRoute::_('index.php?option=com_judownload&task=forms.unpublish&id=' . $this->item->id . '&return=' . base64_encode(urlencode($uri)) . '&' . JSession::getFormToken() . '=1&Itemid=' . JUDownloadHelperRoute::findItemIdOfDocument($this->item->id)); } else { $this->item->editstate_link = JRoute::_('index.php?option=com_judownload&task=forms.publish&id=' . $this->item->id . '&return=' . base64_encode(urlencode($uri)) . '&' . JSession::getFormToken() . '=1&Itemid=' . JUDownloadHelperRoute::findItemIdOfDocument($this->item->id)); } } $this->item->delete_link = JRoute::_('index.php?option=com_judownload&task=forms.delete&id=' . $this->item->id . '&return=' . base64_encode(urlencode($uri)) . '&' . JSession::getFormToken() . '=1&Itemid=' . JUDownloadHelperRoute::findItemIdOfDocument($this->item->id)); if ($this->item->is_subscriber) { $this->item->unsubscribe_link = $this->model->getUnsubscribeLink($this->item->id); } else { $this->item->subscribe_link = $this->model->getSubscribeLink($this->item->id); } $this->item->contact_link = JRoute::_(JUDownloadHelperRoute::getContactRoute($this->item->id)); $this->item->report_link = JRoute::_(JUDownloadHelperRoute::getReportDocumentRoute($this->item->id)); $this->item->print_link = JRoute::_(JUDownloadHelperRoute::getDocumentRoute($this->item->id) . '&layout=print&tmpl=component&print=1'); $this->item->comment = new stdClass(); $this->item->comment->total_comments_no_filter = $this->model->getTotalCommentsOfDocument($this->item->id); $this->root_comment = JUDownloadFrontHelperComment::getRootComment(); $langArray = JHtml::_('contentlanguage.existing'); $JAll = new JObject(); $JAll->text = JText::_('JALL'); $JAll->value = '*'; array_unshift($langArray, $JAll); $this->langArray = $langArray; $this->item->comment->items = $this->get('Items'); foreach ($this->item->comment->items AS $comment) { if (JUDownloadFrontHelperPermission::canCheckInComment($comment->id)) { $uri = JUri::getInstance(); $comment->checkout_link = 'index.php?option=com_judownload&task=modcomments.checkin&cid=' . $comment->id . '&' . JSession::getFormToken() . '=1' . '&return=' . base64_encode(urlencode($uri)); } else { $comment->checkout_link = ''; } } $this->item->comment->pagination = $this->get('Pagination'); $this->item->comment->total_comments = $this->get('Total'); $this->item->comment->parent_id = $this->root_comment->id; $this->website_field_in_comment_form = $this->params->get('website_field_in_comment_form', 0); $this->min_comment_characters = $this->params->get('min_comment_characters', 20); $this->max_comment_characters = $this->params->get('max_comment_characters', 1000); $this->allow_vote_comment = $this->params->get('allow_vote_comment', 1); $this->allow_vote_down_comment = $this->params->get('allow_vote_down_comment', 1); $this->order_comment_name_array = $this->model->getCommentOrderingOptions(); $this->order_comment_dir_array = array( 'ASC' => JText::_('COM_JUDOWNLOAD_ASC'), 'DESC' => JText::_('COM_JUDOWNLOAD_DESC') ); $this->total_stars = $this->params->get('number_rating_stars', 5); $this->filter_comment_stars = array('' => JText::_('COM_JUDOWNLOAD_ANY_STAR')); for ($i = 0; $i <= $this->total_stars; $i++) { $score = ($i == 0) ? 0 : round((($i - 1) * 10) / $this->total_stars, 5) . ',' . round(($i * 10) / $this->total_stars, 5); $this->filter_comment_stars[$score] = JText::plural('COM_JUDOWNLOAD_N_STAR', $i); } if ($this->params->get('filter_comment_language', 0)) { $this->list_lang_comment = $this->escape($this->state->get('list.lang')); } $this->list_order_comment = $this->escape($this->state->get('list.ordering')); $this->list_dir_comment = $this->escape($this->state->get('list.direction')); $this->filter_comment_star = $this->state->get('list.star_filter', ''); $this->website = $this->title = $this->email = $this->comment = $this->name = ''; $this->language = '*'; $form = $this->session->get('judownload_commentform_' . $this->item->id, null); $this->form = $form; if (!empty($form) && $form['parent_id'] == $this->root_comment->id) { $this->title = $form['title']; $this->name = $form['guest_name']; $this->email = $form['guest_email']; $this->comment = $form['comment']; $this->website = (isset($form['website'])) ? $form['website'] : ''; $this->language = $form['comment_language']; } }
public function download() { JSession::checkToken('request') or die(JText::_('JINVALID_TOKEN')); $model = $this->getModel(); $model->deleteExpiredTmpFiles(); $app = JFactory::getApplication(); $submittedCategoryId = $app->input->getInt('cat_id', 0); $documentIds = $app->input->get('doc_id', null, null); $fileIds = $app->input->get('file_id', null, null); $version = $app->input->get('version', '', 'string'); $serverTime = JFactory::getDate()->toSql(); $serverTimeStamp = strtotime($serverTime); $valuesStoreId = (array) $app->getUserState('com_judownload.download.storeid'); $params = JUDownloadHelper::getParams(); $noCountingDownloadTime = (int) $params->get('no_counting_download_time', 300); if ($noCountingDownloadTime > 0) { if (!empty($valuesStoreId)) { foreach ($valuesStoreId AS $keyStoreId => $valueStoreId) { if ($serverTimeStamp - $keyStoreId > $noCountingDownloadTime) { unset($valuesStoreId[$keyStoreId]); } } } $app->setUserState('com_judownload.download.storeid', $valuesStoreId); } $dispatcher = JDispatcher::getInstance(); JPluginHelper::importPlugin('judownload'); if (isset($fileIds)) { if (is_array($fileIds)) { $fileIdArray = $fileIds; } else { $fileIdArray = explode(',', $fileIds); } if (count($fileIdArray) > 0) { if (count($fileIdArray) > 1) { $documentId = (int) $documentIds; if (!$documentIds) { $message = JText::_('COM_JUDOWNLOAD_NO_DOCUMENT_DETECTED'); $this->setRedirect($this->getReturnPage(), $message, 'error'); return false; } $fileObjectList = $model->getAllFilesOfDocument($documentId); $validFileIdArray = array(); foreach ($fileObjectList AS $fileObject) { if (in_array($fileObject->id, $fileIdArray)) { $validFileIdArray[] = $fileObject->id; } } } else { $fileObject = $model->getFileObject($fileIdArray[0]); $documentId = $fileObject->doc_id; if (isset($documentIds)) { $documentIdPost = (int) $documentIds; if ($documentIdPost != $documentId) { $message = JText::_('COM_JUDOWNLOAD_INVALID_DATA'); $this->setRedirect($this->getReturnPage(), $message, 'error'); return false; } } $validFileIdArray = $fileIdArray; $physicalFilePath = $model->getPhysicalFilePath($validFileIdArray[0]); $physicalFilePath = JPath::clean($physicalFilePath); if (!JFile::exists($physicalFilePath)) { $message = JText::_('COM_JUDOWNLOAD_FILE_DOES_NOT_EXIST'); $this->setRedirect($this->getReturnPage(), $message, 'error'); return false; } } $canDownloadDocument = $model->canDownloadDocument($documentId); if ($canDownloadDocument) { if (count($validFileIdArray) > 0) { $externalField = new JUDownloadFieldCore_external_link(); $document = JUDownloadHelper::getDocumentById($documentId); if ($externalField->isPublished() && $document->external_link != '') { $dispatcher->trigger('onAfterDownloadDocument', array($documentId, array(), 0)); $logData = array( 'user_id' => JFactory::getUser()->id, 'event' => 'document.download', 'item_id' => $documentId, 'doc_id' => $documentId, 'value' => 0, 'reference' => 'external' ); JUDownloadFrontHelperLog::addLog($logData); JUDownloadFrontHelperMail::sendEmailByEvent('document.download', $documentId); $model->updateDocumentDownloadCounter($documentId); $this->setRedirect(JRoute::_($document->external_link, false)); return true; } if (count($validFileIdArray) > 1) { $params = JUDownloadHelper::getParams(null, (int) $documentId); if (!$params->get('allow_zip_file', 1)) { $message = JText::_('COM_JUDOWNLOAD_INVALID_DOWNLOAD_DATA'); $this->setRedirect($this->getReturnPage(), $message, 'error'); return false; } } foreach ($validFileIdArray AS $validFileId) { $canDownloadFile = $model->canDownloadFile($validFileId, false); if (!$canDownloadFile) { $fileObject = $model->getFileObject($validFileId); $message = JText::sprintf('COM_JUDOWNLOAD_YOU_CAN_NOT_DOWNLOAD_FILE_X', $fileObject->rename); $this->setRedirect($this->getReturnPage(), $message, 'error'); return false; } } if ($noCountingDownloadTime > 0) { sort($validFileIdArray); $storeID = md5('file' . serialize($validFileIdArray) . $version); if (in_array($storeID, $valuesStoreId)) { $generalCheck = true; } else { $generalCheck = $model->generalCheckDownload(); } } else { $generalCheck = $model->generalCheckDownload(); } if (!$generalCheck) { $message = $model->getError(); $this->setRedirect($this->getReturnPage(), $message, 'error'); return false; } if ($model->download('file', $validFileIdArray, $documentId, $version) === false) { $message = $model->getError(); $this->setRedirect($this->getReturnPage(), $message, 'error'); return false; } } else { $message = JText::_('COM_JUDOWNLOAD_INVALID_DOWNLOAD_DATA'); $this->setRedirect($this->getReturnPage(), $message, 'error'); return false; } } else { $message = implode("<br/>", $model->getErrors()); $params = JUDownloadHelper::getParams(null, $documentId); $display_messages = $params->get('show_rule_messages', 'modal'); if ($display_messages == "redirect") { $return = $app->input->get('return', null, 'base64'); $this->setRedirect(JRoute::_('index.php?option=com_judownload&view=downloaderror&return=' . $return, false), $message, 'error'); } else { $this->setRedirect($this->getReturnPage(), $message, 'error'); } return false; } } else { $message = JText::_('COM_JUDOWNLOAD_NO_FILE_TO_DOWNLOAD'); $this->setRedirect($this->getReturnPage(), $message, 'error'); return false; } } else { if (is_array($documentIds)) { $documentIdArray = $documentIds; } else { $documentIdArray = explode(',', $documentIds); } if (count($documentIdArray) > 0) { if (count($documentIdArray) > 1) { $categoryId = $submittedCategoryId; if (!$categoryId) { $message = JText::_('COM_JUDOWNLOAD_NO_CATEGORY_DETECTED'); $this->setRedirect($this->getReturnPage(), $message, 'error'); return false; } $params = JUDownloadHelper::getParams(null, $categoryId); if (!$params->get('allow_download_multi_docs', 0)) { $message = JText::_('COM_JUDOWNLOAD_INVALID_DOWNLOAD_DATA'); $this->setRedirect($this->getReturnPage(), $message, 'error'); return false; } $validDocumentIdArray = array(); $documentIdsInCat = $model->getChildDocumentIds($categoryId); foreach ($documentIdsInCat AS $documentIdInCat) { if (in_array($documentIdInCat, $documentIdArray)) { $validDocumentIdArray[] = $documentIdInCat; } } } else { $documentId = $documentIdArray[0]; $categoryId = JUDownloadFrontHelperCategory::getMainCategoryId($documentId); $validDocumentIdArray = $documentIdArray; $documentIdInCat = JUDownloadHelper::getDocumentById($documentId); $externalField = new JUDownloadFieldCore_external_link(); if ($externalField->isPublished() && $documentIdInCat->external_link != '') { $dispatcher->trigger('onAfterDownloadDocument', array($documentId, array(), 0)); $logData = array( 'user_id' => JFactory::getUser()->id, 'event' => 'document.download', 'item_id' => $documentId, 'doc_id' => $documentId, 'value' => 0, 'reference' => 'external' ); JUDownloadFrontHelperLog::addLog($logData); JUDownloadFrontHelperMail::sendEmailByEvent('document.download', $documentId); $model->updateDocumentDownloadCounter($documentId); $this->setRedirect(JRoute::_($documentIdInCat->external_link, false)); return true; } } if (count($validDocumentIdArray) > 1) { $params = JUDownloadHelper::getParams($categoryId); if (!$params->get('allow_zip_file', 1)) { $message = JText::_('COM_JUDOWNLOAD_INVALID_DOWNLOAD_DATA'); $this->setRedirect($this->getReturnPage(), $message, 'error'); return false; } } elseif (count($validDocumentIdArray) == 1) { $filesInDocument = JUDownloadFrontHelperDocument::getFilesByDocumentId((int) $validDocumentIdArray[0]); if (count($filesInDocument) > 1) { if (!$params->get('allow_zip_file', 1)) { $linkFiles = JUDownloadHelperRoute::getDocumentRoute((int) $validDocumentIdArray[0]); $linkFiles .= '#judl-files'; $app->redirect(JRoute::_($linkFiles, false)); } } } foreach ($validDocumentIdArray AS $documentId) { $canDownloadDocument = $model->canDownloadDocument($documentId); if (!$canDownloadDocument) { $message = implode("<br/>", $model->getErrors()); $params = JUDownloadHelper::getParams(null, $documentId); $display_messages = $params->get('show_rule_messages', 'modal'); if ($display_messages == "redirect") { $return = $app->input->get('return', null, 'base64'); $this->setRedirect(JRoute::_('index.php?option=com_judownload&view=downloaderror&return=' . $return, false), $message, 'error'); } else { $this->setRedirect($this->getReturnPage(), $message, 'error'); } return false; } } if ($noCountingDownloadTime > 0) { sort($validDocumentIdArray); $storeID = md5('document' . serialize($validDocumentIdArray) . $version); if (in_array($storeID, $valuesStoreId)) { $generalCheck = true; } else { $generalCheck = $model->generalCheckDownload(); } } else { $generalCheck = $model->generalCheckDownload(); } if (!$generalCheck) { $message = $model->getError(); $this->setRedirect($this->getReturnPage(), $message, 'error'); return false; } if (count($validDocumentIdArray) == 1) { if (count($filesInDocument) == 1) { if (!$params->get('allow_zip_file', 1)) { $fileObject = $filesInDocument[0]; $fileId = $fileObject->id; if ($model->download('file', array($fileId), $validDocumentIdArray[0], $version) === false) { $message = $model->getError(); $this->setRedirect($this->getReturnPage(), $message, 'error'); return false; } } } } if ($model->download('document', $validDocumentIdArray, $categoryId, $version) === false) { $message = $model->getError(); $this->setRedirect($this->getReturnPage(), $message, 'error'); return false; } } else { $message = JText::_('COM_JUDOWNLOAD_NO_DOCUMENT_TO_DOWNLOAD'); $this->setRedirect($this->getReturnPage(), $message, 'error'); return false; } } }