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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
|
<?php
/**
* Get or set simple key-value-pairs, backed by the database
* to make them persistent.
*/
class Property
{
private static $cache = false;
/**
* Retrieve value from property store.
*
* @param string $key key to retrieve the value of
* @param mixed $default value to return if $key does not exist in the property store
* @return mixed the value attached to $key, or $default if $key does not exist
*/
private static function get($key, $default = false)
{
if (self::$cache === false) {
$NOW = time();
$res = Database::simpleQuery("SELECT name, dateline, value FROM property");
while ($row = $res->fetch(PDO::FETCH_ASSOC)) {
if ($row['dateline'] != 0 && $row['dateline'] < $NOW)
continue;
self::$cache[$row['name']] = $row['value'];
}
}
if (!isset(self::$cache[$key]))
return $default;
return self::$cache[$key];
}
/**
* Set value in property store.
*
* @param string $key key of value to set
* @param type $value the value to store for $key
* @param int $minage how long to keep this entry around at least, in minutes. 0 for infinite
*/
private static function set($key, $value, $minage = 0)
{
if (self::$cache === false || self::get($key) != $value) { // Simple compare, so it works for numbers accidentally casted to string somewhere
Database::exec("INSERT INTO property (name, value, dateline) VALUES (:key, :value, :dateline)"
. " ON DUPLICATE KEY UPDATE value = VALUES(value), dateline = VALUES(dateline)", array(
'key' => $key,
'value' => $value,
'dateline' => ($minage === 0 ? 0 : time() + ($minage * 60))
));
}
if (self::$cache !== false) {
self::$cache[$key] = $value;
}
}
public static function getServerIp()
{
return self::get('server-ip', 'none');
}
public static function setServerIp($value, $automatic = false)
{
if ($value === self::getServerIp())
return false;
EventLog::info('Server IP changed from ' . self::getServerIp() . ' to ' . $value . ($automatic ? ' (auto detected)' : ''));
self::set('server-ip', $value);
Event::serverIpChanged();
return true;
}
public static function getBootMenu()
{
return json_decode(self::get('ipxe-menu'), true);
}
public static function setBootMenu($value)
{
self::set('ipxe-menu', json_encode($value));
}
public static function getVersionCheckTaskId()
{
return self::get('versioncheck-task');
}
public static function setVersionCheckTaskId($value)
{
self::set('versioncheck-task', $value);
}
public static function getVersionCheckInformation()
{
$data = json_decode(self::get('versioncheck-data'), true);
if (isset($data['time']) && $data['time'] + 120 > time())
return $data;
$task = Taskmanager::submit('DownloadText', array(
'url' => CONFIG_REMOTE_ML . '/list.php'
));
if (!isset($task['id']))
return 'Could not start list download (' . Message::asString() . ')';
if ($task['statusCode'] !== TASK_FINISHED) {
$task = Taskmanager::waitComplete($task['id'], 5000);
}
if ($task['statusCode'] !== TASK_FINISHED || !isset($task['data']['content'])) {
return $task['data']['error'];
}
$data = json_decode($task['data']['content'], true);
$data['time'] = time();
self::setVersionCheckInformation($data);
return $data;
}
public static function setVersionCheckInformation($value)
{
self::set('versioncheck-data', json_encode($value), 1);
}
public static function getVmStoreConfig()
{
return json_decode(self::get('vmstore-config'), true);
}
public static function getVmStoreUrl()
{
$store = self::getVmStoreConfig();
if (!isset($store['storetype']))
return false;
if ($store['storetype'] === 'nfs')
return $store['nfsaddr'];
if ($store['storetype'] === 'cifs')
return $store['cifsaddr'];
if ($store['storetype'] === 'internal')
return '<local>';
return '<unknown>';
}
public static function setVmStoreConfig($value)
{
self::set('vmstore-config', json_encode($value));
}
public static function getDownloadTask($name)
{
return self::get('dl-' . $name);
}
public static function setDownloadTask($name, $taskId)
{
self::set('dl-' . $name, $taskId, 5);
}
public static function getCurrentSchemaVersion()
{
return self::get('webif-version');
}
public static function setLastWarningId($id)
{
self::set('last-warn-event-id', $id);
}
public static function getLastWarningId()
{
return self::get('last-warn-event-id', 0);
}
public static function setNeedsSetup($value)
{
self::set('needs-setup', $value);
}
public static function getNeedsSetup()
{
return self::get('needs-setup');
}
public static function setPasswordFieldType($value)
{
return self::set('password-type', $value);
}
public static function getPasswordFieldType()
{
return self::get('password-type', 'password');
}
public static function getIpxeDefault()
{
return self::get('default-ipxe');
}
}
|