Feed.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  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 _id ($value) {
  77. $this->id = $value;
  78. }
  79. public function _url ($value) {
  80. if (!is_null ($value) && !preg_match ('#^https?://#', $value)) {
  81. $value = 'http://' . $value;
  82. }
  83. if (!is_null ($value) && filter_var ($value, FILTER_VALIDATE_URL)) {
  84. $this->url = $value;
  85. } else {
  86. throw new Exception ();
  87. }
  88. }
  89. public function _category ($value) {
  90. $this->category = $value;
  91. }
  92. public function _name ($value) {
  93. if (is_null ($value)) {
  94. $value = '';
  95. }
  96. $this->name = $value;
  97. }
  98. public function _website ($value) {
  99. if (is_null ($value)) {
  100. $value = '';
  101. }
  102. $this->website = $value;
  103. }
  104. public function _description ($value) {
  105. if (is_null ($value)) {
  106. $value = '';
  107. }
  108. $this->description = $value;
  109. }
  110. public function _lastUpdate ($value) {
  111. $this->lastUpdate = $value;
  112. }
  113. public function _priority ($value) {
  114. if (!is_int (intval ($value))) {
  115. $value = 10;
  116. }
  117. $this->priority = $value;
  118. }
  119. public function _pathEntries ($value) {
  120. $this->pathEntries = $value;
  121. }
  122. public function _httpAuth ($value) {
  123. $this->httpAuth = $value;
  124. }
  125. public function load () {
  126. if (!is_null ($this->url)) {
  127. if (CACHE_PATH === false) {
  128. throw new FileNotExistException (
  129. 'CACHE_PATH',
  130. MinzException::ERROR
  131. );
  132. } else {
  133. $feed = new SimplePie ();
  134. $url = preg_replace ('/&amp;/', '&', $this->url);
  135. if ($this->httpAuth != '') {
  136. $url = preg_replace ('#((.+)://)(.+)#', '${1}' . $this->httpAuth . '@${3}', $url);
  137. }
  138. $feed->set_feed_url ($url);
  139. $feed->set_cache_location (CACHE_PATH);
  140. $feed->init ();
  141. if ($feed->error()) {
  142. throw new FeedException ($feed->error);
  143. }
  144. $subscribe_url = $feed->subscribe_url ();
  145. if (!is_null ($subscribe_url) && $subscribe_url != $this->url) {
  146. if ($this->httpAuth != '') {
  147. $subscribe_url = preg_replace ('#((.+)://)((.+)@)(.+)#', '${1}${5}', $subscribe_url);
  148. }
  149. $this->_url ($subscribe_url);
  150. }
  151. $title = $feed->get_title ();
  152. $this->_name (!is_null ($title) ? $title : $this->url);
  153. $this->_website ($feed->get_link ());
  154. $this->_description ($feed->get_description ());
  155. $this->loadEntries ($feed);
  156. }
  157. }
  158. }
  159. private function loadEntries ($feed) {
  160. $entries = array ();
  161. foreach ($feed->get_items () as $item) {
  162. $title = $item->get_title ();
  163. $author = $item->get_author ();
  164. $link = $item->get_permalink ();
  165. $date = strtotime ($item->get_date ());
  166. // gestion des tags (catégorie == tag)
  167. $tags_tmp = $item->get_categories ();
  168. $tags = array ();
  169. if (!is_null ($tags_tmp)) {
  170. foreach ($tags_tmp as $tag) {
  171. $tags[] = $tag->get_label ();
  172. }
  173. }
  174. // Gestion du contenu
  175. // On cherche à récupérer les articles en entier... même si le flux ne le propose pas
  176. $path = $this->pathEntries ();
  177. if ($path) {
  178. try {
  179. $content = get_content_by_parsing ($item->get_permalink (), $path);
  180. } catch (Exception $e) {
  181. $content = $item->get_content ();
  182. }
  183. } else {
  184. $content = $item->get_content ();
  185. }
  186. $entry = new Entry (
  187. $this->id (),
  188. $item->get_id (),
  189. !is_null ($title) ? $title : '',
  190. !is_null ($author) ? $author->name : '',
  191. !is_null ($content) ? $content : '',
  192. !is_null ($link) ? $link : '',
  193. $date ? $date : time ()
  194. );
  195. $entry->_tags ($tags);
  196. $entries[$entry->id ()] = $entry;
  197. }
  198. $this->entries = $entries;
  199. }
  200. }
  201. class FeedDAO extends Model_pdo {
  202. public function addFeed ($valuesTmp) {
  203. $sql = 'INSERT INTO feed (id, url, category, name, website, description, lastUpdate, priority, httpAuth, error) VALUES(?, ?, ?, ?, ?, ?, ?, 10, ?, 0)';
  204. $stm = $this->bd->prepare ($sql);
  205. $values = array (
  206. $valuesTmp['id'],
  207. $valuesTmp['url'],
  208. $valuesTmp['category'],
  209. $valuesTmp['name'],
  210. $valuesTmp['website'],
  211. $valuesTmp['description'],
  212. $valuesTmp['lastUpdate'],
  213. base64_encode ($valuesTmp['httpAuth']),
  214. );
  215. if ($stm && $stm->execute ($values)) {
  216. return true;
  217. } else {
  218. $info = $stm->errorInfo();
  219. Log::record ('SQL error : ' . $info[2], Log::ERROR);
  220. return false;
  221. }
  222. }
  223. public function updateFeed ($id, $valuesTmp) {
  224. $set = '';
  225. foreach ($valuesTmp as $key => $v) {
  226. $set .= $key . '=?, ';
  227. if ($key == 'httpAuth') {
  228. $valuesTmp[$key] = base64_encode ($v);
  229. }
  230. }
  231. $set = substr ($set, 0, -2);
  232. $sql = 'UPDATE feed SET ' . $set . ' WHERE id=?';
  233. $stm = $this->bd->prepare ($sql);
  234. foreach ($valuesTmp as $v) {
  235. $values[] = $v;
  236. }
  237. $values[] = $id;
  238. if ($stm && $stm->execute ($values)) {
  239. return true;
  240. } else {
  241. $info = $stm->errorInfo();
  242. Log::record ('SQL error : ' . $info[2], Log::ERROR);
  243. return false;
  244. }
  245. }
  246. public function updateLastUpdate ($id) {
  247. $sql = 'UPDATE feed SET lastUpdate=? WHERE id=?';
  248. $stm = $this->bd->prepare ($sql);
  249. $values = array (
  250. time (),
  251. $id
  252. );
  253. if ($stm && $stm->execute ($values)) {
  254. return true;
  255. } else {
  256. $info = $stm->errorInfo();
  257. Log::record ('SQL error : ' . $info[2], Log::ERROR);
  258. return false;
  259. }
  260. }
  261. public function deleteFeed ($id) {
  262. $sql = 'DELETE FROM feed WHERE id=?';
  263. $stm = $this->bd->prepare ($sql);
  264. $values = array ($id);
  265. if ($stm && $stm->execute ($values)) {
  266. return true;
  267. } else {
  268. $info = $stm->errorInfo();
  269. Log::record ('SQL error : ' . $info[2], Log::ERROR);
  270. return false;
  271. }
  272. }
  273. public function deleteFeedByCategory ($id) {
  274. $sql = 'DELETE FROM feed WHERE category=?';
  275. $stm = $this->bd->prepare ($sql);
  276. $values = array ($id);
  277. if ($stm && $stm->execute ($values)) {
  278. return true;
  279. } else {
  280. $info = $stm->errorInfo();
  281. Log::record ('SQL error : ' . $info[2], Log::ERROR);
  282. return false;
  283. }
  284. }
  285. public function searchById ($id) {
  286. $sql = 'SELECT * FROM feed WHERE id=?';
  287. $stm = $this->bd->prepare ($sql);
  288. $values = array ($id);
  289. $stm->execute ($values);
  290. $res = $stm->fetchAll (PDO::FETCH_ASSOC);
  291. $feed = HelperFeed::daoToFeed ($res);
  292. if (isset ($feed[$id])) {
  293. return $feed[$id];
  294. } else {
  295. return false;
  296. }
  297. }
  298. public function searchByUrl ($url) {
  299. $sql = 'SELECT * FROM feed WHERE url=?';
  300. $stm = $this->bd->prepare ($sql);
  301. $values = array ($url);
  302. $stm->execute ($values);
  303. $res = $stm->fetchAll (PDO::FETCH_ASSOC);
  304. $feed = current (HelperFeed::daoToFeed ($res));
  305. if (isset ($feed)) {
  306. return $feed;
  307. } else {
  308. return false;
  309. }
  310. }
  311. public function listFeeds () {
  312. $sql = 'SELECT * FROM feed ORDER BY name';
  313. $stm = $this->bd->prepare ($sql);
  314. $stm->execute ();
  315. return HelperFeed::daoToFeed ($stm->fetchAll (PDO::FETCH_ASSOC));
  316. }
  317. public function listFeedsOrderUpdate () {
  318. $sql = 'SELECT * FROM feed ORDER BY lastUpdate';
  319. $stm = $this->bd->prepare ($sql);
  320. $stm->execute ();
  321. return HelperFeed::daoToFeed ($stm->fetchAll (PDO::FETCH_ASSOC));
  322. }
  323. public function listByCategory ($cat) {
  324. $sql = 'SELECT * FROM feed WHERE category=? ORDER BY name';
  325. $stm = $this->bd->prepare ($sql);
  326. $values = array ($cat);
  327. $stm->execute ($values);
  328. return HelperFeed::daoToFeed ($stm->fetchAll (PDO::FETCH_ASSOC));
  329. }
  330. public function count () {
  331. $sql = 'SELECT COUNT(*) AS count FROM feed';
  332. $stm = $this->bd->prepare ($sql);
  333. $stm->execute ();
  334. $res = $stm->fetchAll (PDO::FETCH_ASSOC);
  335. return $res[0]['count'];
  336. }
  337. public function countEntries ($id) {
  338. $sql = 'SELECT COUNT(*) AS count FROM entry WHERE id_feed=?';
  339. $stm = $this->bd->prepare ($sql);
  340. $values = array ($id);
  341. $stm->execute ($values);
  342. $res = $stm->fetchAll (PDO::FETCH_ASSOC);
  343. return $res[0]['count'];
  344. }
  345. public function countNotRead ($id) {
  346. $sql = 'SELECT COUNT(*) AS count FROM entry WHERE is_read=0 AND 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. }
  354. class HelperFeed {
  355. public static function daoToFeed ($listDAO) {
  356. $list = array ();
  357. if (!is_array ($listDAO)) {
  358. $listDAO = array ($listDAO);
  359. }
  360. foreach ($listDAO as $key => $dao) {
  361. if (isset ($dao['id'])) {
  362. $key = $dao['id'];
  363. }
  364. $list[$key] = new Feed ($dao['url']);
  365. $list[$key]->_category ($dao['category']);
  366. $list[$key]->_name ($dao['name']);
  367. $list[$key]->_website ($dao['website']);
  368. $list[$key]->_description ($dao['description']);
  369. $list[$key]->_lastUpdate ($dao['lastUpdate']);
  370. $list[$key]->_priority ($dao['priority']);
  371. $list[$key]->_pathEntries ($dao['pathEntries']);
  372. $list[$key]->_httpAuth (base64_decode ($dao['httpAuth']));
  373. if (isset ($dao['id'])) {
  374. $list[$key]->_id ($dao['id']);
  375. }
  376. }
  377. return $list;
  378. }
  379. }