feedController.php 12 KB

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