feedController.php 13 KB

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