Feed.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  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 $pathEntries = '';
  12. private $httpAuth = '';
  13. public function __construct ($url) {
  14. $this->_url ($url);
  15. }
  16. public function id () {
  17. if(is_null($this->id)) {
  18. return small_hash ($this->url . Configuration::selApplication ());
  19. } else {
  20. return $this->id;
  21. }
  22. }
  23. public function url () {
  24. return $this->url;
  25. }
  26. public function category () {
  27. return $this->category;
  28. }
  29. public function entries () {
  30. if (!is_null ($this->entries)) {
  31. return $this->entries;
  32. } else {
  33. return array ();
  34. }
  35. }
  36. public function name () {
  37. return $this->name;
  38. }
  39. public function website () {
  40. return $this->website;
  41. }
  42. public function description () {
  43. return $this->description;
  44. }
  45. public function lastUpdate () {
  46. return $this->lastUpdate;
  47. }
  48. public function pathEntries () {
  49. return $this->pathEntries;
  50. }
  51. public function httpAuth ($raw = true) {
  52. if ($raw) {
  53. return $this->httpAuth;
  54. } else {
  55. return array (
  56. 'username' => '',
  57. 'password' => ''
  58. );
  59. }
  60. }
  61. public function nbEntries () {
  62. $feedDAO = new FeedDAO ();
  63. return $feedDAO->countEntries ($this->id ());
  64. }
  65. public function nbNotRead () {
  66. $feedDAO = new FeedDAO ();
  67. return $feedDAO->countNotRead ($this->id ());
  68. }
  69. public function _id ($value) {
  70. $this->id = $value;
  71. }
  72. public function _url ($value) {
  73. if (!is_null ($value) && !preg_match ('#^https?://#', $value)) {
  74. $value = 'http://' . $value;
  75. }
  76. if (!is_null ($value) && filter_var ($value, FILTER_VALIDATE_URL)) {
  77. $this->url = $value;
  78. } else {
  79. throw new Exception ();
  80. }
  81. }
  82. public function _category ($value) {
  83. $this->category = $value;
  84. }
  85. public function _name ($value) {
  86. if (is_null ($value)) {
  87. $value = '';
  88. }
  89. $this->name = $value;
  90. }
  91. public function _website ($value) {
  92. if (is_null ($value)) {
  93. $value = '';
  94. }
  95. $this->website = $value;
  96. }
  97. public function _description ($value) {
  98. if (is_null ($value)) {
  99. $value = '';
  100. }
  101. $this->description = $value;
  102. }
  103. public function _lastUpdate ($value) {
  104. $this->lastUpdate = $value;
  105. }
  106. public function _pathEntries ($value) {
  107. $this->pathEntries = $value;
  108. }
  109. public function _httpAuth ($value) {
  110. $this->httpAuth = $value;
  111. }
  112. public function load () {
  113. if (!is_null ($this->url)) {
  114. if (CACHE_PATH === false) {
  115. throw new FileNotExistException (
  116. 'CACHE_PATH',
  117. MinzException::ERROR
  118. );
  119. } else {
  120. $feed = new SimplePie ();
  121. $feed->set_feed_url ($this->url);
  122. $feed->set_cache_location (CACHE_PATH);
  123. $feed->init ();
  124. if ($feed->error()) {
  125. throw new FeedException ($feed->error);
  126. }
  127. $subscribe_url = $feed->subscribe_url ();
  128. if (!is_null ($subscribe_url) && $subscribe_url != $this->url) {
  129. $this->_url ($subscribe_url);
  130. }
  131. $title = $feed->get_title ();
  132. $this->_name (!is_null ($title) ? $title : $this->url);
  133. $this->_website ($feed->get_link ());
  134. $this->_description ($feed->get_description ());
  135. $this->loadEntries ($feed);
  136. }
  137. }
  138. }
  139. private function loadEntries ($feed) {
  140. $entries = array ();
  141. foreach ($feed->get_items () as $item) {
  142. $title = $item->get_title ();
  143. $author = $item->get_author ();
  144. $link = $item->get_permalink ();
  145. $date = strtotime ($item->get_date ());
  146. // gestion des tags (catégorie == tag)
  147. $tags_tmp = $item->get_categories ();
  148. $tags = array ();
  149. if (!is_null ($tags_tmp)) {
  150. foreach ($tags_tmp as $tag) {
  151. $tags[] = $tag->get_label ();
  152. }
  153. }
  154. // Gestion du contenu
  155. // On cherche à récupérer les articles en entier... même si le flux ne le propose pas
  156. $path = $this->pathEntries ();
  157. if ($path) {
  158. try {
  159. $content = get_content_by_parsing ($item->get_permalink (), $path);
  160. } catch (Exception $e) {
  161. $content = $item->get_content ();
  162. }
  163. } else {
  164. $content = $item->get_content ();
  165. }
  166. $entry = new Entry (
  167. $this->id (),
  168. $item->get_id (),
  169. !is_null ($title) ? $title : '',
  170. !is_null ($author) ? $author->name : '',
  171. !is_null ($content) ? $content : '',
  172. !is_null ($link) ? $link : '',
  173. $date ? $date : time ()
  174. );
  175. $entry->_tags ($tags);
  176. $entries[$entry->id ()] = $entry;
  177. }
  178. $this->entries = $entries;
  179. }
  180. }
  181. class FeedDAO extends Model_pdo {
  182. public function addFeed ($valuesTmp) {
  183. $sql = 'INSERT INTO feed (id, url, category, name, website, description, lastUpdate) VALUES(?, ?, ?, ?, ?, ?, ?)';
  184. $stm = $this->bd->prepare ($sql);
  185. $values = array (
  186. $valuesTmp['id'],
  187. $valuesTmp['url'],
  188. $valuesTmp['category'],
  189. $valuesTmp['name'],
  190. $valuesTmp['website'],
  191. $valuesTmp['description'],
  192. $valuesTmp['lastUpdate'],
  193. );
  194. if ($stm && $stm->execute ($values)) {
  195. return true;
  196. } else {
  197. $info = $stm->errorInfo();
  198. Log::record ('SQL error : ' . $info[2], Log::ERROR);
  199. return false;
  200. }
  201. }
  202. public function updateFeed ($id, $valuesTmp) {
  203. $set = '';
  204. foreach ($valuesTmp as $key => $v) {
  205. $set .= $key . '=?, ';
  206. }
  207. $set = substr ($set, 0, -2);
  208. $sql = 'UPDATE feed SET ' . $set . ' WHERE id=?';
  209. $stm = $this->bd->prepare ($sql);
  210. foreach ($valuesTmp as $v) {
  211. $values[] = $v;
  212. }
  213. $values[] = $id;
  214. if ($stm && $stm->execute ($values)) {
  215. return true;
  216. } else {
  217. $info = $stm->errorInfo();
  218. Log::record ('SQL error : ' . $info[2], Log::ERROR);
  219. return false;
  220. }
  221. }
  222. public function updateLastUpdate ($id) {
  223. $sql = 'UPDATE feed SET lastUpdate=? WHERE id=?';
  224. $stm = $this->bd->prepare ($sql);
  225. $values = array (
  226. time (),
  227. $id
  228. );
  229. if ($stm && $stm->execute ($values)) {
  230. return true;
  231. } else {
  232. $info = $stm->errorInfo();
  233. Log::record ('SQL error : ' . $info[2], Log::ERROR);
  234. return false;
  235. }
  236. }
  237. public function deleteFeed ($id) {
  238. $sql = 'DELETE FROM feed WHERE id=?';
  239. $stm = $this->bd->prepare ($sql);
  240. $values = array ($id);
  241. if ($stm && $stm->execute ($values)) {
  242. return true;
  243. } else {
  244. $info = $stm->errorInfo();
  245. Log::record ('SQL error : ' . $info[2], Log::ERROR);
  246. return false;
  247. }
  248. }
  249. public function searchById ($id) {
  250. $sql = 'SELECT * FROM feed WHERE id=?';
  251. $stm = $this->bd->prepare ($sql);
  252. $values = array ($id);
  253. $stm->execute ($values);
  254. $res = $stm->fetchAll (PDO::FETCH_ASSOC);
  255. $feed = HelperFeed::daoToFeed ($res);
  256. if (isset ($feed[$id])) {
  257. return $feed[$id];
  258. } else {
  259. $info = $stm->errorInfo();
  260. Log::record ('SQL error : ' . $info[2], Log::ERROR);
  261. return false;
  262. }
  263. }
  264. public function listFeeds () {
  265. $sql = 'SELECT * FROM feed ORDER BY name';
  266. $stm = $this->bd->prepare ($sql);
  267. $stm->execute ();
  268. return HelperFeed::daoToFeed ($stm->fetchAll (PDO::FETCH_ASSOC));
  269. }
  270. public function listFeedsOrderUpdate () {
  271. $sql = 'SELECT * FROM feed ORDER BY lastUpdate';
  272. $stm = $this->bd->prepare ($sql);
  273. $stm->execute ();
  274. return HelperFeed::daoToFeed ($stm->fetchAll (PDO::FETCH_ASSOC));
  275. }
  276. public function listByCategory ($cat) {
  277. $sql = 'SELECT * FROM feed WHERE category=? ORDER BY name';
  278. $stm = $this->bd->prepare ($sql);
  279. $values = array ($cat);
  280. $stm->execute ($values);
  281. return HelperFeed::daoToFeed ($stm->fetchAll (PDO::FETCH_ASSOC));
  282. }
  283. public function count () {
  284. $sql = 'SELECT COUNT(*) AS count FROM feed';
  285. $stm = $this->bd->prepare ($sql);
  286. $stm->execute ();
  287. $res = $stm->fetchAll (PDO::FETCH_ASSOC);
  288. return $res[0]['count'];
  289. }
  290. public function countEntries ($id) {
  291. $sql = 'SELECT COUNT(*) AS count FROM entry WHERE id_feed=?';
  292. $stm = $this->bd->prepare ($sql);
  293. $values = array ($id);
  294. $stm->execute ($values);
  295. $res = $stm->fetchAll (PDO::FETCH_ASSOC);
  296. return $res[0]['count'];
  297. }
  298. public function countNotRead ($id) {
  299. $sql = 'SELECT COUNT(*) AS count FROM entry WHERE is_read=0 AND id_feed=?';
  300. $stm = $this->bd->prepare ($sql);
  301. $values = array ($id);
  302. $stm->execute ($values);
  303. $res = $stm->fetchAll (PDO::FETCH_ASSOC);
  304. return $res[0]['count'];
  305. }
  306. }
  307. class HelperFeed {
  308. public static function daoToFeed ($listDAO) {
  309. $list = array ();
  310. if (!is_array ($listDAO)) {
  311. $listDAO = array ($listDAO);
  312. }
  313. foreach ($listDAO as $key => $dao) {
  314. if (isset ($dao['id'])) {
  315. $key = $dao['id'];
  316. }
  317. $list[$key] = new Feed ($dao['url']);
  318. $list[$key]->_category ($dao['category']);
  319. $list[$key]->_name ($dao['name']);
  320. $list[$key]->_website ($dao['website']);
  321. $list[$key]->_description ($dao['description']);
  322. $list[$key]->_lastUpdate ($dao['lastUpdate']);
  323. $list[$key]->_pathEntries ($dao['pathEntries']);
  324. $list[$key]->_httpAuth ($dao['httpAuth']);
  325. if (isset ($dao['id'])) {
  326. $list[$key]->_id ($dao['id']);
  327. }
  328. }
  329. return $list;
  330. }
  331. }