Configuration.class.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <?php
  2. /**
  3. * Manage application and plugins configurations with key/value pair.
  4. *
  5. * **nb:** It's possible to specify namespace in order to distinct global configuration to plugin custom configuration
  6. *
  7. * @author valentin carruesco
  8. *
  9. * @category Core
  10. *
  11. * @license copyright
  12. */
  13. class Configuration extends Entity
  14. {
  15. protected $id,$key,$value,$confTab;
  16. protected $fields =
  17. array(
  18. 'id' => 'key',
  19. 'key' => 'longstring',
  20. 'value' => 'longstring',
  21. );
  22. /**
  23. * Get all configurations from database OR session if it was yet loaded
  24. * 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.
  25. * #### Example
  26. * ```php
  27. * $confs = Configuration::getAll();
  28. * var_dump($confs);
  29. * ```.
  30. *
  31. * @return array Array of configurations
  32. */
  33. public function getAll()
  34. {
  35. if (!isset($_SESSION['configuration'])) {
  36. $configurationManager = new self();
  37. $configs = $configurationManager->populate();
  38. $confTab = array();
  39. foreach ($configs as $config) {
  40. $this->confTab[$config->getKey()] = Functions::decrypt($config->getValue());
  41. }
  42. $_SESSION['configuration'] = serialize($this->confTab);
  43. } else {
  44. $this->confTab = unserialize($_SESSION['configuration']);
  45. }
  46. }
  47. /**
  48. * Get configuration value from it key
  49. * #### Example
  50. * ```php
  51. * global $conf; // global var, contain configurations
  52. * echo $conf->get('myConfigKey'); // print myConfigKey value
  53. * ```.
  54. *
  55. * @param string configuration key
  56. * @param string configuration namespace (default is 'conf')
  57. *
  58. * @return string valeur de la configuration
  59. */
  60. public function get($key)
  61. {
  62. return isset($this->confTab[$key]) ? $this->confTab[$key] : '';
  63. }
  64. /**
  65. * Update or insert configuration value in database with specified key
  66. * #### Example
  67. * ```php
  68. * global $conf; // global var, contain configurations
  69. * echo $conf->put('myNewConfigKey','hello!'); //create configuration myNewConfigKey with value 'hello!'
  70. * echo $conf->put('myNewConfigKey','hello 2!'); //update configuration myNewConfigKey with value 'hello2!'
  71. * ```.
  72. *
  73. * @param string configuration key
  74. * @param string configuration value
  75. * @param string configuration namespace (default is 'conf')
  76. */
  77. public function put($key, $value)
  78. {
  79. $secured_value = Functions::crypt($value);
  80. $configurationManager = new self();
  81. if (isset($this->confTab[$key])) {
  82. $configurationManager->change(array('value' => $secured_value), array('key' => $key));
  83. } else {
  84. $configurationManager->add($key, $secured_value);
  85. }
  86. $this->confTab[$key] = $value;
  87. unset($_SESSION['configuration']);
  88. }
  89. /**
  90. * Remove configuration value in database with specified key
  91. * #### Example
  92. * ```php
  93. * global $conf; // global var, contain configurations
  94. * echo $conf->remove('myNewConfigKey'); //delete myNewConfigKey from 'conf' default namespace
  95. * echo $conf->remove('myNewConfigKey','myCustomPluginConfig'); //delete myNewConfigKey from 'myCustomPluginConfig' namespace
  96. * ```.
  97. *
  98. * @param string configuration key
  99. * @param string configuration namespace (default is 'conf')
  100. */
  101. public function add($key, $value)
  102. {
  103. $config = new self();
  104. $config->setKey($key);
  105. $config->setValue($value);
  106. $config->save();
  107. $this->confTab[$key] = $value;
  108. unset($_SESSION['configuration']);
  109. }
  110. public function getId()
  111. {
  112. return $this->id;
  113. }
  114. public function getKey()
  115. {
  116. return $this->key;
  117. }
  118. public function setKey($key)
  119. {
  120. $this->key = $key;
  121. }
  122. public function getValue()
  123. {
  124. return $this->value;
  125. }
  126. public function setValue($value)
  127. {
  128. $this->value = $value;
  129. }
  130. }