Feed.php 8.3 KB

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