forked from CpanelInc/xmlapi-php
-
Notifications
You must be signed in to change notification settings - Fork 0
/
xmlapi.php
2212 lines (2003 loc) · 73.7 KB
/
xmlapi.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
/**
* cPanel XMLAPI Client Class
*
* This class allows for easy interaction with cPanel's XML-API allow functions within the XML-API to be called
* by calling funcions within this class
*
* LICENSE:
*
* Copyright (c) 2009, cPanel, Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are permitted provided
* that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice, this list of conditions and the
* following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
* following disclaimer in the documentation and/or other materials provided with the distribution.
* * Neither the name of the cPanel, Inc. nor the names of its contributors may be used to endorse or promote
* products derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
* TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* Version: 1.0.7
* Last updated: 10 November 2010
*
* Changes
*
* 1.0.7:
* Corrected typo for setrellerlimits where xml_query incorrectly called xml-api's setresellerips
*
* 1.0.6:
* Changed 'user' URL parameter for API1/2 calls to 'cpanel_xmlapi_user'/'cpanel_jsonapi_user' to resolve conflicts with API2 functions that use 'user' as a parameter
* Relocated exmaple script to Example subdirectory
* Modified example scripts to take remote server IP and root password from environment variables REMOTE_HOST and REMOTE_PASSWORD, respectively
* Created subdirectory Tests for PHPUnit tests
* Add PHPUnit test BasicParseTest.php
*
* 1.0.5:
* fix bug where api1_query and api2_query would not return JSON data
*
* 1.0.4:
* set_port will now convert non-int values to ints
*
* 1.0.3:
* Fixed issue with set_auth_type using incorrect logic for determining acceptable auth types
* Suppress non-UTF8 encoding when using curl
*
* 1.0.2:
* Increased curl buffer size to 128kb from 16kb
* Fix double encoding issue in terminateresellers()
*
* 1.0.1:
* Fixed use of wrong variable name in curl error checking
* adjust park() to use api2 rather than API1
*
* 1.0
* Added in 11.25 functions
* Changed the constructor to allow for either the "DEFINE" config setting method or using parameters
* Removed used of the gui setting
* Added fopen support
* Added auto detection for fopen or curl (uses curl by default)
* Added ability to return in multiple formats: associative array, simplexml, xml, json
* Added PHP Documentor documentation for all necessary functions
* Changed submission from GET to POST
*
*
* @copyright 2009 cPanel, Inc
* @license http://sdk.cpanel.net/license/bsd.html
* @version 1.0.6
* @link http://twiki.cpanel.net/twiki/bin/view/AllDocumentation/AutomationIntegration/XmlApi
* @since File available since release 0.1
**/
/**
* The base XML-API class
*
* The XML-API class allows for easy execution of cPanel XML-API calls. The goal of this project is to create
* an open source library that can be used for multiple types of applications. This class relies on PHP5 compiled
* with both curl and simplexml support.
*
* Making Calls with this class are done in the following steps:
*
* 1.) Instaniating the class:
* $xmlapi = new xmlapi($host);
*
* 2.) Setting access credentials within the class via either set_password or set_hash:
* $xmlapi->set_hash("username", $accessHash);
* $xmlapi->set_password("username", "password");
*
* 3.) Execute a function
* $xmlapi->listaccts();
*
* @category Cpanel
* @package xmlapi
* @copyright 2009 cPanel, Inc.
* @license http://sdk.cpanel.net/license/bsd.html
* @version Release: 1.0.6
* @link http://twiki.cpanel.net/twiki/bin/view/AllDocumentation/AutomationIntegration/XmlApi
* @since Class available since release 0.1
**/
class xmlapi {
// should debugging statements be printed?
private $debug = false;
// The host to connect to
private $host = '127.0.0.1';
// the port to connect to
private $port = '2087';
// should be the literal strings http or https
private $protocol = 'https';
// output that should be given by the xml-api
private $output = 'simplexml';
// literal strings hash or password
private $auth_type = null;
// the actual password or hash
private $auth = null;
// username to authenticate as
private $user = null;
// The HTTP Client to use
private $http_client = 'curl';
/**
* Instantiate the XML-API Object
* All parameters to this function are optional and can be set via the accessor functions or constants
* This defaults to password auth, however set_hash can be used to use hash authentication
*
* @param string $host The host to perform queries on
* @param string $user The username to authenticate as
* @param string $password The password to authenticate with
* @return Xml_Api object
*/
public function __construct($host = null, $user = null, $password = null ) {
// Check if debugging must be enabled
if ( (defined('XMLAPI_DEBUG')) && (XMLAPI_DEBUG == '1') ) {
$this->debug = true;
}
// Check if raw xml output must be enabled
if ( (defined('XMLAPI_RAW_XML')) && (XMLAPI_RAW_XML == '1') ) {
$this->raw_xml = true;
}
/**
* Authentication
* This can either be passed at this point or by using the set_hash or set_password functions
**/
if ( ( defined('XMLAPI_USER') ) && ( strlen(XMLAPI_USER) > 0 ) ) {
$this->user = XMLAPI_USER;
// set the authtype to pass and place the password in $this->pass
if ( ( defined('XMLAPI_PASS') ) && ( strlen(XMLAPI_PASS) > 0 ) ) {
$this->auth_type = 'pass';
$this->auth = XMLAPI_PASS;
}
// set the authtype to hash and place the hash in $this->auth
if ( ( defined('XMLAPI_HASH') ) && ( strlen(XMLAPI_HASH) > 0 ) ) {
$this->auth_type = 'hash';
$this->auth = preg_replace("/(\n|\r|\s)/", '', XMLAPI_HASH);
}
// Throw warning if XMLAPI_HASH and XMLAPI_PASS are defined
if ( ( ( defined('XMLAPI_HASH') ) && ( strlen(XMLAPI_HASH) > 0 ) )
&& ( ( defined('XMLAPI_PASS') ) && ( strlen(XMLAPI_PASS) > 0 ) ) ) {
error_log('warning: both XMLAPI_HASH and XMLAPI_PASS are defined, defaulting to XMLAPI_HASH');
}
// Throw a warning if XMLAPI_HASH and XMLAPI_PASS are undefined and XMLAPI_USER is defined
if ( !(defined('XMLAPI_HASH') ) || !defined('XMLAPI_PASS') ) {
error_log('warning: XMLAPI_USER set but neither XMLAPI_HASH or XMLAPI_PASS have not been defined');
}
}
if ( ( $user != null ) && ( strlen( $user ) < 9 ) ) {
$this->user = $user;
}
if ($password != null ) {
$this->set_password($password);
}
/**
* Connection
*
* $host/XMLAPI_HOST should always be equal to either the IP of the server or it's hostname
*/
// Set the host, error if not defined
if ( $host == null ) {
if ( (defined('XMLAPI_HOST')) && (strlen(XMLAPI_HOST) > 0) ) {
$this->host = XMLAPI_HOST;
} else {
throw new Exception("No host defined");
}
} else {
$this->host = $host;
}
// disabling SSL is probably a bad idea.. just saying.
if ( defined('XMLAPI_USER_SSL' ) && (XMLAPI_USE_SSL == '0' ) ) {
$this->protocol = "http";
}
// Detemine what the default http client should be.
if ( function_exists('curl_setopt') ) {
$this->http_client = "curl";
} elseif ( ini_get('allow_url_fopen') ) {
$this->http_client = "fopen";
} else {
throw new Exception('allow_url_fopen and curl are neither available in this PHP configuration');
}
}
/**
* Accessor Functions
**/
/**
* Return whether the debug option is set within the object
*
* @return boolean
* @see set_debug()
*/
public function get_debug() {
return $this->debug;
}
/**
* Turn on debug mode
*
* Enabling this option will cause this script to print debug information such as
* the queries made, the response XML/JSON and other such pertinent information.
* Calling this function without any parameters will enable debug mode.
*
* @param bool $debug turn on or off debug mode
* @see get_debug()
*/
public function set_debug( $debug = 1 ) {
$this->debug = $debug;
}
/**
* Get the host being connected to
*
* This function will return the host being connected to
* @return string host
* @see set_host()
*/
public function get_host() {
return $this->host;
}
/**
* Set the host to query
*
* Setting this will set the host to be queried
* @param string $host The host to query
* @see get_host()
*/
public function set_host( $host ) {
$this->host = $host;
}
/**
* Get the port to connect to
*
* This will return which port the class is connecting to
* @return int $port
* @see set_port()
*/
public function get_port() {
return $this->port;
}
/**
* Set the port to connect to
*
* This will allow a user to define which port needs to be connected to.
* The default port set within the class is 2087 (WHM-SSL) however other ports are optional
* this function will automatically set the protocol to http if the port is equal to:
* - 2082
* - 2086
* - 2095
* - 80
* @param int $port the port to connect to
* @see set_protocol()
* @see get_port()
*/
public function set_port( $port ) {
if ( !is_int( $port ) ) {
$port = intval($port);
}
if ( $port < 1 || $port > 65535 ) {
throw new Exception('non integer or negative integer passed to set_port');
}
// Account for ports that are non-ssl
if ( $port == '2086' || $port == '2082' || $port == '80' || $port == '2095' ) {
$this->set_protocol('http');
}
$this->port = $port;
}
/**
* Return the protocol being used to query
*
* This will return the protocol being connected to
* @return string
* @see set_protocol()
*/
public function get_protocol() {
return $this->protocol;
}
/**
* Set the protocol to use to query
*
* This will allow you to set the protocol to query cpsrvd with. The only to acceptable values
* to be passed to this function are 'http' or 'https'. Anything else will cause the class to throw
* an Exception.
* @param string $proto the protocol to use to connect to cpsrvd
* @see get_protocol()
*/
public function set_protocol( $proto ) {
if ( $proto != 'https' && $proto != 'http' ) {
throw new Exception('https and http are the only protocols that can be passed to set_protocol');
}
$this->protocol = $proto;
}
/**
* Return what format calls with be returned in
*
* This function will return the currently set output format
* @see set_output()
* @return string
*/
public function get_output() {
return $this->output;
}
/**
* Set the output format for call functions
*
* This class is capable of returning data in numerous formats including:
* - json
* - xml
* - {@link http://php.net/simplexml SimpleXML}
* - {@link http://us.php.net/manual/en/language.types.array.php Associative Arrays}
*
* These can be set by passing this class any of the following values:
* - json - return JSON string
* - xml - return XML string
* - simplexml - return SimpleXML object
* - array - Return an associative array
*
* Passing any value other than these to this class will cause an Exception to be thrown.
* @param string $output the output type to be set
* @see get_output()
*/
public function set_output( $output ) {
if ( $output != 'json' && $output != 'xml' && $output != 'array' && $output != 'simplexml' ) {
throw new Exception('json, xml, array and simplexml are the only allowed values for set_output');
}
$this->output = $output;
}
/**
* Return the auth_type being used
*
* This function will return a string containing the auth type in use
* @return string auth type
* @see set_auth_type()
*/
public function get_auth_type() {
return $this->auth_type;
}
/**
* Set the auth type
*
* This class is capable of authenticating with both hash auth and password auth
* This function will allow you to manually set which auth_type you are using.
*
* the only accepted parameters for this function are "hash" and "pass" anything else will cuase
* an exception to be thrown
*
* @see set_password()
* @see set_hash()
* @see get_auth_type()
* @param string auth_type the auth type to be set
*/
public function set_auth_type( $auth_type ) {
if ( $auth_type != 'hash' && $auth_type != 'pass') {
throw new Exception('the only two allowable auth types arehash and path');
}
$this->auth_type = $auth_type;
}
/**
* Set the password to be autenticated with
*
* This will set the password to be authenticated with, the auth_type will be automatically adjusted
* when this function is used
*
* @param string $pass the password to authenticate with
* @see set_hash()
* @see set_auth_type()
* @see set_user()
*/
public function set_password( $pass ) {
$this->auth_type = 'pass';
$this->auth = $pass;
}
/**
* Set the hash to authenticate with
*
* This will set the hash to authenticate with, the auth_type will automatically be set when this function
* is used. This function will automatically strip the newlines from the hash.
* @param string $hash the hash to autenticate with
* @see set_password()
* @see set_auth_type()
* @see set_user()
*/
public function set_hash( $hash ) {
$this->auth_type = 'hash';
$this->auth = preg_replace("/(\n|\r|\s)/", '', $hash);
}
/**
* Return the user being used for authtication
*
* This will return the username being authenticated against.
*
* @return string
*/
public function get_user() {
return $this->user;
}
/**
* Set the user to authenticate against
*
* This will set the user being authenticated against.
* @param string $user username
* @see set_password()
* @see set_hash()
* @see get_user()
*/
public function set_user( $user ) {
$this->user = $user;
}
/**
* Set the user and hash to be used for authentication
*
* This function will allow one to set the user AND hash to be authenticated with
*
* @param string $user username
* @param string $hash WHM Access Hash
* @see set_hash()
* @see set_user()
*/
public function hash_auth( $user, $hash ) {
$this->set_hash( $hash );
$this->set_user( $user );
}
/**
* Set the user and password to be used for authentication
*
* This function will allow one to set the user AND password to be authenticated with
* @param string $user username
* @param string $pass password
* @see set_pass()
* @see set_user()
*/
public function password_auth( $user, $pass ) {
$this->set_password( $pass );
$this->set_user( $user );
}
/**
* Return XML format
*
* this function will cause call functions to return XML format, this is the same as doing:
* set_output('xml')
*
* @see set_output()
*/
public function return_xml() {
$this->set_output('xml');
}
/**
* Return simplexml format
*
* this function will cause all call functions to return simplexml format, this is the same as doing:
* set_output('simplexml')
*
* @see set_output()
*/
public function return_object() {
$this->set_output('simplexml');
}
/**
* Set the HTTP client to use
*
* This class is capable of two types of HTTP Clients:
* - curl
* - fopen
*
* When using allow url fopen the class will use get_file_contents to perform the query
* The only two acceptable parameters for this function are 'curl' and 'fopen'.
* This will default to fopen, however if allow_url_fopen is disabled inside of php.ini
* it will switch to curl
*
* @param string client The http client to use
* @see get_http_client()
*/
public function set_http_client( $client ) {
if ( ( $client != 'curl' ) && ( $client != 'fopen' ) ) {
throw new Exception('only curl and fopen and allowed http clients');
}
$this->http_client = $client;
}
/**
* Get the HTTP Client in use
*
* This will return a string containing the HTTP client currently in use
*
* @see set_http_client()
* @return string
*/
public function get_http_client() {
return $this->http_client;
}
/*
* Query Functions
* --
* This is where the actual calling of the XML-API, building API1 & API2 calls happens
*/
/**
* Perform an XML-API Query
*
* This function will perform an XML-API Query and return the specified output format of the call being made
*
* @param string $function The XML-API call to execute
* @param array $vars An associative array of the parameters to be passed to the XML-API Calls
* @return mixed
*/
public function xmlapi_query( $function, $vars = array() ) {
// Check to make sure all the data needed to perform the query is in place
if (!$function) {
throw new Exception('xmlapi_query() requires a function to be passed to it');
}
if ($this->user == null ) {
throw new Exception('no user has been set');
}
if ($this->auth ==null) {
throw new Exception('no authentication information has been set');
}
// Build the query:
$query_type = '/xml-api/';
if ( $this->output == 'json' ) {
$query_type = '/json-api/';
}
$args = http_build_query($vars, '', '&');
$url = $this->protocol . '://' . $this->host . ':' . $this->port . $query_type . $function;
if ($this->debug) {
error_log('URL: ' . $url);
error_log('DATA: ' . $args);
}
// Set the $auth string
$authstr;
if ( $this->auth_type == 'hash' ) {
$authstr = 'Authorization: WHM ' . $this->user . ':' . $this->auth . "\r\n";
} elseif ($this->auth_type == 'pass' ) {
$authstr = 'Authorization: Basic ' . base64_encode($this->user .':'. $this->auth) . "\r\n";
} else {
throw new Exception('invalid auth_type set');
}
if ($this->debug) {
error_log("Authentication Header: " . $authstr ."\n");
}
// Perform the query (or pass the info to the functions that actually do perform the query)
$response;
if ( $this->http_client == 'curl' ) {
$response = $this->curl_query($url, $args, $authstr);
} elseif ( $this->http_client == 'fopen' ) {
$response = $this->fopen_query($url, $args, $authstr);
}
/*
* Post-Query Block
* Handle response, return proper data types, debug, etc
*/
// print out the response if debug mode is enabled.
if ($this->debug) {
error_log("RESPONSE:\n " . $response);
}
// The only time a response should contain <html> is in the case of authentication error
// cPanel 11.25 fixes this issue, but if <html> is in the response, we'll error out.
if (stristr($response, '<html>') == true) {
if (stristr($response, 'Login Attempt Failed') == true) {
error_log("Login Attempt Failed");
return;
}
if (stristr($response, 'action="/login/"') == true) {
error_log("Authentication Error");
return;
}
return;
}
// perform simplexml transformation (array relies on this)
if ( ($this->output == 'simplexml') || $this->output == 'array') {
$response = simplexml_load_string($response, null, LIBXML_NOERROR | LIBXML_NOWARNING);
if (!$response){
error_log("Some error message here");
return;
}
if ( $this->debug ) {
error_log("SimpleXML var_dump:\n" . print_r($response, true));
}
}
// perform array tranformation
if ($this->output == 'array') {
$response = $this->unserialize_xml($response);
if ( $this->debug ) {
error_log("Associative Array var_dump:\n" . print_r($response, true));
}
}
return $response;
}
private function curl_query( $url, $postdata, $authstr ) {
$curl = curl_init();
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
// Return contents of transfer on curl_exec
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
// Allow self-signed certs
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
// Set the URL
curl_setopt($curl, CURLOPT_URL, $url);
// Increase buffer size to avoid "funny output" exception
curl_setopt($curl, CURLOPT_BUFFERSIZE, 131072);
// Pass authentication header
$header[0] =$authstr .
"Content-Type: application/x-www-form-urlencoded\r\n" .
"Content-Length: " . strlen($postdata) . "\r\n" . "\r\n" . $postdata;
curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
curl_setopt($curl, CURLOPT_POST, 1);
$result = curl_exec($curl);
if ($result == false) {
throw new Exception("curl_exec threw error \"" . curl_error($curl) . "\" for " . $url . "?" . $postdata );
}
curl_close($curl);
return $result;
}
private function fopen_query( $url, $postdata, $authstr ) {
if ( !(ini_get('allow_url_fopen') ) ) {
throw new Exception('fopen_query called on system without allow_url_fopen enabled in php.ini');
}
$opts = array(
'http' => array(
'allow_self_signed' => true,
'method' => 'POST',
'header' => $authstr .
"Content-Type: application/x-www-form-urlencoded\r\n" .
"Content-Length: " . strlen($postdata) . "\r\n" .
"\r\n" . $postdata
)
);
$context = stream_context_create($opts);
return file_get_contents($url, false, $context);
}
/*
* Convert simplexml to associative arrays
*
* This function will convert simplexml to associative arrays.
*/
private function unserialize_xml($input, $callback = null, $recurse = false) {
// Get input, loading an xml string with simplexml if its the top level of recursion
$data = ( (!$recurse) && is_string($input) ) ? simplexml_load_string($input) : $input;
// Convert SimpleXMLElements to array
if ($data instanceof SimpleXMLElement) {
$data = (array) $data;
}
// Recurse into arrays
if (is_array($data)) {
foreach ($data as &$item) {
$item = $this->unserialize_xml($item, $callback, true);
}
}
// Run callback and return
return (!is_array($data) && is_callable($callback)) ? call_user_func($callback, $data) : $data;
}
/* TO DO:
Implement API1 and API2 query functions!!!!!
*/
/**
* Call an API1 function
*
* This function allows you to call API1 from within the XML-API, This allowes a user to peform actions
* such as adding ftp accounts, etc
*
* @param string $user The username of the account to perform API1 actions on
* @param string $module The module of the API1 call to use
* @param string $function The function of the API1 call
* @param array $args The arguments for the API1 function, this should be a non-associative array
* @return mixed
* @link http://docs.cpanel.net/twiki/bin/view/AllDocumentation/AutomationIntegration/CallingAPIFunctions XML API Call documentation
* @link http://docs.cpanel.net/twiki/bin/view/DeveloperResources/ApiRef/WebHome API1 & API2 Call documentation
* @link http://docs.cpanel.net/twiki/bin/view/DeveloperResources/ApiBasics/CallingApiOne API1 Documentation
*/
public function api1_query($user, $module, $function, $args = array() ) {
if ( !isset($module) || !isset($function) || !isset($user) ) {
error_log("api1_query requires that a module and function are passed to it");
return false;
}
if (!is_array($args)) {
error_log('api1_query requires that it is passed an array as the 4th parameter');
return false;
}
$cpuser = 'cpanel_xmlapi_user';
$module_type = 'cpanel_xmlapi_module';
$func_type = 'cpanel_xmlapi_func';
$api_type = 'cpanel_xmlapi_apiversion';
if ( $this->get_output() == 'json' ) {
$cpuser = 'cpanel_jsonapi_user';
$module_type = 'cpanel_jsonapi_module';
$func_type = 'cpanel_jsonapi_func';
$api_type = 'cpanel_jsonapi_apiversion';
}
$call = array(
$cpuser => $user,
$module_type => $module,
$func_type => $function,
$api_type => '1'
);
for ($int = 0; $int < count($args); $int++) {
$call['arg-' . $int] = $args[$int];
}
return $this->xmlapi_query('cpanel', $call);
}
/**
* Call an API2 Function
*
* This function allows you to call an API2 function, this is the modern API for cPanel and should be used in preference over
* API1 when possible
*
* @param string $user The username of the account to perform API2 actions on
* @param string $module The module of the API2 call to use
* @param string $function The function of the API2 call
* @param array $args An associative array containing the arguments for the API2 call
* @return mixed
* @link http://docs.cpanel.net/twiki/bin/view/AllDocumentation/AutomationIntegration/CallingAPIFunctions XML API Call documentation
* @link http://docs.cpanel.net/twiki/bin/view/DeveloperResources/ApiRef/WebHome API1 & API2 Call documentation
* @link http://docs.cpanel.net/twiki/bin/view/AllDocumentation/AutomationIntegration/ApiTwo Legacy API2 Documentation
* @link http://docs.cpanel.net/twiki/bin/view/DeveloperResources/ApiBasics/CallingApiTwo API2 Documentation
*/
public function api2_query($user, $module, $function, $args = array()) {
if (!isset($user) || !isset($module) || !isset($function) ) {
error_log("api2_query requires that a username, module and function are passed to it");
return false;
}
if (!is_array($args)) {
error_log("api2_query requires that an array is passed to it as the 4th parameter");
return false;
}
$cpuser = 'cpanel_xmlapi_user';
$module_type = 'cpanel_xmlapi_module';
$func_type = 'cpanel_xmlapi_func';
$api_type = 'cpanel_xmlapi_apiversion';
if ( $this->get_output() == 'json' ) {
$cpuser = 'cpanel_jsonapi_user';
$module_type = 'cpanel_jsonapi_module';
$func_type = 'cpanel_jsonapi_func';
$api_type = 'cpanel_jsonapi_apiversion';
}
$args[$cpuser] = $user;
$args[$module_type] = $module;
$args[$func_type] = $function;
$args[$api_type] = '2';
return $this->xmlapi_query('cpanel', $args);
}
####
# XML API Functions
####
/**
* Return a list of available XML-API calls
*
* This function will return an array containing all applications available within the XML-API
*
* @return mixed
* @link http://docs.cpanel.net/twiki/bin/view/AllDocumentation/AutomationIntegration/ListAvailableCalls XML API Call documentation
*/
public function applist() {
return $this->xmlapi_query('applist');
}
####
# Account functions
####
/**
* Create a cPanel Account
*
* This function will allow one to create an account, the $acctconf parameter requires that the follow
* three associations are defined:
* - username
* - password
* - domain
*
* Failure to prive these will cause an error to be logged. Any other key/value pairs as defined by the createaccount call
* documentation are allowed parameters for this call.
*
* @param array $acctconf
* @return mixed
* @link http://docs.cpanel.net/twiki/bin/view/AllDocumentation/AutomationIntegration/CreateAccount XML API Call documentation
*/
public function createacct($acctconf) {
if (!is_array($acctconf)) {
error_log("createacct requires that first parameter passed to it is an array");
return false;
}
if (!isset($acctconf['username']) || !isset($acctconf['password']) || !isset($acctconf['domain'])) {
error_log("createacct requires that username, password & domain elements are in the array passed to it");
return false;
}
return $this->xmlapi_query('createacct', $acctconf);
}
/**
* Change a cPanel Account's Password
*
* This function will allow you to change the password of a cpanel account
*
* @param string $username The username to change the password of
* @param string $pass The new password for the cPanel Account
* @return mixed
* @link http://docs.cpanel.net/twiki/bin/view/AllDocumentation/AutomationIntegration/ChangePassword XML API Call documentation
*/
public function passwd($username, $pass){
if (!isset($username) || !isset($pass)) {
error_log("passwd requires that an username and password are passed to it");
return false;
}
return $this->xmlapi_query('passwd', array('user' => $username, 'pass' => $pass));
}
/**
* Limit an account's monthly bandwidth usage
*
* This function will set an account's bandwidth limit.
*
* @param string $username The username of the cPanel account to modify
* @param int $bwlimit The new bandwidth limit in megabytes
* @return mixed
* @link http://docs.cpanel.net/twiki/bin/view/AllDocumentation/AutomationIntegration/LimitBandwidth XML API Call documentation
*/
public function limitbw($username, $bwlimit) {
if (!isset($username) || !isset($bwlimit)) {
error_log("limitbw requires that an username and bwlimit are passed to it");
return false;
}
return $this->xmlapi_query('limitbw', array('user' => $username, 'bwlimit' => $bwlimit));
}
/**
* List accounts on Server
*
* This call will return a list of account on a server, either no parameters or both parameters may be passed to this function.
*
* @param string $searchtype Type of account search to use, allowed values: domain, owner, user, ip or package
* @param string $search the string to search against
* @return mixed
* @link http://docs.cpanel.net/twiki/bin/view/AllDocumentation/AutomationIntegration/ListAccounts XML API Call documentation
*/
public function listaccts($searchtype = null, $search = null) {
if ($search) {
return $this->xmlapi_query('listaccts', array('searchtype' => $searchtype, 'search' => $search ));
}
return $this->xmlapi_query('listaccts');
}
/**
* Modify a cPanel account
*
* This call will allow you to change limitations and information about an account. See the XML API call documentation for a list of
* acceptable values for args.
*
* @param string $username The username to modify
* @param array $args the new values for the modified account (see {@link http://docs.cpanel.net/twiki/bin/view/AllDocumentation/AutomationIntegration/ModifyAccount modifyacct documentation})
* @return mixed
* @link http://docs.cpanel.net/twiki/bin/view/AllDocumentation/AutomationIntegration/ModifyAccount XML API Call documentation
*/
public function modifyacct($username, $args = array()) {
if (!isset($username)) {
error_log("modifyacct requires that username is passed to it");
return false;
}
$args['user'] = $username;
if (sizeof($args) < 2) {
error_log("modifyacct requires that at least one attribute is passed to it");
return false;
}
return $this->xmlapi_query('modifyacct', $args);
}
/**
* Edit a cPanel Account's Quota
*
* This call will allow you to change a cPanel account's quota
*
* @param string $username The username of the account to modify the quota.
* @param int $quota the new quota in megabytes
* @return mixed
* @link http://docs.cpanel.net/twiki/bin/view/AllDocumentation/AutomationIntegration/EditQuota XML API Call documentation
*/
public function editquota($username, $quota) {
if (!isset($username) || !isset($quota)) {
error_log("editquota requires that an username and quota are passed to it");
return false;
}