nmps-server

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

commit 718d6a0a51fe1770f4c18a0c565281218f504230
Author: kocotian <kocotian@kocotian.pl>
Date:   Mon,  9 Nov 2020 21:15:14 +0100

initial

Diffstat:
Aauth.php | 14++++++++++++++
Arequire/argv.php | 2++
Arequire/dbconnect.php | 9+++++++++
Arequire/dbutils.php | 34++++++++++++++++++++++++++++++++++
4 files changed, 59 insertions(+), 0 deletions(-)

diff --git a/auth.php b/auth.php @@ -0,0 +1,14 @@ +<?php + + require_once "require/argv.php"; + require_once "require/dbconnect.php"; + require_once "require/dbutils.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"; + else + echo "Wrong password"; + else + echo "Unexisting account"; diff --git a/require/argv.php b/require/argv.php @@ -0,0 +1,2 @@ +<?php +$argv = explode("\1", $_GET['argv']); diff --git a/require/dbconnect.php b/require/dbconnect.php @@ -0,0 +1,9 @@ +<?php + + return + [ + "host" => "<MYSQL DATABASE HOST>", + "username" => "<MYSQL DATABASE USERNAME>", + "password" => "<MYSQL DATABASE PASSWORD>", + "database" => "<MYSQL DATABASE NAME>" + ]; diff --git a/require/dbutils.php b/require/dbutils.php @@ -0,0 +1,34 @@ +<?php + + 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; + } + } + } + +?>