connect(); } function connect() { require(__DIR__.'/../../sharedConfig/databaseConf.inc.php'); $temp = mysql_connect($db_host, $db_user, $db_password); if(!$temp) echo 'nepodarilo se navazat spojeni s DB'; $temp2 = mysql_select_db($db_name); if(!$temp2) echo 'nepodarilo pripojit do dane DB'; } function rawQuery($query) { $results = mysql_query($query); if(!$results) { echo "\nNeplatny prikaz do DB: ".$query; return; } $array = array(); while ($row = mysql_fetch_array($results, MYSQL_ASSOC)) { //var_dump($row); array_push($array, $row); } return $array; } function rawUpdate($update) { mysql_query($update); } function close() { mysql_close(); } } ?> rawQuery('SELECT id, username, pwd_hash,auth_level FROM users WHERE username = \''.mysql_real_escape_string($username).'\';'); if(sizeof($results) == 0) { $connector->close(); return AUTH_LOGIN_NO_EXIST_NAME; } if($username == "guest") { } else { if($results[0]['pwd_hash'] != $password) { $connector->close(); return AUTH_LOGIN_WRONG_PWD; } } $_SESSION['id'] = $results[0]['id']; $_SESSION['auth_level'] = $results[0]['auth_level']; $connector->rawUpdate('INSERT INTO logins (user_id, last_ip) VALUES ('.intval($_SESSION['id']).', "'.$_SERVER['REMOTE_ADDR'].'")'); $connector->close(); return AUTH_OK; } function pwd_change($info) { $new_pwd = $info['password']; $old_pwd = $info['old_password']; if(!$this->is_loged()) return; $connector = new DBConnector(); $results = $connector->rawQuery('SELECT pwd_hash FROM users WHERE id ='.intval($_SESSION['id'])); if($results[0]['pwd_hash'] != md5($old_pwd)) { $connector->close(); return AUTH_PWD_CHANGE_WRONG_PWD; } $connector->rawUpdate('UPDATE users SET pwd_hash = "'.md5(mysql_real_escape_string($new_pwd)).'" WHERE id = '.intval($_SESSION['id'])); return AUTH_OK; } function pwd_reset_finish($info) { $new_pwd = $info['password']; $hash = $info['hash']; $connector = new DBConnector(); $results = $connector->rawQuery("select * from resets where uniq_hash = '".mysql_real_escape_string($hash)."' AND time > NOW() - INTERVAL 1 HOUR;"); if(count($results) == 0) return AUTH_PWD_RESET_INVALID_PAGE; $user_id = $results[0]['user_id']; $connector->rawUpdate('UPDATE users SET pwd_hash = "'.md5(mysql_real_escape_string($new_pwd)).'" WHERE id = '.intval($user_id)); $connector->rawUpdate('DELETE FROM resets WHERE user_id = '.intval($user_id)); return AUTH_OK; } function pwd_reset($info) { $name = $info['username']; $email = $info['email']; $connector = new DBConnector(); $result = $connector->rawQuery("SELECT id, email FROM users WHERE username = '".mysql_real_escape_string($name)."' AND email = '".mysql_real_escape_string($email)."'"); if(!count($result)) { $connector->close(); return AUTH_OK; } $result = $result[0]; $hash_id = md5($this->generateRandomString(10)).md5($result['id']); $connector->rawUpdate("INSERT INTO resets(uniq_hash, user_id) VALUES ('".$hash_id."','".$result['id']."') ON DUPLICATE KEY UPDATE uniq_hash = '".$hash_id."', time = NOW()"); mail( $email, "Password reset", "If you want reset password go to: ".WEB_SERVER_URL."/?page=pwdreset_finish&hash=".$hash_id ); return AUTH_OK; } function generateRandomString($length) { $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; $randomString = ''; for ($i = 0; $i < $length; $i++) { $randomString .= $characters[rand(0, strlen($characters) - 1)]; } return $randomString; } function register($info, $hash) { $name = $info['username']; $password = $info['password']; if(!isset($hash) || $hash != 1) $password = md5($password); $auth_level = $info['auth_level']; $email = $info['email']; $connector = new DBConnector(); $result = $connector->rawQuery('SELECT COUNT(*) as count FROM users WHERE username = \''.mysql_real_escape_string($name).'\'' ); if(isset($result) && sizeof($result) == 0) { $connector->close(); return AUTH_DB_ERROR; } $count =$result[0]['count']; if($count > 0) { $connector->close(); return AUTH_REGISTER_USER_EXISTS; } $result = $connector->rawQuery('SELECT COUNT(*) as count FROM users WHERE email = \''.mysql_real_escape_string($email).'\'' ); if(isset($result) && sizeof($result) == 0) { $connector->close(); return AUTH_DB_ERROR; } $count =$result[0]['count']; if($count > 0) { $connector->close(); return AUTH_REGISTER_EMAIL_EXISTS; } $connector->rawUpdate('INSERT INTO users (username, pwd_hash, auth_level, email) VALUES (\''.mysql_real_escape_string($name).'\', \''.mysql_real_escape_string($password).'\', 1, "'.mysql_real_escape_string($email).'")'); $result = $connector->rawQuery('SELECT LAST_INSERT_ID() as lastId' ); if(isset($result) && sizeof($result) > 0) { return AUTH_OK; } return AUTH_DB_ERROR; } function editUser($info) { $connector = new DBConnector(); $password = md5($info['password']); $id = $info['id']; if($password == '***') { } else { $connector->rawUpdate("UPDATE users SET pwd_hash = '".$password."' WHERE id = ".$id); } $connector->close(); return AUTH_OK; } function get_id() { return $_SESSION['id']; } function is_loged() { return ($this->get_auth_level() > 0) ? 1 : 0; } FUNCTION get_auth_level() { if(isset($_SESSION['auth_level'])) return $_SESSION['auth_level']; return 0; } function log_out() { session_destroy(); $_SESSION['auth_level'] = 0; $_SESSION['id'] = 0; } function try_log_in() { if(isset($_POST['sign_in'])) { $username = $_POST['username']; $password = $_POST['password']; $result = $this->log_in($username, $password, 0); $this->auth_result['log_in'] = $result; } } function try_log_out() { if(isset($_GET['action']) && $_GET['action'] = "sign_out") { $auth = new Authorizer(); $result = $this->log_out(); $this->auth_result['log_out'] = $result; header("HTTP/1.1 301 Moved Permanently"); header("Location: http://karel1981.com/"); header("Connection: close"); } } function try_register() { if(isset($_POST['register'])) { if($_POST['username'] == '') { $this->auth_result['register'] = AUTH_REGISTER_NO_USERNAME; return; } if($_POST['password'] != $_POST['password_2']) { $this->auth_result['register'] = AUTH_REGISTER_DIFFERENT_PWDS; return; } if($_POST['password'] == '') { $this->auth_result['register'] = AUTH_REGISTER_NO_PWD; return; } if($_POST['email'] == '') { $this->auth_result['register'] = AUTH_REGISTER_EMAIL_EXISTS; return;} $info['username'] = $_POST['username']; $info['password'] = $_POST['password']; $info['auth_level'] = 1; $info['email'] = $_POST['email']; if(isset($_POST['id']) && $_POST['id'] != '') { $info['id'] = $_POST['id']; $this->editUser($info); $this->auth_result['register'] = $result; } else { $result = $this->register($info, 0); $this->auth_result['register'] = $result; } } } function try_pwd_change() { if(isset($_POST['pwd_change'])) { if($_POST['password'] != $_POST['password_2']) { $this->auth_result['pwd_change'] = AUTH_PWD_CHANGE_DIFFERENT_PWDS; return;} if($_POST['password'] == '') { $this->auth_result['pwd_change'] = AUTH_PWD_CHANGE_INCOMPLETE; return; } if($_POST['old_password'] == '') { $this->auth_result['pwd_change'] = AUTH_PWD_CHANGE_INCOMPLETE; return; } $info['password'] = $_POST['password']; $info['old_password'] = $_POST['old_password']; $result = $this->pwd_change($info); $this->auth_result['pwd_change'] = $result; } } function try_pwd_reset_finish() { if(isset($_POST['pwd_reset_finish'])) { if($_POST['password'] != $_POST['password_2']) { $this->auth_result['pwd_change']; return AUTH_PWD_CHANGE_DIFFERENT_PWDS; } if($_POST['password'] == '') { $this->auth_result['pwd_change'] = AUTH_PWD_CHANGE_INCOMPLETE; return; } $info['hash'] = $_GET['hash']; $info['password'] = $_POST['password']; $result = $this->pwd_reset_finish($info); $this->auth_result['pwd_reset_finish'] = $result; } } function try_pwd_reset() { if(isset($_POST['pwd_reset'])) { if($_POST['username'] == '') { return; } if($_POST['email'] == '') { return;} $info['username'] = $_POST['username']; $info['email'] = $_POST['email']; $result = $this->pwd_reset($info); $this->auth_result['pwd_reset'] = $result; } } } $auth = new Authorizer(); $auth->try_log_in(); $auth->try_log_out(); $auth->try_register(); $auth->try_pwd_reset(); $auth->try_pwd_reset_finish(); $auth->try_pwd_change(); $result_array = $auth->auth_result; ?> auth = $auth; } function check_posts() { if(!isset($_GET['admin_action'])) return; if($this->auth->get_auth_level() < 2) return; $action = $_GET['admin_action']; if($action == 'ban') { if(!isset($_GET['id'])) return; $id = $_GET['id']; $this->ban($id); } else if($action == 'unban') { if(!isset($_GET['id'])) return; $id = $_GET['id']; $this->unban($id); } else if($action == 'delete') { if(!isset($_GET['id'])) return; $id = $_GET['id']; $this->delete($id); } } function delete($id) { $connector = new DBConnector(); $connector->rawUpdate("DELETE FROM users WHERE id = ".$id); $connector->close(); } function ban($id) { $connector = new DBConnector(); $connector->rawUpdate("UPDATE users SET ban = 1 WHERE id = ".$id); $connector->close(); } function unban($id) { $connector = new DBConnector(); $connector->rawUpdate("UPDATE users SET ban = 0 WHERE id = ".$id); $connector->close(); } } $admin = new AdminTools($auth); $admin->check_posts(); ?> Robot Karel