QuartzMap/admin/setup.php

254 lines
6.8 KiB
PHP
Raw Normal View History

2024-03-16 23:03:30 +00:00
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
require('class/user.php');
if(file_exists('incl/const.php')){
require('incl/const.php');
}
$msg="";
$smtp_keys = ['host', 'user', 'pass', 'port'];
$host=empty(DB_HOST) ? '' : DB_HOST;
$port=empty(DB_PORT) ? '' : DB_PORT;
$dbuname=empty(DB_USER) ? '' : DB_USER;
$dbpwd=empty(DB_PASS) ? '' : DB_PASS;
$dbname=empty(DB_NAME) ? '' : DB_NAME;
$apps_dir=empty(APPS_DIR) ? '' : APPS_DIR;
$data_dir=empty(DATA_DIR) ? '' : DATA_DIR;
$cache_dir=empty(CACHE_DIR) ? '' : CACHE_DIR;
if(isset($_POST['submit'])){
$host=$_POST['host'];
$port=$_POST['port'];
$dbuname=$_POST['dbuname'];
$dbpwd=$_POST['dbpwd'];
$dbname=$_POST['dbname'];
$con = pg_connect("dbname=$dbname user=$dbuname password=$dbpwd host=$host port=$port");
if(!$con){
$msg= pg_last_error($con);
}else{
$file_data = "<?php\n";
$file_data .= "const DB_HOST = '$host';\n";
$file_data .= "const DB_NAME = '$dbname';\n";
$file_data .= "const DB_SCMA = 'public';\n";
$file_data .= "const DB_USER = '$dbuname';\n";
$file_data .= "const DB_PASS = '$dbpwd';\n";
$file_data .= "const DB_PORT = '$port';\n";
$file_data .= "const SUPER_ADMIN_ID = 1;\n";
$file_data .= "const ACCESS_LEVELS = array('User', 'Admin', 'Devel');\n";
$file_data .= "const ADMINISTRATION_ACCESS_LEVELS = array('Admin', 'Devel');\n";
$file_data .= "const APPS_DIR = '$apps_dir';\n";
$file_data .= "const DATA_DIR = '$data_dir';\n";
$file_data .= "const CACHE_DIR = '$cache_dir';\n";
foreach($smtp_keys as $k){
$file_data .= "const SMTP_".strtoupper($k)." = '".$_POST['smtp_'.$k]."';\n";
}
file_put_contents('incl/const.php', $file_data);
$sql = file_get_contents('setup.sql');
$res = pg_query($con, $sql);
if(!$res){
echo pg_last_error($con);
die();
}
$def_user = array('name' => 'John Smith', 'email' => 'admin@admin.com', 'password' => '1234',
'accesslevel' => 'Admin', 'ftp_user' => 'admin1', 'owner_id' => 1);
$def_grp = array('name' => 'Default', 'owner_id' => 1);
$def_usr_grps = array('user_id' => 1, 'access_group_id' => 1);
$def_user['password'] = password_hash($def_user['password'], PASSWORD_DEFAULT);
// insert manually
if(!pg_insert($con, 'public.user', $def_user) ||
!pg_insert($con, 'public.access_groups', $def_grp) ||
!pg_insert($con, 'public.user_access', $def_usr_grps) ){
die(pg_last_error($con));
}
user_Class::create_ftp_user($def_user['ftp_user'], $def_user['email'], $def_user['password']);
if(!isset($_POST['allow_signup'])){
$result = pg_query($con, 'DROP TABLE signup');
unlink('../signup.php');
unlink('class/signup.php');
unlink('action/signup.php');
unlink('action/verify.php');
}
unlink('setup.sql');
unlink('setup.php');
header('location:index.php');
}
}
?>
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<title>QGIS2Map App Installer</title>
<link href="../assets/dist/css/bootstrap.min.css" rel="stylesheet">
<style>
table{width:30% !important; text-align:center; margin:auto; margin-top:70px;}
.success{color:green;}
.error{color:red;}
.frm{width:70% !important; margin:auto; margin-top:100px;}
</style>
</head>
<body>
<main role="main" class="container">
<?php
if((isset($_GET['step'])) && $_GET['step']==2){
?>
<div align="center"><p>&nbsp;</p><img src="img/jri-admin-logo.png"></div>
<form class="frm" method="post">
<span class="error"><?=$msg?></span>
<div>
<fieldset>
<legend>Database</legend>
<div class="form-group">
<input type="text" class="form-control" placeholder="Host" required name="host" value="<?=$host?>">
</div>
<div class="form-group">
<input type="number" class="form-control" placeholder="Port Number" required name="port" value="<?=$port?>">
</div>
<div class="form-group">
<input type="text" class="form-control" placeholder="Database User Name" required name="dbuname" value="<?=$dbuname?>">
</div>
<div class="form-group">
<input type="text" class="form-control" placeholder="Database Password" name="dbpwd" value="<?=$dbpwd?>">
</div>
<div class="form-group">
<input type="text" class="form-control" placeholder="Database Name" required name="dbname" value="<?=$dbname?>">
</div>
</fieldset>
</div>
<div>
<fieldset>
<legend>SMTP details:</legend>
<div class="form-group">
<?php foreach($smtp_keys as $k){ ?>
<input type="text" class="form-control" placeholder="<?=$k?>" name="smtp_<?=$k?>" value="" required>
<?php } ?>
</div>
</fieldset>
</div>
<div>
<fieldset>
<legend>Options</legend>
<div class="form-group">
<input type="checkbox" class="form-checkbox" placeholder="signup allowed" name="allow_signup" value="1"/>
<label for="allow_signup">Allow Sign-Up for Admin accounts</label>
</div>
</fieldset>
</div>
<div align="right">
<button type="submit" name="submit" class="btn btn-primary">Submit</button>
</div>
</form>
<?php
}else{
?>
<div align="center"><p>&nbsp;</p>QGIS2Map App Installer</div>
<table class="table">
<thead>
<tr>
<th scope="col">Requirement</th>
<th scope="col">Status</th>
</tr>
</thead>
<tbody>
<tr>
<th scope="row">PHP Version</th>
<td>
<?php
$is_error="";
$php_version=phpversion();
if($php_version>5){
echo "<span class='success'>".$php_version."</span>";
}else{
echo "<span class='error'>".$php_version."</span>";
$is_error='yes';
}
?>
</td>
</tr>
<tr>
<th scope="row">Session Working</th>
<td>
<?php
$_SESSION['IS_WORKING']=1;
if(!empty($_SESSION['IS_WORKING'])){
echo "<span class='success'>Yes</span>";
}else{
echo "<span class='error'>No</span>";
$is_error='yes';
}
?>
</td>
</tr>
<tr>
<th scope="row"><?=$apps_dir?></th>
<td>
<?php
if(is_writeable($apps_dir)){
echo "<span class='success'>Writeable</span>";
}else{
echo "<span class='error'>Not writeable</span>";
$is_error='yes';
}
?>
</td>
</tr>
<tr>
<td colspan="2">
<?php
if($is_error==''){
?>
<a href="?step=2"><button type="button" class="btn btn-success">Next</button></a>
<?php
}else{
?><button type="button" class="btn btn-danger">Errors</button><br><br>Please fix above error(s) and try again<?php
}
?>
</td>
</tr>
</tbody>
</table>
<?php }?>
</main>
<script src="https://getbootstrap.com/docs/4.0/dist/js/bootstrap.min.js"></script>
</body>
</html>