subscriptionController.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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);
  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. $cat = intval(Minz_Request::param('category', 0));
  106. $mute = Minz_Request::param('mute', false);
  107. $ttl = intval(Minz_Request::param('ttl', FreshRSS_Feed::TTL_DEFAULT));
  108. if ($mute && FreshRSS_Feed::TTL_DEFAULT === $ttl) {
  109. $ttl = FreshRSS_Context::$user_conf->ttl_default;
  110. }
  111. $feed->_attributes('read_upon_gone', Minz_Request::paramTernary('read_upon_gone'));
  112. $feed->_attributes('mark_updated_article_unread', Minz_Request::paramTernary('mark_updated_article_unread'));
  113. $feed->_attributes('read_upon_reception', Minz_Request::paramTernary('read_upon_reception'));
  114. $feed->_attributes('clear_cache', Minz_Request::paramTernary('clear_cache'));
  115. $keep_max_n_unread = intval(Minz_Request::param('keep_max_n_unread', 0));
  116. $feed->_attributes('keep_max_n_unread', $keep_max_n_unread > 0 ? $keep_max_n_unread : null);
  117. $read_when_same_title_in_feed = Minz_Request::param('read_when_same_title_in_feed', '');
  118. if ($read_when_same_title_in_feed === '') {
  119. $read_when_same_title_in_feed = null;
  120. } else {
  121. $read_when_same_title_in_feed = intval($read_when_same_title_in_feed);
  122. if ($read_when_same_title_in_feed <= 0) {
  123. $read_when_same_title_in_feed = false;
  124. }
  125. }
  126. $feed->_attributes('read_when_same_title_in_feed', $read_when_same_title_in_feed);
  127. $cookie = Minz_Request::param('curl_params_cookie', '');
  128. $useragent = Minz_Request::param('curl_params_useragent', '');
  129. $proxy_address = Minz_Request::param('curl_params', '');
  130. $proxy_type = Minz_Request::param('proxy_type', '');
  131. $opts = [];
  132. if ($proxy_address !== '' && $proxy_type !== '' && in_array($proxy_type, [0, 2, 4, 5, 6, 7])) {
  133. $opts[CURLOPT_PROXY] = $proxy_address;
  134. $opts[CURLOPT_PROXYTYPE] = intval($proxy_type);
  135. }
  136. if ($cookie !== '') {
  137. $opts[CURLOPT_COOKIE] = $cookie;
  138. }
  139. if ($useragent !== '') {
  140. $opts[CURLOPT_USERAGENT] = $useragent;
  141. }
  142. $feed->_attributes('curl_params', empty($opts) ? null : $opts);
  143. $feed->_attributes('content_action', Minz_Request::param('content_action', 'replace'));
  144. $feed->_attributes('ssl_verify', Minz_Request::paramTernary('ssl_verify'));
  145. $timeout = intval(Minz_Request::param('timeout', 0));
  146. $feed->_attributes('timeout', $timeout > 0 ? $timeout : null);
  147. if (Minz_Request::paramBoolean('use_default_purge_options')) {
  148. $feed->_attributes('archiving', null);
  149. } else {
  150. if (!Minz_Request::paramBoolean('enable_keep_max')) {
  151. $keepMax = false;
  152. } elseif (!$keepMax = Minz_Request::param('keep_max')) {
  153. $keepMax = FreshRSS_Feed::ARCHIVING_RETENTION_COUNT_LIMIT;
  154. }
  155. if ($enableRetentionPeriod = Minz_Request::paramBoolean('enable_keep_period')) {
  156. $keepPeriod = FreshRSS_Feed::ARCHIVING_RETENTION_PERIOD;
  157. if (is_numeric(Minz_Request::param('keep_period_count')) && preg_match('/^PT?1[YMWDH]$/', Minz_Request::param('keep_period_unit'))) {
  158. $keepPeriod = str_replace('1', Minz_Request::param('keep_period_count'), Minz_Request::param('keep_period_unit'));
  159. }
  160. } else {
  161. $keepPeriod = false;
  162. }
  163. $feed->_attributes('archiving', [
  164. 'keep_period' => $keepPeriod,
  165. 'keep_max' => $keepMax,
  166. 'keep_min' => intval(Minz_Request::param('keep_min', 0)),
  167. 'keep_favourites' => Minz_Request::paramBoolean('keep_favourites'),
  168. 'keep_labels' => Minz_Request::paramBoolean('keep_labels'),
  169. 'keep_unreads' => Minz_Request::paramBoolean('keep_unreads'),
  170. ]);
  171. }
  172. $feed->_filtersAction('read', preg_split('/[\n\r]+/', Minz_Request::param('filteractions_read', '')));
  173. $feed_kind = Minz_Request::param('feed_kind', FreshRSS_Feed::KIND_RSS);
  174. if ($feed_kind == FreshRSS_Feed::KIND_HTML_XPATH) {
  175. $xPathSettings = [];
  176. if (Minz_Request::param('xPathItem', '') != '') $xPathSettings['item'] = Minz_Request::param('xPathItem', '', true);
  177. if (Minz_Request::param('xPathItemTitle', '') != '') $xPathSettings['itemTitle'] = Minz_Request::param('xPathItemTitle', '', true);
  178. if (Minz_Request::param('xPathItemContent', '') != '') $xPathSettings['itemContent'] = Minz_Request::param('xPathItemContent', '', true);
  179. if (Minz_Request::param('xPathItemUri', '') != '') $xPathSettings['itemUri'] = Minz_Request::param('xPathItemUri', '', true);
  180. if (Minz_Request::param('xPathItemAuthor', '') != '') $xPathSettings['itemAuthor'] = Minz_Request::param('xPathItemAuthor', '', true);
  181. if (Minz_Request::param('xPathItemTimestamp', '') != '') $xPathSettings['itemTimestamp'] = Minz_Request::param('xPathItemTimestamp', '', true);
  182. if (Minz_Request::param('xPathItemThumbnail', '') != '') $xPathSettings['itemThumbnail'] = Minz_Request::param('xPathItemThumbnail', '', true);
  183. if (Minz_Request::param('xPathItemCategories', '') != '') $xPathSettings['itemCategories'] = Minz_Request::param('xPathItemCategories', '', true);
  184. if (!empty($xPathSettings)) {
  185. $feed->_attributes('xpath', $xPathSettings);
  186. }
  187. }
  188. $values = array(
  189. 'name' => Minz_Request::param('name', ''),
  190. 'kind' => $feed_kind,
  191. 'description' => sanitizeHTML(Minz_Request::param('description', '', true)),
  192. 'website' => checkUrl(Minz_Request::param('website', '')),
  193. 'url' => checkUrl(Minz_Request::param('url', '')),
  194. 'category' => $cat,
  195. 'pathEntries' => Minz_Request::param('path_entries', ''),
  196. 'priority' => intval(Minz_Request::param('priority', FreshRSS_Feed::PRIORITY_MAIN_STREAM)),
  197. 'httpAuth' => $httpAuth,
  198. 'ttl' => $ttl * ($mute ? -1 : 1),
  199. 'attributes' => $feed->attributes(),
  200. );
  201. invalidateHttpCache();
  202. $from = Minz_Request::param('from');
  203. switch ($from) {
  204. case 'stats':
  205. $url_redirect = array('c' => 'stats', 'a' => 'idle', 'params' => array('id' => $id, 'from' => 'stats'));
  206. break;
  207. case 'normal':
  208. case 'reader':
  209. $get = Minz_Request::param('get');
  210. if ($get) {
  211. $url_redirect = array('c' => 'index', 'a' => $from, 'params' => array('get' => $get));
  212. } else {
  213. $url_redirect = array('c' => 'index', 'a' => $from);
  214. }
  215. break;
  216. default:
  217. $url_redirect = array('c' => 'subscription', 'params' => array('id' => $id));
  218. }
  219. if ($feedDAO->updateFeed($id, $values) !== false) {
  220. $feed->_category($cat);
  221. $feed->faviconPrepare();
  222. Minz_Request::good(_t('feedback.sub.feed.updated'), $url_redirect);
  223. } else {
  224. Minz_Request::bad(_t('feedback.sub.feed.error'), $url_redirect);
  225. }
  226. }
  227. }
  228. public function categoryAction() {
  229. $this->view->_layout(false);
  230. $categoryDAO = FreshRSS_Factory::createCategoryDao();
  231. $id = Minz_Request::param('id');
  232. $category = $categoryDAO->searchById($id);
  233. if ($id === false || null === $category) {
  234. Minz_Error::error(404);
  235. return;
  236. }
  237. $this->view->category = $category;
  238. if (Minz_Request::isPost()) {
  239. if (Minz_Request::paramBoolean('use_default_purge_options')) {
  240. $category->_attributes('archiving', null);
  241. } else {
  242. if (!Minz_Request::paramBoolean('enable_keep_max')) {
  243. $keepMax = false;
  244. } elseif (!$keepMax = Minz_Request::param('keep_max')) {
  245. $keepMax = FreshRSS_Feed::ARCHIVING_RETENTION_COUNT_LIMIT;
  246. }
  247. if ($enableRetentionPeriod = Minz_Request::paramBoolean('enable_keep_period')) {
  248. $keepPeriod = FreshRSS_Feed::ARCHIVING_RETENTION_PERIOD;
  249. if (is_numeric(Minz_Request::param('keep_period_count')) && preg_match('/^PT?1[YMWDH]$/', Minz_Request::param('keep_period_unit'))) {
  250. $keepPeriod = str_replace('1', Minz_Request::param('keep_period_count'), Minz_Request::param('keep_period_unit'));
  251. }
  252. } else {
  253. $keepPeriod = false;
  254. }
  255. $category->_attributes('archiving', [
  256. 'keep_period' => $keepPeriod,
  257. 'keep_max' => $keepMax,
  258. 'keep_min' => intval(Minz_Request::param('keep_min', 0)),
  259. 'keep_favourites' => Minz_Request::paramBoolean('keep_favourites'),
  260. 'keep_labels' => Minz_Request::paramBoolean('keep_labels'),
  261. 'keep_unreads' => Minz_Request::paramBoolean('keep_unreads'),
  262. ]);
  263. }
  264. $position = Minz_Request::param('position');
  265. $category->_attributes('position', '' === $position ? null : (int) $position);
  266. $values = [
  267. 'name' => Minz_Request::param('name', ''),
  268. 'attributes' => $category->attributes(),
  269. ];
  270. invalidateHttpCache();
  271. $url_redirect = array('c' => 'subscription', 'params' => array('id' => $id, 'type' => 'category'));
  272. if (false !== $categoryDAO->updateCategory($id, $values)) {
  273. Minz_Request::good(_t('feedback.sub.category.updated'), $url_redirect);
  274. } else {
  275. Minz_Request::bad(_t('feedback.sub.category.error'), $url_redirect);
  276. }
  277. }
  278. }
  279. /**
  280. * This action displays the bookmarklet page.
  281. */
  282. public function bookmarkletAction() {
  283. FreshRSS_View::prependTitle(_t('sub.title.subscription_tools') . ' . ');
  284. }
  285. /**
  286. * This action displays the page to add a new feed
  287. */
  288. public function addAction() {
  289. FreshRSS_View::appendScript(Minz_Url::display('/scripts/feed.js?' . @filemtime(PUBLIC_PATH . '/scripts/feed.js')));
  290. FreshRSS_View::prependTitle(_t('sub.title.add') . ' . ');
  291. }
  292. }