subscriptionController.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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 boiler plate for every action. It is triggered by the
  9. * underlying framework.
  10. */
  11. public function firstAction() {
  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() {
  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::paramTernary('error');
  45. $id = Minz_Request::param('id');
  46. $this->view->displaySlider = false;
  47. if (false !== $id) {
  48. $type = Minz_Request::param('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() {
  83. if (Minz_Request::param('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::param('id');
  91. if ($id === false || !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 = trim(Minz_Request::param('http_user_feed' . $id, ''));
  100. $pass = trim(Minz_Request::param('http_pass_feed' . $id, ''));
  101. $httpAuth = '';
  102. if ($user !== '' && $pass !== '') { //TODO: Sanitize
  103. $httpAuth = $user . ':' . $pass;
  104. }
  105. $feed->_ttl(intval(Minz_Request::param('ttl', FreshRSS_Feed::TTL_DEFAULT)));
  106. $feed->_mute(boolval(Minz_Request::param('mute', false)));
  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 = intval(Minz_Request::param('keep_max_n_unread', 0));
  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::param('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 = intval($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::param('curl_params_cookie', '');
  124. $cookie_file = Minz_Request::paramBoolean('curl_params_cookiefile');
  125. $max_redirs = intval(Minz_Request::param('curl_params_redirects', 0));
  126. $useragent = Minz_Request::param('curl_params_useragent', '');
  127. $proxy_address = Minz_Request::param('curl_params', '');
  128. $proxy_type = Minz_Request::param('proxy_type', '');
  129. $opts = [];
  130. if ($proxy_address !== '' && $proxy_type !== '' && in_array($proxy_type, [0, 2, 4, 5, 6, 7])) {
  131. $opts[CURLOPT_PROXY] = $proxy_address;
  132. $opts[CURLOPT_PROXYTYPE] = intval($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::param('content_action', 'replace', true));
  151. $feed->_attributes('ssl_verify', Minz_Request::paramTernary('ssl_verify'));
  152. $timeout = intval(Minz_Request::param('timeout', 0));
  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 = false;
  159. } elseif (!$keepMax = Minz_Request::param('keep_max')) {
  160. $keepMax = FreshRSS_Feed::ARCHIVING_RETENTION_COUNT_LIMIT;
  161. }
  162. if ($enableRetentionPeriod = Minz_Request::paramBoolean('enable_keep_period')) {
  163. $keepPeriod = FreshRSS_Feed::ARCHIVING_RETENTION_PERIOD;
  164. if (is_numeric(Minz_Request::param('keep_period_count')) && preg_match('/^PT?1[YMWDH]$/', Minz_Request::param('keep_period_unit'))) {
  165. $keepPeriod = str_replace('1', Minz_Request::param('keep_period_count'), Minz_Request::param('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' => intval(Minz_Request::param('keep_min', 0)),
  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::param('filteractions_read', '')));
  180. $feed->_kind(intval(Minz_Request::param('feed_kind', FreshRSS_Feed::KIND_RSS)));
  181. if ($feed->kind() == FreshRSS_Feed::KIND_HTML_XPATH) {
  182. $xPathSettings = [];
  183. if (Minz_Request::param('xPathItem', '') != '') $xPathSettings['item'] = Minz_Request::param('xPathItem', '', true);
  184. if (Minz_Request::param('xPathItemTitle', '') != '') $xPathSettings['itemTitle'] = Minz_Request::param('xPathItemTitle', '', true);
  185. if (Minz_Request::param('xPathItemContent', '') != '') $xPathSettings['itemContent'] = Minz_Request::param('xPathItemContent', '', true);
  186. if (Minz_Request::param('xPathItemUri', '') != '') $xPathSettings['itemUri'] = Minz_Request::param('xPathItemUri', '', true);
  187. if (Minz_Request::param('xPathItemAuthor', '') != '') $xPathSettings['itemAuthor'] = Minz_Request::param('xPathItemAuthor', '', true);
  188. if (Minz_Request::param('xPathItemTimestamp', '') != '') $xPathSettings['itemTimestamp'] = Minz_Request::param('xPathItemTimestamp', '', true);
  189. if (Minz_Request::param('xPathItemTimeFormat', '') != '') $xPathSettings['itemTimeFormat'] = Minz_Request::param('xPathItemTimeFormat', '', true);
  190. if (Minz_Request::param('xPathItemThumbnail', '') != '') $xPathSettings['itemThumbnail'] = Minz_Request::param('xPathItemThumbnail', '', true);
  191. if (Minz_Request::param('xPathItemCategories', '') != '') $xPathSettings['itemCategories'] = Minz_Request::param('xPathItemCategories', '', true);
  192. if (Minz_Request::param('xPathItemUid', '') != '') $xPathSettings['itemUid'] = Minz_Request::param('xPathItemUid', '', true);
  193. if (!empty($xPathSettings)) {
  194. $feed->_attributes('xpath', $xPathSettings);
  195. }
  196. }
  197. $feed->_attributes('path_entries_filter', Minz_Request::param('path_entries_filter', '', true));
  198. $values = array(
  199. 'name' => Minz_Request::param('name', ''),
  200. 'kind' => $feed->kind(),
  201. 'description' => sanitizeHTML(Minz_Request::param('description', '', true)),
  202. 'website' => checkUrl(Minz_Request::param('website', '')),
  203. 'url' => checkUrl(Minz_Request::param('url', '')),
  204. 'category' => intval(Minz_Request::param('category', 0)),
  205. 'pathEntries' => Minz_Request::param('path_entries', ''),
  206. 'priority' => intval(Minz_Request::param('priority', FreshRSS_Feed::PRIORITY_MAIN_STREAM)),
  207. 'httpAuth' => $httpAuth,
  208. 'ttl' => $feed->ttl(true),
  209. 'attributes' => $feed->attributes(),
  210. );
  211. invalidateHttpCache();
  212. $from = Minz_Request::param('from');
  213. switch ($from) {
  214. case 'stats':
  215. $url_redirect = array('c' => 'stats', 'a' => 'idle', 'params' => array('id' => $id, 'from' => 'stats'));
  216. break;
  217. case 'normal':
  218. case 'reader':
  219. $get = Minz_Request::param('get');
  220. if ($get) {
  221. $url_redirect = array('c' => 'index', 'a' => $from, 'params' => array('get' => $get));
  222. } else {
  223. $url_redirect = array('c' => 'index', 'a' => $from);
  224. }
  225. break;
  226. default:
  227. $url_redirect = array('c' => 'subscription', 'params' => array('id' => $id));
  228. }
  229. if ($values['url'] != '' && $feedDAO->updateFeed($id, $values) !== false) {
  230. $feed->_categoryId($values['category']);
  231. // update url and website values for faviconPrepare
  232. $feed->_url($values['url'], false);
  233. $feed->_website($values['website'], false);
  234. $feed->faviconPrepare();
  235. Minz_Request::good(_t('feedback.sub.feed.updated'), $url_redirect);
  236. } else {
  237. if ($values['url'] == '') {
  238. Minz_Log::warning('Invalid feed URL!');
  239. }
  240. Minz_Request::bad(_t('feedback.sub.feed.error'), $url_redirect);
  241. }
  242. }
  243. }
  244. public function categoryAction() {
  245. $this->view->_layout(false);
  246. $categoryDAO = FreshRSS_Factory::createCategoryDao();
  247. $id = Minz_Request::param('id');
  248. $category = $categoryDAO->searchById($id);
  249. if ($id === false || null === $category) {
  250. Minz_Error::error(404);
  251. return;
  252. }
  253. $this->view->category = $category;
  254. if (Minz_Request::isPost()) {
  255. if (Minz_Request::paramBoolean('use_default_purge_options')) {
  256. $category->_attributes('archiving', null);
  257. } else {
  258. if (!Minz_Request::paramBoolean('enable_keep_max')) {
  259. $keepMax = false;
  260. } elseif (!$keepMax = Minz_Request::param('keep_max')) {
  261. $keepMax = FreshRSS_Feed::ARCHIVING_RETENTION_COUNT_LIMIT;
  262. }
  263. if ($enableRetentionPeriod = Minz_Request::paramBoolean('enable_keep_period')) {
  264. $keepPeriod = FreshRSS_Feed::ARCHIVING_RETENTION_PERIOD;
  265. if (is_numeric(Minz_Request::param('keep_period_count')) && preg_match('/^PT?1[YMWDH]$/', Minz_Request::param('keep_period_unit'))) {
  266. $keepPeriod = str_replace('1', Minz_Request::param('keep_period_count'), Minz_Request::param('keep_period_unit'));
  267. }
  268. } else {
  269. $keepPeriod = false;
  270. }
  271. $category->_attributes('archiving', [
  272. 'keep_period' => $keepPeriod,
  273. 'keep_max' => $keepMax,
  274. 'keep_min' => intval(Minz_Request::param('keep_min', 0)),
  275. 'keep_favourites' => Minz_Request::paramBoolean('keep_favourites'),
  276. 'keep_labels' => Minz_Request::paramBoolean('keep_labels'),
  277. 'keep_unreads' => Minz_Request::paramBoolean('keep_unreads'),
  278. ]);
  279. }
  280. $position = Minz_Request::param('position');
  281. $category->_attributes('position', '' === $position ? null : (int) $position);
  282. $opml_url = checkUrl(Minz_Request::param('opml_url', ''));
  283. if ($opml_url != '') {
  284. $category->_kind(FreshRSS_Category::KIND_DYNAMIC_OPML);
  285. $category->_attributes('opml_url', $opml_url);
  286. } else {
  287. $category->_kind(FreshRSS_Category::KIND_NORMAL);
  288. $category->_attributes('opml_url', null);
  289. }
  290. $values = [
  291. 'kind' => $category->kind(),
  292. 'name' => Minz_Request::param('name', ''),
  293. 'attributes' => $category->attributes(),
  294. ];
  295. invalidateHttpCache();
  296. $url_redirect = array('c' => 'subscription', 'params' => array('id' => $id, 'type' => 'category'));
  297. if (false !== $categoryDAO->updateCategory($id, $values)) {
  298. Minz_Request::good(_t('feedback.sub.category.updated'), $url_redirect);
  299. } else {
  300. Minz_Request::bad(_t('feedback.sub.category.error'), $url_redirect);
  301. }
  302. }
  303. }
  304. /**
  305. * This action displays the bookmarklet page.
  306. */
  307. public function bookmarkletAction() {
  308. FreshRSS_View::prependTitle(_t('sub.title.subscription_tools') . ' . ');
  309. }
  310. /**
  311. * This action displays the page to add a new feed
  312. */
  313. public function addAction() {
  314. FreshRSS_View::appendScript(Minz_Url::display('/scripts/feed.js?' . @filemtime(PUBLIC_PATH . '/scripts/feed.js')));
  315. FreshRSS_View::prependTitle(_t('sub.title.add') . ' . ');
  316. }
  317. }