1 | <?php
|
---|
2 |
|
---|
3 | include_once(dirname(__FILE__).'/Update.php');
|
---|
4 | include_once(dirname(__FILE__).'/DefaultConfig.php');
|
---|
5 | include_once(dirname(__FILE__).'/FullInstall.php');
|
---|
6 | include_once(dirname(__FILE__).'/Updates.php');
|
---|
7 |
|
---|
8 | class PageSetup extends Page
|
---|
9 | {
|
---|
10 | var $UpdateManager;
|
---|
11 | var $ConfigDefinition;
|
---|
12 | var $Config;
|
---|
13 | var $DatabaseRevision;
|
---|
14 | var $Revision;
|
---|
15 | var $Updates;
|
---|
16 | var $ConfigDir;
|
---|
17 |
|
---|
18 | function __construct($System)
|
---|
19 | {
|
---|
20 | parent::__construct($System);
|
---|
21 | $this->FullTitle = 'Instalace aplikace';
|
---|
22 | $this->ShortTitle = 'Instalátor';
|
---|
23 | //$this->ParentClass = 'PagePortal';
|
---|
24 | $this->ConfigDir = dirname(__FILE__).'/../..';
|
---|
25 | }
|
---|
26 |
|
---|
27 | function LoginPanel()
|
---|
28 | {
|
---|
29 | $Output = '<h3>Přihlášení k instalaci</h3>'.
|
---|
30 | '<form action="?" method="post">'.
|
---|
31 | '<table>'.
|
---|
32 | '<tr><td>Systémové heslo:</td><td> <input type="password" name="SystemPassword" value=""/></td></tr>'.
|
---|
33 | '</table>'.
|
---|
34 | '<input type="submit" name="login" value="Přihlásit"/>'.
|
---|
35 | '</form>';
|
---|
36 | return($Output);
|
---|
37 | }
|
---|
38 |
|
---|
39 | function ControlPanel()
|
---|
40 | {
|
---|
41 | global $YesNo;
|
---|
42 | $Output = '';
|
---|
43 |
|
---|
44 | $Output .= 'Je připojení k databázi: '.$YesNo[$this->UpdateManager->Database->Connected()].'<br/>';
|
---|
45 | if($this->UpdateManager->Database->Connected())
|
---|
46 | {
|
---|
47 | $Output .= 'Je instalováno: '.$YesNo[$this->UpdateManager->IsInstalled()].'<br/>';
|
---|
48 | if($this->UpdateManager->IsInstalled())
|
---|
49 | $Output .= 'Je aktuální: '.$YesNo[$this->UpdateManager->IsUpToDate()].'<br/>'.
|
---|
50 | 'Verze databáze: '.$this->UpdateManager->GetDbVersion().'<br/>';
|
---|
51 | $Output .= 'Verze databáze kódu: '.$this->UpdateManager->Revision.'<br/>';
|
---|
52 | if($this->UpdateManager->IsInstalled())
|
---|
53 | {
|
---|
54 | if(!$this->UpdateManager->IsUpToDate())
|
---|
55 | $Output .= '<a href="?action=upgrade">Povýšit</a> ';
|
---|
56 | $Output .= '<a href="?action=insert_sample_data">Vložit vzorová data</a> ';
|
---|
57 | $Output .= '<a href="?action=uninstall">Odinstalovat</a> ';
|
---|
58 | $Output .= '<a href="?action=modules">Správa modulů</a> ';
|
---|
59 | } else $Output .= '<a href="?action=install">Instalovat</a> ';
|
---|
60 | }
|
---|
61 | $Output .= '<a href="?action=configure">Nastavit</a> ';
|
---|
62 | $Output .= '<a href="?action=logout">Odhlásit</a> ';
|
---|
63 | $Output .= '';
|
---|
64 | return($Output);
|
---|
65 | }
|
---|
66 |
|
---|
67 | function Show()
|
---|
68 | {
|
---|
69 | global $ConfigDefinition, $DatabaseRevision, $Config, $Updates;
|
---|
70 |
|
---|
71 | $this->UpdateManager = $this->System->Setup->UpdateManager;
|
---|
72 | $DefaultConfig = new DefaultConfig();
|
---|
73 | $this->ConfigDefinition = $DefaultConfig->Get();
|
---|
74 | $this->DatabaseRevision = $DatabaseRevision;
|
---|
75 | $this->Config = &$Config;
|
---|
76 |
|
---|
77 | $Output = '';
|
---|
78 | if(isset($this->Config))
|
---|
79 | {
|
---|
80 | if(!array_key_exists('SystemPassword', $_SESSION)) $_SESSION['SystemPassword'] = '';
|
---|
81 | if(array_key_exists('login', $_POST)) $_SESSION['SystemPassword'] = $_POST['SystemPassword'];
|
---|
82 | if(sha1($_SESSION['SystemPassword']) != $this->Config['SystemPassword'])
|
---|
83 | {
|
---|
84 | $Output .= $this->LoginPanel();
|
---|
85 | } else
|
---|
86 | {
|
---|
87 | if(array_key_exists('action', $_GET)) $Action = $_GET['action'];
|
---|
88 | else $Action = '';
|
---|
89 | if($Action == 'logout')
|
---|
90 | {
|
---|
91 | $_SESSION['SystemPassword'] = '';
|
---|
92 | $Output .= 'Odhlášen';
|
---|
93 | $Output .= $this->LoginPanel();
|
---|
94 | } else
|
---|
95 | if($Action == 'upgrade')
|
---|
96 | {
|
---|
97 | $Output .= '<h3>Povýšení</h3>';
|
---|
98 | $Output .= $this->System->Setup->Upgrade();
|
---|
99 | $Output .= $this->ControlPanel();
|
---|
100 | } else
|
---|
101 | if($Action == 'install')
|
---|
102 | {
|
---|
103 | $Output .= '<h3>Instalace</h3>';
|
---|
104 | $this->System->Setup->Install();
|
---|
105 | $Output .= $this->System->Setup->Upgrade();
|
---|
106 | $Output .= $this->ControlPanel();
|
---|
107 | } else
|
---|
108 | if($Action == 'uninstall')
|
---|
109 | {
|
---|
110 | $Output .= '<h3>Odinstalace</h3>';
|
---|
111 | $this->System->Setup->Uninstall();
|
---|
112 | $Output .= $this->ControlPanel();
|
---|
113 | } else
|
---|
114 | if($Action == 'insert_sample_data')
|
---|
115 | {
|
---|
116 | $Output .= '<h3>Vložení vzorových dat</h3>';
|
---|
117 | $this->System->Setup->InsertSampleData();
|
---|
118 | $Output .= $this->ControlPanel();
|
---|
119 | } else
|
---|
120 | if($Action == 'modules')
|
---|
121 | {
|
---|
122 | $Output .= $this->ShowModules();
|
---|
123 | } else
|
---|
124 | if($Action == 'configure_save')
|
---|
125 | {
|
---|
126 | $Output .= $this->ConfigSave($this->Config);
|
---|
127 | $Output .= $this->ControlPanel();
|
---|
128 | } else
|
---|
129 | if($Action == 'configure')
|
---|
130 | {
|
---|
131 | $Output .= $this->PrepareConfig($this->Config);
|
---|
132 | } else
|
---|
133 | {
|
---|
134 | $Output .= $this->ControlPanel();
|
---|
135 | }
|
---|
136 | }
|
---|
137 | } else
|
---|
138 | {
|
---|
139 | if(array_key_exists('configure_save', $_POST))
|
---|
140 | {
|
---|
141 | $Output .= $this->ConfigSave(array());
|
---|
142 | $Output .= 'Pokračujte k přihlášení <a href="">zde</a>';
|
---|
143 | } else {
|
---|
144 | $Output .= $this->PrepareConfig(array());
|
---|
145 | }
|
---|
146 | }
|
---|
147 | return($Output);
|
---|
148 | }
|
---|
149 |
|
---|
150 | function ShowModules()
|
---|
151 | {
|
---|
152 | $Output = '';
|
---|
153 | if(array_key_exists('op', $_GET)) $Operation = $_GET['op'];
|
---|
154 | else $Operation = '';
|
---|
155 | if($Operation == 'install')
|
---|
156 | {
|
---|
157 | $this->System->ModuleManager->Modules[$_GET['name']]->Install();
|
---|
158 | $this->System->ModuleManager->SaveState();
|
---|
159 | $Output .= 'Modul '.$_GET['name'].' instalován<br/>';
|
---|
160 | } else
|
---|
161 | if($Operation == 'uninstall')
|
---|
162 | {
|
---|
163 | $this->System->ModuleManager->Modules[$_GET['name']]->Uninstall();
|
---|
164 | $this->System->ModuleManager->SaveState();
|
---|
165 | $Output .= 'Modul '.$_GET['name'].' odinstalován<br/>';
|
---|
166 | } else
|
---|
167 | if($Operation == 'enable')
|
---|
168 | {
|
---|
169 | $this->System->ModuleManager->Modules[$_GET['name']]->Enable();
|
---|
170 | $this->System->ModuleManager->SaveState();
|
---|
171 | $Output .= 'Modul '.$_GET['name'].' povolen<br/>';
|
---|
172 | } else
|
---|
173 | if($Operation == 'disable')
|
---|
174 | {
|
---|
175 | $this->System->ModuleManager->Modules[$_GET['name']]->Disable();
|
---|
176 | $this->System->ModuleManager->SaveState();
|
---|
177 | $Output .= 'Modul '.$_GET['name'].' zakázán<br/>';
|
---|
178 | } else
|
---|
179 | if($Operation == 'upgrade')
|
---|
180 | {
|
---|
181 | $this->System->ModuleManager->Modules[$_GET['name']]->Upgrade();
|
---|
182 | $this->System->ModuleManager->SaveState();
|
---|
183 | $Output .= 'Modul '.$_GET['name'].' povýšen<br/>';
|
---|
184 | }
|
---|
185 | $Output .= '<h3>Správa modulů</h3>';
|
---|
186 | $Output .= $this->ShowList();
|
---|
187 | return($Output);
|
---|
188 | }
|
---|
189 |
|
---|
190 | function ShowList()
|
---|
191 | {
|
---|
192 | global $YesNo;
|
---|
193 |
|
---|
194 | $Output = '';
|
---|
195 | $PageList = GetPageList(count($this->System->ModuleManager->Modules));
|
---|
196 |
|
---|
197 | $Output .= $PageList['Output'];
|
---|
198 | $Output .= '<table class="WideTable" style="font-size: small;">';
|
---|
199 |
|
---|
200 | $TableColumns = array(
|
---|
201 | array('Name' => 'Name', 'Title' => 'Jméno'),
|
---|
202 | array('Name' => 'Creator', 'Title' => 'Tvůrce'),
|
---|
203 | array('Name' => 'Version', 'Title' => 'Verze'),
|
---|
204 | array('Name' => 'License', 'Title' => 'Licence'),
|
---|
205 | array('Name' => 'Installed', 'Title' => 'Instalováno'),
|
---|
206 | array('Name' => 'Enabled', 'Title' => 'Povoleno'),
|
---|
207 | array('Name' => 'Description', 'Title' => 'Popis'),
|
---|
208 | array('Name' => 'Dependencies', 'Title' => 'Závislosti'),
|
---|
209 | array('Name' => '', 'Title' => 'Akce'),
|
---|
210 | );
|
---|
211 | $Order = GetOrderTableHeader($TableColumns, 'Name', 0);
|
---|
212 | $Output .= $Order['Output'];
|
---|
213 |
|
---|
214 | foreach($this->System->ModuleManager->Modules as $Module)
|
---|
215 | {
|
---|
216 | if(($Module->Dependencies) > 0) $Dependencies = implode(',', $Module->Dependencies);
|
---|
217 | else $Dependencies = ' ';
|
---|
218 | $Actions = '';
|
---|
219 | if($Module->Installed == true)
|
---|
220 | {
|
---|
221 | $Actions .= ' <a href="?action=modules&op=uninstall&name='.$Module->Name.'">Odinstalovat</a>';
|
---|
222 | if($Module->Enabled == true) $Actions .= ' <a href="?action=modules&op=disable&name='.$Module->Name.'">Zakázat</a>';
|
---|
223 | else $Actions .= ' <a href="?action=modules&op=enable&name='.$Module->Name.'">Povolit</a>';
|
---|
224 | if($Module->InstalledVersion != $Module->Version) $Actions .= ' <a href="?action=modules&op=upgrade&name='.$Module->Name.'">Povýšit</a>';
|
---|
225 | } else $Actions .= ' <a href="?action=modules&op=install&name='.$Module->Name.'">Instalovat</a>';
|
---|
226 |
|
---|
227 | $Output .= '<tr><td>'.$Module->Name.'</td>'.
|
---|
228 | '<td>'.$Module->Creator.'</td>'.
|
---|
229 | '<td>'.$Module->Version.'</td>'.
|
---|
230 | '<td>'.$Module->License.'</td>'.
|
---|
231 | '<td>'.$YesNo[$Module->Installed].'</td>'.
|
---|
232 | '<td>'.$YesNo[$Module->Enabled].'</td>'.
|
---|
233 | '<td>'.$Module->Description.'</td>'.
|
---|
234 | '<td>'.$Dependencies.'</td>'.
|
---|
235 | '<td>'.$Actions.'</td></tr>';
|
---|
236 | }
|
---|
237 | $Output .= '</table>';
|
---|
238 | $Output .= $PageList['Output'];
|
---|
239 | //$Output .= '<p><a href="?A=SaveToDb">Uložit do databáze</a></p>';
|
---|
240 | return($Output);
|
---|
241 | }
|
---|
242 |
|
---|
243 | function PrepareConfig($Config)
|
---|
244 | {
|
---|
245 | $Output = '';
|
---|
246 | if(!file_exists($this->ConfigDir.'/config.php') and !is_writable($this->ConfigDir))
|
---|
247 | $Output .= 'Varování: Konfigurační soubor nebude možné zapsat, protože složka není povolená pro zápis!';
|
---|
248 | if(file_exists($this->ConfigDir.'/config.php') and !is_writable($this->ConfigDir.'/config.php'))
|
---|
249 | $Output .= 'Varování: Konfigurační soubor nebude možné zapsat, protože soubor config.php není povolen pro zápis!';
|
---|
250 | $Output .= '<h3>Nastavení systému</h3>'.
|
---|
251 | '<form action="?action=configure_save" method="post">'.
|
---|
252 | '<table>';
|
---|
253 | foreach($this->ConfigDefinition as $Def)
|
---|
254 | {
|
---|
255 | $PathParts = explode('/', $Def['Name']);
|
---|
256 | $TempConfig = &$Config;
|
---|
257 | foreach($PathParts as $Part)
|
---|
258 | if(array_key_exists($Part, $TempConfig))
|
---|
259 | {
|
---|
260 | $TempConfig = &$TempConfig[$Part];
|
---|
261 | }
|
---|
262 | if(!is_array($TempConfig)) $Value = $TempConfig;
|
---|
263 | else $Value = $Def['Default'];
|
---|
264 | $Output .= '<tr><td>'.$Def['Title'].'</td><td>';
|
---|
265 | if($Def['Type'] == 'String') $Output .= '<input type="text" name="'.$Def['Name'].'" value="'.$Value.'"/>';
|
---|
266 | if($Def['Type'] == 'Password') $Output .= '<input type="password" name="'.$Def['Name'].'"/>';
|
---|
267 | if($Def['Type'] == 'PasswordEncoded') $Output .= '<input type="password" name="'.$Def['Name'].'"/>';
|
---|
268 | if($Def['Type'] == 'Integer') $Output .= '<input type="text" name="'.$Def['Name'].'" value="'.$Value.'"/>';
|
---|
269 | if($Def['Type'] == 'Float') $Output .= '<input type="text" name="'.$Def['Name'].'" value="'.$Value.'"/>';
|
---|
270 | if($Def['Type'] == 'Boolean') $Output .= '<input type="text" name="'.$Def['Name'].'" value="'.$Value.'"/>';
|
---|
271 | }
|
---|
272 | $Output .= '</td></tr>'.
|
---|
273 | '<tr><td colspan="2"><input type="submit" name="configure_save" value="Nastavit"/></td></tr>'.
|
---|
274 | '</table>'.
|
---|
275 | '</form>';
|
---|
276 | return($Output);
|
---|
277 | }
|
---|
278 |
|
---|
279 | function ConfigSave($DefaultConfig)
|
---|
280 | {
|
---|
281 | $Config = $DefaultConfig;
|
---|
282 | foreach($this->ConfigDefinition as $Def)
|
---|
283 | {
|
---|
284 | $Value = null;
|
---|
285 | if($Def['Type'] == 'String') if(array_key_exists($Def['Name'], $_POST))
|
---|
286 | $Value = $_POST[$Def['Name']];
|
---|
287 | if($Def['Type'] == 'Password') if(array_key_exists($Def['Name'], $_POST) and ($_POST[$Def['Name']] != ''))
|
---|
288 | $Value = $_POST[$Def['Name']];
|
---|
289 | if($Def['Type'] == 'PasswordEncoded') if(array_key_exists($Def['Name'], $_POST) and ($_POST[$Def['Name']] != ''))
|
---|
290 | $Value = sha1($_POST[$Def['Name']]);
|
---|
291 | if($Def['Type'] == 'Integer') if(array_key_exists($Def['Name'], $_POST))
|
---|
292 | $Value = $_POST[$Def['Name']];
|
---|
293 | if($Def['Type'] == 'Float') if(array_key_exists($Def['Name'], $_POST))
|
---|
294 | $Value = $_POST[$Def['Name']];
|
---|
295 | if($Def['Type'] == 'Boolean') if(array_key_exists($Def['Name'], $_POST))
|
---|
296 | $Value = $_POST[$Def['Name']];
|
---|
297 | if(!is_null($Value))
|
---|
298 | {
|
---|
299 | $PathParts = explode('/', $Def['Name']);
|
---|
300 | $TempConfig = &$Config;
|
---|
301 | foreach($PathParts as $Part)
|
---|
302 | {
|
---|
303 | $TempConfig = &$TempConfig[$Part];
|
---|
304 | }
|
---|
305 | if(!is_array($TempConfig)) $TempConfig = $Value;
|
---|
306 | else $Value = $Def['Default'];
|
---|
307 | }
|
---|
308 | }
|
---|
309 | $ConfigText = $this->CreateConfig($Config);
|
---|
310 | file_put_contents($this->ConfigDir.'/config.php', $ConfigText);
|
---|
311 | $Output .= 'Konfigurace nastavena<br/>';
|
---|
312 | return($Output);
|
---|
313 | }
|
---|
314 |
|
---|
315 | function CreateConfig($Config)
|
---|
316 | {
|
---|
317 | $Output = "<?php\n\n".
|
---|
318 | "\$IsDeveloper = in_array(\$_SERVER['REMOTE_ADDR'], array('127.0.0.1'));\n\n";
|
---|
319 |
|
---|
320 | foreach($this->ConfigDefinition as $Def)
|
---|
321 | {
|
---|
322 | $PathParts = explode('/', $Def['Name']);
|
---|
323 | $Output .= "\$Config";
|
---|
324 | foreach($PathParts as $Part)
|
---|
325 | $Output .= "['".$Part."']";
|
---|
326 | $TempConfig = &$Config;
|
---|
327 | foreach($PathParts as $Part)
|
---|
328 | if(array_key_exists($Part, $TempConfig))
|
---|
329 | {
|
---|
330 | $TempConfig = &$TempConfig[$Part];
|
---|
331 | }
|
---|
332 | if(!is_array($TempConfig)) $Value = $TempConfig;
|
---|
333 | else $Value = $Def['Default'];
|
---|
334 | $Output .= " = '".$Value."';\n";
|
---|
335 | }
|
---|
336 | $Output .= "\n\n";
|
---|
337 | return($Output);
|
---|
338 | }
|
---|
339 | }
|
---|
340 |
|
---|
341 | class PageSetupRedirect extends Page
|
---|
342 | {
|
---|
343 | function Show()
|
---|
344 | {
|
---|
345 | $Output = '';
|
---|
346 | if(!$this->Database->Connected()) $Output .= 'Nelze se připojit k databázi.<br>';
|
---|
347 | else {
|
---|
348 | if(!$this->System->Setup->UpdateManager->IsInstalled())
|
---|
349 | $Output .= 'Systém vyžaduje instalaci databáze.<br>';
|
---|
350 | else
|
---|
351 | if(!$this->System->Setup->UpdateManager->IsUpToDate())
|
---|
352 | $Output .= 'Systém vyžaduje aktualizaci databáze.<br>';
|
---|
353 | }
|
---|
354 | $Output .= 'Pokračujte <a href="'.$this->System->Link('/setup/').'">zde</a>';
|
---|
355 | return($Output);
|
---|
356 | }
|
---|
357 | }
|
---|
358 |
|
---|
359 | class Setup extends Model
|
---|
360 | {
|
---|
361 | var $UpdateManager;
|
---|
362 |
|
---|
363 | function Start()
|
---|
364 | {
|
---|
365 | global $DatabaseRevision;
|
---|
366 |
|
---|
367 | $this->System->RegisterPage('', 'PageSetupRedirect');
|
---|
368 | $this->System->RegisterPage('setup', 'PageSetup');
|
---|
369 |
|
---|
370 | // Check database persistence structure
|
---|
371 | $this->UpdateManager = new UpdateManager();
|
---|
372 | $this->UpdateManager->Database = $this->Database;
|
---|
373 | $this->UpdateManager->Revision = $DatabaseRevision;
|
---|
374 | $Updates = new Updates();
|
---|
375 | $this->UpdateManager->Trace = $Updates->Get();
|
---|
376 | $this->UpdateManager->InstallMethod = 'FullInstall';
|
---|
377 | }
|
---|
378 |
|
---|
379 | function Stop()
|
---|
380 | {
|
---|
381 | unset($this->UpdateManager);
|
---|
382 | $this->System->UnregisterPage('');
|
---|
383 | $this->System->UnregisterPage('setup');
|
---|
384 | }
|
---|
385 |
|
---|
386 | function CheckState()
|
---|
387 | {
|
---|
388 | return($this->Database->Connected() and $this->UpdateManager->IsInstalled() and
|
---|
389 | $this->UpdateManager->IsUpToDate());
|
---|
390 | }
|
---|
391 |
|
---|
392 | function Install()
|
---|
393 | {
|
---|
394 | $this->Database->query('CREATE TABLE IF NOT EXISTS `SystemVersion` (
|
---|
395 | `Id` int(11) NOT NULL AUTO_INCREMENT,
|
---|
396 | `Revision` int(11) NOT NULL,
|
---|
397 | PRIMARY KEY (`Id`)
|
---|
398 | ) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;');
|
---|
399 | $this->Database->query("INSERT INTO `SystemVersion` (`Id`, `Revision`) VALUES
|
---|
400 | (1, 591);");
|
---|
401 | $this->Database->query("CREATE TABLE IF NOT EXISTS `Module` (
|
---|
402 | `Id` int(11) NOT NULL AUTO_INCREMENT,
|
---|
403 | `Name` varchar(255) NOT NULL,
|
---|
404 | `Title` varchar(255) NOT NULL,
|
---|
405 | PRIMARY KEY (`Id`)
|
---|
406 | ) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;");
|
---|
407 | }
|
---|
408 |
|
---|
409 | function Uninstall()
|
---|
410 | {
|
---|
411 | $this->System->ModuleManager->UninstallAll();
|
---|
412 | $this->Database->query('DROP TABLE `Module`');
|
---|
413 | $this->Database->query('DROP TABLE `SystemVersion`');
|
---|
414 | }
|
---|
415 |
|
---|
416 | function IsInstalled()
|
---|
417 | {
|
---|
418 | $DbResult = $this->Database->query('SHOW TABLES LIKE "'.$this->VersionTable.'"');
|
---|
419 | return($DbResult->num_rows > 0);
|
---|
420 | }
|
---|
421 |
|
---|
422 | function Upgrade()
|
---|
423 | {
|
---|
424 | $Output = $this->UpdateManager->Upgrade();
|
---|
425 | return($Output);
|
---|
426 | }
|
---|
427 | }
|
---|