11<?php
2+
3+ /**
4+ * Class cpanelUAPI
5+ */
26class cpanelUAPI
37{
4- public $cpanelUAPI = '1.0';
5- public $scope = ""; //String - Module we want to use
6- public $ssl = 1; //Bool - TRUE / FALSE for ssl connection
7- public $port = 2083; //default for ssl servers.
8- public $server;
9-
10- protected $auth;
11- protected $user;
12- protected $pass;
13- protected $type;
14- protected $port;
15- protected $requestUrl;
16-
17- function __construct(){
18- $this->user = (isset($this->user) ? $this->user : '');
19- $this->pass = (isset($this->pass) ? $this->pass : '');
20- $this->server = (isset($this->server) ? $this->server : '');
21- }
22-
23- function __construct($user, $pass){
24- $this->user = $user;
25- $this->pass = $pass;
26- $this->server = (isset($this->server) ? $this->server : '');
27- }
28-
29- function __construct($user, $pass, $server){
30- $this->user = $user;
31- $this->pass = $pass;
32- $this->server = $server;
33- }
34-
35- /**
36- * Magic __call method, will translate all function calls to object to API requests
37- * @param String $name name of the function
38- * @param array $arguments an array of arguments
39- * @return OauthResult
40- */
41- public function __call($name, $arguments)
42- {
43- $this->connections(); //set paths etc at the last possible moment to allow for changes before this call is made.
44- if(count($arguments) < 1 || !is_array($arguments[0]))
45- $arguments[0] = array();
46- $scope = $this->scope;
47- return json_decode($this->APIcall($name, $arguments[0]));
8+ public $cpanelUAPI = '1.0';
9+ public $scope = ""; //String - Module we want to use
10+ public $ssl = 1; //Bool - TRUE / FALSE for ssl connection
11+ public $port = 2083; //default for ssl servers.
12+ public $server;
13+
14+ protected $auth;
15+ protected $user;
16+ protected $pass;
17+ protected $type;
18+ protected $requestUrl;
19+
20+ /**
21+ * @param $user
22+ * @param $pass
23+ * @param $server
24+ */
25+ function __construct($user, $pass, $server)
26+ {
27+ $this->user = $user;
28+ $this->pass = $pass;
29+ $this->server = $server;
30+ }
31+
32+ /**
33+ * Magic __call method, will translate all function calls to object to API requests
34+ * @param String $name name of the function
35+ * @param array $arguments an array of arguments
36+ * @return - Object from json_decode
37+ */
38+ public function __call($name, $arguments)
39+ {
40+ $this->connections(); //set paths etc at the last possible moment to allow for changes before this call is made.
41+ if (count($arguments) < 1 || !is_array($arguments[0]))
42+ $arguments[0] = array();
43+ return json_decode($this->APIcall($name, $arguments[0]));
44+ }
45+
46+ /**
47+ * function to set all the connection variables, called before APIcall
48+ */
49+ protected function connections()
50+ {
51+ $this->type = $this->ssl == 1 ? "https://" : "http://";
52+ $this->requestUrl = $this->type . $this->server . ':' . $this->port . '/execute/';
53+ $this->auth = base64_encode($user . ":" . $pass);
54+ }
55+
56+ /**
57+ * @param $name
58+ * @param $arguments
59+ * @return bool|mixed
60+ */
61+ protected function APIcall($name, $arguments)
62+ {
63+ $url = $this->requestUrl . ($this->scope != '' ? $this->scope . "/" : '') . $name . '?';
64+ foreach ($arguments as $key => $value) {
65+ $url .= $key . "=" . $value . "&";
4866 }
4967
50- /**
51- * function to set all the connection variables, called before APIcall
52- */
53- private function connections(){
54- $this->type = $this->ssl == 1 ? "https://" : "http://";
55- $this->requestUrl = $this->type.$this->server.':'.$this->port.'/execute/';
56- $this->auth = base64_encode($user .":". $pass);
68+ return $this->curl_request($url);
69+ }
70+
71+ /**
72+ * @param $url
73+ * @return bool|mixed
74+ */
75+ protected function curl_request($url)
76+ {
77+
78+ $ch = curl_init();
79+ curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
80+ curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
81+ curl_setopt($ch, CURLOPT_HEADER, 0);
82+ curl_setopt($ch, CURLOPT_URL, $url);
83+ curl_setopt($ch, CURLOPT_HTTPHEADER, array("Authorization: Basic " . $this->auth));
84+ curl_setopt($ch, CURLOPT_TIMEOUT, 100020);
85+ curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
86+
87+ $content = $this->curl_exec_follow($ch);
88+ $err = curl_errno($ch);
89+ $errmsg = curl_error($ch);
90+ $header = curl_getinfo($ch);
91+
92+ curl_close($ch);
93+
94+ $header['errno'] = $err;
95+ $header['errmsg'] = $errmsg;
96+ $header['content'] = $content;
97+
98+ return $header['content'];
99+ }
100+
101+ /**
102+ * @param $ch
103+ * @param null $maxredirect
104+ * @return bool|mixed
105+ */
106+ protected function curl_exec_follow($ch, &$maxredirect = null)
107+ {
108+
109+ // we emulate a browser here since some websites detect
110+ // us as a bot and don't let us do our job
111+ $user_agent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.5)" .
112+ " Gecko/20041107 Firefox/1.0";
113+ curl_setopt($ch, CURLOPT_USERAGENT, $user_agent);
114+
115+ $mr = $maxredirect === null ? 5 : intval($maxredirect);
116+
117+ if (ini_get('open_basedir') == '' && ini_get('safe_mode') == 'Off') {
118+
119+ curl_setopt($ch, CURLOPT_FOLLOWLOCATION, $mr > 0);
120+ curl_setopt($ch, CURLOPT_MAXREDIRS, $mr);
121+ curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
122+ curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
123+
124+ } else {
125+
126+ curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
127+
128+ if ($mr > 0) {
129+ $original_url = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);
130+ $newurl = $original_url;
131+
132+ $rch = curl_copy_handle($ch);
133+
134+ curl_setopt($rch, CURLOPT_HEADER, true);
135+ curl_setopt($rch, CURLOPT_NOBODY, true);
136+ curl_setopt($rch, CURLOPT_FORBID_REUSE, false);
137+ do {
138+ curl_setopt($rch, CURLOPT_URL, $newurl);
139+ $header = curl_exec($rch);
140+ if (curl_errno($rch)) {
141+ $code = 0;
142+ } else {
143+ $code = curl_getinfo($rch, CURLINFO_HTTP_CODE);
144+ if ($code == 301 || $code == 302) {
145+ preg_match('/Location:(.*?)\n/', $header, $matches);
146+ $newurl = trim(array_pop($matches));
147+
148+ // if no scheme is present then the new url is a
149+ // relative path and thus needs some extra care
150+ if (!preg_match("/^https?:/i", $newurl)) {
151+ $newurl = $original_url . $newurl;
152+ }
153+ } else {
154+ $code = 0;
155+ }
156+ }
157+ } while ($code && --$mr);
158+
159+ curl_close($rch);
160+
161+ if (!$mr) {
162+ if ($maxredirect === null)
163+ trigger_error('Too many redirects.', E_USER_WARNING);
164+ else
165+ $maxredirect = 0;
166+
167+ return false;
168+ }
169+ curl_setopt($ch, CURLOPT_URL, $newurl);
170+ }
57171 }
172+ return curl_exec($ch);
173+ }
174+ }
58175
59- //function to build request URL
60- private function APIcall($name, $arguments){
61- $url = $this->requestUrl.($this->scope != '' ? $this->scope."/" : '').$name.'?';
62- foreach($arguments as $key => $value){
63- $url .= $key."=".$value."&";
64- }
65- sc_utils_watch("request URL", '', $url);
66- return $this->curl_request($url);
67- }
68-
69- private function curl_request($url){
70-
71- $ch = curl_init ();
72- curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
73- curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
74- curl_setopt($ch, CURLOPT_HEADER, 0);
75- curl_setopt($ch, CURLOPT_URL, $url);
76- curl_setopt($ch, CURLOPT_HTTPHEADER, array("Authorization: Basic " . $this->auth));
77- curl_setopt($ch, CURLOPT_TIMEOUT, 100020);
78- curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
79-
80- $content = $this->curl_exec_follow($ch);
81- $err = curl_errno ($ch);
82- $errmsg = curl_error ($ch);
83- $header = curl_getinfo ($ch);
84- $httpCode = curl_getinfo ($ch, CURLINFO_HTTP_CODE);
85-
86- curl_close ($ch);
87-
88- $header['errno'] = $err;
89- $header['errmsg'] = $errmsg;
90- $header['content'] = $content;
91-
92- return $header['content'];
93- }
94-
95- private function curl_exec_follow($ch, &$maxredirect = null) {
96-
97- // we emulate a browser here since some websites detect
98- // us as a bot and don't let us do our job
99- $user_agent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.5)".
100- " Gecko/20041107 Firefox/1.0";
101- curl_setopt($ch, CURLOPT_USERAGENT, $user_agent );
102-
103- $mr = $maxredirect === null ? 5 : intval($maxredirect);
104-
105- if (ini_get('open_basedir') == '' && ini_get('safe_mode') == 'Off') {
106-
107- curl_setopt($ch, CURLOPT_FOLLOWLOCATION, $mr > 0);
108- curl_setopt($ch, CURLOPT_MAXREDIRS, $mr);
109- curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
110- curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
111-
112- } else {
113-
114- curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
115-
116- if ($mr > 0)
117- {
118- $original_url = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);
119- $newurl = $original_url;
120-
121- $rch = curl_copy_handle($ch);
122-
123- curl_setopt($rch, CURLOPT_HEADER, true);
124- curl_setopt($rch, CURLOPT_NOBODY, true);
125- curl_setopt($rch, CURLOPT_FORBID_REUSE, false);
126- do
127- {
128- curl_setopt($rch, CURLOPT_URL, $newurl);
129- $header = curl_exec($rch);
130- if (curl_errno($rch)) {
131- $code = 0;
132- } else {
133- $code = curl_getinfo($rch, CURLINFO_HTTP_CODE);
134- if ($code == 301 || $code == 302) {
135- preg_match('/Location:(.*?)\n/', $header, $matches);
136- $newurl = trim(array_pop($matches));
137-
138- // if no scheme is present then the new url is a
139- // relative path and thus needs some extra care
140- if(!preg_match("/^https?:/i", $newurl)){
141- $newurl = $original_url . $newurl;
142- }
143- } else {
144- $code = 0;
145- }
146- }
147- } while ($code && --$mr);
148-
149- curl_close($rch);
150-
151- if (!$mr)
152- {
153- if ($maxredirect === null)
154- trigger_error('Too many redirects.', E_USER_WARNING);
155- else
156- $maxredirect = 0;
157-
158- return false;
159- }
160- curl_setopt($ch, CURLOPT_URL, $newurl);
161- }
162- }
163- return curl_exec($ch);
164- }
165- }
176+ ?>
0 commit comments