feedController.php 22 KB

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