feedController.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. <?php
  2. class feedController extends ActionController {
  3. public function firstAction () {
  4. $catDAO = new CategoryDAO ();
  5. $catDAO->checkDefault ();
  6. }
  7. public function addAction () {
  8. if (login_is_conf ($this->view->conf) && !is_logged ()) {
  9. Error::error (
  10. 403,
  11. array ('error' => array (Translate::t ('access_denied')))
  12. );
  13. } else {
  14. if (Request::isPost ()) {
  15. $url = Request::param ('url_rss');
  16. $cat = Request::param ('category');
  17. $user = Request::param ('username');
  18. $pass = Request::param ('password');
  19. $params = array ();
  20. try {
  21. $feed = new Feed ($url);
  22. $feed->_category ($cat);
  23. $httpAuth = '';
  24. if ($user != '' || $pass != '') {
  25. $httpAuth = $user . ':' . $pass;
  26. }
  27. $feed->_httpAuth ($httpAuth);
  28. $feed->load ();
  29. $feedDAO = new FeedDAO ();
  30. $values = array (
  31. 'id' => $feed->id (),
  32. 'url' => $feed->url (),
  33. 'category' => $feed->category (),
  34. 'name' => $feed->name (),
  35. 'website' => $feed->website (),
  36. 'description' => $feed->description (),
  37. 'lastUpdate' => time (),
  38. 'httpAuth' => $feed->httpAuth (),
  39. );
  40. if ($feedDAO->searchByUrl ($values['url'])) {
  41. $notif = array (
  42. 'type' => 'bad',
  43. 'content' => Translate::t ('already_subscribed', $feed->name ())
  44. );
  45. Session::_param ('notification', $notif);
  46. } elseif ($feedDAO->addFeed ($values)) {
  47. $entryDAO = new EntryDAO ();
  48. $entries = $feed->entries ();
  49. foreach ($entries as $entry) {
  50. $values = $entry->toArray ();
  51. $entryDAO->addEntry ($values);
  52. }
  53. // notif
  54. $notif = array (
  55. 'type' => 'good',
  56. 'content' => Translate::t ('feed_added', $feed->name ())
  57. );
  58. Session::_param ('notification', $notif);
  59. $params['id'] = $feed->id ();
  60. } else {
  61. // notif
  62. $notif = array (
  63. 'type' => 'bad',
  64. 'content' => Translate::t ('feed_not_added', $feed->name ())
  65. );
  66. Session::_param ('notification', $notif);
  67. }
  68. } catch (FeedException $e) {
  69. Log::record ($e->getMessage (), Log::ERROR);
  70. $notif = array (
  71. 'type' => 'bad',
  72. 'content' => Translate::t ('internal_problem_feed')
  73. );
  74. Session::_param ('notification', $notif);
  75. } catch (Exception $e) {
  76. // notif
  77. $notif = array (
  78. 'type' => 'bad',
  79. 'content' => Translate::t ('invalid_url', $url)
  80. );
  81. Session::_param ('notification', $notif);
  82. }
  83. Request::forward (array ('c' => 'configure', 'a' => 'feed', 'params' => $params), true);
  84. }
  85. }
  86. }
  87. public function actualizeAction () {
  88. $feedDAO = new FeedDAO ();
  89. $entryDAO = new EntryDAO ();
  90. $id = Request::param ('id');
  91. $feeds = array ();
  92. if ($id) {
  93. $feed = $feedDAO->searchById ($id);
  94. if ($feed) {
  95. $feeds = array ($feed);
  96. }
  97. } else {
  98. $feeds = $feedDAO->listFeedsOrderUpdate ();
  99. }
  100. // pour ne pas ajouter des entrées trop anciennes
  101. $nb_month_old = $this->view->conf->oldEntries ();
  102. $date_min = time () - (60 * 60 * 24 * 30 * $nb_month_old);
  103. $i = 0;
  104. foreach ($feeds as $feed) {
  105. try {
  106. $feed->load ();
  107. $entries = $feed->entries ();
  108. foreach ($entries as $entry) {
  109. if ($entry->date (true) >= $date_min) {
  110. $values = $entry->toArray ();
  111. $entryDAO->addEntry ($values);
  112. }
  113. }
  114. $feedDAO->updateLastUpdate ($feed->id ());
  115. } catch (FeedException $e) {
  116. Log::record ($e->getMessage (), Log::ERROR);
  117. }
  118. $i++;
  119. if ($i >= 10) {
  120. break;
  121. }
  122. }
  123. $entryDAO->cleanOldEntries ($nb_month_old);
  124. // notif
  125. $url = array ();
  126. if ($i == 1) {
  127. $feed = reset ($feeds);
  128. $notif = array (
  129. 'type' => 'good',
  130. 'content' => Translate::t ('feed_actualized', $feed->name ())
  131. );
  132. $url['params'] = array ('get' => 'f_' . $feed->id ());
  133. } elseif ($i > 0) {
  134. $notif = array (
  135. 'type' => 'good',
  136. 'content' => Translate::t ('n_feeds_actualized', $i)
  137. );
  138. } else {
  139. $notif = array (
  140. 'type' => 'bad',
  141. 'content' => Translate::t ('no_feed_actualized')
  142. );
  143. }
  144. if (Request::param ('ajax', 0) == 0) {
  145. Session::_param ('notification', $notif);
  146. Request::forward ($url, true);
  147. } else {
  148. $notif = array (
  149. 'type' => 'good',
  150. 'content' => Translate::t ('feeds_actualized')
  151. );
  152. Session::_param ('notification', $notif);
  153. $this->view->_useLayout (false);
  154. }
  155. }
  156. public function massiveImportAction () {
  157. if (login_is_conf ($this->view->conf) && !is_logged ()) {
  158. Error::error (
  159. 403,
  160. array ('error' => array (Translate::t ('access_denied')))
  161. );
  162. } else {
  163. $entryDAO = new EntryDAO ();
  164. $feedDAO = new FeedDAO ();
  165. $categories = Request::param ('categories', array ());
  166. $feeds = Request::param ('feeds', array ());
  167. $this->addCategories ($categories);
  168. $nb_month_old = $this->view->conf->oldEntries ();
  169. $date_min = time () - (60 * 60 * 24 * 30 * $nb_month_old);
  170. $error = false;
  171. $i = 0;
  172. foreach ($feeds as $feed) {
  173. try {
  174. $feed->load ();
  175. // Enregistrement du flux
  176. $values = array (
  177. 'id' => $feed->id (),
  178. 'url' => $feed->url (),
  179. 'category' => $feed->category (),
  180. 'name' => $feed->name (),
  181. 'website' => $feed->website (),
  182. 'description' => $feed->description (),
  183. 'lastUpdate' => 0
  184. );
  185. if (!$feedDAO->searchByUrl ($values['url'])) {
  186. if (!$feedDAO->addFeed ($values)) {
  187. $error = true;
  188. }
  189. }
  190. } catch (FeedException $e) {
  191. $error = true;
  192. Log::record ($e->getMessage (), Log::ERROR);
  193. }
  194. }
  195. if ($error) {
  196. $res = Translate::t ('feeds_imported_with_errors');
  197. } else {
  198. $res = Translate::t ('feeds_imported');
  199. }
  200. $notif = array (
  201. 'type' => 'good',
  202. 'content' => $res
  203. );
  204. Session::_param ('notification', $notif);
  205. Request::forward (array (
  206. 'c' => 'configure',
  207. 'a' => 'importExport'
  208. ), true);
  209. }
  210. }
  211. public function deleteAction () {
  212. if (login_is_conf ($this->view->conf) && !is_logged ()) {
  213. Error::error (
  214. 403,
  215. array ('error' => array (Translate::t ('access_denied')))
  216. );
  217. } else {
  218. $type = Request::param ('type', 'feed');
  219. $id = Request::param ('id');
  220. $feedDAO = new FeedDAO ();
  221. if ($type == 'category') {
  222. if ($feedDAO->deleteFeedByCategory ($id)) {
  223. $notif = array (
  224. 'type' => 'good',
  225. 'content' => Translate::t ('category_emptied')
  226. );
  227. } else {
  228. $notif = array (
  229. 'type' => 'bad',
  230. 'content' => Translate::t ('error_occured')
  231. );
  232. }
  233. } else {
  234. if ($feedDAO->deleteFeed ($id)) {
  235. $notif = array (
  236. 'type' => 'good',
  237. 'content' => Translate::t ('feed_deleted')
  238. );
  239. } else {
  240. $notif = array (
  241. 'type' => 'bad',
  242. 'content' => Translate::t ('error_occured')
  243. );
  244. }
  245. }
  246. Session::_param ('notification', $notif);
  247. if ($type == 'category') {
  248. Request::forward (array ('c' => 'configure', 'a' => 'categorize'), true);
  249. } else {
  250. Request::forward (array ('c' => 'configure', 'a' => 'feed'), true);
  251. }
  252. }
  253. }
  254. private function addCategories ($categories) {
  255. $catDAO = new CategoryDAO ();
  256. foreach ($categories as $cat) {
  257. if (!$catDAO->searchByName ($cat->name ())) {
  258. $values = array (
  259. 'id' => $cat->id (),
  260. 'name' => $cat->name (),
  261. 'color' => $cat->color ()
  262. );
  263. $catDAO->addCategory ($values);
  264. }
  265. }
  266. }
  267. }