1 | <?php |
||
2 | /** |
||
3 | * sysPass |
||
4 | * |
||
5 | * @author nuxsmin |
||
6 | * @link https://syspass.org |
||
7 | * @copyright 2012-2019, 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, |
||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
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 | * @return bool |
||
132 | */ |
||
133 | public function returnRawJson(string $data) |
||
134 | { |
||
135 | return $this->response |
||
136 | ->header('Content-type', 'application/json; charset=utf-8') |
||
137 | ->body($data) |
||
138 | ->send(true) |
||
139 | ->isSent(); |
||
140 | } |
||
141 | |||
142 | /** |
||
143 | * Devuelve una respuesta en formato JSON con el estado y el mensaje. |
||
144 | * |
||
145 | * @param JsonResponse $jsonResponse |
||
146 | * |
||
147 | * @return bool |
||
148 | */ |
||
149 | public function returnJson(JsonResponse $jsonResponse) |
||
150 | { |
||
151 | $this->response->header('Content-type', 'application/json; charset=utf-8'); |
||
152 | |||
153 | try { |
||
154 | $this->response->body(self::getJson($jsonResponse)); |
||
155 | } catch (SPException $e) { |
||
156 | $jsonResponse = new JsonResponse($e->getMessage()); |
||
157 | $jsonResponse->addMessage($e->getHint()); |
||
158 | |||
159 | $this->response->body(json_encode($jsonResponse)); |
||
160 | } |
||
161 | |||
162 | return $this->response->send(true)->isSent(); |
||
163 | } |
||
164 | |||
165 | /** |
||
166 | * Devuelve una cadena en formato JSON |
||
167 | * |
||
168 | * @param mixed $data |
||
169 | * @param int $flags JSON_* flags |
||
170 | * |
||
171 | * @return string La cadena en formato JSON |
||
172 | * @throws SPException |
||
173 | */ |
||
174 | public static function getJson($data, $flags = 0) |
||
175 | { |
||
176 | $json = json_encode($data, JSON_PARTIAL_OUTPUT_ON_ERROR | $flags); |
||
177 | |||
178 | if ($json === false || json_last_error() !== JSON_ERROR_NONE) { |
||
179 | throw new SPException( |
||
180 | __u('Encoding error'), |
||
181 | SPException::CRITICAL, |
||
182 | json_last_error_msg() |
||
183 | ); |
||
184 | } |
||
185 | |||
186 | return $json; |
||
187 | } |
||
188 | } |