Feed.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612
  1. <?php
  2. class Feed extends 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 = false;
  18. public function __construct ($url, $validate=true) {
  19. if ($validate) {
  20. $this->_url ($url);
  21. } else {
  22. $this->url = $url;
  23. }
  24. }
  25. public function id () {
  26. return $this->id;
  27. }
  28. public function url () {
  29. return $this->url;
  30. }
  31. public function category () {
  32. return $this->category;
  33. }
  34. public function entries () {
  35. if (!is_null ($this->entries)) {
  36. return $this->entries;
  37. } else {
  38. return array ();
  39. }
  40. }
  41. public function name () {
  42. return $this->name;
  43. }
  44. public function website () {
  45. return $this->website;
  46. }
  47. public function description () {
  48. return $this->description;
  49. }
  50. public function lastUpdate () {
  51. return $this->lastUpdate;
  52. }
  53. public function priority () {
  54. return $this->priority;
  55. }
  56. public function pathEntries () {
  57. return $this->pathEntries;
  58. }
  59. public function httpAuth ($raw = true) {
  60. if ($raw) {
  61. return $this->httpAuth;
  62. } else {
  63. $pos_colon = strpos ($this->httpAuth, ':');
  64. $user = substr ($this->httpAuth, 0, $pos_colon);
  65. $pass = substr ($this->httpAuth, $pos_colon + 1);
  66. return array (
  67. 'username' => $user,
  68. 'password' => $pass
  69. );
  70. }
  71. }
  72. public function inError () {
  73. return $this->error;
  74. }
  75. public function keepHistory () {
  76. return $this->keep_history;
  77. }
  78. public function nbEntries () {
  79. if ($this->nbEntries < 0) {
  80. $feedDAO = new FeedDAO ();
  81. $this->nbEntries = $feedDAO->countEntries ($this->id ());
  82. }
  83. return $this->nbEntries;
  84. }
  85. public function nbNotRead () {
  86. if ($this->nbNotRead < 0) {
  87. $feedDAO = new FeedDAO ();
  88. $this->nbNotRead = $feedDAO->countNotRead ($this->id ());
  89. }
  90. return $this->nbNotRead;
  91. }
  92. public function faviconPrepare() {
  93. $file = DATA_PATH . '/favicons/' . $this->id () . '.txt';
  94. if (!file_exists ($file)) {
  95. $t = $this->website;
  96. if (empty($t)) {
  97. $t = $this->url;
  98. }
  99. file_put_contents($file, $t);
  100. }
  101. }
  102. public static function faviconDelete($id) {
  103. $path = DATA_PATH . '/favicons/' . $id;
  104. @unlink($path . '.ico');
  105. @unlink($path . '.txt');
  106. }
  107. public function favicon () {
  108. return Url::display ('/f.php?' . $this->id ());
  109. }
  110. public function _id ($value) {
  111. $this->id = $value;
  112. }
  113. public function _url ($value, $validate=true) {
  114. if ($validate) {
  115. $value = checkUrl($value);
  116. }
  117. if (empty ($value)) {
  118. throw new BadUrlException ($value);
  119. }
  120. $this->url = $value;
  121. }
  122. public function _category ($value) {
  123. $this->category = $value;
  124. }
  125. public function _name ($value) {
  126. if (is_null ($value)) {
  127. $value = '';
  128. }
  129. $this->name = $value;
  130. }
  131. public function _website ($value, $validate=true) {
  132. if ($validate) {
  133. $value = checkUrl($value);
  134. }
  135. if (empty ($value)) {
  136. $value = '';
  137. }
  138. $this->website = $value;
  139. }
  140. public function _description ($value) {
  141. if (is_null ($value)) {
  142. $value = '';
  143. }
  144. $this->description = $value;
  145. }
  146. public function _lastUpdate ($value) {
  147. $this->lastUpdate = $value;
  148. }
  149. public function _priority ($value) {
  150. $this->priority = ctype_digit ($value) ? intval ($value) : 10;
  151. }
  152. public function _pathEntries ($value) {
  153. $this->pathEntries = $value;
  154. }
  155. public function _httpAuth ($value) {
  156. $this->httpAuth = $value;
  157. }
  158. public function _error ($value) {
  159. if ($value) {
  160. $value = true;
  161. } else {
  162. $value = false;
  163. }
  164. $this->error = $value;
  165. }
  166. public function _keepHistory ($value) {
  167. if ($value) {
  168. $value = true;
  169. } else {
  170. $value = false;
  171. }
  172. $this->keep_history = $value;
  173. }
  174. public function _nbNotRead ($value) {
  175. $this->nbNotRead = ctype_digit ($value) ? intval ($value) : -1;
  176. }
  177. public function _nbEntries ($value) {
  178. $this->nbEntries = ctype_digit ($value) ? intval ($value) : -1;
  179. }
  180. public function load () {
  181. if (!is_null ($this->url)) {
  182. if (CACHE_PATH === false) {
  183. throw new FileNotExistException (
  184. 'CACHE_PATH',
  185. MinzException::ERROR
  186. );
  187. } else {
  188. $feed = new SimplePie ();
  189. $feed->set_useragent(Translate::t ('freshrss') . '/' . FRESHRSS_VERSION . ' (' . PHP_OS . '; ' . FRESHRSS_WEBSITE . ') ' . SIMPLEPIE_NAME . '/' . SIMPLEPIE_VERSION);
  190. $url = htmlspecialchars_decode ($this->url, ENT_QUOTES);
  191. if ($this->httpAuth != '') {
  192. $url = preg_replace ('#((.+)://)(.+)#', '${1}' . $this->httpAuth . '@${3}', $url);
  193. }
  194. $feed->set_feed_url ($url);
  195. $feed->set_cache_location (CACHE_PATH);
  196. $feed->set_cache_duration(1500);
  197. $feed->strip_htmltags (array (
  198. 'base', 'blink', 'body', 'doctype', 'embed',
  199. 'font', 'form', 'frame', 'frameset', 'html',
  200. 'input', 'marquee', 'meta', 'noscript',
  201. 'object', 'param', 'plaintext', 'script', 'style',
  202. ));
  203. $feed->strip_attributes(array_merge($feed->strip_attributes, array(
  204. 'autoplay', 'onload', 'onunload', 'onclick', 'ondblclick', 'onmousedown', 'onmouseup',
  205. 'onmouseover', 'onmousemove', 'onmouseout', 'onfocus', 'onblur',
  206. 'onkeypress', 'onkeydown', 'onkeyup', 'onselect', 'onchange', 'seamless')));
  207. $feed->add_attributes(array(
  208. 'img' => array('lazyload' => ''), //http://www.w3.org/TR/resource-priorities/
  209. 'audio' => array('preload' => 'none'),
  210. 'iframe' => array('postpone' => '', 'sandbox' => 'allow-scripts allow-same-origin'),
  211. 'video' => array('postpone' => '', 'preload' => 'none'),
  212. ));
  213. $feed->set_url_replacements(array(
  214. 'a' => 'href',
  215. 'area' => 'href',
  216. 'audio' => 'src',
  217. 'blockquote' => 'cite',
  218. 'del' => 'cite',
  219. 'form' => 'action',
  220. 'iframe' => 'src',
  221. 'img' => array(
  222. 'longdesc',
  223. 'src'
  224. ),
  225. 'input' => 'src',
  226. 'ins' => 'cite',
  227. 'q' => 'cite',
  228. 'source' => 'src',
  229. 'track' => 'src',
  230. 'video' => array(
  231. 'poster',
  232. 'src',
  233. ),
  234. ));
  235. $feed->init ();
  236. if ($feed->error ()) {
  237. throw new FeedException ($feed->error . ' [' . $url . ']');
  238. }
  239. // si on a utilisé l'auto-discover, notre url va avoir changé
  240. $subscribe_url = $feed->subscribe_url ();
  241. if (!is_null ($subscribe_url) && $subscribe_url != $this->url) {
  242. if ($this->httpAuth != '') {
  243. // on enlève les id si authentification HTTP
  244. $subscribe_url = preg_replace ('#((.+)://)((.+)@)(.+)#', '${1}${5}', $subscribe_url);
  245. }
  246. $this->_url ($subscribe_url);
  247. }
  248. $title = $feed->get_title ();
  249. $this->_name (!is_null ($title) ? $title : $this->url);
  250. $this->_website ($feed->get_link ());
  251. $this->_description ($feed->get_description ());
  252. // et on charge les articles du flux
  253. $this->loadEntries ($feed);
  254. }
  255. }
  256. }
  257. private function loadEntries ($feed) {
  258. $entries = array ();
  259. foreach ($feed->get_items () as $item) {
  260. $title = html_only_entity_decode (strip_tags ($item->get_title ()));
  261. $author = $item->get_author ();
  262. $link = $item->get_permalink ();
  263. $date = @strtotime ($item->get_date ());
  264. // gestion des tags (catégorie == tag)
  265. $tags_tmp = $item->get_categories ();
  266. $tags = array ();
  267. if (!is_null ($tags_tmp)) {
  268. foreach ($tags_tmp as $tag) {
  269. $tags[] = html_only_entity_decode ($tag->get_label ());
  270. }
  271. }
  272. $content = html_only_entity_decode ($item->get_content ());
  273. $elinks = array();
  274. foreach ($item->get_enclosures() as $enclosure) {
  275. $elink = $enclosure->get_link();
  276. if (array_key_exists($elink, $elinks)) continue;
  277. $elinks[$elink] = '1';
  278. $mime = strtolower($enclosure->get_type());
  279. if (strpos($mime, 'image/') === 0) {
  280. $content .= '<br /><img src="' . $elink . '" alt="" />';
  281. }
  282. }
  283. $entry = new Entry (
  284. $this->id (),
  285. $item->get_id (),
  286. !is_null ($title) ? $title : '',
  287. !is_null ($author) ? html_only_entity_decode ($author->name) : '',
  288. !is_null ($content) ? $content : '',
  289. !is_null ($link) ? $link : '',
  290. $date ? $date : time ()
  291. );
  292. $entry->_tags ($tags);
  293. // permet de récupérer le contenu des flux tronqués
  294. $entry->loadCompleteContent($this->pathEntries());
  295. $entries[] = $entry;
  296. }
  297. $this->entries = $entries;
  298. }
  299. }
  300. class FeedDAO extends Model_pdo {
  301. public function addFeed ($valuesTmp) {
  302. $sql = 'INSERT INTO `' . $this->prefix . 'feed` (url, category, name, website, description, lastUpdate, priority, httpAuth, error, keep_history) VALUES(?, ?, ?, ?, ?, ?, 10, ?, 0, 0)';
  303. $stm = $this->bd->prepare ($sql);
  304. $values = array (
  305. substr($valuesTmp['url'], 0, 511),
  306. $valuesTmp['category'],
  307. substr($valuesTmp['name'], 0, 255),
  308. substr($valuesTmp['website'], 0, 255),
  309. substr($valuesTmp['description'], 0, 1023),
  310. $valuesTmp['lastUpdate'],
  311. base64_encode ($valuesTmp['httpAuth']),
  312. );
  313. if ($stm && $stm->execute ($values)) {
  314. return $this->bd->lastInsertId();
  315. } else {
  316. $info = $stm->errorInfo();
  317. Minz_Log::record ('SQL error : ' . $info[2], Minz_Log::ERROR);
  318. return false;
  319. }
  320. }
  321. public function updateFeed ($id, $valuesTmp) {
  322. $set = '';
  323. foreach ($valuesTmp as $key => $v) {
  324. $set .= $key . '=?, ';
  325. if ($key == 'httpAuth') {
  326. $valuesTmp[$key] = base64_encode ($v);
  327. }
  328. }
  329. $set = substr ($set, 0, -2);
  330. $sql = 'UPDATE `' . $this->prefix . 'feed` SET ' . $set . ' WHERE id=?';
  331. $stm = $this->bd->prepare ($sql);
  332. foreach ($valuesTmp as $v) {
  333. $values[] = $v;
  334. }
  335. $values[] = $id;
  336. if ($stm && $stm->execute ($values)) {
  337. return $stm->rowCount();
  338. } else {
  339. $info = $stm->errorInfo();
  340. Minz_Log::record ('SQL error : ' . $info[2], Minz_Log::ERROR);
  341. return false;
  342. }
  343. }
  344. public function updateLastUpdate ($id, $inError = 0) {
  345. $sql = 'UPDATE `' . $this->prefix . 'feed` f ' //2 sub-requests with FOREIGN KEY(e.id_feed), INDEX(e.is_read) faster than 1 request with GROUP BY or CASE
  346. . 'SET f.cache_nbEntries=(SELECT COUNT(e1.id) FROM `' . $this->prefix . 'entry` e1 WHERE e1.id_feed=f.id),'
  347. . 'f.cache_nbUnreads=(SELECT COUNT(e2.id) FROM `' . $this->prefix . 'entry` e2 WHERE e2.id_feed=f.id AND e2.is_read=0),'
  348. . 'lastUpdate=?, error=? '
  349. . 'WHERE f.id=?';
  350. $stm = $this->bd->prepare ($sql);
  351. $values = array (
  352. time (),
  353. $inError,
  354. $id,
  355. );
  356. if ($stm && $stm->execute ($values)) {
  357. return $stm->rowCount();
  358. } else {
  359. $info = $stm->errorInfo();
  360. Minz_Log::record ('SQL error : ' . $info[2], Minz_Log::ERROR);
  361. return false;
  362. }
  363. }
  364. public function changeCategory ($idOldCat, $idNewCat) {
  365. $catDAO = new CategoryDAO ();
  366. $newCat = $catDAO->searchById ($idNewCat);
  367. if (!$newCat) {
  368. $newCat = $catDAO->getDefault ();
  369. }
  370. $sql = 'UPDATE `' . $this->prefix . 'feed` SET category=? WHERE category=?';
  371. $stm = $this->bd->prepare ($sql);
  372. $values = array (
  373. $newCat->id (),
  374. $idOldCat
  375. );
  376. if ($stm && $stm->execute ($values)) {
  377. return $stm->rowCount();
  378. } else {
  379. $info = $stm->errorInfo();
  380. Minz_Log::record ('SQL error : ' . $info[2], Minz_Log::ERROR);
  381. return false;
  382. }
  383. }
  384. public function deleteFeed ($id) {
  385. /*//For MYISAM (MySQL 5.5-) without FOREIGN KEY
  386. $sql = 'DELETE FROM `' . $this->prefix . 'entry` WHERE id_feed=?';
  387. $stm = $this->bd->prepare ($sql);
  388. $values = array ($id);
  389. if (!($stm && $stm->execute ($values))) {
  390. $info = $stm->errorInfo();
  391. Minz_Log::record ('SQL error : ' . $info[2], Minz_Log::ERROR);
  392. return false;
  393. }*/
  394. $sql = 'DELETE FROM `' . $this->prefix . 'feed` WHERE id=?';
  395. $stm = $this->bd->prepare ($sql);
  396. $values = array ($id);
  397. if ($stm && $stm->execute ($values)) {
  398. return $stm->rowCount();
  399. } else {
  400. $info = $stm->errorInfo();
  401. Minz_Log::record ('SQL error : ' . $info[2], Minz_Log::ERROR);
  402. return false;
  403. }
  404. }
  405. public function deleteFeedByCategory ($id) {
  406. /*//For MYISAM (MySQL 5.5-) without FOREIGN KEY
  407. $sql = 'DELETE FROM `' . $this->prefix . 'entry` e '
  408. . 'INNER JOIN `' . $this->prefix . 'feed` f ON e.id_feed = f.id '
  409. . 'WHERE f.category=?';
  410. $stm = $this->bd->prepare ($sql);
  411. $values = array ($id);
  412. if (!($stm && $stm->execute ($values))) {
  413. $info = $stm->errorInfo();
  414. Minz_Log::record ('SQL error : ' . $info[2], Minz_Log::ERROR);
  415. return false;
  416. }*/
  417. $sql = 'DELETE FROM `' . $this->prefix . 'feed` WHERE category=?';
  418. $stm = $this->bd->prepare ($sql);
  419. $values = array ($id);
  420. if ($stm && $stm->execute ($values)) {
  421. return $stm->rowCount();
  422. } else {
  423. $info = $stm->errorInfo();
  424. Minz_Log::record ('SQL error : ' . $info[2], Minz_Log::ERROR);
  425. return false;
  426. }
  427. }
  428. public function searchById ($id) {
  429. $sql = 'SELECT * FROM `' . $this->prefix . 'feed` WHERE id=?';
  430. $stm = $this->bd->prepare ($sql);
  431. $values = array ($id);
  432. $stm->execute ($values);
  433. $res = $stm->fetchAll (PDO::FETCH_ASSOC);
  434. $feed = HelperFeed::daoToFeed ($res);
  435. if (isset ($feed[$id])) {
  436. return $feed[$id];
  437. } else {
  438. return false;
  439. }
  440. }
  441. public function searchByUrl ($url) {
  442. $sql = 'SELECT * FROM `' . $this->prefix . 'feed` WHERE url=?';
  443. $stm = $this->bd->prepare ($sql);
  444. $values = array ($url);
  445. $stm->execute ($values);
  446. $res = $stm->fetchAll (PDO::FETCH_ASSOC);
  447. $feed = current (HelperFeed::daoToFeed ($res));
  448. if (isset ($feed)) {
  449. return $feed;
  450. } else {
  451. return false;
  452. }
  453. }
  454. public function listFeeds () {
  455. $sql = 'SELECT * FROM `' . $this->prefix . 'feed` ORDER BY name';
  456. $stm = $this->bd->prepare ($sql);
  457. $stm->execute ();
  458. return HelperFeed::daoToFeed ($stm->fetchAll (PDO::FETCH_ASSOC));
  459. }
  460. public function listFeedsOrderUpdate () {
  461. $sql = 'SELECT * FROM `' . $this->prefix . 'feed` ORDER BY lastUpdate';
  462. $stm = $this->bd->prepare ($sql);
  463. $stm->execute ();
  464. return HelperFeed::daoToFeed ($stm->fetchAll (PDO::FETCH_ASSOC));
  465. }
  466. public function listByCategory ($cat) {
  467. $sql = 'SELECT * FROM `' . $this->prefix . 'feed` WHERE category=? ORDER BY name';
  468. $stm = $this->bd->prepare ($sql);
  469. $values = array ($cat);
  470. $stm->execute ($values);
  471. return HelperFeed::daoToFeed ($stm->fetchAll (PDO::FETCH_ASSOC));
  472. }
  473. public function countEntries ($id) {
  474. $sql = 'SELECT COUNT(*) AS count FROM `' . $this->prefix . 'entry` WHERE id_feed=?';
  475. $stm = $this->bd->prepare ($sql);
  476. $values = array ($id);
  477. $stm->execute ($values);
  478. $res = $stm->fetchAll (PDO::FETCH_ASSOC);
  479. return $res[0]['count'];
  480. }
  481. public function countNotRead ($id) {
  482. $sql = 'SELECT COUNT(*) AS count FROM `' . $this->prefix . 'entry` WHERE id_feed=? AND is_read=0';
  483. $stm = $this->bd->prepare ($sql);
  484. $values = array ($id);
  485. $stm->execute ($values);
  486. $res = $stm->fetchAll (PDO::FETCH_ASSOC);
  487. return $res[0]['count'];
  488. }
  489. public function updateCachedValues () { //For one single feed, call updateLastUpdate($id)
  490. $sql = 'UPDATE `' . $this->prefix . 'feed` f '
  491. . 'INNER JOIN ('
  492. . 'SELECT e.id_feed, '
  493. . 'COUNT(CASE WHEN e.is_read = 0 THEN 1 END) AS nbUnreads, '
  494. . 'COUNT(e.id) AS nbEntries '
  495. . 'FROM `' . $this->prefix . 'entry` e '
  496. . 'GROUP BY e.id_feed'
  497. . ') x ON x.id_feed=f.id '
  498. . 'SET f.cache_nbEntries=x.nbEntries, f.cache_nbUnreads=x.nbUnreads';
  499. $stm = $this->bd->prepare ($sql);
  500. $values = array ($feed_id);
  501. if ($stm && $stm->execute ($values)) {
  502. return $stm->rowCount();
  503. } else {
  504. $info = $stm->errorInfo();
  505. Minz_Log::record ('SQL error : ' . $info[2], Minz_Log::ERROR);
  506. return false;
  507. }
  508. }
  509. }
  510. class HelperFeed {
  511. public static function daoToFeed ($listDAO, $catID = null) {
  512. $list = array ();
  513. if (!is_array ($listDAO)) {
  514. $listDAO = array ($listDAO);
  515. }
  516. foreach ($listDAO as $key => $dao) {
  517. if (!isset ($dao['name'])) {
  518. continue;
  519. }
  520. if (isset ($dao['id'])) {
  521. $key = $dao['id'];
  522. }
  523. $myFeed = new Feed (isset($dao['url']) ? $dao['url'] : '', false);
  524. $myFeed->_category ($catID === null ? $dao['category'] : $catID);
  525. $myFeed->_name ($dao['name']);
  526. $myFeed->_website ($dao['website'], false);
  527. $myFeed->_description (isset($dao['description']) ? $dao['description'] : '');
  528. $myFeed->_lastUpdate (isset($dao['lastUpdate']) ? $dao['lastUpdate'] : 0);
  529. $myFeed->_priority ($dao['priority']);
  530. $myFeed->_pathEntries (isset($dao['pathEntries']) ? $dao['pathEntries'] : '');
  531. $myFeed->_httpAuth (isset($dao['httpAuth']) ? base64_decode ($dao['httpAuth']) : '');
  532. $myFeed->_error ($dao['error']);
  533. $myFeed->_keepHistory (isset($dao['keep_history']) ? $dao['keep_history'] : '');
  534. $myFeed->_nbNotRead ($dao['cache_nbUnreads']);
  535. $myFeed->_nbEntries ($dao['cache_nbEntries']);
  536. if (isset ($dao['id'])) {
  537. $myFeed->_id ($dao['id']);
  538. }
  539. $list[$key] = $myFeed;
  540. }
  541. return $list;
  542. }
  543. }