feedController.php 21 KB

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