This commit is contained in:
Matteo Gheza 2020-06-05 17:56:22 +02:00
parent 524ef72576
commit bb309f27c4
4 changed files with 77 additions and 40 deletions

5
.dockerignore Normal file
View File

@ -0,0 +1,5 @@
*/vendor
*.md
.git
config.php
config.old.php

View File

@ -161,11 +161,18 @@ class database{
}
}
public function isOptionsEmpty(){
return empty($this->database->exec("SELECT * FROM `%PREFIX%_options`;", true));
}
public function __construct(){
if(!defined("DATABASE")){
define("DATABASE", "OK");
}
$this->connect();
if($this->isOptionsEmpty){
header('Location: install/install.php');
}
}
public function close(){

View File

@ -12,6 +12,43 @@ if (file_exists("installHelper.php")) {
if (!file_exists("runInstall.php")) {
die("Already installed");
}
$populated = false;
if (file_exists("../config.php")) {
try {
require('../config.php');
$dbnameValue = DB_NAME;
$unameValue = DB_USER;
$pwdValue = DB_PASSWORD;
$dbhostValue = DB_HOST;
$prefixValue = DB_PREFIX;
if(checkConnection($dbhostValue,$unameValue,$pwdValue,$dbnameValue,true)){
$configExist = true;
try{
$connection = new PDO("mysql:host=$dbhostValue;dbname=$dbnameValue", $unameValue, $pwdValue,[PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]);
$populated = !empty($connection->prepare(str_replace("%PREFIX%", DB_PREFIX, "SELECT * FROM `%PREFIX%_dbversion`;"))->execute());
$userPopulated = !empty($connection->prepare(str_replace("%PREFIX%", DB_PREFIX, "SELECT * FROM `%PREFIX%_users`;"))->execute());
} catch (PDOException $e){
$populated = false;
}
}
} catch (Exception $e) {
$dbnameValue = "allerta";
$unameValue = "user";
$pwdValue = "password";
$dbhostValue = "localhost";
$prefixValue = "allerta01";
$configExist = false;
}
} else {
$dbnameValue = "allerta";
$unameValue = "user";
$pwdValue = "password";
$dbhostValue = "localhost";
$prefixValue = "allerta01";
$configExist = false;
}
if(!is_cli()){
?>
<html xmlns="http://www.w3.org/1999/xhtml"><head>
@ -27,7 +64,7 @@ if(!is_cli()){
</head>
<body class="wp-core-ui">
<p id="logo"><a href="javascript:alert('TODO: add docs');">Allerta</a></p>
<?php if(!isset($_POST["step"])){ ?>
<?php if(!isset($_POST["step"]) && !$configExist){ ?>
<h1 class="screen-reader-text">Prima di iniziare</h1>
<p>Benvenuto in Allerta. Prima di iniziare abbiamo bisogno di alcune informazioni sul database. Devi conoscere i seguenti dati prima di procedere.</p>
<ol>
@ -49,29 +86,7 @@ if(!is_cli()){
</form>
</p>
<?php
} else if ($_POST["step"] == "2") {
if (file_exists("../config.php")) {
try {
require('../config.php');
$dbnameValue = DB_NAME;
$unameValue = DB_USER;
$pwdValue = DB_PASSWORD;
$dbhostValue = DB_HOST;
$prefixValue = DB_PREFIX;
} catch (Exception $e) {
$dbnameValue = "allerta";
$unameValue = "user";
$pwdValue = "password";
$dbhostValue = "localhost";
$prefixValue = "allerta01";
}
} else {
$dbnameValue = "allerta";
$unameValue = "user";
$pwdValue = "password";
$dbhostValue = "localhost";
$prefixValue = "allerta01";
}
} else if (in_array("2",$_POST)) {
?>
<h1 class="screen-reader-text">Configura la connection al database</h1>
<form method="post">
@ -111,7 +126,7 @@ if(!is_cli()){
<p class="step"><input name="submit" type="submit" value="Invia" class="button button-large"></p>
</form>
<?php
} else if ($_POST["step"] == "3") {
} else if (in_array("3",$_POST)) {
checkConnection($_POST["dbhost"],$_POST["uname"],$_POST["pwd"],$_POST["dbname"]);
generateConfig($_POST["dbhost"],$_POST["uname"],$_POST["pwd"],$_POST["dbname"],$_POST["prefix"]);
?>
@ -124,8 +139,10 @@ if(!is_cli()){
</form>
</p>
<?php
} else if ($_POST["step"] == "4") {
} else if ($configExist && !$populated) {
initDB();
header("Location: install.php");
} else if ($populated && !$userPopulated) {
?>
<h1 class="screen-reader-text">Evviva!</h1>
<p>Hai <b>quasi terminato</b> l'installazione di Allerta, devi solo inserire alcune informazioni.</p>
@ -218,7 +235,7 @@ if(!is_cli()){
</form>
</p>
<?php
} else if ($_POST["step"] == "5") {
} else if ($_POST["step"] == "5" || $userPopulated) {
initOptions($_POST["user_name"], isset($_POST["admin_visible"]), $_POST["admin_password"], $_POST["admin_email"], $_POST["owner"]);
?>
<h1 class="screen-reader-text">Installazione terminata con successo.</h1>

View File

@ -38,16 +38,19 @@ if (file_exists('../vendor/autoload.php')) {
define('NAME', 'AllertaVVF');
define('VERSION', '0.1-alpha');
function checkConnection($host, $user, $password, $database){
function checkConnection($host, $user, $password, $database, $return=false){
try{
$connection = new PDO("mysql:host=$host", $user, $password,[PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]);
$connectionOk = true;
} catch (PDOException $e){
if(is_cli()){
echo($e);
exit(8);
}
$connectionOk = false;
if($return){
return false;
} else {
if(is_cli()){
echo($e);
exit(8);
}
$connectionOk = false;
?>
<div class="wp-die-message"><h1>Errore nello stabilire una connection al database</h1>
<p>Questo potrebbe voler dire che name user e password nel file <code>config.php</code> sono sbagliate o che non possiamo contattare il database <code><?php echo $database; ?></code>. Potrebbe voler dire che il tuo database è irraddbile.</p>
@ -64,7 +67,8 @@ function checkConnection($host, $user, $password, $database){
<p class="step"><a href="#" onclick="javascript:history.go(-1);return false;" class="button button-large">Riprova</a></p>
</div>
<?php
exit();
exit();
}
}
if($connectionOk){
try{
@ -75,10 +79,13 @@ function checkConnection($host, $user, $password, $database){
}
$connection->exec("use " . preg_replace('/[^a-zA-Z0-9]/', '', trim($database)));
} catch (PDOException $e){
if(is_cli()){
echo($e);
exit(7);
}
if($return){
return false;
} else {
if(is_cli()){
echo($e);
exit(7);
}
?>
<div class="wp-die-message"><h1>Impossibile selezionare il database</h1>
<p>Siamo riusciti a connetterci al server del database (il che significa che il tuo name user e password sono ok), ma non siamo riusciti a selezionare il database <code><?php echo $database; ?></code>.</p>
@ -95,8 +102,10 @@ function checkConnection($host, $user, $password, $database){
<p class="step"><a href="#" onclick="javascript:history.go(-1);return false;" class="button button-large">Riprova</a></p>
</div>
<?php
exit();
exit();
}
}
return true;
}
}
@ -160,7 +169,6 @@ define( 'DB_PREFIX', '<?php echo $prefix; ?>' );<br>
function initDB(){
try{
require "../config.php";
$connection = new PDO("mysql:host=".DB_HOST.";dbname=".DB_NAME, DB_USER, DB_PASSWORD,[PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]);
$prefix = DB_PREFIX;
$connection->exec("