Example #1
0
 /**
  * This method is invoked when a user submits a comment via ajax.
  *
  * @access	public
  * @params	Array	$post 	An array of posted data.
  * @return	null
  */
 public function commentSave($post)
 {
     $ajax = new Ejax();
     $app = JFactory::getApplication();
     $my = JFactory::getUser();
     $config = EasyBlogHelper::getConfig();
     $acl = EasyBlogACLHelper::getRuleSet();
     if (empty($acl->rules->allow_comment) && (empty($my->id) && !$config->get('main_allowguestcomment'))) {
         $ajax->script('eblog.spinner.hide()');
         $ajax->script("eblog.loader.doneLoading();");
         $ajax->script('eblog.comment.displayInlineMsg( "error" , "' . JText::_('COM_EASYBLOG_NO_PERMISSION_TO_POST_COMMENT') . '");');
         $ajax->send();
     }
     $isModerated = false;
     $parentId = $post['parent_id'];
     $commentDepth = $post['comment_depth'];
     $blogId = $post['id'];
     $subscribeBlog = isset($post['subscribe-to-blog']) ? true : false;
     // @task: Cleanup posted values.
     array_walk($post, array($this, '_trim'));
     array_walk($post, array($this, '_revertValue'));
     if (!$config->get('comment_require_email') && !isset($post['esemail'])) {
         $post['esemail'] = '';
     }
     // @task: Run some validation tests on the posted values.
     if (!$this->_validateFields($post)) {
         // @task: Reload captcha if necessary
         EasyBlogHelper::getHelper('Captcha')->reload($ajax, $post);
         $ajax->script("eblog.loader.doneLoading();");
         $ajax->script('eblog.spinner.hide()');
         $ajax->script('$("#' . $this->err[1] . '").addClass("input-error");');
         $ajax->script("eblog.element.focus('" . $this->err[1] . "');");
         $ajax->script('eblog.comment.displayInlineMsg(\'error\', \'' . $this->err[0] . '\');');
         $ajax->send();
         return;
     }
     // @task: Akismet detection service.
     if ($config->get('comment_akismet')) {
         $data = array('author' => $post['esname'], 'email' => $post['esname'], 'website' => JURI::root(), 'body' => $post['comment'], 'permalink' => EasyBlogRouter::_('index.php?option=com_easyblog&view=entry&id=' . $post['id']));
         if (EasyBlogHelper::getHelper('Akismet')->isSpam($data)) {
             $ajax->script('eblog.comment.displayInlineMsg(\'error\', \'' . JText::_('COM_EASYBLOG_SPAM_DETECTED_IN_COMMENT') . '\');');
             $ajax->script("eblog.loader.doneLoading();");
             $ajax->script('eblog.spinner.hide();');
             $ajax->send();
             return false;
         }
     }
     // @task: Retrieve the comments model
     $model = $this->getModel('Comment');
     // @task: Retrieve the comment's table
     $comment = EasyBlogHelper::getTable('Comment');
     // We need to rename the esname and esemail back to name and email.
     $post['name'] = $post['esname'];
     $post['email'] = $post['esemail'];
     unset($post['esname']);
     unset($post['esemail']);
     // @task: Bind posted values into the table.
     $comment->bindPost($post);
     if (!EasyBlogHelper::getHelper('Captcha')->verify($post)) {
         return EasyBlogHelper::getHelper('Captcha')->getError($ajax, $post);
     }
     // @task: Process registrations
     $registerUser = isset($post['esregister']) ? true : false;
     $fullname = isset($post['name']) ? $post['name'] : '';
     $username = isset($post['esusername']) ? $post['esusername'] : '';
     $email = $post['email'];
     $message = '';
     $newUserId = 0;
     // @task: Process registrations if necessary
     if ($registerUser && $my->id <= 0) {
         $state = $this->processRegistrations($post, $username, $email, $ajax);
         if (!is_numeric($state)) {
             $ajax->script("eblog.loader.doneLoading();");
             $ajax->script('eblog.comment.displayInlineMsg( "error" , "' . $state . '");');
             EasyBlogHelper::getHelper('Captcha')->reload($ajax, $post);
             return $ajax->send();
         }
         $newUserId = $state;
     }
     $totalComments = empty($post['totalComment']) ? 1 : $post['totalComment'];
     $date = EasyBlogHelper::getDate();
     $comment->set('created', $date->toMySQL());
     $comment->set('modified', $date->toMySQL());
     $comment->set('published', 1);
     $comment->set('parent_id', $parentId);
     $comment->set('sent', 0);
     $comment->set('created_by', $my->id);
     // @rule: Update the user's id if they have just registered earlier.
     if ($newUserId != 0) {
         $comment->set('created_by', $newUserId);
     }
     // @rule: Update publish status if the comment requires moderation
     if ($config->get('comment_moderatecomment') == 1 || $my->id == 0 && $config->get('comment_moderateguestcomment') == 1) {
         $comment->set('published', EBLOG_COMMENT_STATUS_MODERATED);
         $isModerated = true;
     }
     $blog = EasyBlogHelper::getTable('Blog');
     $blog->load($blogId);
     // If moderation for author is disabled, ensure that the comment is published.
     // If the author is the owner of the blog, it should never be moderated.
     if (!$config->get('comment_moderateauthorcomment') && $blog->created_by == $my->id) {
         $comment->set('published', 1);
         $isModerated = false;
     }
     if (!$comment->store()) {
         //$ejax->alert( JText::_('COM_EASYBLOG_COMMENT_FAILED_TO_SAVE'), JText::_('COM_EASYBLOG_ERROR') , '450', 'auto');
         $ajax->script('eblog.comment.displayInlineMsg(\'error\', \'' . JText::_('COM_EASYBLOG_COMMENT_FAILED_TO_SAVE') . '\');');
         return $ajax->send();
     }
     // @task: Clean up the comment form
     $ajax->script('$(\'#title\').val(\'\');');
     $ajax->script('$(\'#url\').val(\'\');');
     $ajax->script('$(\'#comment\').val(\'\');');
     $ajax->script('$(\'#esusername\').val(\'\');');
     $ajax->script('$(\'#esregister\').attr(\'checked\',false);');
     $message = JText::_('COM_EASYBLOG_COMMENTS_SUCCESS');
     if ($newUserId != 0 && $registerUser) {
         $message = JText::_('COM_EASYBLOG_COMMENTS_SUCCESS_AND_REGISTERED');
     }
     // @rule: Process subscription for blog automatically when the user submits a new comment and wants to subscribe to the blog.
     if ($subscribeBlog && $config->get('main_subscription') && $blog->subscription) {
         $isSubscribed = false;
         $userId = $my->id;
         $blogModel = EasyblogHelper::getModel('Blog');
         if ($userId == 0) {
             $sid = $blogModel->isBlogSubscribedEmail($blog->id, $email);
             if (empty($sid)) {
                 $isSubscribed = $blogModel->addBlogSubscription($blog->id, $email, '', $fullname);
             }
         } else {
             $sid = $blogModel->isBlogSubscribedUser($blog->id, $userId, $email);
             if (!empty($sid)) {
                 // @task: User found, update the email address
                 $blogModel->updateBlogSubscriptionEmail($sid, $userId, $email);
             } else {
                 $isSubscribed = $blogModel->addBlogSubscription($blog->id, $email, $userId, $fullname);
             }
         }
         if ($isSubscribed) {
             $message .= ' ' . JText::_('COM_EASYBLOG_ENTRY_AUTO_SUBSCRIBED_SUCCESS');
             $sid = $blogModel->isBlogSubscribedUser($blog->id, $userId, $email);
             $html = '';
             $html .= '<div id="unsubscription-box" class="unsubscription-box">';
             $html .= '	' . JText::_('COM_EASYBLOG_ENTRY_AUTO_SUBSCRIBE_SUBSCRIBED_NOTE');
             $html .= '	<a href="javascript:void(0);" title="" onclick="eblog.blog.unsubscribe( \'' . $sid . '\', \'' . $blog->id . '\' );">';
             $html .= '		' . JText::_('COM_EASYBLOG_UNSUBSCRIBE_BLOG');
             $html .= '	</a>';
             $html .= '</div>';
             $ajax->append('subscription-box', $html);
             $ajax->script('$(\'#subscription-message\').remove();');
         }
     }
     $row = $comment;
     $creator = EasyBlogHelper::getTable('Profile');
     $creator->load($my->id);
     $row->poster = $creator;
     $row->comment = nl2br($row->comment);
     $row->comment = EasyBlogCommentHelper::parseBBCode($row->comment);
     $row->depth = is_null($commentDepth) ? '0' : $commentDepth;
     $row->likesAuthor = '';
     // @rule: Process notifications
     $comment->processEmails($isModerated, $blog);
     if ($isModerated) {
         $tpl = new CodeThemes();
         $tpl->set('comment', $row);
         $tpl->set('totalComment', $totalComments);
         $tpl->set('config', $config);
         $tpl->set('my', $my);
         $commentHtml = $tpl->fetch('blog.comment.moderate.php');
         if ($parentId != 0) {
             $ajax->after('comment-' . $parentId, $commentHtml);
             $ajax->script('eblog.comment.cancel(\'' . $parentId . '\')');
         } else {
             $ajax->append('blog-comment', $commentHtml);
         }
         // Reload recaptcha image once the comment is saved.
         EasyBlogHelper::getHelper('Captcha')->reload($ajax, $post);
         $ajax->script("eblog.loader.doneLoading();");
         $ajax->script('eblog.comment.displayInlineMsg(\'info\', \'' . $message . '\');');
         $ajax->send();
         return;
     }
     $tpl = new CodeThemes();
     $tpl->set('comment', $row);
     $tpl->set('totalComment', $totalComments);
     $tpl->set('config', $config);
     $tpl->set('my', $my);
     $tpl->set('acl', $acl);
     $commentHtml = $tpl->fetch('blog.comment.ejax.php');
     if ($parentId != 0) {
         $ajax->after('comment-' . $parentId, $commentHtml);
         $ajax->script('eblog.comment.cancel(\'' . $parentId . '\')');
     } else {
         $ajax->append('blog-comment', $commentHtml);
     }
     //update the sent flag to sent
     $comment->updateSent();
     // Reload whichever captcha necessary for the next run
     EasyBlogHelper::getHelper('Captcha')->reload($ajax, $post);
     $ajax->script("eblog.loader.doneLoading();");
     // update comment total count text on blog post
     if ($comment->published == 1) {
         $commentText = $tpl->getNouns('COM_EASYBLOG_COMMENT_COUNT', $totalComments, true);
         $ajax->script('$(\'.blog-comments a\').text(\'' . $commentText . '\');');
     }
     //update the comment total count
     $ajax->script('$(\'#comment-total-count\').text(\'' . $totalComments . '\');');
     //the next count.
     $ajax->script('$(\'#totalComment\').val(\'' . ($totalComments + 1) . '\');');
     //$ejax->alert( $message, JText::_('COM_EASYBLOG_INFO') , '450', 'auto');
     $ajax->script('eblog.comment.displayInlineMsg(\'info\', \'' . $message . '\');');
     $ajax->send();
 }