Feed.php 8.5 KB

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