-
Notifications
You must be signed in to change notification settings - Fork 0
/
Auth.php
59 lines (42 loc) · 1.8 KB
/
Auth.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
<?php
require_once('Mechanism.php');
require_once('User.php');
class Lsucs_Auth {
private static $logfile;
public static function log($message) {
if(self::$logfile)
fputs(self::$logfile, $message);
}
public static function run() {
require('config.php');
self::$logfile = fopen($config['log_file'], 'a');
self::log(date('Y-m-d H:i:s') . " Incoming request from " . $_SERVER['REMOTE_ADDR'] . "\n");
//Check key
if (!isset($_POST["key"]) || $_POST["key"] != $config['key']) {
self::error(0);
}
self::log("Data:\n" . print_r($_REQUEST, true) . "\n");
//Load auth mechanism
require_once('Mechanism/' . $config['mechanism'] . '.php');
$mechanism = new $config['mechanism'];
//Handle api request
$mechanism->handleRequest();
}
public static function error($code) {
switch ($code) {
case 0: $message = 'Invalid API key'; break;
case 1: $message = 'Invalid API method'; break;
case 2: $message = 'Missing method parameters'; break;
case 3: $message = 'Match not found'; break;
case 4: $message = 'Invalid parameter type'; break;
default: $message = 'Unknown error'; break;
}
self::log('ERROR: ' . $message . "\n\n");
self::respond(array("error" => $message, "code" => $code));
}
public static function respond($data) {
self::log("Response\n" . print_r($data, true) . "\n");
die(json_encode($data));
}
}
?>