Feed.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  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 category() {
  49. return $this->category;
  50. }
  51. public function entries() {
  52. return $this->entries === null ? array() : $this->entries;
  53. }
  54. public function name() {
  55. return $this->name;
  56. }
  57. public function website() {
  58. return $this->website;
  59. }
  60. public function description() {
  61. return $this->description;
  62. }
  63. public function lastUpdate() {
  64. return $this->lastUpdate;
  65. }
  66. public function priority() {
  67. return $this->priority;
  68. }
  69. public function pathEntries() {
  70. return $this->pathEntries;
  71. }
  72. public function httpAuth($raw = true) {
  73. if ($raw) {
  74. return $this->httpAuth;
  75. } else {
  76. $pos_colon = strpos($this->httpAuth, ':');
  77. $user = substr($this->httpAuth, 0, $pos_colon);
  78. $pass = substr($this->httpAuth, $pos_colon + 1);
  79. return array(
  80. 'username' => $user,
  81. 'password' => $pass
  82. );
  83. }
  84. }
  85. public function inError() {
  86. return $this->error;
  87. }
  88. public function keepHistory() {
  89. return $this->keep_history;
  90. }
  91. public function ttl() {
  92. return $this->ttl;
  93. }
  94. public function nbEntries() {
  95. if ($this->nbEntries < 0) {
  96. $feedDAO = FreshRSS_Factory::createFeedDao();
  97. $this->nbEntries = $feedDAO->countEntries($this->id());
  98. }
  99. return $this->nbEntries;
  100. }
  101. public function nbNotRead() {
  102. if ($this->nbNotRead < 0) {
  103. $feedDAO = FreshRSS_Factory::createFeedDao();
  104. $this->nbNotRead = $feedDAO->countNotRead($this->id());
  105. }
  106. return $this->nbNotRead;
  107. }
  108. public function faviconPrepare() {
  109. $file = DATA_PATH . '/favicons/' . $this->hash() . '.txt';
  110. if (!file_exists($file)) {
  111. $t = $this->website;
  112. if ($t == '') {
  113. $t = $this->url;
  114. }
  115. file_put_contents($file, $t);
  116. }
  117. }
  118. public static function faviconDelete($hash) {
  119. $path = DATA_PATH . '/favicons/' . $hash;
  120. @unlink($path . '.ico');
  121. @unlink($path . '.txt');
  122. }
  123. public function favicon() {
  124. return Minz_Url::display('/f.php?' . $this->hash());
  125. }
  126. public function _id($value) {
  127. $this->id = $value;
  128. }
  129. public function _url($value, $validate=true) {
  130. $this->hash = null;
  131. if ($validate) {
  132. $value = checkUrl($value);
  133. }
  134. if (empty($value)) {
  135. throw new FreshRSS_BadUrl_Exception($value);
  136. }
  137. $this->url = $value;
  138. }
  139. public function _category($value) {
  140. $value = intval($value);
  141. $this->category = $value >= 0 ? $value : 0;
  142. }
  143. public function _name($value) {
  144. $this->name = $value === null ? '' : $value;
  145. }
  146. public function _website($value, $validate=true) {
  147. if ($validate) {
  148. $value = checkUrl($value);
  149. }
  150. if (empty($value)) {
  151. $value = '';
  152. }
  153. $this->website = $value;
  154. }
  155. public function _description($value) {
  156. $this->description = $value === null ? '' : $value;
  157. }
  158. public function _lastUpdate($value) {
  159. $this->lastUpdate = $value;
  160. }
  161. public function _priority($value) {
  162. $value = intval($value);
  163. $this->priority = $value >= 0 ? $value : 10;
  164. }
  165. public function _pathEntries($value) {
  166. $this->pathEntries = $value;
  167. }
  168. public function _httpAuth($value) {
  169. $this->httpAuth = $value;
  170. }
  171. public function _error($value) {
  172. $this->error = (bool)$value;
  173. }
  174. public function _keepHistory($value) {
  175. $value = intval($value);
  176. $value = min($value, 1000000);
  177. $value = max($value, -2);
  178. $this->keep_history = $value;
  179. }
  180. public function _ttl($value) {
  181. $value = intval($value);
  182. $value = min($value, 100000000);
  183. $value = max($value, -2);
  184. $this->ttl = $value;
  185. }
  186. public function _nbNotRead($value) {
  187. $this->nbNotRead = intval($value);
  188. }
  189. public function _nbEntries($value) {
  190. $this->nbEntries = intval($value);
  191. }
  192. public function load($loadDetails = false) {
  193. if ($this->url !== null) {
  194. if (CACHE_PATH === false) {
  195. throw new Minz_FileNotExistException(
  196. 'CACHE_PATH',
  197. Minz_Exception::ERROR
  198. );
  199. } else {
  200. $url = htmlspecialchars_decode($this->url, ENT_QUOTES);
  201. if ($this->httpAuth != '') {
  202. $url = preg_replace('#((.+)://)(.+)#', '${1}' . $this->httpAuth . '@${3}', $url);
  203. }
  204. $feed = customSimplePie();
  205. if (substr($url, -11) === '#force_feed') {
  206. $feed->force_feed(true);
  207. $url = substr($url, 0, -11);
  208. }
  209. $feed->set_feed_url($url);
  210. if (!$loadDetails) { //Only activates auto-discovery when adding a new feed
  211. $feed->set_autodiscovery_level(SIMPLEPIE_LOCATOR_NONE);
  212. }
  213. $mtime = $feed->init();
  214. if ((!$mtime) || $feed->error()) {
  215. $errorMessage = $feed->error();
  216. throw new FreshRSS_Feed_Exception(($errorMessage == '' ? 'Feed error' : $errorMessage) . ' [' . $url . ']');
  217. }
  218. $links = $feed->get_links('self');
  219. $this->selfUrl = isset($links[0]) ? $links[0] : null;
  220. $links = $feed->get_links('hub');
  221. $this->hubUrl = isset($links[0]) ? $links[0] : null;
  222. if ($loadDetails) {
  223. // si on a utilisé l'auto-discover, notre url va avoir changé
  224. $subscribe_url = $feed->subscribe_url(false);
  225. $title = strtr(html_only_entity_decode($feed->get_title()), array('<' => '&lt;', '>' => '&gt;', '"' => '&quot;')); //HTML to HTML-PRE //ENT_COMPAT except &
  226. $this->_name($title == '' ? $url : $title);
  227. $this->_website(html_only_entity_decode($feed->get_link()));
  228. $this->_description(html_only_entity_decode($feed->get_description()));
  229. } else {
  230. //The case of HTTP 301 Moved Permanently
  231. $subscribe_url = $feed->subscribe_url(true);
  232. }
  233. $clean_url = SimplePie_Misc::url_remove_credentials($subscribe_url);
  234. if ($subscribe_url !== null && $subscribe_url !== $url) {
  235. $this->_url($clean_url);
  236. }
  237. if (($mtime === true) || ($mtime > $this->lastUpdate)) {
  238. //Minz_Log::debug('FreshRSS no cache ' . $mtime . ' > ' . $this->lastUpdate . ' for ' . $clean_url);
  239. $this->loadEntries($feed); // et on charge les articles du flux
  240. } else {
  241. //Minz_Log::debug('FreshRSS use cache for ' . $clean_url);
  242. $this->entries = array();
  243. }
  244. $feed->__destruct(); //http://simplepie.org/wiki/faq/i_m_getting_memory_leaks
  245. unset($feed);
  246. }
  247. }
  248. }
  249. public function loadEntries($feed) {
  250. $entries = array();
  251. foreach ($feed->get_items() as $item) {
  252. $title = html_only_entity_decode(strip_tags($item->get_title()));
  253. $author = $item->get_author();
  254. $link = $item->get_permalink();
  255. $date = @strtotime($item->get_date());
  256. // gestion des tags (catégorie == tag)
  257. $tags_tmp = $item->get_categories();
  258. $tags = array();
  259. if ($tags_tmp !== null) {
  260. foreach ($tags_tmp as $tag) {
  261. $tags[] = html_only_entity_decode($tag->get_label());
  262. }
  263. }
  264. $content = html_only_entity_decode($item->get_content());
  265. $elinks = array();
  266. foreach ($item->get_enclosures() as $enclosure) {
  267. $elink = $enclosure->get_link();
  268. if (empty($elinks[$elink])) {
  269. $elinks[$elink] = '1';
  270. $mime = strtolower($enclosure->get_type());
  271. if (strpos($mime, 'image/') === 0) {
  272. $content .= '<br /><img lazyload="" postpone="" src="' . $elink . '" alt="" />';
  273. } elseif (strpos($mime, 'audio/') === 0) {
  274. $content .= '<br /><audio lazyload="" postpone="" preload="none" src="' . $elink . '" controls="controls" />';
  275. } elseif (strpos($mime, 'video/') === 0) {
  276. $content .= '<br /><video lazyload="" postpone="" preload="none" src="' . $elink . '" controls="controls" />';
  277. } else {
  278. unset($elinks[$elink]);
  279. }
  280. }
  281. }
  282. $entry = new FreshRSS_Entry(
  283. $this->id(),
  284. $item->get_id(),
  285. $title === null ? '' : $title,
  286. $author === null ? '' : html_only_entity_decode($author->name),
  287. $content === null ? '' : $content,
  288. $link === null ? '' : $link,
  289. $date ? $date : time()
  290. );
  291. $entry->_tags($tags);
  292. // permet de récupérer le contenu des flux tronqués
  293. $entry->loadCompleteContent($this->pathEntries());
  294. $entries[] = $entry;
  295. unset($item);
  296. }
  297. $this->entries = $entries;
  298. }
  299. function lock() {
  300. $this->lockPath = TMP_PATH . '/' . $this->hash() . '.freshrss.lock';
  301. if (file_exists($this->lockPath) && ((time() - @filemtime($this->lockPath)) > 3600)) {
  302. @unlink($this->lockPath);
  303. }
  304. if (($handle = @fopen($this->lockPath, 'x')) === false) {
  305. return false;
  306. }
  307. //register_shutdown_function('unlink', $this->lockPath);
  308. @fclose($handle);
  309. return true;
  310. }
  311. function unlock() {
  312. @unlink($this->lockPath);
  313. }
  314. //<PubSubHubbub>
  315. function pubSubHubbubPrepare() {
  316. $secret = '';
  317. if (FreshRSS_Context::$system_conf->base_url && $this->hubUrl && $this->selfUrl) {
  318. $path = PSHB_PATH . '/feeds/' . base64url_encode($this->selfUrl);
  319. if (!file_exists($path . '/hub.txt')) {
  320. @mkdir($path, 0777, true);
  321. file_put_contents($path . '/hub.txt', $this->hubUrl);
  322. $secret = sha1(FreshRSS_Context::$system_conf->salt . uniqid(mt_rand(), true));
  323. file_put_contents($path . '/secret.txt', $secret);
  324. @mkdir(PSHB_PATH . '/secrets/');
  325. file_put_contents(PSHB_PATH . '/secrets/' . $secret . '.txt', base64url_encode($this->selfUrl));
  326. Minz_Log::notice('PubSubHubbub prepared for ' . $this->url);
  327. file_put_contents(USERS_PATH . '/_/log_pshb.txt', date('c') . "\t" .
  328. 'PubSubHubbub prepared for ' . $this->url . "\n", FILE_APPEND);
  329. }
  330. $path .= '/' . base64url_encode($this->url);
  331. $currentUser = Minz_Session::param('currentUser');
  332. if (ctype_alnum($currentUser) && !file_exists($path . '/' . $currentUser . '.txt')) {
  333. @mkdir($path, 0777, true);
  334. touch($path . '/' . $currentUser . '.txt');
  335. }
  336. }
  337. return $secret;
  338. }
  339. //Parameter true to subscribe, false to unsubscribe.
  340. function pubSubHubbubSubscribe($state, $secret = '') {
  341. if (FreshRSS_Context::$system_conf->base_url && $this->hubUrl && $this->selfUrl) {
  342. $callbackUrl = checkUrl(FreshRSS_Context::$system_conf->base_url . 'api/pshb.php?s=' . $secret);
  343. if ($callbackUrl == '') {
  344. return false;
  345. }
  346. $ch = curl_init();
  347. curl_setopt_array($ch, array(
  348. CURLOPT_URL => $this->hubUrl,
  349. CURLOPT_FOLLOWLOCATION => true,
  350. CURLOPT_RETURNTRANSFER => true,
  351. CURLOPT_USERAGENT => _t('gen.freshrss') . '/' . FRESHRSS_VERSION . ' (' . PHP_OS . '; ' . FRESHRSS_WEBSITE . ')',
  352. CURLOPT_POSTFIELDS => 'hub.verify=sync'
  353. . '&hub.mode=' . ($state ? 'subscribe' : 'unsubscribe')
  354. . '&hub.topic=' . urlencode($this->selfUrl)
  355. . '&hub.callback=' . urlencode($callbackUrl)
  356. )
  357. );
  358. $response = curl_exec($ch);
  359. $info = curl_getinfo($ch);
  360. file_put_contents(USERS_PATH . '/_/log_pshb.txt', date('c') . "\t" .
  361. 'PubSubHubbub ' . ($state ? 'subscribe' : 'unsubscribe') . ' to ' . $this->selfUrl .
  362. ' with callback ' . $callbackUrl . ': ' . $info['http_code'] . ' ' . $response . "\n", FILE_APPEND);
  363. return substr($info['http_code'], 0, 1) == '2';
  364. }
  365. return false;
  366. }
  367. //</PubSubHubbub>
  368. }