feedController.php 13 KB

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