Feed.php 12 KB

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