-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutil.php
More file actions
104 lines (79 loc) · 2.9 KB
/
util.php
File metadata and controls
104 lines (79 loc) · 2.9 KB
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
93
94
95
96
97
98
99
100
101
102
103
104
<?php
function connect($dbhost, $dbuser, $password, $dbname) {
// the @ sign suppresses errors, which we handle in the next line
@ $db = new mysqli($dbhost, $dbuser, $password, $dbname);
// check if there was an error connecting to the database
if ($db->connect_errno) {
reportErrorAndDie("Oops! We could not connect to the database.<p>\n This was the error we found: " .
$db->connect_error . "\n <p>Please <a href='input.php'>try again</a>");
}
return $db;
}
function reportErrorAndDie($errorMessage, $queryStr = '') {
echo $errorMessage;
if (strlen($queryStr) > 0) {
echo "<p>";
echo "Query string: " . $queryStr;
}
die();
}
function getSalt() {
$salt = sprintf('$2a$%02d$', 12);
$bytes = getRandomBytes(16);
$salt .= encodeBytes($bytes);
return $salt;
}
function getRandomBytes($count) {
$bytes = '';
if(function_exists('openssl_random_pseudo_bytes') &&
(strtoupper(substr(PHP_OS, 0, 3)) !== 'WIN')) {
$bytes = openssl_random_pseudo_bytes($count);
}
if($bytes === '' && is_readable('/dev/urandom') &&
($hRand = @fopen('/dev/urandom', 'rb')) !== FALSE) {
$bytes = fread($hRand, $count);
fclose($hRand);
}
if(strlen($bytes) < $count) {
$bytes = '';
$randomState = microtime();
if(function_exists('getmypid')) {
$randomState .= getmypid();
}
for($i = 0; $i < $count; $i += 16) {
$randomState = md5(microtime() . $randomState);
if (PHP_VERSION >= '5') {
$bytes .= md5($randomState, true);
} else {
$bytes .= pack('H*', md5($randomState));
}
}
$bytes = substr($bytes, 0, $count);
}
return $bytes;
}
function encodeBytes($input) {
// The following is code from the PHP Password Hashing Framework
$itoa64 = './ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
$output = '';
$i = 0;
do {
$c1 = ord($input[$i++]);
$output .= $itoa64[$c1 >> 2];
$c1 = ($c1 & 0x03) << 4;
if ($i >= 16) {
$output .= $itoa64[$c1];
break;
}
$c2 = ord($input[$i++]);
$c1 |= $c2 >> 4;
$output .= $itoa64[$c1];
$c1 = ($c2 & 0x0f) << 2;
$c2 = ord($input[$i++]);
$c1 |= $c2 >> 6;
$output .= $itoa64[$c1];
$output .= $itoa64[$c2 & 0x3f];
} while (1);
return $output;
}
?>