Ejemplo n.º 1
0
	public function getItems()
	{
		$user       = JFactory::getUser();
		$token      = JSession::getFormToken();
		$items      = parent::getItems();
		$documentId = (int) $this->getState('document.id');
		$params     = $this->getState('params');
		if (count($items) > 0)
		{
			$commentsRecursive = array();
			foreach ($items AS $item)
			{
				$commentsRecursive[] = $item;
				$commentsRecursive   = array_merge($commentsRecursive, $this->getCommentRecursive($item->id));
			}

			$items = $commentsRecursive;
		}

		foreach ($items AS $item)
		{
			$item->comment_edit = $item->comment;
			$item->comment      = JUDownloadFrontHelper::BBCode2Html($item->comment);
			$item->comment      = JUDownloadFrontHelperComment::parseCommentText($item->comment, $documentId);

			
			$item->can_reply     = JUDownloadFrontHelperPermission::canReplyComment($documentId, $item->id);
			$item->can_vote      = JUDownloadFrontHelperPermission::canVoteComment($documentId, $item->id);
			$item->can_report    = JUDownloadFrontHelperPermission::canReportComment($documentId, $item->id);
			$item->can_subscribe = false;
			$item->can_edit      = false;
			$item->can_delete    = false;
			$isOwnerComment      = JUDownloadFrontHelperPermission::isCommentOwner($item->id);
			if ($isOwnerComment)
			{
				$item->can_edit    = JUDownloadFrontHelperPermission::canEditComment($item->id);
				$item->can_delete  = JUDownloadFrontHelperPermission::canDeleteComment($item->id);
				$item->link_delete = JRoute::_('index.php?option=com_judownload&task=document.deleteComment&comment_id=' . $item->id . '&' . $token . '=1');

				if ($params->get('can_subscribe_own_comment', 1))
				{
					$item->can_subscribe = true;
					if ($this->isSubscriber($user->id, $item->id, 'comment'))
					{
						$item->is_subscriber = true;
						$secret              = JFactory::getConfig()->get('secret');
						$type                = 'comment';
						$code                = md5($user->id . $user->email . $type . $secret);

						$subscriptionObject = JUDownloadFrontHelper::getSubscriptionObjectByType($user->id, $item->id, $type);

						$item->subscribe_link = JRoute::_('index.php?option=com_judownload&task=subscribe.remove&sub_id=' . (int) $subscriptionObject->id .
							'&code=' . $code . '&' . $token . '=1');

					}
					else
					{
						$item->is_subscriber  = false;
						$item->subscribe_link = JRoute::_('index.php?option=com_judownload&task=subscribe.save' .
							'&comment_id=' . $item->id . '&' . $token . '=1');
					}
				}
			}

			$item->voted_value = $this->getCommentVotedValue($item->id);
		}

		return $items;
	}
Ejemplo n.º 2
0
	public function addComment()
	{
		
		JSession::checkToken() or die(JText::_('JINVALID_TOKEN'));

		
		$user  = JFactory::getUser();
		$model = $this->getModel();

		
		$rootComment = JUDownloadFrontHelperComment::getRootComment();

		
		$data = $_POST;

		
		$documentId = $data['doc_id'];
		$params     = JUDownloadHelper::getParams(null, $documentId);
		$parentId   = $data['parent_id'];

		
		$model->setSessionCommentForm($documentId);

		
		if (strlen($data['title']) < 6)
		{
			$this->setError(JText::_('COM_JUDOWNLOAD_COMMENT_INVALID_TITLE'));
			$this->setRedirect(JRoute::_('index.php?option=' . $this->option . '&view=' . $this->view_item . '&id=' . $documentId, false));

			return false;
		}

		
		if (strlen($data['guest_name']) < 1)
		{
			$this->setError(JText::_('COM_JUDOWNLOAD_COMMENT_INVALID_NAME'));
			$this->setRedirect(JRoute::_('index.php?option=' . $this->option . '&view=' . $this->view_item . '&id=' . $documentId, false));

			return false;
		}

		
		if (isset($data['guest_email']))
		{
			if (!preg_match('/^[\w\.-]+@[\w\.-]+\.[\w\.-]{2,6}$/', $data['guest_email']))
			{
				$this->setError(JText::_('COM_JUDOWNLOAD_COMMENT_INVALID_EMAIL'));
				$this->setRedirect(JRoute::_('index.php?option=' . $this->option . '&view=' . $this->view_item . '&id=' . $documentId, false));

				return false;
			}
		}

		
		if (isset($data['website']))
		{
			if (!preg_match('/^(https?:\/\/)?([\w\.-]+)\.([\w\.-]{2,6})([\/\w \.-]*)*\/?$/i', $data['website']))
			{
				$this->setError(JText::_('COM_JUDOWNLOAD_COMMENT_INVALID_WEBSITE'));
				$this->setRedirect(JRoute::_('index.php?option=' . $this->option . '&view=' . $this->view_item . '&id=' . $documentId, false));

				return false;
			}
		}

		
		if (isset($data['comment_language']))
		{
			$langArray = JHtml::_('contentlanguage.existing');
			$langKey   = array_keys($langArray);
			array_unshift($langKey, '*');
			if (!in_array($data['comment_language'], $langKey))
			{
				$this->setError(JText::_('COM_JUDOWNLOAD_COMMENT_INVALID_LANGUAGE'));
				$this->setRedirect(JRoute::_('index.php?option=' . $this->option . '&view=' . $this->view_item . '&id=' . $documentId, false));

				return false;
			}
		}

		
		$minCharacter     = $params->get('min_comment_characters', 20);
		$maxCharacter     = $params->get('max_comment_characters', 1000);
		$comment          = $data['comment'];
		$comment          = JUDownloadFrontHelperComment::parseCommentText($comment, $documentId);
		$comment          = strip_tags($comment);
		$commentCharacter = strlen($comment);
		if ($commentCharacter < $minCharacter || $commentCharacter > $maxCharacter)
		{
			$this->setError(JText::_('COM_JUDOWNLOAD_COMMENT_INVALID_COMMENT'));
			$this->setRedirect(JRoute::_('index.php?option=' . $this->option . '&view=' . $this->view_item . '&id=' . $documentId, false));

			return false;
		}

		
		$showCaptcha = JUDownloadFrontHelperPermission::showCaptchaWhenComment($documentId);

		if ($showCaptcha)
		{
			$validCaptcha = JUDownloadFrontHelperCaptcha::checkCaptcha();
			
			if (!$validCaptcha)
			{
				if ($parentId == $rootComment->id)
				{
					$form = '#judl-comment-form';
				}
				else
				{
					$form = '#comment-reply-wrapper-' . $parentId;
				}

				$this->setError(JText::_('COM_JUDOWNLOAD_INVALID_CAPTCHA'));
				$this->setMessage($this->getError(), 'error');
				$this->setRedirect(JRoute::_('index.php?option=' . $this->option . '&view=' . $this->view_item . '&id=' . $documentId . $form, false));

				return false;
			}
		}

		
		if ($user->get('guest'))
		{
			if (!$model->checkNameOfGuest($documentId))
			{
				$this->setError(JText::_('COM_JUDOWNLOAD_YOU_ARE_NOT_AUTHORIZED_TO_COMMENT_ON_THIS_DOCUMENT'));
				$this->setMessage($model->getError(), 'error');
				$this->setRedirect(JRoute::_('index.php?option=' . $this->option . '&view=' . $this->view_item . '&id=' . $documentId, false));

				return false;
			}

			if (!$model->checkEmailOfGuest())
			{
				$this->setMessage($model->getError(), 'error');
				$this->setRedirect(JRoute::_('index.php?option=' . $this->option . '&view=' . $this->view_item . '&id=' . $documentId, false));

				return false;
			}
		}

		if ($parentId == $rootComment->id)
		{
			
			$canComment = JUDownloadFrontHelperPermission::canComment($documentId, $data['guest_email']);
			if (!$canComment)
			{
				$this->setError(JText::_('COM_JUDOWNLOAD_YOU_ARE_NOT_AUTHORIZED_TO_COMMENT_ON_THIS_DOCUMENT'));
				$this->setMessage($this->getError(), 'error');
				$this->setRedirect(JRoute::_('index.php?option=' . $this->option . '&view=' . $this->view_item . '&id=' . $documentId, false));

				return false;
			}
		}
		elseif ($parentId > 0 && $parentId != $rootComment->id)
		{
			
			$canReplyComment = JUDownloadFrontHelperPermission::canReplyComment($documentId, $parentId);
			if (!$canReplyComment)
			{
				$this->setError(JText::_('COM_JUDOWNLOAD_YOU_ARE_NOT_AUTHORIZED_TO_REPLY_THIS_COMMENT'));
				$this->setMessage($this->getError(), 'error');
				$this->setRedirect(JRoute::_('index.php?option=' . $this->option . '&view=' . $this->view_item . '&id=' . $documentId, false));

				return false;
			}
		}
		else
		{
			$this->setError(JText::_('COM_JUDOWNLOAD_INVALID_DATA'));
			$this->setMessage($this->getError(), 'error');
			$this->setRedirect(JRoute::_('index.php?option=' . $this->option . '&view=' . $this->view_item . '&id=' . $documentId, false));

			return false;
		}

		
		$dataValid = array();
		if ($parentId == $rootComment->id)
		{
			$canRateDocument = JUDownloadFrontHelperPermission::canRateDocument($documentId);
			if ($canRateDocument)
			{
				$dataValid = $this->validateCriteria($data, $parentId);
				if (!$dataValid)
				{
					$this->setError(JText::_('COM_JUDOWNLOAD_INVALID_RATING_VALUE'));
					$this->setMessage($this->getError(), 'error');
					$this->setRedirect(JRoute::_('index.php?option=' . $this->option . '&view=' . $this->view_item . '&id=' . $documentId, false));

					return false;
				}
			}
		}

		$requiredPostNames = array('title', 'guest_name', 'guest_email', 'comment', 'parent_id', 'doc_id');

		if ($params->get('website_field_in_comment_form', 0) == 2)
		{
			array_push($requiredPostNames, 'website');
		}

		if ($parentId == $rootComment->id && $params->get('filter_comment_language', 0))
		{
			array_push($requiredPostNames, 'comment_language');
		}

		foreach ($requiredPostNames AS $requiredPostName)
		{
			if (trim($data[$requiredPostName]) == '')
			{
				$this->setError(JText::_('COM_JUDOWNLOAD_INVALID_INPUT_DATA'));
				$this->setMessage($this->getError(), 'error');
				$this->setRedirect(JRoute::_('index.php?option=' . $this->option . '&view=' . $this->view_item . '&id=' . $documentId, false));

				return false;
			}
		}

		$acceptedPostNames = array('title', 'guest_name', 'guest_email', 'language', 'website', 'comment', 'parent_id', 'doc_id', 'subscribe');
		if ($params->get('website_field_in_comment_form', 0) == 2 || $params->get('website_field_in_comment_form', 0) == 1)
		{
			array_push($acceptedPostNames, 'website');
		}

		if ($params->get('filter_comment_language', 0))
		{
			array_push($acceptedPostNames, 'comment_language');
		}

		foreach ($acceptedPostNames AS $acceptedPostName)
		{
			if (isset($data[$acceptedPostName]))
			{
				$dataValid[$acceptedPostName] = $data[$acceptedPostName];
			}
		}

		$newCommentId = $model->saveComment($dataValid);
		if (!$newCommentId)
		{
			$this->setError($model->getError());
			$this->setMessage($this->getError(), 'error');
			$redirectUrl = JRoute::_(JUDownloadHelperRoute::getDocumentRoute($documentId), false);
			$this->setRedirect($redirectUrl);

			return false;
		}

		
		$session                      = JFactory::getSession();
		$timeNow                      = JFactory::getDate()->toSql();
		$timeNowStamp                 = strtotime($timeNow);
		$sessionCommentOnDocumentTime = 'judl-commented-' . $documentId;
		$sessionCommentTime           = 'judl-commented';
		$session->set($sessionCommentOnDocumentTime, $timeNowStamp);
		$session->set($sessionCommentTime, $timeNowStamp);
		
		$session->clear('judownload_commentform_' . $documentId);

		
		$this->setMessage(JText::_('COM_JUDOWNLOAD_ADD_COMMENT_SUCCESSFULLY'));
		$redirectUrl = JRoute::_(JUDownloadHelperRoute::getDocumentRoute($documentId) . '#comment-item-' . $newCommentId, false);
		$this->setRedirect($redirectUrl);

		return true;
	}