Beispiel #1
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 #2
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 #3
0
 /**
  * Activate an existing user
  * 
  * Activate an existing user based on their account
  * activation token from the database.
  * 
  * @return Kanso\Auth\Helper\User|false
  */
 public function activateUser($username, $email, $password, $token)
 {
     # Get the databse instance
     $Database = \Kanso\Kanso::getInstance()->Database;
     # Get a new Query Builder
     $Query = $Database->Builder();
     # Get the user
     $user = \Kanso\Kanso::getInstance()->Database()->Builder()->SELECT('*')->FROM('users')->WHERE('kanso_register_key', '=', $token)->ROW();
     # Validate the user and is not already activated
     if (!$user || $user['status'] === 'confirmed') {
         return false;
     }
     # Validate the entry email address is same as the requested
     if ($user['email'] !== $email) {
         return false;
     }
     # Validate another user with the same username doeesn't already exist
     $userExists = \Kanso\Kanso::getInstance()->Database()->Builder()->SELECT('*')->FROM('users')->WHERE('username', '=', $username)->ROW();
     if ($userExists) {
         return false;
     }
     # Activate the new user and update their
     # database enties
     $row = [];
     $row['hashed_pass'] = utf8_encode(\Kanso\Security\Encrypt::hash($password));
     $row['slug'] = \Kanso\Utility\Str::slugFilter($username);
     $row['status'] = 'confirmed';
     $row['kanso_register_key'] = '';
     $row['username'] = $username;
     # Update the user's row in the database
     $userRow = $Query->UPDATE('users')->SET($row)->WHERE('id', '=', $user['id'])->QUERY();
     # Validate the user was created
     if (!$userRow) {
         return false;
     }
     # Add the author's slug into Kanso's config
     $this->addAuthorSlug($userRow['slug']);
     # Create array of data for email template
     $website = \Kanso\Kanso::getInstance()->Environment['KANSO_WEBSITE_NAME'];
     $emailData = ['name' => $username, 'username' => $username, 'website' => $website];
     # Remove the password key from the session
     \Kanso\Kanso::getInstance()->Session->remove('session_kanso_register_key');
     # Get the email template
     $msg = \Kanso\Templates\Templater::getTemplate($emailData, 'EmailConfirmNewUser');
     # Send the email
     \Kanso\Utility\Mailer::sendHTMLEmail($userRow['name'], $website, 'no-reply@' . $website, 'Welcome to ' . $website, $msg);
     # Return the user row
     return \Kanso\Kanso::getInstance()->Database()->Builder()->SELECT('*')->FROM('users')->WHERE('id', '=', $user['id'])->ROW();
 }
Beispiel #4
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;
 }