Feed.php 8.3 KB

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