Feed.php 15 KB

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