subscriptionController.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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::prependTitle(_t('sub.title') . ' · ');
  43. $this->view->onlyFeedsWithError = Minz_Request::paramTernary('error');
  44. $id = Minz_Request::param('id');
  45. $this->view->displaySlider = false;
  46. if (false !== $id) {
  47. $type = Minz_Request::param('type');
  48. $this->view->displaySlider = true;
  49. switch ($type) {
  50. case 'category':
  51. $categoryDAO = FreshRSS_Factory::createCategoryDao();
  52. $this->view->category = $categoryDAO->searchById($id);
  53. break;
  54. default:
  55. $feedDAO = FreshRSS_Factory::createFeedDao();
  56. $this->view->feed = $feedDAO->searchById($id);
  57. break;
  58. }
  59. }
  60. }
  61. /**
  62. * This action handles the feed configuration page.
  63. *
  64. * It displays the feed configuration page.
  65. * If this action is reached through a POST request, it stores all new
  66. * configuration values then sends a notification to the user.
  67. *
  68. * The options available on the page are:
  69. * - name
  70. * - description
  71. * - website URL
  72. * - feed URL
  73. * - category id (default: default category id)
  74. * - CSS path to article on website
  75. * - display in main stream (default: 0)
  76. * - HTTP authentication
  77. * - number of article to retain (default: -2)
  78. * - refresh frequency (default: 0)
  79. * Default values are empty strings unless specified.
  80. */
  81. public function feedAction() {
  82. if (Minz_Request::param('ajax')) {
  83. $this->view->_layout(false);
  84. }
  85. $feedDAO = FreshRSS_Factory::createFeedDao();
  86. $this->view->feeds = $feedDAO->listFeeds();
  87. $id = Minz_Request::param('id');
  88. if ($id === false || !isset($this->view->feeds[$id])) {
  89. Minz_Error::error(404);
  90. return;
  91. }
  92. $feed = $this->view->feeds[$id];
  93. $this->view->feed = $feed;
  94. FreshRSS_View::prependTitle(_t('sub.title.feed_management') . ' · ' . $feed->name() . ' · ');
  95. if (Minz_Request::isPost()) {
  96. $user = trim(Minz_Request::param('http_user_feed' . $id, ''));
  97. $pass = trim(Minz_Request::param('http_pass_feed' . $id, ''));
  98. $httpAuth = '';
  99. if ($user !== '' && $pass !== '') { //TODO: Sanitize
  100. $httpAuth = $user . ':' . $pass;
  101. }
  102. $cat = intval(Minz_Request::param('category', 0));
  103. $mute = Minz_Request::param('mute', false);
  104. $ttl = intval(Minz_Request::param('ttl', FreshRSS_Feed::TTL_DEFAULT));
  105. if ($mute && FreshRSS_Feed::TTL_DEFAULT === $ttl) {
  106. $ttl = FreshRSS_Context::$user_conf->ttl_default;
  107. }
  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. $useragent = Minz_Request::param('curl_params_useragent', '');
  125. $proxy_address = Minz_Request::param('curl_params', '');
  126. $proxy_type = Minz_Request::param('proxy_type', '');
  127. $opts = [];
  128. if ($proxy_address !== '' && $proxy_type !== '' && in_array($proxy_type, [0, 2, 4, 5, 6, 7])) {
  129. $opts[CURLOPT_PROXY] = $proxy_address;
  130. $opts[CURLOPT_PROXYTYPE] = intval($proxy_type);
  131. }
  132. if ($cookie !== '') {
  133. $opts[CURLOPT_COOKIE] = $cookie;
  134. }
  135. if ($useragent !== '') {
  136. $opts[CURLOPT_USERAGENT] = $useragent;
  137. }
  138. $feed->_attributes('curl_params', empty($opts) ? null : $opts);
  139. $feed->_attributes('content_action', Minz_Request::param('content_action', 'replace'));
  140. $feed->_attributes('ssl_verify', Minz_Request::paramTernary('ssl_verify'));
  141. $timeout = intval(Minz_Request::param('timeout', 0));
  142. $feed->_attributes('timeout', $timeout > 0 ? $timeout : null);
  143. if (Minz_Request::paramBoolean('use_default_purge_options')) {
  144. $feed->_attributes('archiving', null);
  145. } else {
  146. if (!Minz_Request::paramBoolean('enable_keep_max')) {
  147. $keepMax = false;
  148. } elseif (!$keepMax = Minz_Request::param('keep_max')) {
  149. $keepMax = FreshRSS_Feed::ARCHIVING_RETENTION_COUNT_LIMIT;
  150. }
  151. if ($enableRetentionPeriod = Minz_Request::paramBoolean('enable_keep_period')) {
  152. $keepPeriod = FreshRSS_Feed::ARCHIVING_RETENTION_PERIOD;
  153. if (is_numeric(Minz_Request::param('keep_period_count')) && preg_match('/^PT?1[YMWDH]$/', Minz_Request::param('keep_period_unit'))) {
  154. $keepPeriod = str_replace('1', Minz_Request::param('keep_period_count'), Minz_Request::param('keep_period_unit'));
  155. }
  156. } else {
  157. $keepPeriod = false;
  158. }
  159. $feed->_attributes('archiving', [
  160. 'keep_period' => $keepPeriod,
  161. 'keep_max' => $keepMax,
  162. 'keep_min' => intval(Minz_Request::param('keep_min', 0)),
  163. 'keep_favourites' => Minz_Request::paramBoolean('keep_favourites'),
  164. 'keep_labels' => Minz_Request::paramBoolean('keep_labels'),
  165. 'keep_unreads' => Minz_Request::paramBoolean('keep_unreads'),
  166. ]);
  167. }
  168. $feed->_filtersAction('read', preg_split('/[\n\r]+/', Minz_Request::param('filteractions_read', '')));
  169. $values = array(
  170. 'name' => Minz_Request::param('name', ''),
  171. 'description' => sanitizeHTML(Minz_Request::param('description', '', true)),
  172. 'website' => checkUrl(Minz_Request::param('website', '')),
  173. 'url' => checkUrl(Minz_Request::param('url', '')),
  174. 'category' => $cat,
  175. 'pathEntries' => Minz_Request::param('path_entries', ''),
  176. 'priority' => intval(Minz_Request::param('priority', FreshRSS_Feed::PRIORITY_MAIN_STREAM)),
  177. 'httpAuth' => $httpAuth,
  178. 'ttl' => $ttl * ($mute ? -1 : 1),
  179. 'attributes' => $feed->attributes(),
  180. );
  181. invalidateHttpCache();
  182. $from = Minz_Request::param('from');
  183. if ($from === false) {
  184. $url_redirect = array('c' => 'subscription', 'params' => array('id' => $id));
  185. } else {
  186. $url_redirect = array('c' => 'stats', 'a' => 'idle', 'params' => array('id' => $id, 'from' => 'stats'));
  187. }
  188. if ($feedDAO->updateFeed($id, $values) !== false) {
  189. $feed->_category($cat);
  190. $feed->faviconPrepare();
  191. Minz_Request::good(_t('feedback.sub.feed.updated'), $url_redirect);
  192. } else {
  193. Minz_Request::bad(_t('feedback.sub.feed.error'), $url_redirect);
  194. }
  195. }
  196. }
  197. public function categoryAction() {
  198. $this->view->_layout(false);
  199. $categoryDAO = FreshRSS_Factory::createCategoryDao();
  200. $id = Minz_Request::param('id');
  201. $category = $categoryDAO->searchById($id);
  202. if ($id === false || null === $category) {
  203. Minz_Error::error(404);
  204. return;
  205. }
  206. $this->view->category = $category;
  207. if (Minz_Request::isPost()) {
  208. if (Minz_Request::paramBoolean('use_default_purge_options')) {
  209. $category->_attributes('archiving', null);
  210. } else {
  211. if (!Minz_Request::paramBoolean('enable_keep_max')) {
  212. $keepMax = false;
  213. } elseif (!$keepMax = Minz_Request::param('keep_max')) {
  214. $keepMax = FreshRSS_Feed::ARCHIVING_RETENTION_COUNT_LIMIT;
  215. }
  216. if ($enableRetentionPeriod = Minz_Request::paramBoolean('enable_keep_period')) {
  217. $keepPeriod = FreshRSS_Feed::ARCHIVING_RETENTION_PERIOD;
  218. if (is_numeric(Minz_Request::param('keep_period_count')) && preg_match('/^PT?1[YMWDH]$/', Minz_Request::param('keep_period_unit'))) {
  219. $keepPeriod = str_replace('1', Minz_Request::param('keep_period_count'), Minz_Request::param('keep_period_unit'));
  220. }
  221. } else {
  222. $keepPeriod = false;
  223. }
  224. $category->_attributes('archiving', [
  225. 'keep_period' => $keepPeriod,
  226. 'keep_max' => $keepMax,
  227. 'keep_min' => intval(Minz_Request::param('keep_min', 0)),
  228. 'keep_favourites' => Minz_Request::paramBoolean('keep_favourites'),
  229. 'keep_labels' => Minz_Request::paramBoolean('keep_labels'),
  230. 'keep_unreads' => Minz_Request::paramBoolean('keep_unreads'),
  231. ]);
  232. }
  233. $position = Minz_Request::param('position');
  234. $category->_attributes('position', '' === $position ? null : (int) $position);
  235. $values = [
  236. 'name' => Minz_Request::param('name', ''),
  237. 'attributes' => $category->attributes(),
  238. ];
  239. invalidateHttpCache();
  240. $url_redirect = array('c' => 'subscription', 'params' => array('id' => $id, 'type' => 'category'));
  241. if (false !== $categoryDAO->updateCategory($id, $values)) {
  242. Minz_Request::good(_t('feedback.sub.category.updated'), $url_redirect);
  243. } else {
  244. Minz_Request::bad(_t('feedback.sub.category.error'), $url_redirect);
  245. }
  246. }
  247. }
  248. /**
  249. * This action displays the bookmarklet page.
  250. */
  251. public function bookmarkletAction() {
  252. FreshRSS_View::prependTitle(_t('sub.title.subscription_tools') . ' . ');
  253. }
  254. /**
  255. * This action displays the page to add a new feed
  256. */
  257. public function addAction() {
  258. FreshRSS_View::prependTitle(_t('sub.title.add') . ' . ');
  259. }
  260. }