Feed.php 16 KB

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