diff options
| author | 2013-12-15 11:24:14 +0100 | |
|---|---|---|
| committer | 2013-12-15 11:24:14 +0100 | |
| commit | 4ee4f16ffe06e247d2cb79a2054ab5d5315d82b2 (patch) | |
| tree | 1210892f3fd3da163bcdf8ae3a8024b29607f293 /app/Models | |
| parent | a29dad13761208bfdf024e7f6703023d85428695 (diff) | |
Problème de casse renommage répertoire
Diffstat (limited to 'app/Models')
| -rw-r--r-- | app/Models/Category.php | 85 | ||||
| -rw-r--r-- | app/Models/Days.php | 7 | ||||
| -rw-r--r-- | app/Models/Entry.php | 193 | ||||
| -rw-r--r-- | app/Models/Feed.php | 319 |
4 files changed, 604 insertions, 0 deletions
diff --git a/app/Models/Category.php b/app/Models/Category.php new file mode 100644 index 000000000..e70d1303f --- /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 (is_null ($this->feeds)) { + $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/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..ba0fb48f4 --- /dev/null +++ b/app/Models/Entry.php @@ -0,0 +1,193 @@ +<?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 () { + if (is_null ($this->author)) { + return ''; + } else { + return $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) { + if (ctype_digit ($value)) { + $this->date = intval ($value); + } else { + $this->date = 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) { + $date = $this->dateAdded(true); + $today = @strtotime('today'); + $yesterday = $today - 86400; + + if ($day === FreshRSS_Days::TODAY && + $date >= $today && $date < $today + 86400) { + return true; + } elseif ($day === FreshRSS_Days::YESTERDAY && + $date >= $yesterday && $date < $yesterday + 86400) { + return true; + } elseif ($day === FreshRSS_Days::BEFORE_YESTERDAY && $date < $yesterday) { + return true; + } else { + 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( + $this->link(), $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/Feed.php b/app/Models/Feed.php new file mode 100644 index 000000000..e63ac8c7a --- /dev/null +++ b/app/Models/Feed.php @@ -0,0 +1,319 @@ +<?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 = false; + + public function __construct ($url, $validate=true) { + if ($validate) { + $this->_url ($url); + } else { + $this->url = $url; + } + } + + public function id () { + return $this->id; + } + public function url () { + return $this->url; + } + public function category () { + return $this->category; + } + public function entries () { + if (!is_null ($this->entries)) { + return $this->entries; + } else { + return array (); + } + } + 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->id () . '.txt'; + if (!file_exists ($file)) { + $t = $this->website; + if (empty($t)) { + $t = $this->url; + } + file_put_contents($file, $t); + } + } + public static function faviconDelete($id) { + $path = DATA_PATH . '/favicons/' . $id; + @unlink($path . '.ico'); + @unlink($path . '.txt'); + } + public function favicon () { + return Minz_Url::display ('/f.php?' . $this->id ()); + } + + 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) { + $this->category = $value; + } + public function _name ($value) { + if (is_null ($value)) { + $value = ''; + } + $this->name = $value; + } + public function _website ($value, $validate=true) { + if ($validate) { + $value = checkUrl($value); + } + if (empty ($value)) { + $value = ''; + } + $this->website = $value; + } + public function _description ($value) { + if (is_null ($value)) { + $value = ''; + } + $this->description = $value; + } + public function _lastUpdate ($value) { + $this->lastUpdate = $value; + } + public function _priority ($value) { + $this->priority = ctype_digit ($value) ? intval ($value) : 10; + } + public function _pathEntries ($value) { + $this->pathEntries = $value; + } + public function _httpAuth ($value) { + $this->httpAuth = $value; + } + public function _error ($value) { + if ($value) { + $value = true; + } else { + $value = false; + } + $this->error = $value; + } + public function _keepHistory ($value) { + if ($value) { + $value = true; + } else { + $value = false; + } + $this->keep_history = $value; + } + public function _nbNotRead ($value) { + $this->nbNotRead = ctype_digit ($value) ? intval ($value) : -1; + } + public function _nbEntries ($value) { + $this->nbEntries = ctype_digit ($value) ? intval ($value) : -1; + } + + public function load () { + if (!is_null ($this->url)) { + if (CACHE_PATH === false) { + throw new Minz_FileNotExistException ( + 'CACHE_PATH', + Minz_Exception::ERROR + ); + } else { + $feed = new SimplePie (); + $feed->set_useragent(Minz_Translate::t ('freshrss') . '/' . FRESHRSS_VERSION . ' (' . PHP_OS . '; ' . FRESHRSS_WEBSITE . ') ' . SIMPLEPIE_NAME . '/' . SIMPLEPIE_VERSION); + $url = htmlspecialchars_decode ($this->url, ENT_QUOTES); + if ($this->httpAuth != '') { + $url = preg_replace ('#((.+)://)(.+)#', '${1}' . $this->httpAuth . '@${3}', $url); + } + + $feed->set_feed_url ($url); + $feed->set_cache_location (CACHE_PATH); + $feed->set_cache_duration(1500); + $feed->strip_htmltags (array ( + 'base', 'blink', 'body', 'doctype', 'embed', + 'font', 'form', 'frame', 'frameset', 'html', + 'input', 'marquee', 'meta', 'noscript', + 'object', 'param', 'plaintext', 'script', 'style', + )); + $feed->strip_attributes(array_merge($feed->strip_attributes, array( + 'autoplay', 'onload', 'onunload', 'onclick', 'ondblclick', 'onmousedown', 'onmouseup', + 'onmouseover', 'onmousemove', 'onmouseout', 'onfocus', 'onblur', + 'onkeypress', 'onkeydown', 'onkeyup', 'onselect', 'onchange', 'seamless'))); + $feed->add_attributes(array( + 'img' => array('lazyload' => ''), //http://www.w3.org/TR/resource-priorities/ + 'audio' => array('preload' => 'none'), + 'iframe' => array('postpone' => '', 'sandbox' => 'allow-scripts allow-same-origin'), + 'video' => array('postpone' => '', 'preload' => 'none'), + )); + $feed->set_url_replacements(array( + 'a' => 'href', + 'area' => 'href', + 'audio' => 'src', + 'blockquote' => 'cite', + 'del' => 'cite', + 'form' => 'action', + 'iframe' => 'src', + 'img' => array( + 'longdesc', + 'src' + ), + 'input' => 'src', + 'ins' => 'cite', + 'q' => 'cite', + 'source' => 'src', + 'track' => 'src', + 'video' => array( + 'poster', + 'src', + ), + )); + $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 (!is_null ($subscribe_url) && $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); + } + + $title = $feed->get_title (); + $this->_name (!is_null ($title) ? $title : $this->url); + + $this->_website ($feed->get_link ()); + $this->_description ($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 (!is_null ($tags_tmp)) { + 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 (), + !is_null ($title) ? $title : '', + !is_null ($author) ? html_only_entity_decode ($author->name) : '', + !is_null ($content) ? $content : '', + !is_null ($link) ? $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; + } +} |
