Exemplo n.º 1
0
 /**
  * This implements an inbound webhook from MandrillApp to reply to posts using emails
  *
  */
 public function mailReplyAction()
 {
     $response = new Response();
     if ($this->request->isPost()) {
         if (!isset($this->config->mandrillapp->secret)) {
             return $response;
         }
         if ($this->config->mandrillapp->secret != $this->request->getQuery('secret')) {
             return $response;
         }
         $events = @json_decode($this->request->getPost('mandrill_events'), true);
         if (!is_array($events)) {
             return $response;
         }
         foreach ($events as $event) {
             if (!isset($event['event'])) {
                 continue;
             }
             $type = $event['event'];
             if ($type != 'inbound') {
                 continue;
             }
             if (!isset($event['msg'])) {
                 continue;
             }
             $msg = $event['msg'];
             if (!isset($msg['dkim'])) {
                 continue;
             }
             if (!isset($msg['from_email'])) {
                 continue;
             }
             if (!isset($msg['email'])) {
                 continue;
             }
             if (!isset($msg['text'])) {
                 continue;
             }
             $content = $msg['text'];
             if (!trim($content)) {
                 continue;
             }
             $user = Users::findFirstByEmail($msg['from_email']);
             if (!$user) {
                 continue;
             }
             $email = $msg['email'];
             if (!preg_match('#^reply-i([0-9]+)-([0-9]+)@phosphorum.com$#', $email, $matches)) {
                 continue;
             }
             $post = Posts::findFirst($matches[1]);
             if (!$post) {
                 continue;
             }
             if ($post->deleted) {
                 continue;
             }
             /**
              * Process replies to remove the base message
              */
             $str = array();
             $firstNoBaseReplyLine = false;
             foreach (array_reverse(preg_split('/\\r\\n|\\n/', trim($content))) as $line) {
                 if (!$firstNoBaseReplyLine) {
                     if (substr($line, 0, 1) == '>') {
                         continue;
                     } else {
                         $firstNoBaseReplyLine = true;
                     }
                 }
                 if (preg_match('/^[0-9]{4}\\-[0-9]{2}\\-[0-9]{2} [0-9]{2}:[0-9]{2} GMT([\\-\\+][0-9]{2}:[0-9]{2})? ([^:]*):$/u', $line)) {
                     continue;
                 }
                 if (preg_match('/^On [A-Za-z]{3} [0-9]{1,2}, [0-9]{4} [0-9]{1,2}:[0-9]{2} [AP]M, ([^:]*):$/u', $line)) {
                     continue;
                 }
                 $str[] = $line;
             }
             $content = join("\r\n", array_reverse($str));
             /**
              * Check if the question can have a bounty before add the reply
              */
             $canHaveBounty = $post->canHaveBounty();
             /**
              * Only update the number of replies if the user that commented isn't the same that posted
              */
             if ($post->users_id != $user->id) {
                 $post->number_replies++;
                 $post->modified_at = time();
                 $post->user->increaseKarma(Karma::SOMEONE_REPLIED_TO_MY_POST);
                 $user->increaseKarma(Karma::REPLY_ON_SOMEONE_ELSE_POST);
                 $user->save();
             }
             $postReply = new PostsReplies();
             $postReply->post = $post;
             $postReply->users_id = $user->id;
             $postReply->content = $content;
             if ($postReply->save()) {
                 if ($post->users_id != $user->id && $canHaveBounty) {
                     $bounty = $post->getBounty();
                     $postBounty = new PostsBounties();
                     $postBounty->posts_id = $post->id;
                     $postBounty->users_id = $users->id;
                     $postBounty->posts_replies_id = $postReply->id;
                     $postBounty->points = $bounty['value'];
                     if (!$postBounty->save()) {
                         foreach ($postBounty->getMessages() as $message) {
                             $this->flash->error($message);
                         }
                     }
                 }
             }
         }
     }
     return $response;
 }
Exemplo n.º 2
0
 /**
  * Deletes the Post
  *
  * @param int $id
  * @return Response|\Phalcon\Http\ResponseInterface
  */
 public function deleteAction($id)
 {
     if (!$this->checkTokenGet()) {
         return $this->response->redirect();
     }
     $usersId = $this->session->get('identity');
     if (!$usersId) {
         $this->flashSession->error('You must be logged first');
         return $this->response->redirect();
     }
     /**
      * Find the post using get
      */
     $parameters = ["id = ?0 AND (users_id = ?1 OR 'Y' = ?2)", 'bind' => [$id, $usersId, $this->session->get('identity-moderator')]];
     if (!($post = Posts::findFirst($parameters))) {
         $this->flashSession->error('The discussion does not exist');
         return $this->response->redirect();
     }
     if ($post->deleted == 'Y') {
         $this->flashSession->error("The post is already deleted");
         return $this->response->redirect();
     }
     if ($post->sticked == 'Y') {
         $this->flashSession->error("The discussion cannot be deleted because it's sticked");
         return $this->response->redirect();
     }
     $post->deleted = 1;
     if ($post->save()) {
         $usersId = $this->session->get('identity');
         if ($post->users_id != $usersId) {
             /** @var Users $user */
             if ($user = Users::findFirstById($usersId)) {
                 if ($user->moderator == 'Y') {
                     $user->increaseKarma(Karma::MODERATE_DELETE_POST);
                     $user->save();
                 }
             }
             $user = $post->user;
             $user->decreaseKarma(Karma::DELETE_POST);
             $user->save();
         }
         $this->flashSession->success('Discussion was successfully deleted');
         return $this->response->redirect();
     }
     $this->flashSession->error(join('<br>', $post->getMessages()));
     return $this->response->redirect();
 }
Exemplo n.º 3
0
 /**
  * This shows the create post form and also store the related post
  */
 public function deleteAction($id)
 {
     $usersId = $this->session->get('identity');
     if (!$usersId) {
         $this->flashSession->error('You must be logged first');
         return $this->response->redirect();
     }
     /**
      * Find the post using get
      */
     $parameters = array("id = ?0 AND (users_id = ?1 OR 'Y' = ?2)", "bind" => array($id, $usersId, $this->session->get('identity-moderator')));
     $post = Posts::findFirst($parameters);
     if (!$post) {
         $this->flashSession->error('The discussion does not exist');
         return $this->response->redirect();
     }
     if ($post->sticked == 'Y') {
         $this->flashSession->error('The discussion cannot be deleted because it\'s sticked');
         return $this->response->redirect();
     }
     $post->deleted = 1;
     if ($post->save()) {
         $usersId = $this->session->get('identity');
         if ($post->users_id != $usersId) {
             /** @var Users $user */
             $user = Users::findFirstById($usersId);
             if ($user) {
                 if ($user->moderator == 'Y') {
                     $user->increaseKarma(Karma::MODERATE_DELETE_POST);
                     $user->save();
                 }
             }
             $user = $post->user;
             $user->decreaseKarma(Karma::DELETE_POST);
             $user->save();
         }
         $this->flashSession->success('Discussion was successfully deleted');
         return $this->response->redirect();
     }
 }