public function index($param) { // fetch categories & sources $this->view->categories = Fari_Db::select('hierarchy', 'value, slug', array('type' => 'category'), 'slug ASC'); $this->view->sources = Fari_Db::select('hierarchy', 'value, slug', array('type' => 'source'), 'slug ASC'); $this->view->display('search'); }
public function index($param) { // get installed CSS themes $files = Fari_File::listing('/public'); $themes = array(); foreach ($files as $file) { $css = end(explode('/', $file['path'])); // its cheap if ($file['type'] == 'file' && substr($css, -4) == '.css') { $themes[] = substr($css, 0, -4); } } natsort(&$themes); $this->view->themes = $themes; // are we saving changes? if ($_POST) { $css = Fari_Escape::text($_POST['css']); $title = Fari_Escape::text($_POST['title']); Fari_Db::update('settings', array('value' => $css), array('name' => 'theme')); Fari_Db::update('settings', array('value' => $title), array('name' => 'title')); Fari_Message::success('Settings change successful.'); } $this->view->messages = Fari_Message::get(); $this->view->settings = Fari_Db::toKeyValues(Fari_Db::select('settings', 'name, value'), 'name'); $this->view->display('settings'); }
public function _init() { // a listing of articles in the footer $this->view->list = !Fari_User::isAuthenticated('realname') ? Fari_Db::select('articles', 'name, published, slug', array('status' => 1), 'published DESC', BLOG_LIST) : Fari_Db::select('articles', 'name, published, slug', NULL, 'published DESC', BLOG_LIST); // articles archive (no limit on number of articles) $this->view->archive = !Fari_User::isAuthenticated('realname') ? Fari_Db::select('articles', 'name, published, slug', array('status' => 1), 'published DESC') : Fari_Db::select('articles', 'name, published, slug', NULL, 'published DESC'); }
/** * Builds and returns an XML version of a table. * * @param string/array $items Database table we work with or array of data already * @param string $columns Columns to export * @param array $where Where clause in a form array('column' => 'value') * @param string $order Order by clause * @param string $limit Limit by clause * @return string XML backup of the table, headers not set */ public static function toXML($items, $columns = '*', array $where = NULL, $order = NULL, $limit = NULL) { // dom string $DOMDocument = new DOMDocument('1.0', 'UTF-8'); // get items from the database if we are not passing a formed array already if (!is_array($items)) { $items = Fari_Db::select($items, $columns, $where, $order, $limit); } // <table> root $table = $DOMDocument->appendChild($DOMDocument->createElement('table')); // traverse through all records foreach ($items as $item) { // get array keys of the item // we could explode $columns as well if they are passed $keys = array_keys($item); // <table><row> elemenent we will always have $row = $table->appendChild($DOMDocument->createElement('row')); // traverse through keys/columns foreach ($keys as $column) { // <table><row><column> value, escaped $row->appendChild($DOMDocument->createElement($column, Fari_Escape::XML($item[$column]))); } } // generate xml and return $DOMDocument->formatOutput = TRUE; return $DOMDocument->saveXML(); }
/** * A select statement using Fari_Db::select() on itself. * * @param string $columns Columns to return * @param array $where Where clause in a form array('column' => 'value') * @param string $order Order by clause * @param string $limit Limit by clause * @return array Table */ public static function select($columns = '*', $where = NULL, $id = NULL, $order = NULL, $limit = NULL) { try { // get table name $tableName = strtolower(self::_getChildClassName()); return Fari_Db::select($tableName, $columns, $where, $order, $limit); } catch (Fari_Exception $exception) { $exception->fire(); } }
/** * Builds and returns an RSS feed (check data on db insert!). * * @param string $feedTitle Title of the feed * @param string $feedURL Link to the feed * @param string $feedDescription Description of this feed * @param string $items Database table * @param boolean $isDateInRSS Set to TRUE if dates in tn the $items table are already in RSS format * @return string RSS Feed */ public function create($feedTitle, $feedURL, $feedDescription, $items, $isDateInRSS = FALSE) { // escape input $feedTitle = Fari_Escape::XML($feedTitle); $feedURL = Fari_Escape::XML($feedURL); $feedDescription = Fari_Escape::XML($feedDescription); // set publishing date in RSS format $feedPublished = date(DATE_RSS); // start dom string $DOMDocument = new DOMDocument('1.0', 'UTF-8'); // form columns, we will use the info when traversing articles (and on the line below) $columns = $this->articleTitle . ', ' . $this->articleLink . ', ' . $this->articleDescription . ', ' . $this->articleDate; // get items from the database if we are not passing a formed array already if (!is_array($items)) { $items = Fari_Db::select($items, $columns); } // <rss> $rootNode = $DOMDocument->createElement('rss'); // use RSS version 2.0 attribute $rootNode->setAttribute('version', '2.0'); $DOMDocument->appendChild($rootNode); // <rss><channel> $channel = $rootNode->appendChild($DOMDocument->createElement('channel')); // create the header // <rss><channel><title> $channel->appendChild($DOMDocument->createElement('title', $feedTitle)); // <rss><channel><link> $channel->appendChild($DOMDocument->createElement('link', $feedURL)); // <rss><channel><description> $channel->appendChild($DOMDocument->createElement('description', $feedDescription)); // <rss><channel><pubDate> $channel->appendChild($DOMDocument->createElement('pubDate', $feedPublished)); // column to RSS form 'conversion', elements have to follow that order... $articleColumns = explode(', ', $columns); $RSSColumns = array('title', 'link', 'description', 'pubDate'); // traverse items now foreach ($items as $article) { // <rss><channel><item> $articleNode = $channel->appendChild($DOMDocument->createElement('item')); // traverse the items array consisting of 4 elements for ($i = 0; $i < 4; $i++) { // <rss><channel><item><$column> // <$column> value, escaped $columnText = Fari_Escape::XML($article[$articleColumns[$i]]); // do we need to fix RSS pubDate? if ($RSSColumns[$i] == 'pubDate' && !$isDateInRSS) { $columnText = Fari_Format::date($columnText, 'RSS'); } $articleNode->appendChild($DOMDocument->createElement($RSSColumns[$i], $columnText)); } } // generate XML and return $DOMDocument->formatOutput = TRUE; return $DOMDocument->saveXML(); }
public static function query($query) { // explode the query by space forming an array of searched for words $query = explode(' ', strtolower($query)); // form an SQL LIKE $like = ''; foreach ($query as $word) { $like .= "stems LIKE '%{$word}%' OR titleStems LIKE '%{$word}%' OR tags LIKE '%{$word}%' OR source LIKE '%{$word}%'\n OR category LIKE '%{$word}%' OR type LIKE '%{$word}%' OR comments LIKE '%{$word}%' OR text LIKE '%{$word}%'\n OR "; } $like = substr($like, 0, -4); // leave out the trailing ' OR ' // fetch the text $result = Fari_Db::select('kb', '*', "({$like})"); return self::relevance($query, $result); }
public static function getArchive($month, $isAuthenticated) { // escape $month = Fari_Escape::text($month); // parse month and year passed list($month, $year) = explode('-', $month); $months = array('january', 'february', 'march', 'april', 'may', 'june', 'july', 'august', 'september', 'october', 'november', 'december'); $monthPosition = array_search($month, $months) + 1; if (!empty($monthPosition)) { // we have ourselves the month number $low = mktime(1, 1, 1, $monthPosition, 1, $year); $high = mktime(23, 59, 59, $monthPosition, date('t', $low), $year); return !$isAuthenticated ? Fari_Db::select('articles', '*', "published >= '{$low}' AND published <= '{$high}' AND status = 1", 'published DESC') : Fari_Db::select('articles', '*', "published >= '{$low}' AND published <= '{$high}' AND status != 2", 'published DESC'); } return; }
public function sitemap() { $sitemap = new Fari_Sitemap('slug', 'published'); $articles = Fari_Db::select('articles', 'slug, published', array('status' => 1)); echo $sitemap->create($articles, '/blog/article/'); }
public function _init() { $this->view->settings = Fari_Db::toKeyValues(Fari_Db::select('settings', 'name, value'), 'name'); }
/** * Calculate the total number of items in a query. * * @param string $table Database table we work with * @param string/array $where WHERE $where = $id * @return int Items total count */ private function getItemsTotal($table, $where = NULL) { // count total $array = Fari_Db::select($table, "COUNT(*) AS total", $where); // why this way? to reuse select() easily return $array[0]['total']; }
public function index($param) { // are we saving? if ($_POST) { $success = TRUE; // save categories, sources & types $category = Fari_Escape::text($_POST['category']); $categorySlug = Fari_Escape::slug($category); $source = Fari_Escape::text($_POST['source']); $sourceSlug = Fari_Escape::slug($source); $type = Fari_Escape::text($_POST['type']); $typeSlug = Fari_Escape::slug($type); if (empty($category)) { Fari_Message::fail('The category can\'t be empty.'); $success = FALSE; } else { $result = Fari_Db::selectRow('hierarchy', 'key', array('value' => $category, 'type' => 'category')); if (empty($result)) { Fari_Db::insert('hierarchy', array('value' => $category, 'slug' => $categorySlug, 'type' => 'category')); } } if (empty($source)) { Fari_Message::fail('The source can\'t be empty.'); $success = FALSE; } else { $result = Fari_Db::selectRow('hierarchy', 'key', array('value' => $source, 'type' => 'source')); if (empty($result)) { Fari_Db::insert('hierarchy', array('value' => $source, 'slug' => $sourceSlug, 'type' => 'source')); } } if (empty($type)) { Fari_Message::fail('The category can\'t be empty.'); $success = FALSE; } else { $result = Fari_Db::selectRow('hierarchy', 'key', array('value' => $type, 'type' => 'type')); if (empty($result)) { Fari_Db::insert('hierarchy', array('value' => $type, 'type' => 'type')); } } if ($success) { $title = Fari_Escape::text($_POST['title']); if (empty($title)) { Fari_Message::fail('The title can\'t be empty.'); } else { $slug = Fari_Escape::slug($_POST['title']); // unique slug/title $result = Fari_Db::selectRow('kb', 'id', array('slug' => $slug)); if (!empty($result)) { Fari_Message::fail('The title is not unique.'); } else { $text = Fari_Escape::quotes($_POST['textarea']); // convert title & main text to its stems and add lowercase originals better matches) $titleStems = Knowledge::stems($title) . ' ' . strtolower($title); $stems = Knowledge::stems($text) . ' ' . strtolower($text); $tags = Fari_Escape::text($_POST['tags']); $category = Fari_Escape::text($_POST['category']); $source = Fari_Escape::text($_POST['source']); $type = Fari_Escape::text($_POST['type']); $comments = Fari_Escape::text($_POST['comments']); $date = Fari_Escape::text($_POST['date']); // date if (!Fari_Filter::isDate($date)) { Fari_Message::fail('The date is not in the correct format.'); } else { // INSERT Fari_Db::insert('kb', array('title' => $title, 'slug' => $slug, 'text' => $text, 'tags' => $tags, 'category' => $category, 'categorySlug' => $categorySlug, 'source' => $source, 'sourceSlug' => $sourceSlug, 'type' => $type, 'stems' => $stems, 'comments' => $comments, 'date' => $date, 'titleStems' => $titleStems, 'starred' => 'empty')); Fari_Message::success('Saved successfully.'); $this->redirect('/text/edit/' . $slug); die; } } } } } // fetch categories, sources & types $this->view->categories = $categories = Fari_Db::select('hierarchy', 'key, value', array('type' => 'category'), 'slug ASC'); $this->view->sources = $sources = Fari_Db::select('hierarchy', 'key, value', array('type' => 'source'), 'slug ASC'); $this->view->types = $types = Fari_Db::select('hierarchy', 'key, value', array('type' => 'type'), 'value ASC'); // form if save failed... $this->view->saved = $_POST; // get all messages $this->view->messages = Fari_Message::get(); $this->view->display('new'); }
/** * Builds and returns an XML sitemap. * @uses date in standard db form, W3C Datetime (YYYY-MM-DD) * * @param string/array $items Database table we work with or array of data already * @param string $linksURL URL to append slug links to (e.g., http://.$_SERVER['HTTP_HOST'].WWW_DIR.Controller) * @return XML sitemap */ public function create($items, $linksURL = NULL) { // try determining this server's address if URL is not provided if (!isset($linksURL)) { $linksURL = 'http://' . $_SERVER['SERVER_NAME'] . WWW_DIR; } // add a trailing slash to URL $linksURL = Fari_File::addTrailingSlash($linksURL); // start dom string $DOMDocument = new DOMDocument('1.0', 'UTF-8'); // <urlset> root $rootNode = $DOMDocument->appendChild($DOMDocument->createElementNS('http://www.sitemaps.org/schemas/sitemap/0.9', 'urlset')); // get items from the database if we are not passing a formed array already if (!is_array($items)) { // last modification date and page priority won't be provided if (!isset($this->lastModificationDate) && !isset($this->pagePriority)) { $columns = $this->linkSlug; // last modification date won't be provided } elseif (!isset($this->lastModificationDate)) { $columns = $this->linkSlug . ', ' . $this->pagePriority; // page priority won't be provided } elseif (!isset($this->pagePriority)) { $columns = $this->linkSlug . ', ' . $this->lastModificationDate; // we will be provided with all params } else { $columns = $this->linkSlug . ',' . $this->lastModificationDate . ',' . $this->pagePriority; } // the actual call to the db $items = Fari_Db::select($items, $columns); } // set default element text, page priority $pagePriorityText = self::LINK_PRIORITY; // set default element text, generate last modification date as now $lastModificationText = date('Y-m-d'); // traverse through all records foreach ($items as $item) { // <urlset><url> $URLNode = $rootNode->appendChild($DOMDocument->createElement('url')); // <urlset><url><loc> link address $URLNode->appendChild($DOMDocument->createElement('loc', $linksURL . $item[$this->linkSlug])); // <urlset><url><lastmod> last modification date of the page if (isset($this->lastModificationDate)) { $lastModificationText = $item[$this->lastModificationDate]; // convert UNIX timestamp to well formed date if present if (strlen($lastModificationText) == 10 && $lastModificationText > 1000000000) { $lastModificationText = date('Y-m-d', $lastModificationText); } } $URLNode->appendChild($DOMDocument->createElement('lastmod', $lastModificationText)); // <urlset><url><priority> page priority if (isset($this->pagePriority)) { $pagePriorityText = $item[$this->pagePriority]; } $URLNode->appendChild($DOMDocument->createElement('priority', $pagePriorityText)); } // generate XML and return $DOMDocument->formatOutput = TRUE; return $DOMDocument->saveXML(); }
public function edit($slug) { $slug = Fari_Escape::text($slug); // are we saving? if ($_POST) { $success = TRUE; // save categories, sources & types $category = Fari_Escape::text($_POST['category']); $categorySlug = Fari_Escape::slug($category); $source = Fari_Escape::text($_POST['source']); $sourceSlug = Fari_Escape::slug($source); $type = Fari_Escape::text($_POST['type']); $typeSlug = Fari_Escape::slug($type); if (empty($category)) { Fari_Message::fail('The category can\'t be empty.'); $success = FALSE; } else { $result = Fari_Db::selectRow('hierarchy', 'key', array('value' => $category, 'type' => 'category')); if (empty($result)) { Fari_Db::insert('hierarchy', array('value' => $category, 'slug' => $categorySlug, 'type' => 'category')); } } if (empty($source)) { Fari_Message::fail('The source can\'t be empty.'); $success = FALSE; } else { $result = Fari_Db::selectRow('hierarchy', 'key', array('value' => $source, 'type' => 'source')); if (empty($result)) { Fari_Db::insert('hierarchy', array('value' => $source, 'slug' => $sourceSlug, 'type' => 'source')); } } if (empty($type)) { Fari_Message::fail('The category can\'t be empty.'); $success = FALSE; } else { $result = Fari_Db::selectRow('hierarchy', 'key', array('value' => $type, 'type' => 'type')); if (empty($result)) { Fari_Db::insert('hierarchy', array('value' => $type, 'type' => 'type')); } } if ($success) { $text = Fari_Escape::quotes($_POST['textarea']); // convert main text to stems & add the lowercase original to it (better matches) $stems = Knowledge::stems($text) . ' ' . strtolower($text); $tags = Fari_Escape::text($_POST['tags']); $category = Fari_Escape::text($_POST['category']); $source = Fari_Escape::text($_POST['source']); $type = Fari_Escape::text($_POST['type']); $comments = Fari_Escape::text($_POST['comments']); $date = Fari_Escape::text($_POST['date']); // date if (!Fari_Filter::isDate($date)) { Fari_Message::fail('The date is not in the correct format.'); } else { // INSERT Fari_Db::update('kb', array('text' => $text, 'comments' => $comments, 'date' => $date, 'tags' => $tags, 'category' => $category, 'categorySlug' => $categorySlug, 'source' => $source, 'sourceSlug' => $sourceSlug, 'type' => $type, 'stems' => $stems), array('slug' => $slug)); Fari_Message::success('Saved successfully.'); } } } // fetch categories, sources & types $this->view->categories = $categories = Fari_Db::select('hierarchy', 'key, value', array('type' => 'category'), 'slug ASC'); $this->view->sources = $sources = Fari_Db::select('hierarchy', 'key, value', array('type' => 'source'), 'slug ASC'); $this->view->types = $types = Fari_Db::select('hierarchy', 'key, value', array('type' => 'type'), 'value ASC'); // form $saved = Fari_Db::selectRow('kb', '*', array('slug' => $slug)); $saved['textarea'] = $saved['text']; // for reuse... $this->view->saved = $saved; // get all messages $this->view->messages = Fari_Message::get(); $this->view->display('edit'); }