CPanel::deleteDatabaseUser()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 9

Duplication

Lines 9
Ratio 100 %

Importance

Changes 0
Metric Value
dl 9
loc 9
rs 9.9666
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace Keensoen\CPanelApi;
4
5
use Config;
6
7
class CPanel
8
{
9
    protected $config;
10
    protected $protocol;
11
    protected $domain;
12
    protected $port;
13
    protected $username;
14
    protected $token;
15
16
17
    //-----------------------------------------------------
18
19
    public function __construct($cpanel_domain=null, $cpanel_api_token=null, $cpanel_username=null, $protocol='https', $port=2083)
20
    {
21
22
        $this->config = Config::get('cpanel-api');
23
24
        if(isset($cpanel_domain))
25
        {
26
            $this->protocol = $protocol;
27
            $this->port = $port;
28
            $this->domain = $cpanel_domain;
29
            $this->username = $cpanel_username;
30
            $this->token = $cpanel_api_token;
31
        } else{
32
            $this->protocol = $this->config['protocol'];
33
            $this->domain = $this->config['domain'];
34
            $this->port = $this->config['port'];
35
            $this->username = $this->config['username'];
36
            $this->token = $this->config['api_token'];
37
        }
38
39
40
    }
41
42
    //-----------------------------------------------------
43
44
    public function getEmailAccounts() 
45
    {
46
        $module = "Email";
47
        $function = "list_pops";
48
        $parameters = array();
49
        return $this->call($module, $function, $parameters);
50
    }
51
52
    //-----------------------------------------------------
53
54 View Code Duplication
    public function createEmailAccount($username, $password) 
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
55
    {
56
        $module = 'Email';
57
        $function = 'add_pop';
58
        $parameters = array(
59
            'email' =>  $username,
60
            'password'  => $password,
61
            'quota' =>  '1024'
62
        );
63
64
        return $this->call($module, $function, $parameters);
65
    }
66
67
    //-----------------------------------------------------
68
69 View Code Duplication
    public function deleteEmailAccount($email)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
70
    {
71
        $module = 'Email';
72
        $function = 'delete_pop';
73
        $parameters = array(
74
            'email' =>  $email
75
        );
76
77
        return $this->call($module, $function, $parameters);
78
    }
79
80
    //-----------------------------------------------------
81
82 View Code Duplication
    public function increaseQuota($email, $quota)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
83
    {
84
        $module = 'Email';
85
        $function = 'edit_pop_quota';
86
        $parameters = array(
87
            'email' => $email, 
88
            'quota' =>  $quota
89
        );
90
91
        return $this->call($module, $function, $parameters);
92
    }
93
94
    //-----------------------------------------------------
95
96 View Code Duplication
    public function getDiskUsage($email)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
97
    {
98
        $module = 'Email';
99
        $function = 'get_disk_usage';
100
        $parameters = array(
101
            'user'  =>  $email
102
        );
103
104
        return $this->call($module, $function, $parameters);
105
    }
106
107
    //-----------------------------------------------------
108
109
    public function createSubDomain($subdomain, $rootdomain, $dir)
110
    {
111
        $module = "SubDomain";
112
        $function = "addsubdomain";
113
        $parameters = array(
114
            'domain'        => $subdomain,
115
            'rootdomain'    => $rootdomain,
116
            'canoff'        => 0,
117
            'dir'           => $dir,
118
            'disallowdot'   => 0
119
        );
120
        return $this->call($module, $function, $parameters);
121
    }
122
    //-----------------------------------------------------
123
124
    //-----------------------------------------------------
125
126
    public function createDatabase($database_name)
127
    {
128
        $module = "Mysql";
129
        $function = "create_database";
130
        $parameters = array(
131
            'name'    => $database_name
132
        );
133
        return $this->call($module, $function, $parameters);
134
    }
135
136
    //-----------------------------------------------------
137
138 View Code Duplication
    public function deleteDatabase($database_name)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
139
    {
140
        $module = "Mysql";
141
        $function = "delete_database";
142
        $parameters = array(
143
            'name'    => $database_name
144
        );
145
        return $this->call($module, $function, $parameters);
146
    }
147
148
    //-----------------------------------------------------
149
150
    public function listDatabases()
151
    {
152
        $module = "Mysql";
153
        $function = "list_databases";
154
        $parameters = array(
155
        );
156
        return $this->call($module, $function, $parameters);
157
    }
158
159
    //-----------------------------------------------------
160
161 View Code Duplication
    public function createDatabaseUser($username, $password)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
162
    {
163
        $module = "Mysql";
164
        $function = "create_user";
165
        $parameters = array(
166
            'name'    => $username,
167
            'password'    => $password,
168
        );
169
        return $this->call($module, $function, $parameters);
170
    }
171
172
    //-----------------------------------------------------
173
174 View Code Duplication
    public function deleteDatabaseUser($username)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
175
    {
176
        $module = "Mysql";
177
        $function = "delete_database";
178
        $parameters = array(
179
            'name'    => $username
180
        );
181
        return $this->call($module, $function, $parameters);
182
    }
183
184
    //-----------------------------------------------------
185
186
    public function setAllPrivilegesOnDatabase($database_user, $database_name)
187
    {
188
        $module = "Mysql";
189
        $function = "set_privileges_on_database";
190
        $parameters = array(
191
            'user'    => $database_user,
192
            'database'    => $database_name,
193
            'privileges'    => 'ALL PRIVILEGES',
194
        );
195
        return $this->call($module, $function, $parameters);
196
    }
197
198
    //-----------------------------------------------------
199
200
    //-----------------------------------------------------
201
    public function callUAPI($Module, $function, $parameters_array = array())
202
    {
203
        return $this->call($Module, $function, $parameters_array);
204
    }
205
206
    //-----------------------------------------------------
207
208
    public function call($module, $function, $args = array())
209
    {
210
        $parameters = '';
211
        if ( count($args) > 0 ) {
212
            foreach( $args as $key => $value ) {
213
                $parameters .= '&' . $key . '=' . $value;
214
            }
215
        }
216
217
        $url = $this->protocol.'://'.$this->domain . ':' . $this->port . '/execute/' . $module;
218
        $url .= "/".$function;
219
220
        if(count($args) > 0)
221
        {
222
            $url .= '?'. $parameters;
223
        }
224
225
        $headers = array(
226
            "Authorization: cpanel " . $this->username . ':' . $this->token,
227
            "cache-control: no-cache"
228
        );
229
230
        $curl = curl_init();
231
232
        curl_setopt_array($curl, array(
233
            CURLOPT_PORT => $this->port,
234
            CURLOPT_URL => $url,
235
            CURLOPT_RETURNTRANSFER => true,
236
            CURLOPT_ENCODING => "",
237
            CURLOPT_MAXREDIRS => 10,
238
            CURLOPT_TIMEOUT => 30,
239
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
240
            CURLOPT_CUSTOMREQUEST => "GET",
241
            CURLOPT_POSTFIELDS => "",
242
            CURLOPT_HTTPHEADER => $headers,
243
        ));
244
245
        $response = curl_exec($curl);
246
        $err = curl_error($curl);
247
248
        curl_close($curl);
249
250
251
        if ($err) {
252
253
            $response['status'] = 'failed';
254
            $response['errors'] = $err;
255
            $response['inputs']['url'] = $url;
256
257
        } else {
258
259
            $res = json_decode($response);
260
261
            $response = [];
262
            if(isset($res) && isset($res->status) && $res->status == 0)
263
            {
264
                $response['status'] = 'failed';
265
                $response['errors'][] = $res->errors;
266
                $response['inputs']['url'] = $url;
267
            } else
268
            {
269
                $response['status'] = 'success';
270
                $response['data'] = $res;
271
                $response['inputs']['url'] = $url;
272
            }
273
        }
274
275
        return $response;
276
    }
277
278
    //-----------------------------------------------------
279
}
280