1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Bouncer package. |
5
|
|
|
* |
6
|
|
|
* (c) François Hodierne <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Bouncer\Resource; |
13
|
|
|
|
14
|
|
|
use Bouncer\Bouncer; |
15
|
|
|
use Bouncer\Resource; |
16
|
|
|
|
17
|
|
|
class Signature extends Resource |
18
|
|
|
{ |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* HTTP Headers used to compute the signature |
22
|
|
|
* |
23
|
|
|
* @var array |
24
|
|
|
*/ |
25
|
|
|
public static $signatureHeaders = array( |
26
|
|
|
'User-Agent', |
27
|
|
|
'Accept', |
28
|
|
|
'Accept-Charset', |
29
|
|
|
'Accept-Language', |
30
|
|
|
'Accept-Encoding', |
31
|
|
|
'From', |
32
|
|
|
'Dnt', |
33
|
|
|
); |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* The unique id |
37
|
|
|
* |
38
|
|
|
* @var string |
39
|
|
|
*/ |
40
|
|
|
protected $id; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* The HTTP headers (used to compute the signature) |
44
|
|
|
* |
45
|
|
|
* @var array |
46
|
|
|
*/ |
47
|
|
|
protected $headers; |
48
|
|
|
|
49
|
|
|
public function getId() |
50
|
|
|
{ |
51
|
|
|
return $this->id; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function setId($id) |
55
|
|
|
{ |
56
|
|
|
$this->id = $id; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function setHeaders($headers) |
60
|
|
|
{ |
61
|
|
|
$this->headers = $headers; |
62
|
|
|
$this->id = self::hash($this->headers); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function getLanguage() |
66
|
|
|
{ |
67
|
|
|
return $this->getAttribute('language'); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function getCountryCode() |
71
|
|
|
{ |
72
|
|
|
return $this->getAttribute('country_code'); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
// Static |
76
|
|
|
|
77
|
|
|
public static function hash($array) |
78
|
|
|
{ |
79
|
|
|
$array = self::filterArrayKeys($array, self::$signatureHeaders); |
80
|
|
|
ksort($array); |
81
|
|
|
$string = ''; |
82
|
|
|
foreach ($array as $key => $value) { |
83
|
|
|
$key = self::normalizeKey($key); |
84
|
|
|
$string .= "$key:$value;"; |
85
|
|
|
} |
86
|
|
|
return Bouncer::hash($string); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
public static function filterArrayKeys($array = array(), $keys = array()) |
90
|
|
|
{ |
91
|
|
|
$ikeys = array_map('strtolower', $keys); |
92
|
|
|
foreach ($array as $key => $value) { |
93
|
|
|
$ikey = strtolower($key); |
94
|
|
|
if (!in_array($ikey, $ikeys)) { |
95
|
|
|
unset($array[$key]); |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
return $array; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
public static function normalizeKey($key) |
102
|
|
|
{ |
103
|
|
|
if (strpos($key, '-')) { |
104
|
|
|
$parts = explode('-', $key); |
105
|
|
|
$parts = array_map('strtolower', $parts); |
106
|
|
|
$parts = array_map('ucfirst', $parts); |
107
|
|
|
$key = implode('-', $parts); |
108
|
|
|
} else { |
109
|
|
|
$key = ucfirst(strtolower($key)); |
110
|
|
|
} |
111
|
|
|
return $key; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
} |
115
|
|
|
|