Feed.php 8.5 KB

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