Fix database date: 0000-00-00 -> NULL
This commit is contained in:
parent
1aac22b46d
commit
ab7f85b404
2
.gitignore
vendored
2
.gitignore
vendored
@ -1,2 +1,2 @@
|
|||||||
|
.phpintel
|
||||||
vendor/
|
vendor/
|
||||||
*.komodoproject
|
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
<?php
|
<?php
|
||||||
require(dirname(__FILE__) . "/config.php");
|
require(dirname(__FILE__) . "/config.php");
|
||||||
|
|
||||||
$options[PDO::ATTR_ERRMODE] = PDO::ERRMODE_EXCEPTION;
|
$options[PDO::ATTR_ERRMODE] = PDO::ERRMODE_EXCEPTION;
|
||||||
$bdd = new PDO("mysql:host=$host;port=$port;dbname=$db", $user, $pass, $options);
|
$bdd = new PDO("mysql:host=$host;port=$port;dbname=$db", $user, $pass, $options);
|
||||||
|
@ -1,5 +1,21 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
|
function getMigrationSchemas() {
|
||||||
|
return [ 0, 5 ];
|
||||||
|
}
|
||||||
|
|
||||||
|
function updateSchema($bdd, $newKey) {
|
||||||
|
if ($newKey === 0) {
|
||||||
|
$req_string = 'INSERT INTO `application` (sql_schema) VALUES (?)';
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
$req_string = 'UPDATE `application` SET `sql_schema` = ?';
|
||||||
|
}
|
||||||
|
|
||||||
|
$req = $bdd->prepare($req_string);
|
||||||
|
$req->execute(array($newKey));
|
||||||
|
}
|
||||||
|
|
||||||
function printError($str) {
|
function printError($str) {
|
||||||
echo '<div class="alert alert-danger" role="alert">' . $str . '</div>';
|
echo '<div class="alert alert-danger" role="alert">' . $str . '</div>';
|
||||||
}
|
}
|
||||||
|
@ -119,8 +119,8 @@
|
|||||||
$phone = "";
|
$phone = "";
|
||||||
$online = 0;
|
$online = 0;
|
||||||
$enable = 1;
|
$enable = 1;
|
||||||
$start = "0000-00-00";
|
$start = NULL;
|
||||||
$end = "0000-00-00";
|
$end = NULL;
|
||||||
|
|
||||||
$req = $bdd->prepare('INSERT INTO user (user_id, user_pass, user_mail, user_phone, user_online, user_enable, user_start_date, user_end_date)
|
$req = $bdd->prepare('INSERT INTO user (user_id, user_pass, user_mail, user_phone, user_online, user_enable, user_start_date, user_end_date)
|
||||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?)');
|
VALUES (?, ?, ?, ?, ?, ?, ?, ?)');
|
||||||
@ -154,6 +154,9 @@
|
|||||||
if ($field === 'user_pass') {
|
if ($field === 'user_pass') {
|
||||||
$value = hashPass($value);
|
$value = hashPass($value);
|
||||||
}
|
}
|
||||||
|
else if (($field === 'user_start_date' || $field === 'user_end_date') && $value === '') {
|
||||||
|
$value = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
// /!\ SQL injection: field was checked with in_array function
|
// /!\ SQL injection: field was checked with in_array function
|
||||||
$req_string = 'UPDATE user SET ' . $field . ' = ? WHERE user_id = ?';
|
$req_string = 'UPDATE user SET ' . $field . ' = ? WHERE user_id = ?';
|
||||||
|
37
index.php
37
index.php
@ -8,7 +8,7 @@
|
|||||||
if(isset($_GET['logout'])){
|
if(isset($_GET['logout'])){
|
||||||
session_destroy();
|
session_destroy();
|
||||||
header("Location: .");
|
header("Location: .");
|
||||||
exit -1;
|
exit(-1);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get the configuration files ?
|
// Get the configuration files ?
|
||||||
@ -79,7 +79,7 @@
|
|||||||
if($data && passEqual($_POST['admin_pass'], $data['admin_pass'])) {
|
if($data && passEqual($_POST['admin_pass'], $data['admin_pass'])) {
|
||||||
$_SESSION['admin_id'] = $data['admin_id'];
|
$_SESSION['admin_id'] = $data['admin_id'];
|
||||||
header("Location: index.php?admin");
|
header("Location: index.php?admin");
|
||||||
exit -1;
|
exit(-1);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
$error = true;
|
$error = true;
|
||||||
@ -110,7 +110,7 @@
|
|||||||
if(isInstalled($bdd) == true) {
|
if(isInstalled($bdd) == true) {
|
||||||
printError('OpenVPN-admin is already installed. Redirection.');
|
printError('OpenVPN-admin is already installed. Redirection.');
|
||||||
header( "refresh:3;url=index.php?admin" );
|
header( "refresh:3;url=index.php?admin" );
|
||||||
exit -1;
|
exit(-1);
|
||||||
}
|
}
|
||||||
|
|
||||||
// If the user sent the installation form
|
// If the user sent the installation form
|
||||||
@ -122,18 +122,26 @@
|
|||||||
if($admin_pass != $admin_repeat_pass) {
|
if($admin_pass != $admin_repeat_pass) {
|
||||||
printError('The passwords do not correspond. Redirection.');
|
printError('The passwords do not correspond. Redirection.');
|
||||||
header( "refresh:3;url=index.php?installation" );
|
header( "refresh:3;url=index.php?installation" );
|
||||||
exit -1;
|
exit(-1);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create the tables or die
|
// Create the initial tables
|
||||||
$sql_file = dirname(__FILE__) . '/sql/import.sql';
|
$migrations = getMigrationSchemas();
|
||||||
try {
|
foreach ($migrations as $migration_value) {
|
||||||
$sql = file_get_contents($sql_file);
|
$sql_file = dirname(__FILE__) . "/sql/schema-$migration_value.sql";
|
||||||
$bdd->exec($sql);
|
try {
|
||||||
}
|
$sql = file_get_contents($sql_file);
|
||||||
catch (PDOException $e) {
|
$bdd->exec($sql);
|
||||||
printError($e->getMessage());
|
}
|
||||||
exit -1;
|
catch (PDOException $e) {
|
||||||
|
printError($e->getMessage());
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
unlink($sql_file);
|
||||||
|
|
||||||
|
// Update schema to the new value
|
||||||
|
updateSchema($bdd, $migration_value);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Generate the hash
|
// Generate the hash
|
||||||
@ -143,7 +151,6 @@
|
|||||||
$req = $bdd->prepare('INSERT INTO admin (admin_id, admin_pass) VALUES (?, ?)');
|
$req = $bdd->prepare('INSERT INTO admin (admin_id, admin_pass) VALUES (?, ?)');
|
||||||
$req->execute(array($admin_username, $hash_pass));
|
$req->execute(array($admin_username, $hash_pass));
|
||||||
|
|
||||||
unlink($sql_file);
|
|
||||||
rmdir(dirname(__FILE__) . '/sql');
|
rmdir(dirname(__FILE__) . '/sql');
|
||||||
printSuccess('Well done, OpenVPN-Admin is installed. Redirection.');
|
printSuccess('Well done, OpenVPN-Admin is installed. Redirection.');
|
||||||
header( "refresh:3;url=index.php?admin" );
|
header( "refresh:3;url=index.php?admin" );
|
||||||
@ -154,7 +161,7 @@
|
|||||||
require(dirname(__FILE__) . '/include/html/form/installation.php');
|
require(dirname(__FILE__) . '/include/html/form/installation.php');
|
||||||
}
|
}
|
||||||
|
|
||||||
exit -1;
|
exit(-1);
|
||||||
}
|
}
|
||||||
|
|
||||||
// --------------- CONFIGURATION ---------------
|
// --------------- CONFIGURATION ---------------
|
||||||
|
@ -31,6 +31,7 @@ done
|
|||||||
www=$1
|
www=$1
|
||||||
user=$2
|
user=$2
|
||||||
group=$3
|
group=$3
|
||||||
|
|
||||||
openvpn_admin="$www/openvpn-admin"
|
openvpn_admin="$www/openvpn-admin"
|
||||||
|
|
||||||
# Check the validity of the arguments
|
# Check the validity of the arguments
|
||||||
@ -237,7 +238,6 @@ cp "/etc/openvpn/"{ca.crt,ta.key} "./client-conf/windows/"
|
|||||||
bower --allow-root install
|
bower --allow-root install
|
||||||
chown -R "$user:$group" "$openvpn_admin"
|
chown -R "$user:$group" "$openvpn_admin"
|
||||||
|
|
||||||
|
|
||||||
printf "\033[1m\n#################################### Finish ####################################\n"
|
printf "\033[1m\n#################################### Finish ####################################\n"
|
||||||
|
|
||||||
echo -e "# Congratulations, you have successfully setup OpenVPN-Admin! #\r"
|
echo -e "# Congratulations, you have successfully setup OpenVPN-Admin! #\r"
|
||||||
|
@ -10,9 +10,8 @@ remote_port_1=$(echap "$remote_port_1")
|
|||||||
bytes_received=$(echap "$bytes_received")
|
bytes_received=$(echap "$bytes_received")
|
||||||
bytes_sent=$(echap "$bytes_sent")
|
bytes_sent=$(echap "$bytes_sent")
|
||||||
|
|
||||||
|
|
||||||
# We insert data in the log table
|
# We insert data in the log table
|
||||||
mysql -h$HOST -P$PORT -u$USER -p$PASS $DB -e "INSERT INTO log (log_id, user_id, log_trusted_ip, log_trusted_port, log_remote_ip, log_remote_port, log_start_time, log_end_time, log_received, log_send) VALUES(NULL, '$common_name','$trusted_ip', '$trusted_port','$ifconfig_pool_remote_ip', '$remote_port_1', now(),'0000-00-00 00:00:00', '$bytes_received', '$bytes_sent')"
|
mysql -h$HOST -P$PORT -u$USER -p$PASS $DB -e "INSERT INTO log (log_id, user_id, log_trusted_ip, log_trusted_port, log_remote_ip, log_remote_port, log_start_time, log_end_time, log_received, log_send) VALUES(NULL, '$common_name','$trusted_ip', '$trusted_port','$ifconfig_pool_remote_ip', '$remote_port_1', now(),NULL, '$bytes_received', '$bytes_sent')"
|
||||||
|
|
||||||
# We specify that the user is online
|
# We specify that the user is online
|
||||||
mysql -h$HOST -P$PORT -u$USER -p$PASS $DB -e "UPDATE user SET user_online=1 WHERE user_id='$common_name'"
|
mysql -h$HOST -P$PORT -u$USER -p$PASS $DB -e "UPDATE user SET user_online=1 WHERE user_id='$common_name'"
|
||||||
|
@ -12,4 +12,4 @@ trusted_port=$(echap "$trusted_port")
|
|||||||
mysql -h$HOST -P$PORT -u$USER -p$PASS $DB -e "UPDATE user SET user_online=0 WHERE user_id='$common_name'"
|
mysql -h$HOST -P$PORT -u$USER -p$PASS $DB -e "UPDATE user SET user_online=0 WHERE user_id='$common_name'"
|
||||||
|
|
||||||
# We insert the deconnection datetime
|
# We insert the deconnection datetime
|
||||||
mysql -h$HOST -P$PORT -u$USER -p$PASS $DB -e "UPDATE log SET log_end_time=now(), log_received='$bytes_received', log_send='$bytes_sent' WHERE log_trusted_ip='$trusted_ip' AND log_trusted_port='$trusted_port' AND user_id='$common_name' AND log_end_time='0000-00-00 00:00:00'"
|
mysql -h$HOST -P$PORT -u$USER -p$PASS $DB -e "UPDATE log SET log_end_time=now(), log_received='$bytes_received', log_send='$bytes_sent' WHERE log_trusted_ip='$trusted_ip' AND log_trusted_port='$trusted_port' AND user_id='$common_name' AND log_end_time=NULL"
|
||||||
|
@ -6,7 +6,7 @@ username=$(echap "$username")
|
|||||||
password=$(echap "$password")
|
password=$(echap "$password")
|
||||||
|
|
||||||
# Authentication
|
# Authentication
|
||||||
user_pass=$(mysql -h$HOST -P$PORT -u$USER -p$PASS $DB -sN -e "SELECT user_pass FROM user WHERE user_id = '$username' AND user_enable=1 AND (TO_DAYS(now()) >= TO_DAYS(user_start_date) OR user_start_date='0000-00-00') AND (TO_DAYS(now()) <= TO_DAYS(user_end_date) OR user_end_date='0000-00-00')")
|
user_pass=$(mysql -h$HOST -P$PORT -u$USER -p$PASS $DB -sN -e "SELECT user_pass FROM user WHERE user_id = '$username' AND user_enable=1 AND (TO_DAYS(now()) >= TO_DAYS(user_start_date) OR user_start_date IS NULL) AND (TO_DAYS(now()) <= TO_DAYS(user_end_date) OR user_end_date IS NULL)")
|
||||||
|
|
||||||
# Check the user
|
# Check the user
|
||||||
if [ "$user_pass" == '' ]; then
|
if [ "$user_pass" == '' ]; then
|
||||||
|
55
migration.php
Normal file
55
migration.php
Normal file
@ -0,0 +1,55 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
if (count($argv) !== 2) {
|
||||||
|
echo "Need the www base path as argument";
|
||||||
|
exit(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
$www = $argv[1];
|
||||||
|
|
||||||
|
require("$www/include/config.php");
|
||||||
|
require("$www/include/connect.php");
|
||||||
|
require("$www/include/functions.php");
|
||||||
|
|
||||||
|
$migrations = getMigrationSchemas();
|
||||||
|
|
||||||
|
try {
|
||||||
|
$req = $bdd->prepare('SELECT `sql_schema` FROM `application` LIMIT 1');
|
||||||
|
$req->execute();
|
||||||
|
$data = $req->fetch();
|
||||||
|
|
||||||
|
$sql_schema = 0;
|
||||||
|
if ($data['sql_schema']) {
|
||||||
|
$sql_schema = $data['sql_schema'];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Table does not exist
|
||||||
|
catch (Exception $e) {
|
||||||
|
$sql_schema = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
// For each migrations
|
||||||
|
foreach ($migrations as $migration_value) {
|
||||||
|
|
||||||
|
// Do the migration, we are behind the last schema
|
||||||
|
if ($sql_schema < $migration_value) {
|
||||||
|
|
||||||
|
// Create the tables or die
|
||||||
|
$sql_file = dirname(__FILE__) . "/sql/schema-$migration_value.sql";
|
||||||
|
try {
|
||||||
|
$sql = file_get_contents($sql_file);
|
||||||
|
$bdd->exec($sql);
|
||||||
|
}
|
||||||
|
catch (PDOException $e) {
|
||||||
|
printError($e->getMessage());
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Update schema to the new value
|
||||||
|
updateSchema($bdd, $migration_value);
|
||||||
|
|
||||||
|
echo "Moved to schema $migration_value";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
?>
|
@ -1,3 +1,5 @@
|
|||||||
|
CREATE TABLE IF NOT EXISTS `application` ( `id` INT(11) AUTO_INCREMENT, `sql_schema` INT(11) NOT NULL, PRIMARY KEY (id) );
|
||||||
|
|
||||||
CREATE TABLE IF NOT EXISTS `admin` (
|
CREATE TABLE IF NOT EXISTS `admin` (
|
||||||
`admin_id` varchar(255) NOT NULL,
|
`admin_id` varchar(255) NOT NULL,
|
||||||
`admin_pass` varchar(255) NOT NULL,
|
`admin_pass` varchar(255) NOT NULL,
|
9
sql/schema-5.sql
Normal file
9
sql/schema-5.sql
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
CREATE TABLE IF NOT EXISTS `application` ( `id` INT(11) AUTO_INCREMENT, `sql_schema` INT(11) NOT NULL, PRIMARY KEY (id) );
|
||||||
|
|
||||||
|
ALTER TABLE `user` CHANGE `user_start_date` `user_start_date` DATE NULL DEFAULT NULL;
|
||||||
|
ALTER TABLE `user` CHANGE `user_end_date` `user_end_date` DATE NULL DEFAULT NULL;
|
||||||
|
ALTER TABLE `log` CHANGE `log_end_time` `log_end_time` TIMESTAMP NULL DEFAULT NULL;
|
||||||
|
|
||||||
|
UPDATE `user` SET `user_start_date` = NULL WHERE `user_start_date` = '0000-00-00';
|
||||||
|
UPDATE `user` SET `user_end_date` = NULL WHERE `user_end_date` = '0000-00-00';
|
||||||
|
UPDATE `log` SET `log_end_time` = NULL WHERE `log_end_time` = '0000-00-00';
|
@ -30,7 +30,7 @@ user=$(ls -l "$www/include/config.php" | awk '{ print $3 }')
|
|||||||
group=$(ls -l "$www/include/config.php" | awk '{ print $4 }')
|
group=$(ls -l "$www/include/config.php" | awk '{ print $4 }')
|
||||||
|
|
||||||
|
|
||||||
rm -r "${www:?}/"{index.php,bower.json,.bowerrc,js,include/html,include/connect.php,include/functions.php,include/grids.php,css,vendor}
|
rm -rf "${www:?}/"{index.php,bower.json,.bowerrc,js,include/html,include/connect.php,include/functions.php,include/grids.php,css,vendor}
|
||||||
|
|
||||||
cp -r "$base_path/"{index.php,bower.json,.bowerrc,js,css} "$www"
|
cp -r "$base_path/"{index.php,bower.json,.bowerrc,js,css} "$www"
|
||||||
cp -r "$base_path/include/"{html,connect.php,functions.php,grids.php} "$www/include"
|
cp -r "$base_path/include/"{html,connect.php,functions.php,grids.php} "$www/include"
|
||||||
@ -44,4 +44,10 @@ rm -f "/etc/openvpn/scripts/"{connect.sh,disconnect.sh,login.sh,functions.sh}
|
|||||||
cp "$base_path/installation/scripts/"{connect.sh,disconnect.sh,login.sh,functions.sh} "/etc/openvpn/scripts"
|
cp "$base_path/installation/scripts/"{connect.sh,disconnect.sh,login.sh,functions.sh} "/etc/openvpn/scripts"
|
||||||
chmod +x "/etc/openvpn/scripts/"{connect.sh,disconnect.sh,login.sh,functions.sh}
|
chmod +x "/etc/openvpn/scripts/"{connect.sh,disconnect.sh,login.sh,functions.sh}
|
||||||
|
|
||||||
|
echo "Processing database migration..."
|
||||||
|
|
||||||
|
php "$base_path/migration.php" "$www"
|
||||||
|
|
||||||
|
echo "Database migrations done."
|
||||||
|
|
||||||
echo "OpenVPN-admin upgraded."
|
echo "OpenVPN-admin upgraded."
|
||||||
|
Loading…
Reference in New Issue
Block a user