nmps-server

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

commit 1d7ef010b1d046a2fe965009e59147ba4d7161c1
parent 3c7930ed3e1eba866e8ab35875138248e89ccb98
Author: kocotian <kocotian@kocotian.pl>
Date:   Sun,  6 Dec 2020 21:33:51 +0100

events, better who, reduced wait time, kick, msg

Diffstat:
AeventSender.php | 17+++++++++++++++++
Akick.php | 22++++++++++++++++++++++
Amsg.php | 16++++++++++++++++
Mrequire/nmps.php | 49+++++++++++++++++++++++++++++++++++++++----------
Mwho.php | 5+++--
5 files changed, 97 insertions(+), 12 deletions(-)

diff --git a/eventSender.php b/eventSender.php @@ -0,0 +1,17 @@ +<?php + +require_once "require/nmps.php"; + +$iterations = 0; +while (!count($events = db::query("SELECT * FROM events WHERE userId=:userId", + [':userId' => $userinfo['id']])) && ++$iterations < 45) { + usleep(1000000); +} + +foreach ($events as $event) { + echo ($event['eventType'] == 126 ? "\004\001" : "") . $event['eventData']; + db::query("DELETE FROM events WHERE id=:id", [':id' => $event['id']]); +} + +if (!count($events)) + echo "\033[1;33m[\033[1;97m" . date("H:i:s") . "\033[1;33m] \033[1;97mServer \033[0;37mpinged you"; diff --git a/kick.php b/kick.php @@ -0,0 +1,22 @@ +<?php + +require_once "require/nmps.php"; + +if ($userinfo['op'] < 3) + die("\033[1;31mYour op level is too low"); + +if (!($target = userAccount::usernameToId($argv[0]))) + die("\033[1;31mUser doesn't exists"); + +if (!userAccount::isUserOnline($target)) + die("\033[1;31mUser is offline"); + +unset($argv[0]); +db::query("DELETE FROM authTokens WHERE userId=:userId", + [':userId' => $target]); + +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")]); + +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/msg.php b/msg.php @@ -0,0 +1,16 @@ +<?php + +require_once "require/nmps.php"; + +if (!($target = userAccount::usernameToId($argv[0]))) + die("\033[1;31mUser doesn't exists"); + +if (!userAccount::isUserOnline($target)) + die("\033[1;31mUser is offline"); + +unset($argv[0]); +db::query("INSERT INTO events (userId, senderId, eventType, eventData) VALUES (:userId, :senderId, :eventType, :eventData)", + [':userId' => $target, ':senderId' => $userinfo['id'], + ':eventType' => 0, ':eventData' => "\033[1;33m[\033[1;97m" . date("H:i:s") . "\033[1;33m] \033[0;37m" . $userinfo['username'] . " \033[1;37m-> \033[0;31mYou\033[1;37m: \033[0;97m" . implode(' ', $argv)]); + +echo "\033[1;33m[\033[1;97m" . date("H:i:s") . "\033[1;33m] \033[0;31mYou \033[1;37m-> \033[0;37m" . userAccount::idToUsername($target) . "\033[1;37m: \033[0;97m" . implode(' ', $argv); diff --git a/require/nmps.php b/require/nmps.php @@ -1,7 +1,7 @@ <?php $argv = explode("\1", $_SERVER['HTTP_ARGV']); -db::query("DELETE FROM authTokens WHERE UNIX_TIMESTAMP(CURRENT_TIMESTAMP) - UNIX_TIMESTAMP(lastUsage) >= 300"); +db::query("DELETE FROM authTokens WHERE UNIX_TIMESTAMP(CURRENT_TIMESTAMP) - UNIX_TIMESTAMP(lastUsage) >= 60"); if (!isset($unauthorized)) { $authToken = $_SERVER['HTTP_AUTH_TOKEN']; @@ -20,26 +20,24 @@ if (!isset($unauthorized)) { } } -class db -{ - private static function connect() +class db { + private static function + connect() { - try - { + try { $dbCredentials = require("../nmpsdb.php"); $pdo = new PDO("mysql:host={$dbCredentials['host']};dbname={$dbCredentials['database']};charset=utf8", $dbCredentials['username'], $dbCredentials['password'], [ PDO::ATTR_EMULATE_PREPARES => false, PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION ]); return $pdo; - } - catch(PDOException $error) - { + } catch(PDOException $error) { exit("Database error"); } } - public static function query($query, $parameters = []) + public static function + query($query, $parameters = []) { $statement = self::connect() -> prepare($query); $statement -> execute($parameters); @@ -50,3 +48,34 @@ class db } } } + +class userAccount +{ + public static function + usernameToId($username) + { + $id = db::query("SELECT id FROM users WHERE username=:username", [':username' => $username]); + if ($id) return $id[0]['id']; + else return false; + } + + public static function + idToUsername($id) + { + $username = db::query("SELECT username FROM users WHERE id=:id", [':id' => $id]); + if ($username) return $username[0]['username']; + else return false; + } + + public static function + isUserOnline($id) + { + return count(db::query("SELECT * FROM authTokens WHERE userId=:userId", [':userId' => $id])); + } + + public static function + getOnline() + { + return db::query("SELECT *, UNIX_TIMESTAMP(CURRENT_TIMESTAMP) - UNIX_TIMESTAMP(authTokens.lastUsage) AS timeDifference FROM authTokens WHERE 1"); + } +} diff --git a/who.php b/who.php @@ -1,8 +1,9 @@ <?php require_once "require/nmps.php"; -$onlineUsers = db::query("SELECT *, UNIX_TIMESTAMP(CURRENT_TIMESTAMP) - UNIX_TIMESTAMP(users.lastOnline) AS timeDifference FROM users WHERE UNIX_TIMESTAMP(CURRENT_TIMESTAMP) - UNIX_TIMESTAMP(users.lastOnline) < 300"); +$onlineUsers = userAccount::getOnline(); echo "\033[1;97mNow online:"; foreach ($onlineUsers as $onlineUser) { - echo "\n\033[0;97m" . $onlineUser['username'] . str_repeat(" ", 16 - strlen($onlineUser['username'])) . "\033[0;33m" . $onlineUser['lastAddress'] . str_repeat(" ", 16 - strlen($onlineUser['lastAddress'])) . "\033[0;97m" . ($onlineUser['timeDifference'] > 60 ? (" \033[0;33m(\033[1;33m" . $onlineUser['timeDifference'] . " \033[0;33mseconds ago)") : ""); + $onlineUserUsername = userAccount::idToUsername($onlineUser['userId']); + echo "\n\033[0;97m" . $onlineUserUsername . ($userinfo['op'] > 2 ? (str_repeat(" ", 16 - strlen($onlineUserUsername)) . "\033[0;33m" . $onlineUser['addressAfter']) : ''); }