Feed.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563
  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->strip_htmltags (array (
  193. 'base', 'blink', 'body', 'doctype',
  194. 'font', 'form', 'frame', 'frameset', 'html',
  195. 'input', 'marquee', 'meta', 'noscript',
  196. 'param', 'script', 'style'
  197. ));
  198. $feed->strip_attributes(array_merge($feed->strip_attributes, array(
  199. 'onload', 'onunload', 'onclick', 'ondblclick', 'onmousedown', 'onmouseup',
  200. 'onmouseover', 'onmousemove', 'onmouseout', 'onfocus', 'onblur',
  201. 'onkeypress', 'onkeydown', 'onkeyup', 'onselect', 'onchange')));
  202. $feed->init ();
  203. if ($feed->error ()) {
  204. throw new FeedException ($feed->error . ' [' . $url . ']');
  205. }
  206. // si on a utilisé l'auto-discover, notre url va avoir changé
  207. $subscribe_url = $feed->subscribe_url ();
  208. if (!is_null ($subscribe_url) && $subscribe_url != $this->url) {
  209. if ($this->httpAuth != '') {
  210. // on enlève les id si authentification HTTP
  211. $subscribe_url = preg_replace ('#((.+)://)((.+)@)(.+)#', '${1}${5}', $subscribe_url);
  212. }
  213. $this->_url ($subscribe_url);
  214. }
  215. if (empty($this->name)) { // May come from OPML
  216. $title = $feed->get_title ();
  217. $this->_name (!is_null ($title) ? $title : $this->url);
  218. }
  219. $this->_website ($feed->get_link ());
  220. $this->_description ($feed->get_description ());
  221. // et on charge les articles du flux
  222. $this->loadEntries ($feed);
  223. }
  224. }
  225. }
  226. private function loadEntries ($feed) {
  227. $entries = array ();
  228. foreach ($feed->get_items () as $item) {
  229. $title = strip_tags($item->get_title ());
  230. $author = $item->get_author ();
  231. $link = $item->get_permalink ();
  232. $date = strtotime ($item->get_date ());
  233. // gestion des tags (catégorie == tag)
  234. $tags_tmp = $item->get_categories ();
  235. $tags = array ();
  236. if (!is_null ($tags_tmp)) {
  237. foreach ($tags_tmp as $tag) {
  238. $tags[] = $tag->get_label ();
  239. }
  240. }
  241. $content = $item->get_content ();
  242. $elinks = array();
  243. foreach ($item->get_enclosures() as $enclosure) {
  244. $elink = $enclosure->get_link();
  245. if (array_key_exists($elink, $elinks)) continue;
  246. $elinks[$elink] = '1';
  247. $mime = strtolower($enclosure->get_type());
  248. if (strpos($mime, 'image/') === 0) {
  249. $content .= '<br /><img src="' . $elink . '" alt="" />';
  250. }
  251. }
  252. $entry = new Entry (
  253. $this->id (),
  254. $item->get_id (),
  255. !is_null ($title) ? $title : '',
  256. !is_null ($author) ? $author->name : '',
  257. !is_null ($content) ? $content : '',
  258. !is_null ($link) ? $link : '',
  259. $date ? $date : time ()
  260. );
  261. $entry->_tags ($tags);
  262. // permet de récupérer le contenu des flux tronqués
  263. $entry->loadCompleteContent($this->pathEntries());
  264. $entries[$entry->id ()] = $entry;
  265. }
  266. $this->entries = $entries;
  267. }
  268. }
  269. class FeedDAO extends Model_pdo {
  270. public function addFeed ($valuesTmp) {
  271. $sql = 'INSERT INTO ' . $this->prefix . 'feed (id, url, category, name, website, description, lastUpdate, priority, httpAuth, error, keep_history) VALUES(?, ?, ?, ?, ?, ?, ?, 10, ?, 0, 0)';
  272. $stm = $this->bd->prepare ($sql);
  273. $values = array (
  274. $valuesTmp['id'],
  275. $valuesTmp['url'],
  276. $valuesTmp['category'],
  277. $valuesTmp['name'],
  278. $valuesTmp['website'],
  279. $valuesTmp['description'],
  280. $valuesTmp['lastUpdate'],
  281. base64_encode ($valuesTmp['httpAuth']),
  282. );
  283. if ($stm && $stm->execute ($values)) {
  284. return true;
  285. } else {
  286. $info = $stm->errorInfo();
  287. Minz_Log::record ('SQL error : ' . $info[2], Minz_Log::ERROR);
  288. return false;
  289. }
  290. }
  291. public function updateFeed ($id, $valuesTmp) {
  292. $set = '';
  293. foreach ($valuesTmp as $key => $v) {
  294. $set .= $key . '=?, ';
  295. if ($key == 'httpAuth') {
  296. $valuesTmp[$key] = base64_encode ($v);
  297. }
  298. }
  299. $set = substr ($set, 0, -2);
  300. $sql = 'UPDATE ' . $this->prefix . 'feed SET ' . $set . ' WHERE id=?';
  301. $stm = $this->bd->prepare ($sql);
  302. foreach ($valuesTmp as $v) {
  303. $values[] = $v;
  304. }
  305. $values[] = $id;
  306. if ($stm && $stm->execute ($values)) {
  307. return true;
  308. } else {
  309. $info = $stm->errorInfo();
  310. Minz_Log::record ('SQL error : ' . $info[2], Minz_Log::ERROR);
  311. return false;
  312. }
  313. }
  314. public function updateLastUpdate ($id) {
  315. $sql = 'UPDATE ' . $this->prefix . 'feed SET lastUpdate=?, error=0 WHERE id=?';
  316. $stm = $this->bd->prepare ($sql);
  317. $values = array (
  318. time (),
  319. $id
  320. );
  321. if ($stm && $stm->execute ($values)) {
  322. return true;
  323. } else {
  324. $info = $stm->errorInfo();
  325. Minz_Log::record ('SQL error : ' . $info[2], Minz_Log::ERROR);
  326. return false;
  327. }
  328. }
  329. public function isInError ($id) {
  330. $sql = 'UPDATE ' . $this->prefix . 'feed SET error=1 WHERE id=?';
  331. $stm = $this->bd->prepare ($sql);
  332. $values = array (
  333. $id
  334. );
  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 changeCategory ($idOldCat, $idNewCat) {
  344. $catDAO = new CategoryDAO ();
  345. $newCat = $catDAO->searchById ($idNewCat);
  346. if (!$newCat) {
  347. $newCat = $catDAO->getDefault ();
  348. }
  349. $sql = 'UPDATE ' . $this->prefix . 'feed SET category=? WHERE category=?';
  350. $stm = $this->bd->prepare ($sql);
  351. $values = array (
  352. $newCat->id (),
  353. $idOldCat
  354. );
  355. if ($stm && $stm->execute ($values)) {
  356. return true;
  357. } else {
  358. $info = $stm->errorInfo();
  359. Minz_Log::record ('SQL error : ' . $info[2], Minz_Log::ERROR);
  360. return false;
  361. }
  362. }
  363. public function deleteFeed ($id) {
  364. $sql = 'DELETE FROM ' . $this->prefix . 'feed WHERE id=?';
  365. $stm = $this->bd->prepare ($sql);
  366. $values = array ($id);
  367. if ($stm && $stm->execute ($values)) {
  368. return true;
  369. } else {
  370. $info = $stm->errorInfo();
  371. Minz_Log::record ('SQL error : ' . $info[2], Minz_Log::ERROR);
  372. return false;
  373. }
  374. }
  375. public function deleteFeedByCategory ($id) {
  376. $sql = 'DELETE FROM ' . $this->prefix . 'feed WHERE category=?';
  377. $stm = $this->bd->prepare ($sql);
  378. $values = array ($id);
  379. if ($stm && $stm->execute ($values)) {
  380. return true;
  381. } else {
  382. $info = $stm->errorInfo();
  383. Minz_Log::record ('SQL error : ' . $info[2], Minz_Log::ERROR);
  384. return false;
  385. }
  386. }
  387. public function searchById ($id) {
  388. $sql = 'SELECT * FROM ' . $this->prefix . 'feed WHERE id=?';
  389. $stm = $this->bd->prepare ($sql);
  390. $values = array ($id);
  391. $stm->execute ($values);
  392. $res = $stm->fetchAll (PDO::FETCH_ASSOC);
  393. $feed = HelperFeed::daoToFeed ($res);
  394. if (isset ($feed[$id])) {
  395. return $feed[$id];
  396. } else {
  397. return false;
  398. }
  399. }
  400. public function searchByUrl ($url) {
  401. $sql = 'SELECT * FROM ' . $this->prefix . 'feed WHERE url=?';
  402. $stm = $this->bd->prepare ($sql);
  403. $values = array ($url);
  404. $stm->execute ($values);
  405. $res = $stm->fetchAll (PDO::FETCH_ASSOC);
  406. $feed = current (HelperFeed::daoToFeed ($res));
  407. if (isset ($feed)) {
  408. return $feed;
  409. } else {
  410. return false;
  411. }
  412. }
  413. public function listFeeds () {
  414. $sql = 'SELECT * FROM ' . $this->prefix . 'feed ORDER BY name';
  415. $stm = $this->bd->prepare ($sql);
  416. $stm->execute ();
  417. return HelperFeed::daoToFeed ($stm->fetchAll (PDO::FETCH_ASSOC));
  418. }
  419. public function listFeedsOrderUpdate () {
  420. $sql = 'SELECT * FROM ' . $this->prefix . 'feed ORDER BY lastUpdate';
  421. $stm = $this->bd->prepare ($sql);
  422. $stm->execute ();
  423. return HelperFeed::daoToFeed ($stm->fetchAll (PDO::FETCH_ASSOC));
  424. }
  425. public function listByCategory ($cat) {
  426. $sql = 'SELECT * FROM ' . $this->prefix . 'feed WHERE category=? ORDER BY name';
  427. $stm = $this->bd->prepare ($sql);
  428. $values = array ($cat);
  429. $stm->execute ($values);
  430. return HelperFeed::daoToFeed ($stm->fetchAll (PDO::FETCH_ASSOC));
  431. }
  432. public function count () { //Is this used?
  433. $sql = 'SELECT COUNT(*) AS count FROM ' . $this->prefix . 'feed';
  434. $stm = $this->bd->prepare ($sql);
  435. $stm->execute ();
  436. $res = $stm->fetchAll (PDO::FETCH_ASSOC);
  437. return $res[0]['count'];
  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) { //Is this used?
  448. $sql = 'SELECT COUNT(*) AS count FROM ' . $this->prefix . 'entry WHERE is_read=0 AND id_feed=?';
  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. }
  456. class HelperFeed {
  457. public static function daoToFeed ($listDAO) {
  458. $list = array ();
  459. if (!is_array ($listDAO)) {
  460. $listDAO = array ($listDAO);
  461. }
  462. foreach ($listDAO as $key => $dao) {
  463. if (empty ($dao['url'])) {
  464. continue;
  465. }
  466. if (isset ($dao['id'])) {
  467. $key = $dao['id'];
  468. }
  469. $list[$key] = new Feed ($dao['url'], false);
  470. $list[$key]->_category ($dao['category']);
  471. $list[$key]->_name ($dao['name']);
  472. $list[$key]->_website ($dao['website']);
  473. $list[$key]->_description ($dao['description']);
  474. $list[$key]->_lastUpdate ($dao['lastUpdate']);
  475. $list[$key]->_priority ($dao['priority']);
  476. $list[$key]->_pathEntries ($dao['pathEntries']);
  477. $list[$key]->_httpAuth (base64_decode ($dao['httpAuth']));
  478. $list[$key]->_error ($dao['error']);
  479. $list[$key]->_keepHistory ($dao['keep_history']);
  480. if (isset ($dao['nbNotRead'])) {
  481. $list[$key]->_nbNotRead ($dao['nbNotRead']);
  482. }
  483. if (isset ($dao['nbEntries'])) {
  484. $list[$key]->_nbEntries ($dao['nbEntries']);
  485. }
  486. if (isset ($dao['id'])) {
  487. $list[$key]->_id ($dao['id']);
  488. }
  489. }
  490. return $list;
  491. }
  492. }