diff --git a/.gitignore b/.gitignore index a88b536..b7d6fa6 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,2 @@ +.phpintel vendor/ -*.komodoproject diff --git a/include/connect.php b/include/connect.php index 9808810..0add7e4 100644 --- a/include/connect.php +++ b/include/connect.php @@ -1,5 +1,5 @@ prepare($req_string); + $req->execute(array($newKey)); + } + function printError($str) { echo ''; } diff --git a/include/grids.php b/include/grids.php index 9a7bb46..2af635d 100644 --- a/include/grids.php +++ b/include/grids.php @@ -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 (?, ?, ?, ?, ?, ?, ?, ?)'); @@ -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 = ?'; diff --git a/include/html/menu.php b/include/html/menu.php index e95a7c9..295d56b 100644 --- a/include/html/menu.php +++ b/include/html/menu.php @@ -1,12 +1,12 @@
-
\ No newline at end of file + diff --git a/index.php b/index.php index 569a8dc..c3744dd 100644 --- a/index.php +++ b/index.php @@ -8,7 +8,7 @@ if(isset($_GET['logout'])){ session_destroy(); header("Location: ."); - exit -1; + exit(-1); } // Get the configuration files ? @@ -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; @@ -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 @@ -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 @@ -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" ); @@ -154,7 +161,7 @@ require(dirname(__FILE__) . '/include/html/form/installation.php'); } - exit -1; + exit(-1); } // --------------- CONFIGURATION --------------- diff --git a/install.sh b/install.sh index ce00b4b..59a1637 100755 --- a/install.sh +++ b/install.sh @@ -31,6 +31,7 @@ done www=$1 user=$2 group=$3 + openvpn_admin="$www/openvpn-admin" # Check the validity of the arguments @@ -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" diff --git a/installation/scripts/connect.sh b/installation/scripts/connect.sh index 0d481ae..ace884b 100644 --- a/installation/scripts/connect.sh +++ b/installation/scripts/connect.sh @@ -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'" diff --git a/installation/scripts/disconnect.sh b/installation/scripts/disconnect.sh index 300d446..bfa07fd 100644 --- a/installation/scripts/disconnect.sh +++ b/installation/scripts/disconnect.sh @@ -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" diff --git a/installation/scripts/login.sh b/installation/scripts/login.sh index afc794c..baa18c4 100644 --- a/installation/scripts/login.sh +++ b/installation/scripts/login.sh @@ -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 diff --git a/migration.php b/migration.php new file mode 100644 index 0000000..2ddf2f9 --- /dev/null +++ b/migration.php @@ -0,0 +1,55 @@ +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"; + } + } + +?> diff --git a/sql/import.sql b/sql/schema-0.sql similarity index 92% rename from sql/import.sql rename to sql/schema-0.sql index 50d8be1..d64aae5 100644 --- a/sql/import.sql +++ b/sql/schema-0.sql @@ -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, diff --git a/sql/schema-5.sql b/sql/schema-5.sql new file mode 100644 index 0000000..a67cf2c --- /dev/null +++ b/sql/schema-5.sql @@ -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'; diff --git a/update.sh b/update.sh index 09a3629..0d147e9 100755 --- a/update.sh +++ b/update.sh @@ -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" @@ -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."