Feed.php 9.1 KB

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