feedController.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670
  1. <?php
  2. /**
  3. * Controller to handle every feed actions.
  4. */
  5. class FreshRSS_feed_Controller extends Minz_ActionController {
  6. /**
  7. * This action is called before every other action in that class. It is
  8. * the common boiler plate for every action. It is triggered by the
  9. * underlying framework.
  10. */
  11. public function firstAction() {
  12. if (!FreshRSS_Auth::hasAccess()) {
  13. // Token is useful in the case that anonymous refresh is forbidden
  14. // and CRON task cannot be used with php command so the user can
  15. // set a CRON task to refresh his feeds by using token inside url
  16. $token = FreshRSS_Context::$user_conf->token;
  17. $token_param = Minz_Request::param('token', '');
  18. $token_is_ok = ($token != '' && $token == $token_param);
  19. $action = Minz_Request::actionName();
  20. $allow_anonymous_refresh = FreshRSS_Context::$system_conf->allow_anonymous_refresh;
  21. if ($action !== 'actualize' ||
  22. !($allow_anonymous_refresh || $token_is_ok)) {
  23. Minz_Error::error(403);
  24. }
  25. }
  26. $this->updateTTL();
  27. }
  28. /**
  29. * @param $url
  30. * @param string $title
  31. * @param int $cat_id
  32. * @param string $new_cat_name
  33. * @param string $http_auth
  34. * @return FreshRSS_Feed|the
  35. * @throws FreshRSS_AlreadySubscribed_Exception
  36. * @throws FreshRSS_FeedNotAdded_Exception
  37. * @throws FreshRSS_Feed_Exception
  38. * @throws Minz_FileNotExistException
  39. */
  40. public static function addFeed($url, $title = '', $cat_id = 0, $new_cat_name = '', $http_auth = '') {
  41. FreshRSS_UserDAO::touch();
  42. @set_time_limit(300);
  43. $catDAO = FreshRSS_Factory::createCategoryDao();
  44. $url = trim($url);
  45. $cat = null;
  46. if ($new_cat_name != '') {
  47. $new_cat_id = $catDAO->addCategory(array('name' => $new_cat_name));
  48. $cat_id = $new_cat_id > 0 ? $new_cat_id : $cat_id;
  49. }
  50. if ($cat_id > 0) {
  51. $cat = $catDAO->searchById($cat_id);
  52. }
  53. if ($cat == null) {
  54. $catDAO->checkDefault();
  55. }
  56. $cat_id = $cat == null ? FreshRSS_CategoryDAO::DEFAULTCATEGORYID : $cat->id();
  57. $feed = new FreshRSS_Feed($url); //Throws FreshRSS_BadUrl_Exception
  58. $feed->_httpAuth($http_auth);
  59. $feed->load(true); //Throws FreshRSS_Feed_Exception, Minz_FileNotExistException
  60. $feed->_category($cat_id);
  61. $feedDAO = FreshRSS_Factory::createFeedDao();
  62. if ($feedDAO->searchByUrl($feed->url())) {
  63. throw new FreshRSS_AlreadySubscribed_Exception($url, $feed->name());
  64. }
  65. /** @var FreshRSS_Feed $feed */
  66. $feed = Minz_ExtensionManager::callHook('feed_before_insert', $feed);
  67. if ($feed === null) {
  68. throw new FreshRSS_FeedNotAdded_Exception($url, $feed->name());
  69. }
  70. $values = array(
  71. 'url' => $feed->url(),
  72. 'category' => $feed->category(),
  73. 'name' => $title != '' ? $title : $feed->name(),
  74. 'website' => $feed->website(),
  75. 'description' => $feed->description(),
  76. 'lastUpdate' => time(),
  77. 'httpAuth' => $feed->httpAuth(),
  78. 'attributes' => array(),
  79. );
  80. $id = $feedDAO->addFeed($values);
  81. if (!$id) {
  82. // There was an error in database... we cannot say what here.
  83. throw new FreshRSS_FeedNotAdded_Exception($url, $feed->name());
  84. }
  85. $feed->_id($id);
  86. // Ok, feed has been added in database. Now we have to refresh entries.
  87. self::actualizeFeed($id, $url, false, null, true);
  88. return $feed;
  89. }
  90. /**
  91. * This action subscribes to a feed.
  92. *
  93. * It can be reached by both GET and POST requests.
  94. *
  95. * GET request displays a form to add and configure a feed.
  96. * Request parameter is:
  97. * - url_rss (default: false)
  98. *
  99. * POST request adds a feed in database.
  100. * Parameters are:
  101. * - url_rss (default: false)
  102. * - category (default: false)
  103. * - new_category (required if category == 'nc')
  104. * - http_user (default: false)
  105. * - http_pass (default: false)
  106. * It tries to get website information from RSS feed.
  107. * If no category is given, feed is added to the default one.
  108. *
  109. * If url_rss is false, nothing happened.
  110. */
  111. public function addAction() {
  112. $url = Minz_Request::param('url_rss');
  113. if ($url === false) {
  114. // No url, do nothing
  115. Minz_Request::forward(array(
  116. 'c' => 'subscription',
  117. 'a' => 'index'
  118. ), true);
  119. }
  120. $feedDAO = FreshRSS_Factory::createFeedDao();
  121. $url_redirect = array(
  122. 'c' => 'subscription',
  123. 'a' => 'index',
  124. 'params' => array(),
  125. );
  126. $limits = FreshRSS_Context::$system_conf->limits;
  127. $this->view->feeds = $feedDAO->listFeeds();
  128. if (count($this->view->feeds) >= $limits['max_feeds']) {
  129. Minz_Request::bad(_t('feedback.sub.feed.over_max', $limits['max_feeds']),
  130. $url_redirect);
  131. }
  132. if (Minz_Request::isPost()) {
  133. $cat = Minz_Request::param('category');
  134. $new_cat_name = '';
  135. if ($cat === 'nc') {
  136. // User want to create a new category, new_category parameter
  137. // must exist
  138. $new_cat = Minz_Request::param('new_category');
  139. $new_cat_name = isset($new_cat['name']) ? trim($new_cat['name']) : '';
  140. }
  141. // HTTP information are useful if feed is protected behind a
  142. // HTTP authentication
  143. $user = trim(Minz_Request::param('http_user', ''));
  144. $pass = Minz_Request::param('http_pass', '');
  145. $http_auth = '';
  146. if ($user != '' && $pass != '') { //TODO: Sanitize
  147. $http_auth = $user . ':' . $pass;
  148. }
  149. try {
  150. $feed = self::addFeed($url, '', $cat, $new_cat_name, $http_auth);
  151. } catch (FreshRSS_BadUrl_Exception $e) {
  152. // Given url was not a valid url!
  153. Minz_Log::warning($e->getMessage());
  154. Minz_Request::bad(_t('feedback.sub.feed.invalid_url', $url), $url_redirect);
  155. } catch (FreshRSS_Feed_Exception $e) {
  156. // Something went bad (timeout, server not found, etc.)
  157. Minz_Log::warning($e->getMessage());
  158. Minz_Request::bad(_t('feedback.sub.feed.internal_problem', _url('index', 'logs')), $url_redirect);
  159. } catch (Minz_FileNotExistException $e) {
  160. // Cache directory doesn't exist!
  161. Minz_Log::error($e->getMessage());
  162. Minz_Request::bad(_t('feedback.sub.feed.internal_problem', _url('index', 'logs')), $url_redirect);
  163. } catch (FreshRSS_AlreadySubscribed_Exception $e) {
  164. Minz_Request::bad(_t('feedback.sub.feed.already_subscribed', $e->feedName()), $url_redirect);
  165. } catch (FreshRSS_FeedNotAdded_Exception $e) {
  166. Minz_Request::bad(_t('feedback.sub.feed.not_added', $e->feedName()), $url_redirect);
  167. }
  168. // Entries are in DB, we redirect to feed configuration page.
  169. $url_redirect['params']['id'] = $feed->id();
  170. Minz_Request::good(_t('feedback.sub.feed.added', $feed->name()), $url_redirect);
  171. } else {
  172. // GET request: we must ask confirmation to user before adding feed.
  173. Minz_View::prependTitle(_t('sub.feed.title_add') . ' · ');
  174. $this->catDAO = FreshRSS_Factory::createCategoryDao();
  175. $this->view->categories = $this->catDAO->listCategories(false);
  176. $this->view->feed = new FreshRSS_Feed($url);
  177. try {
  178. // We try to get more information about the feed.
  179. $this->view->feed->load(true);
  180. $this->view->load_ok = true;
  181. } catch (Exception $e) {
  182. $this->view->load_ok = false;
  183. }
  184. $feed = $feedDAO->searchByUrl($this->view->feed->url());
  185. if ($feed) {
  186. // Already subscribe so we redirect to the feed configuration page.
  187. $url_redirect['params']['id'] = $feed->id();
  188. Minz_Request::good(_t('feedback.sub.feed.already_subscribed', $feed->name()), $url_redirect);
  189. }
  190. }
  191. }
  192. /**
  193. * This action remove entries from a given feed.
  194. *
  195. * It should be reached by a POST action.
  196. *
  197. * Parameter is:
  198. * - id (default: false)
  199. */
  200. public function truncateAction() {
  201. $id = Minz_Request::param('id');
  202. $url_redirect = array(
  203. 'c' => 'subscription',
  204. 'a' => 'index',
  205. 'params' => array('id' => $id)
  206. );
  207. if (!Minz_Request::isPost()) {
  208. Minz_Request::forward($url_redirect, true);
  209. }
  210. $feedDAO = FreshRSS_Factory::createFeedDao();
  211. $n = $feedDAO->truncate($id);
  212. invalidateHttpCache();
  213. if ($n === false) {
  214. Minz_Request::bad(_t('feedback.sub.feed.error'), $url_redirect);
  215. } else {
  216. Minz_Request::good(_t('feedback.sub.feed.n_entries_deleted', $n), $url_redirect);
  217. }
  218. }
  219. public static function actualizeFeed($feed_id, $feed_url, $force, $simplePiePush = null, $isNewFeed = false, $noCommit = false) {
  220. @set_time_limit(300);
  221. $feedDAO = FreshRSS_Factory::createFeedDao();
  222. $entryDAO = FreshRSS_Factory::createEntryDao();
  223. // Create a list of feeds to actualize.
  224. // If feed_id is set and valid, corresponding feed is added to the list but
  225. // alone in order to automatize further process.
  226. $feeds = array();
  227. if ($feed_id > 0 || $feed_url) {
  228. $feed = $feed_id > 0 ? $feedDAO->searchById($feed_id) : $feedDAO->searchByUrl($feed_url);
  229. if ($feed) {
  230. $feeds[] = $feed;
  231. }
  232. } else {
  233. $feeds = $feedDAO->listFeedsOrderUpdate(-1);
  234. }
  235. // Calculate date of oldest entries we accept in DB.
  236. $nb_month_old = max(FreshRSS_Context::$user_conf->old_entries, 1);
  237. $date_min = time() - (3600 * 24 * 30 * $nb_month_old);
  238. // WebSub (PubSubHubbub) support
  239. $pubsubhubbubEnabledGeneral = FreshRSS_Context::$system_conf->pubsubhubbub_enabled;
  240. $pshbMinAge = time() - (3600 * 24); //TODO: Make a configuration.
  241. $updated_feeds = 0;
  242. $nb_new_articles = 0;
  243. foreach ($feeds as $feed) {
  244. $url = $feed->url(); //For detection of HTTP 301
  245. $pubSubHubbubEnabled = $pubsubhubbubEnabledGeneral && $feed->pubSubHubbubEnabled();
  246. if ((!$simplePiePush) && (!$feed_id) && $pubSubHubbubEnabled && ($feed->lastUpdate() > $pshbMinAge)) {
  247. //$text = 'Skip pull of feed using PubSubHubbub: ' . $url;
  248. //Minz_Log::debug($text);
  249. //Minz_Log::debug($text, PSHB_LOG);
  250. continue; //When PubSubHubbub is used, do not pull refresh so often
  251. }
  252. $mtime = 0;
  253. if ($feed->mute()) {
  254. continue; //Feed refresh is disabled
  255. }
  256. $ttl = $feed->ttl();
  257. if ((!$simplePiePush) && (!$feed_id) &&
  258. ($feed->lastUpdate() + 10 >= time() - ($ttl == FreshRSS_Feed::TTL_DEFAULT ? FreshRSS_Context::$user_conf->ttl_default : $ttl))) {
  259. //Too early to refresh from source, but check whether the feed was updated by another user
  260. $mtime = $feed->cacheModifiedTime();
  261. if ($feed->lastUpdate() + 10 >= $mtime) {
  262. continue; //Nothing newer from other users
  263. }
  264. //Minz_Log::debug($feed->url(false) . ' was updated at ' . date('c', $mtime) . ' by another user');
  265. //Will take advantage of the newer cache
  266. }
  267. if (!$feed->lock()) {
  268. Minz_Log::notice('Feed already being actualized: ' . $feed->url(false));
  269. continue;
  270. }
  271. try {
  272. if ($simplePiePush) {
  273. $feed->loadEntries($simplePiePush); //Used by PubSubHubbub
  274. } else {
  275. $feed->load(false, $isNewFeed);
  276. }
  277. } catch (FreshRSS_Feed_Exception $e) {
  278. Minz_Log::warning($e->getMessage());
  279. $feedDAO->updateLastUpdate($feed->id(), true);
  280. $feed->unlock();
  281. continue;
  282. }
  283. $feed_history = $feed->keepHistory();
  284. if ($isNewFeed) {
  285. $feed_history = FreshRSS_Feed::KEEP_HISTORY_INFINITE;
  286. } elseif (FreshRSS_Feed::KEEP_HISTORY_DEFAULT === $feed_history) {
  287. $feed_history = FreshRSS_Context::$user_conf->keep_history_default;
  288. }
  289. $needFeedCacheRefresh = false;
  290. // We want chronological order and SimplePie uses reverse order.
  291. $entries = array_reverse($feed->entries());
  292. if (count($entries) > 0) {
  293. $newGuids = array();
  294. foreach ($entries as $entry) {
  295. $newGuids[] = safe_ascii($entry->guid());
  296. }
  297. // For this feed, check existing GUIDs already in database.
  298. $existingHashForGuids = $entryDAO->listHashForFeedGuids($feed->id(), $newGuids);
  299. $newGuids = array();
  300. $oldGuids = array();
  301. // Add entries in database if possible.
  302. foreach ($entries as $entry) {
  303. if (isset($newGuids[$entry->guid()])) {
  304. continue; //Skip subsequent articles with same GUID
  305. }
  306. $newGuids[$entry->guid()] = true;
  307. $entry_date = $entry->date(true);
  308. if (isset($existingHashForGuids[$entry->guid()])) {
  309. $existingHash = $existingHashForGuids[$entry->guid()];
  310. if (strcasecmp($existingHash, $entry->hash()) === 0 || trim($existingHash, '0') == '') {
  311. //This entry already exists and is unchanged. TODO: Remove the test with the zero'ed hash in FreshRSS v1.3
  312. $oldGuids[] = $entry->guid();
  313. } else { //This entry already exists but has been updated
  314. //Minz_Log::debug('Entry with GUID `' . $entry->guid() . '` updated in feed ' . $feed->url(false) .
  315. //', old hash ' . $existingHash . ', new hash ' . $entry->hash());
  316. $mark_updated_article_unread = $feed->attributes('mark_updated_article_unread') !== null ? (
  317. $feed->attributes('mark_updated_article_unread')
  318. ) : FreshRSS_Context::$user_conf->mark_updated_article_unread;
  319. $needFeedCacheRefresh = $mark_updated_article_unread;
  320. $entry->_isRead(FreshRSS_Context::$user_conf->mark_updated_article_unread ? false : null); //Change is_read according to policy.
  321. $entry = Minz_ExtensionManager::callHook('entry_before_insert', $entry);
  322. if ($entry === null) {
  323. // An extension has returned a null value, there is nothing to insert.
  324. continue;
  325. }
  326. if (!$entryDAO->inTransaction()) {
  327. $entryDAO->beginTransaction();
  328. }
  329. $entryDAO->updateEntry($entry->toArray());
  330. }
  331. } elseif ($feed_history == 0 && $entry_date < $date_min) {
  332. // This entry should not be added considering configuration and date.
  333. $oldGuids[] = $entry->guid();
  334. } else {
  335. $read_upon_reception = $feed->attributes('read_upon_reception') !== null ? (
  336. $feed->attributes('read_upon_reception')
  337. ) : FreshRSS_Context::$user_conf->mark_when['reception'];
  338. if ($isNewFeed) {
  339. $id = min(time(), $entry_date) . uSecString();
  340. $entry->_isRead($read_upon_reception);
  341. } elseif ($entry_date < $date_min) {
  342. $id = min(time(), $entry_date) . uSecString();
  343. $entry->_isRead(true); //Old article that was not in database. Probably an error, so mark as read
  344. } else {
  345. $id = uTimeString();
  346. $entry->_isRead($read_upon_reception);
  347. }
  348. $entry->_id($id);
  349. $entry = Minz_ExtensionManager::callHook('entry_before_insert', $entry);
  350. if ($entry === null) {
  351. // An extension has returned a null value, there is nothing to insert.
  352. continue;
  353. }
  354. if ($pubSubHubbubEnabled && !$simplePiePush) { //We use push, but have discovered an article by pull!
  355. $text = 'An article was discovered by pull although we use PubSubHubbub!: Feed ' . $url . ' GUID ' . $entry->guid();
  356. Minz_Log::warning($text, PSHB_LOG);
  357. Minz_Log::warning($text);
  358. $pubSubHubbubEnabled = false;
  359. $feed->pubSubHubbubError(true);
  360. }
  361. if (!$entryDAO->inTransaction()) {
  362. $entryDAO->beginTransaction();
  363. }
  364. $entryDAO->addEntry($entry->toArray());
  365. $nb_new_articles++;
  366. }
  367. }
  368. $entryDAO->updateLastSeen($feed->id(), $oldGuids, $mtime);
  369. }
  370. if ($feed_history >= 0 && mt_rand(0, 30) === 1) {
  371. // TODO: move this function in web cron when available (see entry::purge)
  372. // Remove old entries once in 30.
  373. if (!$entryDAO->inTransaction()) {
  374. $entryDAO->beginTransaction();
  375. }
  376. $nb = $entryDAO->cleanOldEntries($feed->id(),
  377. $date_min,
  378. max($feed_history, count($entries) + 10));
  379. if ($nb > 0) {
  380. $needFeedCacheRefresh = true;
  381. Minz_Log::debug($nb . ' old entries cleaned in feed [' . $feed->url(false) . ']');
  382. }
  383. }
  384. $feedDAO->updateLastUpdate($feed->id(), false, $mtime);
  385. if ($needFeedCacheRefresh) {
  386. $feedDAO->updateCachedValue($feed->id());
  387. }
  388. if ($entryDAO->inTransaction()) {
  389. $entryDAO->commit();
  390. }
  391. if ($feed->hubUrl() && $feed->selfUrl()) { //selfUrl has priority for WebSub
  392. if ($feed->selfUrl() !== $url) { //https://code.google.com/p/pubsubhubbub/wiki/MovingFeedsOrChangingHubs
  393. $selfUrl = checkUrl($feed->selfUrl());
  394. if ($selfUrl) {
  395. Minz_Log::debug('WebSub unsubscribe ' . $feed->url(false));
  396. if (!$feed->pubSubHubbubSubscribe(false)) { //Unsubscribe
  397. Minz_Log::warning('Error while WebSub unsubscribing from ' . $feed->url(false));
  398. }
  399. $feed->_url($selfUrl, false);
  400. Minz_Log::notice('Feed ' . $url . ' canonical address moved to ' . $feed->url(false));
  401. $feedDAO->updateFeed($feed->id(), array('url' => $feed->url()));
  402. }
  403. }
  404. } elseif ($feed->url() !== $url) { // HTTP 301 Moved Permanently
  405. Minz_Log::notice('Feed ' . $url . ' moved permanently to ' . $feed->url(false));
  406. $feedDAO->updateFeed($feed->id(), array('url' => $feed->url()));
  407. }
  408. $feed->faviconPrepare();
  409. if ($pubsubhubbubEnabledGeneral && $feed->pubSubHubbubPrepare()) {
  410. Minz_Log::notice('WebSub subscribe ' . $feed->url(false));
  411. if (!$feed->pubSubHubbubSubscribe(true)) { //Subscribe
  412. Minz_Log::warning('Error while WebSub subscribing to ' . $feed->url(false));
  413. }
  414. }
  415. $feed->unlock();
  416. $updated_feeds++;
  417. unset($feed);
  418. // No more than 10 feeds unless $force is true to avoid overloading
  419. // the server.
  420. if ($updated_feeds >= 10 && !$force) {
  421. break;
  422. }
  423. }
  424. if (!$noCommit) {
  425. if (!$entryDAO->inTransaction()) {
  426. $entryDAO->beginTransaction();
  427. }
  428. $entryDAO->commitNewEntries();
  429. $feedDAO->updateCachedValues();
  430. if ($entryDAO->inTransaction()) {
  431. $entryDAO->commit();
  432. }
  433. $databaseDAO = FreshRSS_Factory::createDatabaseDAO();
  434. $databaseDAO->minorDbMaintenance();
  435. }
  436. return array($updated_feeds, reset($feeds), $nb_new_articles);
  437. }
  438. /**
  439. * This action actualizes entries from one or several feeds.
  440. *
  441. * Parameters are:
  442. * - id (default: false): Feed ID
  443. * - url (default: false): Feed URL
  444. * - force (default: false)
  445. * - noCommit (default: 0): Set to 1 to prevent committing the new articles to the main database
  446. * If id and url are not specified, all the feeds are actualized. But if force is
  447. * false, process stops at 10 feeds to avoid time execution problem.
  448. */
  449. public function actualizeAction() {
  450. Minz_Session::_param('actualize_feeds', false);
  451. $id = Minz_Request::param('id');
  452. $url = Minz_Request::param('url');
  453. $force = Minz_Request::param('force');
  454. $noCommit = Minz_Request::fetchPOST('noCommit', 0) == 1;
  455. if ($id == -1 && !$noCommit) { //Special request only to commit & refresh DB cache
  456. $updated_feeds = 0;
  457. $entryDAO = FreshRSS_Factory::createEntryDao();
  458. $feedDAO = FreshRSS_Factory::createFeedDao();
  459. $entryDAO->beginTransaction();
  460. $entryDAO->commitNewEntries();
  461. $feedDAO->updateCachedValues();
  462. $entryDAO->commit();
  463. $databaseDAO = FreshRSS_Factory::createDatabaseDAO();
  464. $databaseDAO->minorDbMaintenance();
  465. } else {
  466. list($updated_feeds, $feed, $nb_new_articles) = self::actualizeFeed($id, $url, $force, null, false, $noCommit);
  467. }
  468. if (Minz_Request::param('ajax')) {
  469. // Most of the time, ajax request is for only one feed. But since
  470. // there are several parallel requests, we should return that there
  471. // are several updated feeds.
  472. $notif = array(
  473. 'type' => 'good',
  474. 'content' => _t('feedback.sub.feed.actualizeds')
  475. );
  476. Minz_Session::_param('notification', $notif);
  477. // No layout in ajax request.
  478. $this->view->_useLayout(false);
  479. } else {
  480. // Redirect to the main page with correct notification.
  481. if ($updated_feeds === 1) {
  482. Minz_Request::good(_t('feedback.sub.feed.actualized', $feed->name()), array(
  483. 'params' => array('get' => 'f_' . $feed->id())
  484. ));
  485. } elseif ($updated_feeds > 1) {
  486. Minz_Request::good(_t('feedback.sub.feed.n_actualized', $updated_feeds), array());
  487. } else {
  488. Minz_Request::good(_t('feedback.sub.feed.no_refresh'), array());
  489. }
  490. }
  491. return $updated_feeds;
  492. }
  493. public static function renameFeed($feed_id, $feed_name) {
  494. if ($feed_id <= 0 || $feed_name == '') {
  495. return false;
  496. }
  497. FreshRSS_UserDAO::touch();
  498. $feedDAO = FreshRSS_Factory::createFeedDao();
  499. return $feedDAO->updateFeed($feed_id, array('name' => $feed_name));
  500. }
  501. public static function moveFeed($feed_id, $cat_id, $new_cat_name = '') {
  502. if ($feed_id <= 0 || ($cat_id <= 0 && $new_cat_name == '')) {
  503. return false;
  504. }
  505. FreshRSS_UserDAO::touch();
  506. $catDAO = FreshRSS_Factory::createCategoryDao();
  507. if ($cat_id > 0) {
  508. $cat = $catDAO->searchById($cat_id);
  509. $cat_id = $cat == null ? 0 : $cat->id();
  510. }
  511. if ($cat_id <= 1 && $new_cat_name != '') {
  512. $cat_id = $catDAO->addCategory(array('name' => $new_cat_name));
  513. }
  514. if ($cat_id <= 1) {
  515. $catDAO->checkDefault();
  516. $cat_id = FreshRSS_CategoryDAO::DEFAULTCATEGORYID;
  517. }
  518. $feedDAO = FreshRSS_Factory::createFeedDao();
  519. return $feedDAO->updateFeed($feed_id, array('category' => $cat_id));
  520. }
  521. /**
  522. * This action changes the category of a feed.
  523. *
  524. * This page must be reached by a POST request.
  525. *
  526. * Parameters are:
  527. * - f_id (default: false)
  528. * - c_id (default: false)
  529. * If c_id is false, default category is used.
  530. *
  531. * @todo should handle order of the feed inside the category.
  532. */
  533. public function moveAction() {
  534. if (!Minz_Request::isPost()) {
  535. Minz_Request::forward(array('c' => 'subscription'), true);
  536. }
  537. $feed_id = Minz_Request::param('f_id');
  538. $cat_id = Minz_Request::param('c_id');
  539. if (self::moveFeed($feed_id, $cat_id)) {
  540. // TODO: return something useful
  541. // Log a notice to prevent "Empty IF statement" warning in PHP_CodeSniffer
  542. Minz_Log::notice('Moved feed `' . $feed_id . '` ' .
  543. 'in the category `' . $cat_id . '`');;
  544. } else {
  545. Minz_Log::warning('Cannot move feed `' . $feed_id . '` ' .
  546. 'in the category `' . $cat_id . '`');
  547. Minz_Error::error(404);
  548. }
  549. }
  550. public static function deleteFeed($feed_id) {
  551. FreshRSS_UserDAO::touch();
  552. $feedDAO = FreshRSS_Factory::createFeedDao();
  553. if ($feedDAO->deleteFeed($feed_id)) {
  554. // TODO: Delete old favicon
  555. // Remove related queries
  556. FreshRSS_Context::$user_conf->queries = remove_query_by_get(
  557. 'f_' . $feed_id, FreshRSS_Context::$user_conf->queries);
  558. FreshRSS_Context::$user_conf->save();
  559. return true;
  560. }
  561. return false;
  562. }
  563. /**
  564. * This action deletes a feed.
  565. *
  566. * This page must be reached by a POST request.
  567. * If there are related queries, they are deleted too.
  568. *
  569. * Parameters are:
  570. * - id (default: false)
  571. * - r (default: false)
  572. * r permits to redirect to a given page at the end of this action.
  573. *
  574. * @todo handle "r" redirection in Minz_Request::forward()?
  575. */
  576. public function deleteAction() {
  577. $redirect_url = Minz_Request::param('r', false, true);
  578. if (!$redirect_url) {
  579. $redirect_url = array('c' => 'subscription', 'a' => 'index');
  580. }
  581. if (!Minz_Request::isPost()) {
  582. Minz_Request::forward($redirect_url, true);
  583. }
  584. $id = Minz_Request::param('id');
  585. if (self::deleteFeed($id)) {
  586. Minz_Request::good(_t('feedback.sub.feed.deleted'), $redirect_url);
  587. } else {
  588. Minz_Request::bad(_t('feedback.sub.feed.error'), $redirect_url);
  589. }
  590. }
  591. /**
  592. * This method update TTL values for feeds if needed.
  593. * It changes the old default value (-2) to the new default value (0).
  594. * It changes the old disabled value (-1) to the default disabled value.
  595. */
  596. private function updateTTL() {
  597. $feedDAO = FreshRSS_Factory::createFeedDao();
  598. $feedDAO->updateTTL();
  599. }
  600. }