nmps-server

server-side nmps
git clone git://git.kocotian.pl/nmps-server.git
Log | Files | Refs | README

commit 7573d3b51beb1b55b141aac04781f5e8295badc9
parent 1d7ef010b1d046a2fe965009e59147ba4d7161c1
Author: kocotian <kocotian@kocotian.pl>
Date:   Mon,  7 Dec 2020 16:04:50 +0100

better events

Diffstat:
Mauth.php | 25++++++++++++++++---------
MeventSender.php | 2+-
Mexit.php | 5+++++
Mkick.php | 2+-
Ame.php | 12++++++++++++
Mpay.php | 29++++++++++++++++-------------
Mrequire/nmps.php | 23++++++++++++++++++++++-
Asay.php | 12++++++++++++
8 files changed, 85 insertions(+), 25 deletions(-)

diff --git a/auth.php b/auth.php @@ -2,19 +2,26 @@ $unauthorized = 1; require_once "require/nmps.php"; + if (strlen($authToken) != 6) + die("\033[1;31mYou are already logged in"); $user = db::query("SELECT * FROM users WHERE username=:username", ['username' => $argv[0]])[0]; if ($user) if (password_verify($argv[1], $user['password'])) { - if (!count(db::query("SELECT * FROM authTokens WHERE userId=:userId", [':userId' => $user['id']]))) { - $cstrong = true; - $token = bin2hex(openssl_random_pseudo_bytes(64, $cstrong)); - db::query("INSERT INTO authTokens (userId, token, addressBefore, addressAfter) VALUES (:userId, :token, :addressBefore, :addressAfter)", [':userId' => $user['id'], ':token' => sha1($token), ':addressBefore' => $user['lastAddress'], ':addressAfter' => $_SERVER['REMOTE_ADDR']]); - db::query("UPDATE users SET lastAddress=:ip WHERE id=:userId", [':ip' => $_SERVER['REMOTE_ADDR'], ':userId' => $user['id']]); - echo "Authorized\n" . $token; + if (count(db::query("SELECT * FROM authTokens WHERE userId=:userId", [':userId' => $user['id']]))) { + db::query("DELETE FROM authTokens WHERE userId=:userId", [':userId' => $user['id']]); } else - echo "You can log in on one account in the same time only"; + $cstrong = true; + $token = bin2hex(openssl_random_pseudo_bytes(64, $cstrong)); + db::query("INSERT INTO authTokens (userId, token, addressBefore, addressAfter) VALUES (:userId, :token, :addressBefore, :addressAfter)", [':userId' => $user['id'], ':token' => sha1($token), ':addressBefore' => $user['lastAddress'], ':addressAfter' => $_SERVER['REMOTE_ADDR']]); + db::query("UPDATE users SET lastAddress=:ip WHERE id=:userId", [':ip' => $_SERVER['REMOTE_ADDR'], ':userId' => $user['id']]); + foreach (userAccount::getOnline() as $onlineUser) { + if ($onlineUser['userId'] != $user['id']) + nmps::sendEvent($onlineUser['userId'], $user['id'], 2, + "\033[0;37m" . $user['username'] . " \033[0;32mjoined."); + } + echo "Authorized\n" . $token; } else - echo "Wrong password"; + die("Wrong password"); else - echo "Unexisting account"; + die("Unexisting account"); diff --git a/eventSender.php b/eventSender.php @@ -9,7 +9,7 @@ while (!count($events = db::query("SELECT * FROM events WHERE userId=:userId", } foreach ($events as $event) { - echo ($event['eventType'] == 126 ? "\004\001" : "") . $event['eventData']; + echo ($event['eventType'] == 126 ? "\004\001" : "") . $event['eventData'] . "\n"; db::query("DELETE FROM events WHERE id=:id", [':id' => $event['id']]); } diff --git a/exit.php b/exit.php @@ -1,5 +1,10 @@ <?php require_once "require/nmps.php"; + foreach (userAccount::getOnline() as $onlineUser) { + if ($onlineUser['userId'] != $userinfo['id']) + nmps::sendEvent($onlineUser['userId'], $userinfo['id'], 3, + "\033[0;37m" . $userinfo['username'] . " \033[0;31munjoined."); + } db::query("DELETE FROM authTokens WHERE userId=:userId", [':userId' => $userinfo['id']]); echo "\004\001"; diff --git a/kick.php b/kick.php @@ -17,6 +17,6 @@ db::query("DELETE FROM authTokens WHERE userId=:userId", db::query("INSERT INTO events (userId, senderId, eventType, eventData) VALUES (:userId, :senderId, :eventType, :eventData)", [':userId' => $target, ':senderId' => $userinfo['id'], - ':eventType' => 126, ':eventData' => "\033[1;33m[\033[1;97m" . date("H:i:s") . "\033[1;33m] \033[0;37mYou were kicked by \033[1;97m" . $userinfo['username'] . " \033[0;37mfor \033[1;31m" . (count($argv) ? implode(' ', $argv) : "no reason")]); + ':eventType' => 126, ':eventData' => "\033[1;33m[\033[1;97m" . date("H:i:s") . "\033[1;33m] \033[0;37mYou were kicked by \033[1;97m" . $userinfo['username'] . " \033[0;37mfor \033[1;31m" . (count($argv) > 1 ? implode(' ', $argv) : "no reason")]); echo "\033[1;33m[\033[1;97m" . date("H:i:s") . "\033[1;33m] \033[0;32mKicked \033[0;37m" . userAccount::idToUsername($target) . "\033[1;37m."; diff --git a/me.php b/me.php @@ -0,0 +1,12 @@ +<?php + +require_once "require/nmps.php"; + +foreach (userAccount::getOnline() as $onlineUser) { + if ($onlineUser['userId'] != $userinfo['id']) + db::query("INSERT INTO events (userId, senderId, eventType, eventData) VALUES (:userId, :senderId, :eventType, :eventData)", + [':userId' => $onlineUser['userId'], ':senderId' => $userinfo['id'], + ':eventType' => 2, ':eventData' => "\033[1;33m[\033[1;97m" . date("H:i:s") . "\033[1;33m] \033[0;33m* \033[0;37m" . $userinfo['username'] . " \033[0;97m" . implode(' ', $argv)]); +} + +echo "\033[1;33m[\033[1;97m" . date("H:i:s") . "\033[1;33m] \033[0;33m* \033[0;37m" . $userinfo['username'] . " \033[0;97m" . implode(' ', $argv); diff --git a/pay.php b/pay.php @@ -1,19 +1,22 @@ <?php - require_once "require/nmps.php"; +require_once "require/nmps.php"; - $user = db::query("SELECT * FROM users WHERE username=:username", ['username' => $argv[0]])[0]; - if (count($user)) { - if (is_numeric($argv[1])) { - if ($argv[1] > 0) { - if ($userinfo['money'] >= $argv[1]) { - db::query("UPDATE users SET money = money - :amount WHERE id=:id", [':amount' => $argv[1], ':id' => $userinfo['id']]); - db::query("UPDATE users SET money = money + :amount WHERE id=:id", [':amount' => $argv[1], ':id' => $user['id']]); - } else - echo "\033[1;31mYou don't have enough money"; +$user = db::query("SELECT * FROM users WHERE username=:username", ['username' => $argv[0]])[0]; +if (count($user)) { + if (is_numeric($argv[1])) { + if ($argv[1] > 0) { + if ($userinfo['money'] >= $argv[1]) { + db::query("UPDATE users SET money = money - :amount WHERE id=:id", [':amount' => $argv[1], ':id' => $userinfo['id']]); + db::query("UPDATE users SET money = money + :amount WHERE id=:id", [':amount' => $argv[1], ':id' => $user['id']]); + nmps::sendEvent($user['id'], $userinfo['id'], 0, + "\033[1;97m" . $userinfo['username'] . " \033[1;37msend you \033[1;32m$" . $argv[1]); + nmps::printEvent("\033[1;37mSent \033[1;32m$" . $argv[1] . "\033[0;37m to \033[1;97m" . $user['username']); } else - echo "\033[1;31mYou can pay money that's greater than 0 only 😳"; + echo "\033[1;31mYou don't have enough money"; } else - echo "\033[1;31mArgument is not a number"; + echo "\033[1;31mYou can pay money that's greater than 0 only 😳"; } else - echo "\033[1;31mUser doesn't exists"; + echo "\033[1;31mArgument is not a number"; +} else + echo "\033[1;31mUser doesn't exists"; diff --git a/require/nmps.php b/require/nmps.php @@ -3,8 +3,9 @@ $argv = explode("\1", $_SERVER['HTTP_ARGV']); db::query("DELETE FROM authTokens WHERE UNIX_TIMESTAMP(CURRENT_TIMESTAMP) - UNIX_TIMESTAMP(lastUsage) >= 60"); +$authToken = $_SERVER['HTTP_AUTH_TOKEN']; + if (!isset($unauthorized)) { - $authToken = $_SERVER['HTTP_AUTH_TOKEN']; $userinfo = db::query("SELECT users.*, authTokens.addressBefore, authTokens.addressAfter FROM users, authTokens WHERE authTokens.token=:token @@ -79,3 +80,23 @@ class userAccount return db::query("SELECT *, UNIX_TIMESTAMP(CURRENT_TIMESTAMP) - UNIX_TIMESTAMP(authTokens.lastUsage) AS timeDifference FROM authTokens WHERE 1"); } } + +class nmps +{ + public static function + sendEvent($target, $sender, $eventType, $eventData) + { + return db::query("INSERT INTO events (userId, senderId, eventType, eventData) VALUES (:userId, :senderId, :eventType, :eventData)", + [':userId' => $target, ':senderId' => $sender, + ':eventType' => $eventType, + ':eventData' => ((0 ? "" : + "\033[1;33m[\033[1;97m" . date("H:i:s") . "\033[1;33m] \033[0;97m") . + $eventData)]); + } + + public static function + printEvent($eventData) + { + echo "\033[1;33m[\033[1;97m" . date("H:i:s") . "\033[1;33m] \033[0;97m" . $eventData; + } +} diff --git a/say.php b/say.php @@ -0,0 +1,12 @@ +<?php + +require_once "require/nmps.php"; + +foreach (userAccount::getOnline() as $onlineUser) { + if ($onlineUser['userId'] != $userinfo['id']) + db::query("INSERT INTO events (userId, senderId, eventType, eventData) VALUES (:userId, :senderId, :eventType, :eventData)", + [':userId' => $onlineUser['userId'], ':senderId' => $userinfo['id'], + ':eventType' => 2, ':eventData' => "\033[1;33m[\033[1;97m" . date("H:i:s") . "\033[1;33m] \033[0;37m" . $userinfo['username'] . "\033[1;37m: \033[0;97m" . implode(' ', $argv)]); +} + +echo "\033[1;33m[\033[1;97m" . date("H:i:s") . "\033[1;33m] \033[0;37m" . $userinfo['username'] . "\033[1;37m: \033[0;97m" . implode(' ', $argv);