Beispiel #1
0
 /**
  * Gegenerate an ajax Token
  *
  * @return   array 
  */
 public static function generate()
 {
     $key = \Kanso\Utility\Str::generateRandom(100);
     $salt = \Kanso\Utility\Str::generateRandom(21);
     $encryptedKey = self::encrypt($key, $salt);
     $keys = ['key' => $encryptedKey, 'salt' => $salt];
     return $keys;
 }
Beispiel #2
0
 /**
  * Create a tag if it doesn't exist already
  *
  * @param  array   $tags       Array of tag names to be created
  * @return array             
  */
 private function createTags($tags)
 {
     # Get a new Query Builder
     $Query = \Kanso\Kanso::getInstance()->Database->Builder();
     # Set an empty list
     $tagsList = [['id' => 1, 'name' => 'Untagged', 'slug' => 'untagged']];
     if (is_string($tags)) {
         $tags = array_filter(array_map('trim', explode(',', $tags)));
     }
     if (empty($tags)) {
         return $tagsList;
     }
     foreach ($tags as $tag) {
         if (is_array($tag) && isset($tag['name'])) {
             $tag = $tag['name'];
         }
         if (ucfirst($tag) === 'Untagged') {
             continue;
         }
         $tagRow = $Query->SELECT('*')->FROM('tags')->WHERE('name', '=', $tag)->FIND();
         if ($tagRow) {
             $tagsList[] = $tagRow;
         } else {
             $row = ['name' => $tag, 'slug' => \Kanso\Utility\Str::slugFilter($tag)];
             $Query->INSERT_INTO('tags')->VALUES($row)->QUERY();
             $row['id'] = intval(\Kanso\Kanso::getInstance()->Database->lastInsertId());
             $tagsList[] = $row;
         }
     }
     if (count($tagsList) > 1) {
         array_shift($tagsList);
     }
     return $tagsList;
 }
Beispiel #3
0
 /**
  * Get the meta description
  *
  * @return string
  */
 public function the_meta_description()
 {
     $description = $this->website_description();
     if ($this->is_single() || $this->is_page()) {
         $description = $this->post->excerpt;
     } else {
         if ($this->is_search()) {
             $meta_description = 'Search Results for: ' . $this->search_query() . ' - ' . $this->website_title();
         }
     }
     return \Kanso\Utility\Str::reduce($description, 180);
 }
Beispiel #4
0
 /**
  * Publish an existing article
  *
  * @return bool
  */
 private function publishArticle()
 {
     # Validate the user is logged in
     if (!$this->isLoggedIn) {
         return false;
     }
     # Sanitize and validate the POST variables
     $postVars = $this->GUMP->sanitize($this->postVars);
     # Filter and sanitize the POST variables
     $this->GUMP->validation_rules(['type' => 'required|contains,post page']);
     $this->GUMP->filter_rules(['id' => 'sanitize_numbers', 'title' => 'trim|sanitize_string', 'category' => 'trim|sanitize_string', 'tags' => 'trim|sanitize_string', 'type' => 'trim|sanitize_string', 'excerpt' => 'trim|sanitize_string', 'category' => 'trim|sanitize_string', 'thumbnail' => 'trim|sanitize_string']);
     $validated_data = $this->GUMP->run($postVars);
     if (!$validated_data) {
         return false;
     }
     $validated_data['status'] = 'published';
     if (isset($validated_data['id']) && (int) $validated_data['id'] > 0) {
         $newArticle = false;
         $validated_data['id'] = (int) $validated_data['id'];
     } else {
         $newArticle = true;
         $validated_data['id'] = 0;
     }
     $validated_data['comments_enabled'] = \Kanso\Utility\Str::bool($validated_data['comments']);
     $article = $newArticle ? \Kanso\Kanso::getInstance()->Bookkeeper->create() : \Kanso\Kanso::getInstance()->Bookkeeper->existing($validated_data['id']);
     # Get the article content directly from the _POST global
     # so it is not filtered in any way
     if (isset($_POST['content'])) {
         $validated_data['content'] = $_POST['content'];
     }
     foreach ($validated_data as $key => $value) {
         $article->{$key} = $value;
     }
     # save the article
     $save = $article->save();
     if ($save) {
         return ['id' => $article->id, 'slug' => $article->slug];
     }
     return false;
 }
Beispiel #5
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;
 }
Beispiel #6
0
 /**
  * Forgot password
  *
  * @param  string    $username
  * @return boolean
  */
 public function forgotPassword($username)
 {
     # Get a new Query Builder
     $Query = \Kanso\Kanso::getInstance()->Database->Builder();
     # Validate the user exists
     $user = $Query->SELECT('*')->FROM('users')->WHERE('username', '=', $username)->ROW();
     if (!$user) {
         return false;
     }
     # generate a token
     $token = \Kanso\Utility\Str::generateRandom(85, true);
     $Query->UPDATE('users')->SET(['kanso_password_key' => $token])->WHERE('id', '=', $user['id'])->QUERY();
     # Create array of data for email template
     $website = \Kanso\Kanso::getInstance()->Environment['KANSO_WEBSITE_NAME'];
     $emailData = ['name' => $user['name'], 'website' => $website, 'key' => $token];
     # Get the email template
     $msg = \Kanso\Templates\Templater::getTemplate($emailData, 'EmailForgotPassword');
     # Send email
     return \Kanso\Utility\Mailer::sendHTMLEmail($user['email'], $website, 'no-reply@' . $website, 'A reset password request has been made', $msg);
     return false;
 }
Beispiel #7
0
 /**
  * Convert a title to a slug with permalink structure
  *
  * @param  string    $title             The title of the article
  * @param  string    $categorySlug      The category slug
  * @param  string    $authorSlug        The author's slug
  * @param  int       $created           A unix timestamp of when the article was created
  * @param  string    $type              post/page
  * @return string                       The slug to the article             
  */
 private function titleToSlug($title, $categorySlug, $authorSlug, $created, $type)
 {
     if ($type === 'page') {
         return \Kanso\Utility\Str::slugFilter($title) . '/';
     }
     $format = $this->tempConfig['KANSO_PERMALINKS'];
     $dateMap = ['year' => 'Y', 'month' => 'm', 'day' => 'd', 'hour' => 'h', 'minute' => 'i', 'second' => 's'];
     $varMap = ['postname' => \Kanso\Utility\Str::slugFilter($title), 'category' => $categorySlug, 'author' => $authorSlug];
     $slug = '';
     $urlPieces = explode('/', $format);
     foreach ($urlPieces as $key) {
         if (isset($dateMap[$key])) {
             $slug .= date($dateMap[$key], $created) . '/';
         } else {
             if (isset($varMap[$key])) {
                 $slug .= $varMap[$key] . '/';
             }
         }
     }
     return $slug;
 }
Beispiel #8
0
 /**
  * Clear the entire cache or a single file
  *
  * @param  string    $url    A valid permalink wildcard (optional)
  * @return bool
  */
 public function clearCache($url = false)
 {
     if ($url) {
         $name = substr($url, strrpos($url, '/') + 1);
         $name = \Kanso\Utility\Str::slugFilter(preg_replace("/\\..+/", '', $name));
         $file = \Kanso\Kanso::getInstance()->Environment['KANSO_DIR'] . DIRECTORY_SEPARATOR . 'Cache' . DIRECTORY_SEPARATOR . 'Library' . DIRECTORY_SEPARATOR . $name . '.html';
         if (file_exists($file) && is_file($file)) {
             return unlink($file);
         }
     } else {
         $files = glob(\Kanso\Kanso::getInstance()->Environment['KANSO_DIR'] . DIRECTORY_SEPARATOR . 'Cache' . DIRECTORY_SEPARATOR . 'Library' . DIRECTORY_SEPARATOR . '*');
         foreach ($files as $file) {
             if (is_file($file)) {
                 if (!unlink($file)) {
                     return false;
                 }
             }
         }
         return true;
     }
     return false;
 }
Beispiel #9
0
 /**
  * Get the comments for listing in the admin panel
  *
  * @param  $queries    array     POST data from client
  * @param  $filter     string
  * @return array
  */
 public function loadAllComments($queries, $filter)
 {
     # Get the Kanso Query object
     $Query = \Kanso\Kanso::getInstance()->Query();
     # Get the SQL builder
     $SQL = \Kanso\Kanso::getInstance()->Database->Builder();
     $isSearch = $queries['search'] !== 'false';
     $page = (int) $queries['page'] - 1;
     $comments = [];
     $sort = $queries['sortBy'] === 'newest' ? 'DESC' : 'ASC';
     if ($isSearch) {
         $validKeys = ['ip' => 'ip_address', 'status' => 'status', 'user' => 'name', 'email' => 'email'];
         $searchValue = $queries['search'];
         $searchKey = false;
         if (\Kanso\Utility\Str::contains($searchValue, ':')) {
             $value = \Kanso\Utility\Str::getAfterFirstChar($searchValue, ':');
             $key = \Kanso\Utility\Str::getBeforeFirstChar($searchValue, ':');
             $key = isset($validKeys[$key]) ? $validKeys[$key] : false;
             if ($key) {
                 $searchKey = $key;
                 $searchValue = $value;
             }
         }
         if ($searchKey) {
             $comments = $SQL->SELECT('*')->FROM('comments')->WHERE($searchKey, '=', $searchValue);
         } else {
             $comments = $SQL->SELECT('*')->FROM('comments')->WHERE('content', 'LIKE', "%{$searchValue}%");
         }
         if ($filter === 'all') {
             $comments = $comments->ORDER_BY('date', $sort)->FIND_ALL();
         }
         if ($filter === 'approved') {
             $comments = $comments->WHERE('status', '=', 'approved')->ORDER_BY('date', $sort)->FIND_ALL();
         }
         if ($filter === 'spam') {
             $comments = $comments->WHERE('status', '=', 'spam')->ORDER_BY('date', $sort)->FIND_ALL();
         }
         if ($filter === 'pending') {
             $comments = $comments->WHERE('status', '=', 'pending')->ORDER_BY('date', $sort)->FIND_ALL();
         }
         if ($filter === 'deleted') {
             $comments = $comments->WHERE('status', '=', 'deleted')->ORDER_BY('date', $sort)->FIND_ALL();
         }
     } else {
         if ($filter === 'all') {
             $comments = $SQL->SELECT('*')->FROM('comments')->ORDER_BY('date', $sort)->FIND_ALL();
         }
         if ($filter === 'approved') {
             $comments = $SQL->SELECT('*')->FROM('comments')->WHERE('status', '=', 'approved')->ORDER_BY('date', $sort)->FIND_ALL();
         }
         if ($filter === 'spam') {
             $comments = $SQL->SELECT('*')->FROM('comments')->WHERE('status', '=', 'spam')->ORDER_BY('date', $sort)->FIND_ALL();
         }
         if ($filter === 'pending') {
             $comments = $SQL->SELECT('*')->FROM('comments')->WHERE('status', '=', 'pending')->ORDER_BY('date', $sort)->FIND_ALL();
         }
         if ($filter === 'deleted') {
             $comments = $SQL->SELECT('*')->FROM('comments')->WHERE('status', '=', 'deleted')->ORDER_BY('date', $sort)->FIND_ALL();
         }
     }
     foreach ($comments as $key => $comment) {
         $comments[$key]['permalink'] = $Query->the_permalink($comment['post_id']);
         $comments[$key]['title'] = $Query->the_title($comment['post_id']);
         $comments[$key]['avatar'] = $Query->get_avatar($comment['email'], 100, true);
     }
     $comments = \Kanso\Utility\Arr::paginate($comments, $page, 10);
     return $comments;
 }