Feed.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  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. private $hubUrl = '';
  22. private $selfUrl = '';
  23. public function __construct($url, $validate=true) {
  24. if ($validate) {
  25. $this->_url($url);
  26. } else {
  27. $this->url = $url;
  28. }
  29. }
  30. public static function example() {
  31. $f = new FreshRSS_Feed('http://example.net/', false);
  32. $f->faviconPrepare();
  33. return $f;
  34. }
  35. public function id() {
  36. return $this->id;
  37. }
  38. public function hash() {
  39. if ($this->hash === null) {
  40. $salt = FreshRSS_Context::$system_conf->salt;
  41. $this->hash = hash('crc32b', $salt . $this->url);
  42. }
  43. return $this->hash;
  44. }
  45. public function url() {
  46. return $this->url;
  47. }
  48. public function selfUrl() {
  49. return $this->selfUrl;
  50. }
  51. public function hubUrl() {
  52. return $this->hubUrl;
  53. }
  54. public function category() {
  55. return $this->category;
  56. }
  57. public function entries() {
  58. return $this->entries === null ? array() : $this->entries;
  59. }
  60. public function name() {
  61. return $this->name;
  62. }
  63. public function website() {
  64. return $this->website;
  65. }
  66. public function description() {
  67. return $this->description;
  68. }
  69. public function lastUpdate() {
  70. return $this->lastUpdate;
  71. }
  72. public function priority() {
  73. return $this->priority;
  74. }
  75. public function pathEntries() {
  76. return $this->pathEntries;
  77. }
  78. public function httpAuth($raw = true) {
  79. if ($raw) {
  80. return $this->httpAuth;
  81. } else {
  82. $pos_colon = strpos($this->httpAuth, ':');
  83. $user = substr($this->httpAuth, 0, $pos_colon);
  84. $pass = substr($this->httpAuth, $pos_colon + 1);
  85. return array(
  86. 'username' => $user,
  87. 'password' => $pass
  88. );
  89. }
  90. }
  91. public function inError() {
  92. return $this->error;
  93. }
  94. public function keepHistory() {
  95. return $this->keep_history;
  96. }
  97. public function ttl() {
  98. return $this->ttl;
  99. }
  100. // public function ttlExpire() {
  101. // $ttl = $this->ttl;
  102. // if ($ttl == -2) { //Default
  103. // $ttl = FreshRSS_Context::$user_conf->ttl_default;
  104. // }
  105. // if ($ttl == -1) { //Never
  106. // $ttl = 64000000; //~2 years. Good enough for PubSubHubbub logic
  107. // }
  108. // return $this->lastUpdate + $ttl;
  109. // }
  110. public function nbEntries() {
  111. if ($this->nbEntries < 0) {
  112. $feedDAO = FreshRSS_Factory::createFeedDao();
  113. $this->nbEntries = $feedDAO->countEntries($this->id());
  114. }
  115. return $this->nbEntries;
  116. }
  117. public function nbNotRead() {
  118. if ($this->nbNotRead < 0) {
  119. $feedDAO = FreshRSS_Factory::createFeedDao();
  120. $this->nbNotRead = $feedDAO->countNotRead($this->id());
  121. }
  122. return $this->nbNotRead;
  123. }
  124. public function faviconPrepare() {
  125. $file = DATA_PATH . '/favicons/' . $this->hash() . '.txt';
  126. if (!file_exists($file)) {
  127. $t = $this->website;
  128. if ($t == '') {
  129. $t = $this->url;
  130. }
  131. file_put_contents($file, $t);
  132. }
  133. }
  134. public static function faviconDelete($hash) {
  135. $path = DATA_PATH . '/favicons/' . $hash;
  136. @unlink($path . '.ico');
  137. @unlink($path . '.txt');
  138. }
  139. public function favicon() {
  140. return Minz_Url::display('/f.php?' . $this->hash());
  141. }
  142. public function _id($value) {
  143. $this->id = $value;
  144. }
  145. public function _url($value, $validate=true) {
  146. $this->hash = null;
  147. if ($validate) {
  148. $value = checkUrl($value);
  149. }
  150. if (empty($value)) {
  151. throw new FreshRSS_BadUrl_Exception($value);
  152. }
  153. $this->url = $value;
  154. }
  155. public function _category($value) {
  156. $value = intval($value);
  157. $this->category = $value >= 0 ? $value : 0;
  158. }
  159. public function _name($value) {
  160. $this->name = $value === null ? '' : $value;
  161. }
  162. public function _website($value, $validate=true) {
  163. if ($validate) {
  164. $value = checkUrl($value);
  165. }
  166. if (empty($value)) {
  167. $value = '';
  168. }
  169. $this->website = $value;
  170. }
  171. public function _description($value) {
  172. $this->description = $value === null ? '' : $value;
  173. }
  174. public function _lastUpdate($value) {
  175. $this->lastUpdate = $value;
  176. }
  177. public function _priority($value) {
  178. $value = intval($value);
  179. $this->priority = $value >= 0 ? $value : 10;
  180. }
  181. public function _pathEntries($value) {
  182. $this->pathEntries = $value;
  183. }
  184. public function _httpAuth($value) {
  185. $this->httpAuth = $value;
  186. }
  187. public function _error($value) {
  188. $this->error = (bool)$value;
  189. }
  190. public function _keepHistory($value) {
  191. $value = intval($value);
  192. $value = min($value, 1000000);
  193. $value = max($value, -2);
  194. $this->keep_history = $value;
  195. }
  196. public function _ttl($value) {
  197. $value = intval($value);
  198. $value = min($value, 100000000);
  199. $value = max($value, -2);
  200. $this->ttl = $value;
  201. }
  202. public function _nbNotRead($value) {
  203. $this->nbNotRead = intval($value);
  204. }
  205. public function _nbEntries($value) {
  206. $this->nbEntries = intval($value);
  207. }
  208. public function load($loadDetails = false) {
  209. if ($this->url !== null) {
  210. if (CACHE_PATH === false) {
  211. throw new Minz_FileNotExistException(
  212. 'CACHE_PATH',
  213. Minz_Exception::ERROR
  214. );
  215. } else {
  216. $url = htmlspecialchars_decode($this->url, ENT_QUOTES);
  217. if ($this->httpAuth != '') {
  218. $url = preg_replace('#((.+)://)(.+)#', '${1}' . $this->httpAuth . '@${3}', $url);
  219. }
  220. $feed = customSimplePie();
  221. if (substr($url, -11) === '#force_feed') {
  222. $feed->force_feed(true);
  223. $url = substr($url, 0, -11);
  224. }
  225. $feed->set_feed_url($url);
  226. if (!$loadDetails) { //Only activates auto-discovery when adding a new feed
  227. $feed->set_autodiscovery_level(SIMPLEPIE_LOCATOR_NONE);
  228. }
  229. $mtime = $feed->init();
  230. if ((!$mtime) || $feed->error()) {
  231. $errorMessage = $feed->error();
  232. throw new FreshRSS_Feed_Exception(($errorMessage == '' ? 'Feed error' : $errorMessage) . ' [' . $url . ']');
  233. }
  234. $links = $feed->get_links('self');
  235. $this->selfUrl = isset($links[0]) ? $links[0] : null;
  236. $links = $feed->get_links('hub');
  237. $this->hubUrl = isset($links[0]) ? $links[0] : null;
  238. if ($loadDetails) {
  239. // si on a utilisé l'auto-discover, notre url va avoir changé
  240. $subscribe_url = $feed->subscribe_url(false);
  241. $title = strtr(html_only_entity_decode($feed->get_title()), array('<' => '&lt;', '>' => '&gt;', '"' => '&quot;')); //HTML to HTML-PRE //ENT_COMPAT except &
  242. $this->_name($title == '' ? $url : $title);
  243. $this->_website(html_only_entity_decode($feed->get_link()));
  244. $this->_description(html_only_entity_decode($feed->get_description()));
  245. } else {
  246. //The case of HTTP 301 Moved Permanently
  247. $subscribe_url = $feed->subscribe_url(true);
  248. }
  249. $clean_url = SimplePie_Misc::url_remove_credentials($subscribe_url);
  250. if ($subscribe_url !== null && $subscribe_url !== $url) {
  251. $this->_url($clean_url);
  252. }
  253. if (($mtime === true) || ($mtime > $this->lastUpdate)) {
  254. //Minz_Log::debug('FreshRSS no cache ' . $mtime . ' > ' . $this->lastUpdate . ' for ' . $clean_url);
  255. $this->loadEntries($feed); // et on charge les articles du flux
  256. } else {
  257. //Minz_Log::debug('FreshRSS use cache for ' . $clean_url);
  258. $this->entries = array();
  259. }
  260. $feed->__destruct(); //http://simplepie.org/wiki/faq/i_m_getting_memory_leaks
  261. unset($feed);
  262. }
  263. }
  264. }
  265. public function loadEntries($feed) {
  266. $entries = array();
  267. foreach ($feed->get_items() as $item) {
  268. $title = html_only_entity_decode(strip_tags($item->get_title()));
  269. $author = $item->get_author();
  270. $link = $item->get_permalink();
  271. $date = @strtotime($item->get_date());
  272. // gestion des tags (catégorie == tag)
  273. $tags_tmp = $item->get_categories();
  274. $tags = array();
  275. if ($tags_tmp !== null) {
  276. foreach ($tags_tmp as $tag) {
  277. $tags[] = html_only_entity_decode($tag->get_label());
  278. }
  279. }
  280. $content = html_only_entity_decode($item->get_content());
  281. $elinks = array();
  282. foreach ($item->get_enclosures() as $enclosure) {
  283. $elink = $enclosure->get_link();
  284. if (empty($elinks[$elink])) {
  285. $elinks[$elink] = '1';
  286. $mime = strtolower($enclosure->get_type());
  287. if (strpos($mime, 'image/') === 0) {
  288. $content .= '<br /><img lazyload="" postpone="" src="' . $elink . '" alt="" />';
  289. } elseif (strpos($mime, 'audio/') === 0) {
  290. $content .= '<br /><audio lazyload="" postpone="" preload="none" src="' . $elink . '" controls="controls" />';
  291. } elseif (strpos($mime, 'video/') === 0) {
  292. $content .= '<br /><video lazyload="" postpone="" preload="none" src="' . $elink . '" controls="controls" />';
  293. } else {
  294. unset($elinks[$elink]);
  295. }
  296. }
  297. }
  298. $entry = new FreshRSS_Entry(
  299. $this->id(),
  300. $item->get_id(),
  301. $title === null ? '' : $title,
  302. $author === null ? '' : html_only_entity_decode($author->name),
  303. $content === null ? '' : $content,
  304. $link === null ? '' : $link,
  305. $date ? $date : time()
  306. );
  307. $entry->_tags($tags);
  308. // permet de récupérer le contenu des flux tronqués
  309. $entry->loadCompleteContent($this->pathEntries());
  310. $entries[] = $entry;
  311. unset($item);
  312. }
  313. $this->entries = $entries;
  314. }
  315. function lock() {
  316. $this->lockPath = TMP_PATH . '/' . $this->hash() . '.freshrss.lock';
  317. if (file_exists($this->lockPath) && ((time() - @filemtime($this->lockPath)) > 3600)) {
  318. @unlink($this->lockPath);
  319. }
  320. if (($handle = @fopen($this->lockPath, 'x')) === false) {
  321. return false;
  322. }
  323. //register_shutdown_function('unlink', $this->lockPath);
  324. @fclose($handle);
  325. return true;
  326. }
  327. function unlock() {
  328. @unlink($this->lockPath);
  329. }
  330. //<PubSubHubbub>
  331. function pubSubHubbubEnabled() {
  332. $url = $this->selfUrl ? $this->selfUrl : $this->url;
  333. $hubFilename = PSHB_PATH . '/feeds/' . base64url_encode($url) . '/!hub.json';
  334. if ($hubFile = @file_get_contents($hubFilename)) {
  335. $hubJson = json_decode($hubFile, true);
  336. if ($hubJson && empty($hubJson['error']) &&
  337. (empty($hubJson['lease_end']) || $hubJson['lease_end'] > time())) {
  338. return true;
  339. }
  340. }
  341. return false;
  342. }
  343. function pubSubHubbubError($error = true) {
  344. $url = $this->selfUrl ? $this->selfUrl : $this->url;
  345. $hubFilename = PSHB_PATH . '/feeds/' . base64url_encode($url) . '/!hub.json';
  346. $hubFile = @file_get_contents($hubFilename);
  347. $hubJson = $hubFile ? json_decode($hubFile, true) : array();
  348. if (!isset($hubJson['error']) || $hubJson['error'] !== (bool)$error) {
  349. $hubJson['error'] = (bool)$error;
  350. file_put_contents($hubFilename, json_encode($hubJson));
  351. file_put_contents(USERS_PATH . '/_/log_pshb.txt', date('c') . "\t"
  352. . 'Set error to ' . ($error ? 1 : 0) . ' for ' . $url . "\n", FILE_APPEND);
  353. }
  354. return false;
  355. }
  356. function pubSubHubbubPrepare() {
  357. $key = '';
  358. if (FreshRSS_Context::$system_conf->base_url && $this->hubUrl && $this->selfUrl && @is_dir(PSHB_PATH)) {
  359. $path = PSHB_PATH . '/feeds/' . base64url_encode($this->selfUrl);
  360. $hubFilename = $path . '/!hub.json';
  361. if ($hubFile = @file_get_contents($hubFilename)) {
  362. $hubJson = json_decode($hubFile, true);
  363. if (!$hubJson || empty($hubJson['key']) || !ctype_xdigit($hubJson['key'])) {
  364. $text = 'Invalid JSON for PubSubHubbub: ' . $this->url;
  365. Minz_Log::warning($text);
  366. file_put_contents(USERS_PATH . '/_/log_pshb.txt', date('c') . "\t" . $text . "\n", FILE_APPEND);
  367. return false;
  368. }
  369. if ((!empty($hubJson['lease_end'])) && ($hubJson['lease_end'] < (time() + (3600 * 23)))) { //TODO: Make a better policy
  370. $text = 'PubSubHubbub lease ends at '
  371. . date('c', empty($hubJson['lease_end']) ? time() : $hubJson['lease_end'])
  372. . ' and needs renewal: ' . $this->url;
  373. Minz_Log::warning($text);
  374. file_put_contents(USERS_PATH . '/_/log_pshb.txt', date('c') . "\t" . $text . "\n", FILE_APPEND);
  375. $key = $hubJson['key']; //To renew our lease
  376. } elseif (((!empty($hubJson['error'])) || empty($hubJson['lease_end'])) &&
  377. (empty($hubJson['lease_start']) || $hubJson['lease_start'] < time() - (3600 * 23))) { //Do not renew too often
  378. $key = $hubJson['key']; //To renew our lease
  379. }
  380. } else {
  381. @mkdir($path, 0777, true);
  382. $key = sha1($path . FreshRSS_Context::$system_conf->salt . uniqid(mt_rand(), true));
  383. $hubJson = array(
  384. 'hub' => $this->hubUrl,
  385. 'key' => $key,
  386. );
  387. file_put_contents($hubFilename, json_encode($hubJson));
  388. @mkdir(PSHB_PATH . '/keys/');
  389. file_put_contents(PSHB_PATH . '/keys/' . $key . '.txt', base64url_encode($this->selfUrl));
  390. $text = 'PubSubHubbub prepared for ' . $this->url;
  391. Minz_Log::debug($text);
  392. file_put_contents(USERS_PATH . '/_/log_pshb.txt', date('c') . "\t" . $text . "\n", FILE_APPEND);
  393. }
  394. $currentUser = Minz_Session::param('currentUser');
  395. if (ctype_alnum($currentUser) && !file_exists($path . '/' . $currentUser . '.txt')) {
  396. touch($path . '/' . $currentUser . '.txt');
  397. }
  398. }
  399. return $key;
  400. }
  401. //Parameter true to subscribe, false to unsubscribe.
  402. function pubSubHubbubSubscribe($state) {
  403. if (FreshRSS_Context::$system_conf->base_url && $this->hubUrl && $this->selfUrl) {
  404. $hubFilename = PSHB_PATH . '/feeds/' . base64url_encode($this->selfUrl) . '/!hub.json';
  405. $hubFile = @file_get_contents($hubFilename);
  406. if ($hubFile === false) {
  407. Minz_Log::warning('JSON not found for PubSubHubbub: ' . $this->url);
  408. return false;
  409. }
  410. $hubJson = json_decode($hubFile, true);
  411. if (!$hubJson || empty($hubJson['key']) || !ctype_xdigit($hubJson['key'])) {
  412. Minz_Log::warning('Invalid JSON for PubSubHubbub: ' . $this->url);
  413. return false;
  414. }
  415. $callbackUrl = checkUrl(FreshRSS_Context::$system_conf->base_url . 'api/pshb.php?k=' . $hubJson['key']);
  416. if ($callbackUrl == '') {
  417. Minz_Log::warning('Invalid callback for PubSubHubbub: ' . $this->url);
  418. return false;
  419. }
  420. if (!$state) { //unsubscribe
  421. $hubJson['lease_end'] = time() - 60;
  422. file_put_contents($hubFilename, json_encode($hubJson));
  423. }
  424. $ch = curl_init();
  425. curl_setopt_array($ch, array(
  426. CURLOPT_URL => $this->hubUrl,
  427. CURLOPT_FOLLOWLOCATION => true,
  428. CURLOPT_RETURNTRANSFER => true,
  429. CURLOPT_USERAGENT => _t('gen.freshrss') . '/' . FRESHRSS_VERSION . ' (' . PHP_OS . '; ' . FRESHRSS_WEBSITE . ')',
  430. CURLOPT_POSTFIELDS => 'hub.verify=sync'
  431. . '&hub.mode=' . ($state ? 'subscribe' : 'unsubscribe')
  432. . '&hub.topic=' . urlencode($this->selfUrl)
  433. . '&hub.callback=' . urlencode($callbackUrl)
  434. )
  435. );
  436. $response = curl_exec($ch);
  437. $info = curl_getinfo($ch);
  438. file_put_contents(USERS_PATH . '/_/log_pshb.txt', date('c') . "\t" .
  439. 'PubSubHubbub ' . ($state ? 'subscribe' : 'unsubscribe') . ' to ' . $this->selfUrl .
  440. ' with callback ' . $callbackUrl . ': ' . $info['http_code'] . ' ' . $response . "\n", FILE_APPEND);
  441. if (substr($info['http_code'], 0, 1) == '2') {
  442. return true;
  443. } else {
  444. $hubJson['lease_start'] = time(); //Prevent trying again too soon
  445. $hubJson['error'] = true;
  446. file_put_contents($hubFilename, json_encode($hubJson));
  447. return false;
  448. }
  449. }
  450. return false;
  451. }
  452. //</PubSubHubbub>
  453. }