Feed.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  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. global $favicons_dir;
  126. require_once(LIB_PATH . '/favicons.php');
  127. $url = $this->website;
  128. if ($url == '') {
  129. $url = $this->url;
  130. }
  131. $txt = $favicons_dir . $this->hash() . '.txt';
  132. if (!file_exists($txt)) {
  133. file_put_contents($txt, $url);
  134. }
  135. if (FreshRSS_Context::$isCron) {
  136. $ico = $favicons_dir . $this->hash() . '.ico';
  137. $ico_mtime = @filemtime($ico);
  138. $txt_mtime = @filemtime($txt);
  139. if ($txt_mtime != false &&
  140. ($ico_mtime == false || $ico_mtime < $txt_mtime || ($ico_mtime < time() - (14 * 86400)))) {
  141. // no ico file or we should download a new one.
  142. $url = file_get_contents($txt);
  143. download_favicon($url, $ico) || touch($ico);
  144. }
  145. }
  146. }
  147. public static function faviconDelete($hash) {
  148. $path = DATA_PATH . '/favicons/' . $hash;
  149. @unlink($path . '.ico');
  150. @unlink($path . '.txt');
  151. }
  152. public function favicon() {
  153. return Minz_Url::display('/f.php?' . $this->hash());
  154. }
  155. public function _id($value) {
  156. $this->id = $value;
  157. }
  158. public function _url($value, $validate=true) {
  159. $this->hash = null;
  160. if ($validate) {
  161. $value = checkUrl($value);
  162. }
  163. if (empty($value)) {
  164. throw new FreshRSS_BadUrl_Exception($value);
  165. }
  166. $this->url = $value;
  167. }
  168. public function _category($value) {
  169. $value = intval($value);
  170. $this->category = $value >= 0 ? $value : 0;
  171. }
  172. public function _name($value) {
  173. $this->name = $value === null ? '' : $value;
  174. }
  175. public function _website($value, $validate=true) {
  176. if ($validate) {
  177. $value = checkUrl($value);
  178. }
  179. if (empty($value)) {
  180. $value = '';
  181. }
  182. $this->website = $value;
  183. }
  184. public function _description($value) {
  185. $this->description = $value === null ? '' : $value;
  186. }
  187. public function _lastUpdate($value) {
  188. $this->lastUpdate = $value;
  189. }
  190. public function _priority($value) {
  191. $value = intval($value);
  192. $this->priority = $value >= 0 ? $value : 10;
  193. }
  194. public function _pathEntries($value) {
  195. $this->pathEntries = $value;
  196. }
  197. public function _httpAuth($value) {
  198. $this->httpAuth = $value;
  199. }
  200. public function _error($value) {
  201. $this->error = (bool)$value;
  202. }
  203. public function _keepHistory($value) {
  204. $value = intval($value);
  205. $value = min($value, 1000000);
  206. $value = max($value, -2);
  207. $this->keep_history = $value;
  208. }
  209. public function _ttl($value) {
  210. $value = intval($value);
  211. $value = min($value, 100000000);
  212. $value = max($value, -2);
  213. $this->ttl = $value;
  214. }
  215. public function _nbNotRead($value) {
  216. $this->nbNotRead = intval($value);
  217. }
  218. public function _nbEntries($value) {
  219. $this->nbEntries = intval($value);
  220. }
  221. public function load($loadDetails = false, $noCache = false) {
  222. if ($this->url !== null) {
  223. if (CACHE_PATH === false) {
  224. throw new Minz_FileNotExistException(
  225. 'CACHE_PATH',
  226. Minz_Exception::ERROR
  227. );
  228. } else {
  229. $url = htmlspecialchars_decode($this->url, ENT_QUOTES);
  230. if ($this->httpAuth != '') {
  231. $url = preg_replace('#((.+)://)(.+)#', '${1}' . $this->httpAuth . '@${3}', $url);
  232. }
  233. $feed = customSimplePie();
  234. if (substr($url, -11) === '#force_feed') {
  235. $feed->force_feed(true);
  236. $url = substr($url, 0, -11);
  237. }
  238. $feed->set_feed_url($url);
  239. if (!$loadDetails) { //Only activates auto-discovery when adding a new feed
  240. $feed->set_autodiscovery_level(SIMPLEPIE_LOCATOR_NONE);
  241. }
  242. $mtime = $feed->init();
  243. if ((!$mtime) || $feed->error()) {
  244. $errorMessage = $feed->error();
  245. throw new FreshRSS_Feed_Exception(($errorMessage == '' ? 'Unknown error for feed' : $errorMessage) . ' [' . $url . ']');
  246. }
  247. $links = $feed->get_links('self');
  248. $this->selfUrl = isset($links[0]) ? $links[0] : null;
  249. $links = $feed->get_links('hub');
  250. $this->hubUrl = isset($links[0]) ? $links[0] : null;
  251. if ($loadDetails) {
  252. // si on a utilisé l'auto-discover, notre url va avoir changé
  253. $subscribe_url = $feed->subscribe_url(false);
  254. $title = strtr(html_only_entity_decode($feed->get_title()), array('<' => '&lt;', '>' => '&gt;', '"' => '&quot;')); //HTML to HTML-PRE //ENT_COMPAT except &
  255. $this->_name($title == '' ? $url : $title);
  256. $this->_website(html_only_entity_decode($feed->get_link()));
  257. $this->_description(html_only_entity_decode($feed->get_description()));
  258. } else {
  259. //The case of HTTP 301 Moved Permanently
  260. $subscribe_url = $feed->subscribe_url(true);
  261. }
  262. $clean_url = SimplePie_Misc::url_remove_credentials($subscribe_url);
  263. if ($subscribe_url !== null && $subscribe_url !== $url) {
  264. $this->_url($clean_url);
  265. }
  266. if (($mtime === true) || ($mtime > $this->lastUpdate) || $noCache) {
  267. //Minz_Log::debug('FreshRSS no cache ' . $mtime . ' > ' . $this->lastUpdate . ' for ' . $clean_url);
  268. $this->loadEntries($feed); // et on charge les articles du flux
  269. } else {
  270. //Minz_Log::debug('FreshRSS use cache for ' . $clean_url);
  271. $this->entries = array();
  272. }
  273. $feed->__destruct(); //http://simplepie.org/wiki/faq/i_m_getting_memory_leaks
  274. unset($feed);
  275. }
  276. }
  277. }
  278. public function loadEntries($feed) {
  279. $entries = array();
  280. foreach ($feed->get_items() as $item) {
  281. $title = html_only_entity_decode(strip_tags($item->get_title()));
  282. $author = $item->get_author();
  283. $link = $item->get_permalink();
  284. $date = @strtotime($item->get_date());
  285. // gestion des tags (catégorie == tag)
  286. $tags_tmp = $item->get_categories();
  287. $tags = array();
  288. if ($tags_tmp !== null) {
  289. foreach ($tags_tmp as $tag) {
  290. $tags[] = html_only_entity_decode($tag->get_label());
  291. }
  292. }
  293. $content = html_only_entity_decode($item->get_content());
  294. $elinks = array();
  295. foreach ($item->get_enclosures() as $enclosure) {
  296. $elink = $enclosure->get_link();
  297. if (empty($elinks[$elink])) {
  298. $elinks[$elink] = '1';
  299. $mime = strtolower($enclosure->get_type());
  300. if (strpos($mime, 'image/') === 0) {
  301. $content .= '<p class="enclosure"><img src="' . $elink . '" alt="" /></p>';
  302. } elseif (strpos($mime, 'audio/') === 0) {
  303. $content .= '<p class="enclosure"><audio preload="none" src="' . $elink . '" controls="controls"></audio> <a download="" href="' . $elink . '">💾</a></p>';
  304. } elseif (strpos($mime, 'video/') === 0) {
  305. $content .= '<p class="enclosure"><video preload="none" src="' . $elink . '" controls="controls"></video> <a download="" href="' . $elink . '">💾</a></p>';
  306. } else {
  307. unset($elinks[$elink]);
  308. }
  309. }
  310. }
  311. $entry = new FreshRSS_Entry(
  312. $this->id(),
  313. $item->get_id(),
  314. $title === null ? '' : $title,
  315. $author === null ? '' : html_only_entity_decode($author->name),
  316. $content === null ? '' : $content,
  317. $link === null ? '' : $link,
  318. $date ? $date : time()
  319. );
  320. $entry->_tags($tags);
  321. // permet de récupérer le contenu des flux tronqués
  322. $entry->loadCompleteContent($this->pathEntries());
  323. $entries[] = $entry;
  324. unset($item);
  325. }
  326. $this->entries = $entries;
  327. }
  328. function cacheModifiedTime() {
  329. return @filemtime(CACHE_PATH . '/' . md5($this->url) . '.spc');
  330. }
  331. function lock() {
  332. $this->lockPath = TMP_PATH . '/' . $this->hash() . '.freshrss.lock';
  333. if (file_exists($this->lockPath) && ((time() - @filemtime($this->lockPath)) > 3600)) {
  334. @unlink($this->lockPath);
  335. }
  336. if (($handle = @fopen($this->lockPath, 'x')) === false) {
  337. return false;
  338. }
  339. //register_shutdown_function('unlink', $this->lockPath);
  340. @fclose($handle);
  341. return true;
  342. }
  343. function unlock() {
  344. @unlink($this->lockPath);
  345. }
  346. //<PubSubHubbub>
  347. function pubSubHubbubEnabled() {
  348. $url = $this->selfUrl ? $this->selfUrl : $this->url;
  349. $hubFilename = PSHB_PATH . '/feeds/' . base64url_encode($url) . '/!hub.json';
  350. if ($hubFile = @file_get_contents($hubFilename)) {
  351. $hubJson = json_decode($hubFile, true);
  352. if ($hubJson && empty($hubJson['error']) &&
  353. (empty($hubJson['lease_end']) || $hubJson['lease_end'] > time())) {
  354. return true;
  355. }
  356. }
  357. return false;
  358. }
  359. function pubSubHubbubError($error = true) {
  360. $url = $this->selfUrl ? $this->selfUrl : $this->url;
  361. $hubFilename = PSHB_PATH . '/feeds/' . base64url_encode($url) . '/!hub.json';
  362. $hubFile = @file_get_contents($hubFilename);
  363. $hubJson = $hubFile ? json_decode($hubFile, true) : array();
  364. if (!isset($hubJson['error']) || $hubJson['error'] !== (bool)$error) {
  365. $hubJson['error'] = (bool)$error;
  366. file_put_contents($hubFilename, json_encode($hubJson));
  367. file_put_contents(USERS_PATH . '/_/log_pshb.txt', date('c') . "\t"
  368. . 'Set error to ' . ($error ? 1 : 0) . ' for ' . $url . "\n", FILE_APPEND);
  369. }
  370. return false;
  371. }
  372. function pubSubHubbubPrepare() {
  373. $key = '';
  374. if (FreshRSS_Context::$system_conf->base_url && $this->hubUrl && $this->selfUrl && @is_dir(PSHB_PATH)) {
  375. $path = PSHB_PATH . '/feeds/' . base64url_encode($this->selfUrl);
  376. $hubFilename = $path . '/!hub.json';
  377. if ($hubFile = @file_get_contents($hubFilename)) {
  378. $hubJson = json_decode($hubFile, true);
  379. if (!$hubJson || empty($hubJson['key']) || !ctype_xdigit($hubJson['key'])) {
  380. $text = 'Invalid JSON for PubSubHubbub: ' . $this->url;
  381. Minz_Log::warning($text);
  382. file_put_contents(USERS_PATH . '/_/log_pshb.txt', date('c') . "\t" . $text . "\n", FILE_APPEND);
  383. return false;
  384. }
  385. if ((!empty($hubJson['lease_end'])) && ($hubJson['lease_end'] < (time() + (3600 * 23)))) { //TODO: Make a better policy
  386. $text = 'PubSubHubbub lease ends at '
  387. . date('c', empty($hubJson['lease_end']) ? time() : $hubJson['lease_end'])
  388. . ' and needs renewal: ' . $this->url;
  389. Minz_Log::warning($text);
  390. file_put_contents(USERS_PATH . '/_/log_pshb.txt', date('c') . "\t" . $text . "\n", FILE_APPEND);
  391. $key = $hubJson['key']; //To renew our lease
  392. } elseif (((!empty($hubJson['error'])) || empty($hubJson['lease_end'])) &&
  393. (empty($hubJson['lease_start']) || $hubJson['lease_start'] < time() - (3600 * 23))) { //Do not renew too often
  394. $key = $hubJson['key']; //To renew our lease
  395. }
  396. } else {
  397. @mkdir($path, 0777, true);
  398. $key = sha1($path . FreshRSS_Context::$system_conf->salt . uniqid(mt_rand(), true));
  399. $hubJson = array(
  400. 'hub' => $this->hubUrl,
  401. 'key' => $key,
  402. );
  403. file_put_contents($hubFilename, json_encode($hubJson));
  404. @mkdir(PSHB_PATH . '/keys/');
  405. file_put_contents(PSHB_PATH . '/keys/' . $key . '.txt', base64url_encode($this->selfUrl));
  406. $text = 'PubSubHubbub prepared for ' . $this->url;
  407. Minz_Log::debug($text);
  408. file_put_contents(USERS_PATH . '/_/log_pshb.txt', date('c') . "\t" . $text . "\n", FILE_APPEND);
  409. }
  410. $currentUser = Minz_Session::param('currentUser');
  411. if (ctype_alnum($currentUser) && !file_exists($path . '/' . $currentUser . '.txt')) {
  412. touch($path . '/' . $currentUser . '.txt');
  413. }
  414. }
  415. return $key;
  416. }
  417. //Parameter true to subscribe, false to unsubscribe.
  418. function pubSubHubbubSubscribe($state) {
  419. if (FreshRSS_Context::$system_conf->base_url && $this->hubUrl && $this->selfUrl) {
  420. $hubFilename = PSHB_PATH . '/feeds/' . base64url_encode($this->selfUrl) . '/!hub.json';
  421. $hubFile = @file_get_contents($hubFilename);
  422. if ($hubFile === false) {
  423. Minz_Log::warning('JSON not found for PubSubHubbub: ' . $this->url);
  424. return false;
  425. }
  426. $hubJson = json_decode($hubFile, true);
  427. if (!$hubJson || empty($hubJson['key']) || !ctype_xdigit($hubJson['key'])) {
  428. Minz_Log::warning('Invalid JSON for PubSubHubbub: ' . $this->url);
  429. return false;
  430. }
  431. $callbackUrl = checkUrl(Minz_Request::getBaseUrl() . '/api/pshb.php?k=' . $hubJson['key']);
  432. if ($callbackUrl == '') {
  433. Minz_Log::warning('Invalid callback for PubSubHubbub: ' . $this->url);
  434. return false;
  435. }
  436. if (!$state) { //unsubscribe
  437. $hubJson['lease_end'] = time() - 60;
  438. file_put_contents($hubFilename, json_encode($hubJson));
  439. }
  440. $ch = curl_init();
  441. curl_setopt_array($ch, array(
  442. CURLOPT_URL => $this->hubUrl,
  443. CURLOPT_FOLLOWLOCATION => true,
  444. CURLOPT_RETURNTRANSFER => true,
  445. CURLOPT_USERAGENT => 'FreshRSS/' . FRESHRSS_VERSION . ' (' . PHP_OS . '; ' . FRESHRSS_WEBSITE . ')',
  446. CURLOPT_POSTFIELDS => 'hub.verify=sync'
  447. . '&hub.mode=' . ($state ? 'subscribe' : 'unsubscribe')
  448. . '&hub.topic=' . urlencode($this->selfUrl)
  449. . '&hub.callback=' . urlencode($callbackUrl)
  450. )
  451. );
  452. $response = curl_exec($ch);
  453. $info = curl_getinfo($ch);
  454. file_put_contents(USERS_PATH . '/_/log_pshb.txt', date('c') . "\t" .
  455. 'PubSubHubbub ' . ($state ? 'subscribe' : 'unsubscribe') . ' to ' . $this->selfUrl .
  456. ' with callback ' . $callbackUrl . ': ' . $info['http_code'] . ' ' . $response . "\n", FILE_APPEND);
  457. if (substr($info['http_code'], 0, 1) == '2') {
  458. return true;
  459. } else {
  460. $hubJson['lease_start'] = time(); //Prevent trying again too soon
  461. $hubJson['error'] = true;
  462. file_put_contents($hubFilename, json_encode($hubJson));
  463. return false;
  464. }
  465. }
  466. return false;
  467. }
  468. //</PubSubHubbub>
  469. }