Skip to content

Commit 6710d2a

Browse files
replace random tabs with (4) spaces, add Content-Length header to authorization (fixes apache 2.4 sending 400 for Content-Length: -1), trim newline in token (fixes nginx sending 400 for CR/LF in token), if not GET then add json_encode($params) to CURLOPT_POSTFIELDS
1 parent a695f40 commit 6710d2a

File tree

1 file changed

+112
-106
lines changed

1 file changed

+112
-106
lines changed

php-client/class.phpipam-api.php

Lines changed: 112 additions & 106 deletions
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
@@ -728,8 +731,8 @@ private function curl_add_token_header ($token_file) {
728731
$token = @file($token_file);
729732
// save token
730733
if(isset($token[0])) {
731-
$this->token = $token[0];
732-
$this->token_expires = $token[1];
734+
$this->token = trim($token[0]);
735+
$this->token_expires = trim($token[1]);
733736

734737
// is token still valid ?
735738
if (strlen($this->token)<2 && $this->token_expires < time()) {
@@ -790,7 +793,7 @@ private function write_token_file ($filename) {
790793
fclose($myfile);
791794
}
792795
catch ( Exception $e ) {
793-
$this->exception("Cannot write file $filename");
796+
$this->exception("Cannot write file $filename");
794797
}
795798
}
796799

@@ -810,7 +813,7 @@ private function delete_token_file ($token_file) {
810813
fclose($myfile);
811814
}
812815
catch ( Exception $e ) {
813-
$this->exception("Cannot write file $token_file");
816+
$this->exception("Cannot write file $token_file");
814817
}
815818
}
816819

@@ -857,19 +860,22 @@ private function curl_authenticate () {
857860

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

0 commit comments

Comments
 (0)