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