source: web/Packages/Common/Setup.php

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