forked from StartupAPI/users
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtools.php
More file actions
71 lines (62 loc) · 1.9 KB
/
tools.php
File metadata and controls
71 lines (62 loc) · 1.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
<?php
/*
* Various tools used within UserBase
*/
class UserTools
{
// CSRF prevention variables
public static $CSRF_NONCE;
/*
* Escapes strings making it safe to include user data in HTML output
*/
public static function escape($string)
{
return htmlentities($string, ENT_COMPAT, 'UTF-8');
}
public static function preventCSRF() {
$storage = new MrClay_CookieStorage(array(
'secret' => UserConfig::$SESSION_SECRET,
'mode' => MrClay_CookieStorage::MODE_ENCRYPT,
'path' => UserConfig::$SITEROOTURL,
'httponly' => true
));
/*
* Preventing CSRFs in all POST requests by double-posting cookies
*/
if (count($_POST) > 0) {
if (!array_key_exists('CSRF_NONCE', $_POST)) {
error_log('POST request in admin interface without CSRF nonce. Make sure form includes CSRF_NONCE hidden field.');
header('HTTP/1.0 403 POST request origin check failed', true, 403);
exit;
}
$passed_nonce = $storage->fetch(UserConfig::$csrf_nonce_key);
if ($passed_nonce != $_POST['CSRF_NONCE']) {
error_log('[Possible CSRF attach] POST request with wrong nonce!!!');
header('HTTP/1.0 403 POST request origin check failed', true, 403);
exit;
}
}
self::$CSRF_NONCE = base64_encode(mcrypt_create_iv(50, MCRYPT_DEV_URANDOM));
$storage->store(UserConfig::$csrf_nonce_key, self::$CSRF_NONCE);
}
public static function renderCSRFNonce() {
?><input type="hidden" name="CSRF_NONCE" value="<?php echo self::escape(self::$CSRF_NONCE); ?>"/>
<?php
}
/**
* Debug wrapper for simplified debugging, call it like this:
*
* UserTools::debug('... some message ...');
*/
public static function debug($message) {
if (UserConfig::$DEBUG) {
$trace = debug_backtrace();
error_log('[DEBUG] ' . $message .
' (' . $trace[1]['function'] .
'(' . var_export($trace[1]['args'], true) . ')' .
' on line ' . $trace[0]['line'] .
' in ' . $trace[0]['file'] .
')');
}
}
}