Feed.php 16 KB

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