Feed.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  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 Exception ();
  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->init ();
  149. if ($feed->error()) {
  150. throw new FeedException ($feed->error);
  151. }
  152. $subscribe_url = $feed->subscribe_url ();
  153. if (!is_null ($subscribe_url) && $subscribe_url != $this->url) {
  154. if ($this->httpAuth != '') {
  155. $subscribe_url = preg_replace ('#((.+)://)((.+)@)(.+)#', '${1}${5}', $subscribe_url);
  156. }
  157. $this->_url ($subscribe_url);
  158. }
  159. $title = $feed->get_title ();
  160. $this->_name (!is_null ($title) ? $title : $this->url);
  161. $this->_website ($feed->get_link ());
  162. $this->_description ($feed->get_description ());
  163. $this->loadEntries ($feed);
  164. }
  165. }
  166. }
  167. private function loadEntries ($feed) {
  168. $entries = array ();
  169. foreach ($feed->get_items () as $item) {
  170. $title = $item->get_title ();
  171. $author = $item->get_author ();
  172. $link = $item->get_permalink ();
  173. $date = strtotime ($item->get_date ());
  174. // gestion des tags (catégorie == tag)
  175. $tags_tmp = $item->get_categories ();
  176. $tags = array ();
  177. if (!is_null ($tags_tmp)) {
  178. foreach ($tags_tmp as $tag) {
  179. $tags[] = $tag->get_label ();
  180. }
  181. }
  182. // Gestion du contenu
  183. // On cherche à récupérer les articles en entier... même si le flux ne le propose pas
  184. $path = $this->pathEntries ();
  185. if ($path) {
  186. try {
  187. $content = get_content_by_parsing ($item->get_permalink (), $path);
  188. } catch (Exception $e) {
  189. $content = $item->get_content ();
  190. }
  191. } else {
  192. $content = $item->get_content ();
  193. }
  194. $entry = new Entry (
  195. $this->id (),
  196. $item->get_id (),
  197. !is_null ($title) ? $title : '',
  198. !is_null ($author) ? $author->name : '',
  199. !is_null ($content) ? $content : '',
  200. !is_null ($link) ? $link : '',
  201. $date ? $date : time ()
  202. );
  203. $entry->_tags ($tags);
  204. $entries[$entry->id ()] = $entry;
  205. }
  206. $this->entries = $entries;
  207. }
  208. }
  209. class FeedDAO extends Model_pdo {
  210. public function addFeed ($valuesTmp) {
  211. $sql = 'INSERT INTO feed (id, url, category, name, website, description, lastUpdate, priority, httpAuth, error) VALUES(?, ?, ?, ?, ?, ?, ?, 10, ?, 0)';
  212. $stm = $this->bd->prepare ($sql);
  213. $values = array (
  214. $valuesTmp['id'],
  215. $valuesTmp['url'],
  216. $valuesTmp['category'],
  217. $valuesTmp['name'],
  218. $valuesTmp['website'],
  219. $valuesTmp['description'],
  220. $valuesTmp['lastUpdate'],
  221. base64_encode ($valuesTmp['httpAuth']),
  222. );
  223. if ($stm && $stm->execute ($values)) {
  224. return true;
  225. } else {
  226. $info = $stm->errorInfo();
  227. Log::record ('SQL error : ' . $info[2], Log::ERROR);
  228. return false;
  229. }
  230. }
  231. public function updateFeed ($id, $valuesTmp) {
  232. $set = '';
  233. foreach ($valuesTmp as $key => $v) {
  234. $set .= $key . '=?, ';
  235. if ($key == 'httpAuth') {
  236. $valuesTmp[$key] = base64_encode ($v);
  237. }
  238. }
  239. $set = substr ($set, 0, -2);
  240. $sql = 'UPDATE feed SET ' . $set . ' WHERE id=?';
  241. $stm = $this->bd->prepare ($sql);
  242. foreach ($valuesTmp as $v) {
  243. $values[] = $v;
  244. }
  245. $values[] = $id;
  246. if ($stm && $stm->execute ($values)) {
  247. return true;
  248. } else {
  249. $info = $stm->errorInfo();
  250. Log::record ('SQL error : ' . $info[2], Log::ERROR);
  251. return false;
  252. }
  253. }
  254. public function updateLastUpdate ($id) {
  255. $sql = 'UPDATE feed SET lastUpdate=? WHERE id=?';
  256. $stm = $this->bd->prepare ($sql);
  257. $values = array (
  258. time (),
  259. $id
  260. );
  261. if ($stm && $stm->execute ($values)) {
  262. return true;
  263. } else {
  264. $info = $stm->errorInfo();
  265. Log::record ('SQL error : ' . $info[2], Log::ERROR);
  266. return false;
  267. }
  268. }
  269. public function changeCategory ($idOldCat, $idNewCat) {
  270. $catDAO = new CategoryDAO ();
  271. $newCat = $catDAO->searchById ($idNewCat);
  272. if (!$newCat) {
  273. $newCat = $catDAO->getDefault ();
  274. }
  275. $sql = 'UPDATE feed SET category=? WHERE category=?';
  276. $stm = $this->bd->prepare ($sql);
  277. $values = array (
  278. $newCat->id (),
  279. $idOldCat
  280. );
  281. if ($stm && $stm->execute ($values)) {
  282. return true;
  283. } else {
  284. $info = $stm->errorInfo();
  285. Log::record ('SQL error : ' . $info[2], Log::ERROR);
  286. return false;
  287. }
  288. }
  289. public function deleteFeed ($id) {
  290. $sql = 'DELETE FROM feed WHERE id=?';
  291. $stm = $this->bd->prepare ($sql);
  292. $values = array ($id);
  293. if ($stm && $stm->execute ($values)) {
  294. return true;
  295. } else {
  296. $info = $stm->errorInfo();
  297. Log::record ('SQL error : ' . $info[2], Log::ERROR);
  298. return false;
  299. }
  300. }
  301. public function deleteFeedByCategory ($id) {
  302. $sql = 'DELETE FROM feed WHERE category=?';
  303. $stm = $this->bd->prepare ($sql);
  304. $values = array ($id);
  305. if ($stm && $stm->execute ($values)) {
  306. return true;
  307. } else {
  308. $info = $stm->errorInfo();
  309. Log::record ('SQL error : ' . $info[2], Log::ERROR);
  310. return false;
  311. }
  312. }
  313. public function searchById ($id) {
  314. $sql = 'SELECT * FROM feed WHERE id=?';
  315. $stm = $this->bd->prepare ($sql);
  316. $values = array ($id);
  317. $stm->execute ($values);
  318. $res = $stm->fetchAll (PDO::FETCH_ASSOC);
  319. $feed = HelperFeed::daoToFeed ($res);
  320. if (isset ($feed[$id])) {
  321. return $feed[$id];
  322. } else {
  323. return false;
  324. }
  325. }
  326. public function searchByUrl ($url) {
  327. $sql = 'SELECT * FROM feed WHERE url=?';
  328. $stm = $this->bd->prepare ($sql);
  329. $values = array ($url);
  330. $stm->execute ($values);
  331. $res = $stm->fetchAll (PDO::FETCH_ASSOC);
  332. $feed = current (HelperFeed::daoToFeed ($res));
  333. if (isset ($feed)) {
  334. return $feed;
  335. } else {
  336. return false;
  337. }
  338. }
  339. public function listFeeds () {
  340. $sql = 'SELECT * FROM feed ORDER BY name';
  341. $stm = $this->bd->prepare ($sql);
  342. $stm->execute ();
  343. return HelperFeed::daoToFeed ($stm->fetchAll (PDO::FETCH_ASSOC));
  344. }
  345. public function listFeedsOrderUpdate () {
  346. $sql = 'SELECT * FROM feed ORDER BY lastUpdate';
  347. $stm = $this->bd->prepare ($sql);
  348. $stm->execute ();
  349. return HelperFeed::daoToFeed ($stm->fetchAll (PDO::FETCH_ASSOC));
  350. }
  351. public function listByCategory ($cat) {
  352. $sql = 'SELECT * FROM feed WHERE category=? ORDER BY name';
  353. $stm = $this->bd->prepare ($sql);
  354. $values = array ($cat);
  355. $stm->execute ($values);
  356. return HelperFeed::daoToFeed ($stm->fetchAll (PDO::FETCH_ASSOC));
  357. }
  358. public function count () {
  359. $sql = 'SELECT COUNT(*) AS count FROM feed';
  360. $stm = $this->bd->prepare ($sql);
  361. $stm->execute ();
  362. $res = $stm->fetchAll (PDO::FETCH_ASSOC);
  363. return $res[0]['count'];
  364. }
  365. public function countEntries ($id) {
  366. $sql = 'SELECT COUNT(*) AS count FROM entry WHERE id_feed=?';
  367. $stm = $this->bd->prepare ($sql);
  368. $values = array ($id);
  369. $stm->execute ($values);
  370. $res = $stm->fetchAll (PDO::FETCH_ASSOC);
  371. return $res[0]['count'];
  372. }
  373. public function countNotRead ($id) {
  374. $sql = 'SELECT COUNT(*) AS count FROM entry WHERE is_read=0 AND id_feed=?';
  375. $stm = $this->bd->prepare ($sql);
  376. $values = array ($id);
  377. $stm->execute ($values);
  378. $res = $stm->fetchAll (PDO::FETCH_ASSOC);
  379. return $res[0]['count'];
  380. }
  381. }
  382. class HelperFeed {
  383. public static function daoToFeed ($listDAO) {
  384. $list = array ();
  385. if (!is_array ($listDAO)) {
  386. $listDAO = array ($listDAO);
  387. }
  388. foreach ($listDAO as $key => $dao) {
  389. if (isset ($dao['id'])) {
  390. $key = $dao['id'];
  391. }
  392. $list[$key] = new Feed ($dao['url']);
  393. $list[$key]->_category ($dao['category']);
  394. $list[$key]->_name ($dao['name']);
  395. $list[$key]->_website ($dao['website']);
  396. $list[$key]->_description ($dao['description']);
  397. $list[$key]->_lastUpdate ($dao['lastUpdate']);
  398. $list[$key]->_priority ($dao['priority']);
  399. $list[$key]->_pathEntries ($dao['pathEntries']);
  400. $list[$key]->_httpAuth (base64_decode ($dao['httpAuth']));
  401. if (isset ($dao['id'])) {
  402. $list[$key]->_id ($dao['id']);
  403. }
  404. }
  405. return $list;
  406. }
  407. }