From b8913a2b52554c98e67785e40bb954303bafd77d Mon Sep 17 00:00:00 2001 From: Horus3 Date: Sun, 23 Feb 2014 14:23:40 +0100 Subject: index --- blob/nginx_rewrite_rules | 8 +- www/functions.php | 242 +++++++++++++++++++++++++++++++++++++++++++++++ www/index.php | 98 +++++++++++-------- 3 files changed, 303 insertions(+), 45 deletions(-) create mode 100644 www/functions.php diff --git a/blob/nginx_rewrite_rules b/blob/nginx_rewrite_rules index 376a10f..10c6edd 100644 --- a/blob/nginx_rewrite_rules +++ b/blob/nginx_rewrite_rules @@ -1,18 +1,18 @@ location ~* ^/?login(\?[0-9a-zA-Z]*(=[0-9a-zA-Z]*)?)? { - rewrite ^/?login(\?[0-9a-zA-Z]*(=[0-9a-zA-Z]*)?)? /login.php?$1 last; + rewrite ^/?login(\?[0-9a-zA-Z]*(=[0-9a-zA-Z]*)?)? /index.php?task=login&arguments=$1 last; } location ~* ^/?logout([?/]?.*) { - rewrite ^/?logout([?/]?.*) /login.php?logout last; + rewrite ^/?logout([?/]?.*) /index.php?task=logout&arguments=$1 last; } location ~* ^/?register(\?[0-9a-zA-Z]*(=[0-9a-zA-Z]*)?)? { - rewrite ^/?register(\?[0-9a-zA-Z]*(=[0-9a-zA-Z]*)?)? /register.php?$1 last; + rewrite ^/?register(\?[0-9a-zA-Z]*(=[0-9a-zA-Z]*)?)? /index.php?task=register&arguments=$1 last; } location ~* ^/?invite(\?[0-9a-zA-Z]*(=[0-9a-zA-Z]*)?)? { - rewrite ^/?invite(\?[0-9a-zA-Z]*(=[0-9a-zA-Z]*)?)? /invite.php?$1 last; + rewrite ^/?invite(\?[0-9a-zA-Z]*(=[0-9a-zA-Z]*)?)? /index.php?task=invite&arguments=$1 last; } location ~* \.php(\?[0-9a-zA-Z]*(=[0-9a-zA-Z]*)?)? {} #empty block to catch all diff --git a/www/functions.php b/www/functions.php new file mode 100644 index 0000000..56f11ad --- /dev/null +++ b/www/functions.php @@ -0,0 +1,242 @@ + + +/* --LOGIN-- */ + +function login(){ + if($_SERVER['REQUEST_METHOD'] == 'POST') { + + /*___Database Query: Login___*/ + $unsafe_username = $_POST["username"]; + $unsafe_passwort = $_POST["password"]; + $username = SQLite3::escapeString("$unsafe_username"); + $passwort = SQLite3::escapeString("$unsafe_passwort"); + + $db = new SQLite3("../database/sqlite.db"); + $salt_db = $db->query("SELECT salt FROM user WHERE name='$username';"); + while($salt_array = $salt_db->fetchArray(SQLITE3_NUM)){ + foreach($salt_array as $firstelement){ + $salt=$firstelement; + } + } + + $password = "$salt"."$passwort"; + $hash_password = md5($password); + for($i=0;$i<15000;$i++) + $hash_password = md5($hash_password); + + $real_password_db = $db->query("SELECT password FROM user WHERE name='$username';"); + while($real_password_array = $real_password_db->fetchArray(SQLITE3_NUM)){ + foreach($real_password_array as $secondelement){ + $real_password=$secondelement; + } + } + + /*___Login___*/ + if ($real_password == $hash_password) { + + if($db->exec(" + BEGIN TRANSACTION; + INSERT INTO log (id, user, login) VALUES (NULL, (SELECT id FROM user WHERE name='$username'), (SELECT datetime()) ); + COMMIT; + ")){ + + $_SESSION["login"] = true; + $_SESSION["username"] = "$unsafe_username"; + + header("Refresh: 0; /"); + + } else { + header("Refresh: 0; login?reason=database"); + } + } else { + header("Refresh: 0; login?reason=failure"); + } + } else { + + /*Prints the GET version*/ + + if($_SESSION["login"]){ + header("Refresh: 0; /"); + } else { + echo $logout; + + echo "
"; + } + } +} + +/* --LOGOUT-- */ + +function logout(){ + session_destroy(); + header("Refresh: 0; /login?success"); + exit; +} + +/* --INVITE-- */ + +function invite(){ + if($_SERVER['REQUEST_METHOD'] == 'POST') { + $name=$_SESSION["username"]; + $safe_name = SQLite3::escapeString("$name"); + + $email=$_POST["email"]; + $safe_email=SQLite3::escapeString("$email"); + + $db = new SQLite3("../database/sqlite.db"); + + $invite_db = $db->query("SELECT invites FROM user WHERE name='$safe_name';"); + $invite_ar = $invite_db->fetchArray(SQLITE3_NUM); + $invite = $invite_ar[0]; + + if($invite > 0){ + + /*Generates the invite key => [-_0-9a-zA-Z]{11}*/ + + $key_array = array("a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "_", "-", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" ); + + $length = count($key_array); + $key = ""; + + for ($i=0;$i<11;$i++){ + $index = mt_rand(0,$length-1); + $key = "$key".$key_array[$index]; + } + + $id_db = $db->query("SELECT id FROM USER WHERE name='$safe_name';"); + $id_ar = $id_db->fetchArray(SQLITE3_NUM); + $id = $id_ar[0]; + + /*Generates the new user and decrease the invites*/ + + $invite = $invite - 1; + + echo "ID: '$id', KEY: '$key', SAFE_EMAIL: '$safe_email'"; + + if($db->exec(" + BEGIN TRANSACTION; + INSERT INTO user (id, name, email, senpai, key, status) VALUES (NULL, NULL, '$safe_email', '$id', '$key', 0); + UPDATE user SET invites='$invite' WHERE id='$id'; + COMMIT;") + ){ + header("Refresh: 0; /invite?reason=success"); + } else { + header("Refresh: 0; /invite?reason=database"); + } + + } else { + header("Refresh: 0; /invite?reason=invites"); + } + } else { + + /*Prints the formular if requested with GET*/ + + foreach ($_GET as $name => $value) { + echo 'Name: ' . $name . ' Value: ' . $value . '