public function execute()
 {
     $post_title = waRequest::post('post_title', '', waRequest::TYPE_STRING_TRIM);
     $blog_id = waRequest::post('blog_id', 0, waRequest::TYPE_INT);
     $slug = waRequest::post('slug', '', waRequest::TYPE_STRING_TRIM);
     $blog_model = new blogBlogModel();
     $blog = $blog_model->getById($blog_id);
     if (!$blog) {
         throw new waException(_w("Can't find corresponding blog"));
     }
     $this->response['is_private_blog'] = $blog['status'] == blogBlogModel::STATUS_PRIVATE;
     $post_id = waRequest::post('post_id', 0, waRequest::TYPE_INT);
     $post_model = new blogPostModel();
     if ($post_id) {
         $post = $post_model->getById($post_id, array('text', 'text_before_cut'));
         if (!$post) {
             throw new waException(_w("Can't find corresponding post"));
         }
         if ($post['status'] != blogPostModel::STATUS_PUBLISHED) {
             $options = array('contact_id' => $post['contact_id'], 'blog_id' => $blog_id, 'post_id' => $post['id'], 'user_id' => wa()->getUser()->getId());
             $this->response['preview_hash'] = blogPostModel::getPreviewHash($options);
             $this->response['preview_hash'] = base64_encode($this->response['preview_hash'] . $options['user_id']);
         }
         $this->response['slug'] = $post['url'];
         $this->response['is_published'] = $post['status'] == blogPostModel::STATUS_PUBLISHED;
         $this->response['is_adding'] = false;
     } else {
         $post = array();
         $this->response['slug'] = $slug ? $slug : blogHelper::transliterate($post_title);
         $this->response['is_published'] = false;
         $this->response['is_adding'] = true;
     }
     $post['blog_id'] = $blog_id;
     $post['album_link_type'] = 'blog';
     $other_links = blogPostModel::getPureUrls($post);
     $this->response['link'] = array_shift($other_links);
     if (!$this->response['link']) {
         $this->response['is_private_blog'] = true;
     }
     $this->response['other_links'] = $other_links;
     foreach ($this->response as $k => &$item) {
         if (!$item || !is_string($item) && !is_array($item)) {
             continue;
         }
         if (is_array($item)) {
             $item = array_map('htmlspecialchars', $item, array_fill(0, count($item), ENT_QUOTES));
             continue;
         }
         $item = htmlspecialchars($item, ENT_QUOTES);
     }
     unset($item);
     $this->getResponse()->addHeader('Content-type', 'application/json');
 }
 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);
 }
 private function save($post)
 {
     $options = array();
     if (waRequest::post('transliterate', null)) {
         $options['transliterate'] = true;
     }
     $this->validate_messages = $this->post_model->validate($post, $options);
     if ($this->validate_messages) {
         $this->errors = $this->validate_messages;
     } else {
         $post['text_before_cut'] = null;
         $post['cut_link_label'] = null;
         $template = '<!--[\\s]*?more[\\s]*?(text[\\s]*?=[\\s]*?[\'"]([\\s\\S]*?)[\'"])*[\\s]*?-->';
         $descriptor = preg_split("/{$template}/", $post['text'], 2, PREG_SPLIT_DELIM_CAPTURE);
         if ($descriptor) {
             if (count($descriptor) == 2) {
                 $post['text_before_cut'] = blogPost::closeTags($descriptor[0]);
             } elseif (count($descriptor) > 2) {
                 $post['text_before_cut'] = blogPost::closeTags($descriptor[0]);
                 if (isset($descriptor[2])) {
                     $post['cut_link_label'] = $descriptor[2];
                 }
             }
         }
         if ($post['id']) {
             $prev_post = $this->post_model->getFieldsById($post['id'], 'status');
             if ($prev_post['status'] != blogPostModel::STATUS_PUBLISHED && $post['status'] == blogPostModel::STATUS_PUBLISHED) {
                 $this->inline = false;
             }
             $this->post_model->updateItem($post['id'], $post);
             if ($prev_post['status'] != blogPostModel::STATUS_PUBLISHED && $post['status'] == blogPostModel::STATUS_PUBLISHED) {
                 $this->log('post_publish', 1);
             } else {
                 $this->log('post_edit', 1);
             }
         } else {
             $post['id'] = $this->post_model->updateItem(null, $post);
             $this->log('post_publish', 1);
         }
         $this->saveParams($post['id']);
         $this->clearViewCache($post['id'], $post['url']);
         if (!$this->inline) {
             if ($post['status'] != blogPostModel::STATUS_PUBLISHED) {
                 $params = array('module' => 'post', 'action' => 'edit', 'id' => $post['id']);
             } elseif ($post['blog_status'] == blogBlogModel::STATUS_PUBLIC) {
                 $params = array('blog' => $post['blog_id']);
             } else {
                 $params = array('module' => 'post', 'id' => $post['id']);
             }
             $this->response['redirect'] = $this->getRedirectUrl($params);
         } else {
             $this->response['formatted_datetime'] = waDateTime::format('humandatetime', $post['datetime']);
             $this->response['id'] = $post['id'];
             $this->response['url'] = $post['url'];
             if ($post['status'] != blogPostModel::STATUS_PUBLISHED) {
                 $options = array('contact_id' => $post['contact_id'], 'blog_id' => $post['blog_id'], 'post_id' => $post['id'], 'user_id' => wa()->getUser()->getId());
                 $preview_hash = blogPostModel::getPreviewHash($options);
                 $this->response['preview_hash'] = base64_encode($preview_hash . $options['user_id']);
                 $this->response['debug'] = $options;
             }
         }
     }
 }
 public function execute()
 {
     $post_id = waRequest::get('id', null, waRequest::TYPE_INT);
     $blog_model = new blogBlogModel();
     $blogs = $blog_model->getAvailable();
     if (!$blogs) {
         $this->setTemplate('BlogNotFound');
         return;
     }
     $blogs = $blog_model->prepareView($blogs);
     if ($post_id) {
         // edit post
         $post_model = new blogPostModel();
         $post = $post_model->getById($post_id);
         if (!$post) {
             throw new waException(_w('Post not found'), 404);
         }
         //check rights
         if (blogHelper::checkRights($post['blog_id']) < blogRightConfig::RIGHT_FULL && $post['contact_id'] != $this->getUser()->getId()) {
             throw new waRightsException(_w('Access denied'));
         }
         $post['datetime'] = $post['datetime'] >= 1971 ? $post['datetime'] : '';
         $blog_id = $post['blog_id'];
         $blog = $blogs[$blog_id];
         $title = trim(sprintf(_w('Editing post %s'), $post['title']));
     } else {
         // add post
         $date = waRequest::get('date', '');
         $blog = $this->getAllowedBlog($blogs, wa()->getStorage()->read('blog_last_id'));
         if (!$blog) {
             throw new waRightsException(_w('Access denied'));
         }
         $blog_id = $blog['id'];
         $post = array('title' => $this->getRequest()->post('title', '', waRequest::TYPE_STRING_TRIM), 'text' => $this->getRequest()->post('text', '', waRequest::TYPE_STRING_TRIM), 'continued_text' => null, 'categories' => array(), 'contact_id' => wa()->getUser()->getId(), 'url' => '', 'blog_id' => $blog_id, 'comments_allowed' => true);
         $post['id'] = '';
         $post['status'] = $date ? blogPostModel::STATUS_DEADLINE : blogPostModel::STATUS_DRAFT;
         $post['datetime'] = '';
         $post['meta_title'] = null;
         $post['meta_keywords'] = null;
         $post['meta_description'] = null;
         $title = _w('Adding new post');
     }
     $all_links = blogPostModel::getPureUrls($post);
     $post['other_links'] = $all_links;
     $post['link'] = array_shift($post['other_links']);
     $post['remaining_time'] = null;
     if ($post['status'] == blogPostModel::STATUS_SCHEDULED && $post['datetime']) {
         $post['remaining_time'] = $this->calculateRemainingTime($post['datetime']);
     }
     if ($blog['rights'] >= blogRightConfig::RIGHT_FULL) {
         $users = blogHelper::getAuthors($post['blog_id']);
     } else {
         $user = $this->getUser();
         $users = array($user->getId() => $user->getName());
     }
     // preview hash for all type of drafts
     if ($post['status'] != blogPostModel::STATUS_PUBLISHED) {
         $options = array('contact_id' => $post['contact_id'], 'blog_id' => $blog_id, 'post_id' => $post['id'], 'user_id' => wa()->getUser()->getId());
         $preview_hash = blogPostModel::getPreviewHash($options);
         $this->view->assign('preview_hash', base64_encode($preview_hash . $options['user_id']));
     }
     $this->view->assign('no_settlements', empty($all_links) ? true : false);
     $this->view->assign('params', $this->getPostParams($post['id']));
     $this->view->assign('blog', $blog);
     $this->view->assign('users', $users);
     $this->view->assign('blogs', $blogs);
     $allow_change_blog = 0;
     foreach ($blogs as $blog_item) {
         if ($blog_item['rights'] >= blogRightConfig::RIGHT_READ_WRITE) {
             ++$allow_change_blog;
         }
     }
     $this->view->assign('allow_change_blog', $allow_change_blog);
     $this->view->assign('post_id', $post_id);
     $this->view->assign('datetime_timezone', waDateTime::date("T", null, wa()->getUser()->getTimezone()));
     /**
      * Backend post edit page
      * UI hook allow extends post edit page
      * @event backend_post_edit
      * @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%]['sidebar'] Plugin sidebar html output
      * @return array[string][string]string $return[%plugin_id%]['toolbar'] Plugin toolbar html output
      * @return array[string][string]string $return[%plugin_id%]['editor_tab'] Plugin editor tab html output
      */
     $this->view->assign('backend_post_edit', wa()->event('backend_post_edit', $post, array('sidebar', 'toolbar', 'editor_tab')));
     $app_settings = new waAppSettingsModel();
     $show_comments = $app_settings->get($this->getApp(), 'show_comments', true);
     $this->view->assign('show_comments', $show_comments);
     $this->view->assign('post', $post);
     /**
      * @deprecated 
      * For backward compatibility reason
      */
     $this->view->assign('cron_schedule_time', waSystem::getSetting('cron_schedule', 0, 'blog'));
     $this->view->assign('last_schedule_cron_time', waSystem::getSetting('last_schedule_cron_time', 0, 'blog'));
     $this->view->assign('cron_command', 'php ' . wa()->getConfig()->getRootPath() . '/cli.php blog schedule');
     $this->setLayout(new blogDefaultLayout());
     $this->getResponse()->setTitle($title);
 }