Setting.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. <?php
  2. namespace dokuwiki\plugin\config\core\Setting;
  3. use dokuwiki\plugin\config\core\Configuration;
  4. /**
  5. * Class Setting
  6. */
  7. class Setting {
  8. /** @var string unique identifier of this setting */
  9. protected $key = '';
  10. /** @var mixed the default value of this setting */
  11. protected $default = null;
  12. /** @var mixed the local value of this setting */
  13. protected $local = null;
  14. /** @var mixed the protected value of this setting */
  15. protected $protected = null;
  16. /** @var array valid alerts, images matching the alerts are in the plugin's images directory */
  17. static protected $validCautions = array('warning', 'danger', 'security');
  18. protected $pattern = '';
  19. protected $error = false; // only used by those classes which error check
  20. protected $input = null; // only used by those classes which error check
  21. protected $caution = null; // used by any setting to provide an alert along with the setting
  22. /**
  23. * Constructor.
  24. *
  25. * The given parameters will be set up as class properties
  26. *
  27. * @see initialize() to set the actual value of the setting
  28. *
  29. * @param string $key
  30. * @param array|null $params array with metadata of setting
  31. */
  32. public function __construct($key, $params = null) {
  33. $this->key = $key;
  34. if(is_array($params)) {
  35. foreach($params as $property => $value) {
  36. $property = trim($property, '_'); // we don't use underscores anymore
  37. $this->$property = $value;
  38. }
  39. }
  40. }
  41. /**
  42. * Set the current values for the setting $key
  43. *
  44. * This is used to initialize the setting with the data read form the config files.
  45. *
  46. * @see update() to set a new value
  47. * @param mixed $default default setting value
  48. * @param mixed $local local setting value
  49. * @param mixed $protected protected setting value
  50. */
  51. public function initialize($default = null, $local = null, $protected = null) {
  52. $this->default = $this->cleanValue($default);
  53. $this->local = $this->cleanValue($local);
  54. $this->protected = $this->cleanValue($protected);
  55. }
  56. /**
  57. * update changed setting with validated user provided value $input
  58. * - if changed value fails validation check, save it to $this->input (to allow echoing later)
  59. * - if changed value passes validation check, set $this->local to the new value
  60. *
  61. * @param mixed $input the new value
  62. * @return boolean true if changed, false otherwise
  63. */
  64. public function update($input) {
  65. if(is_null($input)) return false;
  66. if($this->isProtected()) return false;
  67. $input = $this->cleanValue($input);
  68. $value = is_null($this->local) ? $this->default : $this->local;
  69. if($value == $input) return false;
  70. // validate new value
  71. if($this->pattern && !preg_match($this->pattern, $input)) {
  72. $this->error = true;
  73. $this->input = $input;
  74. return false;
  75. }
  76. // update local copy of this setting with new value
  77. $this->local = $input;
  78. // setting ready for update
  79. return true;
  80. }
  81. /**
  82. * Clean a value read from a config before using it internally
  83. *
  84. * Default implementation returns $value as is. Subclasses can override.
  85. * Note: null should always be returned as null!
  86. *
  87. * This is applied in initialize() and update()
  88. *
  89. * @param mixed $value
  90. * @return mixed
  91. */
  92. protected function cleanValue($value) {
  93. return $value;
  94. }
  95. /**
  96. * Should this type of config have a default?
  97. *
  98. * @return bool
  99. */
  100. public function shouldHaveDefault() {
  101. return true;
  102. }
  103. /**
  104. * Get this setting's unique key
  105. *
  106. * @return string
  107. */
  108. public function getKey() {
  109. return $this->key;
  110. }
  111. /**
  112. * Get the key of this setting marked up human readable
  113. *
  114. * @param bool $url link to dokuwiki.org manual?
  115. * @return string
  116. */
  117. public function getPrettyKey($url = true) {
  118. $out = str_replace(Configuration::KEYMARKER, "»", $this->key);
  119. if($url && !strstr($out, '»')) {//provide no urls for plugins, etc.
  120. if($out == 'start') {
  121. // exception, because this config name is clashing with our actual start page
  122. return '<a href="https://www.dokuwiki.org/config:startpage">' . $out . '</a>';
  123. } else {
  124. return '<a href="https://www.dokuwiki.org/config:' . $out . '">' . $out . '</a>';
  125. }
  126. }
  127. return $out;
  128. }
  129. /**
  130. * Returns setting key as an array key separator
  131. *
  132. * This is used to create form output
  133. *
  134. * @return string key
  135. */
  136. public function getArrayKey() {
  137. return str_replace(Configuration::KEYMARKER, "']['", $this->key);
  138. }
  139. /**
  140. * What type of configuration is this
  141. *
  142. * Returns one of
  143. *
  144. * 'plugin' for plugin configuration
  145. * 'template' for template configuration
  146. * 'dokuwiki' for core configuration
  147. *
  148. * @return string
  149. */
  150. public function getType() {
  151. if(substr($this->getKey(), 0, 10) == 'plugin' . Configuration::KEYMARKER) {
  152. return 'plugin';
  153. } else if(substr($this->getKey(), 0, 7) == 'tpl' . Configuration::KEYMARKER) {
  154. return 'template';
  155. } else {
  156. return 'dokuwiki';
  157. }
  158. }
  159. /**
  160. * Build html for label and input of setting
  161. *
  162. * @param \admin_plugin_config $plugin object of config plugin
  163. * @param bool $echo true: show inputted value, when error occurred, otherwise the stored setting
  164. * @return string[] with content array(string $label_html, string $input_html)
  165. */
  166. public function html(\admin_plugin_config $plugin, $echo = false) {
  167. $disable = '';
  168. if($this->isProtected()) {
  169. $value = $this->protected;
  170. $disable = 'disabled="disabled"';
  171. } else {
  172. if($echo && $this->error) {
  173. $value = $this->input;
  174. } else {
  175. $value = is_null($this->local) ? $this->default : $this->local;
  176. }
  177. }
  178. $key = htmlspecialchars($this->key);
  179. $value = formText($value);
  180. $label = '<label for="config___' . $key . '">' . $this->prompt($plugin) . '</label>';
  181. $input = '<textarea rows="3" cols="40" id="config___' . $key .
  182. '" name="config[' . $key . ']" class="edit" ' . $disable . '>' . $value . '</textarea>';
  183. return array($label, $input);
  184. }
  185. /**
  186. * Should the current local value be saved?
  187. *
  188. * @see out() to run when this returns true
  189. * @return bool
  190. */
  191. public function shouldBeSaved() {
  192. if($this->isProtected()) return false;
  193. if($this->local === null) return false;
  194. if($this->default == $this->local) return false;
  195. return true;
  196. }
  197. /**
  198. * Escaping
  199. *
  200. * @param string $string
  201. * @return string
  202. */
  203. protected function escape($string) {
  204. $tr = array("\\" => '\\\\', "'" => '\\\'');
  205. return "'" . strtr(cleanText($string), $tr) . "'";
  206. }
  207. /**
  208. * Generate string to save local setting value to file according to $fmt
  209. *
  210. * @see shouldBeSaved() to check if this should be called
  211. * @param string $var name of variable
  212. * @param string $fmt save format
  213. * @return string
  214. */
  215. public function out($var, $fmt = 'php') {
  216. if ($fmt != 'php') return '';
  217. if (is_array($this->local)) {
  218. $value = 'array(' . join(', ', array_map([$this, 'escape'], $this->local)) . ')';
  219. } else {
  220. $value = $this->escape($this->local);
  221. }
  222. $out = '$' . $var . "['" . $this->getArrayKey() . "'] = $value;\n";
  223. return $out;
  224. }
  225. /**
  226. * Returns the localized prompt
  227. *
  228. * @param \admin_plugin_config $plugin object of config plugin
  229. * @return string text
  230. */
  231. public function prompt(\admin_plugin_config $plugin) {
  232. $prompt = $plugin->getLang($this->key);
  233. if(!$prompt) $prompt = htmlspecialchars(str_replace(array('____', '_'), ' ', $this->key));
  234. return $prompt;
  235. }
  236. /**
  237. * Is setting protected
  238. *
  239. * @return bool
  240. */
  241. public function isProtected() {
  242. return !is_null($this->protected);
  243. }
  244. /**
  245. * Is setting the default?
  246. *
  247. * @return bool
  248. */
  249. public function isDefault() {
  250. return !$this->isProtected() && is_null($this->local);
  251. }
  252. /**
  253. * Has an error?
  254. *
  255. * @return bool
  256. */
  257. public function hasError() {
  258. return $this->error;
  259. }
  260. /**
  261. * Returns caution
  262. *
  263. * @return false|string caution string, otherwise false for invalid caution
  264. */
  265. public function caution() {
  266. if(empty($this->caution)) return false;
  267. if(!in_array($this->caution, Setting::$validCautions)) {
  268. throw new \RuntimeException(
  269. 'Invalid caution string (' . $this->caution . ') in metadata for setting "' . $this->key . '"'
  270. );
  271. }
  272. return $this->caution;
  273. }
  274. }