Feed.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588
  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. $favicon_url = dowload_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 = is_numeric ($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 = is_numeric ($value) ? intval ($value) : -1;
  169. }
  170. public function _nbEntries ($value) {
  171. $this->nbEntries = is_numeric ($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 = str_replace ('&amp;', '&', $this->url);
  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. static function html_only_entity_decode($text) {
  243. static $htmlEntitiesOnly = null;
  244. if ($htmlEntitiesOnly === null) {
  245. $htmlEntitiesOnly = array_flip(array_diff(
  246. get_html_translation_table(HTML_ENTITIES, ENT_NOQUOTES, 'UTF-8'), //Decode HTML entities
  247. get_html_translation_table(HTML_SPECIALCHARS, ENT_NOQUOTES, 'UTF-8') //Preserve XML entities
  248. ));
  249. }
  250. return strtr($text, $htmlEntitiesOnly);
  251. }
  252. private function loadEntries ($feed) {
  253. $entries = array ();
  254. foreach ($feed->get_items () as $item) {
  255. $title = html_only_entity_decode (strip_tags ($item->get_title ()));
  256. $author = $item->get_author ();
  257. $link = $item->get_permalink ();
  258. $date = strtotime ($item->get_date ());
  259. // gestion des tags (catégorie == tag)
  260. $tags_tmp = $item->get_categories ();
  261. $tags = array ();
  262. if (!is_null ($tags_tmp)) {
  263. foreach ($tags_tmp as $tag) {
  264. $tags[] = html_only_entity_decode ($tag->get_label ());
  265. }
  266. }
  267. $content = html_only_entity_decode ($item->get_content ());
  268. $elinks = array();
  269. foreach ($item->get_enclosures() as $enclosure) {
  270. $elink = $enclosure->get_link();
  271. if (array_key_exists($elink, $elinks)) continue;
  272. $elinks[$elink] = '1';
  273. $mime = strtolower($enclosure->get_type());
  274. if (strpos($mime, 'image/') === 0) {
  275. $content .= '<br /><img src="' . $elink . '" alt="" />';
  276. }
  277. }
  278. $entry = new Entry (
  279. $this->id (),
  280. $item->get_id (),
  281. !is_null ($title) ? $title : '',
  282. !is_null ($author) ? html_only_entity_decode ($author->name) : '',
  283. !is_null ($content) ? $content : '',
  284. !is_null ($link) ? $link : '',
  285. $date ? $date : time ()
  286. );
  287. $entry->_tags ($tags);
  288. // permet de récupérer le contenu des flux tronqués
  289. $entry->loadCompleteContent($this->pathEntries());
  290. $entries[$entry->id ()] = $entry;
  291. }
  292. $this->entries = $entries;
  293. }
  294. }
  295. class FeedDAO extends Model_pdo {
  296. public function addFeed ($valuesTmp) {
  297. $sql = 'INSERT INTO ' . $this->prefix . 'feed (url, category, name, website, description, lastUpdate, priority, httpAuth, error, keep_history) VALUES(?, ?, ?, ?, ?, ?, 10, ?, 0, 0)';
  298. $stm = $this->bd->prepare ($sql);
  299. $values = array (
  300. substr($valuesTmp['url'], 0, 511),
  301. $valuesTmp['category'],
  302. substr($valuesTmp['name'], 0, 255),
  303. substr($valuesTmp['website'], 0, 255),
  304. substr($valuesTmp['description'], 0, 1023),
  305. $valuesTmp['lastUpdate'],
  306. base64_encode ($valuesTmp['httpAuth']),
  307. );
  308. if ($stm && $stm->execute ($values)) {
  309. return $this->bd->lastInsertId();
  310. } else {
  311. $info = $stm->errorInfo();
  312. Minz_Log::record ('SQL error : ' . $info[2], Minz_Log::ERROR);
  313. return false;
  314. }
  315. }
  316. public function updateFeed ($id, $valuesTmp) {
  317. $set = '';
  318. foreach ($valuesTmp as $key => $v) {
  319. $set .= $key . '=?, ';
  320. if ($key == 'httpAuth') {
  321. $valuesTmp[$key] = base64_encode ($v);
  322. }
  323. }
  324. $set = substr ($set, 0, -2);
  325. $sql = 'UPDATE ' . $this->prefix . 'feed SET ' . $set . ' WHERE id=?';
  326. $stm = $this->bd->prepare ($sql);
  327. foreach ($valuesTmp as $v) {
  328. $values[] = $v;
  329. }
  330. $values[] = $id;
  331. if ($stm && $stm->execute ($values)) {
  332. return $stm->rowCount();
  333. } else {
  334. $info = $stm->errorInfo();
  335. Minz_Log::record ('SQL error : ' . $info[2], Minz_Log::ERROR);
  336. return false;
  337. }
  338. }
  339. public function updateLastUpdate ($id, $inError = 0) {
  340. $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
  341. . 'SET f.cache_nbEntries=(SELECT COUNT(e1.id) FROM ' . $this->prefix . 'entry e1 WHERE e1.id_feed=f.id),'
  342. . 'f.cache_nbUnreads=(SELECT COUNT(e2.id) FROM ' . $this->prefix . 'entry e2 WHERE e2.id_feed=f.id AND e2.is_read=0),'
  343. . 'lastUpdate=?, error=? '
  344. . 'WHERE f.id=?';
  345. $stm = $this->bd->prepare ($sql);
  346. $values = array (
  347. time (),
  348. $inError,
  349. $id,
  350. );
  351. if ($stm && $stm->execute ($values)) {
  352. return $stm->rowCount();
  353. } else {
  354. $info = $stm->errorInfo();
  355. Minz_Log::record ('SQL error : ' . $info[2], Minz_Log::ERROR);
  356. return false;
  357. }
  358. }
  359. public function changeCategory ($idOldCat, $idNewCat) {
  360. $catDAO = new CategoryDAO ();
  361. $newCat = $catDAO->searchById ($idNewCat);
  362. if (!$newCat) {
  363. $newCat = $catDAO->getDefault ();
  364. }
  365. $sql = 'UPDATE ' . $this->prefix . 'feed SET category=? WHERE category=?';
  366. $stm = $this->bd->prepare ($sql);
  367. $values = array (
  368. $newCat->id (),
  369. $idOldCat
  370. );
  371. if ($stm && $stm->execute ($values)) {
  372. return $stm->rowCount();
  373. } else {
  374. $info = $stm->errorInfo();
  375. Minz_Log::record ('SQL error : ' . $info[2], Minz_Log::ERROR);
  376. return false;
  377. }
  378. }
  379. public function deleteFeed ($id) {
  380. $sql = 'DELETE FROM ' . $this->prefix . 'feed WHERE id=?';
  381. $stm = $this->bd->prepare ($sql);
  382. $values = array ($id);
  383. if ($stm && $stm->execute ($values)) {
  384. return $stm->rowCount();
  385. } else {
  386. $info = $stm->errorInfo();
  387. Minz_Log::record ('SQL error : ' . $info[2], Minz_Log::ERROR);
  388. return false;
  389. }
  390. }
  391. public function deleteFeedByCategory ($id) {
  392. $sql = 'DELETE FROM ' . $this->prefix . 'feed WHERE category=?';
  393. $stm = $this->bd->prepare ($sql);
  394. $values = array ($id);
  395. if ($stm && $stm->execute ($values)) {
  396. return $stm->rowCount();
  397. } else {
  398. $info = $stm->errorInfo();
  399. Minz_Log::record ('SQL error : ' . $info[2], Minz_Log::ERROR);
  400. return false;
  401. }
  402. }
  403. public function searchById ($id) {
  404. $sql = 'SELECT * FROM ' . $this->prefix . 'feed WHERE id=?';
  405. $stm = $this->bd->prepare ($sql);
  406. $values = array ($id);
  407. $stm->execute ($values);
  408. $res = $stm->fetchAll (PDO::FETCH_ASSOC);
  409. $feed = HelperFeed::daoToFeed ($res);
  410. if (isset ($feed[$id])) {
  411. return $feed[$id];
  412. } else {
  413. return false;
  414. }
  415. }
  416. public function searchByUrl ($url) {
  417. $sql = 'SELECT * FROM ' . $this->prefix . 'feed WHERE url=?';
  418. $stm = $this->bd->prepare ($sql);
  419. $values = array ($url);
  420. $stm->execute ($values);
  421. $res = $stm->fetchAll (PDO::FETCH_ASSOC);
  422. $feed = current (HelperFeed::daoToFeed ($res));
  423. if (isset ($feed)) {
  424. return $feed;
  425. } else {
  426. return false;
  427. }
  428. }
  429. public function listFeeds () {
  430. $sql = 'SELECT * FROM ' . $this->prefix . 'feed ORDER BY name';
  431. $stm = $this->bd->prepare ($sql);
  432. $stm->execute ();
  433. return HelperFeed::daoToFeed ($stm->fetchAll (PDO::FETCH_ASSOC));
  434. }
  435. public function listFeedsOrderUpdate () {
  436. $sql = 'SELECT * FROM ' . $this->prefix . 'feed ORDER BY lastUpdate';
  437. $stm = $this->bd->prepare ($sql);
  438. $stm->execute ();
  439. return HelperFeed::daoToFeed ($stm->fetchAll (PDO::FETCH_ASSOC));
  440. }
  441. public function listByCategory ($cat) {
  442. $sql = 'SELECT * FROM ' . $this->prefix . 'feed WHERE category=? ORDER BY name';
  443. $stm = $this->bd->prepare ($sql);
  444. $values = array ($cat);
  445. $stm->execute ($values);
  446. return HelperFeed::daoToFeed ($stm->fetchAll (PDO::FETCH_ASSOC));
  447. }
  448. public function countEntries ($id) {
  449. $sql = 'SELECT COUNT(*) AS count FROM ' . $this->prefix . 'entry WHERE id_feed=?';
  450. $stm = $this->bd->prepare ($sql);
  451. $values = array ($id);
  452. $stm->execute ($values);
  453. $res = $stm->fetchAll (PDO::FETCH_ASSOC);
  454. return $res[0]['count'];
  455. }
  456. public function countNotRead ($id) {
  457. $sql = 'SELECT COUNT(*) AS count FROM ' . $this->prefix . 'entry WHERE id_feed=? AND is_read=0';
  458. $stm = $this->bd->prepare ($sql);
  459. $values = array ($id);
  460. $stm->execute ($values);
  461. $res = $stm->fetchAll (PDO::FETCH_ASSOC);
  462. return $res[0]['count'];
  463. }
  464. public function updateCachedValues () { //For one single feed, call updateLastUpdate($id)
  465. $sql = 'UPDATE ' . $this->prefix . 'feed f '
  466. . 'INNER JOIN ('
  467. . 'SELECT e.id_feed, '
  468. . 'COUNT(CASE WHEN e.is_read = 0 THEN 1 END) AS nbUnreads, '
  469. . 'COUNT(e.id) AS nbEntries '
  470. . 'FROM ' . $this->prefix . 'entry e '
  471. . 'GROUP BY e.id_feed'
  472. . ') x ON x.id_feed=f.id '
  473. . 'SET f.cache_nbEntries=x.nbEntries, f.cache_nbUnreads=x.nbUnreads';
  474. $stm = $this->bd->prepare ($sql);
  475. $values = array ($feed_id);
  476. if ($stm && $stm->execute ($values)) {
  477. return $stm->rowCount();
  478. } else {
  479. $info = $stm->errorInfo();
  480. Minz_Log::record ('SQL error : ' . $info[2], Minz_Log::ERROR);
  481. return false;
  482. }
  483. }
  484. }
  485. class HelperFeed {
  486. public static function daoToFeed ($listDAO, $catID = null) {
  487. $list = array ();
  488. if (!is_array ($listDAO)) {
  489. $listDAO = array ($listDAO);
  490. }
  491. foreach ($listDAO as $key => $dao) {
  492. if (!isset ($dao['name'])) {
  493. continue;
  494. }
  495. if (isset ($dao['id'])) {
  496. $key = $dao['id'];
  497. }
  498. $myFeed = new Feed (isset($dao['url']) ? $dao['url'] : '', false);
  499. $myFeed->_category ($catID === null ? $dao['category'] : $catID);
  500. $myFeed->_name ($dao['name']);
  501. $myFeed->_website ($dao['website']);
  502. $myFeed->_description (isset($dao['description']) ? $dao['description'] : '');
  503. $myFeed->_lastUpdate (isset($dao['lastUpdate']) ? $dao['lastUpdate'] : 0);
  504. $myFeed->_priority ($dao['priority']);
  505. $myFeed->_pathEntries (isset($dao['pathEntries']) ? $dao['pathEntries'] : '');
  506. $myFeed->_httpAuth (isset($dao['httpAuth']) ? base64_decode ($dao['httpAuth']) : '');
  507. $myFeed->_error ($dao['error']);
  508. $myFeed->_keepHistory (isset($dao['keep_history']) ? $dao['keep_history'] : '');
  509. $myFeed->_nbNotRead ($dao['cache_nbUnreads']);
  510. $myFeed->_nbEntries ($dao['cache_nbEntries']);
  511. if (isset ($dao['id'])) {
  512. $myFeed->_id ($dao['id']);
  513. }
  514. $list[$key] = $myFeed;
  515. }
  516. return $list;
  517. }
  518. }