11<?php
22
33/**
4- * PHP class to handle connections with cPanel's UAPI as seamlessly and simply as possible.
4+ * PHP class to handle connections with cPanel's UAPI and API2 specifically through cURL requests as seamlessly and simply as possible.
55 *
66 * For documentation on cPanel's UAPI:
77 * @see https://documentation.cpanel.net/display/SDK/UAPI+Functions
88 *
9+ * For documentation on cPanel's API2:
10+ * @see https://documentation.cpanel.net/display/SDK/Guide+to+cPanel+API+2
11+ *
12+ * Please use UAPI where possible, only use API2 where the equivalent doesn't exist for UAPI
13+ *
914 * @author N1ghteyes - www.source-control.co.uk
10- * @copyright 2014 N1ghteyes
15+ * @copyright 2016 N1ghteyes
1116 * @license license.txt The MIT License (MIT)
1217 * @link https://github.com/N1ghteyes/cpanel-UAPI-php-class
1318 */
1419
1520/**
16- * Class cpanelUAPI
21+ * Class cPanelUAPI
22+ * Added for backwards compatibility
23+ */
24+ class cpanelUAPI extends cpanelAPI{
25+ function __construct ($ user , $ pass , $ server ){
26+ parent ::__construct ($ user , $ pass , $ server );
27+ $ this ->setApi ('uapi ' );
28+ $ this ->version = '1.0 ' ;
29+ }
30+ }
31+
32+ /**
33+ * Class cPanelAPI2
34+ * Added to set api2 info.
35+ */
36+ class cpanelAPI2 extends cpanelAPI{
37+ function __construct ($ user , $ pass , $ server ){
38+ parent ::__construct ($ user , $ pass , $ server );
39+ $ this ->setApi ('api2 ' );
40+ $ this ->version = '1.0 ' ;
41+ }
42+ }
43+
44+ /**
45+ * Class cPanelAPI
1746 */
18- class cpanelUAPI
47+ class cpanelAPI
1948{
20- public $ cpanelUAPI = '1.0 ' ;
49+ public $ version = '1.0 ' ;
2150 public $ scope = "" ; //String - Module we want to use
2251 public $ ssl = 1 ; //Bool - TRUE / FALSE for ssl connection
2352 public $ port = 2083 ; //default for ssl servers.
2453 public $ server ;
54+ public $ maxredirect = 0 ; //Number of redirects to make, typically 0 is fine. on some shared setups this will need to be increased.
2555
56+ protected $ api ;
2657 protected $ auth ;
2758 protected $ user ;
2859 protected $ pass ;
60+ protected $ secret ;
2961 protected $ type ;
62+ protected $ session ;
63+ protected $ method ;
3064 protected $ requestUrl ;
3165
3266 /**
@@ -41,43 +75,71 @@ function __construct($user, $pass, $server)
4175 $ this ->server = $ server ;
4276 }
4377
78+ protected function setApi ($ api ){
79+ $ this ->api = $ api ;
80+ $ this ->setMethod ();
81+ }
82+
4483 /**
4584 * Magic __call method, will translate all function calls to object to API requests
46- * @param String $name name of the function
47- * @param array $arguments an array of arguments
48- * @return - Object from json_decode
85+ * @param $name - name of the function
86+ * @param $arguments - an array of arguments
87+ * @return mixed
88+ * @throws Exception
4989 */
5090 public function __call ($ name , $ arguments )
5191 {
52- $ this ->connections (); //set paths etc at the last possible moment to allow for changes before this call is made.
5392 if (count ($ arguments ) < 1 || !is_array ($ arguments [0 ]))
5493 $ arguments [0 ] = array ();
5594 return json_decode ($ this ->APIcall ($ name , $ arguments [0 ]));
5695 }
5796
58- /**
59- * function to set all the connection variables, called before APIcall
60- */
61- protected function connections ()
62- {
63- $ this ->type = $ this ->ssl == 1 ? "https:// " : "http:// " ;
64- $ this ->requestUrl = $ this ->type . $ this ->server . ': ' . $ this ->port . '/execute/ ' ;
65- $ this ->auth = base64_encode ($ this ->user . ": " . $ this ->pass );
97+ public function getLastRequest (){
98+ return $ this ->requestUrl ;
99+ }
100+
101+ protected function setMethod (){
102+ switch ($ this ->api ){
103+ case 'uapi ' :
104+ $ this ->method = '/execute/ ' ;
105+ break ;
106+ case 'api2 ' :
107+ $ this ->method = '/json-api/cpanel/ ' ;
108+ break ;
109+ default :
110+ throw new Exception ('$this->api is not set or is incorrectly set. The only available options are \'uapi \' or \'api2 \'' );
111+ }
66112 }
67113
68114 /**
69115 * @param $name
70116 * @param $arguments
71117 * @return bool|mixed
118+ * @throws Exception
72119 */
73120 protected function APIcall ($ name , $ arguments )
74121 {
75- $ url = $ this ->requestUrl . ($ this ->scope != '' ? $ this ->scope . "/ " : '' ) . $ name . '? ' ;
122+ $ this ->auth = base64_encode ($ this ->user . ": " . $ this ->pass );
123+ $ this ->type = $ this ->ssl == 1 ? "https:// " : "http:// " ;
124+ $ this ->requestUrl = $ this ->type . $ this ->server . ': ' . $ this ->port . $ this ->method ;
125+ switch ($ this ->api ){
126+ case 'uapi ' :
127+ $ this ->requestUrl .= ($ this ->scope != '' ? $ this ->scope . "/ " : '' ) . $ name . '? ' ;
128+ break ;
129+ case 'api2 ' :
130+ if ($ this ->scope == '' ){
131+ throw new Exception ('Scope must be set. ' );
132+ }
133+ $ this ->requestUrl .= '?cpanel_jsonapi_user= ' .$ this ->user .'&cpanel_jsonapi_apiversion=2&cpanel_jsonapi_module= ' .$ this ->scope .'&cpanel_jsonapi_func= ' .$ name .'& ' ;
134+ break ;
135+ default :
136+ throw new Exception ('$this->api is not set or is incorrectly set. The only available options are \'uapi \' or \'api2 \'' );
137+ }
76138 foreach ($ arguments as $ key => $ value ) {
77- $ url .= $ key . "= " . $ value . "& " ;
139+ $ this -> requestUrl .= $ key . "= " . $ value . "& " ;
78140 }
79141
80- return $ this ->curl_request ($ url );
142+ return $ this ->curl_request ($ this -> requestUrl );
81143 }
82144
83145 /**
@@ -96,7 +158,7 @@ protected function curl_request($url)
96158 curl_setopt ($ ch , CURLOPT_TIMEOUT , 100020 );
97159 curl_setopt ($ ch , CURLOPT_RETURNTRANSFER , TRUE );
98160
99- $ content = $ this ->curl_exec_follow ($ ch );
161+ $ content = $ this ->curl_exec_follow ($ ch, $ this -> maxredirect );
100162 $ err = curl_errno ($ ch );
101163 $ errmsg = curl_error ($ ch );
102164 $ header = curl_getinfo ($ ch );
@@ -183,6 +245,4 @@ protected function curl_exec_follow($ch, &$maxredirect = null)
183245 }
184246 return curl_exec ($ ch );
185247 }
186- }
187-
188- ?>
248+ }
0 commit comments