subscriptionController.php 12 KB

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