Feed.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576
  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->init ();
  204. if ($feed->error ()) {
  205. throw new FeedException ($feed->error . ' [' . $url . ']');
  206. }
  207. // si on a utilisé l'auto-discover, notre url va avoir changé
  208. $subscribe_url = $feed->subscribe_url ();
  209. if (!is_null ($subscribe_url) && $subscribe_url != $this->url) {
  210. if ($this->httpAuth != '') {
  211. // on enlève les id si authentification HTTP
  212. $subscribe_url = preg_replace ('#((.+)://)((.+)@)(.+)#', '${1}${5}', $subscribe_url);
  213. }
  214. $this->_url ($subscribe_url);
  215. }
  216. if (empty($this->name)) { // May come from OPML
  217. $title = $feed->get_title ();
  218. $this->_name (!is_null ($title) ? $title : $this->url);
  219. }
  220. $this->_website ($feed->get_link ());
  221. $this->_description ($feed->get_description ());
  222. // et on charge les articles du flux
  223. $this->loadEntries ($feed);
  224. }
  225. }
  226. }
  227. static function html_only_entity_decode($text) {
  228. static $htmlEntitiesOnly = null;
  229. if ($htmlEntitiesOnly === null) {
  230. $htmlEntitiesOnly = array_flip(array_diff(
  231. get_html_translation_table(HTML_ENTITIES, ENT_NOQUOTES, 'UTF-8'), //Decode HTML entities
  232. get_html_translation_table(HTML_SPECIALCHARS, ENT_NOQUOTES, 'UTF-8') //Preserve XML entities
  233. ));
  234. }
  235. return strtr($text, $htmlEntitiesOnly);
  236. }
  237. private function loadEntries ($feed) {
  238. $entries = array ();
  239. foreach ($feed->get_items () as $item) {
  240. $title = self::html_only_entity_decode (strip_tags ($item->get_title ()));
  241. $author = self::html_only_entity_decode ($item->get_author ());
  242. $link = $item->get_permalink ();
  243. $date = strtotime ($item->get_date ());
  244. // gestion des tags (catégorie == tag)
  245. $tags_tmp = $item->get_categories ();
  246. $tags = array ();
  247. if (!is_null ($tags_tmp)) {
  248. foreach ($tags_tmp as $tag) {
  249. $tags[] = self::html_only_entity_decode ($tag->get_label ());
  250. }
  251. }
  252. $content = self::html_only_entity_decode ($item->get_content ());
  253. $elinks = array();
  254. foreach ($item->get_enclosures() as $enclosure) {
  255. $elink = $enclosure->get_link();
  256. if (array_key_exists($elink, $elinks)) continue;
  257. $elinks[$elink] = '1';
  258. $mime = strtolower($enclosure->get_type());
  259. if (strpos($mime, 'image/') === 0) {
  260. $content .= '<br /><img src="' . $elink . '" alt="" />';
  261. }
  262. }
  263. $entry = new Entry (
  264. $this->id (),
  265. $item->get_id (),
  266. !is_null ($title) ? $title : '',
  267. !is_null ($author) ? $author->name : '',
  268. !is_null ($content) ? $content : '',
  269. !is_null ($link) ? $link : '',
  270. $date ? $date : time ()
  271. );
  272. $entry->_tags ($tags);
  273. // permet de récupérer le contenu des flux tronqués
  274. $entry->loadCompleteContent($this->pathEntries());
  275. $entries[$entry->id ()] = $entry;
  276. }
  277. $this->entries = $entries;
  278. }
  279. }
  280. class FeedDAO extends Model_pdo {
  281. public function addFeed ($valuesTmp) {
  282. $sql = 'INSERT INTO ' . $this->prefix . 'feed (id, url, category, name, website, description, lastUpdate, priority, httpAuth, error, keep_history) VALUES(?, ?, ?, ?, ?, ?, ?, 10, ?, 0, 0)';
  283. $stm = $this->bd->prepare ($sql);
  284. $values = array (
  285. $valuesTmp['id'],
  286. substr($valuesTmp['url'], 0, 511),
  287. $valuesTmp['category'],
  288. substr($valuesTmp['name'], 0, 255),
  289. substr($valuesTmp['website'], 0, 255),
  290. substr($valuesTmp['description'], 0, 1023),
  291. $valuesTmp['lastUpdate'],
  292. base64_encode ($valuesTmp['httpAuth']),
  293. );
  294. if ($stm && $stm->execute ($values)) {
  295. return true;
  296. } else {
  297. $info = $stm->errorInfo();
  298. Minz_Log::record ('SQL error : ' . $info[2], Minz_Log::ERROR);
  299. return false;
  300. }
  301. }
  302. public function updateFeed ($id, $valuesTmp) {
  303. $set = '';
  304. foreach ($valuesTmp as $key => $v) {
  305. $set .= $key . '=?, ';
  306. if ($key == 'httpAuth') {
  307. $valuesTmp[$key] = base64_encode ($v);
  308. }
  309. }
  310. $set = substr ($set, 0, -2);
  311. $sql = 'UPDATE ' . $this->prefix . 'feed SET ' . $set . ' WHERE id=?';
  312. $stm = $this->bd->prepare ($sql);
  313. foreach ($valuesTmp as $v) {
  314. $values[] = $v;
  315. }
  316. $values[] = $id;
  317. if ($stm && $stm->execute ($values)) {
  318. return true;
  319. } else {
  320. $info = $stm->errorInfo();
  321. Minz_Log::record ('SQL error : ' . $info[2], Minz_Log::ERROR);
  322. return false;
  323. }
  324. }
  325. public function updateLastUpdate ($id) {
  326. $sql = 'UPDATE ' . $this->prefix . 'feed SET lastUpdate=?, error=0 WHERE id=?';
  327. $stm = $this->bd->prepare ($sql);
  328. $values = array (
  329. time (),
  330. $id
  331. );
  332. if ($stm && $stm->execute ($values)) {
  333. return true;
  334. } else {
  335. $info = $stm->errorInfo();
  336. Minz_Log::record ('SQL error : ' . $info[2], Minz_Log::ERROR);
  337. return false;
  338. }
  339. }
  340. public function isInError ($id) {
  341. $sql = 'UPDATE ' . $this->prefix . 'feed SET error=1 WHERE id=?';
  342. $stm = $this->bd->prepare ($sql);
  343. $values = array (
  344. $id
  345. );
  346. if ($stm && $stm->execute ($values)) {
  347. return true;
  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 true;
  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. $sql = 'DELETE FROM ' . $this->prefix . 'feed WHERE id=?';
  376. $stm = $this->bd->prepare ($sql);
  377. $values = array ($id);
  378. if ($stm && $stm->execute ($values)) {
  379. return true;
  380. } else {
  381. $info = $stm->errorInfo();
  382. Minz_Log::record ('SQL error : ' . $info[2], Minz_Log::ERROR);
  383. return false;
  384. }
  385. }
  386. public function deleteFeedByCategory ($id) {
  387. $sql = 'DELETE FROM ' . $this->prefix . 'feed WHERE category=?';
  388. $stm = $this->bd->prepare ($sql);
  389. $values = array ($id);
  390. if ($stm && $stm->execute ($values)) {
  391. return true;
  392. } else {
  393. $info = $stm->errorInfo();
  394. Minz_Log::record ('SQL error : ' . $info[2], Minz_Log::ERROR);
  395. return false;
  396. }
  397. }
  398. public function searchById ($id) {
  399. $sql = 'SELECT * FROM ' . $this->prefix . 'feed WHERE id=?';
  400. $stm = $this->bd->prepare ($sql);
  401. $values = array ($id);
  402. $stm->execute ($values);
  403. $res = $stm->fetchAll (PDO::FETCH_ASSOC);
  404. $feed = HelperFeed::daoToFeed ($res);
  405. if (isset ($feed[$id])) {
  406. return $feed[$id];
  407. } else {
  408. return false;
  409. }
  410. }
  411. public function searchByUrl ($url) {
  412. $sql = 'SELECT * FROM ' . $this->prefix . 'feed WHERE url=?';
  413. $stm = $this->bd->prepare ($sql);
  414. $values = array ($url);
  415. $stm->execute ($values);
  416. $res = $stm->fetchAll (PDO::FETCH_ASSOC);
  417. $feed = current (HelperFeed::daoToFeed ($res));
  418. if (isset ($feed)) {
  419. return $feed;
  420. } else {
  421. return false;
  422. }
  423. }
  424. public function listFeeds () {
  425. $sql = 'SELECT * FROM ' . $this->prefix . 'feed ORDER BY name';
  426. $stm = $this->bd->prepare ($sql);
  427. $stm->execute ();
  428. return HelperFeed::daoToFeed ($stm->fetchAll (PDO::FETCH_ASSOC));
  429. }
  430. public function listFeedsOrderUpdate () {
  431. $sql = 'SELECT * FROM ' . $this->prefix . 'feed ORDER BY lastUpdate';
  432. $stm = $this->bd->prepare ($sql);
  433. $stm->execute ();
  434. return HelperFeed::daoToFeed ($stm->fetchAll (PDO::FETCH_ASSOC));
  435. }
  436. public function listByCategory ($cat) {
  437. $sql = 'SELECT * FROM ' . $this->prefix . 'feed WHERE category=? ORDER BY name';
  438. $stm = $this->bd->prepare ($sql);
  439. $values = array ($cat);
  440. $stm->execute ($values);
  441. return HelperFeed::daoToFeed ($stm->fetchAll (PDO::FETCH_ASSOC));
  442. }
  443. public function count () { //Is this used?
  444. $sql = 'SELECT COUNT(*) AS count FROM ' . $this->prefix . 'feed';
  445. $stm = $this->bd->prepare ($sql);
  446. $stm->execute ();
  447. $res = $stm->fetchAll (PDO::FETCH_ASSOC);
  448. return $res[0]['count'];
  449. }
  450. public function countEntries ($id) {
  451. $sql = 'SELECT COUNT(*) AS count FROM ' . $this->prefix . 'entry WHERE id_feed=?';
  452. $stm = $this->bd->prepare ($sql);
  453. $values = array ($id);
  454. $stm->execute ($values);
  455. $res = $stm->fetchAll (PDO::FETCH_ASSOC);
  456. return $res[0]['count'];
  457. }
  458. public function countNotRead ($id) { //Is this used?
  459. $sql = 'SELECT COUNT(*) AS count FROM ' . $this->prefix . 'entry WHERE is_read=0 AND id_feed=?';
  460. $stm = $this->bd->prepare ($sql);
  461. $values = array ($id);
  462. $stm->execute ($values);
  463. $res = $stm->fetchAll (PDO::FETCH_ASSOC);
  464. return $res[0]['count'];
  465. }
  466. }
  467. class HelperFeed {
  468. public static function daoToFeed ($listDAO, $catID = null) {
  469. $list = array ();
  470. if (!is_array ($listDAO)) {
  471. $listDAO = array ($listDAO);
  472. }
  473. foreach ($listDAO as $key => $dao) {
  474. if (!isset ($dao['name'])) {
  475. continue;
  476. }
  477. if (isset ($dao['id'])) {
  478. $key = $dao['id'];
  479. }
  480. $myFeed = new Feed (isset($dao['url']) ? $dao['url'] : '', false);
  481. $myFeed->_category ($catID === null ? $dao['category'] : $catID);
  482. $myFeed->_name ($dao['name']);
  483. $myFeed->_website ($dao['website']);
  484. $myFeed->_description (isset($dao['description']) ? $dao['description'] : '');
  485. $myFeed->_lastUpdate (isset($dao['lastUpdate']) ? $dao['lastUpdate'] : 0);
  486. $myFeed->_priority ($dao['priority']);
  487. $myFeed->_pathEntries (isset($dao['pathEntries']) ? $dao['pathEntries'] : '');
  488. $myFeed->_httpAuth (isset($dao['httpAuth']) ? base64_decode ($dao['httpAuth']) : '');
  489. $myFeed->_error ($dao['error']);
  490. $myFeed->_keepHistory (isset($dao['keep_history']) ? $dao['keep_history'] : '');
  491. if (isset ($dao['nbNotRead'])) {
  492. $myFeed->_nbNotRead ($dao['nbNotRead']);
  493. }
  494. if (isset ($dao['nbEntries'])) {
  495. $myFeed->_nbEntries ($dao['nbEntries']);
  496. }
  497. if (isset ($dao['id'])) {
  498. $myFeed->_id ($dao['id']);
  499. }
  500. $list[$key] = $myFeed;
  501. }
  502. return $list;
  503. }
  504. }