ConfigurationSetter.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. <?php
  2. class FreshRSS_ConfigurationSetter {
  3. /**
  4. * Return if the given key is supported by this setter.
  5. * @param $key the key to test.
  6. * @return true if the key is supported, false else.
  7. */
  8. public function support($key) {
  9. $name_setter = '_' . $key;
  10. return is_callable(array($this, $name_setter));
  11. }
  12. /**
  13. * Set the given key in data with the current value.
  14. * @param $data an array containing the list of all configuration data.
  15. * @param $key the key to update.
  16. * @param $value the value to set.
  17. */
  18. public function handle(&$data, $key, $value) {
  19. $name_setter = '_' . $key;
  20. call_user_func_array(array($this, $name_setter), array(&$data, $value));
  21. }
  22. /**
  23. * The (long) list of setters.
  24. */
  25. private function _apiPasswordHash(&$data, $value) {
  26. $data['apiPasswordHash'] = ctype_graph($value) && (strlen($value) >= 60) ? $value : '';
  27. }
  28. private function _content_width(&$data, $value) {
  29. if (!in_array($value, array('thin', 'medium', 'large', 'no_limit'))) {
  30. $value = 'thin';
  31. }
  32. $data['content_width'] = $value;
  33. }
  34. private function _default_state(&$data, $value) {
  35. $data['default_state'] = (int)$value;
  36. }
  37. private function _default_view(&$data, $value) {
  38. switch ($value) {
  39. case 'all':
  40. $data['default_view'] = $value;
  41. $data['default_state'] = (FreshRSS_Entry::STATE_READ +
  42. FreshRSS_Entry::STATE_NOT_READ);
  43. break;
  44. case 'adaptive':
  45. case 'unread':
  46. default:
  47. $data['default_view'] = $value;
  48. $data['default_state'] = FreshRSS_Entry::STATE_NOT_READ;
  49. }
  50. }
  51. private function _html5_notif_timeout(&$data, $value) {
  52. $value = intval($value);
  53. $data['html5_notif_timeout'] = $value >= 0 ? $value : 0;
  54. }
  55. private function _keep_history_default(&$data, $value) {
  56. $value = intval($value);
  57. $data['keep_history_default'] = $value >= -1 ? $value : 0;
  58. }
  59. private function _language(&$data, $value) {
  60. $languages = Minz_Translate::availableLanguages();
  61. if (!isset($languages[$value])) {
  62. $value = 'en';
  63. }
  64. $data['language'] = $value;
  65. }
  66. private function _mail_login(&$data, $value) {
  67. $value = filter_var($value, FILTER_VALIDATE_EMAIL);
  68. $data['mail_login'] = $value ? $value : '';
  69. }
  70. private function _old_entries(&$data, $value) {
  71. $value = intval($value);
  72. $data['old_entries'] = $value > 0 ? $value : 3;
  73. }
  74. private function _passwordHash(&$data, $value) {
  75. $data['passwordHash'] = ctype_graph($value) && (strlen($value) >= 60) ? $value : '';
  76. }
  77. private function _posts_per_page(&$data, $value) {
  78. $value = intval($value);
  79. $data['posts_per_page'] = $value > 0 ? $value : 10;
  80. }
  81. private function _queries(&$data, $values) {
  82. $data['queries'] = array();
  83. foreach ($values as $value) {
  84. $value = array_filter($value);
  85. $params = $value;
  86. unset($params['name']);
  87. unset($params['url']);
  88. $value['url'] = Minz_Url::display(array('params' => $params));
  89. $data['queries'][] = $value;
  90. }
  91. }
  92. private function _sharing(&$data, $values) {
  93. $data['sharing'] = array();
  94. foreach ($values as $value) {
  95. if (!is_array($value)) {
  96. continue;
  97. }
  98. // Verify URL and add default value when needed
  99. if (isset($value['url'])) {
  100. $is_url = (
  101. filter_var($value['url'], FILTER_VALIDATE_URL) ||
  102. (version_compare(PHP_VERSION, '5.3.3', '<') &&
  103. (strpos($value, '-') > 0) &&
  104. ($value === filter_var($value, FILTER_SANITIZE_URL)))
  105. ); //PHP bug #51192
  106. if (!$is_url) {
  107. continue;
  108. }
  109. } else {
  110. $value['url'] = null;
  111. }
  112. $data['sharing'][] = $value;
  113. }
  114. }
  115. private function _shortcuts(&$data, $values) {
  116. foreach ($values as $key => $value) {
  117. if (isset($data['shortcuts'][$key])) {
  118. $data['shortcuts'][$key] = $value;
  119. }
  120. }
  121. }
  122. private function _sort_order(&$data, $value) {
  123. $data['sort_order'] = $value === 'ASC' ? 'ASC' : 'DESC';
  124. }
  125. private function _ttl_default(&$data, $value) {
  126. $value = intval($value);
  127. $data['ttl_default'] = $value >= -1 ? $value : 3600;
  128. }
  129. private function _view_mode(&$data, $value) {
  130. if (!in_array($value, array('global', 'normal', 'reader'))) {
  131. $value = 'normal';
  132. }
  133. $data['view_mode'] = $value;
  134. }
  135. /**
  136. * A list of boolean setters.
  137. */
  138. private function handleBool($value) {
  139. return ((bool)$value) && $value !== 'no';
  140. }
  141. private function _anon_access(&$data, $value) {
  142. $data['anon_access'] = $this->handleBool($value);
  143. }
  144. private function _auto_load_more(&$data, $value) {
  145. $data['auto_load_more'] = $this->handleBool($value);
  146. }
  147. private function _auto_remove_article(&$data, $value) {
  148. $data['auto_remove_article'] = $this->handleBool($value);
  149. }
  150. private function _display_categories(&$data, $value) {
  151. $data['display_categories'] = $this->handleBool($value);
  152. }
  153. private function _display_posts(&$data, $value) {
  154. $data['display_posts'] = $this->handleBool($value);
  155. }
  156. private function _hide_read_feeds(&$data, $value) {
  157. $data['hide_read_feeds'] = $this->handleBool($value);
  158. }
  159. private function _lazyload(&$data, $value) {
  160. $data['lazyload'] = $this->handleBool($value);
  161. }
  162. private function _mark_when(&$data, $values) {
  163. foreach ($values as $key => $value) {
  164. if (isset($data['mark_when'][$key])) {
  165. $data['mark_when'][$key] = $this->handleBool($value);
  166. }
  167. }
  168. }
  169. private function _onread_jump_next(&$data, $value) {
  170. $data['onread_jump_next'] = $this->handleBool($value);
  171. }
  172. private function _reading_confirm(&$data, $value) {
  173. $data['reading_confirm'] = $this->handleBool($value);
  174. }
  175. private function _sticky_post(&$data, $value) {
  176. $data['sticky_post'] = $this->handleBool($value);
  177. }
  178. private function _bottomline_date(&$data, $value) {
  179. $data['bottomline_date'] = $this->handleBool($value);
  180. }
  181. private function _bottomline_favorite(&$data, $value) {
  182. $data['bottomline_favorite'] = $this->handleBool($value);
  183. }
  184. private function _bottomline_link(&$data, $value) {
  185. $data['bottomline_link'] = $this->handleBool($value);
  186. }
  187. private function _bottomline_read(&$data, $value) {
  188. $data['bottomline_read'] = $this->handleBool($value);
  189. }
  190. private function _bottomline_sharing(&$data, $value) {
  191. $data['bottomline_sharing'] = $this->handleBool($value);
  192. }
  193. private function _bottomline_tags(&$data, $value) {
  194. $data['bottomline_tags'] = $this->handleBool($value);
  195. }
  196. private function _topline_date(&$data, $value) {
  197. $data['topline_date'] = $this->handleBool($value);
  198. }
  199. private function _topline_favorite(&$data, $value) {
  200. $data['topline_favorite'] = $this->handleBool($value);
  201. }
  202. private function _topline_link(&$data, $value) {
  203. $data['topline_link'] = $this->handleBool($value);
  204. }
  205. private function _topline_read(&$data, $value) {
  206. $data['topline_read'] = $this->handleBool($value);
  207. }
  208. }