Skip to content

Commit

Permalink
Fix database date: 0000-00-00 -> NULL
Browse files Browse the repository at this point in the history
  • Loading branch information
Chocobozzz committed Feb 5, 2017
1 parent 1aac22b commit ab7f85b
Show file tree
Hide file tree
Showing 14 changed files with 125 additions and 28 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
.phpintel
vendor/
*.komodoproject
2 changes: 1 addition & 1 deletion include/connect.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php
require(dirname(__FILE__) . "/config.php");
require(dirname(__FILE__) . "/config.php");

$options[PDO::ATTR_ERRMODE] = PDO::ERRMODE_EXCEPTION;
$bdd = new PDO("mysql:host=$host;port=$port;dbname=$db", $user, $pass, $options);
Expand Down
16 changes: 16 additions & 0 deletions include/functions.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,21 @@
<?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) {
echo '<div class="alert alert-danger" role="alert">' . $str . '</div>';
}
Expand Down
7 changes: 5 additions & 2 deletions include/grids.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,8 @@
$phone = "";
$online = 0;
$enable = 1;
$start = "0000-00-00";
$end = "0000-00-00";
$start = NULL;
$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)
VALUES (?, ?, ?, ?, ?, ?, ?, ?)');
Expand Down Expand Up @@ -154,6 +154,9 @@
if ($field === 'user_pass') {
$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
$req_string = 'UPDATE user SET ' . $field . ' = ? WHERE user_id = ?';
Expand Down
6 changes: 3 additions & 3 deletions include/html/menu.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<div class="col-md-6 col-md-offset-3">
<nav class="navbar navbar-default">

<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<ul class="nav navbar-nav">
<li <?php if(!isset($_GET['admin'])) echo 'class="active"'; ?>><a href="index.php">Configurations</a></li>
<li <?php if(isset($_GET['admin'])) echo 'class="active"'; ?>><a href="index.php?admin">Administrator</a></li>
</ul>
</div>

</nav>
</div>
</div>
37 changes: 22 additions & 15 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
if(isset($_GET['logout'])){
session_destroy();
header("Location: .");
exit -1;
exit(-1);
}

// Get the configuration files ?
Expand Down Expand Up @@ -79,7 +79,7 @@
if($data && passEqual($_POST['admin_pass'], $data['admin_pass'])) {
$_SESSION['admin_id'] = $data['admin_id'];
header("Location: index.php?admin");
exit -1;
exit(-1);
}
else {
$error = true;
Expand Down Expand Up @@ -110,7 +110,7 @@
if(isInstalled($bdd) == true) {
printError('OpenVPN-admin is already installed. Redirection.');
header( "refresh:3;url=index.php?admin" );
exit -1;
exit(-1);
}

// If the user sent the installation form
Expand All @@ -122,18 +122,26 @@
if($admin_pass != $admin_repeat_pass) {
printError('The passwords do not correspond. Redirection.');
header( "refresh:3;url=index.php?installation" );
exit -1;
exit(-1);
}

// Create the tables or die
$sql_file = dirname(__FILE__) . '/sql/import.sql';
try {
$sql = file_get_contents($sql_file);
$bdd->exec($sql);
}
catch (PDOException $e) {
printError($e->getMessage());
exit -1;
// Create the initial tables
$migrations = getMigrationSchemas();
foreach ($migrations as $migration_value) {
$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);
}

unlink($sql_file);

// Update schema to the new value
updateSchema($bdd, $migration_value);
}

// Generate the hash
Expand All @@ -143,7 +151,6 @@
$req = $bdd->prepare('INSERT INTO admin (admin_id, admin_pass) VALUES (?, ?)');
$req->execute(array($admin_username, $hash_pass));

unlink($sql_file);
rmdir(dirname(__FILE__) . '/sql');
printSuccess('Well done, OpenVPN-Admin is installed. Redirection.');
header( "refresh:3;url=index.php?admin" );
Expand All @@ -154,7 +161,7 @@
require(dirname(__FILE__) . '/include/html/form/installation.php');
}

exit -1;
exit(-1);
}

// --------------- CONFIGURATION ---------------
Expand Down
2 changes: 1 addition & 1 deletion install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ done
www=$1
user=$2
group=$3

openvpn_admin="$www/openvpn-admin"

# Check the validity of the arguments
Expand Down Expand Up @@ -237,7 +238,6 @@ cp "/etc/openvpn/"{ca.crt,ta.key} "./client-conf/windows/"
bower --allow-root install
chown -R "$user:$group" "$openvpn_admin"


printf "\033[1m\n#################################### Finish ####################################\n"

echo -e "# Congratulations, you have successfully setup OpenVPN-Admin! #\r"
Expand Down
3 changes: 1 addition & 2 deletions installation/scripts/connect.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,8 @@ remote_port_1=$(echap "$remote_port_1")
bytes_received=$(echap "$bytes_received")
bytes_sent=$(echap "$bytes_sent")


# 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
mysql -h$HOST -P$PORT -u$USER -p$PASS $DB -e "UPDATE user SET user_online=1 WHERE user_id='$common_name'"
2 changes: 1 addition & 1 deletion installation/scripts/disconnect.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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'"

# 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"
2 changes: 1 addition & 1 deletion installation/scripts/login.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ username=$(echap "$username")
password=$(echap "$password")

# 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
if [ "$user_pass" == '' ]; then
Expand Down
55 changes: 55 additions & 0 deletions migration.php
Original file line number Diff line number Diff line change
@@ -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";
}
}

?>
2 changes: 2 additions & 0 deletions sql/import.sql → sql/schema-0.sql
Original file line number Diff line number Diff line change
@@ -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` (
`admin_id` varchar(255) NOT NULL,
`admin_pass` varchar(255) NOT NULL,
Expand Down
9 changes: 9 additions & 0 deletions sql/schema-5.sql
Original file line number Diff line number Diff line change
@@ -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';
8 changes: 7 additions & 1 deletion update.sh
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ user=$(ls -l "$www/include/config.php" | awk '{ print $3 }')
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/include/"{html,connect.php,functions.php,grids.php} "$www/include"
Expand All @@ -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"
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."

0 comments on commit ab7f85b

Please sign in to comment.