Feed.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550
  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. $this->priority = is_numeric ($value) ? intval ($value) : 10;
  139. }
  140. public function _pathEntries ($value) {
  141. $this->pathEntries = $value;
  142. }
  143. public function _httpAuth ($value) {
  144. $this->httpAuth = $value;
  145. }
  146. public function _error ($value) {
  147. if ($value) {
  148. $value = true;
  149. } else {
  150. $value = false;
  151. }
  152. $this->error = $value;
  153. }
  154. public function _keepHistory ($value) {
  155. if ($value) {
  156. $value = true;
  157. } else {
  158. $value = false;
  159. }
  160. $this->keep_history = $value;
  161. }
  162. public function _nbNotRead ($value) {
  163. $this->nbNotRead = is_numeric ($value) ? intval ($value) : -1;
  164. }
  165. public function _nbEntries ($value) {
  166. $this->nbEntries = is_numeric ($value) ? intval ($value) : -1;
  167. }
  168. public function load () {
  169. if (!is_null ($this->url)) {
  170. if (CACHE_PATH === false) {
  171. throw new FileNotExistException (
  172. 'CACHE_PATH',
  173. MinzException::ERROR
  174. );
  175. } else {
  176. $feed = new SimplePie ();
  177. $url = str_replace ('&amp;', '&', $this->url);
  178. if ($this->httpAuth != '') {
  179. $url = preg_replace ('#((.+)://)(.+)#', '${1}' . $this->httpAuth . '@${3}', $url);
  180. }
  181. $feed->set_feed_url ($url);
  182. $feed->set_cache_location (CACHE_PATH);
  183. $feed->strip_htmltags (array (
  184. 'base', 'blink', 'body', 'doctype',
  185. 'font', 'form', 'frame', 'frameset', 'html',
  186. 'input', 'marquee', 'meta', 'noscript',
  187. 'param', 'script', 'style'
  188. ));
  189. $feed->init ();
  190. if ($feed->error ()) {
  191. throw new FeedException ($feed->error . ' [' . $url . ']');
  192. }
  193. // si on a utilisé l'auto-discover, notre url va avoir changé
  194. $subscribe_url = $feed->subscribe_url ();
  195. if (!is_null ($subscribe_url) && $subscribe_url != $this->url) {
  196. if ($this->httpAuth != '') {
  197. // on enlève les id si authentification HTTP
  198. $subscribe_url = preg_replace ('#((.+)://)((.+)@)(.+)#', '${1}${5}', $subscribe_url);
  199. }
  200. $this->_url ($subscribe_url);
  201. }
  202. if (empty($this->name)) { // May come from OPML
  203. $title = $feed->get_title ();
  204. $this->_name (!is_null ($title) ? $title : $this->url);
  205. }
  206. $this->_website ($feed->get_link ());
  207. $this->_description ($feed->get_description ());
  208. // et on charge les articles du flux
  209. $this->loadEntries ($feed);
  210. }
  211. }
  212. }
  213. private function loadEntries ($feed) {
  214. $entries = array ();
  215. foreach ($feed->get_items () as $item) {
  216. $title = strip_tags($item->get_title ());
  217. $author = $item->get_author ();
  218. $link = $item->get_permalink ();
  219. $date = strtotime ($item->get_date ());
  220. // gestion des tags (catégorie == tag)
  221. $tags_tmp = $item->get_categories ();
  222. $tags = array ();
  223. if (!is_null ($tags_tmp)) {
  224. foreach ($tags_tmp as $tag) {
  225. $tags[] = $tag->get_label ();
  226. }
  227. }
  228. $content = $item->get_content ();
  229. $elinks = array();
  230. foreach ($item->get_enclosures() as $enclosure) {
  231. $elink = $enclosure->get_link();
  232. if (array_key_exists($elink, $elinks)) continue;
  233. $elinks[$elink] = '1';
  234. $mime = strtolower($enclosure->get_type());
  235. if (strpos($mime, 'image/') === 0) {
  236. $content .= '<br /><img src="' . $elink . '" />';
  237. }
  238. }
  239. $entry = new Entry (
  240. $this->id (),
  241. $item->get_id (),
  242. !is_null ($title) ? $title : '',
  243. !is_null ($author) ? $author->name : '',
  244. !is_null ($content) ? $content : '',
  245. !is_null ($link) ? $link : '',
  246. $date ? $date : time ()
  247. );
  248. $entry->_tags ($tags);
  249. // permet de récupérer le contenu des flux tronqués
  250. $entry->loadCompleteContent($this->pathEntries());
  251. $entries[$entry->id ()] = $entry;
  252. }
  253. $this->entries = $entries;
  254. }
  255. }
  256. class FeedDAO extends Model_pdo {
  257. public function addFeed ($valuesTmp) {
  258. $sql = 'INSERT INTO ' . $this->prefix . 'feed (id, url, category, name, website, description, lastUpdate, priority, httpAuth, error, keep_history) VALUES(?, ?, ?, ?, ?, ?, ?, 10, ?, 0, 0)';
  259. $stm = $this->bd->prepare ($sql);
  260. $values = array (
  261. $valuesTmp['id'],
  262. $valuesTmp['url'],
  263. $valuesTmp['category'],
  264. $valuesTmp['name'],
  265. $valuesTmp['website'],
  266. $valuesTmp['description'],
  267. $valuesTmp['lastUpdate'],
  268. base64_encode ($valuesTmp['httpAuth']),
  269. );
  270. if ($stm && $stm->execute ($values)) {
  271. return true;
  272. } else {
  273. $info = $stm->errorInfo();
  274. Minz_Log::record ('SQL error : ' . $info[2], Minz_Log::ERROR);
  275. return false;
  276. }
  277. }
  278. public function updateFeed ($id, $valuesTmp) {
  279. $set = '';
  280. foreach ($valuesTmp as $key => $v) {
  281. $set .= $key . '=?, ';
  282. if ($key == 'httpAuth') {
  283. $valuesTmp[$key] = base64_encode ($v);
  284. }
  285. }
  286. $set = substr ($set, 0, -2);
  287. $sql = 'UPDATE ' . $this->prefix . 'feed SET ' . $set . ' WHERE id=?';
  288. $stm = $this->bd->prepare ($sql);
  289. foreach ($valuesTmp as $v) {
  290. $values[] = $v;
  291. }
  292. $values[] = $id;
  293. if ($stm && $stm->execute ($values)) {
  294. return true;
  295. } else {
  296. $info = $stm->errorInfo();
  297. Minz_Log::record ('SQL error : ' . $info[2], Minz_Log::ERROR);
  298. return false;
  299. }
  300. }
  301. public function updateLastUpdate ($id) {
  302. $sql = 'UPDATE ' . $this->prefix . 'feed SET lastUpdate=?, error=0 WHERE id=?';
  303. $stm = $this->bd->prepare ($sql);
  304. $values = array (
  305. time (),
  306. $id
  307. );
  308. if ($stm && $stm->execute ($values)) {
  309. return true;
  310. } else {
  311. $info = $stm->errorInfo();
  312. Minz_Log::record ('SQL error : ' . $info[2], Minz_Log::ERROR);
  313. return false;
  314. }
  315. }
  316. public function isInError ($id) {
  317. $sql = 'UPDATE ' . $this->prefix . 'feed SET error=1 WHERE id=?';
  318. $stm = $this->bd->prepare ($sql);
  319. $values = array (
  320. $id
  321. );
  322. if ($stm && $stm->execute ($values)) {
  323. return true;
  324. } else {
  325. $info = $stm->errorInfo();
  326. Minz_Log::record ('SQL error : ' . $info[2], Minz_Log::ERROR);
  327. return false;
  328. }
  329. }
  330. public function changeCategory ($idOldCat, $idNewCat) {
  331. $catDAO = new CategoryDAO ();
  332. $newCat = $catDAO->searchById ($idNewCat);
  333. if (!$newCat) {
  334. $newCat = $catDAO->getDefault ();
  335. }
  336. $sql = 'UPDATE ' . $this->prefix . 'feed SET category=? WHERE category=?';
  337. $stm = $this->bd->prepare ($sql);
  338. $values = array (
  339. $newCat->id (),
  340. $idOldCat
  341. );
  342. if ($stm && $stm->execute ($values)) {
  343. return true;
  344. } else {
  345. $info = $stm->errorInfo();
  346. Minz_Log::record ('SQL error : ' . $info[2], Minz_Log::ERROR);
  347. return false;
  348. }
  349. }
  350. public function deleteFeed ($id) {
  351. $sql = 'DELETE FROM ' . $this->prefix . 'feed WHERE id=?';
  352. $stm = $this->bd->prepare ($sql);
  353. $values = array ($id);
  354. if ($stm && $stm->execute ($values)) {
  355. return true;
  356. } else {
  357. $info = $stm->errorInfo();
  358. Minz_Log::record ('SQL error : ' . $info[2], Minz_Log::ERROR);
  359. return false;
  360. }
  361. }
  362. public function deleteFeedByCategory ($id) {
  363. $sql = 'DELETE FROM ' . $this->prefix . 'feed WHERE category=?';
  364. $stm = $this->bd->prepare ($sql);
  365. $values = array ($id);
  366. if ($stm && $stm->execute ($values)) {
  367. return true;
  368. } else {
  369. $info = $stm->errorInfo();
  370. Minz_Log::record ('SQL error : ' . $info[2], Minz_Log::ERROR);
  371. return false;
  372. }
  373. }
  374. public function searchById ($id) {
  375. $sql = 'SELECT * FROM ' . $this->prefix . 'feed WHERE id=?';
  376. $stm = $this->bd->prepare ($sql);
  377. $values = array ($id);
  378. $stm->execute ($values);
  379. $res = $stm->fetchAll (PDO::FETCH_ASSOC);
  380. $feed = HelperFeed::daoToFeed ($res);
  381. if (isset ($feed[$id])) {
  382. return $feed[$id];
  383. } else {
  384. return false;
  385. }
  386. }
  387. public function searchByUrl ($url) {
  388. $sql = 'SELECT * FROM ' . $this->prefix . 'feed WHERE url=?';
  389. $stm = $this->bd->prepare ($sql);
  390. $values = array ($url);
  391. $stm->execute ($values);
  392. $res = $stm->fetchAll (PDO::FETCH_ASSOC);
  393. $feed = current (HelperFeed::daoToFeed ($res));
  394. if (isset ($feed)) {
  395. return $feed;
  396. } else {
  397. return false;
  398. }
  399. }
  400. public function listFeeds () {
  401. $sql = 'SELECT * FROM ' . $this->prefix . 'feed ORDER BY name';
  402. $stm = $this->bd->prepare ($sql);
  403. $stm->execute ();
  404. return HelperFeed::daoToFeed ($stm->fetchAll (PDO::FETCH_ASSOC));
  405. }
  406. public function listFeedsOrderUpdate () {
  407. $sql = 'SELECT * FROM ' . $this->prefix . 'feed ORDER BY lastUpdate';
  408. $stm = $this->bd->prepare ($sql);
  409. $stm->execute ();
  410. return HelperFeed::daoToFeed ($stm->fetchAll (PDO::FETCH_ASSOC));
  411. }
  412. public function listByCategory ($cat) {
  413. $sql = 'SELECT * FROM ' . $this->prefix . 'feed WHERE category=? ORDER BY name';
  414. $stm = $this->bd->prepare ($sql);
  415. $values = array ($cat);
  416. $stm->execute ($values);
  417. return HelperFeed::daoToFeed ($stm->fetchAll (PDO::FETCH_ASSOC));
  418. }
  419. public function count () { //Is this used?
  420. $sql = 'SELECT COUNT(*) AS count FROM ' . $this->prefix . 'feed';
  421. $stm = $this->bd->prepare ($sql);
  422. $stm->execute ();
  423. $res = $stm->fetchAll (PDO::FETCH_ASSOC);
  424. return $res[0]['count'];
  425. }
  426. public function countEntries ($id) {
  427. $sql = 'SELECT COUNT(*) AS count FROM ' . $this->prefix . 'entry WHERE id_feed=?';
  428. $stm = $this->bd->prepare ($sql);
  429. $values = array ($id);
  430. $stm->execute ($values);
  431. $res = $stm->fetchAll (PDO::FETCH_ASSOC);
  432. return $res[0]['count'];
  433. }
  434. public function countNotRead ($id) { //Is this used?
  435. $sql = 'SELECT COUNT(*) AS count FROM ' . $this->prefix . 'entry WHERE is_read=0 AND id_feed=?';
  436. $stm = $this->bd->prepare ($sql);
  437. $values = array ($id);
  438. $stm->execute ($values);
  439. $res = $stm->fetchAll (PDO::FETCH_ASSOC);
  440. return $res[0]['count'];
  441. }
  442. }
  443. class HelperFeed {
  444. public static function daoToFeed ($listDAO) {
  445. $list = array ();
  446. if (!is_array ($listDAO)) {
  447. $listDAO = array ($listDAO);
  448. }
  449. foreach ($listDAO as $key => $dao) {
  450. if (empty ($dao['url'])) {
  451. continue;
  452. }
  453. if (isset ($dao['id'])) {
  454. $key = $dao['id'];
  455. }
  456. $list[$key] = new Feed ($dao['url']);
  457. $list[$key]->_category ($dao['category']);
  458. $list[$key]->_name ($dao['name']);
  459. $list[$key]->_website ($dao['website']);
  460. $list[$key]->_description ($dao['description']);
  461. $list[$key]->_lastUpdate ($dao['lastUpdate']);
  462. $list[$key]->_priority ($dao['priority']);
  463. $list[$key]->_pathEntries ($dao['pathEntries']);
  464. $list[$key]->_httpAuth (base64_decode ($dao['httpAuth']));
  465. $list[$key]->_error ($dao['error']);
  466. $list[$key]->_keepHistory ($dao['keep_history']);
  467. if (isset ($dao['nbNotRead'])) {
  468. $list[$key]->_nbNotRead ($dao['nbNotRead']);
  469. }
  470. if (isset ($dao['nbEntries'])) {
  471. $list[$key]->_nbEntries ($dao['nbEntries']);
  472. }
  473. if (isset ($dao['id'])) {
  474. $list[$key]->_id ($dao['id']);
  475. }
  476. }
  477. return $list;
  478. }
  479. }