Feed.php 11 KB

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