|
1 | | -<?php |
2 | | - |
3 | | -namespace myPHPnotes; |
4 | | - |
5 | | -/** |
6 | | -* Controlling cPanel |
7 | | -* PHP module for the cPanel JSON API |
8 | | -* @author Adnan Hussain Turki <adnan@myphpnotes.tk> |
9 | | -* @copyright Copyright (c) 2017 myPHPnotes |
10 | | -* |
11 | | -*/ |
12 | | -class cPanel |
13 | | -{ |
14 | | - private $host; |
15 | | - private $port; |
16 | | - private $username; |
17 | | - private $password; |
18 | | - private $log; |
19 | | - private $cFile; |
20 | | - private $curlfile; |
21 | | - private $emailArray; |
22 | | - private $cpsess; |
23 | | - private $homepage; |
24 | | - private $exPage; |
25 | | - |
26 | | - function __construct($username, $password, $host, $port = 2083, $log = false) |
27 | | - { |
28 | | - $this->host = $host; |
29 | | - $this->port = $port; |
30 | | - $this->username = $username; |
31 | | - $this->password = $password; |
32 | | - $this->log = $log; |
33 | | - $this->cFile = "cookies/cookie_".rand(99999, 9999999).".txt"; |
34 | | - $this->signIn(); |
35 | | - } |
36 | | - // Makes an HTTP request |
37 | | - private function Request($url,$params=array()){ |
38 | | - if($this->log){ |
39 | | - $curl_log = fopen($this->curlfile, 'a+'); |
40 | | - } |
41 | | - if(!file_exists($this->cFile)){ |
42 | | - try{ |
43 | | - fopen($this->cFile, "w"); |
44 | | - }catch(Exception $ex){ |
45 | | - if(!file_exists($this->cFile)){ |
46 | | - echo $ex.'Cookie file missing.'; exit; |
47 | | - } |
48 | | - } |
49 | | - }else if(!is_writable($this->cFile)){ |
50 | | - echo 'Cookie file not writable.'; exit; |
51 | | - } |
52 | | - $ch = curl_init(); |
53 | | - $curlOpts = array( |
54 | | - CURLOPT_URL => $url, |
55 | | - CURLOPT_USERAGENT => 'Mozilla/5.0 (Windows NT 6.3; WOW64; rv:29.0) Gecko/20100101 Firefox/29.0', |
56 | | - CURLOPT_SSL_VERIFYPEER => false, |
57 | | - CURLOPT_SSL_VERIFYHOST => false, |
58 | | - CURLOPT_RETURNTRANSFER => true, |
59 | | - CURLOPT_COOKIEJAR => realpath($this->cFile), |
60 | | - CURLOPT_COOKIEFILE => realpath($this->cFile), |
61 | | - CURLOPT_FOLLOWLOCATION => true, |
62 | | - CURLOPT_HTTPHEADER => array( |
63 | | - "Host: ".$this->host, |
64 | | - "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8", |
65 | | - "Accept-Language: en-US,en;q=0.5", |
66 | | - "Accept-Encoding: gzip, deflate", |
67 | | - "Connection: keep-alive", |
68 | | - "Content-Type: application/x-www-form-urlencoded") |
69 | | - ); |
70 | | - if(!empty($params)){ |
71 | | - $curlOpts[CURLOPT_POST] = true; |
72 | | - $curlOpts[CURLOPT_POSTFIELDS] = $params; |
73 | | - } |
74 | | - if($this->log){ |
75 | | - $curlOpts[CURLOPT_STDERR] = $curl_log; |
76 | | - $curlOpts[CURLOPT_FAILONERROR] = false; |
77 | | - $curlOpts[CURLOPT_VERBOSE] = true; |
78 | | - } |
79 | | - curl_setopt_array($ch,$curlOpts); |
80 | | - $answer = curl_exec($ch); |
81 | | - if (curl_error($ch)) { |
82 | | - echo curl_error($ch); exit; |
83 | | - } |
84 | | - curl_close($ch); |
85 | | - if($this->log){ |
86 | | - fclose($curl_log); |
87 | | - } |
88 | | - return (@gzdecode($answer)) ? gzdecode($answer) : $answer; |
89 | | - } |
90 | | - // Function to start a session at cPanel |
91 | | - private function signIn() { |
92 | | - $url = 'https://'.$this->host.":".$this->port."/login/?login_only=1"; |
93 | | - $url .= "&user=".$this->username."&pass=".urlencode($this->password); |
94 | | - $reply = $this->Request($url); |
95 | | - $reply = json_decode($reply, true); |
96 | | - if(isset($reply['status']) && $reply['status'] == 1){ |
97 | | - $this->cpsess = $reply['security_token']; |
98 | | - $this->homepage = 'https://'.$this->host.":".$this->port.$reply['redirect']; |
99 | | - $this->exPage = 'https://'.$this->host.":".$this->port. "/{$this->cpsess}/execute/"; |
100 | | - } |
101 | | - else { |
102 | | - throw new Exception("Cannot connect to your cPanel server : Invalid Credentials", 1); |
103 | | - } |
104 | | - } |
105 | | - public function execute($api, $module, $function, array $parameters) |
106 | | - { |
107 | | - switch ($api) { |
108 | | - case 'api2': |
109 | | - return $this->api2($module, $function, $parameters); |
110 | | - break; |
111 | | - case 'uapi': |
112 | | - return $this->uapi($module, $function, $parameters); |
113 | | - break; |
114 | | - default: |
115 | | - throw new Exception("Invalid API type : api2 and uapi are accepted", 1); |
116 | | - break; |
117 | | - } |
118 | | - } |
119 | | - // UAPI Handler |
120 | | - public function uapi($module, $function, array $parameters = []) |
121 | | - { |
122 | | - if (count($parameters) < 1) { |
123 | | - $parameters = ""; |
124 | | - } else { |
125 | | - $parameters = (http_build_query($parameters)); |
126 | | - } |
127 | | - |
128 | | - return json_decode($this->Request($this->exPage . $module . "/" . $function . "?" . $parameters)); |
129 | | - |
130 | | - } |
131 | | - // API 2 Handler |
132 | | - public function api2($module, $function, array $parameters = []) |
133 | | - { |
134 | | - if (count($parameters) < 1) { |
135 | | - $parameters = ""; |
136 | | - } else { |
137 | | - $parameters = (http_build_query($parameters)); |
138 | | - } |
139 | | - $url = "https://".$this->host.":".$this->port.$this->cpsess."/json-api/cpanel". |
140 | | - "?cpanel_jsonapi_version=2". |
141 | | - "&cpanel_jsonapi_func={$function}". |
142 | | - "&cpanel_jsonapi_module={$module}&". $parameters; |
143 | | - return json_decode($this->Request($url,$parameters)); |
144 | | - } |
145 | | - |
146 | | -} |
| 1 | +<?php |
| 2 | + |
| 3 | +namespace myPHPnotes; |
| 4 | +/** |
| 5 | +* Controlling cPanel |
| 6 | +* PHP module for the cPanel JSON API |
| 7 | +* @author Adnan Hussain Turki <adnan@myphpnotes.tk> |
| 8 | +* @copyright Copyright (c) 2017 myPHPnotes |
| 9 | +* |
| 10 | +*/ |
| 11 | +class cPanel |
| 12 | +{ |
| 13 | + private $host; |
| 14 | + private $port; |
| 15 | + private $username; |
| 16 | + private $password; |
| 17 | + private $log; |
| 18 | + private $cFile; |
| 19 | + private $curlfile; |
| 20 | + private $emailArray; |
| 21 | + private $cpsess; |
| 22 | + private $homepage; |
| 23 | + private $exPage; |
| 24 | + |
| 25 | + function __construct($username, $password, $host, $port = 2083, $log = false) |
| 26 | + { |
| 27 | + $this->host = $host; |
| 28 | + $this->port = $port; |
| 29 | + $this->username = $username; |
| 30 | + $this->password = $password; |
| 31 | + $this->log = $log; |
| 32 | + $this->cFile = "cookies/cookie_".rand(99999, 9999999).".txt"; |
| 33 | + $this->signIn(); |
| 34 | + } |
| 35 | + // Makes an HTTP request |
| 36 | + private function Request($url,$params=array()){ |
| 37 | + if($this->log){ |
| 38 | + $curl_log = fopen($this->curlfile, 'a+'); |
| 39 | + } |
| 40 | + if(!file_exists($this->cFile)){ |
| 41 | + try{ |
| 42 | + fopen($this->cFile, "w"); |
| 43 | + }catch(Exception $ex){ |
| 44 | + if(!file_exists($this->cFile)){ |
| 45 | + echo $ex.'Cookie file missing.'; exit; |
| 46 | + } |
| 47 | + } |
| 48 | + }else if(!is_writable($this->cFile)){ |
| 49 | + echo 'Cookie file not writable.'; exit; |
| 50 | + } |
| 51 | + $ch = curl_init(); |
| 52 | + $curlOpts = array( |
| 53 | + CURLOPT_URL => $url, |
| 54 | + CURLOPT_USERAGENT => 'Mozilla/5.0 (Windows NT 6.3; WOW64; rv:29.0) Gecko/20100101 Firefox/29.0', |
| 55 | + CURLOPT_SSL_VERIFYPEER => false, |
| 56 | + CURLOPT_SSL_VERIFYHOST => false, |
| 57 | + CURLOPT_RETURNTRANSFER => true, |
| 58 | + CURLOPT_COOKIEJAR => realpath($this->cFile), |
| 59 | + CURLOPT_COOKIEFILE => realpath($this->cFile), |
| 60 | + CURLOPT_FOLLOWLOCATION => true, |
| 61 | + CURLOPT_HTTPHEADER => array( |
| 62 | + "Host: ".$this->host, |
| 63 | + "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8", |
| 64 | + "Accept-Language: en-US,en;q=0.5", |
| 65 | + "Accept-Encoding: gzip, deflate", |
| 66 | + "Connection: keep-alive", |
| 67 | + "Content-Type: application/x-www-form-urlencoded") |
| 68 | + ); |
| 69 | + if(!empty($params)){ |
| 70 | + $curlOpts[CURLOPT_POST] = true; |
| 71 | + $curlOpts[CURLOPT_POSTFIELDS] = $params; |
| 72 | + } |
| 73 | + if($this->log){ |
| 74 | + $curlOpts[CURLOPT_STDERR] = $curl_log; |
| 75 | + $curlOpts[CURLOPT_FAILONERROR] = false; |
| 76 | + $curlOpts[CURLOPT_VERBOSE] = true; |
| 77 | + } |
| 78 | + curl_setopt_array($ch,$curlOpts); |
| 79 | + $answer = curl_exec($ch); |
| 80 | + if (curl_error($ch)) { |
| 81 | + echo curl_error($ch); exit; |
| 82 | + } |
| 83 | + curl_close($ch); |
| 84 | + if($this->log){ |
| 85 | + fclose($curl_log); |
| 86 | + } |
| 87 | + return (@gzdecode($answer)) ? gzdecode($answer) : $answer; |
| 88 | + } |
| 89 | + // Function to start a session at cPanel |
| 90 | + private function signIn() { |
| 91 | + $url = 'https://'.$this->host.":".$this->port."/login/?login_only=1"; |
| 92 | + $url .= "&user=".$this->username."&pass=".urlencode($this->password); |
| 93 | + $reply = $this->Request($url); |
| 94 | + $reply = json_decode($reply, true); |
| 95 | + if(isset($reply['status']) && $reply['status'] == 1){ |
| 96 | + $this->cpsess = $reply['security_token']; |
| 97 | + $this->homepage = 'https://'.$this->host.":".$this->port.$reply['redirect']; |
| 98 | + $this->exPage = 'https://'.$this->host.":".$this->port. "/{$this->cpsess}/execute/"; |
| 99 | + } |
| 100 | + else { |
| 101 | + throw new Exception("Cannot connect to your cPanel server : Invalid Credentials", 1); |
| 102 | + } |
| 103 | + } |
| 104 | + public function execute($api, $module, $function, array $parameters) |
| 105 | + { |
| 106 | + switch ($api) { |
| 107 | + case 'api2': |
| 108 | + return $this->api2($module, $function, $parameters); |
| 109 | + break; |
| 110 | + case 'uapi': |
| 111 | + return $this->uapi($module, $function, $parameters); |
| 112 | + break; |
| 113 | + default: |
| 114 | + throw new Exception("Invalid API type : api2 and uapi are accepted", 1); |
| 115 | + break; |
| 116 | + } |
| 117 | + } |
| 118 | + // UAPI Handler |
| 119 | + public function uapi($module, $function, array $parameters = []) |
| 120 | + { |
| 121 | + if (count($parameters) < 1) { |
| 122 | + $parameters = ""; |
| 123 | + } else { |
| 124 | + $parameters = (http_build_query($parameters)); |
| 125 | + } |
| 126 | + |
| 127 | + return json_decode($this->Request($this->exPage . $module . "/" . $function . "?" . $parameters)); |
| 128 | + |
| 129 | + } |
| 130 | + // API 2 Handler |
| 131 | + public function api2($module, $function, array $parameters = []) |
| 132 | + { |
| 133 | + if (count($parameters) < 1) { |
| 134 | + $parameters = ""; |
| 135 | + } else { |
| 136 | + $parameters = (http_build_query($parameters)); |
| 137 | + } |
| 138 | + $url = "https://".$this->host.":".$this->port.$this->cpsess."/json-api/cpanel". |
| 139 | + "?cpanel_jsonapi_version=2". |
| 140 | + "&cpanel_jsonapi_func={$function}". |
| 141 | + "&cpanel_jsonapi_module={$module}&". $parameters; |
| 142 | + return json_decode($this->Request($url,$parameters)); |
| 143 | + } |
| 144 | + |
| 145 | +} |
0 commit comments