feedController.php 7.2 KB

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