Configuration.class.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. <?php
  2. /**
  3. * Manage application and plugins configurations with key/value pair.
  4. * **nb:** It's possible to specify namespace in order to distinct global configuration to plugin custom configuration
  5. * @author valentin carruesco
  6. * @category Core
  7. * @license copyright
  8. */
  9. class Configuration extends Entity
  10. {
  11. public $id,$key,$value;
  12. protected $confTab;
  13. protected $fields =
  14. array(
  15. 'id' => 'key',
  16. 'key' => 'longstring',
  17. 'value' => 'longstring',
  18. );
  19. /**
  20. * Get all configurations from database OR session if it was yet loaded
  21. * This function is called at start of program and global var '$conf' is filled with response, so use global $conf instead of call this function.
  22. * #### Example
  23. * ```php
  24. * $confs = Configuration::getAll();
  25. * var_dump($confs);
  26. * ```.
  27. * @return array Array of configurations
  28. */
  29. public function getAll()
  30. {
  31. if (!isset($_SESSION['configuration'])) {
  32. $configurationManager = new self();
  33. $configs = $configurationManager->loadAll();
  34. $confTab = array();
  35. foreach ($configs as $config) {
  36. $this->confTab[$config->key] = decrypt($config->value);
  37. }
  38. $_SESSION['configuration'] = serialize($this->confTab);
  39. } else {
  40. $this->confTab = unserialize($_SESSION['configuration']);
  41. }
  42. }
  43. //Définit / récupere un set de configurations générique
  44. public static function setting($name,$settings=null){
  45. if(!isset($settings)) return isset($GLOBALS['setting'][$name]) ? $GLOBALS['setting'][$name] : array();
  46. $GLOBALS['setting'][$name] = $settings;
  47. }
  48. //Met en page (tableau html) un set de configuration générique
  49. public static function html($name){
  50. global $conf;
  51. $options = isset($GLOBALS['setting'][$name]) ? $GLOBALS['setting'][$name] : array(); ?>
  52. <table id="<?php echo $name; ?>-setting-form" class="table table-striped <?php echo $name; ?>-setting-form">
  53. <tbody>
  54. <?php foreach($options as $key=>$infos):
  55. $input = '';
  56. if(!is_array($infos)): ?>
  57. <tr><th colspan="2"><h4 class="m-0"><i class="fas fa-angle-right"></i> <?php echo $infos;?></h4></th></tr>
  58. <?php continue; endif; ?>
  59. <tr class="<?php echo $key; ?>">
  60. <th class="align-middle"><?php echo $infos['label']; ?>
  61. <?php if (isset($infos['legend'])): ?>
  62. <small class="text-muted"> - <?php echo $infos['legend']; ?></small>
  63. <?php endif; ?>
  64. </th>
  65. <td class="align-middle position-relative">
  66. <?php $attributes = array();
  67. if(isset($infos['placeholder'])) $attributes['placeholder'] = 'placeholder="'.$infos['placeholder'].'"';
  68. $attributes['class'] = 'class="form-control"';
  69. $attributes['id'] = 'id="'.$key.'"';
  70. $confValue = $conf->get($key);
  71. $attributes['value'] = 'value="'.(isset($infos['default']) && empty($confValue) ? htmlentities($infos['default']) : htmlentities($confValue)).'"';
  72. if(isset($infos['parameters'])){
  73. foreach($infos['parameters'] as $attribute=>$parameter){
  74. if(isset($attributes[$attribute])) continue;
  75. $attributes[$attribute] = $attribute.'="'.$parameter.'"';
  76. }
  77. }
  78. switch($infos['type']){
  79. case 'password':
  80. $attributes['type'] = 'data-type="password"';
  81. $input = '<input type="text" autocomplete="new-password" '.implode(' ',$attributes).' >';
  82. break;
  83. case 'dictionnary':
  84. $attributes['type'] = 'data-type="dictionnary"';
  85. $attributes['parameters'] = 'data-slug="'.$infos['slug'].'"';
  86. $input = '<select type="text" '.implode(' ',$attributes).' >';
  87. break;
  88. case 'checkbox':
  89. unset($attributes['class']);
  90. $attributes['type'] = 'data-type="checkbox"';
  91. if(isset($infos['default']) && $confValue == '')
  92. $attributes['value'] = $infos['default'] ? 'checked="checked"' : '';
  93. else
  94. $attributes['value'] = $confValue ? 'checked="checked"' : '';
  95. $input = '<label class="mb-0 pointer no-select"><input type="checkbox" '.implode(' ',$attributes).' > Activer</label>';
  96. break;
  97. case 'select':
  98. unset($attributes['value']);
  99. $input = '<select '.implode(' ',$attributes).'>';
  100. foreach($infos['values'] as $id=>$label)
  101. $input .= '<option value="'.$id.'" '.($id==$confValue || (isset($infos['default']) && $infos['default'] == $id && $confValue == '')?'selected="selected"':'').'>'.$label.'</option>';
  102. $input .= '</select>';
  103. break;
  104. case 'user':
  105. $attributes['data-type'] = 'data-type="user"';
  106. $input = '<input type="text" '.implode(' ',$attributes).'>';
  107. break;
  108. case 'rank':
  109. $attributes['data-type'] = 'data-type="user" data-types="rank"';
  110. $input = '<input type="text" '.implode(' ',$attributes).' >';
  111. break;
  112. case 'number':
  113. $input = '<input type="number" onkeydown="return input_number_control(event,true,true);" '.implode(' ',$attributes).' >';
  114. break;
  115. case 'hour':
  116. $input = '<input type="text" data-type="hour" '.implode(' ',$attributes).' >';
  117. break;
  118. case 'date':
  119. $input = '<input type="text" data-type="date" '.implode(' ',$attributes).' >';
  120. break;
  121. case 'textarea':
  122. $input = '<textarea '.implode(' ',$attributes).' >'.htmlentities($conf->get($key)).'</textarea>';
  123. break;
  124. case 'wysiwyg':
  125. $attributes['data-type'] = 'data-type="wysiwyg"';
  126. $attributes['class'] = 'class="m-0"';
  127. unset($attributes['value']);
  128. $input = '<textarea '.implode(' ',$attributes).' >'.htmlentities($conf->get($key)).'</textarea>';
  129. break;
  130. case 'file':
  131. //Si input file "multiple", possibilité de normlaiser le
  132. //tableau $_FILES récupéré avec la fonction => normalize_php_files();
  133. unset($attributes['class']);
  134. $input = '<input type="file" '.implode(' ',$attributes).' >';
  135. break;
  136. case 'dropzone':
  137. $attributes['id'] = 'id="document"';
  138. $documents = preg_replace("/documents=\"(.*)\"/i", "$1", $attributes['documents']);
  139. unset($attributes['documents']);
  140. $input = '<div data-type="dropzone" '.(isset($attributes['data-label']) ? $attributes['data-label'] : 'Faites glisser vos documents').' '.(isset($attributes['data-delete']) ? $attributes['data-delete'] : '').' '.(isset($attributes['data-allowed']) ? $attributes['data-allowed'] : '').' class="form-control" '.implode(' ',$attributes).'>'.$documents.'</div>';
  141. break;
  142. default:
  143. Plugin::callHook('configuration_fields',array(&$input, $infos, $attributes));
  144. if(empty($input)) $input = '<input type="text" '.implode(' ',$attributes).' >';
  145. break;
  146. }
  147. echo $input; ?>
  148. </td>
  149. </tr>
  150. <?php endforeach; ?>
  151. </tbody>
  152. </table>
  153. <?php
  154. }
  155. /**
  156. * Get configuration value from it key
  157. * #### Example
  158. * ```php
  159. * global $conf; // global var, contain configurations
  160. * echo $conf->get('myConfigKey'); // print myConfigKey value
  161. * ```.
  162. * @param string configuration key
  163. * @param string configuration namespace (default is 'conf')
  164. *
  165. * @return string valeur de la configuration
  166. */
  167. public function get($key)
  168. {
  169. return isset($this->confTab[$key]) ? htmlspecialchars_decode($this->confTab[$key]) : '';
  170. }
  171. /**
  172. * Update or insert configuration value in database with specified key
  173. * #### Example
  174. * ```php
  175. * global $conf; // global var, contain configurations
  176. * echo $conf->put('myNewConfigKey','hello!'); //create configuration myNewConfigKey with value 'hello!'
  177. * echo $conf->put('myNewConfigKey','hello 2!'); //update configuration myNewConfigKey with value 'hello2!'
  178. * ```.
  179. *
  180. * @param string configuration key
  181. * @param string configuration value
  182. * @param string configuration namespace (default is 'conf')
  183. */
  184. public function put($key, $value)
  185. {
  186. $secured_value = encrypt($value);
  187. $configurationManager = new self();
  188. if (isset($this->confTab[$key])) {
  189. $configurationManager->change(array('value' => $secured_value), array('key' => $key));
  190. } else {
  191. $configurationManager->add($key, $secured_value);
  192. }
  193. $this->confTab[$key] = $value;
  194. unset($_SESSION['configuration']);
  195. }
  196. /**
  197. * Remove configuration value in database with specified key
  198. * #### Example
  199. * ```php
  200. * global $conf; // global var, contain configurations
  201. * echo $conf->remove('myNewConfigKey'); //delete myNewConfigKey from 'conf' default namespace
  202. * echo $conf->remove('myNewConfigKey','myCustomPluginConfig'); //delete myNewConfigKey from 'myCustomPluginConfig' namespace
  203. * ```.
  204. *
  205. * @param string configuration key
  206. * @param string configuration namespace (default is 'conf')
  207. */
  208. public function add($key, $value)
  209. {
  210. $config = new self();
  211. $config->key = $key;
  212. $config->value = $value;
  213. $config->save();
  214. $this->confTab[$key] = $value;
  215. unset($_SESSION['configuration']);
  216. }
  217. }