Skip to content

Commit fc8df92

Browse files
authored
Merge pull request #21 from josephtingiris/master
multiple patches
2 parents 4a09519 + 6710d2a commit fc8df92

File tree

1 file changed

+112
-106
lines changed

1 file changed

+112
-106
lines changed

php-client/class.phpipam-api.php

+112-106
Original file line numberDiff line numberDiff line change
@@ -146,24 +146,24 @@ class phpipam_api_client {
146146
* @access public
147147
*/
148148
public $error_codes = array(
149-
// OK
150-
200 => "OK",
151-
201 => "Created",
152-
202 => "Accepted",
153-
204 => "No Content",
154-
// Client errors
155-
400 => "Bad Request",
156-
401 => "Unauthorized",
157-
403 => "Forbidden",
158-
404 => "Not Found",
159-
405 => "Method Not Allowed",
160-
415 => "Unsupported Media Type",
161-
// Server errors
162-
500 => "Internal Server Error",
163-
501 => "Not Implemented",
164-
503 => "Service Unavailable",
165-
505 => "HTTP Version Not Supported",
166-
511 => "Network Authentication Required"
149+
// OK
150+
200 => "OK",
151+
201 => "Created",
152+
202 => "Accepted",
153+
204 => "No Content",
154+
// Client errors
155+
400 => "Bad Request",
156+
401 => "Unauthorized",
157+
403 => "Forbidden",
158+
404 => "Not Found",
159+
405 => "Method Not Allowed",
160+
415 => "Unsupported Media Type",
161+
// Server errors
162+
500 => "Internal Server Error",
163+
501 => "Not Implemented",
164+
503 => "Service Unavailable",
165+
505 => "HTTP Version Not Supported",
166+
511 => "Network Authentication Required"
167167
);
168168

169169
/**
@@ -255,15 +255,15 @@ public function __construct($api_url = false, $app_id = false, $api_key = false,
255255
*/
256256
public function exception ($content) {
257257
//set result parameters
258-
$this->result = array(
259-
'code' => 400,
260-
'success' => false,
261-
'message' => $content
262-
);
258+
$this->result = array(
259+
'code' => 400,
260+
'success' => false,
261+
'message' => $content
262+
);
263263
// print result
264264
$this->print_result ();
265-
// die
266-
die();
265+
// die
266+
die();
267267
}
268268

269269
/**
@@ -284,12 +284,12 @@ public function get_result () {
284284
return (object) $this->result;
285285
}
286286
elseif ($this->result_format=="xml") {
287-
// new SimpleXMLElement object
288-
$xml = new SimpleXMLElement('<'.$_GET['controller'].'/>');
289-
// generate xml from result
290-
$this->array_to_xml($xml, $this->result);
291-
// return XML result
292-
return $xml->asXML();
287+
// new SimpleXMLElement object
288+
$xml = new SimpleXMLElement('<'.$_GET['controller'].'/>');
289+
// generate xml from result
290+
$this->array_to_xml($xml, $this->result);
291+
// return XML result
292+
return $xml->asXML();
293293
}
294294
}
295295

@@ -311,43 +311,43 @@ public function print_result () {
311311
var_dump( (object) $this->result);
312312
}
313313
elseif ($this->result_format=="xml") {
314-
// new SimpleXMLElement object
315-
$xml = new SimpleXMLElement('<apiclient/>');
316-
// generate xml from result
317-
$this->array_to_xml($xml, $this->result);
318-
// return XML result
319-
print $xml->asXML();
314+
// new SimpleXMLElement object
315+
$xml = new SimpleXMLElement('<apiclient/>');
316+
// generate xml from result
317+
$this->array_to_xml($xml, $this->result);
318+
// return XML result
319+
print $xml->asXML();
320320
}
321321
}
322322

323-
/**
324-
* Transforms array to XML
325-
*
326-
* @access private
327-
* @param SimpleXMLElement $object
328-
* @param array $data
329-
* @return void
330-
*/
331-
private function array_to_xml(SimpleXMLElement $object, array $data) {
332-
// loop through values
333-
foreach ($data as $key => $value) {
334-
// if spaces exist in key replace them with underscores
335-
if(strpos($key, " ")>0) { $key = str_replace(" ", "_", $key); }
336-
337-
// if key is numeric append item
338-
if(is_numeric($key)) $key = "item".$key;
339-
340-
// if array add child
341-
if (is_array($value)) {
342-
$new_object = $object->addChild($key);
343-
$this->array_to_xml($new_object, $value);
344-
}
345-
// else write value
346-
else {
347-
$object->addChild($key, $value);
348-
}
349-
}
350-
}
323+
/**
324+
* Transforms array to XML
325+
*
326+
* @access private
327+
* @param SimpleXMLElement $object
328+
* @param array $data
329+
* @return void
330+
*/
331+
private function array_to_xml(SimpleXMLElement $object, array $data) {
332+
// loop through values
333+
foreach ($data as $key => $value) {
334+
// if spaces exist in key replace them with underscores
335+
if(strpos($key, " ")>0) { $key = str_replace(" ", "_", $key); }
336+
337+
// if key is numeric append item
338+
if(is_numeric($key)) $key = "item".$key;
339+
340+
// if array add child
341+
if (is_array($value)) {
342+
$new_object = $object->addChild($key);
343+
$this->array_to_xml($new_object, $value);
344+
}
345+
// else write value
346+
else {
347+
$object->addChild($key, $value);
348+
}
349+
}
350+
}
351351

352352
/**
353353
* Check if all extensions are present
@@ -360,13 +360,13 @@ private function validate_php_extensions () {
360360
$required_ext = array("openssl", "curl");
361361
// mcrypt for crypted extensions
362362
if($this->api_key !== false)
363-
$required_ext[] = "mcrypt";
363+
$required_ext[] = "mcrypt";
364364
// json
365365
if($this->result_format == "json")
366-
$required_ext[] = "json";
366+
$required_ext[] = "json";
367367
// xml
368368
if($this->result_format == "xml")
369-
$required_ext[] = "xmlreader";
369+
$required_ext[] = "xmlreader";
370370

371371
// Available extensions
372372
$available_ext = get_loaded_extensions();
@@ -444,13 +444,13 @@ public function set_api_url ($api_url) {
444444
*/
445445
public function set_api_app_id ($app_id = false) {
446446
if ($app_id!==false) {
447-
// name must be more than 2 and alphanumberic
448-
if(strlen($app_id)<3 || strlen($app_id)>12 || !ctype_alnum($app_id)) {
449-
$this->exception("Invalid APP id");
450-
}
451-
else {
447+
// name must be more than 2 and alphanumberic
448+
if(strlen($app_id)<3 || strlen($app_id)>12 || !ctype_alnum($app_id)) {
449+
$this->exception("Invalid APP id");
450+
}
451+
else {
452452
$this->api_app_id = $app_id;
453-
}
453+
}
454454
}
455455
else {
456456
$this->exception("Invalid APP id");
@@ -617,7 +617,7 @@ public function execute ($method = false, $controller = false, $identifiers = ar
617617
$this->delete_token_file ($token_file);
618618
// auth again
619619
$this->curl_add_token_header ($token_file);
620-
// execute
620+
// execute
621621
$res = $this->curl_execute ();
622622
// save result
623623
$this->result = (array) $res;
@@ -653,20 +653,20 @@ private function curl_set_connection ($token_file) {
653653

654654
// set default curl options and params
655655
curl_setopt_array($this->Connection, array(
656-
CURLOPT_RETURNTRANSFER => 1,
657-
CURLOPT_URL => $url,
658-
CURLOPT_HEADER => 0,
659-
CURLOPT_VERBOSE => $this->debug,
660-
CURLOPT_TIMEOUT => 30,
661-
CURLOPT_HTTPHEADER => array("Content-Type: application/json"),
662-
CURLOPT_USERAGENT => 'phpipam-api php class',
663-
// ssl
664-
CURLOPT_SSL_VERIFYHOST => false,
665-
CURLOPT_SSL_VERIFYPEER => false,
666-
// save headers
667-
CURLINFO_HEADER_OUT => true
668-
)
669-
);
656+
CURLOPT_RETURNTRANSFER => 1,
657+
CURLOPT_URL => $url,
658+
CURLOPT_HEADER => 0,
659+
CURLOPT_VERBOSE => $this->debug,
660+
CURLOPT_TIMEOUT => 30,
661+
CURLOPT_HTTPHEADER => array("Content-Type: application/json"),
662+
CURLOPT_USERAGENT => 'phpipam-api php class',
663+
// ssl
664+
CURLOPT_SSL_VERIFYHOST => false,
665+
CURLOPT_SSL_VERIFYPEER => false,
666+
// save headers
667+
CURLINFO_HEADER_OUT => true
668+
)
669+
);
670670
}
671671
}
672672

@@ -681,7 +681,10 @@ private function curl_set_params ($params) {
681681
// params set ?
682682
if (is_array($params) && !$this->api_encrypt ) {
683683
if (sizeof($params)>0) {
684-
curl_setopt($this->Connection, CURLOPT_URL, $this->api_url.$this->api_app_id.str_replace("//", "/", "/".$this->api_server_controller."/".$this->api_server_identifiers."/?".http_build_query($params)));
684+
if ($this->api_server_method === 'GET')
685+
curl_setopt($this->Connection, CURLOPT_URL, $this->api_url.$this->api_app_id.str_replace("//", "/", "/".$this->api_server_controller."/".$this->api_server_identifiers."/?".http_build_query($params)));
686+
else
687+
curl_setopt($this->Connection, CURLOPT_POSTFIELDS, json_encode($params));
685688
}
686689
}
687690
// encrypt
@@ -730,8 +733,8 @@ private function curl_add_token_header ($token_file) {
730733
$token = @file($token_file);
731734
// save token
732735
if(isset($token[0])) {
733-
$this->token = $token[0];
734-
$this->token_expires = $token[1];
736+
$this->token = trim($token[0]);
737+
$this->token_expires = trim($token[1]);
735738

736739
// is token still valid ?
737740
if (strlen($this->token)<2 && $this->token_expires < time()) {
@@ -792,7 +795,7 @@ private function write_token_file ($filename) {
792795
fclose($myfile);
793796
}
794797
catch ( Exception $e ) {
795-
$this->exception("Cannot write file $filename");
798+
$this->exception("Cannot write file $filename");
796799
}
797800
}
798801

@@ -812,7 +815,7 @@ private function delete_token_file ($token_file) {
812815
fclose($myfile);
813816
}
814817
catch ( Exception $e ) {
815-
$this->exception("Cannot write file $token_file");
818+
$this->exception("Cannot write file $token_file");
816819
}
817820
}
818821

@@ -859,19 +862,22 @@ private function curl_authenticate () {
859862

860863
// set default curl options and params
861864
curl_setopt_array($c_auth, array(
862-
CURLOPT_RETURNTRANSFER => 1,
863-
CURLOPT_URL => $this->api_url.$this->api_app_id."/user/",
864-
CURLOPT_HEADER => 0,
865-
CURLOPT_VERBOSE => $this->debug,
866-
CURLOPT_TIMEOUT => 30,
867-
CURLOPT_USERAGENT => 'phpipam-api php class',
868-
// ssl
869-
CURLOPT_SSL_VERIFYHOST => 0,
870-
CURLOPT_SSL_VERIFYPEER => 0,
871-
CURLOPT_POST => true,
872-
CURLOPT_HTTPHEADER => array('Authorization: Basic '. base64_encode($this->api_username.":".$this->api_password))
865+
CURLOPT_RETURNTRANSFER => 1,
866+
CURLOPT_URL => $this->api_url.$this->api_app_id."/user/",
867+
CURLOPT_HEADER => 0,
868+
CURLOPT_VERBOSE => $this->debug,
869+
CURLOPT_TIMEOUT => 30,
870+
CURLOPT_USERAGENT => 'phpipam-api php class',
871+
// ssl
872+
CURLOPT_SSL_VERIFYHOST => 0,
873+
CURLOPT_SSL_VERIFYPEER => 0,
874+
CURLOPT_POST => true,
875+
CURLOPT_HTTPHEADER => array(
876+
'Content-Length: 0',
877+
'Authorization: Basic '. base64_encode($this->api_username.":".$this->api_password)
873878
)
874-
);
879+
)
880+
);
875881
// send request and save response
876882
$resp = curl_exec($c_auth);
877883

0 commit comments

Comments
 (0)