public function commentValidate($comment)
 {
     $result = null;
     if (!$comment['contact_id'] && ($api_key = $this->getSettingValue('api_key')) && class_exists('Akismet')) {
         $url = wa()->getRouteUrl('blog', array(), true);
         $post_url = null;
         if (isset($comment['post_data'])) {
             $post_url = blogPost::getUrl($comment['post_data']);
             if (is_array($post_url)) {
                 $post_url = array_shift($post_url);
             }
         }
         $akismet = new Akismet($url, $api_key);
         $akismet->setCommentAuthor($comment['name']);
         $akismet->setCommentAuthorEmail($comment['email']);
         //$akismet->setCommentAuthorURL($comment['site']);
         $akismet->setCommentContent($comment['text']);
         if ($post_url) {
             $akismet->setPermalink($post_url);
         }
         if ($akismet->isCommentSpam()) {
             $result = array('text' => _wp('According to Akismet.com, your comment very much looks like spam, thus will not be published. Please rewrite your comment. Sorry for the inconvenience.'));
         }
     }
     return $result;
 }
 public function execute()
 {
     if (!$this->appSettings('show_comments', true)) {
         throw new waException(_ws("Page not found"), 404);
     }
     $this->comment_model = new blogCommentModel();
     $this->blog_id = waRequest::param('blog_id', false, waRequest::TYPE_ARRAY_INT);
     $this->verify();
     if ($this->getRequest()->method() == 'post') {
         $res = $this->addComment();
     } else {
         $this->comment_id = waRequest::param('blog_id', false, waRequest::TYPE_ARRAY_INT);
         $res = true;
     }
     if (waRequest::get('json')) {
         if ($this->comment_id) {
             $this->displayComment();
         }
     } else {
         if (!$res) {
             var_export($this->errors);
             exit;
             //handle error on non ajax
         }
         $url = blogPost::getUrl($this->post) . '#comment' . intval($this->parent_id ? $this->parent_id : $this->comment_id);
         $this->redirect($url);
     }
 }
 public function run($params = NULL)
 {
     $app_settings_model = new waAppSettingsModel();
     $app_settings_model->set(array('blog', 'emailsubscription'), 'last_emailsubscription_cron_time', time());
     $model = new blogEmailsubscriptionLogModel();
     $row = $model->getByField('status', 0);
     if ($row) {
         $post_id = $row['post_id'];
         $post_model = new blogPostModel();
         $post = $post_model->getById($post_id);
         $blog_model = new blogBlogModel();
         $blog = $blog_model->getById($post['blog_id']);
         $subject = $blog['name'] . ': ' . $post['title'];
         $post_title = htmlspecialchars($post['title']);
         if ($blog['status'] == blogBlogModel::STATUS_PUBLIC) {
             $post_url = blogPost::getUrl($post);
         } else {
             $app_settings_model = new waAppSettingsModel();
             $post_url = $app_settings_model->get(array('blog', 'emailsubscription'), 'backend_url', wa()->getRootUrl(true) . wa()->getConfig()->getBackendUrl());
             $post_url .= "/blog/?module=post&id=" . $post_id;
         }
         $blog_name = htmlspecialchars($blog['name']);
         $body = '<html><body>' . sprintf(_wp("New post in the blog “%s”"), $blog_name) . ': <strong><a href="' . $post_url . '">' . $post_title . '</a></strong></body></html>';
         $message = new waMailMessage();
         $message->setEncoder(Swift_Encoding::getBase64Encoding());
         $message->setSubject($subject);
         $message->setBody($body);
         $rows = $model->getByField(array('status' => 0, 'post_id' => $post_id), true);
         $message_count = 0;
         foreach ($rows as $row) {
             try {
                 $message->setTo($row['email'], $row['name']);
                 $status = $message->send() ? 1 : -1;
                 $model->setStatus($row['id'], $status);
                 if ($status) {
                     $message_count++;
                 }
             } catch (Exception $e) {
                 $model->setStatus($row['id'], -1, $e->getMessage());
             }
         }
         /**
          * Notify plugins about sending emailsubscripition
          * @event followup_send
          * @return void
          */
         wa()->event('emailsubscription_send', $message_count);
     }
 }
 public function frontendExecute()
 {
     $post_slug = waRequest::param('post_url', false, waRequest::TYPE_STRING_TRIM);
     $storage = wa()->getStorage();
     $post_model = new blogPostModel();
     $show_comments = $this->appSettings('show_comments', true);
     $request_captcha = $show_comments && $this->appSettings('request_captcha', true);
     $require_authorization = $show_comments && $this->appSettings('require_authorization', false);
     $available = blogHelper::getAvailable();
     // it's preview
     $hash = waRequest::get('preview');
     $post = $post_model->search(array('url' => $post_slug, 'status' => $hash ? false : blogPostModel::STATUS_PUBLISHED), array('comments' => $show_comments ? array(50, 20) : false, 'params' => true, 'escape' => true), array('blog' => $available))->fetchSearchItem();
     if (!$post) {
         throw new waException(_w('Post not found'), 404);
     }
     if ($post['status'] != blogPostModel::STATUS_PUBLISHED) {
         $hash = base64_decode($hash);
         list($hash, $user_id) = array(substr($hash, 0, 32), substr($hash, 32));
         $options = array('contact_id' => $post['contact_id'], 'blog_id' => $post['blog_id'], 'post_id' => $post['id'], 'user_id' => $user_id);
         $preview_cached_options = $storage->read('preview');
         $preview_cached_post_options = isset($preview_cached_options['post_id']) ? $preview_cached_options['post_id'] : null;
         if ($preview_cached_post_options && $preview_cached_post_options != $options) {
             $preview_cached_post_options = null;
         }
         if (!$preview_cached_post_options) {
             if ($hash == blogPostModel::getPreviewHash($options, false, false)) {
                 $preview_cached_options['post_id'] = $preview_cached_post_options = $options;
                 $storage->write('preview', $preview_cached_options);
             }
         }
         if (!$preview_cached_post_options) {
             throw new waException(_w('Post not found'), 404);
         }
         if (!$this->checkAuthorRightsToBlog($user_id, $post)) {
             throw new waException(_w('Post not found'), 404);
         }
     }
     $title = $this->getResponse()->getTitle();
     if ($this->getRequest()->param('title_type', 'blog_post') == 'blog_post') {
         if ($title) {
             $this->getResponse()->setTitle($title . " » " . $post['title']);
         } elseif (isset($available[$post['blog_id']]) && ($title = $available[$post['blog_id']]['title'])) {
             $this->getResponse()->setTitle($title . " » " . $post['title']);
         } else {
             $this->getResponse()->setTitle($post['title']);
         }
     } else {
         $this->getResponse()->setTitle($post['title']);
     }
     $blog_id = (array) $this->getRequest()->param('blog_id');
     if (!in_array($post['blog_id'], $blog_id)) {
         if ($this->getRequest()->param('blog_url_type') == 0) {
             if (isset($available[$post['blog_id']])) {
                 $this->redirect($post['link'], 301);
             }
         }
         throw new waException(_w('Post not found'), 404);
     }
     $this->getRequest()->setParam('blog_id', $post['blog_id']);
     if (isset($post['comments']) && !empty($post['comments'])) {
         $depth = 1000;
         foreach ($post['comments'] as $key => $comment) {
             if ($comment['status'] == blogCommentModel::STATUS_DELETED) {
                 if ($comment['depth'] < $depth) {
                     $depth = $comment['depth'];
                 }
                 unset($post['comments'][$key]);
                 continue;
             }
             if ($comment['depth'] > $depth) {
                 unset($post['comments'][$key]);
             } else {
                 $depth = 1000;
             }
         }
     }
     $errors = array();
     $form = array();
     if ($storage->read('errors') !== null) {
         $errors = $storage->read('errors');
         $form = $storage->read('form');
         $storage->remove('errors');
         $storage->remove('form');
     }
     $post['comment_link'] = blogPost::getUrl($post, 'comment');
     $post['link'] = blogPost::getUrl($post);
     /**
      * Frontend post view page
      * UI hook allow extends frontend post view page
      * @event frontend_post
      * @param array[string]mixed $post
      * @param array[string]int $post['id']
      * @param array[string]int $post['blog_id']
      * @return array[string][string]string $return[%plugin_id%]
      * @return array[string][string]string $return[%plugin_id%]['footer']
      */
     $this->view->assign('frontend_post', wa()->event('frontend_post', $post));
     $this->view->assign('errors', $errors);
     $this->view->assign('form', $form);
     $this->view->assign('show_comments', $show_comments);
     $this->view->assign('request_captcha', $request_captcha);
     $this->view->assign('require_authorization', $require_authorization);
     $this->view->assign('theme', waRequest::param('theme', 'default'));
     $app_url = wa()->getAppStaticUrl();
     $root_url = wa()->getRootUrl();
     $storage = wa()->getStorage();
     $current_auth = $storage->read('auth_user_data');
     $current_auth_source = $current_auth ? $current_auth['source'] : null;
     $this->view->assign('current_auth_source', $current_auth_source);
     $this->view->assign('current_auth', $current_auth, true);
     $adapters = wa()->getAuthAdapters();
     $this->view->assign('auth_adapters', $adapters);
     $this->view->getHelper()->globals($this->getRequest()->param());
     if ($this->getConfig()->getOption('can_use_smarty')) {
         try {
             $post['text'] = $this->view->fetch("string:{$post['text']}", $this->cache_id);
         } catch (SmartyException $ex) {
             $post['text'] = blogPost::handleTemplateException($ex, $post);
         }
     }
     $this->view->assign('post', $post);
 }
 public static function getPureUrls($post)
 {
     if (isset($post['url'])) {
         unset($post['url']);
     }
     $urls = blogPost::getUrl($post);
     $replace = array_merge(explode(' ', date('Y n j')), (array) '');
     $urls = str_replace(array('%year%', '%month%', '%day%', '%post_url%/'), $replace, $urls);
     return $urls;
 }
示例#6
0
 /**
  * Extend items by adding contact info into $rows[i]['user']
  * Uses:
  * - $rows[i]['contact_id']
  * - $rows[i]['name'] or $rows[i]['contact_name'] when contact is not found or its name is empty
  * - $rows[i]['auth_provider'] for default userpic URL
  *
  * @param array $rows
  * @param array $fields
  * @param bool $get_link pass true to get $rows[i]['user']['posts_link']
  */
 public static function extendUser(&$rows, $fields = array(), $get_link = false)
 {
     $default_fields = array('id', 'name', 'firstname', 'middlename', 'lastname');
     $fields = array_unique(array_merge($fields, $default_fields));
     // All contact ids
     $ids = array();
     foreach ($rows as $row) {
         if ($row['contact_id']) {
             $ids[] = intval($row['contact_id']);
         }
     }
     $ids = array_unique($ids);
     // Fetch contacts using collection
     $collection = new waContactsCollection($ids);
     $contacts = $collection->getContacts(implode(',', $fields), 0, count($ids));
     // Prepare data row to use as a placeholder when contact is not found
     $contact = new waContact(0);
     $contacts[0] = array('name' => '');
     $photo_fields = array();
     foreach ($fields as $field) {
         if (preg_match('@^photo_url_(\\d+)$@', $field, $matches)) {
             $photo_fields[] = $field;
             $contacts[0][$field] = $contact->getPhoto($matches[1], $matches[1]);
         } else {
             $contacts[0][$field] = $contact->get($field);
         }
     }
     // Format contact names
     foreach ($contacts as &$c) {
         $c['name'] = waContactNameField::formatName($c);
     }
     unset($c);
     // Add data as 'user' key to each row in $rows
     $app_static_url = wa()->getAppStaticUrl();
     foreach ($rows as &$row) {
         $row['user'] = array();
         $id = $row['contact_id'] = max(0, intval($row['contact_id']));
         if (!isset($contacts[$id])) {
             $id = 0;
         }
         if (isset($contacts[$id])) {
             if (isset($row['url']) && $get_link && !isset($contacts[$id]['posts_link'])) {
                 $contacts[$id]['posts_link'] = blogPost::getUrl($row, 'author');
             }
             $row['user'] = $contacts[$id];
         }
         if (!$id || !isset($contacts[$id])) {
             if (isset($row['name'])) {
                 $row['user']['name'] = $row['name'];
             } elseif (isset($row['contact_name'])) {
                 $row['user']['name'] = $row['contact_name'];
             }
             if (isset($row['auth_provider'])) {
                 if ($row['auth_provider'] && $row['auth_provider'] != blogCommentModel::AUTH_GUEST) {
                     $row['user']['photo_url'] = "{$app_static_url}img/{$row['auth_provider']}.png";
                     foreach ($photo_fields as $field) {
                         $row['user'][$field] =& $row['user']['photo_url'];
                     }
                 }
             }
         }
         unset($row);
     }
 }
 /**
  *
  * Extend items by contact info
  * @param array $rows
  * @param array $fields
  * @param bool $get_link
  */
 public static function extendUser(&$rows, $fields = array(), $get_link = false)
 {
     $default_fields = array('id', 'name');
     $fields = array_unique(array_merge($fields, $default_fields));
     $ids = array();
     foreach ($rows as $row) {
         if ($row['contact_id']) {
             $ids[] = intval($row['contact_id']);
         }
     }
     $ids = array_unique($ids);
     $collection = new waContactsCollection($ids);
     $contacts = $collection->getContacts(implode(',', $fields), 0, count($ids));
     $contact = new waContact(0);
     $contacts[0] = array('name' => '');
     $photo_fields = array();
     foreach ($fields as $field) {
         if (preg_match('@^photo_url_(\\d+)$@', $field, $matches)) {
             $photo_fields[] = $field;
             $contacts[0][$field] = $contact->getPhoto($matches[1], $matches[1]);
         } else {
             $contacts[0][$field] = $contact->get($field);
         }
     }
     $app_static_url = wa()->getAppStaticUrl();
     foreach ($rows as &$row) {
         $row['user'] = array();
         $id = $row['contact_id'] = max(0, intval($row['contact_id']));
         if (!isset($contacts[$id])) {
             $id = 0;
         }
         if (isset($contacts[$id])) {
             if (isset($row['url']) && $get_link && !isset($contacts[$id]['posts_link'])) {
                 $contacts[$id]['posts_link'] = blogPost::getUrl($row, 'author');
             }
             $row['user'] = $contacts[$id];
         }
         if (!$id || !isset($contacts[$id])) {
             if (isset($row['name'])) {
                 $row['user']['name'] = $row['name'];
             } elseif (isset($row['contact_name'])) {
                 $row['user']['name'] = $row['contact_name'];
             }
             if (isset($row['auth_provider'])) {
                 if ($row['auth_provider'] && $row['auth_provider'] != blogCommentModel::AUTH_GUEST) {
                     $row['user']['photo_url'] = "{$app_static_url}img/{$row['auth_provider']}.png";
                     foreach ($photo_fields as $field) {
                         $row['user'][$field] =& $row['user']['photo_url'];
                     }
                 }
             }
         }
         unset($row);
     }
 }