├── 1.png ├── 2.png ├── .styleci.yml ├── CHANGELOG.md ├── config └── config.php ├── src ├── CpanelFacade.php ├── CpanelServiceProvider.php └── Cpanel.php ├── LICENSE.md ├── .github └── workflows │ └── main.yml ├── composer.json ├── CONTRIBUTING.md └── README.md /1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MHasnainJafri/cpanel/HEAD/1.png -------------------------------------------------------------------------------- /2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MHasnainJafri/cpanel/HEAD/2.png -------------------------------------------------------------------------------- /.styleci.yml: -------------------------------------------------------------------------------- 1 | preset: laravel 2 | 3 | disabled: 4 | - single_class_element_per_statement 5 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Changelog 2 | 3 | All notable changes to `cpanel` will be documented in this file 4 | 5 | ## 1.0.0 - 201X-XX-XX 6 | 7 | - initial release 8 | -------------------------------------------------------------------------------- /config/config.php: -------------------------------------------------------------------------------- 1 | 'https', 8 | 'domain' => env('CPANEL_DOMAIN'), 9 | 'port' => env('CPANEL_PORT', 2083), 10 | 'api_token' => env('CPANEL_API_TOKEN'), 11 | 'username' => env('CPANEL_USERNAME'), 12 | ]; -------------------------------------------------------------------------------- /src/CpanelFacade.php: -------------------------------------------------------------------------------- 1 | loadTranslationsFrom(__DIR__.'/../resources/lang', 'cpanel'); 18 | // $this->loadViewsFrom(__DIR__.'/../resources/views', 'cpanel'); 19 | // $this->loadMigrationsFrom(__DIR__.'/../database/migrations'); 20 | // $this->loadRoutesFrom(__DIR__.'/routes.php'); 21 | 22 | if ($this->app->runningInConsole()) { 23 | $this->publishes([ 24 | __DIR__.'/../config/config.php' => config_path('cpanel.php'), 25 | ], 'config'); 26 | 27 | // Publishing the views. 28 | /*$this->publishes([ 29 | __DIR__.'/../resources/views' => resource_path('views/vendor/cpanel'), 30 | ], 'views');*/ 31 | 32 | // Publishing assets. 33 | /*$this->publishes([ 34 | __DIR__.'/../resources/assets' => public_path('vendor/cpanel'), 35 | ], 'assets');*/ 36 | 37 | // Publishing the translation files. 38 | /*$this->publishes([ 39 | __DIR__.'/../resources/lang' => resource_path('lang/vendor/cpanel'), 40 | ], 'lang');*/ 41 | 42 | // Registering package commands. 43 | // $this->commands([]); 44 | } 45 | } 46 | 47 | /** 48 | * Register the application services. 49 | */ 50 | public function register() 51 | { 52 | // Automatically apply the package configuration 53 | $this->mergeConfigFrom(__DIR__.'/../config/config.php', 'cpanel'); 54 | 55 | // Register the main class to use with the facade 56 | $this->app->singleton('cpanel', function () { 57 | return new Cpanel; 58 | }); 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Contributing 2 | 3 | Contributions are **welcome** and will be fully **credited**. 4 | 5 | Please read and understand the contribution guide before creating an issue or pull request. 6 | 7 | ## Etiquette 8 | 9 | This project is open source, and as such, the maintainers give their free time to build and maintain the source code 10 | held within. They make the code freely available in the hope that it will be of use to other developers. It would be 11 | extremely unfair for them to suffer abuse or anger for their hard work. 12 | 13 | Please be considerate towards maintainers when raising issues or presenting pull requests. Let's show the 14 | world that developers are civilized and selfless people. 15 | 16 | It's the duty of the maintainer to ensure that all submissions to the project are of sufficient 17 | quality to benefit the project. Many developers have different skillsets, strengths, and weaknesses. Respect the maintainer's decision, and do not be upset or abusive if your submission is not used. 18 | 19 | ## Viability 20 | 21 | When requesting or submitting new features, first consider whether it might be useful to others. Open 22 | source projects are used by many developers, who may have entirely different needs to your own. Think about 23 | whether or not your feature is likely to be used by other users of the project. 24 | 25 | ## Procedure 26 | 27 | Before filing an issue: 28 | 29 | - Attempt to replicate the problem, to ensure that it wasn't a coincidental incident. 30 | - Check to make sure your feature suggestion isn't already present within the project. 31 | - Check the pull requests tab to ensure that the bug doesn't have a fix in progress. 32 | - Check the pull requests tab to ensure that the feature isn't already in progress. 33 | 34 | Before submitting a pull request: 35 | 36 | - Check the codebase to ensure that your feature doesn't already exist. 37 | - Check the pull requests to ensure that another person hasn't already submitted the feature or fix. 38 | 39 | ## Requirements 40 | 41 | If the project maintainer has any additional requirements, you will find them listed here. 42 | 43 | - **[PSR-2 Coding Standard](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-2-coding-style-guide.md)** - The easiest way to apply the conventions is to install [PHP Code Sniffer](https://pear.php.net/package/PHP_CodeSniffer). 44 | 45 | - **Add tests!** - Your patch won't be accepted if it doesn't have tests. 46 | 47 | - **Document any change in behaviour** - Make sure the `README.md` and any other relevant documentation are kept up-to-date. 48 | 49 | - **Consider our release cycle** - We try to follow [SemVer v2.0.0](https://semver.org/). Randomly breaking public APIs is not an option. 50 | 51 | - **One pull request per feature** - If you want to do more than one thing, send multiple pull requests. 52 | 53 | - **Send coherent history** - Make sure each individual commit in your pull request is meaningful. If you had to make multiple intermediate commits while developing, please [squash them](https://www.git-scm.com/book/en/v2/Git-Tools-Rewriting-History#Changing-Multiple-Commit-Messages) before submitting. 54 | 55 | **Happy coding**! 56 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | [![License](https://img.shields.io/github/license/MHasnainJafri/cpanel.svg)](https://github.com/MHasnainJafri/cpanel/blob/master/LICENSE) 2 | [![Open Issues](https://img.shields.io/github/issues/MHasnainJafri/cpanel.svg)](https://github.com/MHasnainJafri/cpanel/issues) 3 | [![Stars](https://img.shields.io/github/stars/MHasnainJafri/cpanel.svg)](https://github.com/MHasnainJafri/cpanel/stargazers) 4 | [![Forks](https://img.shields.io/github/forks/MHasnainJafri/cpanel.svg)](https://github.com/MHasnainJafri/cpanel/network/members) 5 | 6 | Library Name: Laravel CPanel API 7 | 8 | This library provides a convenient way to interact with the cPanel API using Laravel. It allows you to perform various cPanel functions such as creating subdomains, managing databases, handling email accounts, and managing disk usage. 9 | 10 | Installation 11 | 12 | To use this library in your Laravel project, you can install it via Composer. Run the following command in your terminal: 13 | 14 | ``` 15 | 16 | composer require mhasnainjafri/cpanel 17 | 18 | ``` 19 | ### Usage 20 | 21 | To get started, you need to create a new instance of the Cpanel class. There are two ways to initialize the class: 22 | 23 | 1. Without any parameters: 24 | ```php 25 | $cpanel = new Cpanel(); 26 | ``` 27 | 2. With parameters (if not defined in `.env` file): 28 | ```php 29 | $cpanel = new Cpanel($cpanel_domain, $cpanel_api_token, $cpanel_username, 'https', 2083); 30 | ``` 31 | Alternatively, you can set the cPanel credentials in the `.env` file: 32 | ``` 33 | CPANEL_DOMAIN=domain e.g : xyz.com 34 | CPANEL_PORT=cpanel port (mostly is) 2083 35 | CPANEL_API_TOKEN= Cpanel api token (To get api token view step 3) 36 | CPANEL_USERNAME= Cpanel username 37 | ``` 38 | 3. ### Steps to get Cpanel Api 39 | 40 | - search api in cpanel search bar 41 | - click on ```Manage api token``` 42 | ![step 1](1.png) 43 | - Give token a name and click on create 44 | ![step 2](2.png) 45 | - Copy the token you get 46 | 47 | 48 | ### Subdomain Functions 49 | 50 | To create a subdomain, use the `createSubDomain()` method: 51 | 52 | ```php 53 | 54 | $cpanel->createSubDomain('Sub domain e.g: web', 'domain e.g: xyz.com', 'Path where subdomain points e.g: /home/domain/public_html/subdomain'); 55 | 56 | ``` 57 | 58 | ### Database Functions 59 | 60 | To list all databases, use the `listDatabases()` method: 61 | 62 | ```php 63 | 64 | $cpanel->listDatabases(); 65 | 66 | ``` 67 | 68 | To create a new database, use the `createDatabase()` method: 69 | 70 | ```php 71 | 72 | $cpanel->createDatabase('Database Name'); 73 | 74 | ``` 75 | 76 | To delete a database, use the `deleteDatabase()` method: 77 | 78 | ```php 79 | 80 | $cpanel->deleteDatabase('Database Name'); 81 | 82 | ``` 83 | 84 | To create a database user, use the `createDatabaseUser()` method: 85 | 86 | ```php 87 | 88 | $cpanel->createDatabaseUser('Database username', 'password'); 89 | 90 | ``` 91 | 92 | To delete a database user, use the `deleteDatabaseUser()` method: 93 | 94 | ```php 95 | 96 | $cpanel->deleteDatabaseUser('Database username'); 97 | 98 | ``` 99 | 100 | To set all privileges on a database for a user, use the `setAllPrivilegesOnDatabase()` method: 101 | 102 | ```php 103 | 104 | $cpanel->setAllPrivilegesOnDatabase('Database username', 'Database Name'); 105 | 106 | ``` 107 | 108 | ### Mail Functions 109 | 110 | To create a POP email account, use the `createPopEmailAccount()` method: 111 | 112 | ```php 113 | 114 | $cpanel->createPopEmailAccount('mail@domain.com', 'password'); 115 | 116 | ``` 117 | 118 | To delete a POP email account, use the `deletePopEmailAccount()` method: 119 | 120 | ```php 121 | 122 | $cpanel->deletePopEmailAccount('mail@domain.com'); 123 | 124 | ``` 125 | 126 | To edit the mailbox quota for an email account, use the `editMailboxQuota()` method: 127 | 128 | ```php 129 | 130 | $cpanel->editMailboxQuota($email, $domain, $quota); 131 | 132 | ``` 133 | 134 | To dispatch client settings for an email account, use the `dispatchClientSettings()` method: 135 | 136 | ```php 137 | 138 | $cpanel->dispatchClientSettings('mail@domain.com', 'account'); 139 | 140 | ``` 141 | 142 | To get the POP email count, use the `getPopEmailCount()` method: 143 | 144 | ```php 145 | 146 | $cpanel->getPopEmailCount(); 147 | 148 | ``` 149 | 150 | ### cPanel Disk Management 151 | 152 | To get cPanel statistics bar stats for bandwidth usage or disk usage, use the `getCpanelStatsBarStats()` method: 153 | 154 | ```php 155 | 156 | $cpanel->getCpanelStatsBarStats('bandwidthusage|diskusage'); 157 | 158 | ``` 159 | 160 | To get cPanel disk quota information, use the `getCpanelDiskQuotaInfo()` method: 161 | 162 | ```php 163 | 164 | $cpanel->getCpanelDiskQuotaInfo(); 165 | 166 | ``` 167 | 168 | ### Other API Functions 169 | 170 | To call any other cPanel API function, use the `callUAPI()` method: 171 | 172 | ```php 173 | 174 | $cpanel->callUAPI($Module, $function, $parameters_array = array()); 175 | 176 | ``` 177 | 178 | Replace `$Module` with the desired module, `$function` with the function name, and `$parameters_array` with any additional parameters required by the API function. 179 | 180 | That's it! You can now leverage the power of the Laravel CPanel API library to interact with the cPanel API seamlessly in your Laravel applications. 181 | 182 | 183 | 184 | ### Testing 185 | 186 | ```bash 187 | composer test 188 | ``` 189 | 190 | ### Changelog 191 | 192 | Please see [CHANGELOG](CHANGELOG.md) for more information what has changed recently. 193 | 194 | ## Contributing 195 | 196 | Please see [CONTRIBUTING](CONTRIBUTING.md) for details. 197 | 198 | ### Security 199 | 200 | If you discover any security related issues, please email mhasnainjafri0099@gmail.com instead of using the issue tracker. 201 | 202 | ## Credits 203 | 204 | - [Muhammad Hasnain](https://github.com/MHasnainJafri) 205 | - [Muhammad Anas](https://github.com/MHasnainJafri) 206 | 207 | 208 | ## License 209 | 210 | The MIT License (MIT). Please see [License File](LICENSE.md) for more information. 211 | 212 | ## Laravel Package Boilerplate 213 | 214 | This package was generated using the [Laravel Package Boilerplate](https://laravelpackageboilerplate.com). 215 | -------------------------------------------------------------------------------- /src/Cpanel.php: -------------------------------------------------------------------------------- 1 | config = Config::get('cpanel'); 20 | 21 | // Check if any of the parameters are provided, otherwise use the values from the config array 22 | $this->protocol = $cpanel_domain ? $protocol : $this->config['protocol']; 23 | $this->port = $cpanel_domain ? $port : $this->config['port']; 24 | $this->domain = $cpanel_domain ?? $this->config['domain']; 25 | $this->username = $cpanel_domain ? $cpanel_username : $this->config['username']; 26 | $this->token = $cpanel_domain ? $cpanel_api_token : $this->config['api_token']; 27 | } 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | /** 36 | * Get the cPanel disk quota information for the authenticated user. 37 | * 38 | * @return array An array with the API response, API URL, status, and error information. 39 | */ 40 | public function getCpanelDiskQuotaInfo() 41 | { 42 | // Set the cPanel API module and function for getting disk quota info 43 | $module = "Quota"; 44 | $function = "get_local_quota_info"; 45 | 46 | // Call the CpanelApi function to perform the cPanel API request 47 | return $this->CpanelApi($module, $function); 48 | } 49 | 50 | /** 51 | * Get statistics from the cPanel Stats Bar API endpoint. 52 | * 53 | * @param string $display The type of statistics to retrieve, separated by a vertical bar (|). 54 | * @return array An array with the API response, API URL, status, and error information. 55 | */ 56 | public function getCpanelStatsBarStats($display) 57 | { 58 | // Set the cPanel API module and function for getting statistics from the StatsBar 59 | $module = "StatsBar"; 60 | $function = "get_stats"; 61 | 62 | // Prepare the parameters for the cPanel API call 63 | $parameters = array( 64 | 'display' => $display, 65 | ); 66 | 67 | // Call the CpanelApi function to perform the cPanel API request 68 | return $this->CpanelApi($module, $function, $parameters); 69 | } 70 | 71 | /** 72 | * Create a new POP email account. 73 | * 74 | * @param string $username The username for the email account (e.g., 'noreply@xyz.com'). 75 | * @param string $password The password for the email account. 76 | * @return array An array with the API response, API URL, status, and error information. 77 | */ 78 | public function createPopEmailAccount($username, $password) 79 | { 80 | // Set the cPanel API module and function for adding POP email account 81 | $module = "Email"; 82 | $function = "add_pop"; 83 | 84 | // Prepare the parameters for the cPanel API call 85 | $parameters = array( 86 | 'email' => $username, // We use the username as the email prefix 87 | 'password' => $password, 88 | ); 89 | 90 | // Call the CpanelApi function to perform the cPanel API request 91 | return $this->CpanelApi($module, $function, $parameters); 92 | } 93 | 94 | 95 | /** 96 | * Get the count of POP email accounts. 97 | * 98 | * @return array An array with the API response, API URL, status, and error information. 99 | */ 100 | public function getPopEmailCount() 101 | { 102 | // Set the cPanel API module and function for getting POP email account count 103 | $module = "Email"; 104 | $function = "count_pops"; 105 | 106 | // Call the CpanelApi function to perform the cPanel API request 107 | return $this->CpanelApi($module, $function); 108 | } 109 | 110 | 111 | 112 | /** 113 | * Delete held email messages for a specific email account. 114 | * 115 | * @param string $email The email address for which held messages should be deleted (e.g., 'username@example.com'). 116 | * @return array An array with the API response, API URL, status, and error information. 117 | */ 118 | public function deleteHeldMessages($email) 119 | { 120 | // Set the cPanel API module and function for deleting held messages 121 | $module = "Email"; 122 | $function = "delete_held_messages"; 123 | 124 | // Prepare the parameters for the cPanel API call 125 | $parameters = array( 126 | 'email' => $email, 127 | ); 128 | 129 | // Call the CpanelApi function to perform the cPanel API request 130 | return $this->CpanelApi($module, $function, $parameters); 131 | } 132 | 133 | /** 134 | * Delete a POP email account for the specified email address. 135 | * 136 | * @param string $email The email address for which the POP account should be deleted (e.g., 'user@domain.com'). 137 | * @return array An array with the API response, API URL, status, and error information. 138 | */ 139 | public function deletePopEmailAccount($email) 140 | { 141 | // Set the cPanel API module and function for deleting a POP email account 142 | $module = "Email"; 143 | $function = "delete_pop"; 144 | 145 | // Prepare the parameters for the cPanel API call 146 | $parameters = array( 147 | 'email' => $email, 148 | ); 149 | 150 | // Call the CpanelApi function to perform the cPanel API request 151 | return $this->CpanelApi($module, $function, $parameters); 152 | } 153 | 154 | 155 | /** 156 | * Send email client configuration settings to a specified email address. 157 | * 158 | * @param string $email The email address to which the client settings should be sent (e.g., 'user@example.com'). 159 | * @param string $account The cPanel account username associated with the email address (e.g., 'username'). 160 | * @return array An array with the API response, API URL, status, and error information. 161 | */ 162 | public function dispatchClientSettings($email, $account) 163 | { 164 | // Set the cPanel API module and function for sending email client settings 165 | $module = "Email"; 166 | $function = "dispatch_client_settings"; 167 | 168 | // Prepare the parameters for the cPanel API call 169 | $parameters = array( 170 | 'to' => $email, 171 | 'account' => $account, 172 | ); 173 | 174 | // Call the CpanelApi function to perform the cPanel API request 175 | return $this->CpanelApi($module, $function, $parameters); 176 | } 177 | 178 | 179 | 180 | /** 181 | * Edit the mailbox quota for a specific email account under a domain. 182 | * 183 | * @param string $email The email account (e.g., 'user'). 184 | * @param string $domain The domain associated with the email account (e.g., 'example.com'). 185 | * @param int $quota The new mailbox quota size in megabytes. 186 | * @return array An array with the API response, API URL, status, and error information. 187 | */ 188 | public function editMailboxQuota($email, $domain, $quota) 189 | { 190 | // Set the cPanel API module and function for editing the mailbox quota 191 | $module = "Email"; 192 | $function = "edit_pop_quota"; 193 | 194 | // Prepare the parameters for the cPanel API call 195 | $parameters = array( 196 | 'email' => $email, 197 | 'domain' => $domain, 198 | 'quota' => $quota, 199 | ); 200 | 201 | // Call the CpanelApi function to perform the cPanel API request 202 | return $this->CpanelApi($module, $function, $parameters); 203 | } 204 | 205 | 206 | /** 207 | * Creates a subdomain on a cPanel hosting account. 208 | * 209 | * @param string $subdomain The name of the subdomain to be created. 210 | * @param string $rootdomain The root domain under which the subdomain will be added. 211 | * @param string $dir The directory path where the subdomain files will be stored (relative to the root domain). 212 | * @return array|null The result of the cPanel API call or null if an error occurs. 213 | */ 214 | public function createSubDomain($subdomain, $rootdomain, $dir) 215 | { 216 | // Set the cPanel API module and function for adding a subdomain 217 | $module = "SubDomain"; 218 | $function = "addsubdomain"; 219 | 220 | // Prepare the parameters for the cPanel API call 221 | $parameters = array( 222 | 'domain' => $subdomain, // The name of the subdomain to be created 223 | 'rootdomain' => $rootdomain, // The root domain under which the subdomain will be added 224 | 'canoff' => 0, // Can park or addon domain flag (0: disable, 1: enable) 225 | 'dir' => $dir, // The directory path where the subdomain files will be stored 226 | 'disallowdot' => 0 // Disallow period (dot) in subdomain name flag (0: allow, 1: disallow) 227 | ); 228 | 229 | // Make the cPanel API call and return the result 230 | return $this->CpanelApi($module, $function, $parameters); 231 | } 232 | 233 | 234 | /** 235 | * Creates a new database in cPanel. 236 | * 237 | * @param string $database_name The name of the database with the prefix of domain to be created. 238 | * @return array An array with the API response or error information. 239 | */ 240 | public function createDatabase($database_name) 241 | { 242 | // Set the cPanel API module and function for creating a database 243 | $module = "Mysql"; 244 | $function = "create_database"; 245 | 246 | // Prepare the parameters for the cPanel API call 247 | $parameters = array( 248 | 'name' => $database_name // The name of the database to be created 249 | ); 250 | 251 | // Make the cPanel API call and return the result 252 | return $this->CpanelApi($module, $function, $parameters); 253 | } 254 | 255 | 256 | /** 257 | * Deletes an existing database in cPanel. 258 | * 259 | * @param string $database_name The name of the database to be deleted. 260 | * @return array An array with the API response or error information. 261 | */ 262 | public function deleteDatabase($database_name) 263 | { 264 | // Set the cPanel API module and function for deleting a database 265 | $module = "Mysql"; 266 | $function = "delete_database"; 267 | 268 | // Prepare the parameters for the cPanel API call 269 | $parameters = array( 270 | 'name' => $database_name // The name of the database to be deleted 271 | ); 272 | 273 | // Make the cPanel API call and return the result 274 | return $this->CpanelApi($module, $function, $parameters); 275 | } 276 | 277 | /** 278 | * Retrieves a list of databases available in cPanel. 279 | * 280 | * @return array An array with the API response containing the list of databases or error information. 281 | */ 282 | public function listDatabases() 283 | { 284 | // Set the cPanel API module and function for listing databases 285 | $module = "Mysql"; 286 | $function = "list_databases"; 287 | 288 | // No specific parameters needed for listing databases, so an empty array is used 289 | $parameters = array(); 290 | 291 | // Make the cPanel API call and return the result 292 | return $this->CpanelApi($module, $function, $parameters); 293 | } 294 | 295 | 296 | /** 297 | * Creates a new database user in cPanel. 298 | * 299 | * @param string $username The username of the database user to be created. 300 | * @param string $password The password for the new database user. 301 | * @return array An array with the API response or error information. 302 | */ 303 | public function createDatabaseUser($username, $password) 304 | { 305 | // Set the cPanel API module and function for creating a database user 306 | $module = "Mysql"; 307 | $function = "create_user"; 308 | 309 | // Prepare the parameters for the cPanel API call 310 | $parameters = array( 311 | 'name' => $username, // The username of the database user to be created 312 | 'password' => $password // The password for the new database user 313 | ); 314 | 315 | // Make the cPanel API call and return the result 316 | return $this->CpanelApi($module, $function, $parameters); 317 | } 318 | 319 | /** 320 | * Deletes an existing database user in cPanel. 321 | * 322 | * @param string $username The username of the database user to be deleted. 323 | * @return array An array with the API response or error information. 324 | */ 325 | public function deleteDatabaseUser($username) 326 | { 327 | // Set the cPanel API module and function for deleting a database user 328 | $module = "Mysql"; 329 | $function = "delete_user"; 330 | 331 | // Prepare the parameters for the cPanel API call 332 | $parameters = array( 333 | 'name' => $username // The username of the database user to be deleted 334 | ); 335 | 336 | // Make the cPanel API call and return the result 337 | return $this->CpanelApi($module, $function, $parameters); 338 | } 339 | 340 | 341 | /** 342 | * Sets all privileges for a database user on a specific database in cPanel. 343 | * 344 | * @param string $database_user The username of the database user. 345 | * @param string $database_name The name of the database. 346 | * @return array An array with the API response or error information. 347 | */ 348 | public function setAllPrivilegesOnDatabase($database_user, $database_name) 349 | { 350 | // Set the cPanel API module and function for setting privileges on a database 351 | $module = "Mysql"; 352 | $function = "set_privileges_on_database"; 353 | 354 | // Prepare the parameters for the cPanel API call 355 | $parameters = array( 356 | 'user' => $database_user, // The username of the database user 357 | 'database' => $database_name, // The name of the database 358 | 'privileges' => 'ALL PRIVILEGES', // Set all privileges for the user on the database 359 | ); 360 | 361 | // Make the cPanel API call and return the result 362 | return $this->CpanelApi($module, $function, $parameters); 363 | } 364 | 365 | /** 366 | * Generic function to call a cPanel UAPI (User-API) method. 367 | * This function acts as a wrapper for the 'call' method. 368 | * 369 | * @param string $Module The cPanel UAPI module name. 370 | * @param string $function The cPanel UAPI function name. 371 | * @param array $parameters_array The array of parameters to pass to the cPanel UAPI call. 372 | * @return array An array with the API response or error information. 373 | */ 374 | public function callUAPI($Module, $function, $parameters_array = array()) 375 | { 376 | return $this->CpanelApi($Module, $function, $parameters_array); 377 | } 378 | 379 | 380 | /** 381 | * Performs a cURL request to the cPanel API. 382 | * 383 | * @param string $module The cPanel API module to be called. 384 | * @param string $function The function within the cPanel API module to be executed. 385 | * @param array $args The parameters to be passed to the cPanel API call. 386 | * @return array An array with the API response, status, and error information. 387 | */ 388 | public function CpanelApi($module, $function, $args = array()) 389 | { 390 | // Prepare the parameters for the cPanel API call 391 | $parameters = ''; 392 | if (count($args) > 0) { 393 | foreach ($args as $key => $value) { 394 | $parameters .= '&' . $key . '=' . urlencode($value); 395 | } 396 | } 397 | 398 | // Construct the API call URL 399 | $url = $this->protocol . '://' . $this->domain . ':' . $this->port . '/execute/' . $module; 400 | $url .= "/" . $function; 401 | if (count($args) > 0) { 402 | $url .= '?' . $parameters; 403 | } 404 | 405 | // Set headers for the cURL request 406 | $headers = array( 407 | "Authorization: cpanel " . $this->username . ':' . $this->token, 408 | "cache-control: no-cache" 409 | ); 410 | 411 | // Initialize cURL 412 | $curl = curl_init(); 413 | 414 | // Configure cURL options 415 | curl_setopt_array($curl, array( 416 | CURLOPT_PORT => $this->port, 417 | CURLOPT_URL => $url, 418 | CURLOPT_RETURNTRANSFER => true, 419 | CURLOPT_ENCODING => "", 420 | CURLOPT_MAXREDIRS => 10, 421 | CURLOPT_TIMEOUT => 30, 422 | CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, 423 | CURLOPT_CUSTOMREQUEST => "GET", 424 | CURLOPT_POSTFIELDS => "", 425 | CURLOPT_HTTPHEADER => $headers, 426 | )); 427 | 428 | // Disable SSL verification (for localhost or self-signed certificates) 429 | curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0); 430 | curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); 431 | 432 | // Execute the cURL request and capture the response 433 | $curl_res = curl_exec($curl); 434 | $err = curl_error($curl); 435 | $err_no = curl_errno($curl); 436 | $header_size = curl_getinfo($curl, CURLINFO_HEADER_SIZE); 437 | $header = substr($curl_res, 0, $header_size); 438 | $body = substr($curl_res, $header_size); 439 | 440 | // Close the cURL session 441 | curl_close($curl); 442 | 443 | // Decode the cURL response 444 | $curl_response_decoded = json_decode($curl_res); 445 | 446 | // Prepare the response array 447 | $response = array(); 448 | 449 | // Store API call details and response data in the response array 450 | $response['inputs']['url'] = $url; 451 | $response['curl_response'] = array( 452 | 'curl_response' => $curl_res, 453 | 'curl_response_decoded' => $curl_response_decoded, 454 | 'header_size' => $header_size, 455 | 'headers' => $headers, 456 | 'header' => $header, 457 | 'body' => $body, 458 | 'error' => $err, 459 | 'err_no' => $err_no, 460 | ); 461 | 462 | // Check for cURL errors and handle them 463 | if (!empty($err_no)) { 464 | $response['status'] = 'failed'; 465 | $response['errors'] = [$err]; 466 | return $response; 467 | } 468 | 469 | // Check for API errors and handle them 470 | if (isset($curl_response_decoded->errors) && count($curl_response_decoded->errors) > 0) { 471 | $response['status'] = 'failed'; 472 | if (is_object($curl_response_decoded->errors)) { 473 | $curl_response_decoded->errors = (array) $curl_response_decoded->errors; 474 | } 475 | if (is_array($curl_response_decoded->errors)) { 476 | $response['errors'] = $curl_response_decoded->errors; 477 | } else { 478 | $response['errors'] = [$curl_response_decoded->errors]; 479 | } 480 | return $response; 481 | } else { 482 | // If no errors found, check for response status 483 | if (isset($curl_response_decoded->status) && $curl_response_decoded->status == 0) { 484 | $response['status'] = 'failed'; 485 | $response['errors'] = [$curl_response_decoded->errors]; 486 | return $response; 487 | } else { 488 | $response['data'] = json_decode($curl_res); 489 | $response['status'] = 'success'; 490 | return $response; 491 | } 492 | } 493 | } 494 | } 495 | --------------------------------------------------------------------------------