subscriptionController.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. <?php
  2. /**
  3. * Controller to handle subscription actions.
  4. */
  5. class FreshRSS_subscription_Controller extends Minz_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. }
  22. /**
  23. * This action handles the main subscription page
  24. *
  25. * It displays categories and associated feeds.
  26. */
  27. public function indexAction() {
  28. Minz_View::appendScript(Minz_Url::display('/scripts/category.js?' . @filemtime(PUBLIC_PATH . '/scripts/category.js')));
  29. Minz_View::prependTitle(_t('sub.title') . ' · ');
  30. $this->view->onlyFeedsWithError = Minz_Request::paramTernary('error');
  31. $id = Minz_Request::param('id');
  32. $this->view->displaySlider = false;
  33. if (false !== $id) {
  34. $type = Minz_Request::param('type');
  35. $this->view->displaySlider = true;
  36. switch ($type) {
  37. case 'category':
  38. $categoryDAO = FreshRSS_Factory::createCategoryDao();
  39. $this->view->category = $categoryDAO->searchById($id);
  40. break;
  41. default:
  42. $feedDAO = FreshRSS_Factory::createFeedDao();
  43. $this->view->feed = $feedDAO->searchById($id);
  44. break;
  45. }
  46. }
  47. }
  48. /**
  49. * This action handles the feed configuration page.
  50. *
  51. * It displays the feed configuration page.
  52. * If this action is reached through a POST request, it stores all new
  53. * configuraiton values then sends a notification to the user.
  54. *
  55. * The options available on the page are:
  56. * - name
  57. * - description
  58. * - website URL
  59. * - feed URL
  60. * - category id (default: default category id)
  61. * - CSS path to article on website
  62. * - display in main stream (default: 0)
  63. * - HTTP authentication
  64. * - number of article to retain (default: -2)
  65. * - refresh frequency (default: 0)
  66. * Default values are empty strings unless specified.
  67. */
  68. public function feedAction() {
  69. if (Minz_Request::param('ajax')) {
  70. $this->view->_layout(false);
  71. }
  72. $feedDAO = FreshRSS_Factory::createFeedDao();
  73. $this->view->feeds = $feedDAO->listFeeds();
  74. $id = Minz_Request::param('id');
  75. if ($id === false || !isset($this->view->feeds[$id])) {
  76. Minz_Error::error(404);
  77. return;
  78. }
  79. $feed = $this->view->feeds[$id];
  80. $this->view->feed = $feed;
  81. Minz_View::prependTitle(_t('sub.title.feed_management') . ' · ' . $feed->name() . ' · ');
  82. if (Minz_Request::isPost()) {
  83. $user = trim(Minz_Request::param('http_user_feed' . $id, ''));
  84. $pass = trim(Minz_Request::param('http_pass_feed' . $id, ''));
  85. $httpAuth = '';
  86. if ($user != '' && $pass != '') { //TODO: Sanitize
  87. $httpAuth = $user . ':' . $pass;
  88. }
  89. $cat = intval(Minz_Request::param('category', 0));
  90. $mute = Minz_Request::param('mute', false);
  91. $ttl = intval(Minz_Request::param('ttl', FreshRSS_Feed::TTL_DEFAULT));
  92. if ($mute && FreshRSS_Feed::TTL_DEFAULT === $ttl) {
  93. $ttl = FreshRSS_Context::$user_conf->ttl_default;
  94. }
  95. $feed->_attributes('mark_updated_article_unread', Minz_Request::paramTernary('mark_updated_article_unread'));
  96. $feed->_attributes('read_upon_reception', Minz_Request::paramTernary('read_upon_reception'));
  97. $feed->_attributes('clear_cache', Minz_Request::paramTernary('clear_cache'));
  98. $cookie = Minz_Request::param('curl_params_cookie', '');
  99. $proxy_address = Minz_Request::param('curl_params', '');
  100. $proxy_type = Minz_Request::param('proxy_type', '');
  101. $opts = [];
  102. if ($proxy_address != '' && $proxy_type != '' && in_array($proxy_type, [0, 2, 4, 5, 6, 7])) {
  103. $opts[CURLOPT_PROXY] = $proxy_address;
  104. $opts[CURLOPT_PROXYTYPE] = intval($proxy_type);
  105. }
  106. if ($cookie != '') {
  107. $opts[CURLOPT_COOKIE] = $cookie;
  108. }
  109. $feed->_attributes('curl_params', empty($opts) ? null : $opts);
  110. if (FreshRSS_Auth::hasAccess('admin')) {
  111. $feed->_attributes('ssl_verify', Minz_Request::paramTernary('ssl_verify'));
  112. $timeout = intval(Minz_Request::param('timeout', 0));
  113. $feed->_attributes('timeout', $timeout > 0 ? $timeout : null);
  114. } else {
  115. $feed->_attributes('ssl_verify', null);
  116. $feed->_attributes('timeout', null);
  117. }
  118. if (Minz_Request::paramBoolean('use_default_purge_options')) {
  119. $feed->_attributes('archiving', null);
  120. } else {
  121. if (!Minz_Request::paramBoolean('enable_keep_max')) {
  122. $keepMax = false;
  123. } elseif (!$keepMax = Minz_Request::param('keep_max')) {
  124. $keepMax = FreshRSS_Feed::ARCHIVING_RETENTION_COUNT_LIMIT;
  125. }
  126. if ($enableRetentionPeriod = Minz_Request::paramBoolean('enable_keep_period')) {
  127. $keepPeriod = FreshRSS_Feed::ARCHIVING_RETENTION_PERIOD;
  128. if (is_numeric(Minz_Request::param('keep_period_count')) && preg_match('/^PT?1[YMWDH]$/', Minz_Request::param('keep_period_unit'))) {
  129. $keepPeriod = str_replace(1, Minz_Request::param('keep_period_count'), Minz_Request::param('keep_period_unit'));
  130. }
  131. } else {
  132. $keepPeriod = false;
  133. }
  134. $feed->_attributes('archiving', [
  135. 'keep_period' => $keepPeriod,
  136. 'keep_max' => $keepMax,
  137. 'keep_min' => intval(Minz_Request::param('keep_min', 0)),
  138. 'keep_favourites' => Minz_Request::paramBoolean('keep_favourites'),
  139. 'keep_labels' => Minz_Request::paramBoolean('keep_labels'),
  140. 'keep_unreads' => Minz_Request::paramBoolean('keep_unreads'),
  141. ]);
  142. }
  143. $feed->_filtersAction('read', preg_split('/[\n\r]+/', Minz_Request::param('filteractions_read', '')));
  144. $values = array(
  145. 'name' => Minz_Request::param('name', ''),
  146. 'description' => sanitizeHTML(Minz_Request::param('description', '', true)),
  147. 'website' => checkUrl(Minz_Request::param('website', '')),
  148. 'url' => checkUrl(Minz_Request::param('url', '')),
  149. 'category' => $cat,
  150. 'pathEntries' => Minz_Request::param('path_entries', ''),
  151. 'priority' => intval(Minz_Request::param('priority', FreshRSS_Feed::PRIORITY_MAIN_STREAM)),
  152. 'httpAuth' => $httpAuth,
  153. 'ttl' => $ttl * ($mute ? -1 : 1),
  154. 'attributes' => $feed->attributes(),
  155. );
  156. invalidateHttpCache();
  157. $url_redirect = array('c' => 'subscription', 'params' => array('id' => $id));
  158. if ($feedDAO->updateFeed($id, $values) !== false) {
  159. $feed->_category($cat);
  160. $feed->faviconPrepare();
  161. Minz_Request::good(_t('feedback.sub.feed.updated'), $url_redirect);
  162. } else {
  163. Minz_Request::bad(_t('feedback.sub.feed.error'), $url_redirect);
  164. }
  165. }
  166. }
  167. public function categoryAction() {
  168. $this->view->_layout(false);
  169. $categoryDAO = FreshRSS_Factory::createCategoryDao();
  170. $id = Minz_Request::param('id');
  171. $category = $categoryDAO->searchById($id);
  172. if ($id === false || null === $category) {
  173. Minz_Error::error(404);
  174. return;
  175. }
  176. $this->view->category = $category;
  177. if (Minz_Request::isPost()) {
  178. if (Minz_Request::paramBoolean('use_default_purge_options')) {
  179. $category->_attributes('archiving', null);
  180. } else {
  181. if (!Minz_Request::paramBoolean('enable_keep_max')) {
  182. $keepMax = false;
  183. } elseif (!$keepMax = Minz_Request::param('keep_max')) {
  184. $keepMax = FreshRSS_Feed::ARCHIVING_RETENTION_COUNT_LIMIT;
  185. }
  186. if ($enableRetentionPeriod = Minz_Request::paramBoolean('enable_keep_period')) {
  187. $keepPeriod = FreshRSS_Feed::ARCHIVING_RETENTION_PERIOD;
  188. if (is_numeric(Minz_Request::param('keep_period_count')) && preg_match('/^PT?1[YMWDH]$/', Minz_Request::param('keep_period_unit'))) {
  189. $keepPeriod = str_replace(1, Minz_Request::param('keep_period_count'), Minz_Request::param('keep_period_unit'));
  190. }
  191. } else {
  192. $keepPeriod = false;
  193. }
  194. $category->_attributes('archiving', [
  195. 'keep_period' => $keepPeriod,
  196. 'keep_max' => $keepMax,
  197. 'keep_min' => intval(Minz_Request::param('keep_min', 0)),
  198. 'keep_favourites' => Minz_Request::paramBoolean('keep_favourites'),
  199. 'keep_labels' => Minz_Request::paramBoolean('keep_labels'),
  200. 'keep_unreads' => Minz_Request::paramBoolean('keep_unreads'),
  201. ]);
  202. }
  203. $position = Minz_Request::param('position');
  204. $category->_attributes('position', '' === $position ? null : (int) $position);
  205. $values = [
  206. 'name' => Minz_Request::param('name', ''),
  207. 'attributes' => $category->attributes(),
  208. ];
  209. invalidateHttpCache();
  210. $url_redirect = array('c' => 'subscription', 'params' => array('id' => $id, 'type' => 'category'));
  211. if (false !== $categoryDAO->updateCategory($id, $values)) {
  212. Minz_Request::good(_t('feedback.sub.category.updated'), $url_redirect);
  213. } else {
  214. Minz_Request::bad(_t('feedback.sub.category.error'), $url_redirect);
  215. }
  216. }
  217. }
  218. /**
  219. * This action displays the bookmarklet page.
  220. */
  221. public function bookmarkletAction() {
  222. Minz_View::prependTitle(_t('sub.title.subscription_tools') . ' . ');
  223. }
  224. /**
  225. * This action displays the page to add a new feed
  226. */
  227. public function addAction() {
  228. Minz_View::prependTitle(_t('sub.title.add') . ' . ');
  229. }
  230. }