Feed.php 14 KB

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