source: trunk/Common/Setup/Setup.php@ 619

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