Feed.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  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. $subscribe_url = $feed->subscribe_url ();
  125. if (!is_null ($subscribe_url) && $subscribe_url != $this->url) {
  126. $this->_url ($subscribe_url);
  127. }
  128. $title = $feed->get_title ();
  129. $this->_name (!is_null ($title) ? $title : $this->url);
  130. $this->_website ($feed->get_link ());
  131. $this->_description ($feed->get_description ());
  132. $this->loadEntries ($feed);
  133. }
  134. }
  135. }
  136. private function loadEntries ($feed) {
  137. $entries = array ();
  138. foreach ($feed->get_items () as $item) {
  139. $title = $item->get_title ();
  140. $author = $item->get_author ();
  141. $link = $item->get_permalink ();
  142. $date = strtotime ($item->get_date ());
  143. // gestion des tags (catégorie == tag)
  144. $tags_tmp = $item->get_categories ();
  145. $tags = array ();
  146. if (!is_null ($tags_tmp)) {
  147. foreach ($tags_tmp as $tag) {
  148. $tags[] = $tag->get_label ();
  149. }
  150. }
  151. // Gestion du contenu
  152. // On cherche à récupérer les articles en entier... même si le flux ne le propose pas
  153. $path = $this->pathEntries ();
  154. if ($path) {
  155. try {
  156. $content = get_content_by_parsing ($item->get_permalink (), $path);
  157. } catch (Exception $e) {
  158. $content = $item->get_content ();
  159. }
  160. } else {
  161. $content = $item->get_content ();
  162. }
  163. $entry = new Entry (
  164. $this->id (),
  165. $item->get_id (),
  166. !is_null ($title) ? $title : '',
  167. !is_null ($author) ? $author->name : '',
  168. !is_null ($content) ? $content : '',
  169. !is_null ($link) ? $link : '',
  170. $date ? $date : time ()
  171. );
  172. $entry->_tags ($tags);
  173. $entries[$entry->id ()] = $entry;
  174. }
  175. $this->entries = $entries;
  176. }
  177. }
  178. class FeedDAO extends Model_pdo {
  179. public function addFeed ($valuesTmp) {
  180. $sql = 'INSERT INTO feed (id, url, category, name, website, description, lastUpdate) VALUES(?, ?, ?, ?, ?, ?, ?)';
  181. $stm = $this->bd->prepare ($sql);
  182. $values = array (
  183. $valuesTmp['id'],
  184. $valuesTmp['url'],
  185. $valuesTmp['category'],
  186. $valuesTmp['name'],
  187. $valuesTmp['website'],
  188. $valuesTmp['description'],
  189. $valuesTmp['lastUpdate'],
  190. );
  191. if ($stm && $stm->execute ($values)) {
  192. return true;
  193. } else {
  194. $info = $stm->errorInfo();
  195. Log::record ('SQL error : ' . $info[2], Log::ERROR);
  196. return false;
  197. }
  198. }
  199. public function updateFeed ($id, $valuesTmp) {
  200. $set = '';
  201. foreach ($valuesTmp as $key => $v) {
  202. $set .= $key . '=?, ';
  203. }
  204. $set = substr ($set, 0, -2);
  205. $sql = 'UPDATE feed SET ' . $set . ' WHERE id=?';
  206. $stm = $this->bd->prepare ($sql);
  207. foreach ($valuesTmp as $v) {
  208. $values[] = $v;
  209. }
  210. $values[] = $id;
  211. if ($stm && $stm->execute ($values)) {
  212. return true;
  213. } else {
  214. $info = $stm->errorInfo();
  215. Log::record ('SQL error : ' . $info[2], Log::ERROR);
  216. return false;
  217. }
  218. }
  219. public function updateLastUpdate ($id) {
  220. $sql = 'UPDATE feed SET lastUpdate=? WHERE id=?';
  221. $stm = $this->bd->prepare ($sql);
  222. $values = array (
  223. time (),
  224. $id
  225. );
  226. if ($stm && $stm->execute ($values)) {
  227. return true;
  228. } else {
  229. $info = $stm->errorInfo();
  230. Log::record ('SQL error : ' . $info[2], Log::ERROR);
  231. return false;
  232. }
  233. }
  234. public function deleteFeed ($id) {
  235. $sql = 'DELETE FROM feed WHERE id=?';
  236. $stm = $this->bd->prepare ($sql);
  237. $values = array ($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 searchById ($id) {
  247. $sql = 'SELECT * FROM feed WHERE id=?';
  248. $stm = $this->bd->prepare ($sql);
  249. $values = array ($id);
  250. $stm->execute ($values);
  251. $res = $stm->fetchAll (PDO::FETCH_ASSOC);
  252. $feed = HelperFeed::daoToFeed ($res);
  253. if (isset ($feed[$id])) {
  254. return $feed[$id];
  255. } else {
  256. $info = $stm->errorInfo();
  257. Log::record ('SQL error : ' . $info[2], Log::ERROR);
  258. return false;
  259. }
  260. }
  261. public function listFeeds () {
  262. $sql = 'SELECT * FROM feed ORDER BY name';
  263. $stm = $this->bd->prepare ($sql);
  264. $stm->execute ();
  265. return HelperFeed::daoToFeed ($stm->fetchAll (PDO::FETCH_ASSOC));
  266. }
  267. public function listFeedsOrderUpdate () {
  268. $sql = 'SELECT * FROM feed ORDER BY lastUpdate';
  269. $stm = $this->bd->prepare ($sql);
  270. $stm->execute ();
  271. return HelperFeed::daoToFeed ($stm->fetchAll (PDO::FETCH_ASSOC));
  272. }
  273. public function listByCategory ($cat) {
  274. $sql = 'SELECT * FROM feed WHERE category=? ORDER BY name';
  275. $stm = $this->bd->prepare ($sql);
  276. $values = array ($cat);
  277. $stm->execute ($values);
  278. return HelperFeed::daoToFeed ($stm->fetchAll (PDO::FETCH_ASSOC));
  279. }
  280. public function count () {
  281. $sql = 'SELECT COUNT(*) AS count FROM feed';
  282. $stm = $this->bd->prepare ($sql);
  283. $stm->execute ();
  284. $res = $stm->fetchAll (PDO::FETCH_ASSOC);
  285. return $res[0]['count'];
  286. }
  287. public function countEntries ($id) {
  288. $sql = 'SELECT COUNT(*) AS count FROM entry WHERE id_feed=?';
  289. $stm = $this->bd->prepare ($sql);
  290. $values = array ($id);
  291. $stm->execute ($values);
  292. $res = $stm->fetchAll (PDO::FETCH_ASSOC);
  293. return $res[0]['count'];
  294. }
  295. public function countNotRead ($id) {
  296. $sql = 'SELECT COUNT(*) AS count FROM entry WHERE is_read=0 AND id_feed=?';
  297. $stm = $this->bd->prepare ($sql);
  298. $values = array ($id);
  299. $stm->execute ($values);
  300. $res = $stm->fetchAll (PDO::FETCH_ASSOC);
  301. return $res[0]['count'];
  302. }
  303. }
  304. class HelperFeed {
  305. public static function daoToFeed ($listDAO) {
  306. $list = array ();
  307. if (!is_array ($listDAO)) {
  308. $listDAO = array ($listDAO);
  309. }
  310. foreach ($listDAO as $key => $dao) {
  311. if (isset ($dao['id'])) {
  312. $key = $dao['id'];
  313. }
  314. $list[$key] = new Feed ($dao['url']);
  315. $list[$key]->_category ($dao['category']);
  316. $list[$key]->_name ($dao['name']);
  317. $list[$key]->_website ($dao['website']);
  318. $list[$key]->_description ($dao['description']);
  319. $list[$key]->_lastUpdate ($dao['lastUpdate']);
  320. $list[$key]->_pathEntries ($dao['pathEntries']);
  321. $list[$key]->_httpAuth ($dao['httpAuth']);
  322. if (isset ($dao['id'])) {
  323. $list[$key]->_id ($dao['id']);
  324. }
  325. }
  326. return $list;
  327. }
  328. }