Feed.php 12 KB

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