subscriptionController.php 14 KB

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