Feed.php 8.9 KB

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