subscriptionController.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  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. #[\Override]
  13. public function firstAction(): void {
  14. if (!FreshRSS_Auth::hasAccess()) {
  15. Minz_Error::error(403);
  16. }
  17. $catDAO = FreshRSS_Factory::createCategoryDao();
  18. $catDAO->checkDefault();
  19. $this->view->categories = $catDAO->listSortedCategories(prePopulateFeeds: false, details: true);
  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) ?? FreshRSS_Feed::default();
  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. $id = Minz_Request::paramInt('id');
  88. if ($id === 0) {
  89. Minz_Error::error(400);
  90. return;
  91. }
  92. $feedDAO = FreshRSS_Factory::createFeedDao();
  93. $feed = $feedDAO->searchById($id);
  94. if ($feed === null) {
  95. Minz_Error::error(404);
  96. return;
  97. }
  98. $this->view->feed = $feed;
  99. FreshRSS_View::prependTitle($feed->name() . ' · ' . _t('sub.title.feed_management') . ' · ');
  100. if (Minz_Request::isPost()) {
  101. $unicityCriteria = Minz_Request::paramString('unicityCriteria');
  102. if (in_array($unicityCriteria, ['id', '', null], strict: true)) {
  103. $unicityCriteria = null;
  104. }
  105. if ($unicityCriteria === null && $feed->attributeBoolean('hasBadGuids')) { // Legacy
  106. $unicityCriteria = 'link';
  107. }
  108. $feed->_attribute('hasBadGuids', null); // Remove legacy
  109. $feed->_attribute('unicityCriteria', $unicityCriteria);
  110. $feed->_attribute('unicityCriteriaForced', Minz_Request::paramBoolean('unicityCriteriaForced') ? true : null);
  111. $user = Minz_Request::paramString('http_user_feed' . $id);
  112. $pass = Minz_Request::paramString('http_pass_feed' . $id);
  113. $httpAuth = '';
  114. if ($user !== '' && $pass !== '') { //TODO: Sanitize
  115. $httpAuth = $user . ':' . $pass;
  116. }
  117. $feed->_ttl(Minz_Request::paramInt('ttl') ?: FreshRSS_Feed::TTL_DEFAULT);
  118. $feed->_mute(Minz_Request::paramBoolean('mute'));
  119. $feed->_attribute('read_upon_gone', Minz_Request::paramTernary('read_upon_gone'));
  120. $feed->_attribute('mark_updated_article_unread', Minz_Request::paramTernary('mark_updated_article_unread'));
  121. $feed->_attribute('read_upon_reception', Minz_Request::paramTernary('read_upon_reception'));
  122. $feed->_attribute('clear_cache', Minz_Request::paramTernary('clear_cache'));
  123. $keep_max_n_unread = Minz_Request::paramTernary('keep_max_n_unread') === true ? Minz_Request::paramInt('keep_max_n_unread') : null;
  124. $feed->_attribute('keep_max_n_unread', $keep_max_n_unread >= 0 ? $keep_max_n_unread : null);
  125. $read_when_same_title_in_feed = Minz_Request::paramString('read_when_same_title_in_feed');
  126. if ($read_when_same_title_in_feed === '') {
  127. $read_when_same_title_in_feed = null;
  128. } else {
  129. $read_when_same_title_in_feed = (int)$read_when_same_title_in_feed;
  130. if ($read_when_same_title_in_feed <= 0) {
  131. $read_when_same_title_in_feed = false;
  132. }
  133. }
  134. $feed->_attribute('read_when_same_title_in_feed', $read_when_same_title_in_feed);
  135. $cookie = Minz_Request::paramString('curl_params_cookie', plaintext: true);
  136. $cookie_file = Minz_Request::paramBoolean('curl_params_cookiefile');
  137. $max_redirs = Minz_Request::paramInt('curl_params_redirects');
  138. $useragent = Minz_Request::paramString('curl_params_useragent', plaintext: true);
  139. $proxy_address = Minz_Request::paramString('curl_params', plaintext: true);
  140. $proxy_type = Minz_Request::paramString('proxy_type', plaintext: true);
  141. $request_method = Minz_Request::paramString('curl_method', plaintext: true);
  142. $request_fields = Minz_Request::paramString('curl_fields', plaintext: true);
  143. $headers = Minz_Request::paramTextToArray('http_headers', plaintext: true);
  144. $opts = [];
  145. if ($proxy_type !== '') {
  146. $opts[CURLOPT_PROXY] = $proxy_address;
  147. $opts[CURLOPT_PROXYTYPE] = (int)$proxy_type;
  148. }
  149. if ($cookie !== '') {
  150. $opts[CURLOPT_COOKIE] = $cookie;
  151. }
  152. if ($cookie_file) {
  153. // Pass empty cookie file name to enable the libcurl cookie engine
  154. // without reading any existing cookie data.
  155. $opts[CURLOPT_COOKIEFILE] = '';
  156. }
  157. if ($max_redirs != 0) {
  158. $opts[CURLOPT_MAXREDIRS] = $max_redirs;
  159. $opts[CURLOPT_FOLLOWLOCATION] = 1;
  160. }
  161. if ($useragent !== '') {
  162. $opts[CURLOPT_USERAGENT] = $useragent;
  163. }
  164. if ($request_method === 'POST') {
  165. $opts[CURLOPT_POST] = true;
  166. if ($request_fields !== '') {
  167. $opts[CURLOPT_POSTFIELDS] = $request_fields;
  168. if (json_decode($request_fields, true) !== null) {
  169. $opts[CURLOPT_HTTPHEADER] = ['Content-Type: application/json'];
  170. }
  171. }
  172. }
  173. if (!empty($headers)) {
  174. $headers = array_filter(array_map('trim', $headers));
  175. $opts[CURLOPT_HTTPHEADER] = array_merge($headers, $opts[CURLOPT_HTTPHEADER] ?? []);
  176. }
  177. $feed->_attribute('curl_params', empty($opts) ? null : $opts);
  178. $feed->_attribute('content_action', Minz_Request::paramString('content_action', true) ?: 'replace');
  179. $feed->_attribute('ssl_verify', Minz_Request::paramTernary('ssl_verify'));
  180. $timeout = Minz_Request::paramInt('timeout');
  181. $feed->_attribute('timeout', $timeout > 0 ? $timeout : null);
  182. if (Minz_Request::paramBoolean('use_default_purge_options')) {
  183. $feed->_attribute('archiving', null);
  184. } else {
  185. if (Minz_Request::paramBoolean('enable_keep_max')) {
  186. $keepMax = Minz_Request::paramInt('keep_max') ?: FreshRSS_Feed::ARCHIVING_RETENTION_COUNT_LIMIT;
  187. } else {
  188. $keepMax = false;
  189. }
  190. if (Minz_Request::paramBoolean('enable_keep_period')) {
  191. $keepPeriod = FreshRSS_Feed::ARCHIVING_RETENTION_PERIOD;
  192. if (is_numeric(Minz_Request::paramString('keep_period_count')) && preg_match('/^PT?1[YMWDH]$/', Minz_Request::paramString('keep_period_unit'))) {
  193. $keepPeriod = str_replace('1', Minz_Request::paramString('keep_period_count'), Minz_Request::paramString('keep_period_unit'));
  194. }
  195. } else {
  196. $keepPeriod = false;
  197. }
  198. $feed->_attribute('archiving', [
  199. 'keep_period' => $keepPeriod,
  200. 'keep_max' => $keepMax,
  201. 'keep_min' => Minz_Request::paramInt('keep_min'),
  202. 'keep_favourites' => Minz_Request::paramBoolean('keep_favourites'),
  203. 'keep_labels' => Minz_Request::paramBoolean('keep_labels'),
  204. 'keep_unreads' => Minz_Request::paramBoolean('keep_unreads'),
  205. ]);
  206. }
  207. $feed->_filtersAction('read', Minz_Request::paramTextToArray('filteractions_read'));
  208. $feed->_kind(Minz_Request::paramInt('feed_kind') ?: FreshRSS_Feed::KIND_RSS);
  209. if ($feed->kind() === FreshRSS_Feed::KIND_HTML_XPATH || $feed->kind() === FreshRSS_Feed::KIND_XML_XPATH) {
  210. $xPathSettings = [];
  211. if (Minz_Request::paramString('xPathItem') != '')
  212. $xPathSettings['item'] = Minz_Request::paramString('xPathItem', true);
  213. if (Minz_Request::paramString('xPathItemTitle') != '')
  214. $xPathSettings['itemTitle'] = Minz_Request::paramString('xPathItemTitle', true);
  215. if (Minz_Request::paramString('xPathItemContent') != '')
  216. $xPathSettings['itemContent'] = Minz_Request::paramString('xPathItemContent', true);
  217. if (Minz_Request::paramString('xPathItemUri') != '')
  218. $xPathSettings['itemUri'] = Minz_Request::paramString('xPathItemUri', true);
  219. if (Minz_Request::paramString('xPathItemAuthor') != '')
  220. $xPathSettings['itemAuthor'] = Minz_Request::paramString('xPathItemAuthor', true);
  221. if (Minz_Request::paramString('xPathItemTimestamp') != '')
  222. $xPathSettings['itemTimestamp'] = Minz_Request::paramString('xPathItemTimestamp', true);
  223. if (Minz_Request::paramString('xPathItemTimeFormat') != '')
  224. $xPathSettings['itemTimeFormat'] = Minz_Request::paramString('xPathItemTimeFormat', true);
  225. if (Minz_Request::paramString('xPathItemThumbnail') != '')
  226. $xPathSettings['itemThumbnail'] = Minz_Request::paramString('xPathItemThumbnail', true);
  227. if (Minz_Request::paramString('xPathItemCategories') != '')
  228. $xPathSettings['itemCategories'] = Minz_Request::paramString('xPathItemCategories', true);
  229. if (Minz_Request::paramString('xPathItemUid') != '')
  230. $xPathSettings['itemUid'] = Minz_Request::paramString('xPathItemUid', true);
  231. if (!empty($xPathSettings))
  232. $feed->_attribute('xpath', $xPathSettings);
  233. } elseif ($feed->kind() === FreshRSS_Feed::KIND_JSON_DOTNOTATION || $feed->kind() === FreshRSS_Feed::KIND_HTML_XPATH_JSON_DOTNOTATION) {
  234. $jsonSettings = [];
  235. if (Minz_Request::paramString('jsonFeedTitle') !== '') {
  236. $jsonSettings['feedTitle'] = Minz_Request::paramString('jsonFeedTitle', true);
  237. }
  238. if (Minz_Request::paramString('jsonItem') !== '') {
  239. $jsonSettings['item'] = Minz_Request::paramString('jsonItem', true);
  240. }
  241. if (Minz_Request::paramString('jsonItemTitle') !== '') {
  242. $jsonSettings['itemTitle'] = Minz_Request::paramString('jsonItemTitle', true);
  243. }
  244. if (Minz_Request::paramString('jsonItemContent') !== '') {
  245. $jsonSettings['itemContent'] = Minz_Request::paramString('jsonItemContent', true);
  246. }
  247. if (Minz_Request::paramString('jsonItemUri') !== '') {
  248. $jsonSettings['itemUri'] = Minz_Request::paramString('jsonItemUri', true);
  249. }
  250. if (Minz_Request::paramString('jsonItemAuthor') !== '') {
  251. $jsonSettings['itemAuthor'] = Minz_Request::paramString('jsonItemAuthor', true);
  252. }
  253. if (Minz_Request::paramString('jsonItemTimestamp') !== '') {
  254. $jsonSettings['itemTimestamp'] = Minz_Request::paramString('jsonItemTimestamp', true);
  255. }
  256. if (Minz_Request::paramString('jsonItemTimeFormat') !== '') {
  257. $jsonSettings['itemTimeFormat'] = Minz_Request::paramString('jsonItemTimeFormat', true);
  258. }
  259. if (Minz_Request::paramString('jsonItemThumbnail') !== '') {
  260. $jsonSettings['itemThumbnail'] = Minz_Request::paramString('jsonItemThumbnail', true);
  261. }
  262. if (Minz_Request::paramString('jsonItemCategories') !== '') {
  263. $jsonSettings['itemCategories'] = Minz_Request::paramString('jsonItemCategories', true);
  264. }
  265. if (Minz_Request::paramString('jsonItemUid') !== '') {
  266. $jsonSettings['itemUid'] = Minz_Request::paramString('jsonItemUid', true);
  267. }
  268. if (!empty($jsonSettings)) {
  269. $feed->_attribute('json_dotnotation', $jsonSettings);
  270. }
  271. if (Minz_Request::paramString('xPathToJson', plaintext: true) !== '') {
  272. $feed->_attribute('xPathToJson', Minz_Request::paramString('xPathToJson', plaintext: true));
  273. }
  274. }
  275. $conditions = Minz_Request::paramTextToArray('path_entries_conditions', plaintext: true);
  276. $conditions = array_filter(array_map('trim', $conditions));
  277. $feed->_attribute('path_entries_conditions', empty($conditions) ? null : $conditions);
  278. $feed->_attribute('path_entries_filter', Minz_Request::paramString('path_entries_filter', true));
  279. $values = [
  280. 'name' => Minz_Request::paramString('name'),
  281. 'kind' => $feed->kind(),
  282. 'description' => sanitizeHTML(Minz_Request::paramString('description', true)),
  283. 'website' => checkUrl(Minz_Request::paramString('website')) ?: '',
  284. 'url' => checkUrl(Minz_Request::paramString('url')) ?: '',
  285. 'category' => Minz_Request::paramInt('category'),
  286. 'pathEntries' => Minz_Request::paramString('path_entries'),
  287. 'priority' => Minz_Request::paramTernary('priority') === null ? FreshRSS_Feed::PRIORITY_MAIN_STREAM : Minz_Request::paramInt('priority'),
  288. 'httpAuth' => $httpAuth,
  289. 'ttl' => $feed->ttl(true),
  290. 'attributes' => $feed->attributes(),
  291. ];
  292. invalidateHttpCache();
  293. $from = Minz_Request::paramString('from');
  294. switch ($from) {
  295. case 'stats':
  296. $url_redirect = ['c' => 'stats', 'a' => 'idle', 'params' => ['id' => $id, 'from' => 'stats']];
  297. break;
  298. case 'normal':
  299. case 'reader':
  300. $get = Minz_Request::paramString('get');
  301. if ($get !== '') {
  302. $url_redirect = ['c' => 'index', 'a' => $from, 'params' => ['get' => $get]];
  303. } else {
  304. $url_redirect = ['c' => 'index', 'a' => $from];
  305. }
  306. break;
  307. default:
  308. $url_redirect = ['c' => 'subscription', 'params' => ['id' => $id]];
  309. }
  310. if ($values['url'] != '' && $feedDAO->updateFeed($id, $values) !== false) {
  311. $feed->_categoryId($values['category']);
  312. // update url and website values for faviconPrepare
  313. $feed->_url($values['url'], false);
  314. $feed->_website($values['website'], false);
  315. $feed->faviconPrepare();
  316. Minz_Request::good(_t('feedback.sub.feed.updated'), $url_redirect);
  317. } else {
  318. if ($values['url'] == '') {
  319. Minz_Log::warning('Invalid feed URL!');
  320. }
  321. Minz_Request::bad(_t('feedback.sub.feed.error'), $url_redirect);
  322. }
  323. }
  324. }
  325. /**
  326. * This action displays the bookmarklet page.
  327. */
  328. public function bookmarkletAction(): void {
  329. FreshRSS_View::prependTitle(_t('sub.title.subscription_tools') . ' . ');
  330. }
  331. /**
  332. * This action displays the page to add a new feed
  333. */
  334. public function addAction(): void {
  335. FreshRSS_View::appendScript(Minz_Url::display('/scripts/feed.js?' . @filemtime(PUBLIC_PATH . '/scripts/feed.js')));
  336. FreshRSS_View::prependTitle(_t('sub.title.add') . ' . ');
  337. }
  338. }