Feed.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592
  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 = '/data/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. $url = str_replace ('&amp;', '&', $this->url);
  187. if ($this->httpAuth != '') {
  188. $url = preg_replace ('#((.+)://)(.+)#', '${1}' . $this->httpAuth . '@${3}', $url);
  189. }
  190. $feed->set_feed_url ($url);
  191. $feed->set_cache_location (CACHE_PATH);
  192. $feed->set_cache_duration(1500);
  193. $feed->strip_htmltags (array (
  194. 'base', 'blink', 'body', 'doctype',
  195. 'font', 'form', 'frame', 'frameset', 'html',
  196. 'input', 'marquee', 'meta', 'noscript',
  197. 'param', 'script', 'style'
  198. ));
  199. $feed->strip_attributes(array_merge($feed->strip_attributes, array(
  200. 'onload', 'onunload', 'onclick', 'ondblclick', 'onmousedown', 'onmouseup',
  201. 'onmouseover', 'onmousemove', 'onmouseout', 'onfocus', 'onblur',
  202. 'onkeypress', 'onkeydown', 'onkeyup', 'onselect', 'onchange')));
  203. $feed->set_url_replacements(array(
  204. 'a' => 'href',
  205. 'area' => 'href',
  206. 'audio' => 'src',
  207. 'blockquote' => 'cite',
  208. 'del' => 'cite',
  209. 'form' => 'action',
  210. 'img' => array(
  211. 'longdesc',
  212. 'src'
  213. ),
  214. 'input' => 'src',
  215. 'ins' => 'cite',
  216. 'q' => 'cite',
  217. 'source' => 'src',
  218. 'track' => 'src',
  219. 'video' => 'src',
  220. ));
  221. $feed->init ();
  222. if ($feed->error ()) {
  223. throw new FeedException ($feed->error . ' [' . $url . ']');
  224. }
  225. // si on a utilisé l'auto-discover, notre url va avoir changé
  226. $subscribe_url = $feed->subscribe_url ();
  227. if (!is_null ($subscribe_url) && $subscribe_url != $this->url) {
  228. if ($this->httpAuth != '') {
  229. // on enlève les id si authentification HTTP
  230. $subscribe_url = preg_replace ('#((.+)://)((.+)@)(.+)#', '${1}${5}', $subscribe_url);
  231. }
  232. $this->_url ($subscribe_url);
  233. }
  234. if (empty($this->name)) { // May come from OPML
  235. $title = $feed->get_title ();
  236. $this->_name (!is_null ($title) ? $title : $this->url);
  237. }
  238. $this->_website ($feed->get_link ());
  239. $this->_description ($feed->get_description ());
  240. // et on charge les articles du flux
  241. $this->loadEntries ($feed);
  242. }
  243. }
  244. }
  245. static function html_only_entity_decode($text) {
  246. static $htmlEntitiesOnly = null;
  247. if ($htmlEntitiesOnly === null) {
  248. $htmlEntitiesOnly = array_flip(array_diff(
  249. get_html_translation_table(HTML_ENTITIES, ENT_NOQUOTES, 'UTF-8'), //Decode HTML entities
  250. get_html_translation_table(HTML_SPECIALCHARS, ENT_NOQUOTES, 'UTF-8') //Preserve XML entities
  251. ));
  252. }
  253. return strtr($text, $htmlEntitiesOnly);
  254. }
  255. private function loadEntries ($feed) {
  256. $entries = array ();
  257. foreach ($feed->get_items () as $item) {
  258. $title = self::html_only_entity_decode (strip_tags ($item->get_title ()));
  259. $author = $item->get_author ();
  260. $link = $item->get_permalink ();
  261. $date = strtotime ($item->get_date ());
  262. // gestion des tags (catégorie == tag)
  263. $tags_tmp = $item->get_categories ();
  264. $tags = array ();
  265. if (!is_null ($tags_tmp)) {
  266. foreach ($tags_tmp as $tag) {
  267. $tags[] = self::html_only_entity_decode ($tag->get_label ());
  268. }
  269. }
  270. $content = self::html_only_entity_decode ($item->get_content ());
  271. $elinks = array();
  272. foreach ($item->get_enclosures() as $enclosure) {
  273. $elink = $enclosure->get_link();
  274. if (array_key_exists($elink, $elinks)) continue;
  275. $elinks[$elink] = '1';
  276. $mime = strtolower($enclosure->get_type());
  277. if (strpos($mime, 'image/') === 0) {
  278. $content .= '<br /><img src="' . $elink . '" alt="" />';
  279. }
  280. }
  281. $entry = new Entry (
  282. $this->id (),
  283. $item->get_id (),
  284. !is_null ($title) ? $title : '',
  285. !is_null ($author) ? self::html_only_entity_decode ($author->name) : '',
  286. !is_null ($content) ? $content : '',
  287. !is_null ($link) ? $link : '',
  288. $date ? $date : time ()
  289. );
  290. $entry->_tags ($tags);
  291. // permet de récupérer le contenu des flux tronqués
  292. $entry->loadCompleteContent($this->pathEntries());
  293. $entries[$entry->id ()] = $entry;
  294. }
  295. $this->entries = $entries;
  296. }
  297. }
  298. class FeedDAO extends Model_pdo {
  299. public function addFeed ($valuesTmp) {
  300. $sql = 'INSERT INTO ' . $this->prefix . 'feed (id, url, category, name, website, description, lastUpdate, priority, httpAuth, error, keep_history) VALUES(?, ?, ?, ?, ?, ?, ?, 10, ?, 0, 0)';
  301. $stm = $this->bd->prepare ($sql);
  302. $values = array (
  303. $valuesTmp['id'],
  304. substr($valuesTmp['url'], 0, 511),
  305. $valuesTmp['category'],
  306. substr($valuesTmp['name'], 0, 255),
  307. substr($valuesTmp['website'], 0, 255),
  308. substr($valuesTmp['description'], 0, 1023),
  309. $valuesTmp['lastUpdate'],
  310. base64_encode ($valuesTmp['httpAuth']),
  311. );
  312. if ($stm && $stm->execute ($values)) {
  313. return $stm->rowCount();
  314. } else {
  315. $info = $stm->errorInfo();
  316. Minz_Log::record ('SQL error : ' . $info[2], Minz_Log::ERROR);
  317. return false;
  318. }
  319. }
  320. public function updateFeed ($id, $valuesTmp) {
  321. $set = '';
  322. foreach ($valuesTmp as $key => $v) {
  323. $set .= $key . '=?, ';
  324. if ($key == 'httpAuth') {
  325. $valuesTmp[$key] = base64_encode ($v);
  326. }
  327. }
  328. $set = substr ($set, 0, -2);
  329. $sql = 'UPDATE ' . $this->prefix . 'feed SET ' . $set . ' WHERE id=?';
  330. $stm = $this->bd->prepare ($sql);
  331. foreach ($valuesTmp as $v) {
  332. $values[] = $v;
  333. }
  334. $values[] = $id;
  335. if ($stm && $stm->execute ($values)) {
  336. return $stm->rowCount();
  337. } else {
  338. $info = $stm->errorInfo();
  339. Minz_Log::record ('SQL error : ' . $info[2], Minz_Log::ERROR);
  340. return false;
  341. }
  342. }
  343. public function updateLastUpdate ($id, $inError = 0) {
  344. $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
  345. . 'SET f.cache_nbEntries=(SELECT COUNT(e1.id) FROM ' . $this->prefix . 'entry e1 WHERE e1.id_feed=f.id),'
  346. . 'f.cache_nbUnreads=(SELECT COUNT(e2.id) FROM ' . $this->prefix . 'entry e2 WHERE e2.id_feed=f.id AND e2.is_read=0),'
  347. . 'lastUpdate=?, error=? '
  348. . 'WHERE f.id=?';
  349. $stm = $this->bd->prepare ($sql);
  350. $values = array (
  351. time (),
  352. $inError,
  353. $id,
  354. );
  355. if ($stm && $stm->execute ($values)) {
  356. return $stm->rowCount();
  357. } else {
  358. $info = $stm->errorInfo();
  359. Minz_Log::record ('SQL error : ' . $info[2], Minz_Log::ERROR);
  360. return false;
  361. }
  362. }
  363. public function changeCategory ($idOldCat, $idNewCat) {
  364. $catDAO = new CategoryDAO ();
  365. $newCat = $catDAO->searchById ($idNewCat);
  366. if (!$newCat) {
  367. $newCat = $catDAO->getDefault ();
  368. }
  369. $sql = 'UPDATE ' . $this->prefix . 'feed SET category=? WHERE category=?';
  370. $stm = $this->bd->prepare ($sql);
  371. $values = array (
  372. $newCat->id (),
  373. $idOldCat
  374. );
  375. if ($stm && $stm->execute ($values)) {
  376. return $stm->rowCount();
  377. } else {
  378. $info = $stm->errorInfo();
  379. Minz_Log::record ('SQL error : ' . $info[2], Minz_Log::ERROR);
  380. return false;
  381. }
  382. }
  383. public function deleteFeed ($id) {
  384. $sql = 'DELETE FROM ' . $this->prefix . 'feed WHERE id=?';
  385. $stm = $this->bd->prepare ($sql);
  386. $values = array ($id);
  387. if ($stm && $stm->execute ($values)) {
  388. return $stm->rowCount();
  389. } else {
  390. $info = $stm->errorInfo();
  391. Minz_Log::record ('SQL error : ' . $info[2], Minz_Log::ERROR);
  392. return false;
  393. }
  394. }
  395. public function deleteFeedByCategory ($id) {
  396. $sql = 'DELETE FROM ' . $this->prefix . 'feed WHERE category=?';
  397. $stm = $this->bd->prepare ($sql);
  398. $values = array ($id);
  399. if ($stm && $stm->execute ($values)) {
  400. return $stm->rowCount();
  401. } else {
  402. $info = $stm->errorInfo();
  403. Minz_Log::record ('SQL error : ' . $info[2], Minz_Log::ERROR);
  404. return false;
  405. }
  406. }
  407. public function searchById ($id) {
  408. $sql = 'SELECT * FROM ' . $this->prefix . 'feed WHERE id=?';
  409. $stm = $this->bd->prepare ($sql);
  410. $values = array ($id);
  411. $stm->execute ($values);
  412. $res = $stm->fetchAll (PDO::FETCH_ASSOC);
  413. $feed = HelperFeed::daoToFeed ($res);
  414. if (isset ($feed[$id])) {
  415. return $feed[$id];
  416. } else {
  417. return false;
  418. }
  419. }
  420. public function searchByUrl ($url) {
  421. $sql = 'SELECT * FROM ' . $this->prefix . 'feed WHERE url=?';
  422. $stm = $this->bd->prepare ($sql);
  423. $values = array ($url);
  424. $stm->execute ($values);
  425. $res = $stm->fetchAll (PDO::FETCH_ASSOC);
  426. $feed = current (HelperFeed::daoToFeed ($res));
  427. if (isset ($feed)) {
  428. return $feed;
  429. } else {
  430. return false;
  431. }
  432. }
  433. public function listFeeds () {
  434. $sql = 'SELECT * FROM ' . $this->prefix . 'feed ORDER BY name';
  435. $stm = $this->bd->prepare ($sql);
  436. $stm->execute ();
  437. return HelperFeed::daoToFeed ($stm->fetchAll (PDO::FETCH_ASSOC));
  438. }
  439. public function listFeedsOrderUpdate () {
  440. $sql = 'SELECT * FROM ' . $this->prefix . 'feed ORDER BY lastUpdate';
  441. $stm = $this->bd->prepare ($sql);
  442. $stm->execute ();
  443. return HelperFeed::daoToFeed ($stm->fetchAll (PDO::FETCH_ASSOC));
  444. }
  445. public function listByCategory ($cat) {
  446. $sql = 'SELECT * FROM ' . $this->prefix . 'feed WHERE category=? ORDER BY name';
  447. $stm = $this->bd->prepare ($sql);
  448. $values = array ($cat);
  449. $stm->execute ($values);
  450. return HelperFeed::daoToFeed ($stm->fetchAll (PDO::FETCH_ASSOC));
  451. }
  452. public function countEntries ($id) {
  453. $sql = 'SELECT COUNT(*) AS count FROM ' . $this->prefix . 'entry WHERE id_feed=?';
  454. $stm = $this->bd->prepare ($sql);
  455. $values = array ($id);
  456. $stm->execute ($values);
  457. $res = $stm->fetchAll (PDO::FETCH_ASSOC);
  458. return $res[0]['count'];
  459. }
  460. public function countNotRead ($id) {
  461. $sql = 'SELECT COUNT(*) AS count FROM ' . $this->prefix . 'entry WHERE id_feed=? AND is_read=0';
  462. $stm = $this->bd->prepare ($sql);
  463. $values = array ($id);
  464. $stm->execute ($values);
  465. $res = $stm->fetchAll (PDO::FETCH_ASSOC);
  466. return $res[0]['count'];
  467. }
  468. public function updateCachedValues () { //For one single feed, call updateLastUpdate($id)
  469. $sql = 'UPDATE freshrss_feed f '
  470. . 'INNER JOIN ('
  471. . 'SELECT e.id_feed, '
  472. . 'COUNT(CASE WHEN e.is_read = 0 THEN 1 END) AS nbUnreads, '
  473. . 'COUNT(e.id) AS nbEntries '
  474. . 'FROM freshrss_entry e '
  475. . 'GROUP BY e.id_feed'
  476. . ') x ON x.id_feed=f.id '
  477. . 'SET f.cache_nbEntries=x.nbEntries, f.cache_nbUnreads=x.nbUnreads';
  478. $stm = $this->bd->prepare ($sql);
  479. $values = array ($feed_id);
  480. if ($stm && $stm->execute ($values)) {
  481. return $stm->rowCount();
  482. } else {
  483. $info = $stm->errorInfo();
  484. Minz_Log::record ('SQL error : ' . $info[2], Minz_Log::ERROR);
  485. return false;
  486. }
  487. }
  488. }
  489. class HelperFeed {
  490. public static function daoToFeed ($listDAO, $catID = null) {
  491. $list = array ();
  492. if (!is_array ($listDAO)) {
  493. $listDAO = array ($listDAO);
  494. }
  495. foreach ($listDAO as $key => $dao) {
  496. if (!isset ($dao['name'])) {
  497. continue;
  498. }
  499. if (isset ($dao['id'])) {
  500. $key = $dao['id'];
  501. }
  502. $myFeed = new Feed (isset($dao['url']) ? $dao['url'] : '', false);
  503. $myFeed->_category ($catID === null ? $dao['category'] : $catID);
  504. $myFeed->_name ($dao['name']);
  505. $myFeed->_website ($dao['website']);
  506. $myFeed->_description (isset($dao['description']) ? $dao['description'] : '');
  507. $myFeed->_lastUpdate (isset($dao['lastUpdate']) ? $dao['lastUpdate'] : 0);
  508. $myFeed->_priority ($dao['priority']);
  509. $myFeed->_pathEntries (isset($dao['pathEntries']) ? $dao['pathEntries'] : '');
  510. $myFeed->_httpAuth (isset($dao['httpAuth']) ? base64_decode ($dao['httpAuth']) : '');
  511. $myFeed->_error ($dao['error']);
  512. $myFeed->_keepHistory (isset($dao['keep_history']) ? $dao['keep_history'] : '');
  513. $myFeed->_nbNotRead ($dao['cache_nbUnreads']);
  514. $myFeed->_nbEntries ($dao['cache_nbEntries']);
  515. if (isset ($dao['id'])) {
  516. $myFeed->_id ($dao['id']);
  517. }
  518. $list[$key] = $myFeed;
  519. }
  520. return $list;
  521. }
  522. }