-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.php
40 lines (34 loc) · 974 Bytes
/
index.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
<?php
require "BasicAuth.php";
// Instantiate an instance of the authentication class
$basic = new BasicAuth('../users.ini');
// Authorize every request or bail
if (!$basic->auth()) {
die();
}
// Split the URI into segments and route the first
$uriSegments = explode("/", parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH));
switch($uriSegments[1]) {
case 'hello':
helloHandler();
break;
case 'hash':
hashHandler($uriSegments[2] ?? null);
break;
default:
echo "Try the <em>/hello</em> or <em>/hash</em> endpoints.";
}
// Return hello
function helloHandler() {
echo "Hello World";
}
// Return a hash
function hashHandler($password) {
// If a password wasn't provided show the URI syntax
if (empty($password)) {
die("Syntax: http://localhost:8004/hash/{password}");
}
// Bcrypt the users password and return a hash
$hash = password_hash($password, PASSWORD_BCRYPT);
echo $hash;
}