-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathUser.php
100 lines (93 loc) · 2.86 KB
/
User.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
93
94
95
96
97
98
99
100
<?php
namespace Luracast\Restler;
/**
* Information gathered about the api user is kept here using static methods
* and properties for other classes to make use of them.
* Typically Authentication classes populate them
*
* @category Framework
* @package restler
* @author R.Arul Kumaran <[email protected]>
* @copyright 2010 Luracast
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @link http://luracast.com/products/restler/
*
*/
class User implements iIdentifyUser
{
private static $initialized = false;
public static $id = null;
public static $cacheId = null;
public static $ip;
public static $browser = '';
public static $platform = '';
public static function init()
{
static::$initialized = true;
static::$ip = static::getIpAddress();
}
public static function getUniqueIdentifier($includePlatform = false)
{
if (!static::$initialized) static::init();
return static::$id ? : base64_encode('ip:' . ($includePlatform
? static::$ip . '-' . static::$platform
: static::$ip
));
}
public static function getIpAddress($ignoreProxies = false)
{
foreach (array('HTTP_CLIENT_IP', 'HTTP_X_FORWARDED_FOR',
'HTTP_X_FORWARDED', 'HTTP_X_CLUSTER_CLIENT_IP',
'HTTP_FORWARDED_FOR', 'HTTP_FORWARDED',
'REMOTE_ADDR') as $key) {
if (array_key_exists($key, $_SERVER) === true) {
foreach (explode(',', $_SERVER[$key]) as $ip) {
$ip = trim($ip); // just to be safe
if (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4
| FILTER_FLAG_NO_PRIV_RANGE
| FILTER_FLAG_NO_RES_RANGE) !== false
) {
return $ip;
}
}
}
}
}
/**
* Authentication classes should call this method
*
* @param string $id user id as identified by the authentication classes
*
* @return void
*/
public static function setUniqueIdentifier($id)
{
static::$id = $id;
}
/**
* User identity to be used for caching purpose
*
* When the dynamic cache service places an object in the cache, it needs to
* label it with a unique identifying string known as a cache ID. This
* method gives that identifier
*
* @return string
*/
public static function getCacheIdentifier()
{
return static::$cacheId ?: static::$id;
}
/**
* User identity for caching purpose
*
* In a role based access control system this will be based on role
*
* @param $id
*
* @return void
*/
public static function setCacheIdentifier($id)
{
static::$cacheId = $id;
}
}