Feed.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  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, $noCache = 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 == '' ? 'Unknown error for feed' : $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) || $noCache) {
  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 .= '<p class="enclosure"><img src="' . $elink . '" alt="" /></p>';
  289. } elseif (strpos($mime, 'audio/') === 0) {
  290. $content .= '<p class="enclosure"><audio preload="none" src="' . $elink . '" controls="controls"></audio> <a download="" href="' . $elink . '">💾</a></p>';
  291. } elseif (strpos($mime, 'video/') === 0) {
  292. $content .= '<p class="enclosure"><video preload="none" src="' . $elink . '" controls="controls"></video> <a download="" href="' . $elink . '">💾</a></p>';
  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 cacheModifiedTime() {
  316. return @filemtime(CACHE_PATH . '/' . md5($this->url) . '.spc');
  317. }
  318. function lock() {
  319. $this->lockPath = TMP_PATH . '/' . $this->hash() . '.freshrss.lock';
  320. if (file_exists($this->lockPath) && ((time() - @filemtime($this->lockPath)) > 3600)) {
  321. @unlink($this->lockPath);
  322. }
  323. if (($handle = @fopen($this->lockPath, 'x')) === false) {
  324. return false;
  325. }
  326. //register_shutdown_function('unlink', $this->lockPath);
  327. @fclose($handle);
  328. return true;
  329. }
  330. function unlock() {
  331. @unlink($this->lockPath);
  332. }
  333. //<PubSubHubbub>
  334. function pubSubHubbubEnabled() {
  335. $url = $this->selfUrl ? $this->selfUrl : $this->url;
  336. $hubFilename = PSHB_PATH . '/feeds/' . base64url_encode($url) . '/!hub.json';
  337. if ($hubFile = @file_get_contents($hubFilename)) {
  338. $hubJson = json_decode($hubFile, true);
  339. if ($hubJson && empty($hubJson['error']) &&
  340. (empty($hubJson['lease_end']) || $hubJson['lease_end'] > time())) {
  341. return true;
  342. }
  343. }
  344. return false;
  345. }
  346. function pubSubHubbubError($error = true) {
  347. $url = $this->selfUrl ? $this->selfUrl : $this->url;
  348. $hubFilename = PSHB_PATH . '/feeds/' . base64url_encode($url) . '/!hub.json';
  349. $hubFile = @file_get_contents($hubFilename);
  350. $hubJson = $hubFile ? json_decode($hubFile, true) : array();
  351. if (!isset($hubJson['error']) || $hubJson['error'] !== (bool)$error) {
  352. $hubJson['error'] = (bool)$error;
  353. file_put_contents($hubFilename, json_encode($hubJson));
  354. file_put_contents(USERS_PATH . '/_/log_pshb.txt', date('c') . "\t"
  355. . 'Set error to ' . ($error ? 1 : 0) . ' for ' . $url . "\n", FILE_APPEND);
  356. }
  357. return false;
  358. }
  359. function pubSubHubbubPrepare() {
  360. $key = '';
  361. if (FreshRSS_Context::$system_conf->base_url && $this->hubUrl && $this->selfUrl && @is_dir(PSHB_PATH)) {
  362. $path = PSHB_PATH . '/feeds/' . base64url_encode($this->selfUrl);
  363. $hubFilename = $path . '/!hub.json';
  364. if ($hubFile = @file_get_contents($hubFilename)) {
  365. $hubJson = json_decode($hubFile, true);
  366. if (!$hubJson || empty($hubJson['key']) || !ctype_xdigit($hubJson['key'])) {
  367. $text = 'Invalid JSON for PubSubHubbub: ' . $this->url;
  368. Minz_Log::warning($text);
  369. file_put_contents(USERS_PATH . '/_/log_pshb.txt', date('c') . "\t" . $text . "\n", FILE_APPEND);
  370. return false;
  371. }
  372. if ((!empty($hubJson['lease_end'])) && ($hubJson['lease_end'] < (time() + (3600 * 23)))) { //TODO: Make a better policy
  373. $text = 'PubSubHubbub lease ends at '
  374. . date('c', empty($hubJson['lease_end']) ? time() : $hubJson['lease_end'])
  375. . ' and needs renewal: ' . $this->url;
  376. Minz_Log::warning($text);
  377. file_put_contents(USERS_PATH . '/_/log_pshb.txt', date('c') . "\t" . $text . "\n", FILE_APPEND);
  378. $key = $hubJson['key']; //To renew our lease
  379. } elseif (((!empty($hubJson['error'])) || empty($hubJson['lease_end'])) &&
  380. (empty($hubJson['lease_start']) || $hubJson['lease_start'] < time() - (3600 * 23))) { //Do not renew too often
  381. $key = $hubJson['key']; //To renew our lease
  382. }
  383. } else {
  384. @mkdir($path, 0777, true);
  385. $key = sha1($path . FreshRSS_Context::$system_conf->salt . uniqid(mt_rand(), true));
  386. $hubJson = array(
  387. 'hub' => $this->hubUrl,
  388. 'key' => $key,
  389. );
  390. file_put_contents($hubFilename, json_encode($hubJson));
  391. @mkdir(PSHB_PATH . '/keys/');
  392. file_put_contents(PSHB_PATH . '/keys/' . $key . '.txt', base64url_encode($this->selfUrl));
  393. $text = 'PubSubHubbub prepared for ' . $this->url;
  394. Minz_Log::debug($text);
  395. file_put_contents(USERS_PATH . '/_/log_pshb.txt', date('c') . "\t" . $text . "\n", FILE_APPEND);
  396. }
  397. $currentUser = Minz_Session::param('currentUser');
  398. if (ctype_alnum($currentUser) && !file_exists($path . '/' . $currentUser . '.txt')) {
  399. touch($path . '/' . $currentUser . '.txt');
  400. }
  401. }
  402. return $key;
  403. }
  404. //Parameter true to subscribe, false to unsubscribe.
  405. function pubSubHubbubSubscribe($state) {
  406. if (FreshRSS_Context::$system_conf->base_url && $this->hubUrl && $this->selfUrl) {
  407. $hubFilename = PSHB_PATH . '/feeds/' . base64url_encode($this->selfUrl) . '/!hub.json';
  408. $hubFile = @file_get_contents($hubFilename);
  409. if ($hubFile === false) {
  410. Minz_Log::warning('JSON not found for PubSubHubbub: ' . $this->url);
  411. return false;
  412. }
  413. $hubJson = json_decode($hubFile, true);
  414. if (!$hubJson || empty($hubJson['key']) || !ctype_xdigit($hubJson['key'])) {
  415. Minz_Log::warning('Invalid JSON for PubSubHubbub: ' . $this->url);
  416. return false;
  417. }
  418. $callbackUrl = checkUrl(Minz_Request::getBaseUrl() . '/api/pshb.php?k=' . $hubJson['key']);
  419. if ($callbackUrl == '') {
  420. Minz_Log::warning('Invalid callback for PubSubHubbub: ' . $this->url);
  421. return false;
  422. }
  423. if (!$state) { //unsubscribe
  424. $hubJson['lease_end'] = time() - 60;
  425. file_put_contents($hubFilename, json_encode($hubJson));
  426. }
  427. $ch = curl_init();
  428. curl_setopt_array($ch, array(
  429. CURLOPT_URL => $this->hubUrl,
  430. CURLOPT_FOLLOWLOCATION => true,
  431. CURLOPT_RETURNTRANSFER => true,
  432. CURLOPT_USERAGENT => 'FreshRSS/' . FRESHRSS_VERSION . ' (' . PHP_OS . '; ' . FRESHRSS_WEBSITE . ')',
  433. CURLOPT_POSTFIELDS => 'hub.verify=sync'
  434. . '&hub.mode=' . ($state ? 'subscribe' : 'unsubscribe')
  435. . '&hub.topic=' . urlencode($this->selfUrl)
  436. . '&hub.callback=' . urlencode($callbackUrl)
  437. )
  438. );
  439. $response = curl_exec($ch);
  440. $info = curl_getinfo($ch);
  441. file_put_contents(USERS_PATH . '/_/log_pshb.txt', date('c') . "\t" .
  442. 'PubSubHubbub ' . ($state ? 'subscribe' : 'unsubscribe') . ' to ' . $this->selfUrl .
  443. ' with callback ' . $callbackUrl . ': ' . $info['http_code'] . ' ' . $response . "\n", FILE_APPEND);
  444. if (substr($info['http_code'], 0, 1) == '2') {
  445. return true;
  446. } else {
  447. $hubJson['lease_start'] = time(); //Prevent trying again too soon
  448. $hubJson['error'] = true;
  449. file_put_contents($hubFilename, json_encode($hubJson));
  450. return false;
  451. }
  452. }
  453. return false;
  454. }
  455. //</PubSubHubbub>
  456. }