Feed.php 15 KB

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