feedController.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. <?php
  2. class FreshRSS_feed_Controller extends Minz_ActionController {
  3. public function firstAction () {
  4. if (!$this->view->loginOk) {
  5. $token = $this->view->conf->token; //TODO: check the token logic again, and if it is still needed
  6. $token_param = Minz_Request::param ('token', '');
  7. $token_is_ok = ($token != '' && $token == $token_param);
  8. $action = Minz_Request::actionName ();
  9. if (!($token_is_ok && $action === 'actualize')) {
  10. Minz_Error::error (
  11. 403,
  12. array ('error' => array (Minz_Translate::t ('access_denied')))
  13. );
  14. }
  15. }
  16. $this->catDAO = new FreshRSS_CategoryDAO ();
  17. $this->catDAO->checkDefault ();
  18. }
  19. public function addAction () {
  20. @set_time_limit(300);
  21. if (Minz_Request::isPost ()) {
  22. $url = Minz_Request::param ('url_rss');
  23. $cat = Minz_Request::param ('category', false);
  24. if ($cat === false) {
  25. $def_cat = $this->catDAO->getDefault ();
  26. $cat = $def_cat->id ();
  27. }
  28. $user = Minz_Request::param ('http_user');
  29. $pass = Minz_Request::param ('http_pass');
  30. $params = array ();
  31. $transactionStarted = false;
  32. try {
  33. $feed = new FreshRSS_Feed ($url);
  34. $feed->_category ($cat);
  35. $httpAuth = '';
  36. if ($user != '' || $pass != '') {
  37. $httpAuth = $user . ':' . $pass;
  38. }
  39. $feed->_httpAuth ($httpAuth);
  40. $feed->load(true);
  41. $feedDAO = new FreshRSS_FeedDAO ();
  42. $values = array (
  43. 'url' => $feed->url (),
  44. 'category' => $feed->category (),
  45. 'name' => $feed->name (),
  46. 'website' => $feed->website (),
  47. 'description' => $feed->description (),
  48. 'lastUpdate' => time (),
  49. 'httpAuth' => $feed->httpAuth (),
  50. );
  51. if ($feedDAO->searchByUrl ($values['url'])) {
  52. // on est déjà abonné à ce flux
  53. $notif = array (
  54. 'type' => 'bad',
  55. 'content' => Minz_Translate::t ('already_subscribed', $feed->name ())
  56. );
  57. Minz_Session::_param ('notification', $notif);
  58. } else {
  59. $id = $feedDAO->addFeed ($values);
  60. if (!$id) {
  61. // problème au niveau de la base de données
  62. $notif = array (
  63. 'type' => 'bad',
  64. 'content' => Minz_Translate::t ('feed_not_added', $feed->name ())
  65. );
  66. Minz_Session::_param ('notification', $notif);
  67. } else {
  68. $feed->_id ($id);
  69. $feed->faviconPrepare();
  70. $is_read = $this->view->conf->mark_when['reception'] ? 1 : 0;
  71. $entryDAO = new FreshRSS_EntryDAO ();
  72. $entries = array_reverse($feed->entries()); //We want chronological order and SimplePie uses reverse order
  73. // on calcule la date des articles les plus anciens qu'on accepte
  74. $nb_month_old = $this->view->conf->old_entries;
  75. $date_min = time () - (3600 * 24 * 30 * $nb_month_old);
  76. $transactionStarted = true;
  77. $feedDAO->beginTransaction ();
  78. // on ajoute les articles en masse sans vérification
  79. foreach ($entries as $entry) {
  80. $values = $entry->toArray ();
  81. $values['id_feed'] = $feed->id ();
  82. $values['id'] = min(time(), $entry->date (true)) . uSecString();
  83. $values['is_read'] = $is_read;
  84. $entryDAO->addEntry ($values);
  85. }
  86. $feedDAO->updateLastUpdate ($feed->id ());
  87. $feedDAO->commit ();
  88. $transactionStarted = false;
  89. // ok, ajout terminé
  90. $notif = array (
  91. 'type' => 'good',
  92. 'content' => Minz_Translate::t ('feed_added', $feed->name ())
  93. );
  94. Minz_Session::_param ('notification', $notif);
  95. // permet de rediriger vers la page de conf du flux
  96. $params['id'] = $feed->id ();
  97. }
  98. }
  99. } catch (FreshRSS_BadUrl_Exception $e) {
  100. Minz_Log::record ($e->getMessage (), Minz_Log::WARNING);
  101. $notif = array (
  102. 'type' => 'bad',
  103. 'content' => Minz_Translate::t ('invalid_url', $url)
  104. );
  105. Minz_Session::_param ('notification', $notif);
  106. } catch (FreshRSS_Feed_Exception $e) {
  107. Minz_Log::record ($e->getMessage (), Minz_Log::WARNING);
  108. $notif = array (
  109. 'type' => 'bad',
  110. 'content' => Minz_Translate::t ('internal_problem_feed')
  111. );
  112. Minz_Session::_param ('notification', $notif);
  113. } catch (Minz_FileNotExistException $e) {
  114. // Répertoire de cache n'existe pas
  115. Minz_Log::record ($e->getMessage (), Minz_Log::ERROR);
  116. $notif = array (
  117. 'type' => 'bad',
  118. 'content' => Minz_Translate::t ('internal_problem_feed')
  119. );
  120. Minz_Session::_param ('notification', $notif);
  121. }
  122. if ($transactionStarted) {
  123. $feedDAO->rollBack ();
  124. }
  125. Minz_Request::forward (array ('c' => 'configure', 'a' => 'feed', 'params' => $params), true);
  126. }
  127. }
  128. public function truncateAction () {
  129. if (Minz_Request::isPost ()) {
  130. $id = Minz_Request::param ('id');
  131. $feedDAO = new FreshRSS_FeedDAO ();
  132. $n = $feedDAO->truncate($id);
  133. $notif = array(
  134. 'type' => $n === false ? 'bad' : 'good',
  135. 'content' => Minz_Translate::t ('n_entries_deleted', $n)
  136. );
  137. Minz_Session::_param ('notification', $notif);
  138. invalidateHttpCache();
  139. Minz_Request::forward (array ('c' => 'configure', 'a' => 'feed', 'params' => array('id' => $id)), true);
  140. }
  141. }
  142. public function actualizeAction () {
  143. @set_time_limit(300);
  144. $feedDAO = new FreshRSS_FeedDAO ();
  145. $entryDAO = new FreshRSS_EntryDAO ();
  146. $id = Minz_Request::param ('id');
  147. $force = Minz_Request::param ('force', false);
  148. // on créé la liste des flux à mettre à actualiser
  149. // si on veut mettre un flux à jour spécifiquement, on le met
  150. // dans la liste, mais seul (permet d'automatiser le traitement)
  151. $feeds = array ();
  152. if ($id) {
  153. $feed = $feedDAO->searchById ($id);
  154. if ($feed) {
  155. $feeds = array ($feed);
  156. }
  157. } else {
  158. $feeds = $feedDAO->listFeedsOrderUpdate ();
  159. }
  160. // on calcule la date des articles les plus anciens qu'on accepte
  161. $nb_month_old = max($this->view->conf->old_entries, 1);
  162. $date_min = time () - (3600 * 24 * 30 * $nb_month_old);
  163. $i = 0;
  164. $flux_update = 0;
  165. $is_read = $this->view->conf->mark_when['reception'] ? 1 : 0;
  166. foreach ($feeds as $feed) {
  167. try {
  168. $url = $feed->url();
  169. $feed->load(false);
  170. $entries = array_reverse($feed->entries()); //We want chronological order and SimplePie uses reverse order
  171. //For this feed, check last n entry GUIDs already in database
  172. $existingGuids = array_fill_keys ($entryDAO->listLastGuidsByFeed ($feed->id (), count($entries) + 10), 1);
  173. $useDeclaredDate = empty($existingGuids);
  174. $feedHistory = $feed->keepHistory();
  175. if ($feedHistory == -2) { //default
  176. $feedHistory = $this->view->conf->keep_history_default;
  177. }
  178. // On ne vérifie pas strictement que l'article n'est pas déjà en BDD
  179. // La BDD refusera l'ajout car (id_feed, guid) doit être unique
  180. $feedDAO->beginTransaction ();
  181. foreach ($entries as $entry) {
  182. $eDate = $entry->date (true);
  183. if ((!isset ($existingGuids[$entry->guid ()])) &&
  184. (($feedHistory != 0) || ($eDate >= $date_min))) {
  185. $values = $entry->toArray ();
  186. //Use declared date at first import, otherwise use discovery date
  187. $values['id'] = ($useDeclaredDate || $eDate < $date_min) ?
  188. min(time(), $eDate) . uSecString() :
  189. uTimeString();
  190. $values['is_read'] = $is_read;
  191. $entryDAO->addEntry ($values);
  192. }
  193. }
  194. if (($feedHistory >= 0) && (rand(0, 30) === 1)) {
  195. $nb = $feedDAO->cleanOldEntries ($feed->id (), $date_min, max($feedHistory, count($entries) + 10));
  196. if ($nb > 0) {
  197. Minz_Log::record ($nb . ' old entries cleaned in feed [' . $feed->url() . ']', Minz_Log::DEBUG);
  198. }
  199. }
  200. // on indique que le flux vient d'être mis à jour en BDD
  201. $feedDAO->updateLastUpdate ($feed->id ());
  202. $feedDAO->commit ();
  203. $flux_update++;
  204. if ($feed->url() !== $url) { //URL has changed (auto-discovery)
  205. $feedDAO->updateFeed($feed->id(), array('url' => $feed->url()));
  206. }
  207. $feed->faviconPrepare();
  208. } catch (FreshRSS_Feed_Exception $e) {
  209. Minz_Log::record ($e->getMessage (), Minz_Log::NOTICE);
  210. $feedDAO->updateLastUpdate ($feed->id (), 1);
  211. }
  212. // On arrête à 10 flux pour ne pas surcharger le serveur
  213. // sauf si le paramètre $force est à vrai
  214. $i++;
  215. if ($i >= 10 && !$force) {
  216. break;
  217. }
  218. }
  219. $url = array ();
  220. if ($flux_update === 1) {
  221. // on a mis un seul flux à jour
  222. $notif = array (
  223. 'type' => 'good',
  224. 'content' => Minz_Translate::t ('feed_actualized', $feed->name ())
  225. );
  226. } elseif ($flux_update > 1) {
  227. // plusieurs flux on été mis à jour
  228. $notif = array (
  229. 'type' => 'good',
  230. 'content' => Minz_Translate::t ('n_feeds_actualized', $flux_update)
  231. );
  232. } else {
  233. // aucun flux n'a été mis à jour, oups
  234. $notif = array (
  235. 'type' => 'bad',
  236. 'content' => Minz_Translate::t ('no_feed_actualized')
  237. );
  238. }
  239. if ($i === 1) {
  240. // Si on a voulu mettre à jour qu'un flux
  241. // on filtre l'affichage par ce flux
  242. $feed = reset ($feeds);
  243. $url['params'] = array ('get' => 'f_' . $feed->id ());
  244. }
  245. if (Minz_Request::param ('ajax', 0) === 0) {
  246. Minz_Session::_param ('notification', $notif);
  247. Minz_Request::forward ($url, true);
  248. } else {
  249. // Une requête Ajax met un seul flux à jour.
  250. // Comme en principe plusieurs requêtes ont lieu,
  251. // on indique que "plusieurs flux ont été mis à jour".
  252. // Cela permet d'avoir une notification plus proche du
  253. // ressenti utilisateur
  254. $notif = array (
  255. 'type' => 'good',
  256. 'content' => Minz_Translate::t ('feeds_actualized')
  257. );
  258. Minz_Session::_param ('notification', $notif);
  259. // et on désactive le layout car ne sert à rien
  260. $this->view->_useLayout (false);
  261. }
  262. }
  263. public function massiveImportAction () {
  264. @set_time_limit(300);
  265. $entryDAO = new FreshRSS_EntryDAO ();
  266. $feedDAO = new FreshRSS_FeedDAO ();
  267. $categories = Minz_Request::param ('categories', array (), true);
  268. $feeds = Minz_Request::param ('feeds', array (), true);
  269. // on ajoute les catégories en masse dans une fonction à part
  270. $this->addCategories ($categories);
  271. // on calcule la date des articles les plus anciens qu'on accepte
  272. $nb_month_old = $this->view->conf->old_entries;
  273. $date_min = time () - (3600 * 24 * 30 * $nb_month_old);
  274. // la variable $error permet de savoir si une erreur est survenue
  275. // Le but est de ne pas arrêter l'import même en cas d'erreur
  276. // L'utilisateur sera mis au courant s'il y a eu des erreurs, mais
  277. // ne connaîtra pas les détails. Ceux-ci seront toutefois logguées
  278. $error = false;
  279. $i = 0;
  280. foreach ($feeds as $feed) {
  281. try {
  282. $values = array (
  283. 'id' => $feed->id (),
  284. 'url' => $feed->url (),
  285. 'category' => $feed->category (),
  286. 'name' => $feed->name (),
  287. 'website' => $feed->website (),
  288. 'description' => $feed->description (),
  289. 'lastUpdate' => 0,
  290. 'httpAuth' => $feed->httpAuth ()
  291. );
  292. // ajout du flux que s'il n'est pas déjà en BDD
  293. if (!$feedDAO->searchByUrl ($values['url'])) {
  294. $id = $feedDAO->addFeed ($values);
  295. if ($id) {
  296. $feed->_id ($id);
  297. $feed->faviconPrepare();
  298. } else {
  299. $error = true;
  300. }
  301. }
  302. } catch (FreshRSS_Feed_Exception $e) {
  303. $error = true;
  304. Minz_Log::record ($e->getMessage (), Minz_Log::WARNING);
  305. }
  306. }
  307. if ($error) {
  308. $res = Minz_Translate::t ('feeds_imported_with_errors');
  309. } else {
  310. $res = Minz_Translate::t ('feeds_imported');
  311. }
  312. $notif = array (
  313. 'type' => 'good',
  314. 'content' => $res
  315. );
  316. Minz_Session::_param ('notification', $notif);
  317. Minz_Session::_param ('actualize_feeds', true);
  318. // et on redirige vers la page d'accueil
  319. Minz_Request::forward (array (
  320. 'c' => 'index',
  321. 'a' => 'index'
  322. ), true);
  323. }
  324. public function deleteAction () {
  325. if (Minz_Request::isPost ()) {
  326. $type = Minz_Request::param ('type', 'feed');
  327. $id = Minz_Request::param ('id');
  328. $feedDAO = new FreshRSS_FeedDAO ();
  329. if ($type == 'category') {
  330. if ($feedDAO->deleteFeedByCategory ($id)) {
  331. $notif = array (
  332. 'type' => 'good',
  333. 'content' => Minz_Translate::t ('category_emptied')
  334. );
  335. //TODO: Delete old favicons
  336. } else {
  337. $notif = array (
  338. 'type' => 'bad',
  339. 'content' => Minz_Translate::t ('error_occured')
  340. );
  341. }
  342. } else {
  343. if ($feedDAO->deleteFeed ($id)) {
  344. $notif = array (
  345. 'type' => 'good',
  346. 'content' => Minz_Translate::t ('feed_deleted')
  347. );
  348. //TODO: Delete old favicon
  349. } else {
  350. $notif = array (
  351. 'type' => 'bad',
  352. 'content' => Minz_Translate::t ('error_occured')
  353. );
  354. }
  355. }
  356. Minz_Session::_param ('notification', $notif);
  357. if ($type == 'category') {
  358. Minz_Request::forward (array ('c' => 'configure', 'a' => 'categorize'), true);
  359. } else {
  360. Minz_Request::forward (array ('c' => 'configure', 'a' => 'feed'), true);
  361. }
  362. }
  363. }
  364. private function addCategories ($categories) {
  365. foreach ($categories as $cat) {
  366. if (!$this->catDAO->searchByName ($cat->name ())) {
  367. $values = array (
  368. 'id' => $cat->id (),
  369. 'name' => $cat->name (),
  370. 'color' => $cat->color ()
  371. );
  372. $catDAO->addCategory ($values);
  373. }
  374. }
  375. }
  376. }