nmps-server

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

commit c52ce1e527b8e74ad4a38830cae500fc4671bff3
parent a376024d0427385fee5d998836f4365114f32301
Author: kocotian <kocotian@kocotian.pl>
Date:   Fri, 20 Nov 2020 21:21:24 +0100

many files, normal authorization, userinfo, more

Diffstat:
Mauth.php | 12+++++++-----
Mclear | 4++--
Aecho.php | 5+++++
Aexit.php | 5+++++
Amailbox.php | 3+++
Amotd.php | 13+++++++++++++
Apay.php | 13+++++++++++++
Arequire/nmps.php | 44++++++++++++++++++++++++++++++++++++++++++++
Arules | 7+++++++
Auserinfo.php | 13+++++++++++++
Awhoami.php | 4++++
11 files changed, 116 insertions(+), 7 deletions(-)

diff --git a/auth.php b/auth.php @@ -1,13 +1,15 @@ <?php - require_once "require/argv.php"; - require_once "require/dbconnect.php"; - require_once "require/dbutils.php"; + require_once "require/nmps.php"; $user = db::query("SELECT * FROM users WHERE username=:username", ['username' => $argv[0]])[0]; if ($user) - if (password_verify($argv[1], $user['password'])) - echo "Authorized\naa13d2b75605c3472101c195c5cd1cb601daa320"; + if (password_verify($argv[1], $user['password'])) { + $cstrong = true; + $token = bin2hex(openssl_random_pseudo_bytes(64, $cstrong)); + db::query("INSERT INTO authTokens (userId, token) VALUES (:userId, :token)", [':userId' => $user['id'], ':token' => sha1($token)]); + echo "Authorized\n" . $token; + } else echo "Wrong password"; else diff --git a/clear b/clear @@ -1 +1 @@ -- \ No newline at end of file +c+ \ No newline at end of file diff --git a/echo.php b/echo.php @@ -0,0 +1,5 @@ +<?php + require_once "require/nmps.php"; + $i = -1; + while (++$i < count($argv)) + printf("%s", ($i ? " " : "") . $argv[$i]); diff --git a/exit.php b/exit.php @@ -0,0 +1,5 @@ +<?php + require_once "require/nmps.php"; + + db::query("DELETE FROM authTokens WHERE token=:token", [':token' => sha1($authToken)]); + echo "\004\001"; diff --git a/mailbox.php b/mailbox.php @@ -0,0 +1,3 @@ +<?php + require_once "require/nmps.php"; + echo $argv[0] == "-s" ? 0 : "\033[1;97mYou have \033[1;33m0 \033[1;97munread mails."; diff --git a/motd.php b/motd.php @@ -0,0 +1,13 @@ +<?php +$eq = "\033[1;33m======================="; +for ($i = 0; $i < strlen($_SERVER['SERVER_NAME']); ++$i) + $eq .= "="; +echo $eq . " + +\033[1;97mWelcome to the \033[1;33m{$_SERVER['SERVER_NAME']} \033[1;97mserver! +\033[1;97mToday is \033[1;33m" . date("Y/m/d") . " +\033[1;97mWrite \033[1;33mhelp \033[1;97mfor help. + +\033[1;97mRules: +" . file_get_contents("rules") . " +$eq"; diff --git a/pay.php b/pay.php @@ -0,0 +1,13 @@ +<?php + + require_once "require/nmps.php"; + + $user = db::query("SELECT * FROM users WHERE username=:username", ['username' => $argv[0]])[0]; + if ($user) { + echo "\033[1;33musername: \033[0;33m" . $user['username'] . "\n"; + echo "\033[1;33mlast online: \033[0;33m" . $user['lastOnline'] . "\n"; + echo "\033[1;33mmoney: \033[0;33m" . $user['money'] . "\n"; + echo "\033[1;33mlevel: \033[0;33m" . $user['level'] . "\n"; + echo "\033[1;33mxp: \033[0;33m" . $user['xp'] . "\n"; + } else + echo "\033[1;31mUser doesn't exists"; diff --git a/require/nmps.php b/require/nmps.php @@ -0,0 +1,44 @@ +<?php + +$argv = explode("\1", $_SERVER['HTTP_ARGV']); + +if (!isset($unauthorized)) { + $authToken = $_SERVER['HTTP_AUTH_TOKEN']; + $userinfo = db::query("SELECT users.* FROM users, authTokens WHERE + authTokens.token=:token AND + authTokens.userId=users.id", [':token' => sha1($authToken)])[0]; + if (count($userinfo)) + db::query("UPDATE users SET lastOnline=CURRENT_TIMESTAMP WHERE id=:userId", + [':userId' => $userinfo['id']]); +} + +class db +{ + private static function connect() + { + try + { + $dbCredentials = require("dbconnect.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) + { + exit("Database error"); + } + } + + public static function query($query, $parameters = []) + { + $statement = self::connect() -> prepare($query); + $statement -> execute($parameters); + if(explode(' ', $query)[0] == 'SELECT') + { + $data = $statement -> fetchAll(); + return $data; + } + } +} diff --git a/rules b/rules @@ -0,0 +1,7 @@ +- No Trolling +- No Constant Cursing +- No Full Caps Messages +- No Spamming +- No Advertising +- Don't be rude +- Respect all Players diff --git a/userinfo.php b/userinfo.php @@ -0,0 +1,13 @@ +<?php + + require_once "require/nmps.php"; + + $user = db::query("SELECT * FROM users WHERE username=:username", ['username' => $argv[0]])[0]; + if ($user) { + echo "\033[1;33musername: \033[0;33m" . $user['username'] . "\n"; + echo "\033[1;33mlast online: \033[0;33m" . $user['lastOnline'] . "\n"; + echo "\033[1;33mmoney: \033[0;33m" . $user['money'] . "\n"; + echo "\033[1;33mlevel: \033[0;33m" . $user['level'] . "\n"; + echo "\033[1;33mxp: \033[0;33m" . $user['xp'] . "\n"; + } else + echo "\033[1;31mUser doesn't exists"; diff --git a/whoami.php b/whoami.php @@ -0,0 +1,4 @@ +<?php + + require_once "require/nmps.php"; + echo $userinfo['username'];