webmum/installer/step7.php
Oliver Hartl a7c3a4f271 Implementation of the WebMUM installer (#52)
* Preparation for installer and discontinue support of old config style.

* Adding installer with step 0

* Fixing travis config, because example config isn't loaded anymore if normal config does not exist.

* Adding installer step 1: database connection

* Adding installer step 2: database schema

* Adding installer step 3: Your first admin user

* Minor fix on step 3

* Adding installer step 4: general settings

* Adding installer step 5: optional features

* Adding final installation step 7: writing config & finish

* Display the current installation progress

* Fixing installation finish

* Disable direct access to installer

* Change colors of "notifying" text

* Add check and cross marks to requirements page of the installer

* Change how config directory is checked, no more writable check but error if writing fails.

* Execute post-receice..
2016-06-06 18:49:21 +02:00

152 lines
No EOL
5.2 KiB
PHP

<?php
if(strpos($_SERVER['REQUEST_URI'], 'installer/') !== false){
die('You cannot directly access the installer files.');
}
/*-----------------------------------------------------------------------------*/
$thisStep = 7;
$error = '';
/*-----------------------------------------------------------------------------*/
$configPath = dirname(__DIR__).DIRECTORY_SEPARATOR.'config'.DIRECTORY_SEPARATOR.'config.php';
$configString = '<?php'.PHP_EOL
.'// This config has been automatically generated by the WebMUM installer.'.PHP_EOL.PHP_EOL
.'return '.var_export($_SESSION['installer']['config'], true).';'.PHP_EOL;
/*-----------------------------------------------------------------------------*/
if(isset($_SESSION['installer']['finished'])){
if(!file_exists($configPath)){
unset($_SESSION['installer']['finished']);
}
}
if(isset($_GET['go'])){
if($_GET['go'] == 'next' && $_SERVER['REQUEST_METHOD'] == 'POST'){
try{
if(isset($_POST['automatic']) && $_POST['automatic'] == 1){
if(file_exists($configPath)){
throw new Exception('The file "'.$configPath.'"" already exists, if you\'ve already written the config manually then complete manually.');
}
if(!file_exists(dirname($configPath)) || !is_dir(dirname($configPath))){
throw new Exception('The directory "'.dirname($configPath).'"" is missing.');
}
// Write config
if(file_put_contents($configPath, $configString) === false){
throw new Exception('Couldn\'t automatically write config to "'.$configPath.'", please write the config on your own.');
}
$_SESSION['installer']['finished'] = true;
}
elseif(isset($_POST['manual']) && $_POST['manual'] == 1){
if(!file_exists($configPath)){
throw new Exception('You need to write the config file first before you can manually complete the installation.');
}
$configValues = require_once 'config/config.php';
if(!is_array($configValues)){
throw new Exception('The data in the config file is invalid, please try again and be sure to use the config provided below.');
}
$_SESSION['installer']['finished'] = true;
}
}
catch(Exception $e){
$error = $e->getMessage();
}
}
elseif($_GET['go'] == 'finish'){
try{
if(isset($_SESSION['installer']['finished'])){
// Load config
$configValues = include_once 'config/config.php';
if(!is_array($configValues)){
throw new Exception('Error writing the config, please manually write the config to "'.$configPath.'".');
}
// Init system
Config::init($configValues);
Database::init(Config::get('mysql'));
Auth::init();
// Login user
Auth::login($_SESSION['installer']['user']['user'], $_SESSION['installer']['user']['password']);
// Reset installer
unset($_SESSION['installer']);
Router::redirect('/');
}
}
catch(Exception $e){
$error = $e->getMessage();
}
}
elseif($_GET['go'] == 'prev'){
installer_prev($thisStep);
}
}
?>
<?php if(isset($_SESSION['installer']['finished'])): ?>
<form class="form" action="/?step=<?php echo $thisStep; ?>&go=finish" method="post">
<div class="notification notification-success">You finished your installation!</div>
<h2 style="text-align: center;">Welcome to WebMUM - Web Mailserver User Manager.</h2>
<div>
If you like this project, be sure to give us a Star and Follow the project on GitHub <a target="_blank" href="https://git.io/vwXhh">https://github.com/ohartl/webmum</a>
<ol>
<li>To change the configuration you have to edit to config file "<?php echo $configPath; ?>" (see <a target="_blank" href="https://git.io/vwXhh#webmum-configuration">the README</a> for further instructions.</li>
<li>If you've found a bug or got a great idea, feel free to submit an issue on GitHub <a target="_blank" href="https://git.io/vrnOM">here</a>.</li>
</ol>
</div>
<hr class="invisible">
<p>By clicking Finish you will end the installation process and get logged in automatically.</p>
<div class="buttons">
<button class="button button-primary" type="submit">Finish &amp; Start using WebMUM</button>
</div>
</form>
<?php else: ?>
<?php echo installer_message(); ?>
<h2>Step 6 of <?php echo INSTALLER_MAX_STEP; ?>: Write the config &amp; finish the installation!</h2>
<?php if(!empty($error)): ?>
<div class="notification notification-fail"><?php echo $error; ?></div>
<?php endif; ?>
<form class="form" action="/?step=<?php echo $thisStep; ?>&go=next" method="post">
<p>The following config needs to be written to <code><?php echo $configPath; ?></code>.</p>
<textarea readonly style="width: 100%; height: 500px;"><?php echo $configString; ?></textarea>
<hr class="invisible">
<div>
This is the last step, you are almost there!<br>
<ul>
<li>Click "Already manually completed" if you already wrote the config to "<?php echo $configPath; ?>".</li>
<li>Or click "Complete automatically" if you want the installer to do the work for you.</li>
</ul>
</div>
<div class="buttons">
<a class="button" href="/?step=<?php echo $thisStep; ?>&go=prev">Back</a>
<button class="button" name="manual" value="1" type="submit">Already manually completed</button>
<button class="button button-primary" name="automatic" value="1" type="submit">Complete automatically!</button>
</div>
</form>
<?php endif; ?>