feedController.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  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. }
  17. public function addAction () {
  18. @set_time_limit(300);
  19. if (Minz_Request::isPost ()) {
  20. $this->catDAO = new FreshRSS_CategoryDAO ();
  21. $this->catDAO->checkDefault ();
  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. Minz_Session::_param('actualize_feeds', false);
  147. $id = Minz_Request::param ('id');
  148. $force = Minz_Request::param ('force', false);
  149. // on créé la liste des flux à mettre à actualiser
  150. // si on veut mettre un flux à jour spécifiquement, on le met
  151. // dans la liste, mais seul (permet d'automatiser le traitement)
  152. $feeds = array ();
  153. if ($id) {
  154. $feed = $feedDAO->searchById ($id);
  155. if ($feed) {
  156. $feeds = array ($feed);
  157. }
  158. } else {
  159. $feeds = $feedDAO->listFeedsOrderUpdate ();
  160. }
  161. // on calcule la date des articles les plus anciens qu'on accepte
  162. $nb_month_old = max($this->view->conf->old_entries, 1);
  163. $date_min = time () - (3600 * 24 * 30 * $nb_month_old);
  164. $i = 0;
  165. $flux_update = 0;
  166. $is_read = $this->view->conf->mark_when['reception'] ? 1 : 0;
  167. foreach ($feeds as $feed) {
  168. if (!$feed->lock()) {
  169. Minz_Log::record('Feed already being actualized: ' . $feed->url(), Minz_Log::NOTICE);
  170. continue;
  171. }
  172. try {
  173. $url = $feed->url();
  174. $feedHistory = $feed->keepHistory();
  175. $feed->load(false);
  176. $entries = array_reverse($feed->entries()); //We want chronological order and SimplePie uses reverse order
  177. $hasTransaction = false;
  178. if (count($entries) > 0) {
  179. //For this feed, check last n entry GUIDs already in database
  180. $existingGuids = array_fill_keys ($entryDAO->listLastGuidsByFeed ($feed->id (), count($entries) + 10), 1);
  181. $useDeclaredDate = empty($existingGuids);
  182. if ($feedHistory == -2) { //default
  183. $feedHistory = $this->view->conf->keep_history_default;
  184. }
  185. $hasTransaction = true;
  186. $feedDAO->beginTransaction();
  187. // On ne vérifie pas strictement que l'article n'est pas déjà en BDD
  188. // La BDD refusera l'ajout car (id_feed, guid) doit être unique
  189. foreach ($entries as $entry) {
  190. $eDate = $entry->date (true);
  191. if ((!isset ($existingGuids[$entry->guid ()])) &&
  192. (($feedHistory != 0) || ($eDate >= $date_min))) {
  193. $values = $entry->toArray ();
  194. //Use declared date at first import, otherwise use discovery date
  195. $values['id'] = ($useDeclaredDate || $eDate < $date_min) ?
  196. min(time(), $eDate) . uSecString() :
  197. uTimeString();
  198. $values['is_read'] = $is_read;
  199. $entryDAO->addEntry ($values);
  200. }
  201. }
  202. }
  203. if (($feedHistory >= 0) && (rand(0, 30) === 1)) {
  204. if (!$hasTransaction) {
  205. $feedDAO->beginTransaction();
  206. }
  207. $nb = $feedDAO->cleanOldEntries ($feed->id (), $date_min, max($feedHistory, count($entries) + 10));
  208. if ($nb > 0) {
  209. Minz_Log::record ($nb . ' old entries cleaned in feed [' . $feed->url() . ']', Minz_Log::DEBUG);
  210. }
  211. }
  212. // on indique que le flux vient d'être mis à jour en BDD
  213. $feedDAO->updateLastUpdate ($feed->id (), 0, $hasTransaction);
  214. if ($hasTransaction) {
  215. $feedDAO->commit();
  216. }
  217. $flux_update++;
  218. if ($feed->url() !== $url) { //URL has changed (auto-discovery)
  219. $feedDAO->updateFeed($feed->id(), array('url' => $feed->url()));
  220. }
  221. } catch (FreshRSS_Feed_Exception $e) {
  222. Minz_Log::record ($e->getMessage (), Minz_Log::NOTICE);
  223. $feedDAO->updateLastUpdate ($feed->id (), 1);
  224. }
  225. $feed->faviconPrepare();
  226. $feed->unlock();
  227. unset($feed);
  228. // On arrête à 10 flux pour ne pas surcharger le serveur
  229. // sauf si le paramètre $force est à vrai
  230. $i++;
  231. if ($i >= 10 && !$force) {
  232. break;
  233. }
  234. }
  235. $url = array ();
  236. if ($flux_update === 1) {
  237. // on a mis un seul flux à jour
  238. $feed = reset ($feeds);
  239. $notif = array (
  240. 'type' => 'good',
  241. 'content' => Minz_Translate::t ('feed_actualized', $feed->name ())
  242. );
  243. } elseif ($flux_update > 1) {
  244. // plusieurs flux on été mis à jour
  245. $notif = array (
  246. 'type' => 'good',
  247. 'content' => Minz_Translate::t ('n_feeds_actualized', $flux_update)
  248. );
  249. } else {
  250. // aucun flux n'a été mis à jour, oups
  251. $notif = array (
  252. 'type' => 'good',
  253. 'content' => Minz_Translate::t ('no_feed_to_refresh')
  254. );
  255. }
  256. if ($i === 1) {
  257. // Si on a voulu mettre à jour qu'un flux
  258. // on filtre l'affichage par ce flux
  259. $feed = reset ($feeds);
  260. $url['params'] = array ('get' => 'f_' . $feed->id ());
  261. }
  262. if (Minz_Request::param ('ajax', 0) === 0) {
  263. Minz_Session::_param ('notification', $notif);
  264. Minz_Request::forward ($url, true);
  265. } else {
  266. // Une requête Ajax met un seul flux à jour.
  267. // Comme en principe plusieurs requêtes ont lieu,
  268. // on indique que "plusieurs flux ont été mis à jour".
  269. // Cela permet d'avoir une notification plus proche du
  270. // ressenti utilisateur
  271. $notif = array (
  272. 'type' => 'good',
  273. 'content' => Minz_Translate::t ('feeds_actualized')
  274. );
  275. Minz_Session::_param ('notification', $notif);
  276. // et on désactive le layout car ne sert à rien
  277. $this->view->_useLayout (false);
  278. }
  279. }
  280. public function massiveImportAction () {
  281. @set_time_limit(300);
  282. $this->catDAO = new FreshRSS_CategoryDAO ();
  283. $this->catDAO->checkDefault ();
  284. $entryDAO = new FreshRSS_EntryDAO ();
  285. $feedDAO = new FreshRSS_FeedDAO ();
  286. $categories = Minz_Request::param ('categories', array (), true);
  287. $feeds = Minz_Request::param ('feeds', array (), true);
  288. // on ajoute les catégories en masse dans une fonction à part
  289. $this->addCategories ($categories);
  290. // on calcule la date des articles les plus anciens qu'on accepte
  291. $nb_month_old = $this->view->conf->old_entries;
  292. $date_min = time () - (3600 * 24 * 30 * $nb_month_old);
  293. // la variable $error permet de savoir si une erreur est survenue
  294. // Le but est de ne pas arrêter l'import même en cas d'erreur
  295. // L'utilisateur sera mis au courant s'il y a eu des erreurs, mais
  296. // ne connaîtra pas les détails. Ceux-ci seront toutefois logguées
  297. $error = false;
  298. $i = 0;
  299. foreach ($feeds as $feed) {
  300. try {
  301. $values = array (
  302. 'id' => $feed->id (),
  303. 'url' => $feed->url (),
  304. 'category' => $feed->category (),
  305. 'name' => $feed->name (),
  306. 'website' => $feed->website (),
  307. 'description' => $feed->description (),
  308. 'lastUpdate' => 0,
  309. 'httpAuth' => $feed->httpAuth ()
  310. );
  311. // ajout du flux que s'il n'est pas déjà en BDD
  312. if (!$feedDAO->searchByUrl ($values['url'])) {
  313. $id = $feedDAO->addFeed ($values);
  314. if ($id) {
  315. $feed->_id ($id);
  316. $feed->faviconPrepare();
  317. } else {
  318. $error = true;
  319. }
  320. }
  321. } catch (FreshRSS_Feed_Exception $e) {
  322. $error = true;
  323. Minz_Log::record ($e->getMessage (), Minz_Log::WARNING);
  324. }
  325. }
  326. if ($error) {
  327. $res = Minz_Translate::t ('feeds_imported_with_errors');
  328. } else {
  329. $res = Minz_Translate::t ('feeds_imported');
  330. }
  331. $notif = array (
  332. 'type' => 'good',
  333. 'content' => $res
  334. );
  335. Minz_Session::_param ('notification', $notif);
  336. Minz_Session::_param ('actualize_feeds', true);
  337. // et on redirige vers la page d'accueil
  338. Minz_Request::forward (array (
  339. 'c' => 'index',
  340. 'a' => 'index'
  341. ), true);
  342. }
  343. public function deleteAction () {
  344. if (Minz_Request::isPost ()) {
  345. $type = Minz_Request::param ('type', 'feed');
  346. $id = Minz_Request::param ('id');
  347. $feedDAO = new FreshRSS_FeedDAO ();
  348. if ($type == 'category') {
  349. if ($feedDAO->deleteFeedByCategory ($id)) {
  350. $notif = array (
  351. 'type' => 'good',
  352. 'content' => Minz_Translate::t ('category_emptied')
  353. );
  354. //TODO: Delete old favicons
  355. } else {
  356. $notif = array (
  357. 'type' => 'bad',
  358. 'content' => Minz_Translate::t ('error_occured')
  359. );
  360. }
  361. } else {
  362. if ($feedDAO->deleteFeed ($id)) {
  363. $notif = array (
  364. 'type' => 'good',
  365. 'content' => Minz_Translate::t ('feed_deleted')
  366. );
  367. //TODO: Delete old favicon
  368. } else {
  369. $notif = array (
  370. 'type' => 'bad',
  371. 'content' => Minz_Translate::t ('error_occured')
  372. );
  373. }
  374. }
  375. Minz_Session::_param ('notification', $notif);
  376. if ($type == 'category') {
  377. Minz_Request::forward (array ('c' => 'configure', 'a' => 'categorize'), true);
  378. } else {
  379. Minz_Request::forward (array ('c' => 'configure', 'a' => 'feed'), true);
  380. }
  381. }
  382. }
  383. private function addCategories ($categories) {
  384. foreach ($categories as $cat) {
  385. if (!$this->catDAO->searchByName ($cat->name ())) {
  386. $values = array (
  387. 'id' => $cat->id (),
  388. 'name' => $cat->name (),
  389. );
  390. $catDAO->addCategory ($values);
  391. }
  392. }
  393. }
  394. }