Configuration.class.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <?php
  2. /*
  3. @nom: Configuration
  4. @auteur: Idleman (http://blog.idleman.fr)
  5. @description: Classe de gestion des préférences, fonctionne sur un simple système clé=>valeur avec un cache session pour eviter les requête inutiles
  6. */
  7. class Configuration extends MysqlEntity{
  8. protected $id,$key,$value,$confTab;
  9. protected $TABLE_NAME = 'configuration';
  10. protected $object_fields =
  11. array(
  12. 'id'=>'key',
  13. 'key'=>'string',
  14. 'value'=>'longstring'
  15. );
  16. protected $object_fields_uniques =
  17. array(
  18. 'key'
  19. );
  20. protected $options = array(
  21. 'articleDisplayAnonymous' => '0',
  22. 'articleDisplayAuthor' => '1',
  23. 'articleDisplayDate' => '1',
  24. 'articleDisplayFolderSort' => '1',
  25. 'articleDisplayHomeSort' => '1',
  26. 'articleDisplayLink' => '1',
  27. 'articleDisplayMode' => 'summary',
  28. 'articlePerPages' => '5',
  29. 'displayOnlyUnreadFeedFolder' => 'false',
  30. 'feedMaxEvents' => '50',
  31. 'language' => 'en',
  32. 'optionFeedIsVerbose' => 1,
  33. 'paginationScale' => 5,
  34. 'syncGradCount' => '10',
  35. 'synchronisationCode' => '',
  36. 'synchronisationEnableCache' => '0',
  37. 'synchronisationForceFeed' => '0',
  38. 'synchronisationType' => 'auto',
  39. 'theme' => 'marigolds',
  40. 'root' => '',
  41. 'cryptographicSalt' => '',
  42. 'otpEnabled' => 0
  43. );
  44. function __construct(){
  45. parent::__construct();
  46. }
  47. public function getAll(){
  48. if(!isset($_SESSION['configuration'])){
  49. $configurationManager = new Configuration();
  50. $configs = $configurationManager->populate();
  51. $confTab = array();
  52. foreach($configs as $config){
  53. $this->confTab[$config->getKey()] = $config->getValue();
  54. }
  55. $_SESSION['configuration'] = serialize($this->confTab);
  56. }else{
  57. $this->confTab = unserialize($_SESSION['configuration']);
  58. }
  59. }
  60. public function get($key){
  61. return (isset($this->confTab[$key])?$this->confTab[$key]:'');
  62. }
  63. public function put($key,$value){
  64. $configurationManager = new Configuration();
  65. if (isset($this->confTab[$key])){
  66. $configurationManager->change(array('value'=>$value),array('key'=>$key));
  67. } else {
  68. $configurationManager->add($key,$value);
  69. }
  70. $this->confTab[$key] = $value;
  71. unset($_SESSION['configuration']);
  72. }
  73. protected function createSynchronisationCode() {
  74. return substr(sha1(rand(0,30).time().rand(0,30)),0,10);
  75. }
  76. public function add($key,$value){
  77. $config = new Configuration();
  78. $config->setKey($key);
  79. $config->setValue($value);
  80. $config->save();
  81. $this->confTab[$key] = $value;
  82. unset($_SESSION['configuration']);
  83. }
  84. public function setDefaults() {
  85. foreach($this->options as $option => $defaultValue) {
  86. switch($option) {
  87. case 'language':
  88. $value = isset($_POST['install_changeLngLeed']) ? $_POST['install_changeLngLeed'] : $defaultValue;
  89. break;
  90. case 'theme':
  91. $value = isset($_POST['template']) ? $_POST['template'] : $defaultValue;
  92. break;
  93. case 'synchronisationCode':
  94. $value = $this->createSynchronisationCode();
  95. break;
  96. case 'root':
  97. $root = $_POST['root'];
  98. $value = (substr($root, strlen($root)-1)=='/'?$root:$root.'/');
  99. break;
  100. case 'cryptographicSalt':
  101. $value = $this->generateSalt();
  102. break;
  103. default:
  104. $value = $defaultValue;
  105. break;
  106. }
  107. $this->add($option, $value);
  108. }
  109. }
  110. protected function generateSalt() {
  111. return ''.mt_rand().mt_rand();
  112. }
  113. function getId(){
  114. return $this->id;
  115. }
  116. function getKey(){
  117. return $this->key;
  118. }
  119. function setKey($key){
  120. $this->key = $key;
  121. }
  122. function getValue(){
  123. return $this->value;
  124. }
  125. function setValue($value){
  126. $this->value = $value;
  127. }
  128. }
  129. ?>