はじまりの大地

This commit is contained in:
miteruzo
2024-07-08 03:32:47 +09:00
commit c616a96f53
7749 changed files with 478270 additions and 0 deletions
+326
View File
@@ -0,0 +1,326 @@
<?php
namespace dokuwiki\plugin\config\core\Setting;
use dokuwiki\plugin\config\core\Configuration;
/**
* Class Setting
*/
class Setting
{
/** @var string unique identifier of this setting */
protected $key = '';
/** @var mixed the default value of this setting */
protected $default;
/** @var mixed the local value of this setting */
protected $local;
/** @var mixed the protected value of this setting */
protected $protected;
/** @var array valid alerts, images matching the alerts are in the plugin's images directory */
protected static $validCautions = ['warning', 'danger', 'security'];
protected $pattern = '';
protected $error = false; // only used by those classes which error check
protected $input; // only used by those classes which error check
protected $caution; // used by any setting to provide an alert along with the setting
/**
* Constructor.
*
* The given parameters will be set up as class properties
*
* @see initialize() to set the actual value of the setting
*
* @param string $key
* @param array|null $params array with metadata of setting
*/
public function __construct($key, $params = null)
{
$this->key = $key;
if (is_array($params)) {
foreach ($params as $property => $value) {
$property = trim($property, '_'); // we don't use underscores anymore
$this->$property = $value;
}
}
}
/**
* Set the current values for the setting $key
*
* This is used to initialize the setting with the data read form the config files.
*
* @see update() to set a new value
* @param mixed $default default setting value
* @param mixed $local local setting value
* @param mixed $protected protected setting value
*/
public function initialize($default = null, $local = null, $protected = null)
{
$this->default = $this->cleanValue($default);
$this->local = $this->cleanValue($local);
$this->protected = $this->cleanValue($protected);
}
/**
* update changed setting with validated user provided value $input
* - if changed value fails validation check, save it to $this->input (to allow echoing later)
* - if changed value passes validation check, set $this->local to the new value
*
* @param mixed $input the new value
* @return boolean true if changed, false otherwise
*/
public function update($input)
{
if (is_null($input)) return false;
if ($this->isProtected()) return false;
$input = $this->cleanValue($input);
$value = is_null($this->local) ? $this->default : $this->local;
if ($value == $input) return false;
// validate new value
if ($this->pattern && !preg_match($this->pattern, $input)) {
$this->error = true;
$this->input = $input;
return false;
}
// update local copy of this setting with new value
$this->local = $input;
// setting ready for update
return true;
}
/**
* Clean a value read from a config before using it internally
*
* Default implementation returns $value as is. Subclasses can override.
* Note: null should always be returned as null!
*
* This is applied in initialize() and update()
*
* @param mixed $value
* @return mixed
*/
protected function cleanValue($value)
{
return $value;
}
/**
* Should this type of config have a default?
*
* @return bool
*/
public function shouldHaveDefault()
{
return true;
}
/**
* Get this setting's unique key
*
* @return string
*/
public function getKey()
{
return $this->key;
}
/**
* Get the key of this setting marked up human readable
*
* @param bool $url link to dokuwiki.org manual?
* @return string
*/
public function getPrettyKey($url = true)
{
$out = str_replace(Configuration::KEYMARKER, "»", $this->key);
if ($url && !strstr($out, '»')) {//provide no urls for plugins, etc.
if ($out == 'start') {
// exception, because this config name is clashing with our actual start page
return '<a href="https://www.dokuwiki.org/config:startpage">' . $out . '</a>';
} else {
return '<a href="https://www.dokuwiki.org/config:' . $out . '">' . $out . '</a>';
}
}
return $out;
}
/**
* Returns setting key as an array key separator
*
* This is used to create form output
*
* @return string key
*/
public function getArrayKey()
{
return str_replace(Configuration::KEYMARKER, "']['", $this->key);
}
/**
* What type of configuration is this
*
* Returns one of
*
* 'plugin' for plugin configuration
* 'template' for template configuration
* 'dokuwiki' for core configuration
*
* @return string
*/
public function getType()
{
if (str_starts_with($this->getKey(), 'plugin' . Configuration::KEYMARKER)) {
return 'plugin';
} elseif (str_starts_with($this->getKey(), 'tpl' . Configuration::KEYMARKER)) {
return 'template';
} else {
return 'dokuwiki';
}
}
/**
* Build html for label and input of setting
*
* @param \admin_plugin_config $plugin object of config plugin
* @param bool $echo true: show inputted value, when error occurred, otherwise the stored setting
* @return string[] with content array(string $label_html, string $input_html)
*/
public function html(\admin_plugin_config $plugin, $echo = false)
{
$disable = '';
if ($this->isProtected()) {
$value = $this->protected;
$disable = 'disabled="disabled"';
} elseif ($echo && $this->error) {
$value = $this->input;
} else {
$value = is_null($this->local) ? $this->default : $this->local;
}
$key = htmlspecialchars($this->key);
$value = formText($value);
$label = '<label for="config___' . $key . '">' . $this->prompt($plugin) . '</label>';
$input = '<textarea rows="3" cols="40" id="config___' . $key .
'" name="config[' . $key . ']" class="edit" ' . $disable . '>' . $value . '</textarea>';
return [$label, $input];
}
/**
* Should the current local value be saved?
*
* @see out() to run when this returns true
* @return bool
*/
public function shouldBeSaved()
{
if ($this->isProtected()) return false;
if ($this->local === null) return false;
if ($this->default == $this->local) return false;
return true;
}
/**
* Escaping
*
* @param string $string
* @return string
*/
protected function escape($string)
{
$tr = ["\\" => '\\\\', "'" => '\\\''];
return "'" . strtr(cleanText($string), $tr) . "'";
}
/**
* Generate string to save local setting value to file according to $fmt
*
* @see shouldBeSaved() to check if this should be called
* @param string $var name of variable
* @param string $fmt save format
* @return string
*/
public function out($var, $fmt = 'php')
{
if ($fmt != 'php') return '';
if (is_array($this->local)) {
$value = 'array(' . implode(', ', array_map([$this, 'escape'], $this->local)) . ')';
} else {
$value = $this->escape($this->local);
}
$out = '$' . $var . "['" . $this->getArrayKey() . "'] = $value;\n";
return $out;
}
/**
* Returns the localized prompt
*
* @param \admin_plugin_config $plugin object of config plugin
* @return string text
*/
public function prompt(\admin_plugin_config $plugin)
{
$prompt = $plugin->getLang($this->key);
if (!$prompt) $prompt = htmlspecialchars(str_replace(['____', '_'], ' ', $this->key));
return $prompt;
}
/**
* Is setting protected
*
* @return bool
*/
public function isProtected()
{
return !is_null($this->protected);
}
/**
* Is setting the default?
*
* @return bool
*/
public function isDefault()
{
return !$this->isProtected() && is_null($this->local);
}
/**
* Has an error?
*
* @return bool
*/
public function hasError()
{
return $this->error;
}
/**
* Returns caution
*
* @return false|string caution string, otherwise false for invalid caution
*/
public function caution()
{
if (empty($this->caution)) return false;
if (!in_array($this->caution, Setting::$validCautions)) {
throw new \RuntimeException(
'Invalid caution string (' . $this->caution . ') in metadata for setting "' . $this->key . '"'
);
}
return $this->caution;
}
}
@@ -0,0 +1,87 @@
<?php
namespace dokuwiki\plugin\config\core\Setting;
/**
* Class setting_array
*/
class SettingArray extends Setting
{
/**
* Create an array from a string
*
* @param string $string
* @return array
*/
protected function fromString($string)
{
$array = explode(',', $string);
$array = array_map('trim', $array);
$array = array_filter($array);
$array = array_unique($array);
return $array;
}
/**
* Create a string from an array
*
* @param array $array
* @return string
*/
protected function fromArray($array)
{
return implode(', ', (array) $array);
}
/**
* update setting with user provided value $input
* if value fails error check, save it
*
* @param string $input
* @return bool true if changed, false otherwise (incl. on error)
*/
public function update($input)
{
if (is_null($input)) return false;
if ($this->isProtected()) return false;
$input = $this->fromString($input);
$value = is_null($this->local) ? $this->default : $this->local;
if ($value == $input) return false;
foreach ($input as $item) {
if ($this->pattern && !preg_match($this->pattern, $item)) {
$this->error = true;
$this->input = $input;
return false;
}
}
$this->local = $input;
return true;
}
/** @inheritdoc */
public function html(\admin_plugin_config $plugin, $echo = false)
{
$disable = '';
if ($this->isProtected()) {
$value = $this->protected;
$disable = 'disabled="disabled"';
} elseif ($echo && $this->error) {
$value = $this->input;
} else {
$value = is_null($this->local) ? $this->default : $this->local;
}
$key = htmlspecialchars($this->key);
$value = htmlspecialchars($this->fromArray($value));
$label = '<label for="config___' . $key . '">' . $this->prompt($plugin) . '</label>';
$input = '<input id="config___' . $key . '" name="config[' . $key .
']" type="text" class="edit" value="' . $value . '" ' . $disable . '/>';
return [$label, $input];
}
}
@@ -0,0 +1,62 @@
<?php
namespace dokuwiki\plugin\config\core\Setting;
/**
* Class setting_authtype
*/
class SettingAuthtype extends SettingMultichoice
{
/** @inheritdoc */
public function initialize($default = null, $local = null, $protected = null)
{
/** @var $plugin_controller \dokuwiki\Extension\PluginController */
global $plugin_controller;
// retrieve auth types provided by plugins
foreach ($plugin_controller->getList('auth') as $plugin) {
$this->choices[] = $plugin;
}
parent::initialize($default, $local, $protected);
}
/** @inheritdoc */
public function update($input)
{
/** @var $plugin_controller \dokuwiki\Extension\PluginController */
global $plugin_controller;
// is an update possible/requested?
$local = $this->local; // save this, parent::update() may change it
if (!parent::update($input)) return false; // nothing changed or an error caught by parent
$this->local = $local; // restore original, more error checking to come
// attempt to load the plugin
$auth_plugin = $plugin_controller->load('auth', $input);
// @TODO: throw an error in plugin controller instead of returning null
if (is_null($auth_plugin)) {
$this->error = true;
msg('Cannot load Auth Plugin "' . $input . '"', -1);
return false;
}
// verify proper instantiation (is this really a plugin?) @TODO use instanceof? implement interface?
if (is_object($auth_plugin) && !method_exists($auth_plugin, 'getPluginName')) {
$this->error = true;
msg('Cannot create Auth Plugin "' . $input . '"', -1);
return false;
}
// did we change the auth type? logout
global $conf;
if ($conf['authtype'] != $input) {
msg('Authentication system changed. Please re-login.');
auth_logoff();
}
$this->local = $input;
return true;
}
}
@@ -0,0 +1,22 @@
<?php
namespace dokuwiki\plugin\config\core\Setting;
/**
* Class setting_compression
*/
class SettingCompression extends SettingMultichoice
{
protected $choices = ['0']; // 0 = no compression, always supported
/** @inheritdoc */
public function initialize($default = null, $local = null, $protected = null)
{
// populate _choices with the compression methods supported by this php installation
if (function_exists('gzopen')) $this->choices[] = 'gz';
if (function_exists('bzopen')) $this->choices[] = 'bz2';
parent::initialize($default, $local, $protected);
}
}
@@ -0,0 +1,34 @@
<?php
namespace dokuwiki\plugin\config\core\Setting;
/**
* Class setting_dirchoice
*/
class SettingDirchoice extends SettingMultichoice
{
protected $dir = '';
/** @inheritdoc */
public function initialize($default = null, $local = null, $protected = null)
{
// populate $this->_choices with a list of directories
$list = [];
if ($dh = @opendir($this->dir)) {
while (false !== ($entry = readdir($dh))) {
if ($entry == '.' || $entry == '..') continue;
if ($this->pattern && !preg_match($this->pattern, $entry)) continue;
$file = (is_link($this->dir . $entry)) ? readlink($this->dir . $entry) : $this->dir . $entry;
if (is_dir($file)) $list[] = $entry;
}
closedir($dh);
}
sort($list);
$this->choices = $list;
parent::initialize($default, $local, $protected);
}
}
@@ -0,0 +1,24 @@
<?php
namespace dokuwiki\plugin\config\core\Setting;
/**
* Class setting_disableactions
*/
class SettingDisableactions extends SettingMulticheckbox
{
/** @inheritdoc */
public function html(\admin_plugin_config $plugin, $echo = false)
{
global $lang;
// make some language adjustments (there must be a better way)
// transfer some DokuWiki language strings to the plugin
$plugin->addLang($this->key . '_revisions', $lang['btn_revs']);
foreach ($this->choices as $choice) {
if (isset($lang['btn_' . $choice])) $plugin->addLang($this->key . '_' . $choice, $lang['btn_' . $choice]);
}
return parent::html($plugin, $echo);
}
}
@@ -0,0 +1,60 @@
<?php
namespace dokuwiki\plugin\config\core\Setting;
/**
* Class setting_email
*/
class SettingEmail extends SettingString
{
protected $multiple = false;
protected $placeholders = false;
/** @inheritdoc */
public function update($input)
{
if (is_null($input)) return false;
if ($this->isProtected()) return false;
$value = is_null($this->local) ? $this->default : $this->local;
if ($value == $input) return false;
if ($input === '') {
$this->local = $input;
return true;
}
$mail = $input;
if ($this->placeholders) {
// replace variables with pseudo values
$mail = str_replace('@USER@', 'joe', $mail);
$mail = str_replace('@NAME@', 'Joe Schmoe', $mail);
$mail = str_replace('@MAIL@', 'joe@example.com', $mail);
}
// multiple mail addresses?
if ($this->multiple) {
$mails = array_filter(array_map('trim', explode(',', $mail)));
} else {
$mails = [$mail];
}
// check them all
foreach ($mails as $mail) {
// only check the address part
if (preg_match('#(.*?)<(.*?)>#', $mail, $matches)) {
$addr = $matches[2];
} else {
$addr = $mail;
}
if (!mail_isvalid($addr)) {
$this->error = true;
$this->input = $input;
return false;
}
}
$this->local = $input;
return true;
}
}
@@ -0,0 +1,17 @@
<?php
namespace dokuwiki\plugin\config\core\Setting;
/**
* A do-nothing class used to detect the 'fieldset' type.
*
* Used to start a new settings "display-group".
*/
class SettingFieldset extends Setting
{
/** @inheritdoc */
public function shouldHaveDefault()
{
return false;
}
}
@@ -0,0 +1,11 @@
<?php
namespace dokuwiki\plugin\config\core\Setting;
/**
* Class setting_hidden
*/
class SettingHidden extends Setting
{
// Used to explicitly ignore a setting in the configuration manager.
}
@@ -0,0 +1,29 @@
<?php
namespace dokuwiki\plugin\config\core\Setting;
/**
* Class setting_im_convert
*/
class SettingImConvert extends SettingString
{
/** @inheritdoc */
public function update($input)
{
if ($this->isProtected()) return false;
$input = trim($input);
$value = is_null($this->local) ? $this->default : $this->local;
if ($value == $input) return false;
if ($input && !file_exists($input)) {
$this->error = true;
$this->input = $input;
return false;
}
$this->local = $input;
return true;
}
}
@@ -0,0 +1,24 @@
<?php
namespace dokuwiki\plugin\config\core\Setting;
/**
* Class setting_license
*/
class SettingLicense extends SettingMultichoice
{
protected $choices = ['']; // none choosen
/** @inheritdoc */
public function initialize($default = null, $local = null, $protected = null)
{
global $license;
foreach ($license as $key => $data) {
$this->choices[] = $key;
$this->lang[$this->key . '_o_' . $key] = $data['name']; // stored in setting
}
parent::initialize($default, $local, $protected);
}
}
@@ -0,0 +1,162 @@
<?php
namespace dokuwiki\plugin\config\core\Setting;
/**
* Class setting_multicheckbox
*/
class SettingMulticheckbox extends SettingString
{
protected $choices = [];
protected $combine = [];
protected $other = 'always';
/** @inheritdoc */
public function update($input)
{
if ($this->isProtected()) return false;
// split any combined values + convert from array to comma separated string
$input = $input ?: [];
$input = $this->array2str($input);
$value = is_null($this->local) ? $this->default : $this->local;
if ($value == $input) return false;
if ($this->pattern && !preg_match($this->pattern, $input)) {
$this->error = true;
$this->input = $input;
return false;
}
$this->local = $input;
return true;
}
/** @inheritdoc */
public function html(\admin_plugin_config $plugin, $echo = false)
{
$disable = '';
if ($this->isProtected()) {
$value = $this->protected;
$disable = 'disabled="disabled"';
} elseif ($echo && $this->error) {
$value = $this->input;
} else {
$value = is_null($this->local) ? $this->default : $this->local;
}
$key = htmlspecialchars($this->key);
// convert from comma separated list into array + combine complimentary actions
$value = $this->str2array($value);
$default = $this->str2array($this->default);
$input = '';
foreach ($this->choices as $choice) {
$idx = array_search($choice, $value);
$idx_default = array_search($choice, $default);
$checked = ($idx !== false) ? 'checked="checked"' : '';
// @todo ideally this would be handled using a second class of "default"
$class = (($idx !== false) === (false !== $idx_default)) ? " selectiondefault" : "";
$prompt = ($plugin->getLang($this->key . '_' . $choice) ?: htmlspecialchars($choice));
$input .= '<div class="selection' . $class . '">' . "\n";
$input .= '<label for="config___' . $key . '_' . $choice . '">' . $prompt . "</label>\n";
$input .= '<input id="config___' . $key . '_' . $choice . '" name="config[' . $key .
'][]" type="checkbox" class="checkbox" value="' . $choice . '" ' . $disable . ' ' . $checked . "/>\n";
$input .= "</div>\n";
// remove this action from the disabledactions array
if ($idx !== false) unset($value[$idx]);
if ($idx_default !== false) unset($default[$idx_default]);
}
// handle any remaining values
if ($this->other != 'never') {
$other = implode(',', $value);
// test equivalent to ($this->_other == 'always' || ($other && $this->_other == 'exists')
// use != 'exists' rather than == 'always' to ensure invalid values default to 'always'
if ($this->other != 'exists' || $other) {
$class = (
(count($default) === count($value)) &&
(count($value) === count(array_intersect($value, $default)))
) ?
" selectiondefault" : "";
$input .= '<div class="other' . $class . '">' . "\n";
$input .= '<label for="config___' . $key . '_other">' .
$plugin->getLang($key . '_other') .
"</label>\n";
$input .= '<input id="config___' . $key . '_other" name="config[' . $key .
'][other]" type="text" class="edit" value="' . htmlspecialchars($other) .
'" ' . $disable . " />\n";
$input .= "</div>\n";
}
}
$label = '<label>' . $this->prompt($plugin) . '</label>';
return [$label, $input];
}
/**
* convert comma separated list to an array and combine any complimentary values
*
* @param string $str
* @return array
*/
protected function str2array($str)
{
$array = explode(',', $str);
if (!empty($this->combine)) {
foreach ($this->combine as $key => $combinators) {
$idx = [];
foreach ($combinators as $val) {
if (($idx[] = array_search($val, $array)) === false) break;
}
if (count($idx) && $idx[count($idx) - 1] !== false) {
foreach ($idx as $i) unset($array[$i]);
$array[] = $key;
}
}
}
return $array;
}
/**
* convert array of values + other back to a comma separated list, incl. splitting any combined values
*
* @param array $input
* @return string
*/
protected function array2str($input)
{
// handle other
$other = trim($input['other']);
$other = empty($other) ? [] : explode(',', str_replace(' ', '', $input['other']));
unset($input['other']);
$array = array_unique(array_merge($input, $other));
// deconstruct any combinations
if (!empty($this->combine)) {
foreach ($this->combine as $key => $combinators) {
$idx = array_search($key, $array);
if ($idx !== false) {
unset($array[$idx]);
$array = array_merge($array, $combinators);
}
}
}
return implode(',', array_unique($array));
}
}
@@ -0,0 +1,74 @@
<?php
namespace dokuwiki\plugin\config\core\Setting;
/**
* Class setting_multichoice
*/
class SettingMultichoice extends SettingString
{
protected $choices = [];
public $lang; //some custom language strings are stored in setting
/** @inheritdoc */
public function html(\admin_plugin_config $plugin, $echo = false)
{
$disable = '';
$nochoice = '';
if ($this->isProtected()) {
$value = $this->protected;
$disable = ' disabled="disabled"';
} else {
$value = is_null($this->local) ? $this->default : $this->local;
}
// ensure current value is included
if (!in_array($value, $this->choices)) {
$this->choices[] = $value;
}
// disable if no other choices
if (!$this->isProtected() && count($this->choices) <= 1) {
$disable = ' disabled="disabled"';
$nochoice = $plugin->getLang('nochoice');
}
$key = htmlspecialchars($this->key);
$label = '<label for="config___' . $key . '">' . $this->prompt($plugin) . '</label>';
$input = "<div class=\"input\">\n";
$input .= '<select class="edit" id="config___' . $key . '" name="config[' . $key . ']"' . $disable . '>' . "\n";
foreach ($this->choices as $choice) {
$selected = ($value == $choice) ? ' selected="selected"' : '';
$option = $plugin->getLang($this->key . '_o_' . $choice);
if (!$option && isset($this->lang[$this->key . '_o_' . $choice])) {
$option = $this->lang[$this->key . '_o_' . $choice];
}
if (!$option) $option = $choice;
$choice = htmlspecialchars($choice);
$option = htmlspecialchars($option);
$input .= ' <option value="' . $choice . '"' . $selected . ' >' . $option . '</option>' . "\n";
}
$input .= "</select> $nochoice \n";
$input .= "</div>\n";
return [$label, $input];
}
/** @inheritdoc */
public function update($input)
{
if (is_null($input)) return false;
if ($this->isProtected()) return false;
$value = is_null($this->local) ? $this->default : $this->local;
if ($value == $input) return false;
if (!in_array($input, $this->choices)) return false;
$this->local = $input;
return true;
}
}
@@ -0,0 +1,13 @@
<?php
namespace dokuwiki\plugin\config\core\Setting;
/**
* Class setting_no_class
* A do-nothing class used to detect settings with a missing setting class.
* Used internaly to hide undefined settings, and generate the undefined settings list.
*/
class SettingNoClass extends SettingUndefined
{
protected $errorMessage = '_msg_setting_no_class';
}
@@ -0,0 +1,14 @@
<?php
namespace dokuwiki\plugin\config\core\Setting;
/**
* Class setting_no_default
*
* A do-nothing class used to detect settings with no default value.
* Used internaly to hide undefined settings, and generate the undefined settings list.
*/
class SettingNoDefault extends SettingUndefined
{
protected $errorMessage = '_msg_setting_no_default';
}
@@ -0,0 +1,12 @@
<?php
namespace dokuwiki\plugin\config\core\Setting;
/**
* A do-nothing class used to detect settings with a missing setting class.
* Used internaly to hide undefined settings, and generate the undefined settings list.
*/
class SettingNoKnownClass extends SettingUndefined
{
protected $errorMessage = '_msg_setting_no_known_class';
}
@@ -0,0 +1,47 @@
<?php
namespace dokuwiki\plugin\config\core\Setting;
/**
* Class setting_numeric
*/
class SettingNumeric extends SettingString
{
// This allows for many PHP syntax errors...
// var $_pattern = '/^[-+\/*0-9 ]*$/';
// much more restrictive, but should eliminate syntax errors.
protected $pattern = '/^[-+]? *\d+ *(?:[-+*] *\d+ *)*$/';
protected $min;
protected $max;
/** @inheritdoc */
public function update($input)
{
$local = $this->local;
$valid = parent::update($input);
if ($valid && !(is_null($this->min) && is_null($this->max))) {
$numeric_local = (int) eval('return ' . $this->local . ';');
if (
(!is_null($this->min) && $numeric_local < $this->min) ||
(!is_null($this->max) && $numeric_local > $this->max)
) {
$this->error = true;
$this->input = $input;
$this->local = $local;
$valid = false;
}
}
return $valid;
}
/** @inheritdoc */
public function out($var, $fmt = 'php')
{
if ($fmt != 'php') return '';
$local = $this->local === '' ? "''" : $this->local;
$out = '$' . $var . "['" . $this->getArrayKey() . "'] = " . $local . ";\n";
return $out;
}
}
@@ -0,0 +1,27 @@
<?php
namespace dokuwiki\plugin\config\core\Setting;
/**
* Class setting_numericopt
*/
class SettingNumericopt extends SettingNumeric
{
// just allow an empty config
protected $pattern = '/^(|[-]?\d+(?:[-+*]\d+)*)$/';
/**
* @inheritdoc
* Empty string is valid for numericopt
*/
public function update($input)
{
if ($input === '') {
if ($input == $this->local) return false;
$this->local = $input;
return true;
}
return parent::update($input);
}
}
@@ -0,0 +1,60 @@
<?php
namespace dokuwiki\plugin\config\core\Setting;
/**
* Class setting_onoff
*/
class SettingOnoff extends SettingNumeric
{
/**
* We treat the strings 'false' and 'off' as false
* @inheritdoc
*/
protected function cleanValue($value)
{
if ($value === null) return null;
if (is_string($value)) {
if (strtolower($value) === 'false') return 0;
if (strtolower($value) === 'off') return 0;
if (trim($value) === '') return 0;
}
return (int) (bool) $value;
}
/** @inheritdoc */
public function html(\admin_plugin_config $plugin, $echo = false)
{
$disable = '';
if ($this->isProtected()) {
$value = $this->protected;
$disable = ' disabled="disabled"';
} else {
$value = is_null($this->local) ? $this->default : $this->local;
}
$key = htmlspecialchars($this->key);
$checked = ($value) ? ' checked="checked"' : '';
$label = '<label for="config___' . $key . '">' . $this->prompt($plugin) . '</label>';
$input = '<div class="input"><input id="config___' . $key . '" name="config[' . $key .
']" type="checkbox" class="checkbox" value="1"' . $checked . $disable . '/></div>';
return [$label, $input];
}
/** @inheritdoc */
public function update($input)
{
if ($this->isProtected()) return false;
$input = ($input) ? 1 : 0;
$value = is_null($this->local) ? $this->default : $this->local;
if ($value == $input) return false;
$this->local = $input;
return true;
}
}
@@ -0,0 +1,41 @@
<?php
namespace dokuwiki\plugin\config\core\Setting;
/**
* Class setting_password
*/
class SettingPassword extends SettingString
{
protected $code = 'plain'; // mechanism to be used to obscure passwords
/** @inheritdoc */
public function update($input)
{
if ($this->isProtected()) return false;
if (!$input) return false;
if ($this->pattern && !preg_match($this->pattern, $input)) {
$this->error = true;
$this->input = $input;
return false;
}
$this->local = conf_encodeString($input, $this->code);
return true;
}
/** @inheritdoc */
public function html(\admin_plugin_config $plugin, $echo = false)
{
$disable = $this->isProtected() ? 'disabled="disabled"' : '';
$key = htmlspecialchars($this->key);
$label = '<label for="config___' . $key . '">' . $this->prompt($plugin) . '</label>';
$input = '<input id="config___' . $key . '" name="config[' . $key .
']" autocomplete="off" type="password" class="edit" value="" ' . $disable . ' />';
return [$label, $input];
}
}
@@ -0,0 +1,35 @@
<?php
namespace dokuwiki\plugin\config\core\Setting;
/**
* Class setting_regex
*/
class SettingRegex extends SettingString
{
protected $delimiter = '/'; // regex delimiter to be used in testing input
protected $pregflags = 'ui'; // regex pattern modifiers to be used in testing input
/** @inheritdoc */
public function update($input)
{
// let parent do basic checks, value, not changed, etc.
$local = $this->local;
if (!parent::update($input)) return false;
$this->local = $local;
// see if the regex compiles and runs (we don't check for effectiveness)
$regex = $this->delimiter . $input . $this->delimiter . $this->pregflags;
$lastError = error_get_last();
@preg_match($regex, 'testdata');
if (preg_last_error() != PREG_NO_ERROR || error_get_last() !== $lastError) {
$this->input = $input;
$this->error = true;
return false;
}
$this->local = $input;
return true;
}
}
@@ -0,0 +1,60 @@
<?php
/**
* additional setting classes specific to these settings
*
* @author Chris Smith <chris@jalakai.co.uk>
*/
namespace dokuwiki\plugin\config\core\Setting;
/**
* Class setting_renderer
*/
class SettingRenderer extends SettingMultichoice
{
protected $prompts = [];
protected $format;
/** @inheritdoc */
public function initialize($default = null, $local = null, $protected = null)
{
$format = $this->format;
foreach (plugin_list('renderer') as $plugin) {
$renderer = plugin_load('renderer', $plugin);
if ($renderer && method_exists($renderer, 'canRender') && $renderer->canRender($format)) {
$this->choices[] = $plugin;
$info = $renderer->getInfo();
$this->prompts[$plugin] = $info['name'];
}
}
parent::initialize($default, $local, $protected);
}
/** @inheritdoc */
public function html(\admin_plugin_config $plugin, $echo = false)
{
// make some language adjustments (there must be a better way)
// transfer some plugin names to the config plugin
foreach ($this->choices as $choice) {
if (!$plugin->getLang($this->key . '_o_' . $choice)) {
if (!isset($this->prompts[$choice])) {
$plugin->addLang(
$this->key . '_o_' . $choice,
sprintf($plugin->getLang('renderer__core'), $choice)
);
} else {
$plugin->addLang(
$this->key . '_o_' . $choice,
sprintf($plugin->getLang('renderer__plugin'), $this->prompts[$choice])
);
}
}
}
return parent::html($plugin, $echo);
}
}
@@ -0,0 +1,27 @@
<?php
namespace dokuwiki\plugin\config\core\Setting;
/**
* Class setting_savedir
*/
class SettingSavedir extends SettingString
{
/** @inheritdoc */
public function update($input)
{
if ($this->isProtected()) return false;
$value = is_null($this->local) ? $this->default : $this->local;
if ($value == $input) return false;
if (!init_path($input)) {
$this->error = true;
$this->input = $input;
return false;
}
$this->local = $input;
return true;
}
}
@@ -0,0 +1,19 @@
<?php
namespace dokuwiki\plugin\config\core\Setting;
/**
* Class setting_sepchar
*/
class SettingSepchar extends SettingMultichoice
{
/** @inheritdoc */
public function __construct($key, $param = null)
{
$str = '_-.';
for ($i = 0; $i < strlen($str); $i++) $this->choices[] = $str[$i];
// call foundation class constructor
parent::__construct($key, $param);
}
}
@@ -0,0 +1,32 @@
<?php
namespace dokuwiki\plugin\config\core\Setting;
/**
* Class setting_string
*/
class SettingString extends Setting
{
/** @inheritdoc */
public function html(\admin_plugin_config $plugin, $echo = false)
{
$disable = '';
if ($this->isProtected()) {
$value = $this->protected;
$disable = 'disabled="disabled"';
} elseif ($echo && $this->error) {
$value = $this->input;
} else {
$value = is_null($this->local) ? $this->default : $this->local;
}
$key = htmlspecialchars($this->key);
$value = htmlspecialchars($value);
$label = '<label for="config___' . $key . '">' . $this->prompt($plugin) . '</label>';
$input = '<input id="config___' . $key . '" name="config[' . $key .
']" type="text" class="edit" value="' . $value . '" ' . $disable . '/>';
return [$label, $input];
}
}
@@ -0,0 +1,43 @@
<?php
namespace dokuwiki\plugin\config\core\Setting;
use dokuwiki\plugin\config\core\Configuration;
/**
* A do-nothing class used to detect settings with no metadata entry.
* Used internaly to hide undefined settings, and generate the undefined settings list.
*/
class SettingUndefined extends SettingHidden
{
protected $errorMessage = '_msg_setting_undefined';
/** @inheritdoc */
public function shouldHaveDefault()
{
return false;
}
/** @inheritdoc */
public function html(\admin_plugin_config $plugin, $echo = false)
{
// determine the name the meta key would be called
if (
preg_match(
'/^(?:plugin|tpl)' . Configuration::KEYMARKER . '.*?' . Configuration::KEYMARKER . '(.*)$/',
$this->getKey(),
$undefined_setting_match
)
) {
$undefined_setting_key = $undefined_setting_match[1];
} else {
$undefined_setting_key = $this->getKey();
}
$label = '<span title="$meta[\'' . $undefined_setting_key . '\']">$' .
'conf' . '[\'' . $this->getArrayKey() . '\']</span>';
$input = $plugin->getLang($this->errorMessage);
return [$label, $input];
}
}