Feed.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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 $ttl = -2;
  19. private $hash = null;
  20. private $lockPath = '';
  21. public function __construct($url, $validate=true) {
  22. if ($validate) {
  23. $this->_url($url);
  24. } else {
  25. $this->url = $url;
  26. }
  27. }
  28. public static function example() {
  29. $f = new FreshRSS_Feed('http://example.net/', false);
  30. $f->faviconPrepare();
  31. return $f;
  32. }
  33. public function id() {
  34. return $this->id;
  35. }
  36. public function hash() {
  37. if ($this->hash === null) {
  38. $this->hash = hash('crc32b', Minz_Configuration::salt() . $this->url);
  39. }
  40. return $this->hash;
  41. }
  42. public function url() {
  43. return $this->url;
  44. }
  45. public function category() {
  46. return $this->category;
  47. }
  48. public function entries() {
  49. return $this->entries === null ? array() : $this->entries;
  50. }
  51. public function name() {
  52. return $this->name;
  53. }
  54. public function website() {
  55. return $this->website;
  56. }
  57. public function description() {
  58. return $this->description;
  59. }
  60. public function lastUpdate() {
  61. return $this->lastUpdate;
  62. }
  63. public function priority() {
  64. return $this->priority;
  65. }
  66. public function pathEntries() {
  67. return $this->pathEntries;
  68. }
  69. public function httpAuth($raw = true) {
  70. if ($raw) {
  71. return $this->httpAuth;
  72. } else {
  73. $pos_colon = strpos($this->httpAuth, ':');
  74. $user = substr($this->httpAuth, 0, $pos_colon);
  75. $pass = substr($this->httpAuth, $pos_colon + 1);
  76. return array(
  77. 'username' => $user,
  78. 'password' => $pass
  79. );
  80. }
  81. }
  82. public function inError() {
  83. return $this->error;
  84. }
  85. public function keepHistory() {
  86. return $this->keep_history;
  87. }
  88. public function ttl() {
  89. return $this->ttl;
  90. }
  91. public function nbEntries() {
  92. if ($this->nbEntries < 0) {
  93. $feedDAO = FreshRSS_Factory::createFeedDao();
  94. $this->nbEntries = $feedDAO->countEntries($this->id());
  95. }
  96. return $this->nbEntries;
  97. }
  98. public function nbNotRead() {
  99. if ($this->nbNotRead < 0) {
  100. $feedDAO = FreshRSS_Factory::createFeedDao();
  101. $this->nbNotRead = $feedDAO->countNotRead($this->id());
  102. }
  103. return $this->nbNotRead;
  104. }
  105. public function faviconPrepare() {
  106. $file = DATA_PATH . '/favicons/' . $this->hash() . '.txt';
  107. if (!file_exists($file)) {
  108. $t = $this->website;
  109. if ($t == '') {
  110. $t = $this->url;
  111. }
  112. file_put_contents($file, $t);
  113. }
  114. }
  115. public static function faviconDelete($hash) {
  116. $path = DATA_PATH . '/favicons/' . $hash;
  117. @unlink($path . '.ico');
  118. @unlink($path . '.txt');
  119. }
  120. public function favicon() {
  121. return Minz_Url::display('/f.php?' . $this->hash());
  122. }
  123. public function _id($value) {
  124. $this->id = $value;
  125. }
  126. public function _url($value, $validate=true) {
  127. $this->hash = null;
  128. if ($validate) {
  129. $value = checkUrl($value);
  130. }
  131. if (empty($value)) {
  132. throw new FreshRSS_BadUrl_Exception($value);
  133. }
  134. $this->url = $value;
  135. }
  136. public function _category($value) {
  137. $value = intval($value);
  138. $this->category = $value >= 0 ? $value : 0;
  139. }
  140. public function _name($value) {
  141. $this->name = $value === null ? '' : $value;
  142. }
  143. public function _website($value, $validate=true) {
  144. if ($validate) {
  145. $value = checkUrl($value);
  146. }
  147. if (empty($value)) {
  148. $value = '';
  149. }
  150. $this->website = $value;
  151. }
  152. public function _description($value) {
  153. $this->description = $value === null ? '' : $value;
  154. }
  155. public function _lastUpdate($value) {
  156. $this->lastUpdate = $value;
  157. }
  158. public function _priority($value) {
  159. $value = intval($value);
  160. $this->priority = $value >= 0 ? $value : 10;
  161. }
  162. public function _pathEntries($value) {
  163. $this->pathEntries = $value;
  164. }
  165. public function _httpAuth($value) {
  166. $this->httpAuth = $value;
  167. }
  168. public function _error($value) {
  169. $this->error = (bool)$value;
  170. }
  171. public function _keepHistory($value) {
  172. $value = intval($value);
  173. $value = min($value, 1000000);
  174. $value = max($value, -2);
  175. $this->keep_history = $value;
  176. }
  177. public function _ttl($value) {
  178. $value = intval($value);
  179. $value = min($value, 100000000);
  180. $value = max($value, -2);
  181. $this->ttl = $value;
  182. }
  183. public function _nbNotRead($value) {
  184. $this->nbNotRead = intval($value);
  185. }
  186. public function _nbEntries($value) {
  187. $this->nbEntries = intval($value);
  188. }
  189. public function load($loadDetails = false) {
  190. if ($this->url !== null) {
  191. if (CACHE_PATH === false) {
  192. throw new Minz_FileNotExistException(
  193. 'CACHE_PATH',
  194. Minz_Exception::ERROR
  195. );
  196. } else {
  197. $url = htmlspecialchars_decode($this->url, ENT_QUOTES);
  198. if ($this->httpAuth != '') {
  199. $url = preg_replace('#((.+)://)(.+)#', '${1}' . $this->httpAuth . '@${3}', $url);
  200. }
  201. $feed = customSimplePie();
  202. $feed->set_feed_url($url);
  203. if (!$loadDetails) { //Only activates auto-discovery when adding a new feed
  204. $feed->set_autodiscovery_level(SIMPLEPIE_LOCATOR_NONE);
  205. }
  206. $mtime = $feed->init();
  207. if ((!$mtime) || $feed->error()) {
  208. throw new FreshRSS_Feed_Exception($feed->error() . ' [' . $url . ']');
  209. }
  210. if ($loadDetails) {
  211. // si on a utilisé l'auto-discover, notre url va avoir changé
  212. $subscribe_url = $feed->subscribe_url(false);
  213. $title = strtr(html_only_entity_decode($feed->get_title()), array('<' => '&lt;', '>' => '&gt;', '"' => '&quot;')); //HTML to HTML-PRE //ENT_COMPAT except &
  214. $this->_name($title == '' ? $this->url : $title);
  215. $this->_website(html_only_entity_decode($feed->get_link()));
  216. $this->_description(html_only_entity_decode($feed->get_description()));
  217. } else {
  218. //The case of HTTP 301 Moved Permanently
  219. $subscribe_url = $feed->subscribe_url(true);
  220. }
  221. if ($subscribe_url !== null && $subscribe_url !== $this->url) {
  222. if ($this->httpAuth != '') {
  223. // on enlève les id si authentification HTTP
  224. $subscribe_url = preg_replace('#((.+)://)((.+)@)(.+)#', '${1}${5}', $subscribe_url);
  225. }
  226. $this->_url($subscribe_url);
  227. }
  228. if (($mtime === true) ||($mtime > $this->lastUpdate)) {
  229. syslog(LOG_DEBUG, 'FreshRSS no cache ' . $mtime . ' > ' . $this->lastUpdate . ' for ' . $subscribe_url);
  230. $this->loadEntries($feed); // et on charge les articles du flux
  231. } else {
  232. syslog(LOG_DEBUG, 'FreshRSS use cache for ' . $subscribe_url);
  233. $this->entries = array();
  234. }
  235. $feed->__destruct(); //http://simplepie.org/wiki/faq/i_m_getting_memory_leaks
  236. unset($feed);
  237. }
  238. }
  239. }
  240. private function loadEntries($feed) {
  241. $entries = array();
  242. foreach ($feed->get_items() as $item) {
  243. $title = html_only_entity_decode(strip_tags($item->get_title()));
  244. $author = $item->get_author();
  245. $link = $item->get_permalink();
  246. $date = @strtotime($item->get_date());
  247. // gestion des tags (catégorie == tag)
  248. $tags_tmp = $item->get_categories();
  249. $tags = array();
  250. if ($tags_tmp !== null) {
  251. foreach ($tags_tmp as $tag) {
  252. $tags[] = html_only_entity_decode($tag->get_label());
  253. }
  254. }
  255. $content = html_only_entity_decode($item->get_content());
  256. $elinks = array();
  257. foreach ($item->get_enclosures() as $enclosure) {
  258. $elink = $enclosure->get_link();
  259. if (empty($elinks[$elink])) {
  260. $elinks[$elink] = '1';
  261. $mime = strtolower($enclosure->get_type());
  262. if (strpos($mime, 'image/') === 0) {
  263. $content .= '<br /><img lazyload="" postpone="" src="' . $elink . '" alt="" />';
  264. } elseif (strpos($mime, 'audio/') === 0) {
  265. $content .= '<br /><audio lazyload="" postpone="" preload="none" src="' . $elink . '" controls="controls" />';
  266. } elseif (strpos($mime, 'video/') === 0) {
  267. $content .= '<br /><video lazyload="" postpone="" preload="none" src="' . $elink . '" controls="controls" />';
  268. }
  269. }
  270. }
  271. $entry = new FreshRSS_Entry(
  272. $this->id(),
  273. $item->get_id(),
  274. $title === null ? '' : $title,
  275. $author === null ? '' : html_only_entity_decode($author->name),
  276. $content === null ? '' : $content,
  277. $link === null ? '' : $link,
  278. $date ? $date : time()
  279. );
  280. $entry->_tags($tags);
  281. // permet de récupérer le contenu des flux tronqués
  282. $entry->loadCompleteContent($this->pathEntries());
  283. $entries[] = $entry;
  284. unset($item);
  285. }
  286. $this->entries = $entries;
  287. }
  288. function lock() {
  289. $this->lockPath = TMP_PATH . '/' . $this->hash() . '.freshrss.lock';
  290. if (file_exists($this->lockPath) && ((time() - @filemtime($this->lockPath)) > 3600)) {
  291. @unlink($this->lockPath);
  292. }
  293. if (($handle = @fopen($this->lockPath, 'x')) === false) {
  294. return false;
  295. }
  296. //register_shutdown_function('unlink', $this->lockPath);
  297. @fclose($handle);
  298. return true;
  299. }
  300. function unlock() {
  301. @unlink($this->lockPath);
  302. }
  303. }