forked from virtuozzo/OnApp-PHP-Wrapper-External
-
Notifications
You must be signed in to change notification settings - Fork 0
/
OnApp.php
1713 lines (1528 loc) · 40.5 KB
/
OnApp.php
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* Current OnApp PHP API wrapper version
*/
define( 'ONAPP_VERSION', '2.0' );
/**
* The OnApp class uses this variable to define the Proxy server used by cURL
*/
define( 'ONAPP_OPTION_CURL_PROXY', 'proxy' );
/**
* The OnApp class uses this variable to define the URL to the API used by cURL
*/
define( 'ONAPP_OPTION_CURL_URL', 'url' );
/**
* The OnApp class uses this variable to define the data type which would help transfer data between the client and the API server
*
* Possible values:
* - xml (default)
* - json (will be available after the parcer is created)
*/
define( 'ONAPP_OPTION_API_TYPE', 'data_type' );
/**
* The OnApp class uses this variable to define the charsets used to transfer data between the client and the API server
*
* Possible values:
* - charset=utf-8 (default)
*/
define( 'ONAPP_OPTION_API_CHARSET', 'charset' );
/**
* The OnApp class uses this value to define the content type used to transfer data between the client and the API server
*
* Possible values:
* - application/xml (default)
* - application/json (will be available after the Json parcer is created)
*/
define( 'ONAPP_OPTION_API_CONTENT', 'content' );
/**
* TODO add description
*/
define( 'ONAPP_OPTION_DEBUG_MODE', 'debug_mode' );
/**
* The OnApp class uses this field name to map this field in the API response and variable in the class
* The field name is used to unserialize the API server response to the necessary class.
*/
define( 'ONAPP_FIELD_MAP', 'map' );
/**
* The field name that stands for the mapped field type in the API response
*
* The field is used to unserialize the object into API request.
* Possible values:
* - integer
* - ...
*/
define( 'ONAPP_FIELD_TYPE', 'type' );
/**
* The field name that stands for the field access in the API response
*
* Used to unserialize the object into API request.
* Possible values:
* - true
* - false
*/
define( 'ONAPP_FIELD_READ_ONLY', 'read_only' );
/**
* The flag to exclude this field from sending request
*
* Possible values:
* - true
* - false
*/
define( 'ONAPP_FIELD_SKIP_FROM_REQUEST', 'skip' );
/**
* The field name that specifies if it is necessary to be used in the API request when new objects are created or existing edited
*
* Possible values:
* - true
* - false
*/
define( 'ONAPP_FIELD_REQUIRED', 'required' );
/**
* The field name that stands for the default field value
*
* The field name is used to unserialize if the field was changed or not loaded.
*/
define( 'ONAPP_FIELD_DEFAULT_VALUE', 'default' );
/**
* Specify field type to serialize and unserialize obgect using their name
*/
define( 'ONAPP_FIELD_CLASS', 'class' );
/**
*
*/
define( 'ONAPP_GETRESOURCE_DEFAULT', 'default' );
/**
*
*/
define( 'ONAPP_GETRESOURCE_LOAD', 'load' );
/**
*
*/
define( 'ONAPP_GETRESOURCE_LIST', 'list' );
/**
*
*
*/
define( 'ONAPP_GETRESOURCE_ADD', 'add' );
/**
*
*
*/
define( 'ONAPP_GETRESOURCE_EDIT', 'edit' );
/**
*
*
*/
define( 'ONAPP_GETRESOURCE_DELETE', 'delete' );
/**
*
*
*/
define( 'ONAPP_GETRESOURCE_VERSION', 'version' );
/**
*
*
*/
define( 'ONAPP_ACTIVATE_GETLIST', 'getList' );
/**
*
*
*/
define( 'ONAPP_ACTIVATE_LOAD', 'load' );
/**
*
*
*/
define( 'ONAPP_ACTIVATE_SAVE', 'save' );
/**
*
*
*/
define( 'ONAPP_ACTIVATE_DELETE', 'delete' );
/**
* Specify the GET request
*
*/
define( 'ONAPP_REQUEST_METHOD_GET', 'GET' );
/**
* Specify the POST request
*
*/
define( 'ONAPP_REQUEST_METHOD_POST', 'POST' );
/**
* Specify the PUT request
*
*/
define( 'ONAPP_REQUEST_METHOD_PUT', 'PUT' );
/**
* Specify the DELETE request
*
*/
define( 'ONAPP_REQUEST_METHOD_DELETE', 'DELETE' );
/**
* API Wrapper for OnApp
*
*
* @package OnApp
*
*
* @todo Pack using the lib (http://pecl.php.net/)
*
* The wrapper is used to describe the following basic methods: {@link load},
* {@link save}, {@link delete} and {@link getList}.
*
* To create a new class inheriting this one, re-define the
* following variables:
* <code>
*
* // root tag used in the API request
* var $_tagRoot = '<root>';
*
* // alias processing the object data
* var $_resource = '<alias>';
*
* // the fields array used in the response and request to the API server
* var $fields = array(
* ...
* )
* </code>
*
* To create a read-only class, close the save and delete methods.
* To re-define the traditional API aliases to the non-traditional,
* re-define the {@link getResource}, {@link getResourceADD}, {@link getResourceEDIT},
* {@link getResourceLOAD}, {@link getResourceDELETE} and {@link getResourceLIST}
* methods in the class that will be inheriting the ONAPP class.
*
*
* This API provides an interface to onapp.net allowing common virtual machine
* and account management tasks
*
* <b> Usage OnApp_VirtualMachine class example ( Could be applied almost to any of the Wrapper classes ): </b> <br /><br />
* <b> Important ( OnApp CP Permissions Set Up): </b>
* <code>
* Go to OnApp CP.
* Users and Groups -> Roles
* Push pencil edit icon to edit role of the user which you are going to use.
* Check checkbox { View OnApp version (settings.version) }
* Check other permissions in order to perform particular actions.
*</code>
*
* <b>Code example:</b> <br />
*
* Require Wrapper AutoLoad Class:
*
* <code>
* require_once '{Path to the Wrapper}/OnAppInit.php';
* </code>
*
*
* Get OnApp Instance:
*
* <code>
* $onapp = new OnApp_Factory('{IP Address / Hostname}', '{Username}', '{Password}');
* </code>
*
*
* Get OnApp_VirtualMachine Instance:
*
* <code>
* $vm_obj = $onapp->factory('VirtualMachine', {debug mode boolean, not required} );
* </code>
*
*
* Get the list of VMs:
*
* <code>
* $vms = $vm_obj->getList();
* </code>
*
*
* Get particular VM:
*
* <code>
* $vm = $vm_obj->load({VM ID});
* </code>
*
*
* Create a VM:
*
* <code>
* $vm_obj->_label = '{VM Label}';
* $vm_obj->_memory = {VM RAM };
* $vm_obj->_cpu_shares = {VM CPU priority};
* $vm_obj->_hostname = '{Hostname}';
* $vm_obj->_cpus = {number of VM CPUs};
* $vm_obj->_primary_disk_size = {VM Disk Space};
* $vm_obj->_swap_disk_size = {VM Swap Size};
* $vm_obj->_template_id = {VM Template ID};
* $vm_obj->_allowed_hot_migrate = {VM Hot Migrate Boolean Value};
*
* $vm_obj->save();
* </code>
*
*
* Edit VM:
*
* <code>
* $vm_obj->_id = {VM ID};
* $vm_obj->_{Field You Want To Edit} = {New Value};
*
* $vm_obj->save();
* </code>
*
*
* Getting debug log ( depends on debug mode ):
*
* <code>
* print_r( $vm_obj );
* </code>
*
* For full fields reference and curl request details visit: ( http://help.onapp.com/manual.php?m=2 )
*/
class OnApp {
/**
* The list of all available options used in the class to create API requests and receive responses,
* as well as to serialize and unserialize.
*
* @access private
* @var array
*/
var $_knownOptions = array(
ONAPP_OPTION_CURL_PROXY,
ONAPP_OPTION_CURL_URL,
ONAPP_OPTION_API_TYPE,
ONAPP_OPTION_API_CHARSET,
ONAPP_OPTION_API_CONTENT,
ONAPP_OPTION_DEBUG_MODE
);
/**
* Default options used in the class to create API requests and receive responses,
* as well as serialize and unserialize objects.
*
* @access private
* @var array
*/
private $defaultOptions = array(
// cURL proxy
ONAPP_OPTION_CURL_PROXY => '',
// cURL url
ONAPP_OPTION_CURL_URL => '',
// API request and response charset
ONAPP_OPTION_API_CHARSET => 'charset=utf-8',
// API request and response type
ONAPP_OPTION_API_TYPE => 'json',
// API request and response content
ONAPP_OPTION_API_CONTENT => 'application/json',
// Debug mode
ONAPP_OPTION_DEBUG_MODE => false,
);
/**
* The array of the options used to create API requests and receive responses,
* as well as serialize and unserialize objects in the class
*
* By default equals to $_defaultOptions
*
* <code>
* var $options = array(
*
* // cURL proxy
* ONAPP_OPTION_CURL_PROXY => '',
*
* // cURL url
* ONAPP_OPTION_CURL_URL => '',
*
* // API request and response type
* ONAPP_OPTION_API_TYPE => 'xml',
*
* // API request and response charset
* ONAPP_OPTION_API_CHARSET => 'charset=utf-8',
*
* // API request and response content
* ONAPP_OPTION_API_CONTENT => 'application/xml',
*
* // Debug mode
* ONAPP_OPTION_DEBUG_MODE => false
* );
* </code>
*
* @access public
* @var array
*/
var $options = array();
/**
* Object cURL
* PHP supports libcurl, a library created by Daniel Stenberg,
* that allows you to connect and communicate to many different types of servers with many different types of protocols.
* libcurl currently supports the http, https, ftp, gopher, telnet, dict, file and ldap protocols.
* libcurl also supports HTTPS certificates, HTTP POST, HTTP PUT, FTP uploading (this can also be done with PHP's ftp extension),
* HTTP form based upload, proxies, cookies and user+password authentication.
*
* @access private
* @var cURL
*/
var $_ch;
/**
* Variable storing the data loaded by the API request. The data is static and cannot be changed by the class setters
*
* @access private
* @var object
*/
var $_obj;
/**
* cURL Object alias used as the basic alias to the load, save, delete and getList methods
*
* @access private
* @var string
*/
var $_resource = null;
/**
* @access private
* @var string
*/
var $_tagRoot = null;
/**
* @access private
* @var array
*/
var $_tagRequired = null;
/**
* @access private
* @var boolean
* @todo move in to getter an setter
*/
var $_is_auth = false;
/**
* @access private
* @var boolean
* @todo move in to getter an setter
*/
var $_is_changed = false;
/**
* @access private
* @var boolean
* @todo move in to getter an setter
*/
var $_is_deleted = false;
/**
* Return OnApp version
*
* @access private
* @var sting
*/
protected $version;
/**
* Return OnApp release
*
* @access private
* @var sting
*/
var $_release;
/**
* Return OnApp fields array mapping
*
* @access private
* @var array
*/
protected $fields;
/**
* Holder for storing properties that were setted via magic setter
*
* @var array
* @access protected
*/
protected $dynamicFields;
/**
* The Object Logger used to log the processes in the basic and inherited classes
* It is possible to use the debug add error log methods
*/
public $logger = null;
/**
* Variable for error handling
*
* @var string
*/
protected $errors;
/**
* @var string current class' name
*/
protected $className;
/**
*
*
*/
protected $response;
/**
* Returns API version
*
* @access private
* @return string $version API version
*/
function _apiVersion() {
return $this->version;
}
/**
* Returns Curl Response
*
* @access public
* @return array response
*/
function getResponse() {
return $this->response;
}
/**
* Resets all options to default options
*
* @return void
* @access public
*/
function resetOptions() {
$this->options = $this->defaultOptions;
}
/**
* Sets an option
*
* Use this method if you do not want
* to set all options in the constructor
*
* @param string $name option name
* @param mixed $value option value
*
* @return void
* @access public
*/
function setOption( $name, $value ) {
$this->logger->debug( 'setOption: Set option ' . $name . ' => ' . $value );
$this->options[ $name ] = $value;
}
/**
* Sets several options at once
*
* Use this method if you do not want
* to set all options in the constructor
*
* @param array $options options array
*
* @return void
* @access public
*/
function setOptions( array $options ) {
$this->options = array_merge( $this->options, $options );
}
/**
* Returns the URL Alias of the API Class that inherits the OnApp class
*
* Can be redefined if the API does not use the default alias (the alias
* consisting of few fields).
* The following example illustrates:
*
* <code>
* function getResource() {
* return "alias/" . $this->_field_name . "/" . $this->_resource;
* }
* </code>
*
* @param string $action
*
* @return string API resource
*/
function getResource( $action = ONAPP_GETRESOURCE_DEFAULT ) {
switch( $action ) {
case ONAPP_GETRESOURCE_LOAD:
case ONAPP_GETRESOURCE_EDIT:
case ONAPP_GETRESOURCE_DELETE:
$resource = $this->getResource() . '/' . $this->_id;
break;
case ONAPP_GETRESOURCE_LIST:
case ONAPP_GETRESOURCE_ADD:
$resource = $this->getResource();
break;
case ONAPP_GETRESOURCE_DEFAULT:
default:
$resource = $this->_resource;
}
$this->logger->debug( 'getResource( ' . $action . ' ): return ' . $resource );
return $resource;
}
/**
* Returns true if the API instance has authentication information set and authentication was succesful
*
* @return boolean true if authenticated
* @access public
*
* @todo move to the defaut getter
*/
function isAuthenticate() {
return $this->_is_auth;
}
/**
* Returns true if the Object was changed and API response was succesfull
*
* @return boolean true if the Object was changed
* @access public
*
* @todo move to the defaut getter
*/
function isChanged() {
return $this->_is_changed;
}
/**
* Returns true if the Object was deleted in the API instance
* and API response was succesfull
*
* @return boolean true if the Object was deleted
* @access public
*
* @todo move to the defaut getter
*/
function isDelete() {
return $this->_is_deleted;
}
/**
* Returns Text written to the full class logs
*
* When the log level is set to debug, the debug messages will be also
* included
*
* @return string All formatted logs
* @access public
*/
function logs() {
if( isset( $this->logger ) ) {
return $this->logger->logs();
}
}
/**
* Authorizes users in the system by the specified URL by means of cURL
*
* To authorize, set the user name and password. Specify the Proxy, if
* needed. When authorized, {@link load}, {@link save}, {@link delete} and
* {@link getList} methods can be used.
*
* @param string $url API URL
* @param string $user user name
* @param string $pass password
* @param string $proxy (optional) proxy server
*
* @return void
* @access public
*/
function auth( $url, $user, $pass, $proxy = '' ) {
$this->logger->setDebug( $this->options[ ONAPP_OPTION_DEBUG_MODE ] );
$this->logger->setTimezone();
$this->logger->debug( 'auth: Authorization(url => ' . $url . ', user => ' . $user . ', pass => ********).' );
$this->setOption( ONAPP_OPTION_CURL_URL, $url );
$this->setOption( ONAPP_OPTION_CURL_PROXY, $proxy );
$this->_init_curl( $user, $pass );
$this->setAPIResource( ONAPP_GETRESOURCE_VERSION );
$response = $this->sendRequest( ONAPP_REQUEST_METHOD_GET );
if( $response[ 'info' ][ 'http_code' ] == '200' ) {
$this->setAPIVersion( $response[ 'response_body' ] );
if( $this->getClassName() != 'OnApp' ) {
$this->initFields( $this->version );
}
$this->setErrors();
$this->_is_auth = true;
}
else {
switch( $this->options[ ONAPP_OPTION_API_TYPE ] ) {
case 'xml':
case 'json':
$tag = 'version';
$this->version = null;
$objCast = new OnApp_Helper_Caster( $this );
$error = $objCast->unserialize( $this->getClassName(), $response[ 'response_body' ], null, 'errors' );
break;
default:
//todo add CLI check
$msg = 'FATAL ERROR: Caster for "' . $this->options[ ONAPP_OPTION_API_TYPE ] . '" is not defined'
. ' in FILE ' . __FILE__ . ' LINE ' . __LINE__ . PHP_EOL . PHP_EOL;
try {
throw new Exception( $msg );
}
catch( Exception $e ) {
echo $e->getMessage();
exit( $this->logger->logs() );
}
}
$this->setErrors( $error );
$this->_is_auth = false;
}
}
protected function setAPIVersion( $data ) {
switch( $this->options[ ONAPP_OPTION_API_TYPE ] ) {
case 'xml':
case 'json':
$tag = 'version';
$this->version = null;
$objCast = new OnApp_Helper_Caster( $this );
$this->version = $objCast->unserialize( $this->getClassName(), $data, null, $tag );
break;
default:
//todo add CLI check
$msg = 'FATAL ERROR: Caster for "' . $this->options[ ONAPP_OPTION_API_TYPE ] . '" is not defined'
. ' in FILE ' . __FILE__ . ' LINE ' . __LINE__ . PHP_EOL . PHP_EOL;
try {
throw new Exception( $msg );
}
catch( Exception $e ) {
echo $e->getMessage();
exit( $this->logger->logs() );
}
}
$this->version = (float)$this->version;
}
public function initFields( $version = null, $className = '' ) {
if( ! is_null( $version ) ) {
$this->version = $version;
}
if( is_null( $this->fields ) && ( $this->getClassName() != 'OnApp' ) ) {
$this->logger->debug( 'No fields defined for current API version [ ' . $version . ' ]' );
}
elseif( ! is_null( $version ) ) {
if( $version == $this->version ) {
if( $this->defaultOptions[ ONAPP_OPTION_DEBUG_MODE ] ) {
$this->logger->debug( $className . '::initFields, version ' . $version . PHP_EOL . print_r( $this->fields, true ) );
}
else {
$this->logger->add( $className . '::initFields, version ' . $version );
}
}
}
if( is_null( $this->fields ) && ! in_array( get_called_class(), array( 'OnApp', 'OnApp_Factory' ) ) ) {
throw new Exception( sprintf(
"The wrapper class '%s' does not support OnApp version '%s'",
get_called_class(),
$version
) );
}
}
/**
* Sets an option for a cURL transfer
*
* @param string $user user name
* @param string $pass password
* @param string $cookiedir Cookies directory
*
* @return void
* @access private
*
* @todo check response from basic URL
*/
function _init_curl( $user, $pass, $cookiedir = '' ) {
$this->logger->debug( "_init_curl: Init Curl (cookiedir => '$cookiedir')." );
$this->_ch = curl_init();
//todo ???
//$this->_is_auth = true;
if( strlen( $this->options[ ONAPP_OPTION_CURL_PROXY ] ) > 0 ) {
curl_setopt(
$this->_ch,
CURLOPT_PROXY,
$this->options[ ONAPP_OPTION_CURL_PROXY ]
);
}
curl_setopt( $this->_ch, CURLOPT_SSL_VERIFYPEER, false );
curl_setopt( $this->_ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt(
$this->_ch, CURLOPT_USERPWD,
$user . ':' . $pass
);
}
/**
* Closes a cURL session
*
* @return void
* @access public
*/
function close_curl() {
curl_close( $this->_ch );
}
/**
* Sets full API path to the variable cURL
*
* @param string $resource API alias
* @param boolean $append_api_version
* @param string $queryString API request
*
* @return void
* @access public
*/
function setAPIResource( $resource, $append_api_version = true, $queryString = '' ) {
$url = $this->options[ ONAPP_OPTION_CURL_URL ];
$this->logger->add(
'setAPIResource: Set an option for a cURL transfer (' .
' url => "' . $url . '", ' .
' resource => "' . $resource . '", ' .
' data_type => "' . $this->options[ ONAPP_OPTION_API_TYPE ] . '"' .
' append_api_version => ' . $this->getAPIVersion() . ',' .
' queryString => "' . $queryString . '" ).'
);
if( $append_api_version ) {
curl_setopt(
$this->_ch,
CURLOPT_URL,
sprintf(
'%1$s/%2$s.%3$s?%4$s',
$url,
$resource,
$this->options[ ONAPP_OPTION_API_TYPE ],
$queryString )
);
}
else {
curl_setopt(
$this->_ch,
CURLOPT_URL,
sprintf(
'%1$s/%2$s?%3$s',
$url,
$resource,
$queryString
)
);
}
}
/**
* Sends API request to the API server and gets response from it
*
* @param string $method
* @param array|null $data
*
* @return array|bool cURL response
*/
protected function sendRequest( $method, $data = null ) {
$alowed_methods = array(
ONAPP_REQUEST_METHOD_GET,
ONAPP_REQUEST_METHOD_POST,
ONAPP_REQUEST_METHOD_PUT,
ONAPP_REQUEST_METHOD_DELETE,
);
if( ! in_array( $method, $alowed_methods ) ) {
$this->logger->error( 'Wrong request method.' );
}
$debug_msg = 'Send ' . $method . ' request.';
if( $data ) {
$debug_msg .= ' Request:' . PHP_EOL . print_r( $data, true );
}
$this->logger->debug( $debug_msg );
$http_header = array(
'Content-Type: ' . $this->options[ ONAPP_OPTION_API_CONTENT ],
'Accept: ' . $this->options[ ONAPP_OPTION_API_CONTENT ]
);
curl_setopt( $this->_ch, CURLOPT_CUSTOMREQUEST, $method );
switch( $method ) {
case ONAPP_REQUEST_METHOD_GET:
curl_setopt( $this->_ch, CURLOPT_HTTPGET, true );
if( ! is_null( $data ) ) {
curl_setopt( $this->_ch, CURLOPT_POSTFIELDS, $data );
}
else {
$http_header[ ] = 'Content-Length: 0';
}
break;
case ONAPP_REQUEST_METHOD_POST:
if( ! is_null( $data ) ) {
curl_setopt( $this->_ch, CURLOPT_POSTFIELDS, $data );
}
break;
case ONAPP_REQUEST_METHOD_PUT:
$http_header[ ] = 'Content-Length: ' . strlen( $data );
if( ! is_null( $data ) ) {
curl_setopt( $this->_ch, CURLOPT_POSTFIELDS, $data );
}
break;
case ONAPP_REQUEST_METHOD_DELETE:
if( ! is_null( $data ) ) {
$http_header[ ] = 'Content-Length: ' . strlen( $data );
curl_setopt( $this->_ch, CURLOPT_POSTFIELDS, $data );
}
break;
}
curl_setopt( $this->_ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $this->_ch, CURLOPT_SSL_VERIFYHOST, false );
curl_setopt( $this->_ch, CURLOPT_HEADER, true );
curl_setopt( $this->_ch, CURLOPT_HTTPHEADER, $http_header );
$result = array();
$result[ 'response_body' ] = curl_exec( $this->_ch );
$result[ 'info' ] = curl_getinfo( $this->_ch );
$curlHeaderSize = $result[ 'info' ][ 'header_size' ];
$result[ 'headers' ] = mb_substr( $result[ 'response_body' ], 0, $curlHeaderSize );
$result[ 'response_body' ] = mb_substr( $result[ 'response_body' ], $curlHeaderSize );
if( ! $result[ 'response_body' ] && $method == ONAPP_REQUEST_METHOD_DELETE ||
! $result[ 'response_body' ] && $method == ONAPP_REQUEST_METHOD_PUT
) {
switch( $this->options[ ONAPP_OPTION_API_TYPE ] ) {
case 'json':
$result[ 'response_body' ] = '{}';
break;
case 'xml':
$result[ 'response_body' ] = ' ';
break;
default:
$this->logger->error( 'Unsupported API method ' . $this->options[ ONAPP_OPTION_API_TYPE ] );
break;
}
}
$this->logger->debug( 'Receive Response ' . print_r( $result, true ) );
if( ! $result[ 'response_body' ] ) {
$this->logger->debug( 'Response body is empty for method: ' . $method );
return false;
}
$this->response = $result;
$content_type = $result[ 'info' ][ 'content_type' ];
if( $content_type == $this->options[ ONAPP_OPTION_API_CONTENT ] . "; " . $this->options[ ONAPP_OPTION_API_CHARSET ] ) {
switch( $result[ 'info' ][ 'http_code' ] ) {
case 200:
case 201:
case 204:
break;
case 422:
case 404:
switch( $this->options[ ONAPP_OPTION_API_TYPE ] ) {
case 'xml':
case 'json':
$this->logger->add( 'Response (code => ' . $result[ 'info' ][ 'http_code' ] . ', cast:' . PHP_EOL . $result[ 'response_body' ] );
break;
}
break;
default:
$this->logger->warning( 'Response (code => ' . $result[ 'info' ][ 'http_code' ] . ', body: ' . PHP_EOL . $result[ 'response_body' ] );
$result[ 'errors' ] = $result[ 'response_body' ];
}
}
else {
$this->logger->add( 'sendRequest: Response:' . PHP_EOL . $result[ 'response_body' ] );
$result[ 'errors' ] = 'Bad response content type: ' . $content_type;
$result[ 'error_response' ] = $result[ 'response_body' ];
}
$this->_errno_curl( $result[ 'response_body' ] );
return $result;
}
/**
*
* @param type $label
*
* @return string
*/
public function getHeader( $label = null ) {
if( ! $label ) {
return $this->response[ 'headers' ];