Skip to content

Commit ec68341

Browse files
Updated namespacing for composer
1 parent c85ff04 commit ec68341

File tree

5 files changed

+185
-149
lines changed

5 files changed

+185
-149
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
vendor/
2+
dist/

README.md

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,17 @@
1-
# cPanel
1+
# cPanel UAPI PHP Wrapper
22
A simple, lightweight library to access JSON cPanel v2 and cPanel UAPI in PHP
3+
4+
## Documentation
5+
For documentation and video tutorials, please refer [here](https://www.myphpnotes.com/post/cpanel-uapi-and-api-php-integration "here").
6+
7+
8+
9+
### Buy me a coffee
10+
[![](https://img.buymeacoffee.com/api/?url=aHR0cHM6Ly9pbWcuYnV5bWVhY29mZmVlLmNvbS9hcGkvP25hbWU9YWRuYW50dXJraSZzaXplPTMwMCZiZy1pbWFnZT1ibWMmYmFja2dyb3VuZD1mZjgxM2Y=&creator=adnanturki&is_creating=building%20cool%20things%20every%20single%20f**king%20day.&design_code=1&design_color=%23ff813f&slug=adnanturki)](https://www.buymeacoffee.com/adnanturki)
11+
12+
### How to contribute
13+
- Create a fork, make changes and send a pull request.
14+
- Raise a issue
15+
16+
### License
17+
Licensed under Apache 2.0. You can check its details [here](https://choosealicense.com/licenses/apache-2.0/ "here").

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"description": "A simple, lightweight library to access JSON cPanel v2 and cPanel UAPI in PHP",
44
"type": "library",
55
"keywords": ["api", "http", "rest", "cPanel", "UAPI", "api2"],
6-
"homepage": "https://www.myphpnotes.tk",
6+
"homepage": "https://www.myphpnotes.com",
77
"require": {
88
"php": ">=5.6.0"
99
},
@@ -16,7 +16,7 @@
1616
"authors": [
1717
{
1818
"name": "Adnan Hussain Turki",
19-
"email": "[email protected]"
19+
"email": "[email protected]"
2020
}
2121
]
2222
}

composer.lock

Lines changed: 20 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 145 additions & 146 deletions
Original file line numberDiff line numberDiff line change
@@ -1,146 +1,145 @@
1-
<?php
2-
3-
namespace myPHPnotes;
4-
5-
/**
6-
* Controlling cPanel
7-
* PHP module for the cPanel JSON API
8-
* @author Adnan Hussain Turki <[email protected]>
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 <[email protected]>
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

Comments
 (0)