Feed.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  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 deleteFeed ($id) {
  270. $sql = 'DELETE FROM feed WHERE id=?';
  271. $stm = $this->bd->prepare ($sql);
  272. $values = array ($id);
  273. if ($stm && $stm->execute ($values)) {
  274. return true;
  275. } else {
  276. $info = $stm->errorInfo();
  277. Log::record ('SQL error : ' . $info[2], Log::ERROR);
  278. return false;
  279. }
  280. }
  281. public function deleteFeedByCategory ($id) {
  282. $sql = 'DELETE FROM feed WHERE category=?';
  283. $stm = $this->bd->prepare ($sql);
  284. $values = array ($id);
  285. if ($stm && $stm->execute ($values)) {
  286. return true;
  287. } else {
  288. $info = $stm->errorInfo();
  289. Log::record ('SQL error : ' . $info[2], Log::ERROR);
  290. return false;
  291. }
  292. }
  293. public function searchById ($id) {
  294. $sql = 'SELECT * FROM feed WHERE id=?';
  295. $stm = $this->bd->prepare ($sql);
  296. $values = array ($id);
  297. $stm->execute ($values);
  298. $res = $stm->fetchAll (PDO::FETCH_ASSOC);
  299. $feed = HelperFeed::daoToFeed ($res);
  300. if (isset ($feed[$id])) {
  301. return $feed[$id];
  302. } else {
  303. return false;
  304. }
  305. }
  306. public function searchByUrl ($url) {
  307. $sql = 'SELECT * FROM feed WHERE url=?';
  308. $stm = $this->bd->prepare ($sql);
  309. $values = array ($url);
  310. $stm->execute ($values);
  311. $res = $stm->fetchAll (PDO::FETCH_ASSOC);
  312. $feed = current (HelperFeed::daoToFeed ($res));
  313. if (isset ($feed)) {
  314. return $feed;
  315. } else {
  316. return false;
  317. }
  318. }
  319. public function listFeeds () {
  320. $sql = 'SELECT * FROM feed ORDER BY name';
  321. $stm = $this->bd->prepare ($sql);
  322. $stm->execute ();
  323. return HelperFeed::daoToFeed ($stm->fetchAll (PDO::FETCH_ASSOC));
  324. }
  325. public function listFeedsOrderUpdate () {
  326. $sql = 'SELECT * FROM feed ORDER BY lastUpdate';
  327. $stm = $this->bd->prepare ($sql);
  328. $stm->execute ();
  329. return HelperFeed::daoToFeed ($stm->fetchAll (PDO::FETCH_ASSOC));
  330. }
  331. public function listByCategory ($cat) {
  332. $sql = 'SELECT * FROM feed WHERE category=? ORDER BY name';
  333. $stm = $this->bd->prepare ($sql);
  334. $values = array ($cat);
  335. $stm->execute ($values);
  336. return HelperFeed::daoToFeed ($stm->fetchAll (PDO::FETCH_ASSOC));
  337. }
  338. public function count () {
  339. $sql = 'SELECT COUNT(*) AS count FROM feed';
  340. $stm = $this->bd->prepare ($sql);
  341. $stm->execute ();
  342. $res = $stm->fetchAll (PDO::FETCH_ASSOC);
  343. return $res[0]['count'];
  344. }
  345. public function countEntries ($id) {
  346. $sql = 'SELECT COUNT(*) AS count FROM entry WHERE id_feed=?';
  347. $stm = $this->bd->prepare ($sql);
  348. $values = array ($id);
  349. $stm->execute ($values);
  350. $res = $stm->fetchAll (PDO::FETCH_ASSOC);
  351. return $res[0]['count'];
  352. }
  353. public function countNotRead ($id) {
  354. $sql = 'SELECT COUNT(*) AS count FROM entry WHERE is_read=0 AND id_feed=?';
  355. $stm = $this->bd->prepare ($sql);
  356. $values = array ($id);
  357. $stm->execute ($values);
  358. $res = $stm->fetchAll (PDO::FETCH_ASSOC);
  359. return $res[0]['count'];
  360. }
  361. }
  362. class HelperFeed {
  363. public static function daoToFeed ($listDAO) {
  364. $list = array ();
  365. if (!is_array ($listDAO)) {
  366. $listDAO = array ($listDAO);
  367. }
  368. foreach ($listDAO as $key => $dao) {
  369. if (isset ($dao['id'])) {
  370. $key = $dao['id'];
  371. }
  372. $list[$key] = new Feed ($dao['url']);
  373. $list[$key]->_category ($dao['category']);
  374. $list[$key]->_name ($dao['name']);
  375. $list[$key]->_website ($dao['website']);
  376. $list[$key]->_description ($dao['description']);
  377. $list[$key]->_lastUpdate ($dao['lastUpdate']);
  378. $list[$key]->_priority ($dao['priority']);
  379. $list[$key]->_pathEntries ($dao['pathEntries']);
  380. $list[$key]->_httpAuth (base64_decode ($dao['httpAuth']));
  381. if (isset ($dao['id'])) {
  382. $list[$key]->_id ($dao['id']);
  383. }
  384. }
  385. return $list;
  386. }
  387. }