Feed.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530
  1. <?php
  2. class Feed extends Model {
  3. private $id = null;
  4. private $url;
  5. private $category = '000000';
  6. private $nbNotRead = -1;
  7. private $entries = null;
  8. private $name = '';
  9. private $website = '';
  10. private $description = '';
  11. private $lastUpdate = 0;
  12. private $priority = 10;
  13. private $pathEntries = '';
  14. private $httpAuth = '';
  15. private $error = false;
  16. private $keep_history = false;
  17. public function __construct ($url) {
  18. $this->_url ($url);
  19. }
  20. public function id () {
  21. if(is_null($this->id)) {
  22. return small_hash ($this->url . Configuration::selApplication ());
  23. } else {
  24. return $this->id;
  25. }
  26. }
  27. public function url () {
  28. return $this->url;
  29. }
  30. public function category () {
  31. return $this->category;
  32. }
  33. public function entries () {
  34. if (!is_null ($this->entries)) {
  35. return $this->entries;
  36. } else {
  37. return array ();
  38. }
  39. }
  40. public function name () {
  41. return $this->name;
  42. }
  43. public function website () {
  44. return $this->website;
  45. }
  46. public function description () {
  47. return $this->description;
  48. }
  49. public function lastUpdate () {
  50. return $this->lastUpdate;
  51. }
  52. public function priority () {
  53. return $this->priority;
  54. }
  55. public function pathEntries () {
  56. return $this->pathEntries;
  57. }
  58. public function httpAuth ($raw = true) {
  59. if ($raw) {
  60. return $this->httpAuth;
  61. } else {
  62. $pos_colon = strpos ($this->httpAuth, ':');
  63. $user = substr ($this->httpAuth, 0, $pos_colon);
  64. $pass = substr ($this->httpAuth, $pos_colon + 1);
  65. return array (
  66. 'username' => $user,
  67. 'password' => $pass
  68. );
  69. }
  70. }
  71. public function inError () {
  72. return $this->error;
  73. }
  74. public function keepHistory () {
  75. return $this->keep_history;
  76. }
  77. public function nbEntries () {
  78. $feedDAO = new FeedDAO ();
  79. return $feedDAO->countEntries ($this->id ());
  80. }
  81. public function nbNotRead () {
  82. if ($this->nbNotRead < 0) {
  83. $feedDAO = new FeedDAO ();
  84. $this->nbNotRead = $feedDAO->countNotRead ($this->id ());
  85. }
  86. return $this->nbNotRead;
  87. }
  88. public function favicon () {
  89. $file = '/data/favicons/' . $this->id () . '.ico';
  90. $favicon_url = Url::display ($file);
  91. if (!file_exists (PUBLIC_PATH . $file)) {
  92. $favicon_url = dowload_favicon ($this->website (), $this->id ());
  93. }
  94. return $favicon_url;
  95. }
  96. public function _id ($value) {
  97. $this->id = $value;
  98. }
  99. public function _url ($value) {
  100. if (!is_null ($value) && !preg_match ('#^https?://#', $value)) {
  101. $value = 'http://' . $value;
  102. }
  103. if (!is_null ($value) && filter_var ($value, FILTER_VALIDATE_URL)) {
  104. $this->url = $value;
  105. } else {
  106. throw new BadUrlException ($value);
  107. }
  108. }
  109. public function _category ($value) {
  110. $this->category = $value;
  111. }
  112. public function _name ($value) {
  113. if (is_null ($value)) {
  114. $value = '';
  115. }
  116. $this->name = $value;
  117. }
  118. public function _website ($value) {
  119. if (is_null ($value)) {
  120. $value = '';
  121. }
  122. $this->website = $value;
  123. }
  124. public function _description ($value) {
  125. if (is_null ($value)) {
  126. $value = '';
  127. }
  128. $this->description = $value;
  129. }
  130. public function _lastUpdate ($value) {
  131. $this->lastUpdate = $value;
  132. }
  133. public function _priority ($value) {
  134. if (!is_int (intval ($value))) {
  135. $value = 10;
  136. }
  137. $this->priority = $value;
  138. }
  139. public function _pathEntries ($value) {
  140. $this->pathEntries = $value;
  141. }
  142. public function _httpAuth ($value) {
  143. $this->httpAuth = $value;
  144. }
  145. public function _error ($value) {
  146. if ($value) {
  147. $value = true;
  148. } else {
  149. $value = false;
  150. }
  151. $this->error = $value;
  152. }
  153. public function _keepHistory ($value) {
  154. if ($value) {
  155. $value = true;
  156. } else {
  157. $value = false;
  158. }
  159. $this->keep_history = $value;
  160. }
  161. public function _nbNotRead ($value) { //Alex
  162. if (!is_int (intval ($value))) {
  163. $value = -1;
  164. }
  165. $this->nbNotRead = $value;
  166. }
  167. public function load () {
  168. if (!is_null ($this->url)) {
  169. if (CACHE_PATH === false) {
  170. throw new FileNotExistException (
  171. 'CACHE_PATH',
  172. MinzException::ERROR
  173. );
  174. } else {
  175. $feed = new SimplePie ();
  176. $url = preg_replace ('/&amp;/', '&', $this->url);
  177. if ($this->httpAuth != '') {
  178. $url = preg_replace ('#((.+)://)(.+)#', '${1}' . $this->httpAuth . '@${3}', $url);
  179. }
  180. $feed->set_feed_url ($url);
  181. $feed->set_cache_location (CACHE_PATH);
  182. $feed->strip_htmltags (array (
  183. 'base', 'blink', 'body', 'doctype',
  184. 'font', 'form', 'frame', 'frameset', 'html',
  185. 'input', 'marquee', 'meta', 'noscript',
  186. 'param', 'script', 'style'
  187. ));
  188. $feed->init ();
  189. if ($feed->error ()) {
  190. throw new FeedException ($feed->error);
  191. }
  192. // si on a utilisé l'auto-discover, notre url va avoir changé
  193. $subscribe_url = $feed->subscribe_url ();
  194. if (!is_null ($subscribe_url) && $subscribe_url != $this->url) {
  195. if ($this->httpAuth != '') {
  196. // on enlève les id si authentification HTTP
  197. $subscribe_url = preg_replace ('#((.+)://)((.+)@)(.+)#', '${1}${5}', $subscribe_url);
  198. }
  199. $this->_url ($subscribe_url);
  200. }
  201. $title = $feed->get_title ();
  202. $this->_name (!is_null ($title) ? $title : $this->url);
  203. $this->_website ($feed->get_link ());
  204. $this->_description ($feed->get_description ());
  205. // et on charge les articles du flux
  206. $this->loadEntries ($feed);
  207. }
  208. }
  209. }
  210. private function loadEntries ($feed) {
  211. $entries = array ();
  212. foreach ($feed->get_items () as $item) {
  213. $title = $item->get_title ();
  214. $title = preg_replace('#<a(.+)>(.+)</a>#', '\\2', $title);
  215. $title = htmlentities($title);
  216. $author = $item->get_author ();
  217. $link = $item->get_permalink ();
  218. $date = strtotime ($item->get_date ());
  219. // gestion des tags (catégorie == tag)
  220. $tags_tmp = $item->get_categories ();
  221. $tags = array ();
  222. if (!is_null ($tags_tmp)) {
  223. foreach ($tags_tmp as $tag) {
  224. $tags[] = $tag->get_label ();
  225. }
  226. }
  227. $content = $item->get_content ();
  228. $entry = new Entry (
  229. $this->id (),
  230. $item->get_id (),
  231. !is_null ($title) ? $title : '',
  232. !is_null ($author) ? $author->name : '',
  233. !is_null ($content) ? $content : '',
  234. !is_null ($link) ? $link : '',
  235. $date ? $date : time ()
  236. );
  237. $entry->_tags ($tags);
  238. // permet de récupérer le contenu des flux tronqués
  239. $entry->loadCompleteContent($this->pathEntries());
  240. $entries[$entry->id ()] = $entry;
  241. }
  242. $this->entries = $entries;
  243. }
  244. }
  245. class FeedDAO extends Model_pdo {
  246. public function addFeed ($valuesTmp) {
  247. $sql = 'INSERT INTO ' . $this->prefix . 'feed (id, url, category, name, website, description, lastUpdate, priority, httpAuth, error, keep_history) VALUES(?, ?, ?, ?, ?, ?, ?, 10, ?, 0, 0)';
  248. $stm = $this->bd->prepare ($sql);
  249. $values = array (
  250. $valuesTmp['id'],
  251. $valuesTmp['url'],
  252. $valuesTmp['category'],
  253. $valuesTmp['name'],
  254. $valuesTmp['website'],
  255. $valuesTmp['description'],
  256. $valuesTmp['lastUpdate'],
  257. base64_encode ($valuesTmp['httpAuth']),
  258. );
  259. if ($stm && $stm->execute ($values)) {
  260. return true;
  261. } else {
  262. $info = $stm->errorInfo();
  263. Minz_Log::record ('SQL error : ' . $info[2], Minz_Log::ERROR);
  264. return false;
  265. }
  266. }
  267. public function updateFeed ($id, $valuesTmp) {
  268. $set = '';
  269. foreach ($valuesTmp as $key => $v) {
  270. $set .= $key . '=?, ';
  271. if ($key == 'httpAuth') {
  272. $valuesTmp[$key] = base64_encode ($v);
  273. }
  274. }
  275. $set = substr ($set, 0, -2);
  276. $sql = 'UPDATE ' . $this->prefix . 'feed SET ' . $set . ' WHERE id=?';
  277. $stm = $this->bd->prepare ($sql);
  278. foreach ($valuesTmp as $v) {
  279. $values[] = $v;
  280. }
  281. $values[] = $id;
  282. if ($stm && $stm->execute ($values)) {
  283. return true;
  284. } else {
  285. $info = $stm->errorInfo();
  286. Minz_Log::record ('SQL error : ' . $info[2], Minz_Log::ERROR);
  287. return false;
  288. }
  289. }
  290. public function updateLastUpdate ($id) {
  291. $sql = 'UPDATE ' . $this->prefix . 'feed SET lastUpdate=?, error=0 WHERE id=?';
  292. $stm = $this->bd->prepare ($sql);
  293. $values = array (
  294. time (),
  295. $id
  296. );
  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 isInError ($id) {
  306. $sql = 'UPDATE ' . $this->prefix . 'feed SET error=1 WHERE id=?';
  307. $stm = $this->bd->prepare ($sql);
  308. $values = array (
  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 changeCategory ($idOldCat, $idNewCat) {
  320. $catDAO = new CategoryDAO ();
  321. $newCat = $catDAO->searchById ($idNewCat);
  322. if (!$newCat) {
  323. $newCat = $catDAO->getDefault ();
  324. }
  325. $sql = 'UPDATE ' . $this->prefix . 'feed SET category=? WHERE category=?';
  326. $stm = $this->bd->prepare ($sql);
  327. $values = array (
  328. $newCat->id (),
  329. $idOldCat
  330. );
  331. if ($stm && $stm->execute ($values)) {
  332. return true;
  333. } else {
  334. $info = $stm->errorInfo();
  335. Minz_Log::record ('SQL error : ' . $info[2], Minz_Log::ERROR);
  336. return false;
  337. }
  338. }
  339. public function deleteFeed ($id) {
  340. $sql = 'DELETE FROM ' . $this->prefix . 'feed WHERE id=?';
  341. $stm = $this->bd->prepare ($sql);
  342. $values = array ($id);
  343. if ($stm && $stm->execute ($values)) {
  344. return true;
  345. } else {
  346. $info = $stm->errorInfo();
  347. Minz_Log::record ('SQL error : ' . $info[2], Minz_Log::ERROR);
  348. return false;
  349. }
  350. }
  351. public function deleteFeedByCategory ($id) {
  352. $sql = 'DELETE FROM ' . $this->prefix . 'feed WHERE category=?';
  353. $stm = $this->bd->prepare ($sql);
  354. $values = array ($id);
  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 searchById ($id) {
  364. $sql = 'SELECT * FROM ' . $this->prefix . 'feed WHERE id=?';
  365. $stm = $this->bd->prepare ($sql);
  366. $values = array ($id);
  367. $stm->execute ($values);
  368. $res = $stm->fetchAll (PDO::FETCH_ASSOC);
  369. $feed = HelperFeed::daoToFeed ($res);
  370. if (isset ($feed[$id])) {
  371. return $feed[$id];
  372. } else {
  373. return false;
  374. }
  375. }
  376. public function searchByUrl ($url) {
  377. $sql = 'SELECT * FROM ' . $this->prefix . 'feed WHERE url=?';
  378. $stm = $this->bd->prepare ($sql);
  379. $values = array ($url);
  380. $stm->execute ($values);
  381. $res = $stm->fetchAll (PDO::FETCH_ASSOC);
  382. $feed = current (HelperFeed::daoToFeed ($res));
  383. if (isset ($feed)) {
  384. return $feed;
  385. } else {
  386. return false;
  387. }
  388. }
  389. public function listFeeds () {
  390. $sql = 'SELECT * FROM ' . $this->prefix . 'feed ORDER BY name';
  391. $stm = $this->bd->prepare ($sql);
  392. $stm->execute ();
  393. return HelperFeed::daoToFeed ($stm->fetchAll (PDO::FETCH_ASSOC));
  394. }
  395. public function listFeedsOrderUpdate () {
  396. $sql = 'SELECT * FROM ' . $this->prefix . 'feed ORDER BY lastUpdate';
  397. $stm = $this->bd->prepare ($sql);
  398. $stm->execute ();
  399. return HelperFeed::daoToFeed ($stm->fetchAll (PDO::FETCH_ASSOC));
  400. }
  401. public function listByCategory ($cat) {
  402. $sql = 'SELECT * FROM ' . $this->prefix . 'feed WHERE category=? ORDER BY name';
  403. $stm = $this->bd->prepare ($sql);
  404. $values = array ($cat);
  405. $stm->execute ($values);
  406. return HelperFeed::daoToFeed ($stm->fetchAll (PDO::FETCH_ASSOC));
  407. }
  408. public function count () {
  409. $sql = 'SELECT COUNT(*) AS count FROM ' . $this->prefix . 'feed';
  410. $stm = $this->bd->prepare ($sql);
  411. $stm->execute ();
  412. $res = $stm->fetchAll (PDO::FETCH_ASSOC);
  413. return $res[0]['count'];
  414. }
  415. public function countEntries ($id) {
  416. $sql = 'SELECT COUNT(*) AS count FROM ' . $this->prefix . 'entry WHERE id_feed=?';
  417. $stm = $this->bd->prepare ($sql);
  418. $values = array ($id);
  419. $stm->execute ($values);
  420. $res = $stm->fetchAll (PDO::FETCH_ASSOC);
  421. return $res[0]['count'];
  422. }
  423. public function countNotRead ($id) {
  424. $sql = 'SELECT COUNT(*) AS count FROM ' . $this->prefix . 'entry WHERE is_read=0 AND id_feed=?';
  425. $stm = $this->bd->prepare ($sql);
  426. $values = array ($id);
  427. $stm->execute ($values);
  428. $res = $stm->fetchAll (PDO::FETCH_ASSOC);
  429. return $res[0]['count'];
  430. }
  431. }
  432. class HelperFeed {
  433. public static function daoToFeed ($listDAO) {
  434. $list = array ();
  435. if (!is_array ($listDAO)) {
  436. $listDAO = array ($listDAO);
  437. }
  438. foreach ($listDAO as $key => $dao) {
  439. if (isset ($dao['id'])) {
  440. $key = $dao['id'];
  441. }
  442. $list[$key] = new Feed ($dao['url']);
  443. $list[$key]->_category ($dao['category']);
  444. $list[$key]->_name ($dao['name']);
  445. $list[$key]->_website ($dao['website']);
  446. $list[$key]->_description ($dao['description']);
  447. $list[$key]->_lastUpdate ($dao['lastUpdate']);
  448. $list[$key]->_priority ($dao['priority']);
  449. $list[$key]->_pathEntries ($dao['pathEntries']);
  450. $list[$key]->_httpAuth (base64_decode ($dao['httpAuth']));
  451. $list[$key]->_error ($dao['error']);
  452. $list[$key]->_keepHistory ($dao['keep_history']);
  453. if (isset ($dao['nbNotRead'])) {
  454. $list[$key]->_nbNotRead ($dao['nbNotRead']);
  455. }
  456. if (isset ($dao['id'])) {
  457. $list[$key]->_id ($dao['id']);
  458. }
  459. }
  460. return $list;
  461. }
  462. }