Feed.php 12 KB

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