Feed.php 13 KB

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