├── .gitignore ├── .travis.yml ├── LICENSE.md ├── README.md ├── composer.json ├── config └── laravel-directadmin.php ├── phpunit.xml └── src ├── LaravelDirectAdmin.php ├── LaravelDirectAdminFacade.php └── LaravelDirectAdminServiceProvider.php /.gitignore: -------------------------------------------------------------------------------- 1 | /vendor/ 2 | composer.lock -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: php 2 | 3 | php: 4 | - 5.6 5 | - 7.0 6 | - 7.1 7 | - 7.2 8 | 9 | before_script: 10 | - composer self-update 11 | - composer install --prefer-source --no-interaction --dev 12 | 13 | script: phpunit 14 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | # MIT License 2 | 3 | Copyright (c) 2016 solitweb.be 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: -------------------------------------------------------------------------------- 1 | # Laravel 5 DirectAdmin API wrapper 2 | [![Latest Version](https://img.shields.io/github/release/solitweb/laravel-directadmin.svg?style=flat-square)](https://github.com/laravel-solitweb/directadmin/releases) 3 | [![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](LICENSE.md) 4 | [![Build Status](https://img.shields.io/travis/solitweb/laravel-directadmin/master.svg?style=flat-square)](https://travis-ci.org/solitweb/laravel-directadmin) 5 | [![Total Downloads](https://img.shields.io/packagist/dt/solitweb/laravel-directadmin.svg?style=flat-square)](https://packagist.org/packages/solitweb/laravel-directadmin) 6 | 7 | ## Installation 8 | 9 | You can install this package via Composer using: 10 | 11 | ```bash 12 | composer require solitweb/laravel-directadmin 13 | ``` 14 | 15 | Laravel 5.5 uses Package Auto-Discovery, so doesn't require you to manually add the ServiceProvider. 16 | 17 | ## Laravel 5.5+: 18 | 19 | If you don't use auto-discovery, add the ServiceProvider to the providers array in config/app.php 20 | 21 | ```php 22 | // config/app.php 23 | 'providers' => [ 24 | ... 25 | Solitweb\LaravelDirectAdmin\LaravelDirectAdminServiceProvider::class, 26 | ]; 27 | ``` 28 | 29 | Optionally, register the facade: 30 | 31 | ```php 32 | // config/app.php 33 | 'aliases' => [ 34 | ... 35 | 'DirectAdmin' => Solitweb\LaravelDirectAdmin\LaravelDirectAdminFacade::class, 36 | ]; 37 | ``` 38 | 39 | To publish the config file to app/config/laravel-directadmin.php run: 40 | 41 | ```bash 42 | php artisan vendor:publish --provider="Solitweb\LaravelDirectAdmin\LaravelDirectAdminServiceProvider" 43 | ``` 44 | 45 | ## Usage 46 | 47 | Import the facade at the top of your file. 48 | 49 | ```php 50 | use DirectAdmin; 51 | ``` 52 | 53 | ### Examples 54 | 55 | This will return an array of all users currently owned the reseller: 56 | 57 | ```php 58 | return DirectAdmin::get()->request('SHOW_USERS'); 59 | ``` 60 | 61 | This will return an array of the user's usages: 62 | 63 | ```php 64 | return DirectAdmin::get()->request('SHOW_USER_USAGE', ['user' => 'john']); 65 | ``` 66 | 67 | For more commands check the [DirectAdmin API docs](https://www.directadmin.com/api.php). 68 | You have to copy the command without the `CMD_API_`. 69 | 70 | ### Magic Methods 71 | 72 | It's also possible to make use of magic methods to get the data from the API as shown below: 73 | 74 | ```php 75 | $users = DirectAdmin::getShowAllUsers(); 76 | // Translates to DirectAdmin->get()->request('SHOW_ALL_USERS'); 77 | ``` 78 | 79 | Arguments are also supported when using a magic method: 80 | 81 | ```php 82 | return DirectAdmin::postAccountAdmin([ 83 | 'action' => 'create', 84 | 'username' => 'New Admin', 85 | .... 86 | ]); 87 | // Translates to DirectAdmin->post()->request('ACCOUNT_ADMIN', [arguments]); 88 | ``` 89 | 90 | Magic Methods are named after the method (get/post) followed by the command without `CMD_API_` in CamelCase. So, if you want to make a GET request with the CMD_API_SHOW_ALL_USERS command, the magic method would be `getShowAllUsers()`. 91 | 92 | ### JSON Support 93 | 94 | It's possible to use JSON support, this allows using the HTTP code for feedback. 95 | No more annoying login screen errors on invalid login parameters. 96 | 97 | ```php 98 | $data = DirectAdmin::get()->requestJson('SHOW_USERS'); 99 | $response = DirectAdmin::get_status_code(); 100 | ``` 101 | 102 | Also added magic methods support: 103 | ```php 104 | $data = DirectAdmin::getJsonShowUsers(); 105 | ``` 106 | 107 | ### Change user during runtime 108 | 109 | It's also possible to change the user during runtime as shown below: 110 | 111 | ```php 112 | DirectAdmin::set_login('username', 'password') 113 | ``` 114 | 115 | ## Credits 116 | - [Phi1 'l0rdphi1' Stier](mailto:l0rdphi1@liquenox.net) 117 | - [Joshua de Gier / Pendo](https://github.com/PendoNL) 118 | 119 | ## License 120 | 121 | The MIT License (MIT). Please see [License File](LICENSE.md) for more information. 122 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "solitweb/laravel-directadmin", 3 | "description": "Laravel 5 DirectAdmin API wrapper", 4 | "keywords": [ 5 | "solitweb", 6 | "laravel", 7 | "directadmin" 8 | ], 9 | "homepage": "https://github.com/solitweb/laravel-directadmin", 10 | "license": "MIT", 11 | "authors": [ 12 | { 13 | "name": "Stijn Vanouplines", 14 | "email": "stijn@solitweb.be", 15 | "homepage": "https://solitweb.be", 16 | "role": "Developer" 17 | } 18 | ], 19 | "require": { 20 | "php": "~5.6|~7.0|~7.1|~7.2|~7.3|~7.4|~8.0|~8.1|~8.2|~8.3", 21 | "illuminate/support": "^5.0|^6.0|^7.0|^8.0|^9.0|^10.0|^11.0", 22 | "solitweb/directadmin": "^3.0.1" 23 | }, 24 | "require-dev": { 25 | "phpunit/phpunit": "^4.8", 26 | "mockery/mockery": "^0.9.4" 27 | }, 28 | "autoload": { 29 | "psr-4": { 30 | "Solitweb\\LaravelDirectAdmin\\": "src/" 31 | } 32 | }, 33 | "autoload-dev": { 34 | "psr-4": { 35 | "Solitweb\\LaravelDirectAdmin\\Test\\": "tests" 36 | } 37 | }, 38 | "extra": { 39 | "laravel": { 40 | "providers": [ 41 | "Solitweb\\LaravelDirectAdmin\\LaravelDirectAdminServiceProvider" 42 | ], 43 | "aliases": { 44 | "DirectAdmin": "Solitweb\\LaravelDirectAdmin\\LaravelDirectAdminFacade" 45 | } 46 | } 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /config/laravel-directadmin.php: -------------------------------------------------------------------------------- 1 | env('DIRECTADMIN_DOMAIN'), 5 | 'port' => env('DIRECTADMIN_PORT', 2222), 6 | 'username' => env('DIRECTADMIN_USERNAME'), 7 | 'password' => env('DIRECTADMIN_PASSWORD'), 8 | ]; -------------------------------------------------------------------------------- /phpunit.xml: -------------------------------------------------------------------------------- 1 | 2 | 12 | 13 | 14 | ./tests/ 15 | 16 | 17 | -------------------------------------------------------------------------------- /src/LaravelDirectAdmin.php: -------------------------------------------------------------------------------- 1 | connection = $connection; 16 | } 17 | 18 | /** 19 | * Set method to GET. 20 | * 21 | * @return $this 22 | */ 23 | public function get() { 24 | $this->connection->set_method('GET'); 25 | return $this; 26 | } 27 | 28 | /** 29 | * Set method to POST. 30 | * 31 | * @return $this 32 | */ 33 | public function post() { 34 | $this->connection->set_method('POST'); 35 | return $this; 36 | } 37 | 38 | /** 39 | * Do an API request. 40 | * 41 | * @return array result parsed 42 | */ 43 | public function request($command, $options = []) 44 | { 45 | $this->connection->query('/CMD_API_'.$command, $options); 46 | return $this->connection->fetch_parsed_body(); 47 | } 48 | 49 | /** 50 | * Do an API request with JSON as an answer. 51 | * HTTP response codes are working with JSON requests. 52 | * 53 | * @return object JSON parsed 54 | */ 55 | public function requestJson($command, $options = []) 56 | { 57 | $json = ['json' => 'yes']; 58 | $options = array_merge($json, $options); 59 | $this->connection->query('/CMD_API_'.$command, $options); 60 | return json_decode($this->connection->fetch_body()); 61 | } 62 | 63 | /** 64 | * Return the last HTTP status code. 65 | * 200 = OK; 66 | * 403 = FORBIDDEN; 67 | * etc. 68 | * 69 | * @return int HTTP status code 70 | */ 71 | public function get_status_code() 72 | { 73 | return $this->connection->get_status_code(); 74 | } 75 | 76 | /** 77 | * Specify a username and password. 78 | * 79 | * @param string|null username. default is null 80 | * @param string|null password. default is null 81 | */ 82 | public function set_login($username, $password) 83 | { 84 | $this->connection->set_login($username, $password); 85 | } 86 | 87 | /** 88 | * Magic Method 89 | * 90 | * @param $methodName 91 | * @param $arguments 92 | * @return bool 93 | * @throws \Exception 94 | */ 95 | public function __call($methodName, $arguments) 96 | { 97 | $arguments = count($arguments) > 0 ? $arguments[0] : $arguments ; 98 | 99 | if(!$response = $this->extractMethod($methodName, $arguments)) { 100 | throw new \Exception("Invalid method called"); 101 | } 102 | 103 | return $response; 104 | } 105 | 106 | /** 107 | * Extract command name from magic method 108 | * 109 | * @param $methodName 110 | * @param $arguments 111 | * @return bool 112 | */ 113 | private function extractMethod($methodName, $arguments) 114 | { 115 | if(strpos(strtolower($methodName), "getjson") !== false) { 116 | return $this->extractCommand("getJson", substr($methodName, 7), $arguments); 117 | } 118 | 119 | if(strpos($methodName, "get") !== false) { 120 | return $this->extractCommand("get", substr($methodName, 3), $arguments); 121 | } 122 | 123 | if(strpos(strtolower($methodName), "postjson") !== false) { 124 | return $this->extractCommand("postJson", substr($methodName, 8), $arguments); 125 | } 126 | 127 | if(strpos($methodName, "post") !== false) { 128 | return $this->extractCommand("post", substr($methodName, 4), $arguments); 129 | } 130 | 131 | return false; 132 | } 133 | 134 | /** 135 | * Set the command based on the magic method name 136 | * 137 | * @param $method 138 | * @param $command 139 | * @param $arguments 140 | * @return array 141 | */ 142 | private function extractCommand($method, $command, $arguments) 143 | { 144 | if($method == "post" || $method == "postJson") 145 | { 146 | $this->connection->set_method("POST"); 147 | } 148 | else 149 | { 150 | $this->connection->set_method("GET"); 151 | } 152 | 153 | 154 | if($method == "postJson" || $method == "getJson") 155 | { 156 | return $this->requestJson( 157 | $this->camelToSnake($command), 158 | $arguments 159 | ); 160 | } 161 | return $this->request( 162 | $this->camelToSnake($command), 163 | $arguments 164 | ); 165 | } 166 | 167 | /** 168 | * Convert CamelCase to snake_case 169 | * 170 | * @param $string 171 | * @return string 172 | */ 173 | private function camelToSnake($string) 174 | { 175 | preg_match_all('!([A-Z][A-Z0-9]*(?=$|[A-Z][a-z0-9])|[A-Za-z][a-z0-9]+)!', $string, $matches); 176 | $ret = $matches[0]; 177 | foreach ($ret as &$match) { 178 | $match = $match == strtoupper($match) ? strtolower($match) : lcfirst($match); 179 | } 180 | return strtoupper(implode('_', $ret)); 181 | } 182 | 183 | } 184 | -------------------------------------------------------------------------------- /src/LaravelDirectAdminFacade.php: -------------------------------------------------------------------------------- 1 | mergeConfigFrom(__DIR__.'/../config/laravel-directadmin.php', 'laravel-directadmin'); 13 | 14 | $this->publishes([ 15 | __DIR__.'/../config/laravel-directadmin.php' => config_path('laravel-directadmin.php'), 16 | ]); 17 | } 18 | 19 | public function register() 20 | { 21 | $this->app->singleton(LaravelDirectAdmin::class, function () { 22 | $api = new DAConnection(); 23 | $api->connect(config('laravel-directadmin.domain'), config('laravel-directadmin.port')); 24 | $api->set_login(config('laravel-directadmin.username'), config('laravel-directadmin.password')); 25 | return new LaravelDirectAdmin($api); 26 | }); 27 | $this->app->alias(LaravelDirectAdmin::class, 'laravel-directadmin'); 28 | } 29 | } --------------------------------------------------------------------------------