subscriptionController.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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->listCategories(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 = 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. if (FreshRSS_Auth::hasAccess('admin')) {
  99. $feed->_attributes('ssl_verify', Minz_Request::paramTernary('ssl_verify'));
  100. $timeout = intval(Minz_Request::param('timeout', 0));
  101. $feed->_attributes('timeout', $timeout > 0 ? $timeout : null);
  102. } else {
  103. $feed->_attributes('ssl_verify', null);
  104. $feed->_attributes('timeout', null);
  105. }
  106. if (Minz_Request::paramBoolean('use_default_purge_options')) {
  107. $feed->_attributes('archiving', null);
  108. } else {
  109. if (!Minz_Request::paramBoolean('enable_keep_max')) {
  110. $keepMax = false;
  111. } elseif (!$keepMax = Minz_Request::param('keep_max')) {
  112. $keepMax = FreshRSS_Feed::ARCHIVING_RETENTION_COUNT_LIMIT;
  113. }
  114. if ($enableRetentionPeriod = Minz_Request::paramBoolean('enable_keep_period')) {
  115. $keepPeriod = FreshRSS_Feed::ARCHIVING_RETENTION_PERIOD;
  116. if (is_numeric(Minz_Request::param('keep_period_count')) && preg_match('/^PT?1[YMWDH]$/', Minz_Request::param('keep_period_unit'))) {
  117. $keepPeriod = str_replace(1, Minz_Request::param('keep_period_count'), Minz_Request::param('keep_period_unit'));
  118. }
  119. } else {
  120. $keepPeriod = false;
  121. }
  122. $feed->_attributes('archiving', [
  123. 'keep_period' => $keepPeriod,
  124. 'keep_max' => $keepMax,
  125. 'keep_min' => intval(Minz_Request::param('keep_min', 0)),
  126. 'keep_favourites' => Minz_Request::paramBoolean('keep_favourites'),
  127. 'keep_labels' => Minz_Request::paramBoolean('keep_labels'),
  128. 'keep_unreads' => Minz_Request::paramBoolean('keep_unreads'),
  129. ]);
  130. }
  131. $feed->_filtersAction('read', preg_split('/[\n\r]+/', Minz_Request::param('filteractions_read', '')));
  132. $values = array(
  133. 'name' => Minz_Request::param('name', ''),
  134. 'description' => sanitizeHTML(Minz_Request::param('description', '', true)),
  135. 'website' => checkUrl(Minz_Request::param('website', '')),
  136. 'url' => checkUrl(Minz_Request::param('url', '')),
  137. 'category' => $cat,
  138. 'pathEntries' => Minz_Request::param('path_entries', ''),
  139. 'priority' => intval(Minz_Request::param('priority', FreshRSS_Feed::PRIORITY_MAIN_STREAM)),
  140. 'httpAuth' => $httpAuth,
  141. 'ttl' => $ttl * ($mute ? -1 : 1),
  142. 'attributes' => $feed->attributes(),
  143. );
  144. invalidateHttpCache();
  145. $url_redirect = array('c' => 'subscription', 'params' => array('id' => $id));
  146. if ($feedDAO->updateFeed($id, $values) !== false) {
  147. $feed->_category($cat);
  148. $feed->faviconPrepare();
  149. Minz_Request::good(_t('feedback.sub.feed.updated'), $url_redirect);
  150. } else {
  151. Minz_Request::bad(_t('feedback.sub.feed.error'), $url_redirect);
  152. }
  153. }
  154. }
  155. public function categoryAction() {
  156. $this->view->_layout(false);
  157. $categoryDAO = FreshRSS_Factory::createCategoryDao();
  158. $id = Minz_Request::param('id');
  159. $category = $categoryDAO->searchById($id);
  160. if ($id === false || null === $category) {
  161. Minz_Error::error(404);
  162. return;
  163. }
  164. $this->view->category = $category;
  165. if (Minz_Request::isPost()) {
  166. if (Minz_Request::paramBoolean('use_default_purge_options')) {
  167. $category->_attributes('archiving', null);
  168. } else {
  169. if (!Minz_Request::paramBoolean('enable_keep_max')) {
  170. $keepMax = false;
  171. } elseif (!$keepMax = Minz_Request::param('keep_max')) {
  172. $keepMax = FreshRSS_Feed::ARCHIVING_RETENTION_COUNT_LIMIT;
  173. }
  174. if ($enableRetentionPeriod = Minz_Request::paramBoolean('enable_keep_period')) {
  175. $keepPeriod = FreshRSS_Feed::ARCHIVING_RETENTION_PERIOD;
  176. if (is_numeric(Minz_Request::param('keep_period_count')) && preg_match('/^PT?1[YMWDH]$/', Minz_Request::param('keep_period_unit'))) {
  177. $keepPeriod = str_replace(1, Minz_Request::param('keep_period_count'), Minz_Request::param('keep_period_unit'));
  178. }
  179. } else {
  180. $keepPeriod = false;
  181. }
  182. $category->_attributes('archiving', [
  183. 'keep_period' => $keepPeriod,
  184. 'keep_max' => $keepMax,
  185. 'keep_min' => intval(Minz_Request::param('keep_min', 0)),
  186. 'keep_favourites' => Minz_Request::paramBoolean('keep_favourites'),
  187. 'keep_labels' => Minz_Request::paramBoolean('keep_labels'),
  188. 'keep_unreads' => Minz_Request::paramBoolean('keep_unreads'),
  189. ]);
  190. }
  191. $values = [
  192. 'name' => Minz_Request::param('name', ''),
  193. 'attributes' => $category->attributes(),
  194. ];
  195. invalidateHttpCache();
  196. $url_redirect = array('c' => 'subscription', 'params' => array('id' => $id, 'type' => 'category'));
  197. if (false !== $categoryDAO->updateCategory($id, $values)) {
  198. Minz_Request::good(_t('feedback.sub.category.updated'), $url_redirect);
  199. } else {
  200. Minz_Request::bad(_t('feedback.sub.category.error'), $url_redirect);
  201. }
  202. }
  203. }
  204. /**
  205. * This action displays the bookmarklet page.
  206. */
  207. public function bookmarkletAction() {
  208. Minz_View::prependTitle(_t('sub.title.subscription_tools') . ' . ');
  209. }
  210. }