Feed.php 16 KB

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