Skip to content

Commit 897daf1

Browse files
committed
Merge branch 'release/1.0.0'
2 parents f09fe9b + d92015e commit 897daf1

File tree

7 files changed

+434
-2
lines changed

7 files changed

+434
-2
lines changed

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2019 WebReinvent
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 145 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,145 @@
1-
# laravel-cpanel
2-
Laravel CPanel API 2.0
1+
# Laravel CPanel
2+
3+
> Laravel Package for CPanel UAPI
4+
5+
Please consider starring the project to show your :heart: and support.
6+
7+
> This laravel package allows you to manage you CPanel based hosting using CPanel UAPI.
8+
9+
Some practical usages are:
10+
- Programmatically create database, sub domains, emails or accounts etc
11+
- Programmatically create database users
12+
- Programmatically set privileges on database of any user
13+
14+
Learn more about at [CPanel UAPI](https://documentation.cpanel.net/display/DD/Guide+to+UAPI)
15+
16+
## Installation
17+
18+
#### Step 1) Install the Package
19+
Use following composer command to install the package
20+
```bash
21+
composer require webreinvent/laravel-cpanel
22+
```
23+
or
24+
Add `webreinvent/laravel-cpanel` as a requirement to `composer.json`:
25+
26+
```
27+
{
28+
...
29+
"require": {
30+
...
31+
"webreinvent/laravel-cpanel": "dev-master"
32+
},
33+
}
34+
```
35+
36+
Update composer:
37+
38+
```
39+
$ composer update
40+
```
41+
#### Step 2) Register the ServiceProvider
42+
Add following service provider in `config/app.php`
43+
```php
44+
/*
45+
* Package Service Providers...
46+
*/
47+
'providers' => [
48+
//...
49+
WebReinvent\CPanel\CPanelServiceProvider::class,
50+
//...
51+
],
52+
```
53+
54+
#### Step 3) Publish Configurations
55+
Run following command:
56+
```
57+
php artisan vendor:publish --provider="WebReinvent\CPanel\CPanelServiceProvider" --tag=config
58+
```
59+
#### Step 4) Set CPanel details in `.env`
60+
```
61+
CPANEL_DOMAIN=
62+
CPANEL_PORT=
63+
CPANEL_API_TOKEN=
64+
CPANEL_USERNAME=
65+
```
66+
To generate `CPANEL_API_TOKEN`, login to the `CPanel >> SECURITY >> Manage API Tokens >> Create`.
67+
68+
## Usages & available methods
69+
Make sure you import:
70+
```php
71+
use WebReinvent\CPanel\CPanel;
72+
```
73+
74+
#### To Create Database
75+
Database name should be prefixed with cpanel username `cpanelusername_databasename`
76+
77+
If your CPanel username is `foo` then your database name
78+
| should be `foo_website`.
79+
80+
```php
81+
$cpanel = new CPanel();
82+
$response = $cpanel->createDatabase('cpanelusername_databasename');
83+
```
84+
Find More Details at [CPanel UAPI - Mysql::create_database](https://documentation.cpanel.net/display/DD/UAPI+Functions+-+Mysql::create_database)
85+
86+
#### To Delete Database
87+
88+
```php
89+
$cpanel = new CPanel();
90+
$response = $cpanel->deleteDatabase('cpanelusername_databasename');
91+
```
92+
93+
[CPanel UAPI - Mysql::delete_database](https://documentation.cpanel.net/display/DD/UAPI+Functions+-+Mysql%3A%3Adelete_database)
94+
95+
#### To Get List of All Databases in the CPanel
96+
97+
```php
98+
$cpanel = new CPanel();
99+
$response = $cpanel->listDatabases();
100+
```
101+
#### To Create Database User
102+
103+
```php
104+
$cpanel = new CPanel();
105+
$response = $cpanel->createDatabaseUser($username, $password);
106+
```
107+
#### To Delete Database User
108+
109+
```php
110+
$cpanel = new CPanel();
111+
$response = $cpanel->deleteDatabaseUser($username);
112+
```
113+
114+
#### To Give All Privileges to a Database User On a Database
115+
116+
```php
117+
$cpanel = new CPanel();
118+
$response = $cpanel->setAllPrivilegesOnDatabase($database_user, $database_name);
119+
```
120+
121+
Those where the available method but you can call all the method available at [CPanel UAPI](https://documentation.cpanel.net/display/DD/Guide+to+UAPI) using following method:
122+
```php
123+
$cpanel = new CPanel();
124+
$response = $cpanel->callUAPI($Module, $function, $parameters_array);
125+
```
126+
Example if you want to add new `ftp` account, documetation is available at [CPanel UAPI - Ftp::add_ftp](https://documentation.cpanel.net/display/DD/UAPI+Functions+-+Ftp%3A%3Aadd_ftp) then use the method as represented below:
127+
```php
128+
$cpanel = new CPanel();
129+
$Method = 'Ftp';
130+
$function = 'add_ftp';
131+
$parameters_array = [
132+
'user'=>'ftp_username',
133+
'pass'=>'ftp_password', //make sure you use strong password
134+
'quota'=>'42',
135+
];
136+
$response = $cpanel->callUAPI($Module, $function, $parameters_array);
137+
```
138+
## Support us
139+
140+
[WebReinvent](https://www.webreinvent.com) is a web agency based in Delhi, India. You'll find an overview of all our open source projects [on github](https://github.com/webreinvent).
141+
142+
## License
143+
144+
The MIT License (MIT). Please see [License File](LICENSE) for more information.
145+

composer.json

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"name": "webreinvent/laravel-cpanel",
3+
"description": "Laravel Package of CPanel API 2.0",
4+
"keywords": ["laravel", "cpanel", "cpanel api", "cpanel uapi"],
5+
"homepage": "https://www.webreinvent.com",
6+
"license": "MIT",
7+
"version": "1.0.0",
8+
"authors": [
9+
{
10+
"name": "WebReinvent",
11+
"email": "we@webreinvent.com"
12+
}
13+
],
14+
"require": {
15+
"php": "^7.2"
16+
},
17+
"autoload": {
18+
"psr-4": {
19+
"WebReinvent\\CPanel\\": "src/"
20+
},
21+
"files": [
22+
]
23+
}
24+
}

src/CPanel.php

Lines changed: 197 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,197 @@
1+
<?php
2+
/**
3+
* CPanel
4+
*
5+
* @author: Pradeep Kumar
6+
* This package uses cPanel API 2.0
7+
*/
8+
9+
namespace WebReinvent\CPanel;
10+
11+
use Config;
12+
13+
class CPanel {
14+
15+
protected $config;
16+
protected $protocol;
17+
protected $domain;
18+
protected $port;
19+
protected $user;
20+
protected $token;
21+
22+
23+
//-----------------------------------------------------
24+
25+
public function __construct()
26+
{
27+
$this->config = Config::get('cpanel');
28+
29+
$this->protocol = $this->config['protocol'];
30+
$this->domain = $this->config['domain'];
31+
$this->port = $this->config['port'];
32+
$this->username = $this->config['username'];
33+
$this->token = $this->config['api_token'];
34+
35+
}
36+
37+
//-----------------------------------------------------
38+
39+
public function createSubDomain($subdomain, $rootdomain, $dir)
40+
{
41+
$module = "SubDomain";
42+
$function = "addsubdomain";
43+
$parameters = array(
44+
'domain' => $subdomain,
45+
'rootdomain' => $rootdomain,
46+
'canoff' => 0,
47+
'dir' => $dir,
48+
'disallowdot' => 0
49+
);
50+
return $this->call($module, $function, $parameters);
51+
}
52+
//-----------------------------------------------------
53+
54+
//-----------------------------------------------------
55+
56+
public function createDatabase($database_name)
57+
{
58+
$module = "Mysql";
59+
$function = "create_database";
60+
$parameters = array(
61+
'name' => $database_name
62+
);
63+
return $this->call($module, $function, $parameters);
64+
}
65+
66+
//-----------------------------------------------------
67+
public function deleteDatabase($database_name)
68+
{
69+
$module = "Mysql";
70+
$function = "delete_database";
71+
$parameters = array(
72+
'name' => $database_name
73+
);
74+
return $this->call($module, $function, $parameters);
75+
}
76+
//-----------------------------------------------------
77+
public function listDatabases()
78+
{
79+
$module = "Mysql";
80+
$function = "list_databases";
81+
$parameters = array(
82+
);
83+
return $this->call($module, $function, $parameters);
84+
}
85+
//-----------------------------------------------------
86+
public function createDatabaseUser($username, $password)
87+
{
88+
$module = "Mysql";
89+
$function = "create_user";
90+
$parameters = array(
91+
'name' => $username,
92+
'password' => $password,
93+
);
94+
return $this->call($module, $function, $parameters);
95+
}
96+
97+
//-----------------------------------------------------
98+
public function deleteDatabaseUser($username)
99+
{
100+
$module = "Mysql";
101+
$function = "delete_database";
102+
$parameters = array(
103+
'name' => $username
104+
);
105+
return $this->call($module, $function, $parameters);
106+
}
107+
//-----------------------------------------------------
108+
public function setAllPrivilegesOnDatabase($database_user, $database_name)
109+
{
110+
$module = "Mysql";
111+
$function = "set_privileges_on_database";
112+
$parameters = array(
113+
'user' => $database_user,
114+
'database' => $database_name,
115+
'privileges' => 'ALL PRIVILEGES',
116+
);
117+
return $this->call($module, $function, $parameters);
118+
}
119+
//-----------------------------------------------------
120+
121+
122+
//-----------------------------------------------------
123+
public function callUAPI($Module, $function, $parameters_array = array())
124+
{
125+
$this->call($Module, $function, $parameters_array);
126+
}
127+
//-----------------------------------------------------
128+
129+
public function call($module, $function, $args = array())
130+
{
131+
$parameters = '';
132+
if ( count($args) > 0 ) {
133+
foreach( $args as $key => $value ) {
134+
$parameters .= '&' . $key . '=' . $value;
135+
}
136+
}
137+
138+
$url = $this->protocol.'://'.$this->domain . ':' . $this->port . '/execute/' . $module;
139+
$url .= "/".$function;
140+
$url .= '?'. $parameters;
141+
142+
$headers = array(
143+
"Authorization: cpanel " . $this->username . ':' . $this->token,
144+
"cache-control: no-cache"
145+
);
146+
147+
$curl = curl_init();
148+
149+
curl_setopt_array($curl, array(
150+
CURLOPT_PORT => $this->port,
151+
CURLOPT_URL => $url,
152+
CURLOPT_RETURNTRANSFER => true,
153+
CURLOPT_ENCODING => "",
154+
CURLOPT_MAXREDIRS => 10,
155+
CURLOPT_TIMEOUT => 30,
156+
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
157+
CURLOPT_CUSTOMREQUEST => "GET",
158+
CURLOPT_POSTFIELDS => "",
159+
CURLOPT_HTTPHEADER => $headers,
160+
));
161+
162+
$response = curl_exec($curl);
163+
$err = curl_error($curl);
164+
165+
curl_close($curl);
166+
167+
168+
if ($err) {
169+
170+
$response['status'] = 'failed';
171+
$response['errors'] = $err;
172+
$response['inputs']['url'] = $url;
173+
174+
} else {
175+
176+
$res = json_decode($response);
177+
178+
$response = [];
179+
if($res->status == 0)
180+
{
181+
$response['status'] = 'failed';
182+
$response['errors'][] = $res->errors;
183+
$response['inputs']['url'] = $url;
184+
} else
185+
{
186+
$response['status'] = 'success';
187+
$response['data'] = $res;
188+
$response['inputs']['url'] = $url;
189+
}
190+
}
191+
192+
return $response;
193+
}
194+
195+
//-----------------------------------------------------
196+
197+
}

0 commit comments

Comments
 (0)