public function execute()
 {
     $data = waRequest::post();
     $exclude = array('left_key', 'right_key', 'type', 'full_url', 'parent_id');
     foreach ($exclude as $k) {
         if (isset($data[$k])) {
             unset($data[$k]);
         }
     }
     // check required params
     $this->post('text', true);
     $post_id = $this->get('post_id', true);
     $post_model = new blogPostModel();
     $post = $post_model->getBlogPost($post_id);
     if (!$post) {
         throw new waAPIException('invalid_param', 'Post not found', 404);
     }
     $parent_id = $this->post('parent_id');
     $comment_model = new blogCommentModel();
     if ($parent_id) {
         $parent = $comment_model->getById($parent_id);
         if (!$parent) {
             throw new waAPIException('invalid_param', 'Parent comment not found', 404);
         }
     }
     $contact_id = wa()->getUser()->getId();
     // check rights
     try {
         blogHelper::checkRights($post['blog_id'], $contact_id, blogRightConfig::RIGHT_READ);
     } catch (waException $e) {
         throw new waAPIException('access_denied', 403);
     }
     // check comment mode
     if (!$post['comments_allowed']) {
         throw new waAPIException('invalid_param', "Isn't allowed comment to this post", 404);
     }
     $data = array_merge($data, array('blog_id' => $post['blog_id'], 'post_id' => $post_id, 'contact_id' => $contact_id, 'auth_provider' => blogCommentModel::AUTH_USER));
     $messages = $comment_model->validate($data);
     if ($messages) {
         throw new waAPIException('invalid_param', 'Validate messages: ' . implode("\n", $messages), 404);
     }
     $id = $comment_model->add($data, $parent_id);
     $_GET['id'] = $id;
     $method = new blogPostCommentsGetInfoMethod();
     $this->response = $method->getResponse(true);
 }
 private function importComments($post_id, $post)
 {
     static $commentors = array();
     static $comment_model;
     if (version_compare($this->version, '2.7', '>=')) {
         try {
             if ($comments = $this->xmlrpc("wp.getComments", $post_id, $this->option('login'), $this->option('password'), array('post_id' => $post_id))) {
                 if (!isset($comment_model)) {
                     $comment_model = new blogCommentModel();
                 }
                 $comment_map = array();
                 // new comment to the top
                 $comments = array_reverse($comments);
                 $emails = array();
                 foreach ($comments as $key => $comment) {
                     $email = trim(strtolower($comment['author_email']));
                     if ($email && !isset($commentors[$email])) {
                         $commentors[$email] = 0;
                         $emails[] = $email;
                     } else {
                         if (!isset($commentors[$email])) {
                             $commentors[$email] = 0;
                         }
                     }
                 }
                 $commentors = array_merge($commentors, $this->getContactByEmail($emails));
                 $comment_model->ping();
                 foreach ($comments as $key => $comment) {
                     $email = trim(strtolower($comment['author_email']));
                     $this->log('comment ' . $key, self::LOG_DEBUG);
                     $datetime = $comment['date_created_gmt'];
                     $datetime = date("Y-m-d H:i:s", $datetime->timestamp);
                     $parent = 0;
                     if ($comment['parent'] && isset($comment_map[$comment['parent']])) {
                         $parent = $comment_map[$comment['parent']];
                     }
                     $contact_id = isset($commentors[$email]) ? $commentors[$email] : 0;
                     $comment_data = array('post_id' => $post['id'], 'blog_id' => $post['blog_id'], 'contact_id' => $contact_id, 'text' => html_entity_decode(strip_tags($comment['content']), ENT_NOQUOTES, 'utf-8'), 'datetime' => $datetime, 'name' => html_entity_decode(trim($comment['author']), ENT_NOQUOTES, 'utf-8'), 'email' => $comment['author_email'], 'site' => $comment['author_url'], 'ip' => ip2long($comment['author_ip']), 'auth_provider' => $contact_id ? blogCommentModel::AUTH_USER : blogCommentModel::AUTH_GUEST, 'status' => $comment['status'] == 'approve' ? blogCommentModel::STATUS_PUBLISHED : blogCommentModel::STATUS_DELETED);
                     $comment_id = $comment_model->add($comment_data, $parent);
                     $comment_map[$comment['comment_id']] = $comment_id;
                 }
                 unset($comment_map);
             }
         } catch (waDbException $ex) {
             $this->log(__METHOD__ . ":\t" . $ex->getMessage() . "\nraw comment:\t" . var_export($comment, true) . "\nformatted comment:\t" . var_export($comment_data, true), self::LOG_WARNING);
         } catch (waException $ex) {
             if ($ex->getCode() == 401) {
                 $this->log($ex->getMessage(), self::LOG_WARNING);
             } else {
                 throw $ex;
             }
         }
     }
 }
 private function addComment()
 {
     $comment = array('blog_id' => $this->post['blog_id'], 'post_id' => $this->post['id'], 'contact_id' => $this->getUser()->getId(), 'text' => waRequest::post('text'));
     if ($this->getUser()->getId()) {
         $comment['auth_provider'] = 'user';
     } else {
         $comment['auth_provider'] = waRequest::post('auth_provider', 'guest', 'string_trim');
         if ($comment['auth_provider'] == 'user') {
             $comment['auth_provider'] = 'guest';
         } elseif (!$comment['auth_provider']) {
             $comment['auth_provider'] = 'guest';
         }
     }
     switch ($adapter = $comment['auth_provider']) {
         case 'user':
             break;
         case 'guest':
             $comment['name'] = waRequest::post('name', '', 'string_trim');
             $comment['email'] = waRequest::post('email', '', 'string_trim');
             $comment['site'] = waRequest::post('site', '', 'string_trim');
             $this->getStorage()->del('auth_user_data');
             if ($this->appSettings('require_authorization', false)) {
                 $this->errors[] = array('name' => _w('Only registered users can add comments'));
                 break;
             }
             if ($this->appSettings('request_captcha', true)) {
                 $captcha = new waCaptcha();
                 if (!wa()->getCaptcha()->isValid()) {
                     $this->errors[] = array('captcha' => _w('Invalid captcha code'));
                 }
             }
             break;
         default:
             $auth_adapters = wa()->getAuthAdapters();
             if (!isset($auth_adapters[$adapter])) {
                 $this->errors[] = _w('Invalid auth provider');
             } elseif ($user_data = $this->getStorage()->get('auth_user_data')) {
                 $comment['name'] = $user_data['name'];
                 $comment['email'] = '';
                 $comment['site'] = $user_data['url'];
             } else {
                 $this->errors[] = _w('Invalid auth provider data');
             }
             break;
     }
     $this->errors += $this->comment_model->validate($comment);
     if (count($this->errors) > 0) {
         if (waRequest::get('json')) {
             $this->getResponse()->addHeader('Content-type', 'application/json');
         }
         return false;
     }
     $this->parent_id = (int) waRequest::post('parent', 0);
     try {
         $comment['post_data'] = $this->post;
         $this->comment_id = $this->comment_model->add($comment, $this->parent_id);
         return true;
     } catch (Exception $e) {
         throw new waException(_w('Database error'));
     }
 }
 public function execute()
 {
     $this->post_id = max(0, $this->getRequest()->get('id', 0, waRequest::TYPE_INT));
     $this->parent_id = max(0, $this->getRequest()->post('parent', 0, waRequest::TYPE_INT));
     $comment_model = new blogCommentModel();
     $post_model = new blogPostModel();
     /**
      *
      * Parent comment data
      * @var array
      */
     $parent = null;
     $stream = false;
     //find comment parent
     if ($this->parent_id && ($parent = $comment_model->getById($this->parent_id))) {
         if ($this->post_id && $this->post_id != $parent['post_id']) {
             throw new waRightsException(_w('Access denied'));
         }
         if (!$this->post_id) {
             $stream = true;
         }
         $this->post_id = $parent['post_id'];
     } else {
         $this->parent_id = 0;
     }
     //find post
     if (!$this->post_id || !($post = $post_model->getBlogPost($this->post_id))) {
         throw new waException(_w('Post not found'), 404);
     }
     $contact_id = $this->getUser()->getId();
     #check rights
     $rights = blogHelper::checkRights($post['blog_id'], $contact_id, blogRightConfig::RIGHT_READ);
     //check comment mode
     if (!$post['comments_allowed']) {
         throw new waException(_w("Isn't allowed comment to this post"));
     }
     $comment = array('blog_id' => $post['blog_id'], 'post_id' => $this->post_id, 'contact_id' => $contact_id, 'text' => $this->getRequest()->post('text'), 'auth_provider' => blogCommentModel::AUTH_USER);
     $this->errors += $comment_model->validate($comment);
     if (count($this->errors) > 0) {
         return;
     }
     $id = $comment_model->add($comment, $this->parent_id);
     $this->logAction('comment_add', $id);
     $comment = $comment_model->getById($id);
     //$comment['new'] = false;
     $comment['parent'] = $this->parent_id;
     if ($stream) {
         $comment['parent_text'] = $parent ? $parent['text'] : null;
         $comment['parent_status'] = $parent ? $parent['status'] : null;
     } else {
         $count = $comment_model->getCount($post['blog_id'], $this->post_id);
         $this->response['count_str'] = $count . " " . _w('comment', 'comments', $count);
     }
     $comment['rights'] = $rights;
     $comment['post'] =& $post;
     $post['comments'] = $comment_model->prepareView(array($comment), array('photo_url_20'));
     blogHelper::extendRights($post['comments'], array(), $contact_id);
     if ($stream) {
         $posts = array($this->post_id => &$post);
         $blog_model = new blogBlogModel();
         $extend_data = array('blog' => $blog_model->search(array('id' => $this->post_id))->fetchSearchAll());
         $post_model->prepareView($posts, array('link' => true), $extend_data);
     } else {
         unset($comment['post']);
     }
     $view = wa()->getView();
     $view->assign('post', $post);
     $view->assign('contact_rights', $this->getUser()->getRights('contacts', 'backend'));
     $template = $view->fetch('templates/actions/post/include.comments.html');
     $this->getResponse()->addHeader('Content-type', 'application/json');
     $this->response['template'] = $template;
 }