feedController.php 13 KB

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