1 | <?php
|
---|
2 |
|
---|
3 | include_once('Database.php');
|
---|
4 |
|
---|
5 | class UpdateManager
|
---|
6 | {
|
---|
7 | var $Revision;
|
---|
8 | var $Trace;
|
---|
9 | var $VersionTable;
|
---|
10 | /* @var Database */
|
---|
11 | var $Database;
|
---|
12 | var $InstallMethod;
|
---|
13 |
|
---|
14 | function __construct()
|
---|
15 | {
|
---|
16 | $this->Revision = 0;
|
---|
17 | $this->Trace = array();
|
---|
18 | $this->VersionTable = 'SystemVersion';
|
---|
19 | $this->InstallMethod = 'FullInstall';
|
---|
20 | $this->InsertSampleDataMethod = 'InsertSampleData';
|
---|
21 | }
|
---|
22 |
|
---|
23 | function GetDbVersion()
|
---|
24 | {
|
---|
25 | $DbResult = $this->Database->select($this->VersionTable, '*', 'Id=1');
|
---|
26 | $Version = $DbResult->fetch_assoc();
|
---|
27 | return($Version['Revision']);
|
---|
28 | }
|
---|
29 |
|
---|
30 | function IsInstalled()
|
---|
31 | {
|
---|
32 | $DbResult = $this->Database->query('SHOW TABLES LIKE "'.$this->VersionTable.'"');
|
---|
33 | return($DbResult->num_rows > 0);
|
---|
34 | }
|
---|
35 |
|
---|
36 | function IsUpToDate()
|
---|
37 | {
|
---|
38 | return($this->Revision <= $this->GetDbVersion());
|
---|
39 | }
|
---|
40 |
|
---|
41 | function Update()
|
---|
42 | {
|
---|
43 | $DbRevision = $this->GetDbVersion();
|
---|
44 | $Output = 'Počáteční revize databáze: '.$DbRevision.'<br/>';
|
---|
45 | while($this->Revision > $DbRevision)
|
---|
46 | {
|
---|
47 | $TraceItem = $this->Trace[$DbRevision];
|
---|
48 | $Output .= 'Aktualizace na verzi: '.$TraceItem['Revision'].'<br/>';
|
---|
49 | $RevUpdate = $TraceItem['Function'];
|
---|
50 | $RevUpdate($this);
|
---|
51 | $DbRevision = $TraceItem['Revision'];
|
---|
52 | $this->Database->query('UPDATE `'.$this->VersionTable.'` SET `Revision`= '.$TraceItem['Revision'].' WHERE `Id`=1');
|
---|
53 | }
|
---|
54 | return($Output);
|
---|
55 | }
|
---|
56 |
|
---|
57 | function Install()
|
---|
58 | {
|
---|
59 | $InstallMethod = $this->InstallMethod;
|
---|
60 | $InstallMethod($this);
|
---|
61 | }
|
---|
62 |
|
---|
63 | function Uninstall()
|
---|
64 | {
|
---|
65 |
|
---|
66 | }
|
---|
67 |
|
---|
68 | function InsertSampleData()
|
---|
69 | {
|
---|
70 | $InstallMethod = $this->InsertSampleDataMethod;
|
---|
71 | $InstallMethod($this);
|
---|
72 | }
|
---|
73 |
|
---|
74 | function Execute($Query)
|
---|
75 | {
|
---|
76 | echo($Query.';<br/>');
|
---|
77 | flush();
|
---|
78 | return($this->Database->query($Query));
|
---|
79 | }
|
---|
80 | }
|
---|
81 |
|
---|
82 | class UpdateInterface
|
---|
83 | {
|
---|
84 | var $UpdateManager;
|
---|
85 | var $ConfigDefinition;
|
---|
86 | var $Config;
|
---|
87 | var $DatabaseRevision;
|
---|
88 | var $Revision;
|
---|
89 | var $Updates;
|
---|
90 |
|
---|
91 | function __construct()
|
---|
92 | {
|
---|
93 | }
|
---|
94 |
|
---|
95 | function LoginPanel()
|
---|
96 | {
|
---|
97 | $Output = '<h3>Přihlášení k instalaci</h3>'.
|
---|
98 | '<form action="" method="post">'.
|
---|
99 | '<table>'.
|
---|
100 | '<tr><td>Systémové heslo:</td><td> <input type="password" name="SystemPassword" value=""/></td></tr>'.
|
---|
101 | '</table>'.
|
---|
102 | '<input type="submit" name="login" value="Přihlásit"/>'.
|
---|
103 | '</form>';
|
---|
104 | return($Output);
|
---|
105 | }
|
---|
106 |
|
---|
107 | function ControlPanel()
|
---|
108 | {
|
---|
109 | $YesNo = array(false => 'Ne', true => 'Ano');
|
---|
110 | $Output = '<h3>Správa instance</h3>'.
|
---|
111 | 'Je instalováno: '.$YesNo[$this->UpdateManager->IsInstalled()].'<br/>';
|
---|
112 | if($this->UpdateManager->IsInstalled())
|
---|
113 | $Output .= 'Je aktuální: '.$YesNo[$this->UpdateManager->IsUpToDate()].'<br/>'.
|
---|
114 | 'Verze databáze: '.$this->UpdateManager->GetDbVersion().'<br/>';
|
---|
115 | $Output .= 'Verze databáze kódu: '.$this->UpdateManager->Revision.'<br/>'.
|
---|
116 | '<form action="" method="post">';
|
---|
117 | if($this->UpdateManager->IsInstalled())
|
---|
118 | {
|
---|
119 | if(!$this->UpdateManager->IsUpToDate())
|
---|
120 | $Output .= '<input type="submit" name="update" value="Aktualizovat"/> ';
|
---|
121 | $Output .= '<input type="submit" name="insert_sample_data" value="Vložit vzorová data"/> ';
|
---|
122 | $Output .= '<input type="submit" name="uninstall" value="Odinstalovat"/> ';
|
---|
123 | } else $Output .= '<input type="submit" name="install" value="Instalovat"/> ';
|
---|
124 | $Output .= '<input type="submit" name="configure" value="Nastavit"/> ';
|
---|
125 | $Output .= '<input type="submit" name="logout" value="Odhlásit"/> ';
|
---|
126 | $Output .= '</form>';
|
---|
127 | return($Output);
|
---|
128 | }
|
---|
129 |
|
---|
130 | function Show()
|
---|
131 | {
|
---|
132 | $Output = '<?xml version="1.0" encoding="utf-8"?>
|
---|
133 | <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
|
---|
134 | <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="cz">'.
|
---|
135 | '<head>'.
|
---|
136 | '<meta http-equiv="content-type" content="application/xhtml+xml; charset=utf-8" />'.
|
---|
137 | '<title>Správa instance</title>'.
|
---|
138 | '</head><body>';
|
---|
139 | if(isset($this->Config))
|
---|
140 | {
|
---|
141 | if(!array_key_exists('SystemPassword', $_SESSION)) $_SESSION['SystemPassword'] = '';
|
---|
142 | if(array_key_exists('login', $_POST)) $_SESSION['SystemPassword'] = $_POST['SystemPassword'];
|
---|
143 | if(sha1($_SESSION['SystemPassword']) != $this->Config['SystemPassword'])
|
---|
144 | {
|
---|
145 | $Output .= $this->LoginPanel();
|
---|
146 | } else
|
---|
147 | {
|
---|
148 | date_default_timezone_set('Europe/Prague');
|
---|
149 | // SQL injection hack protection
|
---|
150 | foreach($_POST as $Index => $Item) $_POST[$Index] = addslashes($Item);
|
---|
151 | foreach($_GET as $Index => $Item) $_GET[$Index] = addslashes($Item);
|
---|
152 |
|
---|
153 | if(isset($_SERVER['REMOTE_ADDR'])) session_start();
|
---|
154 |
|
---|
155 | $Database = new Database();
|
---|
156 | $Database->Connect($this->Config['Database']['Host'], $this->Config['Database']['User'],
|
---|
157 | $this->Config['Database']['Password'], $this->Config['Database']['Database']);
|
---|
158 | $Database->Prefix = $this->Config['Database']['Prefix'];
|
---|
159 | $Database->charset($this->Config['Database']['Charset']);
|
---|
160 | $Database->ShowSQLError = $this->Config['Web']['ShowSQLError'];
|
---|
161 | $Database->ShowSQLQuery = $this->Config['Web']['ShowSQLQuery'];
|
---|
162 |
|
---|
163 | $this->UpdateManager = new UpdateManager();
|
---|
164 | $this->UpdateManager->Database = $Database;
|
---|
165 | $this->UpdateManager->Revision = $this->DatabaseRevision;
|
---|
166 | $this->UpdateManager->Trace = $this->Updates;
|
---|
167 | $this->UpdateManager->InstallMethod = 'FullInstall';
|
---|
168 |
|
---|
169 | if(array_key_exists('logout', $_POST))
|
---|
170 | {
|
---|
171 | $_SESSION['SystemPassword'] = '';
|
---|
172 | $Output .= 'Odhlášen';
|
---|
173 | $Output .= $this->LoginPanel();
|
---|
174 | } else
|
---|
175 | if(array_key_exists('update', $_POST))
|
---|
176 | {
|
---|
177 | $Output .= '<h3>Aktualizace</h3>';
|
---|
178 | $Output .= $this->UpdateManager->Update();
|
---|
179 | $Output .= $this->ControlPanel();
|
---|
180 | } else
|
---|
181 | if(array_key_exists('install', $_POST))
|
---|
182 | {
|
---|
183 | $Output .= '<h3>Instalace</h3>';
|
---|
184 | $this->UpdateManager->Install();
|
---|
185 | $Output .= $this->UpdateManager->Update();
|
---|
186 | $Output .= $this->ControlPanel();
|
---|
187 | } else
|
---|
188 | if(array_key_exists('uninstall', $_POST))
|
---|
189 | {
|
---|
190 | $Output .= '<h3>Odinstalace</h3>';
|
---|
191 | $this->UpdateManager->Uninstall();
|
---|
192 | $Output .= $this->ControlPanel();
|
---|
193 | } else
|
---|
194 | if(array_key_exists('insert_sample_data', $_POST))
|
---|
195 | {
|
---|
196 | $Output .= '<h3>Vložení vzorových dat</h3>';
|
---|
197 | $this->UpdateManager->InsertSampleData();
|
---|
198 | $Output .= $this->ControlPanel();
|
---|
199 | } else
|
---|
200 | if(array_key_exists('configure_save', $_POST))
|
---|
201 | {
|
---|
202 | $Output .= $this->ConfigSave($this->Config);
|
---|
203 | $Output .= $this->ControlPanel();
|
---|
204 | } else
|
---|
205 | if(array_key_exists('configure', $_POST))
|
---|
206 | {
|
---|
207 | $Output .= $this->PrepareConfig($this->Config);
|
---|
208 | } else
|
---|
209 | {
|
---|
210 | $Output .= $this->ControlPanel();
|
---|
211 | }
|
---|
212 | }
|
---|
213 | } else
|
---|
214 | {
|
---|
215 | if(array_key_exists('configure_save', $_POST))
|
---|
216 | {
|
---|
217 | $Output .= $this->ConfigSave(array());
|
---|
218 | $Output .= 'Pokračujte k přihlášení <a href="">zde</a>';
|
---|
219 | } else {
|
---|
220 | $Output .= $this->PrepareConfig(array());
|
---|
221 | }
|
---|
222 | }
|
---|
223 | $Output .= '</body></html>';
|
---|
224 | echo($Output);
|
---|
225 | }
|
---|
226 |
|
---|
227 | function PrepareConfig($Config)
|
---|
228 | {
|
---|
229 | $Output = '';
|
---|
230 | if(!file_exists('../config.php') and !is_writable('..'))
|
---|
231 | $Output .= 'Varování: Konfigurační soubor nebude možné zapsat, protože složka není povolená pro zápis!';
|
---|
232 | if(file_exists('../config.php') and !is_writable('../config.php'))
|
---|
233 | $Output .= 'Varování: Konfigurační soubor nebude možné zapsat, protože soubor config.php není povolen pro zápis!';
|
---|
234 | $Output .= '<h3>Nastavení systému</h3>'.
|
---|
235 | '<form action="" method="post">'.
|
---|
236 | '<table>';
|
---|
237 | foreach($this->ConfigDefinition as $Def)
|
---|
238 | {
|
---|
239 | $PathParts = explode('/', $Def['Name']);
|
---|
240 | $TempConfig = &$Config;
|
---|
241 | foreach($PathParts as $Part)
|
---|
242 | if(array_key_exists($Part, $TempConfig))
|
---|
243 | {
|
---|
244 | $TempConfig = &$TempConfig[$Part];
|
---|
245 | }
|
---|
246 | if(!is_array($TempConfig)) $Value = $TempConfig;
|
---|
247 | else $Value = $Def['Default'];
|
---|
248 | $Output .= '<tr><td>'.$Def['Title'].'</td><td>';
|
---|
249 | if($Def['Type'] == 'String') $Output .= '<input type="text" name="'.$Def['Name'].'" value="'.$Value.'"/>';
|
---|
250 | if($Def['Type'] == 'Password') $Output .= '<input type="password" name="'.$Def['Name'].'"/>';
|
---|
251 | if($Def['Type'] == 'Integer') $Output .= '<input type="text" name="'.$Def['Name'].'" value="'.$Value.'"/>';
|
---|
252 | if($Def['Type'] == 'Boolean') $Output .= '<input type="text" name="'.$Def['Name'].'" value="'.$Value.'"/>';
|
---|
253 | }
|
---|
254 | $Output .= '</td></tr>'.
|
---|
255 | '<tr><td colspan="2"><input type="submit" name="configure_save" value="Nastavit"/></td></tr>'.
|
---|
256 | '</table>'.
|
---|
257 | '</form>';
|
---|
258 | return($Output);
|
---|
259 | }
|
---|
260 |
|
---|
261 | function ConfigSave($DefaultConfig)
|
---|
262 | {
|
---|
263 | $Config = $DefaultConfig;
|
---|
264 | foreach($this->ConfigDefinition as $Def)
|
---|
265 | {
|
---|
266 | $Value = null;
|
---|
267 | if($Def['Type'] == 'String') if(array_key_exists($Def['Name'], $_POST))
|
---|
268 | $Value = $_POST[$Def['Name']];
|
---|
269 | if($Def['Type'] == 'Password') if(array_key_exists($Def['Name'], $_POST) and ($_POST[$Def['Name']] != ''))
|
---|
270 | $Value = $_POST[$Def['Name']];
|
---|
271 | if($Def['Type'] == 'Integer') if(array_key_exists($Def['Name'], $_POST))
|
---|
272 | $Value = $_POST[$Def['Name']];
|
---|
273 | if($Def['Type'] == 'Boolean') if(array_key_exists($Def['Name'], $_POST))
|
---|
274 | $Value = $_POST[$Def['Name']];
|
---|
275 | if(!is_null($Value))
|
---|
276 | {
|
---|
277 | $PathParts = explode('/', $Def['Name']);
|
---|
278 | $TempConfig = &$Config;
|
---|
279 | foreach($PathParts as $Part)
|
---|
280 | {
|
---|
281 | $TempConfig = &$TempConfig[$Part];
|
---|
282 | }
|
---|
283 | if(!is_array($TempConfig)) $TempConfig = $Value;
|
---|
284 | else $Value = $Def['Default'];
|
---|
285 | }
|
---|
286 | }
|
---|
287 | $ConfigText = $this->CreateConfig($Config);
|
---|
288 | file_put_contents('../config.php', $ConfigText);
|
---|
289 | $Output .= 'Konfigurace nastavena<br/>';
|
---|
290 | return($Output);
|
---|
291 | }
|
---|
292 |
|
---|
293 | function CreateConfig($Config)
|
---|
294 | {
|
---|
295 |
|
---|
296 | $Output = "<?php\n\n".
|
---|
297 | "\$IsDeveloper = in_array(\$_SERVER['REMOTE_ADDR'], array('127.0.0.1'));\n\n";
|
---|
298 |
|
---|
299 | foreach($this->ConfigDefinition as $Def)
|
---|
300 | {
|
---|
301 | $PathParts = explode('/', $Def['Name']);
|
---|
302 | $Output .= "\$Config";
|
---|
303 | foreach($PathParts as $Part)
|
---|
304 | $Output .= "['".$Part."']";
|
---|
305 | $TempConfig = &$Config;
|
---|
306 | foreach($PathParts as $Part)
|
---|
307 | if(array_key_exists($Part, $TempConfig))
|
---|
308 | {
|
---|
309 | $TempConfig = &$TempConfig[$Part];
|
---|
310 | }
|
---|
311 | if(!is_array($TempConfig)) $Value = $TempConfig;
|
---|
312 | else $Value = $Def['Default'];
|
---|
313 | $Output .= " = '".$Value."';\n";
|
---|
314 | }
|
---|
315 | $Output .= "\n\n?>";
|
---|
316 | return($Output);
|
---|
317 | }
|
---|
318 | }
|
---|
319 |
|
---|
320 | ?>
|
---|