File: CJsonRpc.php

package info (click to toggle)
zabbix 1%3A7.0.10%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 272,688 kB
  • sloc: sql: 946,050; ansic: 389,440; php: 292,698; javascript: 83,388; sh: 5,680; makefile: 3,285; java: 1,420; cpp: 694; perl: 64; xml: 56
file content (243 lines) | stat: -rw-r--r-- 6,745 bytes parent folder | download | duplicates (2)
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
<?php
/*
** Copyright (C) 2001-2025 Zabbix SIA
**
** This program is free software: you can redistribute it and/or modify it under the terms of
** the GNU Affero General Public License as published by the Free Software Foundation, version 3.
**
** This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
** without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
** See the GNU Affero General Public License for more details.
**
** You should have received a copy of the GNU Affero General Public License along with this program.
** If not, see <https://www.gnu.org/licenses/>.
**/


class CJsonRpc {

	const VERSION = '2.0';

	public const AUTH_TYPE_FRONTEND = 0;
	public const AUTH_TYPE_PARAM = 1;
	public const AUTH_TYPE_HEADER = 2;
	public const AUTH_TYPE_COOKIE = 3;

	/**
	 * API client to use for making requests.
	 *
	 * @var CApiClient
	 */
	protected $apiClient;

	private $_response;
	private $_error_list;
	private $_zbx2jsonErrors;
	private $_jsonDecoded;

	/**
	 * Constructor.
	 *
	 * @param CApiClient $apiClient
	 * @param string $data
	 */
	public function __construct(CApiClient $apiClient, $data) {
		$this->apiClient = $apiClient;

		$this->initErrors();

		$this->_response = [];
		$this->_jsonDecoded = json_decode($data, true);
	}

	/**
	 * Executes API requests.
	 *
	 * @param CHttpRequest $request
	 *
	 * @return string JSON encoded value
	 */
	public function execute(CHttpRequest $request) {
		if (json_last_error()) {
			$this->jsonError([], '-32700', null, null, true);
			return json_encode($this->_response[0], JSON_UNESCAPED_SLASHES);
		}

		if (!is_array($this->_jsonDecoded) || $this->_jsonDecoded === []) {
			$this->jsonError([], '-32600', null, null, true);
			return json_encode($this->_response[0], JSON_UNESCAPED_SLASHES);
		}

		foreach (zbx_toArray($this->_jsonDecoded) as $call) {
			if (!$this->validate($call)) {
				continue;
			}

			$auth = [
				'type' => self::AUTH_TYPE_PARAM,
				'auth' => $call['auth']
			];

			list($api, $method) = explode('.', $call['method']) + [1 => ''];

			$header = $request->getAuthBearerValue();
			if ($header != null) {
				$auth = [
					'type' => self::AUTH_TYPE_HEADER,
					'auth' => $header
				];
			}
			elseif ($call['auth'] === null) {
				$session = new CEncryptedCookieSession();

				$auth = [
					'type' => self::AUTH_TYPE_COOKIE,
					'auth' => $session->extractSessionId()
				];
			}

			$result = $this->apiClient->callMethod($api, $method, $call['params'], $auth);

			$this->processResult($call, $result);
		}

		if ($this->_response === array_fill(0, count($this->_response), null)) {
			return '';
		}

		if (is_array($this->_jsonDecoded)
				&& array_keys($this->_jsonDecoded) === range(0, count($this->_jsonDecoded) - 1)) {
			// Return response as encoded batch if $this->_jsonDecoded is associative array.
			return json_encode(array_values(array_filter($this->_response)), JSON_UNESCAPED_SLASHES);
		}

		return ($this->_response[0] !== null) ? json_encode($this->_response[0], JSON_UNESCAPED_SLASHES) : '';
	}

	public function validate(&$call) {
		$api_input_rules = ['type' => API_OBJECT, 'fields' => [
			'jsonrpc' =>	['type' => API_STRING_UTF8, 'flags' => API_REQUIRED, 'in' => self::VERSION],
			'method' =>		['type' => API_STRING_UTF8, 'flags' => API_REQUIRED],
			'params' =>		['type' => API_JSONRPC_PARAMS, 'flags' => API_REQUIRED],
			'auth' =>		['type' => API_STRING_UTF8, 'flags' => API_NOT_EMPTY | API_ALLOW_NULL | API_DEPRECATED, 'default' => null],
			'id' =>			['type' => API_JSONRPC_ID]
		]];

		if (!CApiInputValidator::validate($api_input_rules, $call, '/', $error)) {
			$call_id = is_array($call) ? array_intersect_key($call, array_flip(['id'])) : [];
			$api_input_rules = ['type' => API_OBJECT, 'fields' => [
				'id' =>	['type' => API_JSONRPC_ID]
			]];

			if (!CApiInputValidator::validate($api_input_rules, $call_id, '', $err)) {
				$call_id = [];
			}

			$this->jsonError($call_id, '-32600', $error, null, true);

			return false;
		}

		return true;
	}

	public function processResult(array $call, CApiClientResponse $response) {
		if ($response->errorCode) {
			$errno = $this->_zbx2jsonErrors[$response->errorCode];

			$this->jsonError($call, $errno, $response->errorMessage, $response->debug);
		}
		else {
			// Notifications (request object without an "id" member) MUST NOT be answered.
			$this->_response[] = array_key_exists('id', $call)
				? [
					'jsonrpc' => self::VERSION,
					'result' => $response->data,
					'id' => $call['id']
				]
				: null;
		}
	}

	private function jsonError(array $call, $errno, $data = null, $debug = null, $force_err = false) {
		// Notifications MUST NOT be answered, but error MUST be generated on JSON parse error
		if (!$force_err && !array_key_exists('id', $call)) {
			$this->_response[] = null;
			return;
		}

		if (!array_key_exists($errno, $this->_error_list)) {
			$data = _s('JSON-RPC error generation failed. No such error "%1$s".', $errno);
			$errno = '-32400';
		}

		$error = $this->_error_list[$errno];

		if ($data !== null) {
			$error['data'] = $data;
		}

		if ($debug !== null) {
			$error['debug'] = $debug;
		}

		$this->_response[] = [
			'jsonrpc' => self::VERSION,
			'error' => $error,
			'id' => array_key_exists('id', $call) ? $call['id'] : null
		];
	}

	private function initErrors() {
		$this->_error_list = [
			'-32700' => [
				'code' => -32700,
				'message' => _('Parse error'),
				'data' => _('Invalid JSON. An error occurred on the server while parsing the JSON text.')
			],
			'-32600' => [
				'code' => -32600,
				'message' => _('Invalid request.'),
				'data' => _('The received JSON is not a valid JSON-RPC request.')
			],
			'-32601' => [
				'code' => -32601,
				'message' => _('Method not found.'),
				'data' => _('The requested remote-procedure does not exist / is not available')
			],
			'-32602' => [
				'code' => -32602,
				'message' => _('Invalid params.'),
				'data' => _('Invalid method parameters.')
			],
			'-32603' => [
				'code' => -32603,
				'message' => _('Internal error.'),
				'data' => _('Internal JSON-RPC error.')
			],
			'-32500' => [
				'code' => -32500,
				'message' => _('Application error.'),
				'data' => _('No details')
			],
			'-32400' => [
				'code' => -32400,
				'message' => _('System error.'),
				'data' => _('No details')
			],
			'-32300' => [
				'code' => -32300,
				'message' => _('Transport error.'),
				'data' => _('No details')
			]
		];

		$this->_zbx2jsonErrors = [
			ZBX_API_ERROR_NO_METHOD => '-32601',
			ZBX_API_ERROR_PARAMETERS => '-32602',
			ZBX_API_ERROR_NO_AUTH => '-32602',
			ZBX_API_ERROR_PERMISSIONS => '-32500',
			ZBX_API_ERROR_INTERNAL => '-32500'
		];
	}
}