4
0

Feed.php 16 KB

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