1+ <?php
2+ class cpanelUAPI
3+ {
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]));
48+ }
49+
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);
57+ }
58+
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+ }
0 commit comments