Example #1
0
 /**
  * Save a new and or existing article
  *
  * @param  array     $rowData     Kanso\Articles\Article->tmpRow
  * @return array|boolean
  */
 public function saveArticle($rowData)
 {
     # Is this a new or existing article
     $rowData['id'] = (int) $rowData['id'];
     if (!isset($rowData['id']) || isset($rowData['id']) && $rowData['id'] < 1) {
         $newArticle = true;
     } else {
         $newArticle = false;
     }
     # Save the initial slug
     $initialSlug = $rowData['slug'];
     # Get a new Query Builder
     $Query = \Kanso\Kanso::getInstance()->Database->Builder();
     # If the category doesn't exist - create it
     $rowData['category'] = $this->createCategory($rowData['category']);
     # If the tags don't exist - create them
     $rowData['tags'] = $this->createTags($rowData['tags']);
     # Validate the title
     $rowData['title'] = trim($rowData['title']);
     if (empty($rowData['title'])) {
         $rowData['title'] = 'Untitled';
     }
     # Figure out if the title needs to be changed
     if ($newArticle) {
         $rowData['title'] = $this->uniqueBaseTitle($rowData['title']);
     } else {
         if ($rowData['title'] === 'Untitled' && !$newArticle) {
             $titleExists = $Query->SELECT('title')->FROM('posts')->where('title', '=', $rowData['title'])->AND_WHERE('id', '!=', (int) $rowData['id'])->FIND();
             if ($titleExists) {
                 $rowData['title'] = $this->uniqueBaseTitle('Untitled');
             }
         }
     }
     # Sanitize the thumbnail
     $rowData['thumbnail'] = \Kanso\Utility\Str::getAfterLastChar(rtrim($rowData['thumbnail'], '/'), '/');
     # Create a slug based on the category, tags, slug, author
     $rowData['slug'] = $this->titleToSlug($rowData['title'], $rowData['category']['slug'], $rowData['author']['slug'], $rowData['created'], $rowData['type']);
     # Make sure the author is an array
     if (is_string($rowData['author'])) {
         $author = $Query->SELECT('*')->FROM('users')->WHERE('name', '=', $rowData['author'])->ROW();
         if (!$author) {
             $rowData['author'] = ['id' => 1];
         } else {
             $rowData['author'] = $author;
         }
     } else {
         if (!isset($rowData['author'])) {
             $rowData['author'] = ['id' => 1];
         }
     }
     # Sanitize variables
     $rowData['excerpt'] = $rowData['excerpt'];
     $rowData['author_id'] = (int) $rowData['author']['id'];
     $rowData['category_id'] = (int) $rowData['category']['id'];
     $rowData['comments_enabled'] = (bool) $rowData['comments_enabled'];
     $rowData['content'] = urlencode($rowData['content']);
     $rowData['excerpt'] = urlencode($rowData['excerpt']);
     # Remove joined rows so we can update/insert
     $insertRow = \Kanso\Utility\Arr::unsetMultiple(['tags', 'category', 'content', 'comments', 'author'], $rowData);
     # Insert a new article
     if ($newArticle) {
         unset($insertRow['id']);
         $Query->INSERT_INTO('posts')->VALUES($insertRow)->QUERY();
         $rowData['id'] = intval(\Kanso\Kanso::getInstance()->Database->lastInsertId());
     } else {
         $Query->UPDATE('posts')->SET($insertRow)->WHERE('id', '=', (int) $rowData['id'])->QUERY();
         $Query->DELETE_FROM('tags_to_posts')->WHERE('post_id', '=', (int) $rowData['id'])->QUERY();
         $Query->DELETE_FROM('content_to_posts')->WHERE('post_id', '=', (int) $rowData['id'])->QUERY();
     }
     # Join the tags
     foreach ($rowData['tags'] as $tag) {
         $Query->INSERT_INTO('tags_to_posts')->VALUES(['post_id' => $rowData['id'], 'tag_id' => $tag['id']])->QUERY();
     }
     # Join the content
     $Query->INSERT_INTO('content_to_posts')->VALUES(['post_id' => $rowData['id'], 'content' => $rowData['content']])->QUERY();
     # Fire the event
     if ($newArticle) {
         \Kanso\Events::fire('newArticle', $rowData);
     } else {
         \Kanso\Events::fire('articleSave', $rowData);
     }
     if ($rowData['status'] === 'published') {
         \Kanso\Events::fire('articlePublish', $rowData);
     }
     # If the article is a page and the slug was changed
     # Remove the old slug and add the new one
     if ($rowData['type'] === 'page' && $rowData['slug'] !== $initialSlug) {
         $this->removeFromStaticPages($initialSlug);
         $this->addToStaticPages($rowData['slug']);
     }
     # return the row data
     return $rowData;
 }
Example #2
0
 /**
  * Reset Password authentification
  *
  * Note that this function always returns true, regardless of whether the email
  * exists or not. This is for security reasons to prevent people trying to
  * figure out what email addresses are registerd with Kanso.
  *
  * @return bool
  */
 private function resetPassword()
 {
     # Logged in users can't reset their password
     if ($this->isLoggedIn) {
         return false;
     }
     # Get the key from the user's session
     $sessionKey = \Kanso\Kanso::getInstance()->Session->get('session_kanso_password_key');
     # Get the key from the ajax request
     if (!isset($this->postVars['referer'])) {
         return false;
     }
     $ajaxKey = \Kanso\Utility\Str::getAfterLastChar($this->postVars['referer'], '?');
     # Get the HTTP REFERRER from the session
     $HTTPkey = \Kanso\Kanso::getInstance()->Session->getReferrer();
     if (!$HTTPkey) {
         return false;
     }
     $HTTPkey = \Kanso\Utility\Str::getAfterLastChar($HTTPkey, '?');
     # Validate all 3 keys are the same
     if (!\Kanso\Utility\Str::strcmpMulti($HTTPkey, $sessionKey, $ajaxKey)) {
         return false;
     }
     # Sanitize and validate the POST variables
     $postVars = $this->GUMP->sanitize($this->postVars);
     $this->GUMP->validation_rules(['password' => 'required|max_len,100|min_len,6']);
     $this->GUMP->filter_rules(['password' => 'trim']);
     $validated_data = $this->GUMP->run($postVars);
     if (!$validated_data) {
         return false;
     }
     $change = \Kanso\Kanso::getInstance()->Gatekeeper->resetPassword($validated_data['password'], $ajaxKey);
     if ($change) {
         return 'valid';
     }
     return false;
 }
Example #3
0
 /**
  * Filter posts based on a request type
  *
  * @param  string $requestType    The requested page type (optional)
  */
 public function filterPosts($requestType)
 {
     # Load the query parser
     $parser = new QueryParser();
     # Set the request type
     $this->requestType = $requestType;
     # Save the requested URL
     $uri = rtrim(\Kanso\Kanso::getInstance()->Environment()['REQUEST_URI'], '/');
     # Filter and paginate the posts based on the request type
     if ($requestType === 'home') {
         $perPage = \Kanso\Kanso::getInstance()->Config()['KANSO_POSTS_PER_PAGE'];
         $offset = $this->pageIndex * $perPage;
         $limit = $perPage;
         $this->queryStr = "post_status = published : post_type = post : orderBy = post_created, DESC : limit = {$offset}, {$perPage}";
         $this->posts = $parser->parseQuery($this->queryStr);
         $this->postCount = count($this->posts);
     } else {
         if ($requestType === 'archive') {
             $this->queryStr = 'post_status = published : post_type = post : orderBy = post_created, DESC';
             $this->posts = $parser->parseQuery($this->queryStr);
             $this->postCount = count($this->posts);
         } else {
             if ($requestType === 'tag') {
                 $perPage = \Kanso\Kanso::getInstance()->Config()['KANSO_POSTS_PER_PAGE'];
                 $offset = $this->pageIndex * $perPage;
                 $this->queryStr = 'post_status = published : post_type = post : orderBy = post_created, DESC : tag_slug = ' . explode("/", $uri)[2] . " : limit = {$offset}, {$perPage}";
                 $this->posts = $parser->parseQuery($this->queryStr);
                 $this->postCount = count($this->posts);
             } else {
                 if ($requestType === 'category') {
                     $perPage = \Kanso\Kanso::getInstance()->Config()['KANSO_POSTS_PER_PAGE'];
                     $offset = $this->pageIndex * $perPage;
                     $this->queryStr = 'post_status = published : post_type = post : orderBy = post_created, DESC : category_slug = ' . explode("/", $uri)[2] . " : limit = {$offset}, {$perPage}";
                     $this->posts = $parser->parseQuery($this->queryStr);
                     $this->postCount = count($this->posts);
                 } else {
                     if ($requestType === 'author') {
                         $perPage = \Kanso\Kanso::getInstance()->Config()['KANSO_POSTS_PER_PAGE'];
                         $offset = $this->pageIndex * $perPage;
                         $this->queryStr = ' post_status = published : post_type = post : orderBy = post_created, DESC: author_slug = ' . explode("/", $uri)[2] . ": limit = {$offset}, {$perPage}";
                         $this->posts = $parser->parseQuery($this->queryStr);
                         $this->postCount = count($this->posts);
                     } else {
                         if ($requestType === 'single') {
                             if (strpos($uri, '?draft') !== false) {
                                 $uri = ltrim(str_replace('?draft', '', $uri), '/');
                                 $this->queryStr = 'post_status = draft : post_type = post : post_slug = ' . $uri . '/';
                                 $this->posts = $parser->parseQuery($this->queryStr);
                                 $this->postCount = count($this->posts);
                             } else {
                                 $uri = \Kanso\Utility\Str::GetBeforeLastWord($uri, '/feed');
                                 $uri = ltrim($uri, '/');
                                 $this->queryStr = 'post_status = published : post_type = post : post_slug = ' . $uri . '/';
                                 $this->posts = $parser->parseQuery($this->queryStr);
                                 $this->postCount = count($this->posts);
                             }
                         } else {
                             if ($requestType === 'page') {
                                 if (strpos($uri, '?draft') !== false) {
                                     $uri = ltrim(str_replace('?draft', '', $uri), '/');
                                     $this->queryStr = 'post_status = draft : post_type = page : post_slug = ' . $uri . '/';
                                     $this->posts = $parser->parseQuery($this->queryStr);
                                     $this->postCount = count($this->posts);
                                 } else {
                                     $uri = \Kanso\Utility\Str::GetBeforeLastWord($uri, '/feed');
                                     $uri = ltrim($uri, '/');
                                     $this->queryStr = 'post_status = published : post_type = page : post_slug = ' . $uri . '/';
                                     $this->posts = $parser->parseQuery($this->queryStr);
                                     $this->postCount = count($this->posts);
                                 }
                             } else {
                                 if ($requestType === 'search') {
                                     $perPage = \Kanso\Kanso::getInstance()->Config()['KANSO_POSTS_PER_PAGE'];
                                     $offset = $this->pageIndex * $perPage;
                                     # Get the query
                                     $query = \Kanso\Kanso::getInstance()->Request()->fetch('query');
                                     # Validate the query exts
                                     if (!$query || empty(trim($query))) {
                                         return;
                                     }
                                     # Get the actual search query | sanitize
                                     $query = htmlspecialchars(trim(strtolower(urldecode(\Kanso\Utility\Str::getAfterLastChar($uri, '=')))));
                                     $query = \Kanso\Utility\Str::getBeforeFirstChar($query, '/');
                                     # No need to query empty strings
                                     if (empty($query)) {
                                         return;
                                     }
                                     # Filter the posts
                                     $this->queryStr = "post_status = published : post_type = post : orderBy = post_created, DESC : post_title LIKE {$query} || post_excerpt LIKE {$query} : limit = {$offset}, {$perPage}";
                                     $this->posts = $parser->parseQuery($this->queryStr);
                                     $this->postCount = count($this->posts);
                                     $this->searchQuery = $query;
                                 }
                             }
                         }
                     }
                 }
             }
         }
     }
 }
Example #4
0
 /**
  * Get MIME Type (type/subtype within Content Type header)
  *
  * @return string|false
  */
 public function getContentType()
 {
     if (!headers_sent()) {
         $pathinfo = $this->fetch();
         if (isset($pathinfo['path'])) {
             return $this->extToMime(\Kanso\Utility\Str::getAfterLastChar($pathinfo['path'], '.'));
         }
     }
     return false;
 }