Feed.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593
  1. <?php
  2. class Feed extends Model {
  3. private $id = null;
  4. private $url;
  5. private $category = '000000';
  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. if(is_null($this->id)) {
  27. return small_hash ($this->url . Configuration::selApplication ());
  28. } else {
  29. return $this->id;
  30. }
  31. }
  32. public function url () {
  33. return $this->url;
  34. }
  35. public function category () {
  36. return $this->category;
  37. }
  38. public function entries () {
  39. if (!is_null ($this->entries)) {
  40. return $this->entries;
  41. } else {
  42. return array ();
  43. }
  44. }
  45. public function name () {
  46. return $this->name;
  47. }
  48. public function website () {
  49. return $this->website;
  50. }
  51. public function description () {
  52. return $this->description;
  53. }
  54. public function lastUpdate () {
  55. return $this->lastUpdate;
  56. }
  57. public function priority () {
  58. return $this->priority;
  59. }
  60. public function pathEntries () {
  61. return $this->pathEntries;
  62. }
  63. public function httpAuth ($raw = true) {
  64. if ($raw) {
  65. return $this->httpAuth;
  66. } else {
  67. $pos_colon = strpos ($this->httpAuth, ':');
  68. $user = substr ($this->httpAuth, 0, $pos_colon);
  69. $pass = substr ($this->httpAuth, $pos_colon + 1);
  70. return array (
  71. 'username' => $user,
  72. 'password' => $pass
  73. );
  74. }
  75. }
  76. public function inError () {
  77. return $this->error;
  78. }
  79. public function keepHistory () {
  80. return $this->keep_history;
  81. }
  82. public function nbEntries () {
  83. if ($this->nbEntries < 0) {
  84. $feedDAO = new FeedDAO ();
  85. $this->nbEntries = $feedDAO->countEntries ($this->id ());
  86. }
  87. return $this->nbEntries;
  88. }
  89. public function nbNotRead () {
  90. if ($this->nbNotRead < 0) {
  91. $feedDAO = new FeedDAO ();
  92. $this->nbNotRead = $feedDAO->countNotRead ($this->id ());
  93. }
  94. return $this->nbNotRead;
  95. }
  96. public function favicon () {
  97. $file = '/favicons/' . $this->id () . '.ico';
  98. $favicon_url = Url::display ($file);
  99. if (!file_exists (PUBLIC_PATH . $file)) {
  100. $favicon_url = dowload_favicon ($this->website (), $this->id ());
  101. }
  102. return $favicon_url;
  103. }
  104. public function _id ($value) {
  105. $this->id = $value;
  106. }
  107. public function _url ($value) {
  108. if (empty ($value)) {
  109. throw new BadUrlException ($value);
  110. }
  111. if (!preg_match ('#^https?://#i', $value)) {
  112. $value = 'http://' . $value;
  113. }
  114. if (filter_var ($value, FILTER_VALIDATE_URL)) {
  115. $this->url = $value;
  116. } elseif (version_compare(PHP_VERSION, '5.3.3', '<') && (strpos($value, '-') > 0) && ($value === filter_var($value, FILTER_SANITIZE_URL))) { //PHP bug #51192
  117. $this->url = $value;
  118. } else {
  119. throw new BadUrlException ($value);
  120. }
  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) {
  132. if (is_null ($value)) {
  133. $value = '';
  134. }
  135. $this->website = $value;
  136. }
  137. public function _description ($value) {
  138. if (is_null ($value)) {
  139. $value = '';
  140. }
  141. $this->description = $value;
  142. }
  143. public function _lastUpdate ($value) {
  144. $this->lastUpdate = $value;
  145. }
  146. public function _priority ($value) {
  147. $this->priority = is_numeric ($value) ? intval ($value) : 10;
  148. }
  149. public function _pathEntries ($value) {
  150. $this->pathEntries = $value;
  151. }
  152. public function _httpAuth ($value) {
  153. $this->httpAuth = $value;
  154. }
  155. public function _error ($value) {
  156. if ($value) {
  157. $value = true;
  158. } else {
  159. $value = false;
  160. }
  161. $this->error = $value;
  162. }
  163. public function _keepHistory ($value) {
  164. if ($value) {
  165. $value = true;
  166. } else {
  167. $value = false;
  168. }
  169. $this->keep_history = $value;
  170. }
  171. public function _nbNotRead ($value) {
  172. $this->nbNotRead = is_numeric ($value) ? intval ($value) : -1;
  173. }
  174. public function _nbEntries ($value) {
  175. $this->nbEntries = is_numeric ($value) ? intval ($value) : -1;
  176. }
  177. public function load () {
  178. if (!is_null ($this->url)) {
  179. if (CACHE_PATH === false) {
  180. throw new FileNotExistException (
  181. 'CACHE_PATH',
  182. MinzException::ERROR
  183. );
  184. } else {
  185. $feed = new SimplePie ();
  186. $feed->set_useragent(Translate::t ('freshrss') . '/' . FRESHRSS_VERSION . ' (' . PHP_OS . '; ' . FRESHRSS_WEBSITE . ') ' . SIMPLEPIE_NAME . '/' . SIMPLEPIE_VERSION);
  187. $url = str_replace ('&amp;', '&', $this->url);
  188. if ($this->httpAuth != '') {
  189. $url = preg_replace ('#((.+)://)(.+)#', '${1}' . $this->httpAuth . '@${3}', $url);
  190. }
  191. $feed->set_feed_url ($url);
  192. $feed->set_cache_location (CACHE_PATH);
  193. $feed->set_cache_duration(1500);
  194. $feed->strip_htmltags (array (
  195. 'base', 'blink', 'body', 'doctype',
  196. 'font', 'form', 'frame', 'frameset', 'html',
  197. 'input', 'marquee', 'meta', 'noscript',
  198. 'param', 'script', 'style'
  199. ));
  200. $feed->strip_attributes(array_merge($feed->strip_attributes, array(
  201. 'onload', 'onunload', 'onclick', 'ondblclick', 'onmousedown', 'onmouseup',
  202. 'onmouseover', 'onmousemove', 'onmouseout', 'onfocus', 'onblur',
  203. 'onkeypress', 'onkeydown', 'onkeyup', 'onselect', 'onchange')));
  204. $feed->set_url_replacements(array(
  205. 'a' => 'href',
  206. 'area' => 'href',
  207. 'audio' => 'src',
  208. 'blockquote' => 'cite',
  209. 'del' => 'cite',
  210. 'form' => 'action',
  211. 'img' => array(
  212. 'longdesc',
  213. 'src'
  214. ),
  215. 'input' => 'src',
  216. 'ins' => 'cite',
  217. 'q' => 'cite',
  218. 'source' => 'src',
  219. 'track' => 'src',
  220. 'video' => 'src',
  221. ));
  222. $feed->init ();
  223. if ($feed->error ()) {
  224. throw new FeedException ($feed->error . ' [' . $url . ']');
  225. }
  226. // si on a utilisé l'auto-discover, notre url va avoir changé
  227. $subscribe_url = $feed->subscribe_url ();
  228. if (!is_null ($subscribe_url) && $subscribe_url != $this->url) {
  229. if ($this->httpAuth != '') {
  230. // on enlève les id si authentification HTTP
  231. $subscribe_url = preg_replace ('#((.+)://)((.+)@)(.+)#', '${1}${5}', $subscribe_url);
  232. }
  233. $this->_url ($subscribe_url);
  234. }
  235. if (empty($this->name)) { // May come from OPML
  236. $title = $feed->get_title ();
  237. $this->_name (!is_null ($title) ? $title : $this->url);
  238. }
  239. $this->_website ($feed->get_link ());
  240. $this->_description ($feed->get_description ());
  241. // et on charge les articles du flux
  242. $this->loadEntries ($feed);
  243. }
  244. }
  245. }
  246. static function html_only_entity_decode($text) {
  247. static $htmlEntitiesOnly = null;
  248. if ($htmlEntitiesOnly === null) {
  249. $htmlEntitiesOnly = array_flip(array_diff(
  250. get_html_translation_table(HTML_ENTITIES, ENT_NOQUOTES, 'UTF-8'), //Decode HTML entities
  251. get_html_translation_table(HTML_SPECIALCHARS, ENT_NOQUOTES, 'UTF-8') //Preserve XML entities
  252. ));
  253. }
  254. return strtr($text, $htmlEntitiesOnly);
  255. }
  256. private function loadEntries ($feed) {
  257. $entries = array ();
  258. foreach ($feed->get_items () as $item) {
  259. $title = self::html_only_entity_decode (strip_tags ($item->get_title ()));
  260. $author = $item->get_author ();
  261. $link = $item->get_permalink ();
  262. $date = strtotime ($item->get_date ());
  263. // gestion des tags (catégorie == tag)
  264. $tags_tmp = $item->get_categories ();
  265. $tags = array ();
  266. if (!is_null ($tags_tmp)) {
  267. foreach ($tags_tmp as $tag) {
  268. $tags[] = self::html_only_entity_decode ($tag->get_label ());
  269. }
  270. }
  271. $content = self::html_only_entity_decode ($item->get_content ());
  272. $elinks = array();
  273. foreach ($item->get_enclosures() as $enclosure) {
  274. $elink = $enclosure->get_link();
  275. if (array_key_exists($elink, $elinks)) continue;
  276. $elinks[$elink] = '1';
  277. $mime = strtolower($enclosure->get_type());
  278. if (strpos($mime, 'image/') === 0) {
  279. $content .= '<br /><img src="' . $elink . '" alt="" />';
  280. }
  281. }
  282. $entry = new Entry (
  283. $this->id (),
  284. $item->get_id (),
  285. !is_null ($title) ? $title : '',
  286. !is_null ($author) ? self::html_only_entity_decode ($author->name) : '',
  287. !is_null ($content) ? $content : '',
  288. !is_null ($link) ? $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->id ()] = $entry;
  295. }
  296. $this->entries = $entries;
  297. }
  298. }
  299. class FeedDAO extends Model_pdo {
  300. public function addFeed ($valuesTmp) {
  301. $sql = 'INSERT INTO ' . $this->prefix . 'feed (id, url, category, name, website, description, lastUpdate, priority, httpAuth, error, keep_history) VALUES(?, ?, ?, ?, ?, ?, ?, 10, ?, 0, 0)';
  302. $stm = $this->bd->prepare ($sql);
  303. $values = array (
  304. $valuesTmp['id'],
  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 $stm->rowCount();
  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. $sql = 'DELETE FROM ' . $this->prefix . 'feed WHERE id=?';
  386. $stm = $this->bd->prepare ($sql);
  387. $values = array ($id);
  388. if ($stm && $stm->execute ($values)) {
  389. return $stm->rowCount();
  390. } else {
  391. $info = $stm->errorInfo();
  392. Minz_Log::record ('SQL error : ' . $info[2], Minz_Log::ERROR);
  393. return false;
  394. }
  395. }
  396. public function deleteFeedByCategory ($id) {
  397. $sql = 'DELETE FROM ' . $this->prefix . 'feed WHERE category=?';
  398. $stm = $this->bd->prepare ($sql);
  399. $values = array ($id);
  400. if ($stm && $stm->execute ($values)) {
  401. return $stm->rowCount();
  402. } else {
  403. $info = $stm->errorInfo();
  404. Minz_Log::record ('SQL error : ' . $info[2], Minz_Log::ERROR);
  405. return false;
  406. }
  407. }
  408. public function searchById ($id) {
  409. $sql = 'SELECT * FROM ' . $this->prefix . 'feed WHERE id=?';
  410. $stm = $this->bd->prepare ($sql);
  411. $values = array ($id);
  412. $stm->execute ($values);
  413. $res = $stm->fetchAll (PDO::FETCH_ASSOC);
  414. $feed = HelperFeed::daoToFeed ($res);
  415. if (isset ($feed[$id])) {
  416. return $feed[$id];
  417. } else {
  418. return false;
  419. }
  420. }
  421. public function searchByUrl ($url) {
  422. $sql = 'SELECT * FROM ' . $this->prefix . 'feed WHERE url=?';
  423. $stm = $this->bd->prepare ($sql);
  424. $values = array ($url);
  425. $stm->execute ($values);
  426. $res = $stm->fetchAll (PDO::FETCH_ASSOC);
  427. $feed = current (HelperFeed::daoToFeed ($res));
  428. if (isset ($feed)) {
  429. return $feed;
  430. } else {
  431. return false;
  432. }
  433. }
  434. public function listFeeds () {
  435. $sql = 'SELECT * FROM ' . $this->prefix . 'feed ORDER BY name';
  436. $stm = $this->bd->prepare ($sql);
  437. $stm->execute ();
  438. return HelperFeed::daoToFeed ($stm->fetchAll (PDO::FETCH_ASSOC));
  439. }
  440. public function listFeedsOrderUpdate () {
  441. $sql = 'SELECT * FROM ' . $this->prefix . 'feed ORDER BY lastUpdate';
  442. $stm = $this->bd->prepare ($sql);
  443. $stm->execute ();
  444. return HelperFeed::daoToFeed ($stm->fetchAll (PDO::FETCH_ASSOC));
  445. }
  446. public function listByCategory ($cat) {
  447. $sql = 'SELECT * FROM ' . $this->prefix . 'feed WHERE category=? ORDER BY name';
  448. $stm = $this->bd->prepare ($sql);
  449. $values = array ($cat);
  450. $stm->execute ($values);
  451. return HelperFeed::daoToFeed ($stm->fetchAll (PDO::FETCH_ASSOC));
  452. }
  453. public function countEntries ($id) {
  454. $sql = 'SELECT COUNT(*) AS count FROM ' . $this->prefix . 'entry WHERE id_feed=?';
  455. $stm = $this->bd->prepare ($sql);
  456. $values = array ($id);
  457. $stm->execute ($values);
  458. $res = $stm->fetchAll (PDO::FETCH_ASSOC);
  459. return $res[0]['count'];
  460. }
  461. public function countNotRead ($id) {
  462. $sql = 'SELECT COUNT(*) AS count FROM ' . $this->prefix . 'entry WHERE id_feed=? AND is_read=0';
  463. $stm = $this->bd->prepare ($sql);
  464. $values = array ($id);
  465. $stm->execute ($values);
  466. $res = $stm->fetchAll (PDO::FETCH_ASSOC);
  467. return $res[0]['count'];
  468. }
  469. public function updateCachedValues () { //For one single feed, call updateLastUpdate($id)
  470. $sql = 'UPDATE ' . $this->prefix . 'feed f '
  471. . 'INNER JOIN ('
  472. . 'SELECT e.id_feed, '
  473. . 'COUNT(CASE WHEN e.is_read = 0 THEN 1 END) AS nbUnreads, '
  474. . 'COUNT(e.id) AS nbEntries '
  475. . 'FROM ' . $this->prefix . 'entry e '
  476. . 'GROUP BY e.id_feed'
  477. . ') x ON x.id_feed=f.id '
  478. . 'SET f.cache_nbEntries=x.nbEntries, f.cache_nbUnreads=x.nbUnreads';
  479. $stm = $this->bd->prepare ($sql);
  480. $values = array ($feed_id);
  481. if ($stm && $stm->execute ($values)) {
  482. return $stm->rowCount();
  483. } else {
  484. $info = $stm->errorInfo();
  485. Minz_Log::record ('SQL error : ' . $info[2], Minz_Log::ERROR);
  486. return false;
  487. }
  488. }
  489. }
  490. class HelperFeed {
  491. public static function daoToFeed ($listDAO, $catID = null) {
  492. $list = array ();
  493. if (!is_array ($listDAO)) {
  494. $listDAO = array ($listDAO);
  495. }
  496. foreach ($listDAO as $key => $dao) {
  497. if (!isset ($dao['name'])) {
  498. continue;
  499. }
  500. if (isset ($dao['id'])) {
  501. $key = $dao['id'];
  502. }
  503. $myFeed = new Feed (isset($dao['url']) ? $dao['url'] : '', false);
  504. $myFeed->_category ($catID === null ? $dao['category'] : $catID);
  505. $myFeed->_name ($dao['name']);
  506. $myFeed->_website ($dao['website']);
  507. $myFeed->_description (isset($dao['description']) ? $dao['description'] : '');
  508. $myFeed->_lastUpdate (isset($dao['lastUpdate']) ? $dao['lastUpdate'] : 0);
  509. $myFeed->_priority ($dao['priority']);
  510. $myFeed->_pathEntries (isset($dao['pathEntries']) ? $dao['pathEntries'] : '');
  511. $myFeed->_httpAuth (isset($dao['httpAuth']) ? base64_decode ($dao['httpAuth']) : '');
  512. $myFeed->_error ($dao['error']);
  513. $myFeed->_keepHistory (isset($dao['keep_history']) ? $dao['keep_history'] : '');
  514. $myFeed->_nbNotRead ($dao['cache_nbUnreads']);
  515. $myFeed->_nbEntries ($dao['cache_nbEntries']);
  516. if (isset ($dao['id'])) {
  517. $myFeed->_id ($dao['id']);
  518. }
  519. $list[$key] = $myFeed;
  520. }
  521. return $list;
  522. }
  523. }