public function genUniqueUrl($from)
 {
     static $time = 0;
     static $counter = 0;
     $from = preg_replace('/\\s+/', '-', $from);
     $url = blogHelper::transliterate($from);
     if (strlen($url) == 0) {
         $url = self::shortUuid();
     } else {
         $url = mb_substr($url, 0, $this->url_length);
     }
     $url = mb_strtolower($url);
     $pattern = mb_substr($this->escape($url, 'like'), 0, $this->url_length - 3) . '%';
     $sql = "SELECT url FROM {$this->table} WHERE url LIKE '{$pattern}' ORDER BY LENGTH(url)";
     $alike = $this->query($sql)->fetchAll('url');
     if (is_array($alike) && isset($alike[$url])) {
         $last = array_shift($alike);
         $counter = 1;
         do {
             $modifier = "-{$counter}";
             $length = mb_strlen($modifier);
             $url = mb_substr($last['url'], 0, $this->url_length - $length) . $modifier;
         } while ($counter++ < 99 && isset($alike[$url]));
         if (isset($alike[$url])) {
             $short_uuid = self::shortUuid();
             $length = mb_strlen($short_uuid);
             $url = mb_substr($last['url'], 0, $this->url_length - $length) . $short_uuid;
         }
     }
     return mb_strtolower($url);
 }
 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 execute()
 {
     if (!wa()->getUser()->getRights('blog', blogRightConfig::RIGHT_ADD_BLOG, true)) {
         throw new waAPIException('access_denied', 403);
     }
     $data = waRequest::post();
     // check required param name
     $this->post('name', true);
     $data = array_merge($data, array('color' => 'b-white', 'icon' => 'blog', 'url' => blogHelper::transliterate($data['name'])));
     $blog_model = new blogBlogModel();
     $data['sort'] = (int) $blog_model->select('MAX(`sort`)')->fetchField() + 1;
     $blog_id = $blog_model->insert($data);
     wa()->getUser()->setRight('blog', "blog.{$blog_id}", blogRightConfig::RIGHT_FULL);
     // return info of the new blog
     $_GET['id'] = $blog_id;
     $method = new blogBlogGetInfoMethod();
     $this->response = $method->getResponse(true);
 }
Beispiel #4
0
<?php

//create first blog at install
try {
    $name = wa()->accountName();
    $blog = array('status' => blogBlogModel::STATUS_PUBLIC, 'name' => $name, 'icon' => 'blog', 'color' => 'b-white', 'url' => blogHelper::transliterate($name));
    $app = wa()->getApp();
    $blog_model = new blogBlogModel();
    if ($blog_model->countAll() == 0) {
        $blog_id = $blog_model->insert($blog);
        $user = wa()->getUser();
        if (!$user->isAdmin($app)) {
            $user->setRight($app, "blog.{$blog_id}", blogRightConfig::RIGHT_FULL);
        }
    }
} catch (Exception $e) {
    waLog::log($e->getMessage());
}
 /**
  * Validate data
  *
  * @param array &$data
  * @param array $options
  *
  * @return array messages or empty array
  */
 public function validate(&$data, $options = array())
 {
     $messages = array();
     if ($data['blog_status'] != blogBlogModel::STATUS_PRIVATE) {
         if (!empty($data['id'])) {
             $url_validator = new blogSlugValidator(array('id' => $data['id']));
         } else {
             if (!empty($options['transliterate']) && !$data['url']) {
                 if ($data['title']) {
                     $data['url'] = blogHelper::transliterate($data['title']);
                 } else {
                     $data['url'] = $this->genUniqueUrl('');
                 }
             }
             $url_validator = new blogSlugValidator();
         }
         $url_validator->setSubject(blogSlugValidator::SUBJECT_POST);
         if (!$url_validator->isValid($data['url'])) {
             $messages['url'] = current($url_validator->getErrors());
             if ($url_validator->isError(blogSlugValidator::ERROR_REQUIRED) && ($data['id'] || !$data['id'] && $data['status'] == blogPostModel::STATUS_DRAFT)) {
                 $url = $this->select('url')->where('id = i:id', array('id' => $data['id']))->fetchField('url');
                 $data['url'] = $url ? $url : $this->genUniqueUrl($data['title']);
                 unset($messages['url']);
                 if (!$url_validator->isValid($data['url'])) {
                     $messages['url'] = current($url_validator->getErrors());
                 }
             } elseif (!empty($options['make'])) {
                 $data['url'] = $this->genUniqueUrl($data['url']);
                 unset($messages['url']);
                 if (!$url_validator->isValid($data['url'])) {
                     $messages['url'] = current($url_validator->getErrors());
                 }
             }
         }
     } else {
         if (empty($data['id'])) {
             $data['url'] = $this->genUniqueUrl(empty($data['url']) ? $data['title'] : $data['url']);
         } else {
             $url = $this->select('url')->where('id = i:id', array('id' => $data['id']))->fetchField('url');
             $data['url'] = $url ? $url : $this->genUniqueUrl($data['title']);
         }
     }
     if (isset($data['datetime']) && !is_null($data['datetime'])) {
         if (!empty($options['datetime'])) {
             $formats = (array) $options['datetime'];
         } elseif (isset($options['datetime'])) {
             $formats = array();
         } elseif (strpos($data['datetime'], ':') !== false) {
             $formats = array('fulldatetime', 'datetime');
         } else {
             $formats = array('date');
         }
         if ($data['datetime'] != '') {
             $datetime = $data['datetime'];
             foreach ($formats as $format) {
                 try {
                     if ($datetime = waDateTime::parse($format, $data['datetime'])) {
                         break;
                     }
                 } catch (Exception $ex) {
                     $messages['datetime'] = _w('Incorrect format');
                     waLog::log($ex->getMessage());
                 }
             }
             if (preg_match('/^([\\d]{4})\\-([\\d]{1,2})\\-([\\d]{1,2})(\\s|$)/', $datetime, $matches)) {
                 if (!checkdate($matches[2], $matches[3], $matches[1])) {
                     $messages['datetime'] = _w('Incorrect format');
                 }
             }
             $data['datetime'] = $datetime;
         } else {
             if ($data['status'] != blogPostModel::STATUS_DRAFT) {
                 $data['datetime'] = false;
             }
         }
         if ($data['datetime'] === false) {
             $messages['datetime'] = _w('Incorrect format');
         }
     }
     /**
      * @event post_validate
      * @param array [string]mixed $data
      * @param array ['plugin']['%plugin_id%']mixed plugin data
      * @return array['%plugin_id%']['field']string error
      */
     $messages['plugin'] = wa()->event('post_validate', $data);
     if (empty($messages['plugin'])) {
         unset($messages['plugin']);
     }
     return $messages;
 }
 public function execute()
 {
     $this->getResponse()->addHeader('Content-type', 'application/json');
     $blog_name = waRequest::post('blog_name', '', waRequest::TYPE_STRING_TRIM);
     $this->response = array('slug' => blogHelper::transliterate($blog_name));
 }