4
0

subscriptionController.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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);
  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. $opts = [];
  128. if ($proxy_type !== '') {
  129. $opts[CURLOPT_PROXY] = $proxy_address;
  130. $opts[CURLOPT_PROXYTYPE] = (int)$proxy_type;
  131. }
  132. if ($cookie !== '') {
  133. $opts[CURLOPT_COOKIE] = $cookie;
  134. }
  135. if ($cookie_file) {
  136. // Pass empty cookie file name to enable the libcurl cookie engine
  137. // without reading any existing cookie data.
  138. $opts[CURLOPT_COOKIEFILE] = '';
  139. }
  140. if ($max_redirs != 0) {
  141. $opts[CURLOPT_MAXREDIRS] = $max_redirs;
  142. $opts[CURLOPT_FOLLOWLOCATION] = 1;
  143. }
  144. if ($useragent !== '') {
  145. $opts[CURLOPT_USERAGENT] = $useragent;
  146. }
  147. $feed->_attribute('curl_params', empty($opts) ? null : $opts);
  148. $feed->_attribute('content_action', Minz_Request::paramString('content_action', true) ?: 'replace');
  149. $feed->_attribute('ssl_verify', Minz_Request::paramTernary('ssl_verify'));
  150. $timeout = Minz_Request::paramInt('timeout');
  151. $feed->_attribute('timeout', $timeout > 0 ? $timeout : null);
  152. if (Minz_Request::paramBoolean('use_default_purge_options')) {
  153. $feed->_attribute('archiving', null);
  154. } else {
  155. if (Minz_Request::paramBoolean('enable_keep_max')) {
  156. $keepMax = Minz_Request::paramInt('keep_max') ?: FreshRSS_Feed::ARCHIVING_RETENTION_COUNT_LIMIT;
  157. } else {
  158. $keepMax = false;
  159. }
  160. if (Minz_Request::paramBoolean('enable_keep_period')) {
  161. $keepPeriod = FreshRSS_Feed::ARCHIVING_RETENTION_PERIOD;
  162. if (is_numeric(Minz_Request::paramString('keep_period_count')) && preg_match('/^PT?1[YMWDH]$/', Minz_Request::paramString('keep_period_unit'))) {
  163. $keepPeriod = str_replace('1', Minz_Request::paramString('keep_period_count'), Minz_Request::paramString('keep_period_unit'));
  164. }
  165. } else {
  166. $keepPeriod = false;
  167. }
  168. $feed->_attribute('archiving', [
  169. 'keep_period' => $keepPeriod,
  170. 'keep_max' => $keepMax,
  171. 'keep_min' => Minz_Request::paramInt('keep_min'),
  172. 'keep_favourites' => Minz_Request::paramBoolean('keep_favourites'),
  173. 'keep_labels' => Minz_Request::paramBoolean('keep_labels'),
  174. 'keep_unreads' => Minz_Request::paramBoolean('keep_unreads'),
  175. ]);
  176. }
  177. $feed->_filtersAction('read', Minz_Request::paramTextToArray('filteractions_read'));
  178. $feed->_kind(Minz_Request::paramInt('feed_kind') ?: FreshRSS_Feed::KIND_RSS);
  179. if ($feed->kind() === FreshRSS_Feed::KIND_HTML_XPATH || $feed->kind() === FreshRSS_Feed::KIND_XML_XPATH) {
  180. $xPathSettings = [];
  181. if (Minz_Request::paramString('xPathItem') != '')
  182. $xPathSettings['item'] = Minz_Request::paramString('xPathItem', true);
  183. if (Minz_Request::paramString('xPathItemTitle') != '')
  184. $xPathSettings['itemTitle'] = Minz_Request::paramString('xPathItemTitle', true);
  185. if (Minz_Request::paramString('xPathItemContent') != '')
  186. $xPathSettings['itemContent'] = Minz_Request::paramString('xPathItemContent', true);
  187. if (Minz_Request::paramString('xPathItemUri') != '')
  188. $xPathSettings['itemUri'] = Minz_Request::paramString('xPathItemUri', true);
  189. if (Minz_Request::paramString('xPathItemAuthor') != '')
  190. $xPathSettings['itemAuthor'] = Minz_Request::paramString('xPathItemAuthor', true);
  191. if (Minz_Request::paramString('xPathItemTimestamp') != '')
  192. $xPathSettings['itemTimestamp'] = Minz_Request::paramString('xPathItemTimestamp', true);
  193. if (Minz_Request::paramString('xPathItemTimeFormat') != '')
  194. $xPathSettings['itemTimeFormat'] = Minz_Request::paramString('xPathItemTimeFormat', true);
  195. if (Minz_Request::paramString('xPathItemThumbnail') != '')
  196. $xPathSettings['itemThumbnail'] = Minz_Request::paramString('xPathItemThumbnail', true);
  197. if (Minz_Request::paramString('xPathItemCategories') != '')
  198. $xPathSettings['itemCategories'] = Minz_Request::paramString('xPathItemCategories', true);
  199. if (Minz_Request::paramString('xPathItemUid') != '')
  200. $xPathSettings['itemUid'] = Minz_Request::paramString('xPathItemUid', true);
  201. if (!empty($xPathSettings))
  202. $feed->_attribute('xpath', $xPathSettings);
  203. }
  204. $feed->_attribute('path_entries_filter', Minz_Request::paramString('path_entries_filter', true));
  205. $values = [
  206. 'name' => Minz_Request::paramString('name'),
  207. 'kind' => $feed->kind(),
  208. 'description' => sanitizeHTML(Minz_Request::paramString('description', true)),
  209. 'website' => checkUrl(Minz_Request::paramString('website')) ?: '',
  210. 'url' => checkUrl(Minz_Request::paramString('url')) ?: '',
  211. 'category' => Minz_Request::paramInt('category'),
  212. 'pathEntries' => Minz_Request::paramString('path_entries'),
  213. 'priority' => Minz_Request::paramTernary('priority') === null ? FreshRSS_Feed::PRIORITY_MAIN_STREAM : Minz_Request::paramInt('priority'),
  214. 'httpAuth' => $httpAuth,
  215. 'ttl' => $feed->ttl(true),
  216. 'attributes' => $feed->attributes(),
  217. ];
  218. invalidateHttpCache();
  219. $from = Minz_Request::paramString('from');
  220. switch ($from) {
  221. case 'stats':
  222. $url_redirect = ['c' => 'stats', 'a' => 'idle', 'params' => ['id' => $id, 'from' => 'stats']];
  223. break;
  224. case 'normal':
  225. case 'reader':
  226. $get = Minz_Request::paramString('get');
  227. if ($get) {
  228. $url_redirect = ['c' => 'index', 'a' => $from, 'params' => ['get' => $get]];
  229. } else {
  230. $url_redirect = ['c' => 'index', 'a' => $from];
  231. }
  232. break;
  233. default:
  234. $url_redirect = ['c' => 'subscription', 'params' => ['id' => $id]];
  235. }
  236. if ($values['url'] != '' && $feedDAO->updateFeed($id, $values) !== false) {
  237. $feed->_categoryId($values['category']);
  238. // update url and website values for faviconPrepare
  239. $feed->_url($values['url'], false);
  240. $feed->_website($values['website'], false);
  241. $feed->faviconPrepare();
  242. Minz_Request::good(_t('feedback.sub.feed.updated'), $url_redirect);
  243. } else {
  244. if ($values['url'] == '') {
  245. Minz_Log::warning('Invalid feed URL!');
  246. }
  247. Minz_Request::bad(_t('feedback.sub.feed.error'), $url_redirect);
  248. }
  249. }
  250. }
  251. /**
  252. * This action displays the bookmarklet page.
  253. */
  254. public function bookmarkletAction(): void {
  255. FreshRSS_View::prependTitle(_t('sub.title.subscription_tools') . ' . ');
  256. }
  257. /**
  258. * This action displays the page to add a new feed
  259. */
  260. public function addAction(): void {
  261. FreshRSS_View::appendScript(Minz_Url::display('/scripts/feed.js?' . @filemtime(PUBLIC_PATH . '/scripts/feed.js')));
  262. FreshRSS_View::prependTitle(_t('sub.title.add') . ' . ');
  263. }
  264. }