/var/www/(Del)hsihk.com/wp-content/plugins/backupbuddy/lib/cpanel/cpanel.php


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
<?php
/*
EXAMPLE:

require_once( pb_backupbuddy::plugin_path() . '/lib/cpanel/cpanel.php' );
 
$cpanel_user = pb_backupbuddy::_GET( 'user' );
$cpanel_password = pb_backupbuddy::_GET( 'pass' );
$cpanel_host = "foo.com";
$db_name = 'apples';
$db_user = 'oranges';
$db_pass = 'bananas';
$create_db_result = pb_backupbuddy_cpanel::create_db( $cpanel_user, $cpanel_password, $cpanel_host, $db_name, $db_user, $db_pass );

if ( $create_db_result === true ) {
    echo 'Success! Created database, user, and assigned used to database.';
} else {
    echo 'Error(s):<br><pre>' . print_r( $create_db_result, true ) . '</pre>';
}

*/



/*    pb_backupbuddy_cpanel Class
 *    
 *    Manage some cpanel settings.
 *    
 *    @author        Dustin Bolton <http://dustinbolton.com> Sept 2012. Updated for api2 by Jeremy.
 */
class pb_backupbuddy_cpanel {


    
// TODO: Use more robust than file_get_contents().

    
    /*    create_db()
     *    
     *    Create a database and assign a user to it with all privilages.
     *    
     *    @param        
     *    @return        true|array        Boolean true on success, else an array of errors.
     */
    
public static function create_db$cpanel_user$cpanel_password$cpanel_host$db_name$db_user$db_userpass$cpanel_port '2082' ) {
        
$cpanel_skin "x3";
        
$errors = array();
        
        
$cpanel_password urlencode$cpanel_password ); // Pass often has special chars so encode.
        
        // Calculate base URL.
//         $base_url = "http://{$cpanel_user}:{$cpanel_password}@{$cpanel_host}:{$cpanel_port}/frontend/{$cpanel_skin}";
        
$base_url "http://{$cpanel_user}:{$cpanel_password}@{$cpanel_host}:{$cpanel_port}/execute/Mysql";
        
        
// Generate create database URL.
//         $create_database_url = $base_url . "/sql/addb.html?db={$db_name}";
        
$create_database_url $base_url "/create_database?name={$cpanel_user}_{$db_name}";
        
//echo $create_database_url . '<br>';
        
        
        // Create request core obj for connecting to HTTP.
        
$request = new RequestCore$create_database_url );
        try {
            
$result $request->send_requesttrue );
        } catch (
Exception $e) {
            if ( 
stristr$e->getMessage(), 'couldn\'t connect to host' ) !== false ) {
                
$errors[] = 'Unable to connect to host `' $cpanel_host '` on port `' $cpanel_port '`. Verify the cPanel domain/URL and make sure the server is able to initiate outgoing http connections on port ' $cpanel_port '. Some hosts block this.';
                return 
$errors;
            }
            
$errors[] = 'Caught exception: ' $e->getMessage();
            return 
$errors;
        }
        
        
        
// Generate create database user URL.
//         $create_user_url = $base_url . "/sql/adduser.html?user={$db_user}&pass={$db_userpass}";
        
$create_user_url $base_url "/create_user?name={$cpanel_user}_{$db_user}&password={$db_userpass}";
        
//echo $create_user_url . '<br>';
        
        // Generate assign user database access URL.
//         $assign_user_url = $base_url . "/sql/addusertodb.html?user={$cpanel_user}_{$db_user}&db={$cpanel_user}_{$db_name}&ALL=ALL";
        
$assign_user_url $base_url "/set_privileges_on_database?user={$cpanel_user}_{$db_user}&database={$cpanel_user}_{$db_name}&privileges=ALL";
        
//echo $assign_user_url . '<br>';
        
        
if ( false === $result->isOK() ) {
            
$errors[] = 'Unable to create database - response status code: ' .  $result->status;
        } else {    
            
$result_array json_decode$result->bodytrue );
            if ( isset( 
$result_array'status' ] ) && ( == $result_array'status' ] ) ) {
                
// status = 0 means a failure
                
$errors[] = 'Unable to create database:';
                if ( isset( 
$result_array'errors' ] ) && ( is_array$result_array'errors' ] ) ) ) {
                    foreach ( 
$result_array'errors' ] as $error ) {
                        
$errors[] = $error;
                    }
                }
            }
        }
        
//         if ( stristr( $result, 'Log in' ) !== false ) { // No sucess adding DB.
//             $errors[] = 'Unable to log into cPanel with given username/password. Verify the credentials are correct for this cPanel domain.';
//         }
//         if ( stristr( $result, 'Added the Database' ) === false ) { // No sucess adding DB.
//             $errors[] = 'Error encountered adding database.';
//         }
//         if ( stristr( $result, 'problem creating the database' ) !== false ) { // Something failed.
//             $errors[] = 'Unable to create database.';
//         }
//         if ( stristr( $result, 'database name already exists' ) !== false ) { // DB already exists.
//             $errors[] = 'The database name already exists.';
//         }
        
        
        // Run create database user.
        
if ( count$errors ) === ) {
            
$request = new RequestCore$create_user_url );
            try {
                
$result $request->send_requesttrue );
            } catch (
Exception $e) {
                
$errors[] = 'Caught exception: ' $e->getMessage();
                return 
$errors;
            }
            
            if ( 
false === $result->isOK() ) {
                
$errors[] = 'Unable to creaet user - response status code: ' .  $result->status;
            } else {    
                
$result_array json_decode$result->bodytrue );
                if ( isset( 
$result_array'status' ] ) && ( == $result_array'status' ] ) ) {
                    
// status = 0 means a failure
                    
$errors[] = 'Unable to create user:';
                    if ( isset( 
$result_array'errors' ] ) && ( is_array$result_array'errors' ] ) ) ) {
                        foreach ( 
$result_array'errors' ] as $error ) {
                            
$errors[] = $error;
                        }
                    }
                }
            }
        
//             if ( stristr( $result, 'Added user' ) === false ) { // No success adding user.
//                 $errors[] = 'Error encountered adding user.';
//             }
//             if ( stristr( $result, 'You have successfully created a MySQL user' ) === false ) { // No success adding user.
//                 $errors[] = 'Error encountered adding user.';
//             }
//             if ( stristr( $result, 'No password given' ) !== false ) { // Already exists.
//                 $errors[] = 'No password given.';
//             }
//             if ( stristr( $result, 'exists in the database' ) !== false ) { // Already exists.
//                 $errors[] = 'Username already exists.';
//             }
        
}
        
        
// Run assign user to database.
        
if ( count$errors ) === ) {
            
$request = new RequestCore$assign_user_url );
            try {
                
$result $request->send_requesttrue );
            } catch (
Exception $e) {
                
$errors[] = 'Caught exception: ' $e->getMessage();
                return 
$errors;
            }
            
            if ( 
false === $result->isOK() ) {
                
$errors[] = 'Unable to set privileges for user - response status code: ' .  $result->status;
            } else {    
                
$result_array json_decode$result->bodytrue );
                if ( isset( 
$result_array'status' ] ) && ( == $result_array'status' ] ) ) {
                    
// status = 0 means a failure
                    
$errors[] = 'Unable to set privileges for user:';
                    if ( isset( 
$result_array'errors' ] ) && ( is_array$result_array'errors' ] ) ) ) {
                        foreach ( 
$result_array'errors' ] as $error ) {
                            
$errors[] = $error;
                        }
                    }
                }
            }
        
//             if ( stristr( $result, 'was added to the database' ) === false ) { // No success adding user.
//                 $errors[] = 'Error encountered assigning user to database.';
//             }
        
}
        
        if ( 
count$errors ) > ) { // One or more errors.
            
return $errors;
        } else {
            return 
true// Success!
        
}
        
    }

// end class.