Feed.php 16 KB

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