public function test() { $username = "******"; $password = "******"; $email = "*****@*****.**"; $username2 = "bla2"; $password2 = "pass2"; $email2 = "*****@*****.**"; $username3 = "gue"; $password3 = "pass3"; $email3 = "*****@*****.**"; user::create_new_user($username, $password, $email); $this->assertEquals(1, user::getNumberOfUsers(), "number of users is not correct after adding a new user"); user::create_new_user($username2, $password2, $email2); $this->assertEquals(2, user::getNumberOfUsers(), "number of users is not correct after adding a new user"); user::create_new_user($username3, $password3, $email3); $this->assertEquals(3, user::getNumberOfUsers(), "number of users is not correct after adding a new user"); $user1ID = user::getUserByUsername($username)['id']; $user2ID = user::getUserByUsername($username2)['id']; $user3ID = user::getUserByUsername($username3)['id']; $uuid1 = "352584060201362"; $this->assertTrue(safe_input::is_valid_uuid($uuid1), "safe_input::is_valid_uuid()"); $ip1 = '196.168.2.16'; $this->assertTrue(safe_input::is_valid_ip($ip1), "safe_input::is_valid_ip()"); $this->assertEquals(0, log::get_logs_count(), "[get_logs_count()]"); $this->assertTrue(log::addNewLog($user1ID, $ip1, $uuid1), "[log::addNewLog()]"); $this->assertEquals(1, log::get_logs_count(), "[get_logs_count()]"); $this->assertTrue(log::addNewLog($user1ID, $ip1, $uuid1), "[log::addNewLog()]"); $this->assertEquals(2, log::get_logs_count(), "[get_logs_count()]"); //$this->assertTrue(log::deleteSimilarLogs(),"[deleteSimilarLogs()]") ; //$this->assertEquals(1,log::get_logs_count(),"[get_logs_count()]") ; }
public static function newPendingGame($session, $size) { $session_info = session::get_session_by_hash($session); if ($session_info != null && safe_input::is_number($size) && $size > 1 && $size < 21) { $add_result = pending_game::add_new_pending_game($session_info['userID'], $size); GameControl::matchPendingGames(); return $add_result; } else { Report::warning(__METHOD__ . "," . __LINE__, "trying to create a pending game with an invalid size:" . $size); return false; } }
public static function addNewLog($user_id, $ip, $uuid) { if (safe_input::is_number($user_id) && safe_input::is_valid_ip($ip) && safe_input::is_valid_uuid($uuid)) { $date = microtime(true); $query = "INSERT INTO `log` (`userID`, `date`, `uuid`, `ipAddress`) VALUES ( '{$user_id}', '{$date}', '{$uuid}', '{$ip}');"; $db = new database(); $res = $db->query($query); log::$last_inserted_id = $db->insert_id(); return $res; } else { return false; //invalid input } }
public function test() { $username = "******"; $password = "******"; $email = "*****@*****.**"; user::create_new_user($username, $password, $email); $this->assertEquals(1, user::getNumberOfUsers(), "number of users is not correct after adding a new user"); $r = user::getUserByUsername($username); $this->assertEquals($username, $r['username'], "username was not saved correctly"); $this->assertEquals($email, $r['email'], "email is not saved correctly"); $this->assertEquals(md5(md5($password) . md5($r['salt'])), $r['password'], "password not correct"); user::clear_table(); $this->assertEquals(0, user::getNumberOfUsers()); $this->assertFalse(user::doesUsernameExist($username), "doesUsernameExist() in user is broken"); user::create_new_user($username, $password, $email); $this->assertTrue(user::doesUsernameExist($username), "doesUsernameExist() in user is broken"); $this->assertFalse(user::doesEmailExist($email . "bla"), "doesEmailExist() in user is broken"); $this->assertTrue(user::doesEmailExist($email), "doesEmailExist() in user is broken"); $newPassword = "******"; user::resetPassword($username, $newPassword); $r = user::getUserByUsername($username); $this->assertEquals(md5(md5($newPassword) . md5($r['salt'])), $r['password'], "restting password is broken"); $newEmail = "*****@*****.**"; user::setEmail($username, $newEmail); $r = user::getUserByUsername($username); $this->assertEquals($newEmail, $r['email'], "setEmail is broken"); user::deleteUserByUsername($username); $this->assertFalse(user::doesUsernameExist($username), "deleteUserByUsername() in user is broken"); user::create_new_user($username, $password, $email); $this->assertTrue(user::isLogin($username, $password), "isLogin is broken"); $this->assertFalse(user::isLogin($username, $password . "d"), "isLogin is broken"); $this->assertFalse(user::isLogin($username . "d", $password), "isLogin is broken"); $user_info = user::getUserByUsername($username); $user_info2 = user::getUserById($user_info['id']); $this->assertEquals($user_info['username'], $user_info2['username'], "mismatched usernames when getting user by ID"); $gcm_id1 = "APA91bFpUo1z8PfiyCZG7HzThDyJ0MIg86BB1kj0A-ZGASK_iJ-RTu8pUB4t_5jMgwqkolWCahT4QOOAnp9nNdCox7pd9vlJao1-ncYHqvlS89lOpjdoci2_3XXGxcIWgrWwTz1tC8OlURokekQdbDCGKWuqfzfXLKrhisGxJYpF1ivuItZtJns"; $this->assertTrue(safe_input::is_valid_gcm_id($gcm_id1), "[is_valid_gcm_id()]"); //user::setGCM($user_id,$gcm_id) $this->assertTrue(user::setGCM($user_info['id'], $gcm_id1), "[user::setGCM()]"); $user_info = user::getUserByUsername($username); $this->assertEquals($gcm_id1, $user_info['gcmID'], "setGCM()"); $gcm_id2 = "APA91bHGJbxPpIUNirvnCQib7kojM12Qu2MBBd9dGHXSu0hsfB_Al2rQ4E8UWgpMXhNVIGT6IlSjLE-MB2F0RrBeN_llEYzPErIQoewxnDeON6uqBIHkLcMIY2NQtQHX3TNYBrlNc74wmh7aYec9kLMp5QGogVYSao1Q-RtIx4QV140YHBBASXM"; $this->assertTrue(user::setGCM($user_info2['id'], $gcm_id2), "[user::setGCM()]"); $user_info2 = user::getUserById($user_info['id']); $this->assertEquals($gcm_id2, $user_info2['gcmID'], "setGCM()"); }
public function test() { $username = "******"; $password = "******"; $email = "*****@*****.**"; $username2 = "bla2"; $password2 = "pass2"; $email2 = "*****@*****.**"; $username3 = "gue"; $password3 = "pass3"; $email3 = "*****@*****.**"; user::create_new_user($username, $password, $email); $this->assertEquals(1, user::getNumberOfUsers(), "number of users is not correct after adding a new user"); user::create_new_user($username2, $password2, $email2); $this->assertEquals(2, user::getNumberOfUsers(), "number of users is not correct after adding a new user"); user::create_new_user($username3, $password3, $email3); $this->assertEquals(3, user::getNumberOfUsers(), "number of users is not correct after adding a new user"); $user1ID = user::getUserByUsername($username)['id']; $user2ID = user::getUserByUsername($username2)['id']; $user3ID = user::getUserByUsername($username3)['id']; $this->assertTrue(safe_input::is_valid_session_hash(md5("\$3dfsd43^^%")), "safe_input::is_valid_session_hash()"); $this->assertFalse(safe_input::is_valid_session_hash("x = 2 "), "safe_input::is_valid_session_hash()"); //get_last_session_for_user_id($user_id) //add_new_session($user_id,$hash,$encryption_key) $this->assertFalse(session::add_new_session("hh", md5("bla"), md5("bla2")), "[add_new_session()]"); $this->assertTrue(session::add_new_session($user1ID, md5("bla"), md5("bla2")), "failed to add new session[add_new_session()]"); $s1 = session::$last_inserted_id; $this->assertFalse(session::is_unique_hash(md5("bla")), "[session::is_unique_hash()]"); $this->assertTrue(session::is_unique_hash(md5("bddla")), "[session::is_unique_hash()]"); //$this->assertNull(session::add_new_session($user2ID,md5("bla"),md5("mmm")),"Hash has to be unique! [add_new_session()]") ; $this->assertTrue(session::add_new_session($user2ID, md5("hash2"), md5("bla2")), "failed to add new session[add_new_session()]"); $this->assertTrue(session::add_new_session($user1ID, md5("hash2sss"), md5("blssssa2")), "failed to add new session[add_new_session()]"); $s2 = session::$last_inserted_id; $s_info = session::get_last_session_for_user_id($user1ID); $this->assertEquals($s_info['encryptionKey'], md5("blssssa2"), "[get_last_session_for_user_id()]"); $this->assertFalse(session::is_unique_hash($s_info['hash']), "[is_unique_hash()]"); $this->assertTrue(session::is_unique_hash(md5("asdfasefds")), "[is_unique_hash()]"); $this->assertTrue(session::does_user_have_session($user2ID), "[does_user_have_session()]"); $this->assertFalse(session::does_user_have_session($user3ID), "[does_user_have_session()]"); //delete_all_sessions_for_user_id($user_id) $this->assertFalse(session::delete_all_sessions_for_user_id("sadsadsad"), "[delete_all_sessions_for_user_id()]"); $this->assertTrue(session::delete_all_sessions_for_user_id($user2ID), "[delete_all_sessions_for_user_id()]"); $this->assertFalse(session::does_user_have_session($user2ID), "[does_user_have_session()]"); $this->assertTrue(session::does_user_have_session($user1ID), "[does_user_have_session()]"); //session::delete_session_by_id($session_id) $s_info = session::get_last_session_for_user_id($user1ID); $this->assertTrue(session::add_new_session($user2ID, md5("hash2"), md5("bla2")), "failed to add new session[add_new_session()]"); $s2_info = session::get_last_session_for_user_id($user2ID); $this->assertTrue(session::delete_session_by_id($s2_info['id']), "[delete_session_by_id()]"); $this->assertFalse(session::does_user_have_session($user2ID), "[does_user_have_session()]"); $this->assertTrue(session::does_user_have_session($user1ID), "[does_user_have_session()]"); //session::delete_session_by_hash($hash) $s_info = session::get_last_session_for_user_id($user1ID); $this->assertTrue(session::add_new_session($user2ID, md5("hash2"), md5("bla2")), "failed to add new session[add_new_session()]"); $s2_info = session::get_last_session_for_user_id($user2ID); $this->assertTrue(session::delete_session_by_hash($s2_info['hash']), "[delete_session_by_id()]"); $this->assertFalse(session::does_user_have_session($user2ID), "[does_user_have_session()]"); $this->assertTrue(session::does_user_have_session($user1ID), "[does_user_have_session()]"); //session::get_session_by_hash($hash) $s_infos = session::get_session_by_hash($s_info['hash']); $this->assertEquals($s_info['id'], $s_infos['id'], "session::get_session_by_hash()"); }
public static function delete_move($move_id) { if (safe_input::is_number($move_id)) { $query = "DELETE FROM `move` WHERE `id` = '{$move_id}' ;"; $db = new database(); return $db->query($query); } else { return false; } }
public function startNewGame() { if (XmlRequestValidator::isValidStartPendingGameRequest($this->requestData)) { $session = $this->requestData->body->session; $size = $this->requestData->body->size; if (safe_input::is_valid_session_hash($session) && safe_input::is_number($size) && $size > 1) { //chkec if the session hash exists $session_info = session::get_session_by_hash($session); if ($session_info != null) { $res = Execute::newPendingGame($session, $size); if ($res) { $this->response = XmlBuilder::startNewPendingGameSuccessfullResponse("plain", $session); } else { //faild to add new game Report::error(__METHOD__ . "," . __LINE__, "failed to add new pending game"); $this->response = XmlBuilder::failed_response("plain", 5, 0, "failed to add new pending game, try again"); } } else { //the given hash doesn't exist in the database Report::warning(__METHOD__ . "," . __LINE__, "start new pending game request contains a session hash that does not exist in the database: hash=" . $session); $this->response = XmlBuilder::failed_response("plain", 5, 1, "expired session"); } } else { //invalid data passed Report::error(__METHOD__ . "," . __LINE__, "start new pending game request contains an incorrectly formatted session hash or game size, size:" . $size); $this->response = XmlBuilder::failed_response("plain", 5, 0, "invalid session or gcm id"); } } else { //xml request was not formatted correctly Report::error(__METHOD__ . "," . __LINE__, "invalid new pending game request!"); $this->invalidRequest(); } }
public static function match($game_id1, $game_id2) { $game1 = pending_game::get_pending_game_by_id($game_id1); $game2 = pending_game::get_pending_game_by_id($game_id2); if (safe_input::is_number($game_id1) && safe_input::is_number($game_id2) && $game1 != null && $game2 != null && $game1['size'] == $game2['size']) { $date = time(); $player1_id = $game1['userID']; $player2_id = $game2['userID']; if ($player1_id == $player2_id) { return false; } $size = $game1['size']; $g1_id = $game1['id']; $g2_id = $game2['id']; $db = new database(); $query[] = "INSERT INTO `game` (`winnerID`, `createDate`, `currentTurnPlayerID`, `size`, `lastActivityDate`, `player1ID` , `player2ID`) VALUES ( null, '{$date}', '{$player1_id}', '{$size}', null , '{$player1_id}', '{$player2_id}');"; $query[] = "DELETE FROM `pending_game` WHERE `id` = '{$g1_id}' or `id` = '{$g2_id}'"; $res = $db->execute_transaction($query); return $res; } else { return false; } }
public static function delete_message($message_id) { if (safe_input::is_number($message_id)) { $query = "DELETE FROM `message` WHERE `id` = '{$message_id}' ;"; $db = new database(); $res = $db->query($query); return $res; } else { return false; //invalid $message_id } }
public static function get_session_by_hash($hash) { if (safe_input::is_valid_session_hash($hash)) { $safe_hash = safe_input::sql_inj($hash); $query = "SELECT * FROM `session` WHERE `hash` = '{$safe_hash}'"; $db = new database(); $db->query($query); if ($db->number_of_rows() > 0) { return $db->fetch_row(); } else { return null; } } else { return null; //invalid hash } }
public static function all($value) { $v = safe_input::xss($value); $vv = safe_input::sql_inj($v); return $vv; }
public static function getOpponentId($game_id, $player_id) { if (safe_input::is_number($game_id) && safe_input::is_number($player_id)) { $game = game::getGameById($game_id); if ($game == false) { //no game with that id return null; } else { if ($game['player1ID'] == $player_id) { return $game['player2ID']; } elseif ($game['player2ID'] == $player_id) { return $game['player1ID']; } else { return null; //player id is not a player in the game with the id $game_id } } } else { return null; //ERROR: either the game id or player id or both are not numbers! (invalid) } }
public static function setGCM($user_id, $gcm_id) { if (safe_input::is_number($user_id) && safe_input::is_valid_gcm_id($gcm_id)) { $db = new database(); $query = "UPDATE `user` SET `gcmID` = '{$gcm_id}' WHERE `id` = '{$user_id}'"; return $db->query($query); } else { return false; } }