summaryrefslogtreecommitdiff
path: root/app/Models
diff options
context:
space:
mode:
Diffstat (limited to 'app/Models')
-rw-r--r--app/Models/Category.php85
-rw-r--r--app/Models/CategoryDAO.php251
-rw-r--r--app/Models/Configuration.php247
-rw-r--r--app/Models/Days.php7
-rw-r--r--app/Models/Entry.php186
-rw-r--r--app/Models/EntryDAO.php472
-rw-r--r--app/Models/Feed.php274
-rw-r--r--app/Models/FeedDAO.php345
-rw-r--r--app/Models/Log.php26
-rw-r--r--app/Models/LogDAO.php25
-rw-r--r--app/Models/StatsDAO.php205
-rw-r--r--app/Models/Themes.php106
-rw-r--r--app/Models/UserDAO.php36
13 files changed, 2265 insertions, 0 deletions
diff --git a/app/Models/Category.php b/app/Models/Category.php
new file mode 100644
index 000000000..8e1e44ef8
--- /dev/null
+++ b/app/Models/Category.php
@@ -0,0 +1,85 @@
+<?php
+
+class FreshRSS_Category extends Minz_Model {
+ private $id = 0;
+ private $name;
+ private $color;
+ private $nbFeed = -1;
+ private $nbNotRead = -1;
+ private $feeds = null;
+
+ public function __construct ($name = '', $color = '#0062BE', $feeds = null) {
+ $this->_name ($name);
+ $this->_color ($color);
+ if (isset ($feeds)) {
+ $this->_feeds ($feeds);
+ $this->nbFeed = 0;
+ $this->nbNotRead = 0;
+ foreach ($feeds as $feed) {
+ $this->nbFeed++;
+ $this->nbNotRead += $feed->nbNotRead ();
+ }
+ }
+ }
+
+ public function id () {
+ return $this->id;
+ }
+ public function name () {
+ return $this->name;
+ }
+ public function color () {
+ return $this->color;
+ }
+ public function nbFeed () {
+ if ($this->nbFeed < 0) {
+ $catDAO = new FreshRSS_CategoryDAO ();
+ $this->nbFeed = $catDAO->countFeed ($this->id ());
+ }
+
+ return $this->nbFeed;
+ }
+ public function nbNotRead () {
+ if ($this->nbNotRead < 0) {
+ $catDAO = new FreshRSS_CategoryDAO ();
+ $this->nbNotRead = $catDAO->countNotRead ($this->id ());
+ }
+
+ return $this->nbNotRead;
+ }
+ public function feeds () {
+ if ($this->feeds === null) {
+ $feedDAO = new FreshRSS_FeedDAO ();
+ $this->feeds = $feedDAO->listByCategory ($this->id ());
+ $this->nbFeed = 0;
+ $this->nbNotRead = 0;
+ foreach ($this->feeds as $feed) {
+ $this->nbFeed++;
+ $this->nbNotRead += $feed->nbNotRead ();
+ }
+ }
+
+ return $this->feeds;
+ }
+
+ public function _id ($value) {
+ $this->id = $value;
+ }
+ public function _name ($value) {
+ $this->name = $value;
+ }
+ public function _color ($value) {
+ if (preg_match ('/^#([0-9a-f]{3}|[0-9a-f]{6})$/i', $value)) {
+ $this->color = $value;
+ } else {
+ $this->color = '#0062BE';
+ }
+ }
+ public function _feeds ($values) {
+ if (!is_array ($values)) {
+ $values = array ($values);
+ }
+
+ $this->feeds = $values;
+ }
+}
diff --git a/app/Models/CategoryDAO.php b/app/Models/CategoryDAO.php
new file mode 100644
index 000000000..1cc616ac0
--- /dev/null
+++ b/app/Models/CategoryDAO.php
@@ -0,0 +1,251 @@
+<?php
+
+class FreshRSS_CategoryDAO extends Minz_ModelPdo {
+ public function addCategory ($valuesTmp) {
+ $sql = 'INSERT INTO `' . $this->prefix . 'category` (name, color) VALUES(?, ?)';
+ $stm = $this->bd->prepare ($sql);
+
+ $values = array (
+ substr($valuesTmp['name'], 0, 255),
+ substr($valuesTmp['color'], 0, 7),
+ );
+
+ if ($stm && $stm->execute ($values)) {
+ return $this->bd->lastInsertId();
+ } else {
+ $info = $stm->errorInfo();
+ Minz_Log::record ('SQL error : ' . $info[2], Minz_Log::ERROR);
+ return false;
+ }
+ }
+
+ public function updateCategory ($id, $valuesTmp) {
+ $sql = 'UPDATE `' . $this->prefix . 'category` SET name=?, color=? WHERE id=?';
+ $stm = $this->bd->prepare ($sql);
+
+ $values = array (
+ $valuesTmp['name'],
+ $valuesTmp['color'],
+ $id
+ );
+
+ if ($stm && $stm->execute ($values)) {
+ return $stm->rowCount();
+ } else {
+ $info = $stm->errorInfo();
+ Minz_Log::record ('SQL error : ' . $info[2], Minz_Log::ERROR);
+ return false;
+ }
+ }
+
+ public function deleteCategory ($id) {
+ $sql = 'DELETE FROM `' . $this->prefix . 'category` WHERE id=?';
+ $stm = $this->bd->prepare ($sql);
+
+ $values = array ($id);
+
+ if ($stm && $stm->execute ($values)) {
+ return $stm->rowCount();
+ } else {
+ $info = $stm->errorInfo();
+ Minz_Log::record ('SQL error : ' . $info[2], Minz_Log::ERROR);
+ return false;
+ }
+ }
+
+ public function searchById ($id) {
+ $sql = 'SELECT * FROM `' . $this->prefix . 'category` WHERE id=?';
+ $stm = $this->bd->prepare ($sql);
+
+ $values = array ($id);
+
+ $stm->execute ($values);
+ $res = $stm->fetchAll (PDO::FETCH_ASSOC);
+ $cat = self::daoToCategory ($res);
+
+ if (isset ($cat[0])) {
+ return $cat[0];
+ } else {
+ return false;
+ }
+ }
+ public function searchByName ($name) {
+ $sql = 'SELECT * FROM `' . $this->prefix . 'category` WHERE name=?';
+ $stm = $this->bd->prepare ($sql);
+
+ $values = array ($name);
+
+ $stm->execute ($values);
+ $res = $stm->fetchAll (PDO::FETCH_ASSOC);
+ $cat = self::daoToCategory ($res);
+
+ if (isset ($cat[0])) {
+ return $cat[0];
+ } else {
+ return false;
+ }
+ }
+
+ public function listCategories ($prePopulateFeeds = true, $details = false) {
+ if ($prePopulateFeeds) {
+ $sql = 'SELECT c.id AS c_id, c.name AS c_name, '
+ . ($details ? 'c.color AS c_color, ' : '')
+ . ($details ? 'f.* ' : 'f.id, f.name, f.url, f.website, f.priority, f.error, f.cache_nbEntries, f.cache_nbUnreads ')
+ . 'FROM `' . $this->prefix . 'category` c '
+ . 'LEFT OUTER JOIN `' . $this->prefix . 'feed` f ON f.category = c.id '
+ . 'GROUP BY f.id '
+ . 'ORDER BY c.name, f.name';
+ $stm = $this->bd->prepare ($sql);
+ $stm->execute ();
+ return self::daoToCategoryPrepopulated ($stm->fetchAll (PDO::FETCH_ASSOC));
+ } else {
+ $sql = 'SELECT * FROM `' . $this->prefix . 'category` ORDER BY name';
+ $stm = $this->bd->prepare ($sql);
+ $stm->execute ();
+ return self::daoToCategory ($stm->fetchAll (PDO::FETCH_ASSOC));
+ }
+ }
+
+ public function getDefault () {
+ $sql = 'SELECT * FROM `' . $this->prefix . 'category` WHERE id=1';
+ $stm = $this->bd->prepare ($sql);
+
+ $stm->execute ();
+ $res = $stm->fetchAll (PDO::FETCH_ASSOC);
+ $cat = self::daoToCategory ($res);
+
+ if (isset ($cat[0])) {
+ return $cat[0];
+ } else {
+ return false;
+ }
+ }
+ public function checkDefault () {
+ $def_cat = $this->searchById (1);
+
+ if ($def_cat === false) {
+ $cat = new FreshRSS_Category (Minz_Translate::t ('default_category'));
+ $cat->_id (1);
+
+ $values = array (
+ 'id' => $cat->id (),
+ 'name' => $cat->name (),
+ 'color' => $cat->color ()
+ );
+
+ $this->addCategory ($values);
+ }
+ }
+
+ public function count () {
+ $sql = 'SELECT COUNT(*) AS count FROM `' . $this->prefix . 'category`';
+ $stm = $this->bd->prepare ($sql);
+ $stm->execute ();
+ $res = $stm->fetchAll (PDO::FETCH_ASSOC);
+
+ return $res[0]['count'];
+ }
+
+ public function countFeed ($id) {
+ $sql = 'SELECT COUNT(*) AS count FROM `' . $this->prefix . 'feed` WHERE category=?';
+ $stm = $this->bd->prepare ($sql);
+ $values = array ($id);
+ $stm->execute ($values);
+ $res = $stm->fetchAll (PDO::FETCH_ASSOC);
+
+ return $res[0]['count'];
+ }
+
+ public function countNotRead ($id) {
+ $sql = 'SELECT COUNT(*) AS count FROM `' . $this->prefix . 'entry` e INNER JOIN `' . $this->prefix . 'feed` f ON e.id_feed = f.id WHERE category=? AND e.is_read=0';
+ $stm = $this->bd->prepare ($sql);
+ $values = array ($id);
+ $stm->execute ($values);
+ $res = $stm->fetchAll (PDO::FETCH_ASSOC);
+
+ return $res[0]['count'];
+ }
+
+ public static function findFeed($categories, $feed_id) {
+ foreach ($categories as $category) {
+ foreach ($category->feeds () as $feed) {
+ if ($feed->id () === $feed_id) {
+ return $feed;
+ }
+ }
+ }
+ return null;
+ }
+
+ public static function CountUnreads($categories, $minPriority = 0) {
+ $n = 0;
+ foreach ($categories as $category) {
+ foreach ($category->feeds () as $feed) {
+ if ($feed->priority () >= $minPriority) {
+ $n += $feed->nbNotRead();
+ }
+ }
+ }
+ return $n;
+ }
+
+ public static function daoToCategoryPrepopulated ($listDAO) {
+ $list = array ();
+
+ if (!is_array ($listDAO)) {
+ $listDAO = array ($listDAO);
+ }
+
+ $previousLine = null;
+ $feedsDao = array();
+ foreach ($listDAO as $line) {
+ if ($previousLine['c_id'] != null && $line['c_id'] !== $previousLine['c_id']) {
+ // End of the current category, we add it to the $list
+ $cat = new FreshRSS_Category (
+ $previousLine['c_name'],
+ isset($previousLine['c_color']) ? $previousLine['c_color'] : '',
+ FreshRSS_FeedDAO::daoToFeed ($feedsDao, $previousLine['c_id'])
+ );
+ $cat->_id ($previousLine['c_id']);
+ $list[$previousLine['c_id']] = $cat;
+
+ $feedsDao = array(); //Prepare for next category
+ }
+
+ $previousLine = $line;
+ $feedsDao[] = $line;
+ }
+
+ // add the last category
+ if ($previousLine != null) {
+ $cat = new FreshRSS_Category (
+ $previousLine['c_name'],
+ isset($previousLine['c_color']) ? $previousLine['c_color'] : '',
+ FreshRSS_FeedDAO::daoToFeed ($feedsDao, $previousLine['c_id'])
+ );
+ $cat->_id ($previousLine['c_id']);
+ $list[$previousLine['c_id']] = $cat;
+ }
+
+ return $list;
+ }
+
+ public static function daoToCategory ($listDAO) {
+ $list = array ();
+
+ if (!is_array ($listDAO)) {
+ $listDAO = array ($listDAO);
+ }
+
+ foreach ($listDAO as $key => $dao) {
+ $cat = new FreshRSS_Category (
+ $dao['name'],
+ $dao['color']
+ );
+ $cat->_id ($dao['id']);
+ $list[$key] = $cat;
+ }
+
+ return $list;
+ }
+}
diff --git a/app/Models/Configuration.php b/app/Models/Configuration.php
new file mode 100644
index 000000000..2a7fe95aa
--- /dev/null
+++ b/app/Models/Configuration.php
@@ -0,0 +1,247 @@
+<?php
+
+class FreshRSS_Configuration {
+ private $filename;
+
+ private $data = array(
+ 'language' => 'en',
+ 'old_entries' => 3,
+ 'keep_history_default' => 0,
+ 'mail_login' => '',
+ 'token' => '',
+ 'passwordHash' => '', //CRYPT_BLOWFISH
+ 'posts_per_page' => 20,
+ 'view_mode' => 'normal',
+ 'default_view' => 'not_read',
+ 'auto_load_more' => true,
+ 'display_posts' => false,
+ 'onread_jump_next' => true,
+ 'lazyload' => true,
+ 'sort_order' => 'DESC',
+ 'anon_access' => false,
+ 'mark_when' => array(
+ 'article' => true,
+ 'site' => true,
+ 'scroll' => false,
+ 'reception' => false,
+ ),
+ 'theme' => 'Origine',
+ 'shortcuts' => array(
+ 'mark_read' => 'r',
+ 'mark_favorite' => 'f',
+ 'go_website' => 'space',
+ 'next_entry' => 'j',
+ 'prev_entry' => 'k',
+ 'collapse_entry' => 'c',
+ 'load_more' => 'm',
+ 'auto_share' => 's',
+ ),
+ 'topline_read' => true,
+ 'topline_favorite' => true,
+ 'topline_date' => true,
+ 'topline_link' => true,
+ 'bottomline_read' => true,
+ 'bottomline_favorite' => true,
+ 'bottomline_sharing' => true,
+ 'bottomline_tags' => true,
+ 'bottomline_date' => true,
+ 'bottomline_link' => true,
+ 'sharing' => array(
+ 'shaarli' => '',
+ 'wallabag' => '',
+ 'diaspora' => '',
+ 'twitter' => true,
+ 'g+' => true,
+ 'facebook' => true,
+ 'email' => true,
+ 'print' => true,
+ ),
+ );
+
+ private $available_languages = array(
+ 'en' => 'English',
+ 'fr' => 'Français',
+ );
+
+ public function __construct ($user) {
+ $this->filename = DATA_PATH . '/' . $user . '_user.php';
+
+ $data = @include($this->filename);
+ if (!is_array($data)) {
+ throw new Minz_PermissionDeniedException($this->filename);
+ }
+
+ foreach ($data as $key => $value) {
+ if (isset($this->data[$key])) {
+ $function = '_' . $key;
+ $this->$function($value);
+ }
+ }
+ $this->data['user'] = $user;
+ }
+
+ public function save() {
+ @rename($this->filename, $this->filename . '.bak.php');
+ if (file_put_contents($this->filename, "<?php\n return " . var_export($this->data, true) . ';', LOCK_EX) === false) {
+ throw new Minz_PermissionDeniedException($this->filename);
+ }
+ if (function_exists('opcache_invalidate')) {
+ opcache_invalidate($this->filename); //Clear PHP 5.5+ cache for include
+ }
+ invalidateHttpCache();
+ return true;
+ }
+
+ public function __get($name) {
+ if (array_key_exists($name, $this->data)) {
+ return $this->data[$name];
+ } else {
+ $trace = debug_backtrace();
+ trigger_error('Undefined FreshRSS_Configuration->' . $name . 'in ' . $trace[0]['file'] . ' line ' . $trace[0]['line'], E_USER_NOTICE); //TODO: Use Minz exceptions
+ return null;
+ }
+ }
+
+ public function sharing($key = false) {
+ if ($key === false) {
+ return $this->data['sharing'];
+ }
+ if (isset($this->data['sharing'][$key])) {
+ return $this->data['sharing'][$key];
+ }
+ return false;
+ }
+
+ public function availableLanguages() {
+ return $this->available_languages;
+ }
+
+ public function _language($value) {
+ if (!isset($this->available_languages[$value])) {
+ $value = 'en';
+ }
+ $this->data['language'] = $value;
+ }
+ public function _posts_per_page ($value) {
+ $value = intval($value);
+ $this->data['posts_per_page'] = $value > 0 ? $value : 10;
+ }
+ public function _view_mode ($value) {
+ if ($value === 'global' || $value === 'reader') {
+ $this->data['view_mode'] = $value;
+ } else {
+ $this->data['view_mode'] = 'normal';
+ }
+ }
+ public function _default_view ($value) {
+ $this->data['default_view'] = $value === 'all' ? 'all' : 'not_read';
+ }
+ public function _display_posts ($value) {
+ $this->data['display_posts'] = ((bool)$value) && $value !== 'no';
+ }
+ public function _onread_jump_next ($value) {
+ $this->data['onread_jump_next'] = ((bool)$value) && $value !== 'no';
+ }
+ public function _lazyload ($value) {
+ $this->data['lazyload'] = ((bool)$value) && $value !== 'no';
+ }
+ public function _sort_order ($value) {
+ $this->data['sort_order'] = $value === 'ASC' ? 'ASC' : 'DESC';
+ }
+ public function _old_entries($value) {
+ $value = intval($value);
+ $this->data['old_entries'] = $value > 0 ? $value : 3;
+ }
+ public function _keep_history_default($value) {
+ $value = intval($value);
+ $this->data['keep_history_default'] = $value >= -1 ? $value : 0;
+ }
+ public function _shortcuts ($values) {
+ foreach ($values as $key => $value) {
+ if (isset($this->data['shortcuts'][$key])) {
+ $this->data['shortcuts'][$key] = $value;
+ }
+ }
+ }
+ public function _passwordHash ($value) {
+ $this->data['passwordHash'] = ctype_graph($value) && (strlen($value) >= 60) ? $value : '';
+ }
+ public function _mail_login ($value) {
+ $value = filter_var($value, FILTER_VALIDATE_EMAIL);
+ if ($value) {
+ $this->data['mail_login'] = $value;
+ } else {
+ $this->data['mail_login'] = '';
+ }
+ }
+ public function _anon_access ($value) {
+ $this->data['anon_access'] = ((bool)$value) && $value !== 'no';
+ }
+ public function _mark_when ($values) {
+ foreach ($values as $key => $value) {
+ if (isset($this->data['mark_when'][$key])) {
+ $this->data['mark_when'][$key] = ((bool)$value) && $value !== 'no';
+ }
+ }
+ }
+ public function _sharing ($values) {
+ $are_url = array ('shaarli', 'wallabag', 'diaspora');
+ foreach ($values as $key => $value) {
+ if (in_array($key, $are_url)) {
+ $is_url = (
+ filter_var ($value, FILTER_VALIDATE_URL) ||
+ (version_compare(PHP_VERSION, '5.3.3', '<') &&
+ (strpos($value, '-') > 0) &&
+ ($value === filter_var($value, FILTER_SANITIZE_URL)))
+ ); //PHP bug #51192
+
+ if (!$is_url) {
+ $value = '';
+ }
+ } elseif (!is_bool($value)) {
+ $value = true;
+ }
+
+ $this->data['sharing'][$key] = $value;
+ }
+ }
+ public function _theme($value) {
+ $this->data['theme'] = $value;
+ }
+ public function _token($value) {
+ $this->data['token'] = $value;
+ }
+ public function _auto_load_more($value) {
+ $this->data['auto_load_more'] = ((bool)$value) && $value !== 'no';
+ }
+ public function _topline_read($value) {
+ $this->data['topline_read'] = ((bool)$value) && $value !== 'no';
+ }
+ public function _topline_favorite($value) {
+ $this->data['topline_favorite'] = ((bool)$value) && $value !== 'no';
+ }
+ public function _topline_date($value) {
+ $this->data['topline_date'] = ((bool)$value) && $value !== 'no';
+ }
+ public function _topline_link($value) {
+ $this->data['topline_link'] = ((bool)$value) && $value !== 'no';
+ }
+ public function _bottomline_read($value) {
+ $this->data['bottomline_read'] = ((bool)$value) && $value !== 'no';
+ }
+ public function _bottomline_favorite($value) {
+ $this->data['bottomline_favorite'] = ((bool)$value) && $value !== 'no';
+ }
+ public function _bottomline_sharing($value) {
+ $this->data['bottomline_sharing'] = ((bool)$value) && $value !== 'no';
+ }
+ public function _bottomline_tags($value) {
+ $this->data['bottomline_tags'] = ((bool)$value) && $value !== 'no';
+ }
+ public function _bottomline_date($value) {
+ $this->data['bottomline_date'] = ((bool)$value) && $value !== 'no';
+ }
+ public function _bottomline_link($value) {
+ $this->data['bottomline_link'] = ((bool)$value) && $value !== 'no';
+ }
+}
diff --git a/app/Models/Days.php b/app/Models/Days.php
new file mode 100644
index 000000000..2d770c30b
--- /dev/null
+++ b/app/Models/Days.php
@@ -0,0 +1,7 @@
+<?php
+
+class FreshRSS_Days {
+ const TODAY = 0;
+ const YESTERDAY = 1;
+ const BEFORE_YESTERDAY = 2;
+}
diff --git a/app/Models/Entry.php b/app/Models/Entry.php
new file mode 100644
index 000000000..a6c67221b
--- /dev/null
+++ b/app/Models/Entry.php
@@ -0,0 +1,186 @@
+<?php
+
+class FreshRSS_Entry extends Minz_Model {
+
+ private $id = 0;
+ private $guid;
+ private $title;
+ private $author;
+ private $content;
+ private $link;
+ private $date;
+ private $is_read;
+ private $is_favorite;
+ private $feed;
+ private $tags;
+
+ public function __construct ($feed = '', $guid = '', $title = '', $author = '', $content = '',
+ $link = '', $pubdate = 0, $is_read = false, $is_favorite = false, $tags = '') {
+ $this->_guid ($guid);
+ $this->_title ($title);
+ $this->_author ($author);
+ $this->_content ($content);
+ $this->_link ($link);
+ $this->_date ($pubdate);
+ $this->_isRead ($is_read);
+ $this->_isFavorite ($is_favorite);
+ $this->_feed ($feed);
+ $this->_tags (preg_split('/[\s#]/', $tags));
+ }
+
+ public function id () {
+ return $this->id;
+ }
+ public function guid () {
+ return $this->guid;
+ }
+ public function title () {
+ return $this->title;
+ }
+ public function author () {
+ return $this->author === null ? '' : $this->author;
+ }
+ public function content () {
+ return $this->content;
+ }
+ public function link () {
+ return $this->link;
+ }
+ public function date ($raw = false) {
+ if ($raw) {
+ return $this->date;
+ } else {
+ return timestamptodate ($this->date);
+ }
+ }
+ public function dateAdded ($raw = false) {
+ $date = intval(substr($this->id, 0, -6));
+ if ($raw) {
+ return $date;
+ } else {
+ return timestamptodate ($date);
+ }
+ }
+ public function isRead () {
+ return $this->is_read;
+ }
+ public function isFavorite () {
+ return $this->is_favorite;
+ }
+ public function feed ($object = false) {
+ if ($object) {
+ $feedDAO = new FreshRSS_FeedDAO ();
+ return $feedDAO->searchById ($this->feed);
+ } else {
+ return $this->feed;
+ }
+ }
+ public function tags ($inString = false) {
+ if ($inString) {
+ return empty ($this->tags) ? '' : '#' . implode(' #', $this->tags);
+ } else {
+ return $this->tags;
+ }
+ }
+
+ public function _id ($value) {
+ $this->id = $value;
+ }
+ public function _guid ($value) {
+ $this->guid = $value;
+ }
+ public function _title ($value) {
+ $this->title = $value;
+ }
+ public function _author ($value) {
+ $this->author = $value;
+ }
+ public function _content ($value) {
+ $this->content = $value;
+ }
+ public function _link ($value) {
+ $this->link = $value;
+ }
+ public function _date ($value) {
+ $value = intval($value);
+ $this->date = $value > 1 ? $value : time();
+ }
+ public function _isRead ($value) {
+ $this->is_read = $value;
+ }
+ public function _isFavorite ($value) {
+ $this->is_favorite = $value;
+ }
+ public function _feed ($value) {
+ $this->feed = $value;
+ }
+ public function _tags ($value) {
+ if (!is_array ($value)) {
+ $value = array ($value);
+ }
+
+ foreach ($value as $key => $t) {
+ if (!$t) {
+ unset ($value[$key]);
+ }
+ }
+
+ $this->tags = $value;
+ }
+
+ public function isDay ($day, $today) {
+ $date = $this->dateAdded(true);
+ switch ($day) {
+ case FreshRSS_Days::TODAY:
+ $tomorrow = $today + 86400;
+ return $date >= $today && $date < $tomorrow;
+ case FreshRSS_Days::YESTERDAY:
+ $yesterday = $today - 86400;
+ return $date >= $yesterday && $date < $today;
+ case FreshRSS_Days::BEFORE_YESTERDAY:
+ $yesterday = $today - 86400;
+ return $date < $yesterday;
+ default:
+ return false;
+ }
+ }
+
+ public function loadCompleteContent($pathEntries) {
+ // Gestion du contenu
+ // On cherche à récupérer les articles en entier... même si le flux ne le propose pas
+ if ($pathEntries) {
+ $entryDAO = new FreshRSS_EntryDAO();
+ $entry = $entryDAO->searchByGuid($this->feed, $this->guid);
+
+ if($entry) {
+ // l'article existe déjà en BDD, en se contente de recharger ce contenu
+ $this->content = $entry->content();
+ } else {
+ try {
+ // l'article n'est pas en BDD, on va le chercher sur le site
+ $this->content = get_content_by_parsing(
+ htmlspecialchars_decode($this->link(), ENT_QUOTES), $pathEntries
+ );
+ } catch (Exception $e) {
+ // rien à faire, on garde l'ancien contenu (requête a échoué)
+ }
+ }
+ }
+ }
+
+ public function toArray () {
+ return array (
+ 'id' => $this->id (),
+ 'guid' => $this->guid (),
+ 'title' => $this->title (),
+ 'author' => $this->author (),
+ 'content' => $this->content (),
+ 'link' => $this->link (),
+ 'date' => $this->date (true),
+ 'is_read' => $this->isRead (),
+ 'is_favorite' => $this->isFavorite (),
+ 'id_feed' => $this->feed (),
+ 'tags' => $this->tags (true),
+ );
+ }
+}
diff --git a/app/Models/EntryDAO.php b/app/Models/EntryDAO.php
new file mode 100644
index 000000000..aaf4dcf6a
--- /dev/null
+++ b/app/Models/EntryDAO.php
@@ -0,0 +1,472 @@
+<?php
+
+class FreshRSS_EntryDAO extends Minz_ModelPdo {
+ public function addEntry ($valuesTmp) {
+ $sql = 'INSERT INTO `' . $this->prefix . 'entry`(id, guid, title, author, content_bin, link, date, is_read, is_favorite, id_feed, tags) '
+ . 'VALUES(?, ?, ?, ?, COMPRESS(?), ?, ?, ?, ?, ?, ?)';
+ $stm = $this->bd->prepare ($sql);
+
+ $values = array (
+ $valuesTmp['id'],
+ substr($valuesTmp['guid'], 0, 760),
+ substr($valuesTmp['title'], 0, 255),
+ substr($valuesTmp['author'], 0, 255),
+ $valuesTmp['content'],
+ substr($valuesTmp['link'], 0, 1023),
+ $valuesTmp['date'],
+ $valuesTmp['is_read'] ? 1 : 0,
+ $valuesTmp['is_favorite'] ? 1 : 0,
+ $valuesTmp['id_feed'],
+ substr($valuesTmp['tags'], 0, 1023),
+ );
+
+ if ($stm && $stm->execute ($values)) {
+ return $this->bd->lastInsertId();
+ } else {
+ $info = $stm->errorInfo();
+ if ((int)($info[0] / 1000) !== 23) { //Filter out "SQLSTATE Class code 23: Constraint Violation" because of expected duplicate entries
+ Minz_Log::record ('SQL error ' . $info[0] . ': ' . $info[1] . ' ' . $info[2]
+ . ' while adding entry in feed ' . $valuesTmp['id_feed'] . ' with title: ' . $valuesTmp['title'], Minz_Log::ERROR);
+ } /*else {
+ Minz_Log::record ('SQL error ' . $info[0] . ': ' . $info[1] . ' ' . $info[2]
+ . ' while adding entry in feed ' . $valuesTmp['id_feed'] . ' with title: ' . $valuesTmp['title'], Minz_Log::DEBUG);
+ }*/
+ return false;
+ }
+ }
+
+ public function markFavorite ($id, $is_favorite = true) {
+ $sql = 'UPDATE `' . $this->prefix . 'entry` e '
+ . 'SET e.is_favorite = ? '
+ . 'WHERE e.id=?';
+ $values = array ($is_favorite ? 1 : 0, $id);
+ $stm = $this->bd->prepare ($sql);
+ if ($stm && $stm->execute ($values)) {
+ return $stm->rowCount();
+ } else {
+ $info = $stm->errorInfo();
+ Minz_Log::record ('SQL error : ' . $info[2], Minz_Log::ERROR);
+ return false;
+ }
+ }
+ public function markRead ($id, $is_read = true) {
+ $sql = 'UPDATE `' . $this->prefix . 'entry` e INNER JOIN `' . $this->prefix . 'feed` f ON e.id_feed = f.id '
+ . 'SET e.is_read = ?,'
+ . 'f.cache_nbUnreads=f.cache_nbUnreads' . ($is_read ? '-' : '+') . '1 '
+ . 'WHERE e.id=?';
+ $values = array ($is_read ? 1 : 0, $id);
+ $stm = $this->bd->prepare ($sql);
+ if ($stm && $stm->execute ($values)) {
+ return $stm->rowCount();
+ } else {
+ $info = $stm->errorInfo();
+ Minz_Log::record ('SQL error : ' . $info[2], Minz_Log::ERROR);
+ return false;
+ }
+ }
+ public function markReadEntries ($idMax = 0, $favorites = false) {
+ if ($idMax === 0) {
+ $sql = 'UPDATE `' . $this->prefix . 'entry` e INNER JOIN `' . $this->prefix . 'feed` f ON e.id_feed = f.id '
+ . 'SET e.is_read = 1, f.cache_nbUnreads=0 '
+ . 'WHERE e.is_read = 0 AND ';
+ if ($favorites) {
+ $sql .= 'e.is_favorite = 1';
+ } else {
+ $sql .= 'f.priority > 0';
+ }
+ $stm = $this->bd->prepare ($sql);
+ if ($stm && $stm->execute ()) {
+ return $stm->rowCount();
+ } else {
+ $info = $stm->errorInfo();
+ Minz_Log::record ('SQL error : ' . $info[2], Minz_Log::ERROR);
+ return false;
+ }
+ } else {
+ $this->bd->beginTransaction ();
+
+ $sql = 'UPDATE `' . $this->prefix . 'entry` e INNER JOIN `' . $this->prefix . 'feed` f ON e.id_feed = f.id '
+ . 'SET e.is_read = 1 '
+ . 'WHERE e.is_read = 0 AND e.id <= ? AND ';
+ if ($favorites) {
+ $sql .= 'e.is_favorite = 1';
+ } else {
+ $sql .= 'f.priority > 0';
+ }
+ $values = array ($idMax);
+ $stm = $this->bd->prepare ($sql);
+ if (!($stm && $stm->execute ($values))) {
+ $info = $stm->errorInfo();
+ Minz_Log::record ('SQL error : ' . $info[2], Minz_Log::ERROR);
+ $this->bd->rollBack ();
+ return false;
+ }
+ $affected = $stm->rowCount();
+
+ if ($affected > 0) {
+ $sql = 'UPDATE `' . $this->prefix . 'feed` f '
+ . 'LEFT OUTER JOIN ('
+ . 'SELECT e.id_feed, '
+ . 'COUNT(*) AS nbUnreads '
+ . 'FROM `' . $this->prefix . 'entry` e '
+ . 'WHERE e.is_read = 0 '
+ . 'GROUP BY e.id_feed'
+ . ') x ON x.id_feed=f.id '
+ . 'SET f.cache_nbUnreads=COALESCE(x.nbUnreads, 0)';
+ $stm = $this->bd->prepare ($sql);
+ if (!($stm && $stm->execute ())) {
+ $info = $stm->errorInfo();
+ Minz_Log::record ('SQL error : ' . $info[2], Minz_Log::ERROR);
+ $this->bd->rollBack ();
+ return false;
+ }
+ }
+
+ $this->bd->commit ();
+ return $affected;
+ }
+ }
+ public function markReadCat ($id, $idMax = 0) {
+ if ($idMax === 0) {
+ $sql = 'UPDATE `' . $this->prefix . 'entry` e INNER JOIN `' . $this->prefix . 'feed` f ON e.id_feed = f.id '
+ . 'SET e.is_read = 1, f.cache_nbUnreads=0 '
+ . 'WHERE f.category = ? AND e.is_read = 0';
+ $values = array ($id);
+ $stm = $this->bd->prepare ($sql);
+ if ($stm && $stm->execute ($values)) {
+ return $stm->rowCount();
+ } else {
+ $info = $stm->errorInfo();
+ Minz_Log::record ('SQL error : ' . $info[2], Minz_Log::ERROR);
+ return false;
+ }
+ } else {
+ $this->bd->beginTransaction ();
+
+ $sql = 'UPDATE `' . $this->prefix . 'entry` e INNER JOIN `' . $this->prefix . 'feed` f ON e.id_feed = f.id '
+ . 'SET e.is_read = 1 '
+ . 'WHERE f.category = ? AND e.is_read = 0 AND e.id <= ?';
+ $values = array ($id, $idMax);
+ $stm = $this->bd->prepare ($sql);
+ if (!($stm && $stm->execute ($values))) {
+ $info = $stm->errorInfo();
+ Minz_Log::record ('SQL error : ' . $info[2], Minz_Log::ERROR);
+ $this->bd->rollBack ();
+ return false;
+ }
+ $affected = $stm->rowCount();
+
+ if ($affected > 0) {
+ $sql = 'UPDATE `' . $this->prefix . 'feed` f '
+ . 'LEFT OUTER JOIN ('
+ . 'SELECT e.id_feed, '
+ . 'COUNT(*) AS nbUnreads '
+ . 'FROM `' . $this->prefix . 'entry` e '
+ . 'WHERE e.is_read = 0 '
+ . 'GROUP BY e.id_feed'
+ . ') x ON x.id_feed=f.id '
+ . 'SET f.cache_nbUnreads=COALESCE(x.nbUnreads, 0) '
+ . 'WHERE f.category = ?';
+ $values = array ($id);
+ $stm = $this->bd->prepare ($sql);
+ if (!($stm && $stm->execute ($values))) {
+ $info = $stm->errorInfo();
+ Minz_Log::record ('SQL error : ' . $info[2], Minz_Log::ERROR);
+ $this->bd->rollBack ();
+ return false;
+ }
+ }
+
+ $this->bd->commit ();
+ return $affected;
+ }
+ }
+ public function markReadFeed ($id, $idMax = 0) {
+ if ($idMax === 0) {
+ $sql = 'UPDATE `' . $this->prefix . 'entry` e INNER JOIN `' . $this->prefix . 'feed` f ON e.id_feed = f.id '
+ . 'SET e.is_read = 1, f.cache_nbUnreads=0 '
+ . 'WHERE f.id=? AND e.is_read = 0';
+ $values = array ($id);
+ $stm = $this->bd->prepare ($sql);
+ if ($stm && $stm->execute ($values)) {
+ return $stm->rowCount();
+ } else {
+ $info = $stm->errorInfo();
+ Minz_Log::record ('SQL error : ' . $info[2], Minz_Log::ERROR);
+ return false;
+ }
+ } else {
+ $this->bd->beginTransaction ();
+
+ $sql = 'UPDATE `' . $this->prefix . 'entry` e INNER JOIN `' . $this->prefix . 'feed` f ON e.id_feed = f.id '
+ . 'SET e.is_read = 1 '
+ . 'WHERE f.id=? AND e.is_read = 0 AND e.id <= ?';
+ $values = array ($id, $idMax);
+ $stm = $this->bd->prepare ($sql);
+ if (!($stm && $stm->execute ($values))) {
+ $info = $stm->errorInfo();
+ Minz_Log::record ('SQL error : ' . $info[2], Minz_Log::ERROR);
+ $this->bd->rollBack ();
+ return false;
+ }
+ $affected = $stm->rowCount();
+
+ if ($affected > 0) {
+ $sql = 'UPDATE `' . $this->prefix . 'feed` f '
+ . 'SET f.cache_nbUnreads=f.cache_nbUnreads-' . $affected
+ . ' WHERE f.id=?';
+ $values = array ($id);
+ $stm = $this->bd->prepare ($sql);
+ if (!($stm && $stm->execute ($values))) {
+ $info = $stm->errorInfo();
+ Minz_Log::record ('SQL error : ' . $info[2], Minz_Log::ERROR);
+ $this->bd->rollBack ();
+ return false;
+ }
+ }
+
+ $this->bd->commit ();
+ return $affected;
+ }
+ }
+
+ public function searchByGuid ($feed_id, $id) {
+ // un guid est unique pour un flux donné
+ $sql = 'SELECT id, guid, title, author, UNCOMPRESS(content_bin) AS content, link, date, is_read, is_favorite, id_feed, tags '
+ . 'FROM `' . $this->prefix . 'entry` WHERE id_feed=? AND guid=?';
+ $stm = $this->bd->prepare ($sql);
+
+ $values = array (
+ $feed_id,
+ $id
+ );
+
+ $stm->execute ($values);
+ $res = $stm->fetchAll (PDO::FETCH_ASSOC);
+ $entries = self::daoToEntry ($res);
+ return isset ($entries[0]) ? $entries[0] : false;
+ }
+
+ public function searchById ($id) {
+ $sql = 'SELECT id, guid, title, author, UNCOMPRESS(content_bin) AS content, link, date, is_read, is_favorite, id_feed, tags '
+ . 'FROM `' . $this->prefix . 'entry` WHERE id=?';
+ $stm = $this->bd->prepare ($sql);
+
+ $values = array ($id);
+
+ $stm->execute ($values);
+ $res = $stm->fetchAll (PDO::FETCH_ASSOC);
+ $entries = self::daoToEntry ($res);
+ return isset ($entries[0]) ? $entries[0] : false;
+ }
+
+ public function listWhere($type = 'a', $id = '', $state = 'all', $order = 'DESC', $limit = 1, $firstId = '', $filter = '', $date_min = 0, $keepHistoryDefault = 0) {
+ $where = '';
+ $joinFeed = false;
+ $values = array();
+ switch ($type) {
+ case 'a':
+ $where .= 'f.priority > 0 ';
+ $joinFeed = true;
+ break;
+ case 's':
+ $where .= 'e1.is_favorite = 1 ';
+ break;
+ case 'c':
+ $where .= 'f.category = ? ';
+ $values[] = intval($id);
+ $joinFeed = true;
+ break;
+ case 'f':
+ $where .= 'e1.id_feed = ? ';
+ $values[] = intval($id);
+ break;
+ default:
+ throw new FreshRSS_EntriesGetter_Exception ('Bad type in Entry->listByType: [' . $type . ']!');
+ }
+ switch ($state) {
+ case 'all':
+ break;
+ case 'not_read':
+ $where .= 'AND e1.is_read = 0 ';
+ break;
+ case 'read':
+ $where .= 'AND e1.is_read = 1 ';
+ break;
+ case 'favorite':
+ $where .= 'AND e1.is_favorite = 1 ';
+ break;
+ default:
+ throw new FreshRSS_EntriesGetter_Exception ('Bad state in Entry->listByType: [' . $state . ']!');
+ }
+ switch ($order) {
+ case 'DESC':
+ case 'ASC':
+ break;
+ default:
+ throw new FreshRSS_EntriesGetter_Exception ('Bad order in Entry->listByType: [' . $order . ']!');
+ }
+ if ($firstId !== '') {
+ $where .= 'AND e1.id ' . ($order === 'DESC' ? '<=' : '>=') . $firstId . ' ';
+ }
+ if (($date_min > 0) && ($type !== 's')) {
+ $where .= 'AND (e1.id >= ' . $date_min . '000000 OR e1.is_read = 0 OR e1.is_favorite = 1 OR (f.keep_history <> 0';
+ if (intval($keepHistoryDefault) === 0) {
+ $where .= ' AND f.keep_history <> -2'; //default
+ }
+ $where .= ')) ';
+ $joinFeed = true;
+ }
+ $search = '';
+ if ($filter !== '') {
+ $filter = trim($filter);
+ $filter = addcslashes($filter, '\\%_');
+ if (stripos($filter, 'intitle:') === 0) {
+ $filter = substr($filter, strlen('intitle:'));
+ $intitle = true;
+ } else {
+ $intitle = false;
+ }
+ if (stripos($filter, 'inurl:') === 0) {
+ $filter = substr($filter, strlen('inurl:'));
+ $inurl = true;
+ } else {
+ $inurl = false;
+ }
+ if (stripos($filter, 'author:') === 0) {
+ $filter = substr($filter, strlen('author:'));
+ $author = true;
+ } else {
+ $author = false;
+ }
+ $terms = array_unique(explode(' ', $filter));
+ sort($terms); //Put #tags first
+ foreach ($terms as $word) {
+ $word = trim($word);
+ if (strlen($word) > 0) {
+ if ($intitle) {
+ $search .= 'AND e1.title LIKE ? ';
+ $values[] = '%' . $word .'%';
+ } elseif ($inurl) {
+ $search .= 'AND CONCAT(e1.link, e1.guid) LIKE ? ';
+ $values[] = '%' . $word .'%';
+ } elseif ($author) {
+ $search .= 'AND e1.author LIKE ? ';
+ $values[] = '%' . $word .'%';
+ } else {
+ if ($word[0] === '#' && isset($word[1])) {
+ $search .= 'AND e1.tags LIKE ? ';
+ $values[] = '%' . $word .'%';
+ } else {
+ $search .= 'AND CONCAT(e1.title, UNCOMPRESS(e1.content_bin)) LIKE ? ';
+ $values[] = '%' . $word .'%';
+ }
+ }
+ }
+ }
+ }
+
+ $sql = 'SELECT e.id, e.guid, e.title, e.author, UNCOMPRESS(e.content_bin) AS content, e.link, e.date, e.is_read, e.is_favorite, e.id_feed, e.tags '
+ . 'FROM `' . $this->prefix . 'entry` e '
+ . 'INNER JOIN (SELECT e1.id FROM `' . $this->prefix . 'entry` e1 '
+ . ($joinFeed ? 'INNER JOIN `' . $this->prefix . 'feed` f ON e1.id_feed = f.id ' : '')
+ . 'WHERE ' . $where
+ . $search
+ . 'ORDER BY e1.id ' . $order
+ . ($limit > 0 ? ' LIMIT ' . $limit : '') //TODO: See http://explainextended.com/2009/10/23/mysql-order-by-limit-performance-late-row-lookups/
+ . ') e2 ON e2.id = e.id '
+ . 'ORDER BY e.id ' . $order;
+
+ $stm = $this->bd->prepare ($sql);
+ $stm->execute ($values);
+
+ return self::daoToEntry ($stm->fetchAll (PDO::FETCH_ASSOC));
+ }
+
+ public function listLastGuidsByFeed($id, $n) {
+ $sql = 'SELECT guid FROM `' . $this->prefix . 'entry` WHERE id_feed=? ORDER BY id DESC LIMIT ' . intval($n);
+ $stm = $this->bd->prepare ($sql);
+ $values = array ($id);
+ $stm->execute ($values);
+ return $stm->fetchAll (PDO::FETCH_COLUMN, 0);
+ }
+
+ public function countUnreadRead () {
+ $sql = 'SELECT COUNT(e.id) AS count FROM `' . $this->prefix . 'entry` e INNER JOIN `' . $this->prefix . 'feed` f ON e.id_feed = f.id WHERE priority > 0'
+ . ' UNION SELECT COUNT(e.id) AS count FROM `' . $this->prefix . 'entry` e INNER JOIN `' . $this->prefix . 'feed` f ON e.id_feed = f.id WHERE priority > 0 AND is_read = 0';
+ $stm = $this->bd->prepare ($sql);
+ $stm->execute ();
+ $res = $stm->fetchAll (PDO::FETCH_COLUMN, 0);
+ $all = empty($res[0]) ? 0 : $res[0];
+ $unread = empty($res[1]) ? 0 : $res[1];
+ return array('all' => $all, 'unread' => $unread, 'read' => $all - $unread);
+ }
+ public function count ($minPriority = null) {
+ $sql = 'SELECT COUNT(e.id) AS count FROM `' . $this->prefix . 'entry` e INNER JOIN `' . $this->prefix . 'feed` f ON e.id_feed = f.id';
+ if ($minPriority !== null) {
+ $sql = ' WHERE priority > ' . intval($minPriority);
+ }
+ $stm = $this->bd->prepare ($sql);
+ $stm->execute ();
+ $res = $stm->fetchAll (PDO::FETCH_COLUMN, 0);
+ return $res[0];
+ }
+ public function countNotRead ($minPriority = null) {
+ $sql = 'SELECT COUNT(e.id) AS count FROM `' . $this->prefix . 'entry` e INNER JOIN `' . $this->prefix . 'feed` f ON e.id_feed = f.id WHERE is_read = 0';
+ if ($minPriority !== null) {
+ $sql = ' AND priority > ' . intval($minPriority);
+ }
+ $stm = $this->bd->prepare ($sql);
+ $stm->execute ();
+ $res = $stm->fetchAll (PDO::FETCH_COLUMN, 0);
+ return $res[0];
+ }
+
+ public function countUnreadReadFavorites () {
+ $sql = 'SELECT COUNT(id) FROM `' . $this->prefix . 'entry` WHERE is_favorite=1'
+ . ' UNION SELECT COUNT(id) FROM `' . $this->prefix . 'entry` WHERE is_favorite=1 AND is_read = 0';
+ $stm = $this->bd->prepare ($sql);
+ $stm->execute ();
+ $res = $stm->fetchAll (PDO::FETCH_COLUMN, 0);
+ $all = empty($res[0]) ? 0 : $res[0];
+ $unread = empty($res[1]) ? 0 : $res[1];
+ return array('all' => $all, 'unread' => $unread, 'read' => $all - $unread);
+ }
+
+ public function optimizeTable() {
+ $sql = 'OPTIMIZE TABLE `' . $this->prefix . 'entry`';
+ $stm = $this->bd->prepare ($sql);
+ $stm->execute ();
+ }
+
+ public static function daoToEntry ($listDAO) {
+ $list = array ();
+
+ if (!is_array ($listDAO)) {
+ $listDAO = array ($listDAO);
+ }
+
+ foreach ($listDAO as $key => $dao) {
+ $entry = new FreshRSS_Entry (
+ $dao['id_feed'],
+ $dao['guid'],
+ $dao['title'],
+ $dao['author'],
+ $dao['content'],
+ $dao['link'],
+ $dao['date'],
+ $dao['is_read'],
+ $dao['is_favorite'],
+ $dao['tags']
+ );
+ if (isset ($dao['id'])) {
+ $entry->_id ($dao['id']);
+ }
+ $list[] = $entry;
+ }
+
+ unset ($listDAO);
+
+ return $list;
+ }
+}
diff --git a/app/Models/Feed.php b/app/Models/Feed.php
new file mode 100644
index 000000000..22c019080
--- /dev/null
+++ b/app/Models/Feed.php
@@ -0,0 +1,274 @@
+<?php
+
+class FreshRSS_Feed extends Minz_Model {
+ private $id = 0;
+ private $url;
+ private $category = 1;
+ private $nbEntries = -1;
+ private $nbNotRead = -1;
+ private $entries = null;
+ private $name = '';
+ private $website = '';
+ private $description = '';
+ private $lastUpdate = 0;
+ private $priority = 10;
+ private $pathEntries = '';
+ private $httpAuth = '';
+ private $error = false;
+ private $keep_history = -2;
+ private $hash = null;
+
+ public function __construct ($url, $validate=true) {
+ if ($validate) {
+ $this->_url ($url);
+ } else {
+ $this->url = $url;
+ }
+ }
+
+ public function id () {
+ return $this->id;
+ }
+
+ public function hash() {
+ if ($this->hash === null) {
+ $this->hash = hash('crc32b', Minz_Configuration::salt() . $this->url);
+ }
+ return $this->hash;
+ }
+
+ public function url () {
+ return $this->url;
+ }
+ public function category () {
+ return $this->category;
+ }
+ public function entries () {
+ return $this->entries === null ? array() : $this->entries;
+ }
+ public function name () {
+ return $this->name;
+ }
+ public function website () {
+ return $this->website;
+ }
+ public function description () {
+ return $this->description;
+ }
+ public function lastUpdate () {
+ return $this->lastUpdate;
+ }
+ public function priority () {
+ return $this->priority;
+ }
+ public function pathEntries () {
+ return $this->pathEntries;
+ }
+ public function httpAuth ($raw = true) {
+ if ($raw) {
+ return $this->httpAuth;
+ } else {
+ $pos_colon = strpos ($this->httpAuth, ':');
+ $user = substr ($this->httpAuth, 0, $pos_colon);
+ $pass = substr ($this->httpAuth, $pos_colon + 1);
+
+ return array (
+ 'username' => $user,
+ 'password' => $pass
+ );
+ }
+ }
+ public function inError () {
+ return $this->error;
+ }
+ public function keepHistory () {
+ return $this->keep_history;
+ }
+ public function nbEntries () {
+ if ($this->nbEntries < 0) {
+ $feedDAO = new FreshRSS_FeedDAO ();
+ $this->nbEntries = $feedDAO->countEntries ($this->id ());
+ }
+
+ return $this->nbEntries;
+ }
+ public function nbNotRead () {
+ if ($this->nbNotRead < 0) {
+ $feedDAO = new FreshRSS_FeedDAO ();
+ $this->nbNotRead = $feedDAO->countNotRead ($this->id ());
+ }
+
+ return $this->nbNotRead;
+ }
+ public function faviconPrepare() {
+ $file = DATA_PATH . '/favicons/' . $this->hash() . '.txt';
+ if (!file_exists ($file)) {
+ $t = $this->website;
+ if (empty($t)) {
+ $t = $this->url;
+ }
+ file_put_contents($file, $t);
+ }
+ }
+ public static function faviconDelete($hash) {
+ $path = DATA_PATH . '/favicons/' . $hash;
+ @unlink($path . '.ico');
+ @unlink($path . '.txt');
+ }
+ public function favicon () {
+ return Minz_Url::display ('/f.php?' . $this->hash());
+ }
+
+ public function _id ($value) {
+ $this->id = $value;
+ }
+ public function _url ($value, $validate=true) {
+ if ($validate) {
+ $value = checkUrl($value);
+ }
+ if (empty ($value)) {
+ throw new FreshRSS_BadUrl_Exception ($value);
+ }
+ $this->url = $value;
+ }
+ public function _category ($value) {
+ $value = intval($value);
+ $this->category = $value >= 0 ? $value : 0;
+ }
+ public function _name ($value) {
+ $this->name = $value === null ? '' : $value;
+ }
+ public function _website ($value, $validate=true) {
+ if ($validate) {
+ $value = checkUrl($value);
+ }
+ if (empty ($value)) {
+ $value = '';
+ }
+ $this->website = $value;
+ }
+ public function _description ($value) {
+ $this->description = $value === null ? '' : $value;
+ }
+ public function _lastUpdate ($value) {
+ $this->lastUpdate = $value;
+ }
+ public function _priority ($value) {
+ $value = intval($value);
+ $this->priority = $value >= 0 ? $value : 10;
+ }
+ public function _pathEntries ($value) {
+ $this->pathEntries = $value;
+ }
+ public function _httpAuth ($value) {
+ $this->httpAuth = $value;
+ }
+ public function _error ($value) {
+ $this->error = (bool)$value;
+ }
+ public function _keepHistory ($value) {
+ $value = intval($value);
+ $value = min($value, 1000000);
+ $value = max($value, -2);
+ $this->keep_history = $value;
+ }
+ public function _nbNotRead ($value) {
+ $this->nbNotRead = intval($value);
+ }
+ public function _nbEntries ($value) {
+ $this->nbEntries = intval($value);
+ }
+
+ public function load ($loadDetails = false) {
+ if ($this->url !== null) {
+ if (CACHE_PATH === false) {
+ throw new Minz_FileNotExistException (
+ 'CACHE_PATH',
+ Minz_Exception::ERROR
+ );
+ } else {
+ $url = htmlspecialchars_decode ($this->url, ENT_QUOTES);
+ if ($this->httpAuth != '') {
+ $url = preg_replace ('#((.+)://)(.+)#', '${1}' . $this->httpAuth . '@${3}', $url);
+ }
+ $feed = customSimplePie();
+ $feed->set_feed_url ($url);
+ $feed->init ();
+
+ if ($feed->error ()) {
+ throw new FreshRSS_Feed_Exception ($feed->error . ' [' . $url . ']');
+ }
+
+ // si on a utilisé l'auto-discover, notre url va avoir changé
+ $subscribe_url = $feed->subscribe_url ();
+ if ($subscribe_url !== null && $subscribe_url !== $this->url) {
+ if ($this->httpAuth != '') {
+ // on enlève les id si authentification HTTP
+ $subscribe_url = preg_replace ('#((.+)://)((.+)@)(.+)#', '${1}${5}', $subscribe_url);
+ }
+ $this->_url ($subscribe_url);
+ }
+
+ if ($loadDetails) {
+ $title = htmlspecialchars(html_only_entity_decode($feed->get_title()), ENT_COMPAT, 'UTF-8');
+ $this->_name ($title === null ? $this->url : $title);
+
+ $this->_website(html_only_entity_decode($feed->get_link()));
+ $this->_description(html_only_entity_decode($feed->get_description()));
+ }
+
+ // et on charge les articles du flux
+ $this->loadEntries ($feed);
+ }
+ }
+ }
+ private function loadEntries ($feed) {
+ $entries = array ();
+
+ foreach ($feed->get_items () as $item) {
+ $title = html_only_entity_decode (strip_tags ($item->get_title ()));
+ $author = $item->get_author ();
+ $link = $item->get_permalink ();
+ $date = @strtotime ($item->get_date ());
+
+ // gestion des tags (catégorie == tag)
+ $tags_tmp = $item->get_categories ();
+ $tags = array ();
+ if ($tags_tmp !== null) {
+ foreach ($tags_tmp as $tag) {
+ $tags[] = html_only_entity_decode ($tag->get_label ());
+ }
+ }
+
+ $content = html_only_entity_decode ($item->get_content ());
+
+ $elinks = array();
+ foreach ($item->get_enclosures() as $enclosure) {
+ $elink = $enclosure->get_link();
+ if (array_key_exists($elink, $elinks)) continue;
+ $elinks[$elink] = '1';
+ $mime = strtolower($enclosure->get_type());
+ if (strpos($mime, 'image/') === 0) {
+ $content .= '<br /><img src="' . $elink . '" alt="" />';
+ }
+ }
+
+ $entry = new FreshRSS_Entry (
+ $this->id (),
+ $item->get_id (),
+ $title === null ? '' : $title,
+ $author === null ? '' : html_only_entity_decode ($author->name),
+ $content === null ? '' : $content,
+ $link === null ? '' : $link,
+ $date ? $date : time ()
+ );
+ $entry->_tags ($tags);
+ // permet de récupérer le contenu des flux tronqués
+ $entry->loadCompleteContent($this->pathEntries());
+
+ $entries[] = $entry;
+ }
+
+ $this->entries = $entries;
+ }
+}
diff --git a/app/Models/FeedDAO.php b/app/Models/FeedDAO.php
new file mode 100644
index 000000000..e102da4ec
--- /dev/null
+++ b/app/Models/FeedDAO.php
@@ -0,0 +1,345 @@
+<?php
+
+class FreshRSS_FeedDAO extends Minz_ModelPdo {
+ public function addFeed ($valuesTmp) {
+ $sql = 'INSERT INTO `' . $this->prefix . 'feed` (url, category, name, website, description, lastUpdate, priority, httpAuth, error, keep_history) VALUES(?, ?, ?, ?, ?, ?, 10, ?, 0, -2)';
+ $stm = $this->bd->prepare ($sql);
+
+ $values = array (
+ substr($valuesTmp['url'], 0, 511),
+ $valuesTmp['category'],
+ substr($valuesTmp['name'], 0, 255),
+ substr($valuesTmp['website'], 0, 255),
+ substr($valuesTmp['description'], 0, 1023),
+ $valuesTmp['lastUpdate'],
+ base64_encode ($valuesTmp['httpAuth']),
+ );
+
+ if ($stm && $stm->execute ($values)) {
+ return $this->bd->lastInsertId();
+ } else {
+ $info = $stm->errorInfo();
+ Minz_Log::record ('SQL error : ' . $info[2], Minz_Log::ERROR);
+ return false;
+ }
+ }
+
+ public function updateFeed ($id, $valuesTmp) {
+ $set = '';
+ foreach ($valuesTmp as $key => $v) {
+ $set .= $key . '=?, ';
+
+ if ($key == 'httpAuth') {
+ $valuesTmp[$key] = base64_encode ($v);
+ }
+ }
+ $set = substr ($set, 0, -2);
+
+ $sql = 'UPDATE `' . $this->prefix . 'feed` SET ' . $set . ' WHERE id=?';
+ $stm = $this->bd->prepare ($sql);
+
+ foreach ($valuesTmp as $v) {
+ $values[] = $v;
+ }
+ $values[] = $id;
+
+ if ($stm && $stm->execute ($values)) {
+ return $stm->rowCount();
+ } else {
+ $info = $stm->errorInfo();
+ Minz_Log::record ('SQL error : ' . $info[2], Minz_Log::ERROR);
+ return false;
+ }
+ }
+
+ public function updateLastUpdate ($id, $inError = 0) {
+ $sql = 'UPDATE `' . $this->prefix . 'feed` f ' //2 sub-requests with FOREIGN KEY(e.id_feed), INDEX(e.is_read) faster than 1 request with GROUP BY or CASE
+ . 'SET f.cache_nbEntries=(SELECT COUNT(e1.id) FROM `' . $this->prefix . 'entry` e1 WHERE e1.id_feed=f.id),'
+ . 'f.cache_nbUnreads=(SELECT COUNT(e2.id) FROM `' . $this->prefix . 'entry` e2 WHERE e2.id_feed=f.id AND e2.is_read=0),'
+ . 'lastUpdate=?, error=? '
+ . 'WHERE f.id=?';
+
+ $stm = $this->bd->prepare ($sql);
+
+ $values = array (
+ time (),
+ $inError,
+ $id,
+ );
+
+ if ($stm && $stm->execute ($values)) {
+ return $stm->rowCount();
+ } else {
+ $info = $stm->errorInfo();
+ Minz_Log::record ('SQL error : ' . $info[2], Minz_Log::ERROR);
+ return false;
+ }
+ }
+
+ public function changeCategory ($idOldCat, $idNewCat) {
+ $catDAO = new FreshRSS_CategoryDAO ();
+ $newCat = $catDAO->searchById ($idNewCat);
+ if (!$newCat) {
+ $newCat = $catDAO->getDefault ();
+ }
+
+ $sql = 'UPDATE `' . $this->prefix . 'feed` SET category=? WHERE category=?';
+ $stm = $this->bd->prepare ($sql);
+
+ $values = array (
+ $newCat->id (),
+ $idOldCat
+ );
+
+ if ($stm && $stm->execute ($values)) {
+ return $stm->rowCount();
+ } else {
+ $info = $stm->errorInfo();
+ Minz_Log::record ('SQL error : ' . $info[2], Minz_Log::ERROR);
+ return false;
+ }
+ }
+
+ public function deleteFeed ($id) {
+ /*//For MYISAM (MySQL 5.5-) without FOREIGN KEY
+ $sql = 'DELETE FROM `' . $this->prefix . 'entry` WHERE id_feed=?';
+ $stm = $this->bd->prepare ($sql);
+ $values = array ($id);
+ if (!($stm && $stm->execute ($values))) {
+ $info = $stm->errorInfo();
+ Minz_Log::record ('SQL error : ' . $info[2], Minz_Log::ERROR);
+ return false;
+ }*/
+
+ $sql = 'DELETE FROM `' . $this->prefix . 'feed` WHERE id=?';
+ $stm = $this->bd->prepare ($sql);
+
+ $values = array ($id);
+
+ if ($stm && $stm->execute ($values)) {
+ return $stm->rowCount();
+ } else {
+ $info = $stm->errorInfo();
+ Minz_Log::record ('SQL error : ' . $info[2], Minz_Log::ERROR);
+ return false;
+ }
+ }
+ public function deleteFeedByCategory ($id) {
+ /*//For MYISAM (MySQL 5.5-) without FOREIGN KEY
+ $sql = 'DELETE FROM `' . $this->prefix . 'entry` e '
+ . 'INNER JOIN `' . $this->prefix . 'feed` f ON e.id_feed = f.id '
+ . 'WHERE f.category=?';
+ $stm = $this->bd->prepare ($sql);
+ $values = array ($id);
+ if (!($stm && $stm->execute ($values))) {
+ $info = $stm->errorInfo();
+ Minz_Log::record ('SQL error : ' . $info[2], Minz_Log::ERROR);
+ return false;
+ }*/
+
+ $sql = 'DELETE FROM `' . $this->prefix . 'feed` WHERE category=?';
+ $stm = $this->bd->prepare ($sql);
+
+ $values = array ($id);
+
+ if ($stm && $stm->execute ($values)) {
+ return $stm->rowCount();
+ } else {
+ $info = $stm->errorInfo();
+ Minz_Log::record ('SQL error : ' . $info[2], Minz_Log::ERROR);
+ return false;
+ }
+ }
+
+ public function searchById ($id) {
+ $sql = 'SELECT * FROM `' . $this->prefix . 'feed` WHERE id=?';
+ $stm = $this->bd->prepare ($sql);
+
+ $values = array ($id);
+
+ $stm->execute ($values);
+ $res = $stm->fetchAll (PDO::FETCH_ASSOC);
+ $feed = self::daoToFeed ($res);
+
+ if (isset ($feed[$id])) {
+ return $feed[$id];
+ } else {
+ return false;
+ }
+ }
+ public function searchByUrl ($url) {
+ $sql = 'SELECT * FROM `' . $this->prefix . 'feed` WHERE url=?';
+ $stm = $this->bd->prepare ($sql);
+
+ $values = array ($url);
+
+ $stm->execute ($values);
+ $res = $stm->fetchAll (PDO::FETCH_ASSOC);
+ $feed = current (self::daoToFeed ($res));
+
+ if (isset ($feed)) {
+ return $feed;
+ } else {
+ return false;
+ }
+ }
+
+ public function listFeeds () {
+ $sql = 'SELECT * FROM `' . $this->prefix . 'feed` ORDER BY name';
+ $stm = $this->bd->prepare ($sql);
+ $stm->execute ();
+
+ return self::daoToFeed ($stm->fetchAll (PDO::FETCH_ASSOC));
+ }
+
+ public function listFeedsOrderUpdate () {
+ $sql = 'SELECT id, name, url, pathEntries, httpAuth, keep_history FROM `' . $this->prefix . 'feed` ORDER BY lastUpdate';
+ $stm = $this->bd->prepare ($sql);
+ $stm->execute ();
+
+ return self::daoToFeed ($stm->fetchAll (PDO::FETCH_ASSOC));
+ }
+
+ public function listByCategory ($cat) {
+ $sql = 'SELECT * FROM `' . $this->prefix . 'feed` WHERE category=? ORDER BY name';
+ $stm = $this->bd->prepare ($sql);
+
+ $values = array ($cat);
+
+ $stm->execute ($values);
+
+ return self::daoToFeed ($stm->fetchAll (PDO::FETCH_ASSOC));
+ }
+
+ public function countEntries ($id) {
+ $sql = 'SELECT COUNT(*) AS count FROM `' . $this->prefix . 'entry` WHERE id_feed=?';
+ $stm = $this->bd->prepare ($sql);
+ $values = array ($id);
+ $stm->execute ($values);
+ $res = $stm->fetchAll (PDO::FETCH_ASSOC);
+
+ return $res[0]['count'];
+ }
+ public function countNotRead ($id) {
+ $sql = 'SELECT COUNT(*) AS count FROM `' . $this->prefix . 'entry` WHERE id_feed=? AND is_read=0';
+ $stm = $this->bd->prepare ($sql);
+ $values = array ($id);
+ $stm->execute ($values);
+ $res = $stm->fetchAll (PDO::FETCH_ASSOC);
+
+ return $res[0]['count'];
+ }
+ public function updateCachedValues () { //For one single feed, call updateLastUpdate($id)
+ $sql = 'UPDATE `' . $this->prefix . 'feed` f '
+ . 'INNER JOIN ('
+ . 'SELECT e.id_feed, '
+ . 'COUNT(CASE WHEN e.is_read = 0 THEN 1 END) AS nbUnreads, '
+ . 'COUNT(e.id) AS nbEntries '
+ . 'FROM `' . $this->prefix . 'entry` e '
+ . 'GROUP BY e.id_feed'
+ . ') x ON x.id_feed=f.id '
+ . 'SET f.cache_nbEntries=x.nbEntries, f.cache_nbUnreads=x.nbUnreads';
+ $stm = $this->bd->prepare ($sql);
+
+ $values = array ($feed_id);
+
+ if ($stm && $stm->execute ($values)) {
+ return $stm->rowCount();
+ } else {
+ $info = $stm->errorInfo();
+ Minz_Log::record ('SQL error : ' . $info[2], Minz_Log::ERROR);
+ return false;
+ }
+ }
+
+ public function truncate ($id) {
+ $sql = 'DELETE e.* FROM `' . $this->prefix . 'entry` e WHERE e.id_feed=?';
+ $stm = $this->bd->prepare($sql);
+ $values = array($id);
+ $this->bd->beginTransaction ();
+ if (!($stm && $stm->execute ($values))) {
+ $info = $stm->errorInfo();
+ Minz_Log::record ('SQL error : ' . $info[2], Minz_Log::ERROR);
+ $this->bd->rollBack ();
+ return false;
+ }
+ $affected = $stm->rowCount();
+
+ $sql = 'UPDATE `' . $this->prefix . 'feed` f '
+ . 'SET f.cache_nbEntries=0, f.cache_nbUnreads=0 WHERE f.id=?';
+ $values = array ($id);
+ $stm = $this->bd->prepare ($sql);
+ if (!($stm && $stm->execute ($values))) {
+ $info = $stm->errorInfo();
+ Minz_Log::record ('SQL error : ' . $info[2], Minz_Log::ERROR);
+ $this->bd->rollBack ();
+ return false;
+ }
+
+ $this->bd->commit ();
+ return $affected;
+ }
+
+ public function cleanOldEntries ($id, $date_min, $keep = 15) { //Remember to call updateLastUpdate($id) just after
+ $sql = 'DELETE e.* FROM `' . $this->prefix . 'entry` e '
+ . 'WHERE e.id_feed = :id_feed AND e.id <= :id_max AND e.is_favorite = 0 AND e.id NOT IN '
+ . '(SELECT id FROM (SELECT e2.id FROM `' . $this->prefix . 'entry` e2 WHERE e2.id_feed = :id_feed ORDER BY id DESC LIMIT :keep) keep)'; //Double select because of: MySQL doesn't yet support 'LIMIT & IN/ALL/ANY/SOME subquery'
+ $stm = $this->bd->prepare ($sql);
+
+ $id_max = intval($date_min) . '000000';
+
+ $stm->bindParam(':id_feed', $id, PDO::PARAM_INT);
+ $stm->bindParam(':id_max', $id_max, PDO::PARAM_INT);
+ $stm->bindParam(':keep', $keep, PDO::PARAM_INT);
+
+ if ($stm && $stm->execute ()) {
+ return $stm->rowCount();
+ } else {
+ $info = $stm->errorInfo();
+ Minz_Log::record ('SQL error : ' . $info[2], Minz_Log::ERROR);
+ return false;
+ }
+ }
+
+ public static function daoToFeed ($listDAO, $catID = null) {
+ $list = array ();
+
+ if (!is_array ($listDAO)) {
+ $listDAO = array ($listDAO);
+ }
+
+ foreach ($listDAO as $key => $dao) {
+ if (!isset ($dao['name'])) {
+ continue;
+ }
+ if (isset ($dao['id'])) {
+ $key = $dao['id'];
+ }
+ if ($catID === null) {
+ $category = isset($dao['category']) ? $dao['category'] : 0;
+ } else {
+ $category = $catID ;
+ }
+
+ $myFeed = new FreshRSS_Feed(isset($dao['url']) ? $dao['url'] : '', false);
+ $myFeed->_category($category);
+ $myFeed->_name($dao['name']);
+ $myFeed->_website(isset($dao['website']) ? $dao['website'] : '', false);
+ $myFeed->_description(isset($dao['description']) ? $dao['description'] : '');
+ $myFeed->_lastUpdate(isset($dao['lastUpdate']) ? $dao['lastUpdate'] : 0);
+ $myFeed->_priority(isset($dao['priority']) ? $dao['priority'] : 10);
+ $myFeed->_pathEntries(isset($dao['pathEntries']) ? $dao['pathEntries'] : '');
+ $myFeed->_httpAuth(isset($dao['httpAuth']) ? base64_decode ($dao['httpAuth']) : '');
+ $myFeed->_error(isset($dao['error']) ? $dao['error'] : 0);
+ $myFeed->_keepHistory(isset($dao['keep_history']) ? $dao['keep_history'] : -2);
+ $myFeed->_nbNotRead(isset($dao['cache_nbUnreads']) ? $dao['cache_nbUnreads'] : 0);
+ $myFeed->_nbEntries(isset($dao['cache_nbEntries']) ? $dao['cache_nbEntries'] : 0);
+ if (isset ($dao['id'])) {
+ $myFeed->_id ($dao['id']);
+ }
+ $list[$key] = $myFeed;
+ }
+
+ return $list;
+ }
+}
diff --git a/app/Models/Log.php b/app/Models/Log.php
new file mode 100644
index 000000000..d2794458b
--- /dev/null
+++ b/app/Models/Log.php
@@ -0,0 +1,26 @@
+<?php
+
+class FreshRSS_Log extends Minz_Model {
+ private $date;
+ private $level;
+ private $information;
+
+ public function date () {
+ return $this->date;
+ }
+ public function level () {
+ return $this->level;
+ }
+ public function info () {
+ return $this->information;
+ }
+ public function _date ($date) {
+ $this->date = $date;
+ }
+ public function _level ($level) {
+ $this->level = $level;
+ }
+ public function _info ($information) {
+ $this->information = $information;
+ }
+}
diff --git a/app/Models/LogDAO.php b/app/Models/LogDAO.php
new file mode 100644
index 000000000..d1e515200
--- /dev/null
+++ b/app/Models/LogDAO.php
@@ -0,0 +1,25 @@
+<?php
+
+class FreshRSS_LogDAO {
+ public static function lines() {
+ $logs = array ();
+ $handle = @fopen(LOG_PATH . '/' . Minz_Session::param('currentUser', '_') . '.log', 'r');
+ if ($handle) {
+ while (($line = fgets($handle)) !== false) {
+ if (preg_match ('/^\[([^\[]+)\] \[([^\[]+)\] --- (.*)$/', $line, $matches)) {
+ $myLog = new FreshRSS_Log ();
+ $myLog->_date ($matches[1]);
+ $myLog->_level ($matches[2]);
+ $myLog->_info ($matches[3]);
+ $logs[] = $myLog;
+ }
+ }
+ fclose($handle);
+ }
+ return array_reverse($logs);
+ }
+
+ public static function truncate() {
+ file_put_contents(LOG_PATH . '/' . Minz_Session::param('currentUser', '_') . '.log', '');
+ }
+}
diff --git a/app/Models/StatsDAO.php b/app/Models/StatsDAO.php
new file mode 100644
index 000000000..60cec7847
--- /dev/null
+++ b/app/Models/StatsDAO.php
@@ -0,0 +1,205 @@
+<?php
+
+class FreshRSS_StatsDAO extends Minz_ModelPdo {
+
+ /**
+ * Calculates entry repartition for all feeds and for main stream.
+ * The repartition includes:
+ * - total entries
+ * - read entries
+ * - unread entries
+ * - favorite entries
+ *
+ * @return type
+ */
+ public function calculateEntryRepartition() {
+ $repartition = array();
+
+ // Generates the repartition for the main stream of entry
+ $sql = <<<SQL
+SELECT COUNT(1) AS `total`,
+COUNT(1) - SUM(e.is_read) AS `unread`,
+SUM(e.is_read) AS `read`,
+SUM(e.is_favorite) AS `favorite`
+FROM {$this->prefix}entry AS e
+, {$this->prefix}feed AS f
+WHERE e.id_feed = f.id
+AND f.priority = 10
+SQL;
+ $stm = $this->bd->prepare($sql);
+ $stm->execute();
+ $res = $stm->fetchAll(PDO::FETCH_ASSOC);
+ $repartition['main_stream'] = $res[0];
+
+ // Generates the repartition for all entries
+ $sql = <<<SQL
+SELECT COUNT(1) AS `total`,
+COUNT(1) - SUM(e.is_read) AS `unread`,
+SUM(e.is_read) AS `read`,
+SUM(e.is_favorite) AS `favorite`
+FROM {$this->prefix}entry AS e
+SQL;
+ $stm = $this->bd->prepare($sql);
+ $stm->execute();
+ $res = $stm->fetchAll(PDO::FETCH_ASSOC);
+ $repartition['all_feeds'] = $res[0];
+
+ return $repartition;
+ }
+
+ /**
+ * Calculates entry count per day on a 30 days period.
+ * Returns the result as a JSON string.
+ *
+ * @return string
+ */
+ public function calculateEntryCount() {
+ $count = array();
+
+ // Generates a list of 30 last day to be sure we always have 30 days.
+ // If we do not do that kind of thing, we'll end up with holes in the
+ // days if the user do not have a lot of feeds.
+ $sql = <<<SQL
+SELECT - (tens.val + units.val + 1) AS day
+FROM (
+ SELECT 0 AS val
+ UNION ALL SELECT 1
+ UNION ALL SELECT 2
+ UNION ALL SELECT 3
+ UNION ALL SELECT 4
+ UNION ALL SELECT 5
+ UNION ALL SELECT 6
+ UNION ALL SELECT 7
+ UNION ALL SELECT 8
+ UNION ALL SELECT 9
+) AS units
+CROSS JOIN (
+ SELECT 0 AS val
+ UNION ALL SELECT 10
+ UNION ALL SELECT 20
+) AS tens
+ORDER BY day ASC
+SQL;
+ $stm = $this->bd->prepare($sql);
+ $stm->execute();
+ $res = $stm->fetchAll(PDO::FETCH_ASSOC);
+ foreach ($res as $value) {
+ $count[$value['day']] = 0;
+ }
+
+ // Get stats per day for the last 30 days and applies the result on
+ // the array created with the last query.
+ $sql = <<<SQL
+SELECT DATEDIFF(FROM_UNIXTIME(e.date), NOW()) AS day,
+COUNT(1) AS count
+FROM {$this->prefix}entry AS e
+WHERE FROM_UNIXTIME(e.date, '%Y%m%d') BETWEEN DATE_FORMAT(DATE_ADD(NOW(), INTERVAL -30 DAY), '%Y%m%d') AND DATE_FORMAT(DATE_ADD(NOW(), INTERVAL -1 DAY), '%Y%m%d')
+GROUP BY day
+ORDER BY day ASC
+SQL;
+ $stm = $this->bd->prepare($sql);
+ $stm->execute();
+ $res = $stm->fetchAll(PDO::FETCH_ASSOC);
+
+ foreach ($res as $value) {
+ $count[$value['day']] = (int) $value['count'];
+ }
+
+ return $this->convertToSerie($count);
+ }
+
+ /**
+ * Calculates feed count per category.
+ * Returns the result as a JSON string.
+ *
+ * @return string
+ */
+ public function calculateFeedByCategory() {
+ $sql = <<<SQL
+SELECT c.name AS label
+, COUNT(f.id) AS data
+FROM {$this->prefix}category AS c,
+{$this->prefix}feed AS f
+WHERE c.id = f.category
+GROUP BY label
+ORDER BY data DESC
+SQL;
+ $stm = $this->bd->prepare($sql);
+ $stm->execute();
+ $res = $stm->fetchAll(PDO::FETCH_ASSOC);
+
+ return $this->convertToPieSerie($res);
+ }
+
+ /**
+ * Calculates entry count per category.
+ * Returns the result as a JSON string.
+ *
+ * @return string
+ */
+ public function calculateEntryByCategory() {
+ $sql = <<<SQL
+SELECT c.name AS label
+, COUNT(e.id) AS data
+FROM {$this->prefix}category AS c,
+{$this->prefix}feed AS f,
+{$this->prefix}entry AS e
+WHERE c.id = f.category
+AND f.id = e.id_feed
+GROUP BY label
+ORDER BY data DESC
+SQL;
+ $stm = $this->bd->prepare($sql);
+ $stm->execute();
+ $res = $stm->fetchAll(PDO::FETCH_ASSOC);
+
+ return $this->convertToPieSerie($res);
+ }
+
+ /**
+ * Calculates the 10 top feeds based on their number of entries
+ *
+ * @return array
+ */
+ public function calculateTopFeed() {
+ $sql = <<<SQL
+SELECT f.id AS id
+, MAX(f.name) AS name
+, MAX(c.name) AS category
+, COUNT(e.id) AS count
+FROM {$this->prefix}category AS c,
+{$this->prefix}feed AS f,
+{$this->prefix}entry AS e
+WHERE c.id = f.category
+AND f.id = e.id_feed
+GROUP BY id
+ORDER BY count DESC
+LIMIT 10
+SQL;
+ $stm = $this->bd->prepare($sql);
+ $stm->execute();
+ return $stm->fetchAll(PDO::FETCH_ASSOC);
+ }
+
+ private function convertToSerie($data) {
+ $serie = array();
+
+ foreach ($data as $key => $value) {
+ $serie[] = array($key, $value);
+ }
+
+ return json_encode($serie);
+ }
+
+ private function convertToPieSerie($data) {
+ $serie = array();
+
+ foreach ($data as $value) {
+ $value['data'] = array(array(0, (int) $value['data']));
+ $serie[] = $value;
+ }
+
+ return json_encode($serie);
+ }
+
+}
diff --git a/app/Models/Themes.php b/app/Models/Themes.php
new file mode 100644
index 000000000..c7099a1df
--- /dev/null
+++ b/app/Models/Themes.php
@@ -0,0 +1,106 @@
+<?php
+
+class FreshRSS_Themes extends Minz_Model {
+ private static $themesUrl = '/themes/';
+ private static $defaultIconsUrl = '/themes/icons/';
+ public static $defaultTheme = 'Origine';
+
+ public static function getList() {
+ return array_values(array_diff(
+ scandir(PUBLIC_PATH . self::$themesUrl),
+ array('..', '.')
+ ));
+ }
+
+ public static function get() {
+ $themes_list = self::getList();
+ $list = array();
+ foreach ($themes_list as $theme_dir) {
+ $theme = self::get_infos($theme_dir);
+ if ($theme) {
+ $list[$theme_dir] = $theme;
+ }
+ }
+ return $list;
+ }
+
+ public static function get_infos($theme_id) {
+ $theme_dir = PUBLIC_PATH . self::$themesUrl . $theme_id ;
+ if (is_dir($theme_dir)) {
+ $json_filename = $theme_dir . '/metadata.json';
+ if (file_exists($json_filename)) {
+ $content = file_get_contents($json_filename);
+ $res = json_decode($content, true);
+ if ($res && isset($res['files']) && is_array($res['files'])) {
+ $res['id'] = $theme_id;
+ return $res;
+ }
+ }
+ }
+ return false;
+ }
+
+ private static $themeIconsUrl;
+ private static $themeIcons;
+
+ public static function load($theme_id) {
+ $infos = self::get_infos($theme_id);
+ if (!$infos) {
+ if ($theme_id !== self::$defaultTheme) { //Fall-back to default theme
+ return self::load(self::$defaultTheme);
+ }
+ $themes_list = self::getList();
+ if (!empty($themes_list)) {
+ if ($theme_id !== $themes_list[0]) { //Fall-back to first theme
+ return self::load($themes_list[0]);
+ }
+ }
+ return false;
+ }
+ self::$themeIconsUrl = self::$themesUrl . $theme_id . '/icons/';
+ self::$themeIcons = is_dir(PUBLIC_PATH . self::$themeIconsUrl) ? array_fill_keys(array_diff(
+ scandir(PUBLIC_PATH . self::$themeIconsUrl),
+ array('..', '.')
+ ), 1) : array();
+ return $infos;
+ }
+
+ public static function icon($name, $urlOnly = false) {
+ static $alts = array(
+ 'add' => '✚',
+ 'all' => '☰',
+ 'bookmark' => '★',
+ 'category' => '☷',
+ 'category-white' => '☷',
+ 'close' => '❌',
+ 'configure' => '⚙',
+ 'down' => '▽',
+ 'favorite' => '★',
+ 'help' => 'ⓘ',
+ 'link' => '↗',
+ 'login' => '🔒',
+ 'logout' => '🔓',
+ 'next' => '⏩',
+ 'non-starred' => '☆',
+ 'prev' => '⏪',
+ 'read' => '☑',
+ 'unread' => '☐',
+ 'refresh' => '🔃', //↻
+ 'search' => '🔍',
+ 'share' => '♺',
+ 'starred' => '★',
+ 'tag' => '⚐',
+ 'up' => '△',
+ );
+ if (!isset($alts[$name])) {
+ return '';
+ }
+
+ $url = $name . '.svg';
+ $url = isset(self::$themeIcons[$url]) ? (self::$themeIconsUrl . $url) :
+ (self::$defaultIconsUrl . $url);
+
+ return $urlOnly ? Minz_Url::display($url) :
+ '<img class="icon" src="' . Minz_Url::display($url) . '" alt="' . $alts[$name] . '" />';
+ }
+}
diff --git a/app/Models/UserDAO.php b/app/Models/UserDAO.php
new file mode 100644
index 000000000..a25b57f89
--- /dev/null
+++ b/app/Models/UserDAO.php
@@ -0,0 +1,36 @@
+<?php
+
+class FreshRSS_UserDAO extends Minz_ModelPdo {
+ public function createUser($username) {
+ require_once(APP_PATH . '/sql.php');
+ $db = Minz_Configuration::dataBase();
+
+ $sql = sprintf(SQL_CREATE_TABLES, $db['prefix'] . $username . '_');
+ $stm = $this->bd->prepare($sql, array(PDO::ATTR_EMULATE_PREPARES => true));
+ $values = array(
+ 'catName' => Minz_Translate::t('default_category'),
+ );
+ if ($stm && $stm->execute($values)) {
+ return true;
+ } else {
+ $info = $stm->errorInfo();
+ Minz_Log::record ('SQL error : ' . $info[2], Minz_Log::ERROR);
+ return false;
+ }
+ }
+
+ public function deleteUser($username) {
+ require_once(APP_PATH . '/sql.php');
+ $db = Minz_Configuration::dataBase();
+
+ $sql = sprintf(SQL_DROP_TABLES, $db['prefix'] . $username . '_');
+ $stm = $this->bd->prepare($sql);
+ if ($stm && $stm->execute()) {
+ return true;
+ } else {
+ $info = $stm->errorInfo();
+ Minz_Log::record ('SQL error : ' . $info[2], Minz_Log::ERROR);
+ return false;
+ }
+ }
+}