Feed.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  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 = strip_tags($item->get_title ());
  204. $author = $item->get_author ();
  205. $link = $item->get_permalink ();
  206. $date = strtotime ($item->get_date ());
  207. // gestion des tags (catégorie == tag)
  208. $tags_tmp = $item->get_categories ();
  209. $tags = array ();
  210. if (!is_null ($tags_tmp)) {
  211. foreach ($tags_tmp as $tag) {
  212. $tags[] = $tag->get_label ();
  213. }
  214. }
  215. $content = $item->get_content ();
  216. $entry = new Entry (
  217. $this->id (),
  218. $item->get_id (),
  219. !is_null ($title) ? $title : '',
  220. !is_null ($author) ? $author->name : '',
  221. !is_null ($content) ? $content : '',
  222. !is_null ($link) ? $link : '',
  223. $date ? $date : time ()
  224. );
  225. $entry->_tags ($tags);
  226. // permet de récupérer le contenu des flux tronqués
  227. $entry->loadCompleteContent($this->pathEntries());
  228. $entries[$entry->id ()] = $entry;
  229. }
  230. $this->entries = $entries;
  231. }
  232. }
  233. class FeedDAO extends Model_pdo {
  234. public function addFeed ($valuesTmp) {
  235. $sql = 'INSERT INTO ' . $this->prefix . 'feed (id, url, category, name, website, description, lastUpdate, priority, httpAuth, error, keep_history) VALUES(?, ?, ?, ?, ?, ?, ?, 10, ?, 0, 0)';
  236. $stm = $this->bd->prepare ($sql);
  237. $values = array (
  238. $valuesTmp['id'],
  239. $valuesTmp['url'],
  240. $valuesTmp['category'],
  241. $valuesTmp['name'],
  242. $valuesTmp['website'],
  243. $valuesTmp['description'],
  244. $valuesTmp['lastUpdate'],
  245. base64_encode ($valuesTmp['httpAuth']),
  246. );
  247. if ($stm && $stm->execute ($values)) {
  248. return true;
  249. } else {
  250. $info = $stm->errorInfo();
  251. Minz_Log::record ('SQL error : ' . $info[2], Minz_Log::ERROR);
  252. return false;
  253. }
  254. }
  255. public function updateFeed ($id, $valuesTmp) {
  256. $set = '';
  257. foreach ($valuesTmp as $key => $v) {
  258. $set .= $key . '=?, ';
  259. if ($key == 'httpAuth') {
  260. $valuesTmp[$key] = base64_encode ($v);
  261. }
  262. }
  263. $set = substr ($set, 0, -2);
  264. $sql = 'UPDATE ' . $this->prefix . 'feed SET ' . $set . ' WHERE id=?';
  265. $stm = $this->bd->prepare ($sql);
  266. foreach ($valuesTmp as $v) {
  267. $values[] = $v;
  268. }
  269. $values[] = $id;
  270. if ($stm && $stm->execute ($values)) {
  271. return true;
  272. } else {
  273. $info = $stm->errorInfo();
  274. Minz_Log::record ('SQL error : ' . $info[2], Minz_Log::ERROR);
  275. return false;
  276. }
  277. }
  278. public function updateLastUpdate ($id) {
  279. $sql = 'UPDATE ' . $this->prefix . 'feed SET lastUpdate=?, error=0 WHERE id=?';
  280. $stm = $this->bd->prepare ($sql);
  281. $values = array (
  282. time (),
  283. $id
  284. );
  285. if ($stm && $stm->execute ($values)) {
  286. return true;
  287. } else {
  288. $info = $stm->errorInfo();
  289. Minz_Log::record ('SQL error : ' . $info[2], Minz_Log::ERROR);
  290. return false;
  291. }
  292. }
  293. public function isInError ($id) {
  294. $sql = 'UPDATE ' . $this->prefix . 'feed SET error=1 WHERE id=?';
  295. $stm = $this->bd->prepare ($sql);
  296. $values = array (
  297. $id
  298. );
  299. if ($stm && $stm->execute ($values)) {
  300. return true;
  301. } else {
  302. $info = $stm->errorInfo();
  303. Minz_Log::record ('SQL error : ' . $info[2], Minz_Log::ERROR);
  304. return false;
  305. }
  306. }
  307. public function changeCategory ($idOldCat, $idNewCat) {
  308. $catDAO = new CategoryDAO ();
  309. $newCat = $catDAO->searchById ($idNewCat);
  310. if (!$newCat) {
  311. $newCat = $catDAO->getDefault ();
  312. }
  313. $sql = 'UPDATE ' . $this->prefix . 'feed SET category=? WHERE category=?';
  314. $stm = $this->bd->prepare ($sql);
  315. $values = array (
  316. $newCat->id (),
  317. $idOldCat
  318. );
  319. if ($stm && $stm->execute ($values)) {
  320. return true;
  321. } else {
  322. $info = $stm->errorInfo();
  323. Minz_Log::record ('SQL error : ' . $info[2], Minz_Log::ERROR);
  324. return false;
  325. }
  326. }
  327. public function deleteFeed ($id) {
  328. $sql = 'DELETE FROM ' . $this->prefix . 'feed WHERE id=?';
  329. $stm = $this->bd->prepare ($sql);
  330. $values = array ($id);
  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 deleteFeedByCategory ($id) {
  340. $sql = 'DELETE FROM ' . $this->prefix . 'feed WHERE category=?';
  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 searchById ($id) {
  352. $sql = 'SELECT * FROM ' . $this->prefix . 'feed WHERE id=?';
  353. $stm = $this->bd->prepare ($sql);
  354. $values = array ($id);
  355. $stm->execute ($values);
  356. $res = $stm->fetchAll (PDO::FETCH_ASSOC);
  357. $feed = HelperFeed::daoToFeed ($res);
  358. if (isset ($feed[$id])) {
  359. return $feed[$id];
  360. } else {
  361. return false;
  362. }
  363. }
  364. public function searchByUrl ($url) {
  365. $sql = 'SELECT * FROM ' . $this->prefix . 'feed WHERE url=?';
  366. $stm = $this->bd->prepare ($sql);
  367. $values = array ($url);
  368. $stm->execute ($values);
  369. $res = $stm->fetchAll (PDO::FETCH_ASSOC);
  370. $feed = current (HelperFeed::daoToFeed ($res));
  371. if (isset ($feed)) {
  372. return $feed;
  373. } else {
  374. return false;
  375. }
  376. }
  377. public function listFeeds () {
  378. $sql = 'SELECT * FROM ' . $this->prefix . 'feed ORDER BY name';
  379. $stm = $this->bd->prepare ($sql);
  380. $stm->execute ();
  381. return HelperFeed::daoToFeed ($stm->fetchAll (PDO::FETCH_ASSOC));
  382. }
  383. public function listFeedsOrderUpdate () {
  384. $sql = 'SELECT * FROM ' . $this->prefix . 'feed ORDER BY lastUpdate';
  385. $stm = $this->bd->prepare ($sql);
  386. $stm->execute ();
  387. return HelperFeed::daoToFeed ($stm->fetchAll (PDO::FETCH_ASSOC));
  388. }
  389. public function listByCategory ($cat) {
  390. $sql = 'SELECT * FROM ' . $this->prefix . 'feed WHERE category=? ORDER BY name';
  391. $stm = $this->bd->prepare ($sql);
  392. $values = array ($cat);
  393. $stm->execute ($values);
  394. return HelperFeed::daoToFeed ($stm->fetchAll (PDO::FETCH_ASSOC));
  395. }
  396. public function count () {
  397. $sql = 'SELECT COUNT(*) AS count FROM ' . $this->prefix . 'feed';
  398. $stm = $this->bd->prepare ($sql);
  399. $stm->execute ();
  400. $res = $stm->fetchAll (PDO::FETCH_ASSOC);
  401. return $res[0]['count'];
  402. }
  403. public function countEntries ($id) {
  404. $sql = 'SELECT COUNT(*) AS count FROM ' . $this->prefix . 'entry WHERE id_feed=?';
  405. $stm = $this->bd->prepare ($sql);
  406. $values = array ($id);
  407. $stm->execute ($values);
  408. $res = $stm->fetchAll (PDO::FETCH_ASSOC);
  409. return $res[0]['count'];
  410. }
  411. public function countNotRead ($id) {
  412. $sql = 'SELECT COUNT(*) AS count FROM ' . $this->prefix . 'entry WHERE is_read=0 AND id_feed=?';
  413. $stm = $this->bd->prepare ($sql);
  414. $values = array ($id);
  415. $stm->execute ($values);
  416. $res = $stm->fetchAll (PDO::FETCH_ASSOC);
  417. return $res[0]['count'];
  418. }
  419. }
  420. class HelperFeed {
  421. public static function daoToFeed ($listDAO) {
  422. $list = array ();
  423. if (!is_array ($listDAO)) {
  424. $listDAO = array ($listDAO);
  425. }
  426. foreach ($listDAO as $key => $dao) {
  427. if (isset ($dao['id'])) {
  428. $key = $dao['id'];
  429. }
  430. $list[$key] = new Feed ($dao['url']);
  431. $list[$key]->_category ($dao['category']);
  432. $list[$key]->_name ($dao['name']);
  433. $list[$key]->_website ($dao['website']);
  434. $list[$key]->_description ($dao['description']);
  435. $list[$key]->_lastUpdate ($dao['lastUpdate']);
  436. $list[$key]->_priority ($dao['priority']);
  437. $list[$key]->_pathEntries ($dao['pathEntries']);
  438. $list[$key]->_httpAuth (base64_decode ($dao['httpAuth']));
  439. $list[$key]->_error ($dao['error']);
  440. $list[$key]->_keepHistory ($dao['keep_history']);
  441. if (isset ($dao['id'])) {
  442. $list[$key]->_id ($dao['id']);
  443. }
  444. }
  445. return $list;
  446. }
  447. }