|
1
|
|
|
<?php |
|
2
|
|
|
namespace Graceland\SafeInCloud; |
|
3
|
|
|
|
|
4
|
|
|
use GuzzleHttp\Message\ResponseInterface; |
|
5
|
|
|
|
|
6
|
|
|
class Response |
|
7
|
|
|
{ |
|
8
|
|
|
protected $body; |
|
9
|
|
|
protected $encrypter; |
|
10
|
|
|
protected $request; |
|
11
|
|
|
protected $response; |
|
12
|
|
|
|
|
13
|
|
|
public function __construct(ResponseInterface $response, Request $request) |
|
14
|
|
|
{ |
|
15
|
|
|
$this->request = $request; |
|
16
|
|
|
$this->response = $response; |
|
17
|
|
|
$this->encrypter = $this->request->getEncrypter(); |
|
18
|
|
|
$this->body = $this->response->json(); |
|
19
|
|
|
} |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @return mixed |
|
23
|
|
|
*/ |
|
24
|
|
|
public function get($key, $default = null) |
|
25
|
|
|
{ |
|
26
|
|
|
if ($this->has($key) === false) { |
|
27
|
|
|
return $default; |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
return $this->body[$key]; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @return mixed |
|
35
|
|
|
*/ |
|
36
|
|
|
public function getDecrypted($key, $default = null, $assocKeys = []) |
|
37
|
|
|
{ |
|
38
|
|
|
$value = $this->get($key, $default); |
|
39
|
|
|
|
|
40
|
|
|
if ($value === $default) { |
|
41
|
|
|
return $default; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
return $this->decrypt($value, null, $assocKeys); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
public function has($key) |
|
48
|
|
|
{ |
|
49
|
|
|
return (isset($this->body[$key]) === true); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
public function isSuccessful() |
|
53
|
|
|
{ |
|
54
|
|
|
return (isset($this->body['success']) === true && $this->body['success'] === true); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
public function isValid() |
|
58
|
|
|
{ |
|
59
|
|
|
if ($this->response->getStatusCode() < 200 || $this->response->getStatusCode() >= 300) { |
|
60
|
|
|
return false; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
if (isset($this->body['nonce']) === false || isset($this->body['verifier']) === false) { |
|
64
|
|
|
return false; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
$nonce = base64_decode($this->body['nonce']); |
|
68
|
|
|
$verifier = $this->decrypt($this->body['verifier'], $nonce); |
|
69
|
|
|
|
|
70
|
|
|
return $verifier === $this->body['nonce']; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* @return string |
|
75
|
|
|
*/ |
|
76
|
|
|
protected function decrypt($data, $nonce = null, $assocKeys = []) |
|
77
|
|
|
{ |
|
78
|
|
|
if ($nonce === null) { |
|
79
|
|
|
$nonce = $this->request->getNonce(); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
if (is_array($data) === false) { |
|
83
|
|
|
return $this->encrypter->decrypt(base64_decode($data), $nonce); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
$decrypted = []; |
|
87
|
|
|
$isAssoc = (bool) count(array_filter(array_keys($data), 'is_string')); |
|
88
|
|
|
|
|
89
|
|
|
foreach ($data as $key => $value) { |
|
90
|
|
|
if ($isAssoc === true and in_array($key, $assocKeys) === false) { |
|
|
|
|
|
|
91
|
|
|
$decrypted[$key] = $value; |
|
92
|
|
|
continue; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
$decrypted[$key] = $this->decrypt($value, $nonce, $assocKeys); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
return $decrypted; |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
|
PHP has two types of connecting operators (logical operators, and boolean operators):
and&&or||The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like
&&, or||.Let’s take a look at a few examples:
Logical Operators are used for Control-Flow
One case where you explicitly want to use logical operators is for control-flow such as this:
Since
dieintroduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined withthrowat this point:These limitations lead to logical operators rarely being of use in current PHP code.