Feed.php 12 KB

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