subscriptionController.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * Controller to handle subscription actions.
  5. */
  6. class FreshRSS_subscription_Controller extends FreshRSS_ActionController {
  7. /**
  8. * This action is called before every other action in that class. It is
  9. * the common boilerplate for every action. It is triggered by the
  10. * underlying framework.
  11. */
  12. public function firstAction(): void {
  13. if (!FreshRSS_Auth::hasAccess()) {
  14. Minz_Error::error(403);
  15. }
  16. $catDAO = FreshRSS_Factory::createCategoryDao();
  17. $catDAO->checkDefault();
  18. $this->view->categories = $catDAO->listSortedCategories(false, true) ?: [];
  19. $this->view->default_category = $catDAO->getDefault();
  20. $signalError = false;
  21. foreach ($this->view->categories as $cat) {
  22. $feeds = $cat->feeds();
  23. foreach ($feeds as $feed) {
  24. if ($feed->inError()) {
  25. $signalError = true;
  26. }
  27. }
  28. if ($signalError) {
  29. break;
  30. }
  31. }
  32. $this->view->signalError = $signalError;
  33. }
  34. /**
  35. * This action handles the main subscription page
  36. *
  37. * It displays categories and associated feeds.
  38. */
  39. public function indexAction(): void {
  40. FreshRSS_View::appendScript(Minz_Url::display('/scripts/category.js?' . @filemtime(PUBLIC_PATH . '/scripts/category.js')));
  41. FreshRSS_View::appendScript(Minz_Url::display('/scripts/feed.js?' . @filemtime(PUBLIC_PATH . '/scripts/feed.js')));
  42. FreshRSS_View::prependTitle(_t('sub.title') . ' · ');
  43. $this->view->onlyFeedsWithError = Minz_Request::paramBoolean('error');
  44. $id = Minz_Request::paramInt('id');
  45. $this->view->displaySlider = false;
  46. if ($id !== 0) {
  47. $type = Minz_Request::paramString('type');
  48. $this->view->displaySlider = true;
  49. switch ($type) {
  50. case 'category':
  51. $categoryDAO = FreshRSS_Factory::createCategoryDao();
  52. $this->view->category = $categoryDAO->searchById($id);
  53. break;
  54. default:
  55. $feedDAO = FreshRSS_Factory::createFeedDao();
  56. $this->view->feed = $feedDAO->searchById($id);
  57. break;
  58. }
  59. }
  60. }
  61. /**
  62. * This action handles the feed configuration page.
  63. *
  64. * It displays the feed configuration page.
  65. * If this action is reached through a POST request, it stores all new
  66. * configuration values then sends a notification to the user.
  67. *
  68. * The options available on the page are:
  69. * - name
  70. * - description
  71. * - website URL
  72. * - feed URL
  73. * - category id (default: default category id)
  74. * - CSS path to article on website
  75. * - display in main stream (default: 0)
  76. * - HTTP authentication
  77. * - number of article to retain (default: -2)
  78. * - refresh frequency (default: 0)
  79. * Default values are empty strings unless specified.
  80. */
  81. public function feedAction(): void {
  82. if (Minz_Request::paramBoolean('ajax')) {
  83. $this->view->_layout(null);
  84. } else {
  85. FreshRSS_View::appendScript(Minz_Url::display('/scripts/feed.js?' . @filemtime(PUBLIC_PATH . '/scripts/feed.js')));
  86. }
  87. $feedDAO = FreshRSS_Factory::createFeedDao();
  88. $this->view->feeds = $feedDAO->listFeeds();
  89. $id = Minz_Request::paramInt('id');
  90. if ($id === 0 || !isset($this->view->feeds[$id])) {
  91. Minz_Error::error(404);
  92. return;
  93. }
  94. $feed = $this->view->feeds[$id];
  95. $this->view->feed = $feed;
  96. FreshRSS_View::prependTitle($feed->name() . ' · ' . _t('sub.title.feed_management') . ' · ');
  97. if (Minz_Request::isPost()) {
  98. $user = Minz_Request::paramString('http_user_feed' . $id);
  99. $pass = Minz_Request::paramString('http_pass_feed' . $id);
  100. $httpAuth = '';
  101. if ($user !== '' && $pass !== '') { //TODO: Sanitize
  102. $httpAuth = $user . ':' . $pass;
  103. }
  104. $feed->_ttl(Minz_Request::paramInt('ttl') ?: FreshRSS_Feed::TTL_DEFAULT);
  105. $feed->_mute(Minz_Request::paramBoolean('mute'));
  106. $feed->_attributes('read_upon_gone', Minz_Request::paramTernary('read_upon_gone'));
  107. $feed->_attributes('mark_updated_article_unread', Minz_Request::paramTernary('mark_updated_article_unread'));
  108. $feed->_attributes('read_upon_reception', Minz_Request::paramTernary('read_upon_reception'));
  109. $feed->_attributes('clear_cache', Minz_Request::paramTernary('clear_cache'));
  110. $keep_max_n_unread = Minz_Request::paramTernary('keep_max_n_unread') === true ? Minz_Request::paramInt('keep_max_n_unread') : null;
  111. $feed->_attributes('keep_max_n_unread', $keep_max_n_unread >= 0 ? $keep_max_n_unread : null);
  112. $read_when_same_title_in_feed = Minz_Request::paramString('read_when_same_title_in_feed');
  113. if ($read_when_same_title_in_feed === '') {
  114. $read_when_same_title_in_feed = null;
  115. } else {
  116. $read_when_same_title_in_feed = (int)$read_when_same_title_in_feed;
  117. if ($read_when_same_title_in_feed <= 0) {
  118. $read_when_same_title_in_feed = false;
  119. }
  120. }
  121. $feed->_attributes('read_when_same_title_in_feed', $read_when_same_title_in_feed);
  122. $cookie = Minz_Request::paramString('curl_params_cookie');
  123. $cookie_file = Minz_Request::paramBoolean('curl_params_cookiefile');
  124. $max_redirs = Minz_Request::paramInt('curl_params_redirects');
  125. $useragent = Minz_Request::paramString('curl_params_useragent');
  126. $proxy_address = Minz_Request::paramString('curl_params');
  127. $proxy_type = Minz_Request::paramString('proxy_type');
  128. $opts = [];
  129. if ($proxy_type !== '') {
  130. $opts[CURLOPT_PROXY] = $proxy_address;
  131. $opts[CURLOPT_PROXYTYPE] = (int)$proxy_type;
  132. }
  133. if ($cookie !== '') {
  134. $opts[CURLOPT_COOKIE] = $cookie;
  135. }
  136. if ($cookie_file) {
  137. // Pass empty cookie file name to enable the libcurl cookie engine
  138. // without reading any existing cookie data.
  139. $opts[CURLOPT_COOKIEFILE] = '';
  140. }
  141. if ($max_redirs != 0) {
  142. $opts[CURLOPT_MAXREDIRS] = $max_redirs;
  143. $opts[CURLOPT_FOLLOWLOCATION] = 1;
  144. }
  145. if ($useragent !== '') {
  146. $opts[CURLOPT_USERAGENT] = $useragent;
  147. }
  148. $feed->_attributes('curl_params', empty($opts) ? null : $opts);
  149. $feed->_attributes('content_action', Minz_Request::paramString('content_action', true) ?: 'replace');
  150. $feed->_attributes('ssl_verify', Minz_Request::paramTernary('ssl_verify'));
  151. $timeout = Minz_Request::paramInt('timeout');
  152. $feed->_attributes('timeout', $timeout > 0 ? $timeout : null);
  153. if (Minz_Request::paramBoolean('use_default_purge_options')) {
  154. $feed->_attributes('archiving', null);
  155. } else {
  156. if (Minz_Request::paramBoolean('enable_keep_max')) {
  157. $keepMax = Minz_Request::paramInt('keep_max') ?: FreshRSS_Feed::ARCHIVING_RETENTION_COUNT_LIMIT;
  158. } else {
  159. $keepMax = false;
  160. }
  161. if (Minz_Request::paramBoolean('enable_keep_period')) {
  162. $keepPeriod = FreshRSS_Feed::ARCHIVING_RETENTION_PERIOD;
  163. if (is_numeric(Minz_Request::paramString('keep_period_count')) && preg_match('/^PT?1[YMWDH]$/', Minz_Request::paramString('keep_period_unit'))) {
  164. $keepPeriod = str_replace('1', Minz_Request::paramString('keep_period_count'), Minz_Request::paramString('keep_period_unit'));
  165. }
  166. } else {
  167. $keepPeriod = false;
  168. }
  169. $feed->_attributes('archiving', [
  170. 'keep_period' => $keepPeriod,
  171. 'keep_max' => $keepMax,
  172. 'keep_min' => Minz_Request::paramInt('keep_min'),
  173. 'keep_favourites' => Minz_Request::paramBoolean('keep_favourites'),
  174. 'keep_labels' => Minz_Request::paramBoolean('keep_labels'),
  175. 'keep_unreads' => Minz_Request::paramBoolean('keep_unreads'),
  176. ]);
  177. }
  178. $feed->_filtersAction('read', preg_split('/[\n\r]+/', Minz_Request::paramString('filteractions_read')) ?: []);
  179. $feed->_kind(Minz_Request::paramInt('feed_kind') ?: FreshRSS_Feed::KIND_RSS);
  180. if ($feed->kind() === FreshRSS_Feed::KIND_HTML_XPATH || $feed->kind() === FreshRSS_Feed::KIND_XML_XPATH) {
  181. $xPathSettings = [];
  182. if (Minz_Request::paramString('xPathItem') != '')
  183. $xPathSettings['item'] = Minz_Request::paramString('xPathItem', true);
  184. if (Minz_Request::paramString('xPathItemTitle') != '')
  185. $xPathSettings['itemTitle'] = Minz_Request::paramString('xPathItemTitle', true);
  186. if (Minz_Request::paramString('xPathItemContent') != '')
  187. $xPathSettings['itemContent'] = Minz_Request::paramString('xPathItemContent', true);
  188. if (Minz_Request::paramString('xPathItemUri') != '')
  189. $xPathSettings['itemUri'] = Minz_Request::paramString('xPathItemUri', true);
  190. if (Minz_Request::paramString('xPathItemAuthor') != '')
  191. $xPathSettings['itemAuthor'] = Minz_Request::paramString('xPathItemAuthor', true);
  192. if (Minz_Request::paramString('xPathItemTimestamp') != '')
  193. $xPathSettings['itemTimestamp'] = Minz_Request::paramString('xPathItemTimestamp', true);
  194. if (Minz_Request::paramString('xPathItemTimeFormat') != '')
  195. $xPathSettings['itemTimeFormat'] = Minz_Request::paramString('xPathItemTimeFormat', true);
  196. if (Minz_Request::paramString('xPathItemThumbnail') != '')
  197. $xPathSettings['itemThumbnail'] = Minz_Request::paramString('xPathItemThumbnail', true);
  198. if (Minz_Request::paramString('xPathItemCategories') != '')
  199. $xPathSettings['itemCategories'] = Minz_Request::paramString('xPathItemCategories', true);
  200. if (Minz_Request::paramString('xPathItemUid') != '')
  201. $xPathSettings['itemUid'] = Minz_Request::paramString('xPathItemUid', true);
  202. if (!empty($xPathSettings))
  203. $feed->_attributes('xpath', $xPathSettings);
  204. }
  205. $feed->_attributes('path_entries_filter', Minz_Request::paramString('path_entries_filter', true));
  206. $values = [
  207. 'name' => Minz_Request::paramString('name'),
  208. 'kind' => $feed->kind(),
  209. 'description' => sanitizeHTML(Minz_Request::paramString('description', true)),
  210. 'website' => checkUrl(Minz_Request::paramString('website')) ?: '',
  211. 'url' => checkUrl(Minz_Request::paramString('url')) ?: '',
  212. 'category' => Minz_Request::paramInt('category'),
  213. 'pathEntries' => Minz_Request::paramString('path_entries'),
  214. 'priority' => Minz_Request::paramTernary('priority') === null ? FreshRSS_Feed::PRIORITY_MAIN_STREAM : Minz_Request::paramInt('priority'),
  215. 'httpAuth' => $httpAuth,
  216. 'ttl' => $feed->ttl(true),
  217. 'attributes' => $feed->attributes(),
  218. ];
  219. invalidateHttpCache();
  220. $from = Minz_Request::paramString('from');
  221. switch ($from) {
  222. case 'stats':
  223. $url_redirect = ['c' => 'stats', 'a' => 'idle', 'params' => ['id' => $id, 'from' => 'stats']];
  224. break;
  225. case 'normal':
  226. case 'reader':
  227. $get = Minz_Request::paramString('get');
  228. if ($get) {
  229. $url_redirect = ['c' => 'index', 'a' => $from, 'params' => ['get' => $get]];
  230. } else {
  231. $url_redirect = ['c' => 'index', 'a' => $from];
  232. }
  233. break;
  234. default:
  235. $url_redirect = ['c' => 'subscription', 'params' => ['id' => $id]];
  236. }
  237. if ($values['url'] != '' && $feedDAO->updateFeed($id, $values) !== false) {
  238. $feed->_categoryId($values['category']);
  239. // update url and website values for faviconPrepare
  240. $feed->_url($values['url'], false);
  241. $feed->_website($values['website'], false);
  242. $feed->faviconPrepare();
  243. Minz_Request::good(_t('feedback.sub.feed.updated'), $url_redirect);
  244. } else {
  245. if ($values['url'] == '') {
  246. Minz_Log::warning('Invalid feed URL!');
  247. }
  248. Minz_Request::bad(_t('feedback.sub.feed.error'), $url_redirect);
  249. }
  250. }
  251. }
  252. public function categoryAction(): void {
  253. if (Minz_Request::paramBoolean('ajax')) {
  254. $this->view->_layout(null);
  255. }
  256. $categoryDAO = FreshRSS_Factory::createCategoryDao();
  257. $id = Minz_Request::paramInt('id');
  258. $category = $categoryDAO->searchById($id);
  259. if ($id === 0 || null === $category) {
  260. Minz_Error::error(404);
  261. return;
  262. }
  263. $this->view->category = $category;
  264. FreshRSS_View::prependTitle($category->name() . ' · ' . _t('sub.title') . ' · ');
  265. if (Minz_Request::isPost()) {
  266. if (Minz_Request::paramBoolean('use_default_purge_options')) {
  267. $category->_attributes('archiving', null);
  268. } else {
  269. if (!Minz_Request::paramBoolean('enable_keep_max')) {
  270. $keepMax = false;
  271. } elseif (($keepMax = Minz_Request::paramInt('keep_max')) !== 0) {
  272. $keepMax = FreshRSS_Feed::ARCHIVING_RETENTION_COUNT_LIMIT;
  273. }
  274. if (Minz_Request::paramBoolean('enable_keep_period')) {
  275. $keepPeriod = FreshRSS_Feed::ARCHIVING_RETENTION_PERIOD;
  276. if (is_numeric(Minz_Request::paramString('keep_period_count')) && preg_match('/^PT?1[YMWDH]$/', Minz_Request::paramString('keep_period_unit'))) {
  277. $keepPeriod = str_replace('1', Minz_Request::paramString('keep_period_count'), Minz_Request::paramString('keep_period_unit'));
  278. }
  279. } else {
  280. $keepPeriod = false;
  281. }
  282. $category->_attributes('archiving', [
  283. 'keep_period' => $keepPeriod,
  284. 'keep_max' => $keepMax,
  285. 'keep_min' => Minz_Request::paramInt('keep_min'),
  286. 'keep_favourites' => Minz_Request::paramBoolean('keep_favourites'),
  287. 'keep_labels' => Minz_Request::paramBoolean('keep_labels'),
  288. 'keep_unreads' => Minz_Request::paramBoolean('keep_unreads'),
  289. ]);
  290. }
  291. $position = Minz_Request::paramInt('position') ?: null;
  292. $category->_attributes('position', $position);
  293. $opml_url = checkUrl(Minz_Request::paramString('opml_url'));
  294. if ($opml_url != '') {
  295. $category->_kind(FreshRSS_Category::KIND_DYNAMIC_OPML);
  296. $category->_attributes('opml_url', $opml_url);
  297. } else {
  298. $category->_kind(FreshRSS_Category::KIND_NORMAL);
  299. $category->_attributes('opml_url', null);
  300. }
  301. $values = [
  302. 'kind' => $category->kind(),
  303. 'name' => Minz_Request::paramString('name'),
  304. 'attributes' => $category->attributes(),
  305. ];
  306. invalidateHttpCache();
  307. $url_redirect = ['c' => 'subscription', 'params' => ['id' => $id, 'type' => 'category']];
  308. if (false !== $categoryDAO->updateCategory($id, $values)) {
  309. Minz_Request::good(_t('feedback.sub.category.updated'), $url_redirect);
  310. } else {
  311. Minz_Request::bad(_t('feedback.sub.category.error'), $url_redirect);
  312. }
  313. }
  314. }
  315. /**
  316. * This action displays the bookmarklet page.
  317. */
  318. public function bookmarkletAction(): void {
  319. FreshRSS_View::prependTitle(_t('sub.title.subscription_tools') . ' . ');
  320. }
  321. /**
  322. * This action displays the page to add a new feed
  323. */
  324. public function addAction(): void {
  325. FreshRSS_View::appendScript(Minz_Url::display('/scripts/feed.js?' . @filemtime(PUBLIC_PATH . '/scripts/feed.js')));
  326. FreshRSS_View::prependTitle(_t('sub.title.add') . ' . ');
  327. }
  328. }