PageRenderTime 27ms CodeModel.GetById 1ms RepoModel.GetById 0ms app.codeStats 0ms

/public/phpmyadmin/libraries/config/Form.class.php

https://gitlab.com/qbarbosa/klindev
PHP | 230 lines | 113 code | 18 blank | 99 comment | 14 complexity | 21e66e976917e38b54d91eaf446d4e3a MD5 | raw file
  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4. * Form handling code.
  5. *
  6. * @package PhpMyAdmin
  7. */
  8. /**
  9. * Base class for forms, loads default configuration options, checks allowed
  10. * values etc.
  11. *
  12. * @package PhpMyAdmin
  13. */
  14. class Form
  15. {
  16. /**
  17. * Form name
  18. * @var string
  19. */
  20. public $name;
  21. /**
  22. * Arbitrary index, doesn't affect class' behavior
  23. * @var int
  24. */
  25. public $index;
  26. /**
  27. * Form fields (paths), filled by {@link readFormPaths()}, indexed by field name
  28. * @var array
  29. */
  30. public $fields;
  31. /**
  32. * Stores default values for some fields (eg. pmadb tables)
  33. * @var array
  34. */
  35. public $default;
  36. /**
  37. * Caches field types, indexed by field names
  38. * @var array
  39. */
  40. private $_fieldsTypes;
  41. /**
  42. * ConfigFile instance
  43. * @var ConfigFile
  44. */
  45. private $_configFile;
  46. /**
  47. * Constructor, reads default config values
  48. *
  49. * @param string $form_name Form name
  50. * @param array $form Form data
  51. * @param ConfigFile $cf Config file instance
  52. * @param int $index arbitrary index, stored in Form::$index
  53. */
  54. public function __construct(
  55. $form_name, array $form, ConfigFile $cf, $index = null
  56. ) {
  57. $this->index = $index;
  58. $this->_configFile = $cf;
  59. $this->loadForm($form_name, $form);
  60. }
  61. /**
  62. * Returns type of given option
  63. *
  64. * @param string $option_name path or field name
  65. *
  66. * @return string|null one of: boolean, integer, double, string, select, array
  67. */
  68. public function getOptionType($option_name)
  69. {
  70. $key = ltrim(
  71. /*overload*/mb_substr(
  72. $option_name,
  73. /*overload*/mb_strrpos($option_name, '/')
  74. ),
  75. '/'
  76. );
  77. return isset($this->_fieldsTypes[$key])
  78. ? $this->_fieldsTypes[$key]
  79. : null;
  80. }
  81. /**
  82. * Returns allowed values for select fields
  83. *
  84. * @param string $option_path Option path
  85. *
  86. * @return array
  87. */
  88. public function getOptionValueList($option_path)
  89. {
  90. $value = $this->_configFile->getDbEntry($option_path);
  91. if ($value === null) {
  92. trigger_error("$option_path - select options not defined", E_USER_ERROR);
  93. return array();
  94. }
  95. if (!is_array($value)) {
  96. trigger_error("$option_path - not a static value list", E_USER_ERROR);
  97. return array();
  98. }
  99. // convert array('#', 'a', 'b') to array('a', 'b')
  100. if (isset($value[0]) && $value[0] === '#') {
  101. // remove first element ('#')
  102. array_shift($value);
  103. // $value has keys and value names, return it
  104. return $value;
  105. }
  106. // convert value list array('a', 'b') to array('a' => 'a', 'b' => 'b')
  107. $has_string_keys = false;
  108. $keys = array();
  109. for ($i = 0, $nb = count($value); $i < $nb; $i++) {
  110. if (!isset($value[$i])) {
  111. $has_string_keys = true;
  112. break;
  113. }
  114. $keys[] = is_bool($value[$i]) ? (int)$value[$i] : $value[$i];
  115. }
  116. if (! $has_string_keys) {
  117. $value = array_combine($keys, $value);
  118. }
  119. // $value has keys and value names, return it
  120. return $value;
  121. }
  122. /**
  123. * array_walk callback function, reads path of form fields from
  124. * array (see file comment in setup.forms.php or user_preferences.forms.inc)
  125. *
  126. * @param mixed $value Value
  127. * @param mixed $key Key
  128. * @param mixed $prefix Prefix
  129. *
  130. * @return void
  131. */
  132. private function _readFormPathsCallback($value, $key, $prefix)
  133. {
  134. static $group_counter = 0;
  135. if (is_array($value)) {
  136. $prefix .= $key . '/';
  137. array_walk($value, array($this, '_readFormPathsCallback'), $prefix);
  138. return;
  139. }
  140. if (!is_int($key)) {
  141. $this->default[$prefix . $key] = $value;
  142. $value = $key;
  143. }
  144. // add unique id to group ends
  145. if ($value == ':group:end') {
  146. $value .= ':' . $group_counter++;
  147. }
  148. $this->fields[] = $prefix . $value;
  149. }
  150. /**
  151. * Reads form paths to {@link $fields}
  152. *
  153. * @param array $form Form
  154. *
  155. * @return void
  156. */
  157. protected function readFormPaths($form)
  158. {
  159. // flatten form fields' paths and save them to $fields
  160. $this->fields = array();
  161. array_walk($form, array($this, '_readFormPathsCallback'), '');
  162. // $this->fields is an array of the form: [0..n] => 'field path'
  163. // change numeric indexes to contain field names (last part of the path)
  164. $paths = $this->fields;
  165. $this->fields = array();
  166. foreach ($paths as $path) {
  167. $key = ltrim(
  168. /*overload*/mb_substr($path, /*overload*/mb_strrpos($path, '/')),
  169. '/'
  170. );
  171. $this->fields[$key] = $path;
  172. }
  173. // now $this->fields is an array of the form: 'field name' => 'field path'
  174. }
  175. /**
  176. * Reads fields' types to $this->_fieldsTypes
  177. *
  178. * @return void
  179. */
  180. protected function readTypes()
  181. {
  182. $cf = $this->_configFile;
  183. foreach ($this->fields as $name => $path) {
  184. if (/*overload*/mb_strpos($name, ':group:') === 0) {
  185. $this->_fieldsTypes[$name] = 'group';
  186. continue;
  187. }
  188. $v = $cf->getDbEntry($path);
  189. if ($v !== null) {
  190. $type = is_array($v) ? 'select' : $v;
  191. } else {
  192. $type = gettype($cf->getDefault($path));
  193. }
  194. $this->_fieldsTypes[$name] = $type;
  195. }
  196. }
  197. /**
  198. * Reads form settings and prepares class to work with given subset of
  199. * config file
  200. *
  201. * @param string $form_name Form name
  202. * @param array $form Form
  203. *
  204. * @return void
  205. */
  206. public function loadForm($form_name, $form)
  207. {
  208. $this->name = $form_name;
  209. $this->readFormPaths($form);
  210. $this->readTypes();
  211. }
  212. }