|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* sysPass |
|
4
|
|
|
* |
|
5
|
|
|
* @author nuxsmin |
|
6
|
|
|
* @link https://syspass.org |
|
7
|
|
|
* @copyright 2012-2018, Rubén Domínguez nuxsmin@$syspass.org |
|
8
|
|
|
* |
|
9
|
|
|
* This file is part of sysPass. |
|
10
|
|
|
* |
|
11
|
|
|
* sysPass is free software: you can redistribute it and/or modify |
|
12
|
|
|
* it under the terms of the GNU General Public License as published by |
|
13
|
|
|
* the Free Software Foundation, either version 3 of the License, or |
|
14
|
|
|
* (at your option) any later version. |
|
15
|
|
|
* |
|
16
|
|
|
* sysPass is distributed in the hope that it will be useful, |
|
17
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
18
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
19
|
|
|
* GNU General Public License for more details. |
|
20
|
|
|
* |
|
21
|
|
|
* You should have received a copy of the GNU General Public License |
|
22
|
|
|
* along with sysPass. If not, see <http://www.gnu.org/licenses/>. |
|
23
|
|
|
*/ |
|
24
|
|
|
|
|
25
|
|
|
namespace SP\Http; |
|
26
|
|
|
|
|
27
|
|
|
use Klein\Klein; |
|
28
|
|
|
use Klein\Response; |
|
29
|
|
|
use SP\Bootstrap; |
|
30
|
|
|
use SP\Core\Exceptions\SPException; |
|
31
|
|
|
|
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Class Json con utilidades para JSON |
|
35
|
|
|
* |
|
36
|
|
|
* @package SP\Util |
|
37
|
|
|
*/ |
|
38
|
|
|
final class Json |
|
39
|
|
|
{ |
|
40
|
|
|
const SAFE = [ |
|
41
|
|
|
'from' => ['\\', '"', '\''], |
|
42
|
|
|
'to' => ['\\', '\"', '\\\''] |
|
43
|
|
|
]; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @var Response |
|
47
|
|
|
*/ |
|
48
|
|
|
private $response; |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Json constructor. |
|
52
|
|
|
* |
|
53
|
|
|
* @param Response $response |
|
54
|
|
|
*/ |
|
55
|
|
|
public function __construct(Response $response) |
|
56
|
|
|
{ |
|
57
|
|
|
$this->response = $response; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* @param Response $response |
|
62
|
|
|
* |
|
63
|
|
|
* @return Json |
|
64
|
|
|
*/ |
|
65
|
|
|
public static function factory(Response $response) |
|
66
|
|
|
{ |
|
67
|
|
|
return new self($response); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* @return Json |
|
72
|
|
|
*/ |
|
73
|
|
|
public static function fromDic() |
|
74
|
|
|
{ |
|
75
|
|
|
return new self(Bootstrap::getContainer()->get(Klein::class)->response()); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Devuelve un array con las cadenas formateadas para JSON |
|
80
|
|
|
* |
|
81
|
|
|
* @param $data mixed |
|
82
|
|
|
* |
|
83
|
|
|
* @return mixed |
|
84
|
|
|
*/ |
|
85
|
|
|
public static function safeJson(&$data) |
|
86
|
|
|
{ |
|
87
|
|
|
if (is_array($data) || is_object($data)) { |
|
88
|
|
|
array_walk_recursive($data, |
|
|
|
|
|
|
89
|
|
|
function (&$value) { |
|
90
|
|
|
if (is_object($value)) { |
|
91
|
|
|
foreach ($value as $property => $v) { |
|
92
|
|
|
if (is_string($v) && $v !== '') { |
|
93
|
|
|
$value->$property = self::safeJsonString($v); |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
return $value; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
if (is_string($value) && $value !== '') { |
|
101
|
|
|
return self::safeJsonString($value); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
return $value; |
|
105
|
|
|
} |
|
106
|
|
|
); |
|
107
|
|
|
} elseif (is_string($data) && $data !== '') { |
|
108
|
|
|
return self::safeJsonString($data); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
return $data; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* Devuelve una cadena con los carácteres formateadas para JSON |
|
116
|
|
|
* |
|
117
|
|
|
* @param $string |
|
118
|
|
|
* |
|
119
|
|
|
* @return mixed |
|
120
|
|
|
*/ |
|
121
|
|
|
public static function safeJsonString($string) |
|
122
|
|
|
{ |
|
123
|
|
|
return str_replace(self::SAFE['from'], self::SAFE['to'], $string); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* Devuelve una respuesta en formato JSON |
|
128
|
|
|
* |
|
129
|
|
|
* @param string $data JSON string |
|
130
|
|
|
*/ |
|
131
|
|
|
public function returnRawJson($data) |
|
132
|
|
|
{ |
|
133
|
|
|
$this->response |
|
134
|
|
|
->header('Content-type', 'application/json; charset=utf-8') |
|
135
|
|
|
->body($data) |
|
136
|
|
|
->send(true); |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* Devuelve una respuesta en formato JSON con el estado y el mensaje. |
|
141
|
|
|
* |
|
142
|
|
|
* @param JsonResponse $jsonResponse |
|
143
|
|
|
* |
|
144
|
|
|
* @return void |
|
145
|
|
|
*/ |
|
146
|
|
|
public function returnJson(JsonResponse $jsonResponse) |
|
147
|
|
|
{ |
|
148
|
|
|
$this->response->header('Content-type', 'application/json; charset=utf-8'); |
|
149
|
|
|
|
|
150
|
|
|
try { |
|
151
|
|
|
$this->response->body(self::getJson($jsonResponse)); |
|
152
|
|
|
} catch (SPException $e) { |
|
153
|
|
|
$jsonResponse = new JsonResponse($e->getMessage()); |
|
154
|
|
|
$jsonResponse->addMessage($e->getHint()); |
|
155
|
|
|
|
|
156
|
|
|
$this->response->body(json_encode($jsonResponse)); |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
$this->response->send(true); |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
/** |
|
163
|
|
|
* Devuelve una cadena en formato JSON |
|
164
|
|
|
* |
|
165
|
|
|
* @param $data |
|
166
|
|
|
* |
|
167
|
|
|
* @return string La cadena en formato JSON |
|
168
|
|
|
* @throws \SP\Core\Exceptions\SPException |
|
169
|
|
|
*/ |
|
170
|
|
|
public static function getJson($data) |
|
171
|
|
|
{ |
|
172
|
|
|
$json = json_encode($data, JSON_PARTIAL_OUTPUT_ON_ERROR); |
|
173
|
|
|
|
|
174
|
|
|
if ($json === false || json_last_error() !== JSON_ERROR_NONE) { |
|
175
|
|
|
throw new SPException( |
|
176
|
|
|
__u('Error de codificación'), |
|
177
|
|
|
SPException::CRITICAL, |
|
178
|
|
|
json_last_error_msg() |
|
179
|
|
|
); |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
return $json; |
|
183
|
|
|
} |
|
184
|
|
|
} |