Feed.php 8.3 KB

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