subscriptionController.php 13 KB

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