Configuration.class.php 3.5 KB

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