Feed.php 16 KB

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