-
Notifications
You must be signed in to change notification settings - Fork 1
/
global.php
92 lines (78 loc) · 2.76 KB
/
global.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
<?php
/*
* Cubition PHP Authentication API
* Released under the MIT Licence
* Copyright 2015 - Cubition Team
*/
namespace Cubition\Auth;
use \PDO, PDOException;
// # Set the Content-type header to plaintext
header ('Content-type: text/plain; charset=utf-8');
define('BASEDIR', realpath(__DIR__));
// # Check for CloudFlare IP header
if (!empty ($_SERVER['HTTP_CF_CONNECTING_IP']))
$_SERVER ['REMOTE_ADDR'] = $_SERVER['HTTP_CF_CONNECTING_IP'];
## REGION functions ##
{
// # Error Function
function printError($text, $code = -1)
{
exit('[Cubition PHPAuth] Error' . ($code > - 1 ? ' #' . $code : '') . ':'. PHP_EOL . $text . PHP_EOL);
}
function usernameExists($username)
{
global $db;
$query = $db->prepare('SELECT 0 FROM auth_users WHERE username = :username LIMIT 1');
$query->execute(array(
':username' => $username
));
return $query->rowCount() == 1;
}
function emailExists($email)
{
global $db;
$query = $db->prepare('SELECT 0 FROM auth_users WHERE email = :email LIMIT 1');
$query->execute(array(
':email' => email
));
return $query->rowCount() == 1;
}
function userID($username)
{
global $db;
$query = $db->prepare('SELECT user_id FROM auth_users WHERE username = :username LIMIT 1');
$query->execute(array(
':username' => $username
));
$result = $query->fetch(\PDO::FETCH_NUM);
return $result == null ? null : $result[0];
}
function userPassHash($username)
{
global $db;
$query = $db->prepare('SELECT password FROM auth_users WHERE username = :username LIMIT 1');
$query->execute(array(
':username' => $username
));
$result = $query->fetch(\PDO::FETCH_NUM);
return $result == null ? null : $result[0];
}
}
## REGION connection ##
{
// # Load the configuration file
(@include_once BASEDIR . '/config.php')
|| printError ('The configuration file config.php> appears to be missing.', 1);
// # Check if pdo_mysql is installed
extension_loaded('pdo_mysql')
|| printError ('The pdo_mysql extension, which is required for Cubition PHPAuth to run, appears missing. Please check your PHP.ini.', 2);
// # Set up the MySQL connection using PDO
try {
$db = new PDO('mysql:host=' . $mysqlHost . ';dbname=' . $mysqlDB, $mysqlUser, $mysqlPass);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $ex) {
printError('Cannot connect to the specified MySQL host (<kbd>' . $mysqlHost . '</kbd>)', 3);
}
// # Destroy the MySQL login credentials
unset ($mysqlHost, $mysqlDB, $mysqlUser, $mysqlPass);
}