Feed.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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. return $this->entries === null ? array() : $this->entries;
  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. $value = intval($value);
  127. $this->category = $value >= 0 ? $value : 0;
  128. }
  129. public function _name ($value) {
  130. $this->name = $value === null ? '' : $value;
  131. }
  132. public function _website ($value, $validate=true) {
  133. if ($validate) {
  134. $value = checkUrl($value);
  135. }
  136. if (empty ($value)) {
  137. $value = '';
  138. }
  139. $this->website = $value;
  140. }
  141. public function _description ($value) {
  142. $this->description = $value === null ? '' : $value;
  143. }
  144. public function _lastUpdate ($value) {
  145. $this->lastUpdate = $value;
  146. }
  147. public function _priority ($value) {
  148. $value = intval($value);
  149. $this->priority = $value >= 0 ? $value : 10;
  150. }
  151. public function _pathEntries ($value) {
  152. $this->pathEntries = $value;
  153. }
  154. public function _httpAuth ($value) {
  155. $this->httpAuth = $value;
  156. }
  157. public function _error ($value) {
  158. $this->error = (bool)$value;
  159. }
  160. public function _keepHistory ($value) {
  161. $value = intval($value);
  162. $value = min($value, 1000000);
  163. $value = max($value, -2);
  164. $this->keep_history = $value;
  165. }
  166. public function _nbNotRead ($value) {
  167. $this->nbNotRead = intval($value);
  168. }
  169. public function _nbEntries ($value) {
  170. $this->nbEntries = intval($value);
  171. }
  172. public function load ($loadDetails = false) {
  173. if ($this->url !== null) {
  174. if (CACHE_PATH === false) {
  175. throw new Minz_FileNotExistException (
  176. 'CACHE_PATH',
  177. Minz_Exception::ERROR
  178. );
  179. } else {
  180. $url = htmlspecialchars_decode ($this->url, ENT_QUOTES);
  181. if ($this->httpAuth != '') {
  182. $url = preg_replace ('#((.+)://)(.+)#', '${1}' . $this->httpAuth . '@${3}', $url);
  183. }
  184. $feed = customSimplePie();
  185. $feed->set_feed_url ($url);
  186. $mtime = $feed->init();
  187. if ((!$mtime) || $feed->error()) {
  188. throw new FreshRSS_Feed_Exception ($feed->error() . ' [' . $url . ']');
  189. }
  190. // si on a utilisé l'auto-discover, notre url va avoir changé
  191. $subscribe_url = $feed->subscribe_url ();
  192. if ($subscribe_url !== null && $subscribe_url !== $this->url) {
  193. if ($this->httpAuth != '') {
  194. // on enlève les id si authentification HTTP
  195. $subscribe_url = preg_replace ('#((.+)://)((.+)@)(.+)#', '${1}${5}', $subscribe_url);
  196. }
  197. $this->_url ($subscribe_url);
  198. }
  199. if ($loadDetails) {
  200. $title = strtr(html_only_entity_decode($feed->get_title()), array('<' => '&lt;', '>' => '&gt;', '"' => '&quot;')); //HTML to HTML-PRE //ENT_COMPAT except &
  201. $this->_name ($title == '' ? $this->url : $title);
  202. $this->_website(html_only_entity_decode($feed->get_link()));
  203. $this->_description(html_only_entity_decode($feed->get_description()));
  204. }
  205. if (($mtime === true) || ($mtime > $this->lastUpdate)) {
  206. syslog(LOG_DEBUG, 'FreshRSS no cache ' . $mtime . ' > ' . $this->lastUpdate . ' for ' . $subscribe_url);
  207. $this->loadEntries($feed); // et on charge les articles du flux
  208. } else {
  209. syslog(LOG_DEBUG, 'FreshRSS use cache for ' . $subscribe_url);
  210. $this->entries = array();
  211. }
  212. $feed->__destruct(); //http://simplepie.org/wiki/faq/i_m_getting_memory_leaks
  213. unset($feed);
  214. }
  215. }
  216. }
  217. private function loadEntries ($feed) {
  218. $entries = array ();
  219. foreach ($feed->get_items () as $item) {
  220. $title = html_only_entity_decode (strip_tags ($item->get_title ()));
  221. $author = $item->get_author ();
  222. $link = $item->get_permalink ();
  223. $date = @strtotime ($item->get_date ());
  224. // gestion des tags (catégorie == tag)
  225. $tags_tmp = $item->get_categories ();
  226. $tags = array ();
  227. if ($tags_tmp !== null) {
  228. foreach ($tags_tmp as $tag) {
  229. $tags[] = html_only_entity_decode ($tag->get_label ());
  230. }
  231. }
  232. $content = html_only_entity_decode ($item->get_content ());
  233. $elinks = array();
  234. foreach ($item->get_enclosures() as $enclosure) {
  235. $elink = $enclosure->get_link();
  236. if (empty($elinks[$elink])) {
  237. $elinks[$elink] = '1';
  238. $mime = strtolower($enclosure->get_type());
  239. if (strpos($mime, 'image/') === 0) {
  240. $content .= '<br /><img src="' . $elink . '" alt="" />';
  241. }
  242. }
  243. }
  244. $entry = new FreshRSS_Entry (
  245. $this->id (),
  246. $item->get_id (),
  247. $title === null ? '' : $title,
  248. $author === null ? '' : html_only_entity_decode ($author->name),
  249. $content === null ? '' : $content,
  250. $link === null ? '' : $link,
  251. $date ? $date : time ()
  252. );
  253. $entry->_tags ($tags);
  254. // permet de récupérer le contenu des flux tronqués
  255. $entry->loadCompleteContent($this->pathEntries());
  256. $entries[] = $entry;
  257. unset($item);
  258. }
  259. $this->entries = $entries;
  260. }
  261. function lock() {
  262. $lock = TMP_PATH . '/' . md5(Minz_Configuration::salt() . $this->url) . '.freshrss.lock';
  263. if (file_exists($lock) && ((time() - @filemtime($lock)) > 3600)) {
  264. @unlink($lock);
  265. }
  266. if (($handle = @fopen($lock, 'x')) === false) {
  267. return false;
  268. }
  269. //register_shutdown_function('unlink', $lock);
  270. @fclose($handle);
  271. return true;
  272. }
  273. function unlock() {
  274. $lock = TMP_PATH . '/' . md5(Minz_Configuration::salt() . $this->url) . '.freshrss.lock';
  275. @unlink($lock);
  276. }
  277. }