@@ -146,24 +146,24 @@ class phpipam_api_client {
146
146
* @access public
147
147
*/
148
148
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 "
167
167
);
168
168
169
169
/**
@@ -255,15 +255,15 @@ public function __construct($api_url = false, $app_id = false, $api_key = false,
255
255
*/
256
256
public function exception ($ content ) {
257
257
//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
+ );
263
263
// print result
264
264
$ this ->print_result ();
265
- // die
266
- die ();
265
+ // die
266
+ die ();
267
267
}
268
268
269
269
/**
@@ -284,12 +284,12 @@ public function get_result () {
284
284
return (object ) $ this ->result ;
285
285
}
286
286
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 ();
293
293
}
294
294
}
295
295
@@ -311,43 +311,43 @@ public function print_result () {
311
311
var_dump ( (object ) $ this ->result );
312
312
}
313
313
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 ();
320
320
}
321
321
}
322
322
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
+ }
351
351
352
352
/**
353
353
* Check if all extensions are present
@@ -360,13 +360,13 @@ private function validate_php_extensions () {
360
360
$ required_ext = array ("openssl " , "curl " );
361
361
// mcrypt for crypted extensions
362
362
if ($ this ->api_key !== false )
363
- $ required_ext [] = "mcrypt " ;
363
+ $ required_ext [] = "mcrypt " ;
364
364
// json
365
365
if ($ this ->result_format == "json " )
366
- $ required_ext [] = "json " ;
366
+ $ required_ext [] = "json " ;
367
367
// xml
368
368
if ($ this ->result_format == "xml " )
369
- $ required_ext [] = "xmlreader " ;
369
+ $ required_ext [] = "xmlreader " ;
370
370
371
371
// Available extensions
372
372
$ available_ext = get_loaded_extensions ();
@@ -444,13 +444,13 @@ public function set_api_url ($api_url) {
444
444
*/
445
445
public function set_api_app_id ($ app_id = false ) {
446
446
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 {
452
452
$ this ->api_app_id = $ app_id ;
453
- }
453
+ }
454
454
}
455
455
else {
456
456
$ this ->exception ("Invalid APP id " );
@@ -617,7 +617,7 @@ public function execute ($method = false, $controller = false, $identifiers = ar
617
617
$ this ->delete_token_file ($ token_file );
618
618
// auth again
619
619
$ this ->curl_add_token_header ($ token_file );
620
- // execute
620
+ // execute
621
621
$ res = $ this ->curl_execute ();
622
622
// save result
623
623
$ this ->result = (array ) $ res ;
@@ -653,20 +653,20 @@ private function curl_set_connection ($token_file) {
653
653
654
654
// set default curl options and params
655
655
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
+ );
670
670
}
671
671
}
672
672
@@ -681,7 +681,10 @@ private function curl_set_params ($params) {
681
681
// params set ?
682
682
if (is_array ($ params ) && !$ this ->api_encrypt ) {
683
683
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 ));
685
688
}
686
689
}
687
690
// encrypt
@@ -730,8 +733,8 @@ private function curl_add_token_header ($token_file) {
730
733
$ token = @file ($ token_file );
731
734
// save token
732
735
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 ]) ;
735
738
736
739
// is token still valid ?
737
740
if (strlen ($ this ->token )<2 && $ this ->token_expires < time ()) {
@@ -792,7 +795,7 @@ private function write_token_file ($filename) {
792
795
fclose ($ myfile );
793
796
}
794
797
catch ( Exception $ e ) {
795
- $ this ->exception ("Cannot write file $ filename " );
798
+ $ this ->exception ("Cannot write file $ filename " );
796
799
}
797
800
}
798
801
@@ -812,7 +815,7 @@ private function delete_token_file ($token_file) {
812
815
fclose ($ myfile );
813
816
}
814
817
catch ( Exception $ e ) {
815
- $ this ->exception ("Cannot write file $ token_file " );
818
+ $ this ->exception ("Cannot write file $ token_file " );
816
819
}
817
820
}
818
821
@@ -859,19 +862,22 @@ private function curl_authenticate () {
859
862
860
863
// set default curl options and params
861
864
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 )
873
878
)
874
- );
879
+ )
880
+ );
875
881
// send request and save response
876
882
$ resp = curl_exec ($ c_auth );
877
883
0 commit comments