Feed.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. <?php
  2. class FreshRSS_Feed extends Minz_Model {
  3. private $id = 0;
  4. private $url;
  5. private $category = 1;
  6. private $nbEntries = -1;
  7. private $nbNotRead = -1;
  8. private $entries = null;
  9. private $name = '';
  10. private $website = '';
  11. private $description = '';
  12. private $lastUpdate = 0;
  13. private $priority = 10;
  14. private $pathEntries = '';
  15. private $httpAuth = '';
  16. private $error = false;
  17. private $keep_history = 0;
  18. public function __construct ($url, $validate=true) {
  19. if ($validate) {
  20. $this->_url ($url);
  21. } else {
  22. $this->url = $url;
  23. }
  24. }
  25. public function id () {
  26. return $this->id;
  27. }
  28. public function url () {
  29. return $this->url;
  30. }
  31. public function category () {
  32. return $this->category;
  33. }
  34. public function entries () {
  35. if (!is_null ($this->entries)) {
  36. return $this->entries;
  37. } else {
  38. return array ();
  39. }
  40. }
  41. public function name () {
  42. return $this->name;
  43. }
  44. public function website () {
  45. return $this->website;
  46. }
  47. public function description () {
  48. return $this->description;
  49. }
  50. public function lastUpdate () {
  51. return $this->lastUpdate;
  52. }
  53. public function priority () {
  54. return $this->priority;
  55. }
  56. public function pathEntries () {
  57. return $this->pathEntries;
  58. }
  59. public function httpAuth ($raw = true) {
  60. if ($raw) {
  61. return $this->httpAuth;
  62. } else {
  63. $pos_colon = strpos ($this->httpAuth, ':');
  64. $user = substr ($this->httpAuth, 0, $pos_colon);
  65. $pass = substr ($this->httpAuth, $pos_colon + 1);
  66. return array (
  67. 'username' => $user,
  68. 'password' => $pass
  69. );
  70. }
  71. }
  72. public function inError () {
  73. return $this->error;
  74. }
  75. public function keepHistory () {
  76. return $this->keep_history;
  77. }
  78. public function nbEntries () {
  79. if ($this->nbEntries < 0) {
  80. $feedDAO = new FreshRSS_FeedDAO ();
  81. $this->nbEntries = $feedDAO->countEntries ($this->id ());
  82. }
  83. return $this->nbEntries;
  84. }
  85. public function nbNotRead () {
  86. if ($this->nbNotRead < 0) {
  87. $feedDAO = new FreshRSS_FeedDAO ();
  88. $this->nbNotRead = $feedDAO->countNotRead ($this->id ());
  89. }
  90. return $this->nbNotRead;
  91. }
  92. public function faviconPrepare() {
  93. $file = DATA_PATH . '/favicons/' . $this->id () . '.txt';
  94. if (!file_exists ($file)) {
  95. $t = $this->website;
  96. if (empty($t)) {
  97. $t = $this->url;
  98. }
  99. file_put_contents($file, $t);
  100. }
  101. }
  102. public static function faviconDelete($id) {
  103. $path = DATA_PATH . '/favicons/' . $id;
  104. @unlink($path . '.ico');
  105. @unlink($path . '.txt');
  106. }
  107. public function favicon () {
  108. return Minz_Url::display ('/f.php?' . $this->id ());
  109. }
  110. public function _id ($value) {
  111. $this->id = $value;
  112. }
  113. public function _url ($value, $validate=true) {
  114. if ($validate) {
  115. $value = checkUrl($value);
  116. }
  117. if (empty ($value)) {
  118. throw new FreshRSS_BadUrl_Exception ($value);
  119. }
  120. $this->url = $value;
  121. }
  122. public function _category ($value) {
  123. $this->category = $value;
  124. }
  125. public function _name ($value) {
  126. if (is_null ($value)) {
  127. $value = '';
  128. }
  129. $this->name = $value;
  130. }
  131. public function _website ($value, $validate=true) {
  132. if ($validate) {
  133. $value = checkUrl($value);
  134. }
  135. if (empty ($value)) {
  136. $value = '';
  137. }
  138. $this->website = $value;
  139. }
  140. public function _description ($value) {
  141. if (is_null ($value)) {
  142. $value = '';
  143. }
  144. $this->description = $value;
  145. }
  146. public function _lastUpdate ($value) {
  147. $this->lastUpdate = $value;
  148. }
  149. public function _priority ($value) {
  150. $this->priority = ctype_digit ($value) ? intval ($value) : 10;
  151. }
  152. public function _pathEntries ($value) {
  153. $this->pathEntries = $value;
  154. }
  155. public function _httpAuth ($value) {
  156. $this->httpAuth = $value;
  157. }
  158. public function _error ($value) {
  159. $this->error = (bool)$value;
  160. }
  161. public function _keepHistory ($value) {
  162. $value = intval($value);
  163. $value = min($value, 1000000);
  164. $value = max($value, -1);
  165. $this->keep_history = $value;
  166. }
  167. public function _nbNotRead ($value) {
  168. $this->nbNotRead = ctype_digit ($value) ? intval ($value) : -1;
  169. }
  170. public function _nbEntries ($value) {
  171. $this->nbEntries = ctype_digit ($value) ? intval ($value) : -1;
  172. }
  173. public function load () {
  174. if (!is_null ($this->url)) {
  175. if (CACHE_PATH === false) {
  176. throw new Minz_FileNotExistException (
  177. 'CACHE_PATH',
  178. Minz_Exception::ERROR
  179. );
  180. } else {
  181. $feed = new SimplePie ();
  182. $feed->set_useragent(Minz_Translate::t ('freshrss') . '/' . FRESHRSS_VERSION . ' (' . PHP_OS . '; ' . FRESHRSS_WEBSITE . ') ' . SIMPLEPIE_NAME . '/' . SIMPLEPIE_VERSION);
  183. $url = htmlspecialchars_decode ($this->url, ENT_QUOTES);
  184. if ($this->httpAuth != '') {
  185. $url = preg_replace ('#((.+)://)(.+)#', '${1}' . $this->httpAuth . '@${3}', $url);
  186. }
  187. $feed->set_feed_url ($url);
  188. $feed->set_cache_location (CACHE_PATH);
  189. $feed->set_cache_duration(1500);
  190. $feed->strip_htmltags (array (
  191. 'base', 'blink', 'body', 'doctype', 'embed',
  192. 'font', 'form', 'frame', 'frameset', 'html',
  193. 'input', 'marquee', 'meta', 'noscript',
  194. 'object', 'param', 'plaintext', 'script', 'style',
  195. ));
  196. $feed->strip_attributes(array_merge($feed->strip_attributes, array(
  197. 'autoplay', 'onload', 'onunload', 'onclick', 'ondblclick', 'onmousedown', 'onmouseup',
  198. 'onmouseover', 'onmousemove', 'onmouseout', 'onfocus', 'onblur',
  199. 'onkeypress', 'onkeydown', 'onkeyup', 'onselect', 'onchange', 'seamless')));
  200. $feed->add_attributes(array(
  201. 'img' => array('lazyload' => ''), //http://www.w3.org/TR/resource-priorities/
  202. 'audio' => array('preload' => 'none'),
  203. 'iframe' => array('postpone' => '', 'sandbox' => 'allow-scripts allow-same-origin'),
  204. 'video' => array('postpone' => '', 'preload' => 'none'),
  205. ));
  206. $feed->set_url_replacements(array(
  207. 'a' => 'href',
  208. 'area' => 'href',
  209. 'audio' => 'src',
  210. 'blockquote' => 'cite',
  211. 'del' => 'cite',
  212. 'form' => 'action',
  213. 'iframe' => 'src',
  214. 'img' => array(
  215. 'longdesc',
  216. 'src'
  217. ),
  218. 'input' => 'src',
  219. 'ins' => 'cite',
  220. 'q' => 'cite',
  221. 'source' => 'src',
  222. 'track' => 'src',
  223. 'video' => array(
  224. 'poster',
  225. 'src',
  226. ),
  227. ));
  228. $feed->init ();
  229. if ($feed->error ()) {
  230. throw new FreshRSS_Feed_Exception ($feed->error . ' [' . $url . ']');
  231. }
  232. // si on a utilisé l'auto-discover, notre url va avoir changé
  233. $subscribe_url = $feed->subscribe_url ();
  234. if (!is_null ($subscribe_url) && $subscribe_url != $this->url) {
  235. if ($this->httpAuth != '') {
  236. // on enlève les id si authentification HTTP
  237. $subscribe_url = preg_replace ('#((.+)://)((.+)@)(.+)#', '${1}${5}', $subscribe_url);
  238. }
  239. $this->_url ($subscribe_url);
  240. }
  241. $title = $feed->get_title ();
  242. $this->_name (!is_null ($title) ? $title : $this->url);
  243. $this->_website ($feed->get_link ());
  244. $this->_description ($feed->get_description ());
  245. // et on charge les articles du flux
  246. $this->loadEntries ($feed);
  247. }
  248. }
  249. }
  250. private function loadEntries ($feed) {
  251. $entries = array ();
  252. foreach ($feed->get_items () as $item) {
  253. $title = html_only_entity_decode (strip_tags ($item->get_title ()));
  254. $author = $item->get_author ();
  255. $link = $item->get_permalink ();
  256. $date = @strtotime ($item->get_date ());
  257. // gestion des tags (catégorie == tag)
  258. $tags_tmp = $item->get_categories ();
  259. $tags = array ();
  260. if (!is_null ($tags_tmp)) {
  261. foreach ($tags_tmp as $tag) {
  262. $tags[] = html_only_entity_decode ($tag->get_label ());
  263. }
  264. }
  265. $content = html_only_entity_decode ($item->get_content ());
  266. $elinks = array();
  267. foreach ($item->get_enclosures() as $enclosure) {
  268. $elink = $enclosure->get_link();
  269. if (array_key_exists($elink, $elinks)) continue;
  270. $elinks[$elink] = '1';
  271. $mime = strtolower($enclosure->get_type());
  272. if (strpos($mime, 'image/') === 0) {
  273. $content .= '<br /><img src="' . $elink . '" alt="" />';
  274. }
  275. }
  276. $entry = new FreshRSS_Entry (
  277. $this->id (),
  278. $item->get_id (),
  279. !is_null ($title) ? $title : '',
  280. !is_null ($author) ? html_only_entity_decode ($author->name) : '',
  281. !is_null ($content) ? $content : '',
  282. !is_null ($link) ? $link : '',
  283. $date ? $date : time ()
  284. );
  285. $entry->_tags ($tags);
  286. // permet de récupérer le contenu des flux tronqués
  287. $entry->loadCompleteContent($this->pathEntries());
  288. $entries[] = $entry;
  289. }
  290. $this->entries = $entries;
  291. }
  292. }