-
Notifications
You must be signed in to change notification settings - Fork 42
/
Configuration.class.php
158 lines (128 loc) · 4.19 KB
/
Configuration.class.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
<?php
/*
@nom: Configuration
@auteur: Idleman (http://blog.idleman.fr)
@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
*/
class Configuration extends MysqlEntity{
protected $id,$key,$value,$confTab;
protected $TABLE_NAME = 'configuration';
protected $object_fields =
array(
'id'=>'key',
'key'=>'string',
'value'=>'longstring'
);
protected $object_fields_uniques =
array(
'key'
);
protected $options = array(
'articleDisplayAnonymous' => '0',
'articleDisplayAuthor' => '1',
'articleDisplayDate' => '1',
'articleDisplayFolderSort' => '1',
'articleDisplayHomeSort' => '1',
'articleDisplayLink' => '1',
'articleDisplayMode' => 'summary',
'articlePerPages' => '5',
'displayOnlyUnreadFeedFolder' => 'false',
'feedMaxEvents' => '50',
'language' => 'en',
'optionFeedIsVerbose' => 1,
'paginationScale' => 5,
'syncGradCount' => '10',
'synchronisationCode' => '',
'synchronisationEnableCache' => '0',
'synchronisationForceFeed' => '0',
'synchronisationType' => 'auto',
'theme' => 'marigolds',
'root' => '',
'cryptographicSalt' => ''
);
function __construct(){
parent::__construct();
}
public function getAll(){
if(!isset($_SESSION['configuration'])){
$configurationManager = new Configuration();
$configs = $configurationManager->populate();
$confTab = array();
foreach($configs as $config){
$this->confTab[$config->getKey()] = $config->getValue();
}
$_SESSION['configuration'] = serialize($this->confTab);
}else{
$this->confTab = unserialize($_SESSION['configuration']);
}
}
public function get($key){
return (isset($this->confTab[$key])?$this->confTab[$key]:'');
}
public function put($key,$value){
$configurationManager = new Configuration();
if (isset($this->confTab[$key])){
$configurationManager->change(array('value'=>$value),array('key'=>$key));
} else {
$configurationManager->add($key,$value);
}
$this->confTab[$key] = $value;
unset($_SESSION['configuration']);
}
protected function createSynchronisationCode() {
return substr(sha1(rand(0,30).time().rand(0,30)),0,10);
}
public function add($key,$value){
$config = new Configuration();
$config->setKey($key);
$config->setValue($value);
$config->save();
$this->confTab[$key] = $value;
unset($_SESSION['configuration']);
}
public function setDefaults() {
foreach($this->options as $option => $defaultValue) {
switch($option) {
case 'language':
$value = isset($_POST['install_changeLngLeed']) ? $_POST['install_changeLngLeed'] : $defaultValue;
break;
case 'theme':
$value = isset($_POST['template']) ? $_POST['template'] : $defaultValue;
break;
case 'synchronisationCode':
$value = $this->createSynchronisationCode();
break;
case 'root':
$root = $_POST['root'];
$value = (substr($root, strlen($root)-1)=='/'?$root:$root.'/');
break;
case 'cryptographicSalt':
$value = $this->generateSalt();
break;
default:
$value = $defaultValue;
break;
}
$this->add($option, $value);
}
}
protected function generateSalt() {
return ''.mt_rand().mt_rand();
}
function getId(){
return $this->id;
}
function getKey(){
return $this->key;
}
function setKey($key){
$this->key = $key;
}
function getValue(){
return $this->value;
}
function setValue($value){
$this->value = $value;
}
}
?>