Skip to content

Commit 1353578

Browse files
authored
Refactoring abstract, config and provider
1 parent 05b223b commit 1353578

File tree

1 file changed

+60
-83
lines changed

1 file changed

+60
-83
lines changed

src/ZabbixApiAbstract.php

Lines changed: 60 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -16,144 +16,112 @@ abstract class ZabbixApiAbstract
1616
* @brief Anonymous API functions.
1717
*/
1818

19-
static private $anonymousFunctions = array(
19+
static protected $anonymousFunctions = [
2020
'apiinfo.version'
21-
);
21+
];
2222

2323
/**
2424
* @brief Boolean if requests/responses should be printed out (JSON).
2525
*/
2626

27-
private $printCommunication = FALSE;
27+
protected $printCommunication = false;
2828

2929
/**
3030
* @brief API URL.
3131
*/
3232

33-
private $apiUrl = '';
33+
protected $apiUrl;
3434

3535
/**
3636
* @brief Default params.
3737
*/
3838

39-
private $defaultParams = array();
39+
protected $defaultParams = [];
4040

4141
/**
4242
* @brief Auth string.
4343
*/
4444

45-
private $auth = '';
45+
protected $auth;
4646

4747
/**
4848
* @brief Request ID.
4949
*/
5050

51-
private $id = 0;
51+
protected $id = 0;
5252

5353
/**
5454
* @brief Request array.
5555
*/
5656

57-
private $request = array();
57+
protected $request = [];
5858

5959
/**
6060
* @brief JSON encoded request string.
6161
*/
6262

63-
private $requestEncoded = '';
63+
protected $requestEncoded;
6464

6565
/**
6666
* @brief JSON decoded response string.
6767
*/
6868

69-
private $response = '';
69+
protected $response;
7070

7171
/**
7272
* @brief Response object.
7373
*/
7474

75-
private $responseDecoded = NULL;
75+
protected $responseDecoded = null;
7676

7777
/**
7878
* @brief Extra HTTP headers.
7979
*/
8080

81-
private $extraHeaders = '';
81+
protected $extraHeaders;
8282

8383
/**
8484
* @brief SSL context.
8585
*/
8686

87-
private $sslContext = array();
87+
protected $sslContext = [];
88+
89+
/**
90+
* @brief Verify SSL certificate
91+
*/
92+
93+
protected $verifySsl;
8894

8995
/**
9096
* @brief Class constructor.
9197
*
92-
* @param $apiUrl API url (e.g. http://FQDN/zabbix/api_jsonrpc.php)
98+
* @param $apiUrl API url (e.g. http://FQDN/zabbix/api_jsonrpc.php).
9399
* @param $user Username for Zabbix API.
94100
* @param $password Password for Zabbix API.
95101
* @param $httpUser Username for HTTP basic authorization.
96102
* @param $httpPassword Password for HTTP basic authorization.
97-
* @param $authToken Already issued auth token (e.g. extracted from cookies)
98-
* @param $sslContext SSL context for SSL-enabled connections
103+
* @param $authToken Already issued auth token (e.g. extracted from cookies).
104+
* @param $sslContext SSL context for SSL-enabled connections.
105+
* @param $verifySsl Verify SSL peer name.
99106
*/
100107

101-
public function __construct($apiUrl='', $user='', $password='', $httpUser='', $httpPassword='', $authToken='', $sslContext=NULL)
108+
public function __construct($apiUrl, $user, $password, $httpUser, $httpPassword, $authToken, $sslContext, $verifySsl)
102109
{
103-
if($apiUrl)
104-
$this->setApiUrl($apiUrl);
110+
$this->apiUrl = $apiUrl;
111+
112+
$this->sslContext = $sslContext;
113+
114+
$this->verifySsl = $verifySsl;
105115

106116
if ($httpUser && $httpPassword)
107117
$this->setBasicAuthorization($httpUser, $httpPassword);
108118

109-
if($sslContext)
110-
$this->setSslContext($sslContext);
111-
112119
if ($authToken)
113-
$this->setAuthToken($authToken);
120+
$this->authToken = $authToken;
114121
elseif($user && $password)
115122
$this->userLogin(array('user' => $user, 'password' => $password));
116123
}
117124

118-
/**
119-
* @brief Returns the API url for all requests.
120-
*
121-
* @retval string API url.
122-
*/
123-
124-
public function getApiUrl()
125-
{
126-
return $this->apiUrl;
127-
}
128-
129-
/**
130-
* @brief Sets the API url for all requests.
131-
*
132-
* @param $apiUrl API url.
133-
*
134-
* @retval ZabbixApiAbstract
135-
*/
136-
137-
public function setApiUrl($apiUrl)
138-
{
139-
$this->apiUrl = $apiUrl;
140-
return $this;
141-
}
142-
143-
/**
144-
* @brief Sets the API authorization ID.
145-
*
146-
* @param $authToken API auth ID.
147-
*
148-
* @retval ZabbixApiAbstract
149-
*/
150-
151-
public function setAuthToken($authToken)
152-
{
153-
$this->authToken = $authToken;
154-
return $this;
155-
}
156-
157125
/**
158126
* @brief Sets the username and password for the HTTP basic authorization.
159127
*
@@ -173,22 +141,6 @@ public function setBasicAuthorization($user, $password)
173141
return $this;
174142
}
175143

176-
/**
177-
* @brief Sets the context for SSL-enabled connections.
178-
*
179-
* See http://php.net/manual/en/context.ssl.php for more informations.
180-
*
181-
* @param $context Array with the SSL context
182-
*
183-
* @retval ZabbixApiAbstract
184-
*/
185-
186-
public function setSslContext($context)
187-
{
188-
$this->sslContext = $context;
189-
return $this;
190-
}
191-
192144
/**
193145
* @brief Returns the default params.
194146
*
@@ -282,16 +234,29 @@ public function request($method, $params=NULL, $resultArrayKey='', $auth=TRUE)
282234
'content' => $this->requestEncoded
283235
)
284236
);
237+
238+
if($this->isSecure($this->apiUrl)) {
239+
240+
$ssl = array(
241+
'ssl' => array(
242+
'verify_peer' => $this->verifySsl
243+
)
244+
);
245+
246+
$context = array_merge($context, $ssl);
247+
248+
}
249+
285250
if($this->sslContext)
286251
$context['ssl'] = $this->sslContext;
287252

288253
// create stream context
289254
$streamContext = stream_context_create($context);
290255

291256
// get file handler
292-
$fileHandler = @fopen($this->getApiUrl(), 'rb', false, $streamContext);
257+
$fileHandler = @fopen($this->apiUrl, 'rb', false, $streamContext);
293258
if(!$fileHandler)
294-
throw new Exception('Could not connect to "'.$this->getApiUrl().'"');
259+
throw new Exception('Could not connect to "'.$this->apiUrl.'"');
295260

296261
// get response
297262
$this->response = @stream_get_contents($fileHandler);
@@ -302,7 +267,7 @@ public function request($method, $params=NULL, $resultArrayKey='', $auth=TRUE)
302267

303268
// response verification
304269
if($this->response === FALSE)
305-
throw new Exception('Could not read data from "'.$this->getApiUrl().'"');
270+
throw new Exception('Could not read data from "'.$this->apiUrl.'"');
306271

307272
// decode response
308273
$this->responseDecoded = json_decode($this->response);
@@ -320,6 +285,18 @@ public function request($method, $params=NULL, $resultArrayKey='', $auth=TRUE)
320285
return $this->responseDecoded->result;
321286
}
322287

288+
/**
289+
* @brief Verify if requested URL is made by HTTPS
290+
*
291+
* @retval boolean
292+
*/
293+
protected function isSecure($url)
294+
{
295+
$url = parse_url($url);
296+
297+
return $url['scheme'] == 'https';
298+
}
299+
323300
/**
324301
* @brief Returns the last JSON API request.
325302
*
@@ -351,7 +328,7 @@ public function getResponse()
351328
* @retval associative Array
352329
*/
353330

354-
private function convertToAssociatveArray($objectArray, $useObjectProperty)
331+
protected function convertToAssociatveArray($objectArray, $useObjectProperty)
355332
{
356333
// sanity check
357334
if(count($objectArray) == 0 || !property_exists($objectArray[0], $useObjectProperty))
@@ -392,7 +369,7 @@ private function convertToAssociatveArray($objectArray, $useObjectProperty)
392369
* @retval Array
393370
*/
394371

395-
private function getRequestParamsArray($params)
372+
protected function getRequestParamsArray($params)
396373
{
397374
// if params is a scalar value, turn it into an array
398375
if(is_scalar($params))

0 commit comments

Comments
 (0)