Feed.php 8.2 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 = false;
  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. if ($value) {
  160. $value = true;
  161. } else {
  162. $value = false;
  163. }
  164. $this->error = $value;
  165. }
  166. public function _keepHistory ($value) {
  167. if ($value) {
  168. $value = true;
  169. } else {
  170. $value = false;
  171. }
  172. $this->keep_history = $value;
  173. }
  174. public function _nbNotRead ($value) {
  175. $this->nbNotRead = ctype_digit ($value) ? intval ($value) : -1;
  176. }
  177. public function _nbEntries ($value) {
  178. $this->nbEntries = ctype_digit ($value) ? intval ($value) : -1;
  179. }
  180. public function load () {
  181. if (!is_null ($this->url)) {
  182. if (CACHE_PATH === false) {
  183. throw new Minz_FileNotExistException (
  184. 'CACHE_PATH',
  185. Minz_Exception::ERROR
  186. );
  187. } else {
  188. $feed = new SimplePie ();
  189. $feed->set_useragent(Minz_Translate::t ('freshrss') . '/' . FRESHRSS_VERSION . ' (' . PHP_OS . '; ' . FRESHRSS_WEBSITE . ') ' . SIMPLEPIE_NAME . '/' . SIMPLEPIE_VERSION);
  190. $url = htmlspecialchars_decode ($this->url, ENT_QUOTES);
  191. if ($this->httpAuth != '') {
  192. $url = preg_replace ('#((.+)://)(.+)#', '${1}' . $this->httpAuth . '@${3}', $url);
  193. }
  194. $feed->set_feed_url ($url);
  195. $feed->set_cache_location (CACHE_PATH);
  196. $feed->set_cache_duration(1500);
  197. $feed->strip_htmltags (array (
  198. 'base', 'blink', 'body', 'doctype', 'embed',
  199. 'font', 'form', 'frame', 'frameset', 'html',
  200. 'input', 'marquee', 'meta', 'noscript',
  201. 'object', 'param', 'plaintext', 'script', 'style',
  202. ));
  203. $feed->strip_attributes(array_merge($feed->strip_attributes, array(
  204. 'autoplay', 'onload', 'onunload', 'onclick', 'ondblclick', 'onmousedown', 'onmouseup',
  205. 'onmouseover', 'onmousemove', 'onmouseout', 'onfocus', 'onblur',
  206. 'onkeypress', 'onkeydown', 'onkeyup', 'onselect', 'onchange', 'seamless')));
  207. $feed->add_attributes(array(
  208. 'img' => array('lazyload' => ''), //http://www.w3.org/TR/resource-priorities/
  209. 'audio' => array('preload' => 'none'),
  210. 'iframe' => array('postpone' => '', 'sandbox' => 'allow-scripts allow-same-origin'),
  211. 'video' => array('postpone' => '', 'preload' => 'none'),
  212. ));
  213. $feed->set_url_replacements(array(
  214. 'a' => 'href',
  215. 'area' => 'href',
  216. 'audio' => 'src',
  217. 'blockquote' => 'cite',
  218. 'del' => 'cite',
  219. 'form' => 'action',
  220. 'iframe' => 'src',
  221. 'img' => array(
  222. 'longdesc',
  223. 'src'
  224. ),
  225. 'input' => 'src',
  226. 'ins' => 'cite',
  227. 'q' => 'cite',
  228. 'source' => 'src',
  229. 'track' => 'src',
  230. 'video' => array(
  231. 'poster',
  232. 'src',
  233. ),
  234. ));
  235. $feed->init ();
  236. if ($feed->error ()) {
  237. throw new FreshRSS_Feed_Exception ($feed->error . ' [' . $url . ']');
  238. }
  239. // si on a utilisé l'auto-discover, notre url va avoir changé
  240. $subscribe_url = $feed->subscribe_url ();
  241. if (!is_null ($subscribe_url) && $subscribe_url != $this->url) {
  242. if ($this->httpAuth != '') {
  243. // on enlève les id si authentification HTTP
  244. $subscribe_url = preg_replace ('#((.+)://)((.+)@)(.+)#', '${1}${5}', $subscribe_url);
  245. }
  246. $this->_url ($subscribe_url);
  247. }
  248. $title = $feed->get_title ();
  249. $this->_name (!is_null ($title) ? $title : $this->url);
  250. $this->_website ($feed->get_link ());
  251. $this->_description ($feed->get_description ());
  252. // et on charge les articles du flux
  253. $this->loadEntries ($feed);
  254. }
  255. }
  256. }
  257. private function loadEntries ($feed) {
  258. $entries = array ();
  259. foreach ($feed->get_items () as $item) {
  260. $title = html_only_entity_decode (strip_tags ($item->get_title ()));
  261. $author = $item->get_author ();
  262. $link = $item->get_permalink ();
  263. $date = @strtotime ($item->get_date ());
  264. // gestion des tags (catégorie == tag)
  265. $tags_tmp = $item->get_categories ();
  266. $tags = array ();
  267. if (!is_null ($tags_tmp)) {
  268. foreach ($tags_tmp as $tag) {
  269. $tags[] = html_only_entity_decode ($tag->get_label ());
  270. }
  271. }
  272. $content = html_only_entity_decode ($item->get_content ());
  273. $elinks = array();
  274. foreach ($item->get_enclosures() as $enclosure) {
  275. $elink = $enclosure->get_link();
  276. if (array_key_exists($elink, $elinks)) continue;
  277. $elinks[$elink] = '1';
  278. $mime = strtolower($enclosure->get_type());
  279. if (strpos($mime, 'image/') === 0) {
  280. $content .= '<br /><img src="' . $elink . '" alt="" />';
  281. }
  282. }
  283. $entry = new FreshRSS_Entry (
  284. $this->id (),
  285. $item->get_id (),
  286. !is_null ($title) ? $title : '',
  287. !is_null ($author) ? html_only_entity_decode ($author->name) : '',
  288. !is_null ($content) ? $content : '',
  289. !is_null ($link) ? $link : '',
  290. $date ? $date : time ()
  291. );
  292. $entry->_tags ($tags);
  293. // permet de récupérer le contenu des flux tronqués
  294. $entry->loadCompleteContent($this->pathEntries());
  295. $entries[] = $entry;
  296. }
  297. $this->entries = $entries;
  298. }
  299. }