Feed.php 13 KB

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