public function changepasswordAction()
 {
     $form = new ChangePasswordForm();
     $object = Users::findFirstById($this->auth->getAuth()['id']);
     $this->view->form = $form;
     if ($this->request->isPost()) {
         if (!$form->isValid($this->request->getPost())) {
             foreach ($form->getMessages() as $message) {
                 $this->flashSession->error($message->getMessage());
             }
         } else {
             if ($object && $object->getStatus() == $object::STATUS_ACTIVE) {
                 if (!$this->security->checkHash($this->request->getPost('passwd'), $object->getPasswd())) {
                     $this->flashSession->error(t('Oops! Wrong password.'));
                 } else {
                     $newPass = $this->security->hash($this->request->getPost('passwd_new_confirm'));
                     $object->setPasswd($newPass);
                     if (!$object->save()) {
                         $this->displayModelErrors($object);
                     } else {
                         $this->flashSession->success(t('Hooray! Your password was successfully changed.'));
                         return $this->response->redirect($this->router->getControllerName() . '/changepassword');
                     }
                 }
             } elseif ($object && $object->getStatus() != Users::STATUS_ACTIVE) {
                 $this->flashSession->error(t('User status is: ') . $object->getStatusesWithLabels()[$object->getStatus()] . '. You can\'t change your password.');
             } else {
                 $this->flashSession->error(t('User doesn\'t exist !'));
             }
         }
     }
     return true;
 }
Beispiel #2
0
 /**
  * Check condition to allow comment or vote
  *
  * @return mixed
  */
 public function getVote()
 {
     $identity = $this->session->get('auth');
     if (isset($identity['id'])) {
         $user = Users::findFirstById($identity['id']);
         if (!$user) {
             error_log('The user does not exist' . __CLASS__ . ' and ' . __LINE__);
             return false;
         }
         return $user->getVote();
     }
     return false;
 }
Beispiel #3
0
 /**
  * Displays a post and its comments
  *
  * @param $id
  * @param $slug
  *
  * @return \Phalcon\Http\ResponseInterface
  */
 public function viewAction($id, $slug)
 {
     $id = (int) $id;
     $userId = $this->auth->getAuth()['id'];
     if (!($object = Posts::findFirstById($id))) {
         $this->flashSession->error(t('Posts doesn\'t exist.'));
         return $this->indexRedirect();
     }
     if ($object->getDeleted()) {
         $this->flashSession->error('The Post is deleted');
         return $this->indexRedirect();
     }
     $ipAddress = $this->request->getClientAddress();
     $parameters = ['postsId = ?0 AND ipaddress = ?1', 'bind' => [$id, $ipAddress]];
     $viewed = PostsViews::count($parameters);
     //A view is stored by ipaddress
     if (!$viewed) {
         //Increase the number of views in the post
         $object->setNumberViews($object->getNumberViews() + 1);
         if ($object->getUsersId() != $userId) {
             $object->user->increaseKarma(Karma::VISIT_ON_MY_POST);
             if ($userId > 0) {
                 $user = Users::findFirstById($userId);
                 if ($user) {
                     if ($user->getModerator() == 'Y') {
                         $user->increaseKarma(Karma::MODERATE_VISIT_POST);
                     } else {
                         $user->increaseKarma(Karma::VISIT_POST);
                     }
                     //send log to server
                     if (!$user->save()) {
                         $this->saveLoger($user->getMessages());
                     }
                 }
             }
         }
         if (!$object->save()) {
             $this->saveLoger($object->getMessages());
         }
         $postView = new PostsViews();
         $postView->setPostsId($id);
         $postView->setIpaddress($ipAddress);
         if (!$postView->save()) {
             $this->saveLoger($postView->getMessages());
         }
     }
     $this->view->setVars(['post' => $object, 'form' => new ReplyForm(), 'votes' => $object->getVotes($id, Vote::OBJECT_POSTS), 'postsReply' => $object->getPostsWithVotes($id), 'commentForm' => new CommentForm(), 'userPosts' => $object->user, 'type' => Posts::POST_QUESTIONS]);
     $this->tag->setTitle($this->escaper->escapeHtml($object->getTitle()));
     return $this->view->pick('single');
 }
Beispiel #4
0
 /**
  * Comments are temporary "Post-It" notes left on a question or answer.
  * They can be up-voted (but not down-voted) and flagged, but do not generate reputation.
  * There's no revision history, and when they are deleted they're gone for good.
  *
  * @return mixed
  */
 public function commentAction()
 {
     $this->view->disable();
     if (!$this->request->isPost()) {
         return $this->response->redirect($this->router->getControllerName());
     }
     $user = Users::findFirstById($this->auth->getAuth()['id']);
     if (!$user) {
         $this->flashSession->error(t('You need to login first'));
         return $this->currentRedirect();
     }
     if ($user->getVote() < 9) {
         $this->flashSession->error(t('You must have 10 points to add comment'));
         return $this->currentRedirect();
     }
     $object = new Comment();
     $form = new CommentForm($object);
     $form->bind($_POST, $object);
     if (!$form->isValid($this->request->getPost())) {
         foreach ($form->getMessages() as $message) {
             $this->flashSession->error($message->getMessage());
         }
     } else {
         if (!$object->save()) {
             $this->displayModelErrors($object);
         }
     }
     return $this->currentRedirect();
 }
Beispiel #5
0
 public function settingsAction()
 {
     $object = Users::findFirstById($this->auth->getAuth()['id']);
     if (!$object) {
         $this->flashSession->error(t('Hack attempt!!!'));
         return $this->response->redirect();
     }
     $form = new UserSettingForm($object);
     $form->bind($_POST, $object);
     if ($this->request->isPost()) {
         if (!$form->isValid()) {
             foreach ($form->getMessages() as $message) {
                 $this->flashSession->error($message->getMessage());
             }
         } else {
             $object->setDigest($this->request->getPost('digest'));
             if (!$object->save()) {
                 foreach ($object->getMessages() as $message) {
                     $this->flashSession->error($message->getMessage());
                 }
             } else {
                 $this->flashSession->success(t('Data was successfully saved'));
                 $this->refreshAuthSession($object->toArray());
                 return $this->response->redirect($this->router->getControllerName() . '/settings');
             }
         }
     }
     $this->tag->setTitle(t('Edit profile'));
     $this->view->form = $form;
     $this->view->object = $object;
 }
 /**
  * The answer a question
  * @return mixed
  */
 public function answerAction()
 {
     $this->view->disable();
     $auth = $this->auth->getAuth();
     if (!$auth) {
         $this->flashSession->error(t('You must be logged in first to post answer'));
         return $this->currentRedirect();
     }
     if ($this->request->isPost()) {
         $postId = $this->request->getPost('id');
         $content = $this->request->getPost('content', 'trim');
         if (str_word_count($content) < 10) {
             $this->flashSession->error(t('Body must be at least 15 word'));
             return $this->currentRedirect();
         }
         $post = Posts::findFirstById($postId);
         $user = Users::findFirstById($auth['id']);
         //Only update the number of replies if the user that commented isn't the same that posted
         if ($user->getId() != $post->getUsersId()) {
             $post->setNumberReply($post->getNumberReply() + 1);
             $post->user->increaseKarma(Karma::SOMEONE_REPLIED_TO_MY_POST);
             $user->increaseKarma(Karma::REPLY_ON_SOMEONE_ELSE_POST);
             if (!$post->save() || !$user->save()) {
                 error_log('Save fail answerAction. I am on here ' . __LINE__);
                 return false;
             }
         }
         $object = new PostsReply();
         $object->setPostsId($postId);
         $object->setContent($content);
         $object->setUsersId($auth['id']);
         if (!$object->save()) {
             foreach ($object->getMessages() as $message) {
                 $this->flashSession->error($message);
             }
             return $this->currentRedirect();
         }
         $this->flashSession->success(t('Data was successfully saved'));
         return $this->currentRedirect();
     }
 }
Beispiel #7
0
 /**
  * @param $id
  * @return mixed
  */
 public function getUserById($id)
 {
     return Users::findFirstById($id);
 }