├── .github └── workflows │ └── ci.yaml ├── .gitignore ├── .php-cs-fixer.php ├── .pre-commit-config.yaml ├── LICENSE ├── README.md ├── composer.json ├── phpunit.xml ├── src ├── Client.php ├── Exception │ ├── MismatchedStatusesException.php │ ├── MofhClientException.php │ └── MofhClientHttpException.php └── Message │ ├── AbstractResponse.php │ ├── AvailabilityResponse.php │ ├── CreateAccountResponse.php │ ├── GetCnameResponse.php │ ├── GetDomainUserResponse.php │ ├── GetUserDomainsResponse.php │ ├── PasswordResponse.php │ ├── SuspendResponse.php │ └── UnsuspendResponse.php └── tests ├── ClientTest.php └── Message ├── AvailabilityResponseTest.php ├── CreateAccountResponseTest.php ├── GetCnameResponseTest.php ├── GetDomainUserResponseTest.php ├── GetUserDomainsResponseTest.php ├── PasswordResponseTest.php ├── SuspendResponseTest.php └── UnsuspendResponseTest.php /.github/workflows/ci.yaml: -------------------------------------------------------------------------------- 1 | name: PHPUnit 2 | 3 | on: [push] 4 | 5 | jobs: 6 | phpunit: 7 | runs-on: ubuntu-latest 8 | 9 | strategy: 10 | matrix: 11 | php_version: 12 | - "7.3" 13 | - "7.4" 14 | - "8.0" 15 | - "8.1" 16 | - "8.2" 17 | - "8.3" 18 | 19 | steps: 20 | - uses: actions/checkout@v3 21 | - uses: php-actions/composer@v6 22 | with: 23 | php_version: "${{ matrix.php_version }}" 24 | - uses: php-actions/phpunit@v3 25 | with: 26 | configuration: "phpunit.xml" 27 | php_version: "${{ matrix.php_version }}" 28 | version: "9" 29 | 30 | pre-commit: 31 | runs-on: ubuntu-latest 32 | 33 | steps: 34 | - uses: actions/checkout@v3 35 | - uses: php-actions/composer@v6 36 | - uses: actions/setup-python@v3 37 | - uses: pre-commit/action@v3.0.0 38 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | composer.phar 2 | /vendor/ 3 | /coverage/ 4 | /.phpunit.result.cache 5 | /.phpunit.cache/ 6 | /.php-cs-fixer.cache 7 | 8 | # Commit your application's lock file http://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file 9 | # You may choose to ignore a library lock file http://getcomposer.org/doc/02-libraries.md#lock-file 10 | composer.lock 11 | -------------------------------------------------------------------------------- /.php-cs-fixer.php: -------------------------------------------------------------------------------- 1 | setRules([ 5 | 'braces' => false, 6 | ]); 7 | 8 | return $config; 9 | -------------------------------------------------------------------------------- /.pre-commit-config.yaml: -------------------------------------------------------------------------------- 1 | repos: 2 | - repo: https://github.com/pre-commit/pre-commit-hooks 3 | rev: v4.4.0 4 | hooks: 5 | - id: check-added-large-files 6 | - id: check-case-conflict 7 | - id: check-executables-have-shebangs 8 | - id: check-json 9 | - id: check-merge-conflict 10 | - id: check-shebang-scripts-are-executable 11 | - id: check-symlinks 12 | - id: check-toml 13 | - id: check-xml 14 | - id: check-yaml 15 | - id: destroyed-symlinks 16 | - id: detect-private-key 17 | - id: end-of-file-fixer 18 | - id: fix-byte-order-marker 19 | - id: mixed-line-ending 20 | - id: trailing-whitespace 21 | 22 | - repo: https://github.com/InfinityFreeHosting/pre-commit-php 23 | rev: 1.3.1 24 | hooks: 25 | - id: php-lint 26 | # - id: php-cs 27 | # - id: php-cbf 28 | - id: php-cs-fixer 29 | args: 30 | - --config=.php-cs-fixer.php 31 | # - id: pint 32 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "{}" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright {yyyy} {name of copyright owner} 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # MyOwnFreeHost API Client 2 | 3 | An API client to use the free hosting system from [MyOwnFreeHost](https://myownfreehost.net). 4 | 5 | **IMPORTANT: THIS LIBRARY IS AIMED AT EXPERIENCED PHP DEVELOPERS. Experience with object-oriented PHP and Composer is required. If you can't use oo-PHP and Composer, don't bother with this library.** 6 | 7 | ## Installation 8 | 9 | This package is best installed through Composer: 10 | 11 | ```bash 12 | composer require infinityfree/mofh-client 13 | ``` 14 | 15 | ## Usage 16 | 17 | Before you can get started, you need to get the API credentials from MyOwnFreeHost. Login to the [reseller panel](https://panel.myownfreehost.net), go to API -> Setup WHM API -> select the domain you want to configure. Copy the API Username and API password and set your own IP address as the Allowed IP Address (the IP address of your computer, server, or wherever you want to use this API client). 18 | 19 | ### Available Methods 20 | 21 | The MyOwnFreeHost API exposes the following methods. The available parameters are listed below. 22 | 23 | - createAccount: Create a new hosting account. 24 | - username: A unique, 8 character identifier of the account. 25 | - password: A password to login to the control panel, FTP and databases. 26 | - domain: A domain name to create the account. Can be a subdomain or a custom domain. 27 | - email: The email address of the user. 28 | - plan: The name of the hosting plan to create the account on. Requires a hosting package to be configured through MyOwnFreeHost. 29 | - suspend: Suspend a hosting account. 30 | - username: The unique, 8 character identifier of the account. 31 | - reason: A string with information about why you are suspending the account. 32 | - linked: If true, related accounts will be suspended as well. 33 | - unsuspend: Reactivate a hosting account. 34 | - username: The unique, 8 character identifier of the account. 35 | - password: Change the password of a hosting account. 36 | - username: The unique, 8 character identifier of the account. 37 | - password: The new password to set for the account. 38 | - availability: Check if a given domain name is available to be added to an account. 39 | - domain: The domain name or subdomain to check. 40 | - getUserDomains: Get the domain names linked to a given account. 41 | - username: The VistaPanel login username (e.g. abcd_12345678). 42 | - getDomainUser: Get the information of a particular hosting domain name, including the account it's hosted on and the document root. 43 | - domain: The domain name to search for. 44 | - getCname: Get the CNAME subdomain for a domain name, used for CNAME domain verification. 45 | - username: The VistaPanel login username (e.g. abcd_12345678). 46 | - domain: The domain name to generate the CNAME subdomain for. 47 | 48 | ### Example 49 | 50 | ```php 51 | use \InfinityFree\MofhClient\Client; 52 | 53 | // Create a new API client with your API credentials. 54 | $client = new Client("", ""); 55 | 56 | // Create a new hosting account. 57 | $createResponse = $client->createAccount( 58 | 'abcd1234', // A unique, 8 character identifier of the account. Primarily used as internal identifier. 59 | 'password123', // A password to login to the control panel, FTP and databases. 60 | 'user@example.com', // The email address of the user. 61 | 'userdomain.example.com', // Initial domain of the account. Can be a subdomain or a custom domain. 62 | 'my_plan', // The hosting plan name at MyOwnFreeHost. 63 | ); 64 | 65 | // Check whether the request was successful. 66 | if ($createResponse->isSuccessful()) { 67 | echo "Created account with username: ".$createResponse->getVpUsername(); 68 | } else { 69 | echo 'Failed to create account: ' . $createResponse->getMessage(); 70 | die(); 71 | } 72 | ``` 73 | 74 | ## License 75 | 76 | Copyright 2023 InfinityFree 77 | 78 | Licensed under the Apache License, Version 2.0 (the "License"); 79 | you may not use this file except in compliance with the License. 80 | You may obtain a copy of the License at 81 | 82 | http://www.apache.org/licenses/LICENSE-2.0 83 | 84 | Unless required by applicable law or agreed to in writing, software 85 | distributed under the License is distributed on an "AS IS" BASIS, 86 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 87 | See the License for the specific language governing permissions and 88 | limitations under the License. 89 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "infinityfree/mofh-client", 3 | "description": "A WHM API client for MyOwnFreeHost in PHP", 4 | "minimum-stability": "stable", 5 | "license": "Apache-2.0", 6 | "authors": [ 7 | { 8 | "name": "InfinityFree", 9 | "email": "hello@infinityfree.net" 10 | } 11 | ], 12 | "require": { 13 | "php": ">=7.1", 14 | "ext-json": "*", 15 | "ext-libxml": "*", 16 | "ext-simplexml": "*", 17 | "guzzlehttp/guzzle": "~6.0|~7.0" 18 | }, 19 | "require-dev": { 20 | "fakerphp/faker": "^1.7", 21 | "friendsofphp/php-cs-fixer": "^3.4", 22 | "mockery/mockery": "^1.3", 23 | "phpunit/phpunit": "^7.0|^8.0|^9.0" 24 | }, 25 | "autoload": { 26 | "psr-4": { 27 | "InfinityFree\\MofhClient\\": "src/", 28 | "InfinityFree\\MofhClient\\Tests\\": "tests/" 29 | } 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /phpunit.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | 8 | ./src 9 | 10 | 11 | 12 | 13 | ./tests/ 14 | 15 | 16 | 17 | -------------------------------------------------------------------------------- /src/Client.php: -------------------------------------------------------------------------------- 1 | apiUsername = $apiUsername; 52 | $this->apiPassword = $apiPassword; 53 | $this->apiUrl = $apiUrl; 54 | 55 | $this->httpClient = $httpClient ?: new Guzzle([ 56 | 'connect_timeout' => 5.0, 57 | ]); 58 | } 59 | 60 | /** 61 | * Send a POST query to the XML API. 62 | * 63 | * @param string $function The MOFH API function name 64 | * @param array $parameters The API function arguments 65 | * @return ResponseInterface 66 | * 67 | * @throws MofhClientHttpException 68 | */ 69 | protected function sendPostRequest(string $function, array $parameters): ResponseInterface 70 | { 71 | return $this->sendRawRequest('POST', $function, [ 72 | 'form_params' => $parameters, 73 | 'auth' => [$this->apiUsername, $this->apiPassword], 74 | 'timeout' => 60.0, 75 | ]); 76 | } 77 | 78 | /** 79 | * Send a GET query to the XML API. 80 | * 81 | * @param string $function The MOFH API function name 82 | * @param array $parameters The API function arguments 83 | * @return ResponseInterface 84 | * 85 | * @throws MofhClientHttpException 86 | */ 87 | protected function sendGetRequest(string $function, array $parameters): ResponseInterface 88 | { 89 | return $this->sendRawRequest('GET', $function, [ 90 | 'query' => array_replace([ 91 | 'api_user' => $this->apiUsername, 92 | 'api_key' => $this->apiPassword, 93 | ], $parameters), 94 | 'timeout' => 5.0, 95 | ]); 96 | } 97 | 98 | /** 99 | * Send the actual HTTP request to the API. 100 | * 101 | * @param string $method The HTTP method to use. 102 | * @param string $function The API function to use. 103 | * @param array $requestOptions Any Guzzle request parameters. 104 | * @return ResponseInterface 105 | * @throws MofhClientHttpException 106 | */ 107 | private function sendRawRequest(string $method, string $function, array $requestOptions = []): ResponseInterface 108 | { 109 | try { 110 | return $this->httpClient->request($method, $this->apiUrl.$function, $requestOptions); 111 | } catch (GuzzleException $e) { 112 | throw new MofhClientHttpException('The MOFH API returned a HTTP error: '.$e->getMessage(), 0, $e); 113 | } 114 | } 115 | 116 | /** 117 | * Create a new hosting account. 118 | * 119 | * @param string $username A custom username, max. 8 characters of letters and numbers. 120 | * @param string $password A password used to access the control panel, FTP and database. 121 | * @param string $email The contact email address of the account owner. 122 | * @param string $domain The primary domain name of the account. 123 | * @param string $plan The name of the plan to use at MyOwnFreeHost. Create this in MOFH -> Quotas & Packages -> Set Packages. 124 | * @return CreateAccountResponse 125 | * 126 | * @throws MofhClientHttpException 127 | */ 128 | public function createAccount(string $username, string $password, string $email, string $domain, string $plan): CreateAccountResponse 129 | { 130 | $response = $this->sendPostRequest('createacct', [ 131 | 'username' => $username, 132 | 'password' => $password, 133 | 'contactemail' => $email, 134 | 'domain' => $domain, 135 | 'plan' => $plan, 136 | ]); 137 | 138 | return new CreateAccountResponse($response); 139 | } 140 | 141 | /** 142 | * Suspend an account on MyOwnFreeHost. 143 | * 144 | * @param string $username The custom username of the account. This is the 8 character username used in createAccount, not the FTP username. 145 | * @param string $reason The reason why the account is suspended. Will be prefixed with RES_CLOSE by the system. 146 | * @param bool $linked If set to true, related accounts (from the same email or IP address) will be suspended as well. 147 | * @return SuspendResponse 148 | * 149 | * @throws MofhClientHttpException 150 | */ 151 | public function suspend(string $username, string $reason, bool $linked = false): SuspendResponse 152 | { 153 | $response = $this->sendPostRequest('suspendacct', [ 154 | 'user' => $username, 155 | 'reason' => $reason, 156 | 'linked' => $linked ? '1' : '0', 157 | ]); 158 | 159 | return new SuspendResponse($response); 160 | } 161 | 162 | /** 163 | * Unsuspend the account with the given username at MyOwnFreeHost. 164 | * 165 | * @param string $username The custom username of the account. This is the 8 character username used in createAccount, not the FTP username. 166 | * @return UnsuspendResponse 167 | * 168 | * @throws MofhClientHttpException 169 | */ 170 | public function unsuspend(string $username): UnsuspendResponse 171 | { 172 | $response = $this->sendPostRequest('unsuspendacct', [ 173 | 'user' => $username, 174 | ]); 175 | 176 | return new UnsuspendResponse($response); 177 | } 178 | 179 | /** 180 | * Change the password of an (active) account. 181 | * 182 | * @param string $username The custom username of the account. This is the 8 character username used in createAccount, not the FTP username. 183 | * @param string $password The new password used to access the control panel, FTP and database. 184 | * @return PasswordResponse 185 | * 186 | * @throws MofhClientHttpException 187 | */ 188 | public function password(string $username, string $password): PasswordResponse 189 | { 190 | $response = $this->sendPostRequest('passwd', [ 191 | 'user' => $username, 192 | 'pass' => $password, 193 | ]); 194 | 195 | return new PasswordResponse($response); 196 | } 197 | 198 | /** 199 | * Check whether a domain is available to use at MyOwnFreeHost. 200 | * 201 | * This checks if the domain is in use on another account or not. It doesn't check 202 | * 203 | * @param string $domain The domain name or subdomain to check. 204 | * @return AvailabilityResponse 205 | * 206 | * @throws MofhClientHttpException 207 | */ 208 | public function availability(string $domain): AvailabilityResponse 209 | { 210 | $response = $this->sendGetRequest('checkavailable', [ 211 | 'domain' => $domain, 212 | ]); 213 | 214 | return new AvailabilityResponse($response); 215 | } 216 | 217 | /** 218 | * Get the domains belonging to an account. 219 | * 220 | * @param string $username The generated username for the account (e.g. test_12345678). 221 | * @return GetUserDomainsResponse 222 | * 223 | * @throws MofhClientHttpException 224 | */ 225 | public function getUserDomains(string $username): GetUserDomainsResponse 226 | { 227 | $response = $this->sendGetRequest('getuserdomains', [ 228 | 'username' => $username, 229 | ]); 230 | 231 | return new GetUserDomainsResponse($response); 232 | } 233 | 234 | /** 235 | * Get the account details corresponding to a domain name. 236 | * 237 | * @param string $domain The domain name to search for. 238 | * @return GetDomainUserResponse 239 | * 240 | * @throws MofhClientHttpException 241 | */ 242 | public function getDomainUser(string $domain): GetDomainUserResponse 243 | { 244 | $response = $this->sendGetRequest('getdomainuser', [ 245 | 'domain' => $domain, 246 | ]); 247 | 248 | return new GetDomainUserResponse($response); 249 | } 250 | 251 | /** 252 | * Get the CNAME record corresponding to this domain, used for CNAME verification. 253 | * 254 | * @param string $username 255 | * @param string $domain 256 | * @return GetCnameResponse 257 | * 258 | * @throws MofhClientHttpException 259 | */ 260 | public function getCname(string $username, string $domain): GetCnameResponse 261 | { 262 | $response = $this->sendPostRequest('getcname', [ 263 | 'api_user' => $this->apiUsername, 264 | 'api_key' => $this->apiPassword, 265 | 'username' => $username, 266 | 'domain_name' => $domain, 267 | ]); 268 | 269 | return new GetCnameResponse($response); 270 | } 271 | } 272 | -------------------------------------------------------------------------------- /src/Exception/MismatchedStatusesException.php: -------------------------------------------------------------------------------- 1 | response = $response; 27 | 28 | $this->parseResponse(); 29 | } 30 | 31 | /** 32 | * Parse the response after it has been received. 33 | */ 34 | protected function parseResponse() 35 | { 36 | $data = (string) $this->response->getBody(); 37 | 38 | $xmlData = @simplexml_load_string($data, \SimpleXMLElement::class, LIBXML_NOERROR); 39 | 40 | if ($xmlData !== false) { 41 | $this->data = $this->xmlToArray((array) $xmlData); 42 | } else { 43 | $this->data = $data; 44 | } 45 | } 46 | 47 | /** 48 | * Get the response data. 49 | * 50 | * @return mixed 51 | */ 52 | public function getData() 53 | { 54 | return $this->data; 55 | } 56 | 57 | /** 58 | * Recursively convert a SimpleXMLElement array to regular arrays 59 | */ 60 | protected function xmlToArray(array $input): array 61 | { 62 | foreach ($input as $key => $value) { 63 | if ($value instanceof \SimpleXMLElement) { 64 | $value = (array) $value; 65 | } 66 | 67 | if (is_array($value)) { 68 | $input[$key] = $this->xmlToArray($value); 69 | } 70 | } 71 | 72 | return $input; 73 | } 74 | 75 | /** 76 | * Get the error message from the response if the call failed. 77 | */ 78 | public function getMessage(): ?string 79 | { 80 | if ($this->isSuccessful()) { 81 | return null; 82 | } elseif ($this->getData() && isset($this->getData()['result']['statusmsg'])) { 83 | return trim($this->getData()['result']['statusmsg']); 84 | } else { 85 | return trim($this->response->getBody()); 86 | } 87 | } 88 | 89 | /** 90 | * Whether the action was successful 91 | */ 92 | public function isSuccessful(): bool 93 | { 94 | if ($this->getData() && isset($this->getData()['result']['status'])) { 95 | return $this->getData()['result']['status'] == 1; 96 | } else { 97 | return false; 98 | } 99 | } 100 | } 101 | -------------------------------------------------------------------------------- /src/Message/AvailabilityResponse.php: -------------------------------------------------------------------------------- 1 | isSuccessful() ? null : $this->getData(); 10 | } 11 | 12 | /** 13 | * Whether the request was successful. 14 | */ 15 | public function isSuccessful(): bool 16 | { 17 | return in_array($this->data, ['0', '1']); 18 | } 19 | 20 | /** 21 | * Whether the selected domain name is available or not. 22 | */ 23 | public function isAvailable(): bool 24 | { 25 | return $this->data === '1'; 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /src/Message/CreateAccountResponse.php: -------------------------------------------------------------------------------- 1 | getData()['result']['options']['vpusername'] ?? null; 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /src/Message/GetCnameResponse.php: -------------------------------------------------------------------------------- 1 | response->getBody(); 10 | $this->data = trim($responseBody); 11 | } 12 | 13 | /** 14 | * Check if the request was successful. 15 | */ 16 | public function isSuccessful(): bool 17 | { 18 | return strpos($this->data, 'ERROR') !== 0; 19 | } 20 | 21 | /** 22 | * Get the error message, if defined. 23 | */ 24 | public function getMessage(): ?string 25 | { 26 | return $this->isSuccessful() ? null : $this->getData(); 27 | } 28 | 29 | /** 30 | * Get the returned CNAME record, if the request was successful. 31 | */ 32 | public function getCname(): ?string 33 | { 34 | return $this->isSuccessful() ? $this->getData() : null; 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /src/Message/GetDomainUserResponse.php: -------------------------------------------------------------------------------- 1 | response->getBody(); 18 | 19 | if (strpos($responseBody, '[') === 0) { 20 | $this->data = json_decode($responseBody, true); 21 | 22 | if ($this->data && count($this->data) == 4) { 23 | [$this->status, $this->domain, $this->documentRoot, $this->username] = $this->data; 24 | } 25 | } elseif ($responseBody === 'null') { 26 | $this->data = []; 27 | } else { 28 | $this->data = trim($responseBody); 29 | } 30 | } 31 | 32 | /** 33 | * Get the error message, if defined. 34 | */ 35 | public function getMessage(): ?string 36 | { 37 | return $this->isSuccessful() ? null : $this->getData(); 38 | } 39 | 40 | /** 41 | * Check if the request was successful. 42 | */ 43 | public function isSuccessful(): bool 44 | { 45 | return is_array($this->data); 46 | } 47 | 48 | /** 49 | * Check if the domain was found. 50 | */ 51 | public function isFound(): bool 52 | { 53 | return is_array($this->data) && $this->data !== []; 54 | } 55 | 56 | /** 57 | * Get the domain name which was searched for. 58 | */ 59 | public function getDomain(): ?string 60 | { 61 | return $this->domain; 62 | } 63 | 64 | /** 65 | * Get the status of the account (ACTIVE or SUSPENDED). 66 | */ 67 | public function getStatus(): ?string 68 | { 69 | return $this->status; 70 | } 71 | 72 | /** 73 | * Get the full document root of the domain name. 74 | * 75 | * For example: 76 | * /home/volXX_X/epizy.com/host_12345678/example.com/htdocs 77 | */ 78 | public function getDocumentRoot(): ?string 79 | { 80 | return $this->documentRoot; 81 | } 82 | 83 | /** 84 | * Get the username of the account to which this domain name belongs. 85 | * 86 | * For example: host_12345678 87 | */ 88 | public function getUsername(): ?string 89 | { 90 | return $this->username; 91 | } 92 | } 93 | -------------------------------------------------------------------------------- /src/Message/GetUserDomainsResponse.php: -------------------------------------------------------------------------------- 1 | response->getBody(); 12 | 13 | if (strpos($responseBody, '[') === 0) { 14 | $this->data = json_decode($responseBody, true); 15 | } elseif ($responseBody === 'null') { 16 | $this->data = []; 17 | } else { 18 | $this->data = trim($responseBody); 19 | } 20 | } 21 | 22 | /** 23 | * Get the error message, if defined. 24 | * 25 | * @return array|null|string 26 | */ 27 | public function getMessage(): ?string 28 | { 29 | return $this->isSuccessful() ? null : $this->getData(); 30 | } 31 | 32 | /** 33 | * Check if the request was successful. 34 | */ 35 | public function isSuccessful(): bool 36 | { 37 | return is_array($this->data); 38 | } 39 | 40 | /** 41 | * Get the list of domains on the account. 42 | */ 43 | public function getDomains(): array 44 | { 45 | return array_map(function ($item) { 46 | return $item[1]; 47 | }, is_array($this->data) ? $this->data : []); 48 | } 49 | 50 | /** 51 | * Get the status of the account, either ACTIVE or SUSPENDED. 52 | * 53 | * @throws MismatchedStatusesException 54 | */ 55 | public function getStatus(): ?string 56 | { 57 | if (is_array($this->data)) { 58 | $statuses = array_unique(array_map(function ($item) { 59 | return $item[0]; 60 | }, $this->data)); 61 | 62 | if (count($statuses) == 1) { 63 | return $statuses[0]; 64 | } elseif (count($statuses) > 1) { 65 | throw new MismatchedStatusesException('The account domains have different statuses: '.implode($statuses)); 66 | } else { 67 | return null; 68 | } 69 | } else { 70 | return null; 71 | } 72 | } 73 | } 74 | -------------------------------------------------------------------------------- /src/Message/PasswordResponse.php: -------------------------------------------------------------------------------- 1 | getData()['passwd']['status'])) { 16 | if ($this->getData()['passwd']['status'] == '1') { 17 | $this->status = 'a'; 18 | } elseif (strpos($this->getData()['passwd']['statusmsg'], 'error occured changing this password') !== false) { 19 | // This error means the password is identical. We consider this to be successful (making this call idempotent). 20 | $this->status = 'a'; 21 | } else { 22 | $this->message = trim($this->getData()['passwd']['statusmsg']); 23 | if (preg_match('/the account must be active to change the password\s+\((.+)\)/', $this->message, $matches)) { 24 | $this->status = $matches[1]; 25 | } 26 | } 27 | } else { 28 | $this->message = trim($this->response->getBody()); 29 | } 30 | } 31 | 32 | /** 33 | * Get the error message of the response, if it failed. 34 | */ 35 | public function getMessage(): ?string 36 | { 37 | return $this->message; 38 | } 39 | 40 | /** 41 | * Whether the action was successful 42 | */ 43 | public function isSuccessful(): bool 44 | { 45 | return $this->status == 'a'; 46 | } 47 | 48 | /** 49 | * Get the status of the account if the account is not active. 50 | * 51 | * The result is one of the following chars: 52 | * - a: active 53 | * - x: suspended 54 | * - r: reactivating 55 | * - c: closing 56 | */ 57 | public function getStatus(): ?string 58 | { 59 | return $this->status; 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /src/Message/SuspendResponse.php: -------------------------------------------------------------------------------- 1 | isSuccessful()) { 21 | if (preg_match( 22 | '/This account is not active so can not be suspended \( vPuser : (\S+) , status : (\S+) , reason : (.*) \) ../s', 23 | $this->getMessage(), 24 | $matches) 25 | ) { 26 | $this->username = trim($matches[1]); 27 | $this->status = trim($matches[2]); 28 | $this->reason = trim($matches[3]); 29 | } 30 | } 31 | } 32 | 33 | /** 34 | * Get the status of the account if it's not active. 35 | * 36 | * The result is one of the following chars: 37 | * - x: suspended 38 | * - r: reactivating 39 | * - c: closing 40 | */ 41 | public function getStatus(): ?string 42 | { 43 | return $this->status; 44 | } 45 | 46 | /** 47 | * Get the username of the account if it's not active. 48 | */ 49 | public function getVpUsername(): ?string 50 | { 51 | return $this->username; 52 | } 53 | 54 | /** 55 | * Get the suspension reason of the account if it's not active. 56 | */ 57 | public function getReason(): ?string 58 | { 59 | return $this->reason; 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /src/Message/UnsuspendResponse.php: -------------------------------------------------------------------------------- 1 | isSuccessful()) { 17 | if (preg_match('/account is NOT currently suspended \(status : (\w*) \)/', $this->getMessage(), $matches)) { 18 | if (trim($matches[1]) == '') { 19 | $this->status = 'd'; 20 | } else { 21 | $this->status = trim($matches[1]); 22 | } 23 | } 24 | } 25 | } 26 | 27 | /** 28 | * Get the status of the account if it's not suspended. 29 | * 30 | * Is one of the following chars: 31 | * - a: active 32 | * - r: reactivating 33 | * - c: closing 34 | */ 35 | public function getStatus(): ?string 36 | { 37 | return $this->status; 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /tests/ClientTest.php: -------------------------------------------------------------------------------- 1 | faker = FakerFactory::create(); 59 | $this->apiUsername = base64_encode($this->faker->randomNumber()); 60 | $this->apiPassword = base64_encode($this->faker->randomNumber()); 61 | 62 | $this->guzzleMockHandler = new MockHandler(); 63 | $this->historyContainer = []; 64 | 65 | $handlerStack = HandlerStack::create($this->guzzleMockHandler); 66 | $handlerStack->push(Middleware::history($this->historyContainer)); 67 | $guzzle = new Guzzle(['handler' => $handlerStack]); 68 | 69 | $this->client = new Client($this->apiUsername, $this->apiPassword, 'https://panel.myownfreehost.net/xml-api/', $guzzle); 70 | } 71 | 72 | public function testCreateAccount() 73 | { 74 | $username = $this->faker->lexify('????????'); 75 | $password = $this->faker->regexify('[a-zA-Z0-9]{8,15}'); 76 | $email = $this->faker->email(); 77 | $domain = $this->faker->domainName(); 78 | $plan = $this->faker->word(); 79 | 80 | $this->guzzleMockHandler->append(new Response()); 81 | 82 | $response = $this->client->createAccount($username, $password, $email, $domain, $plan); 83 | $this->assertInstanceOf(CreateAccountResponse::class, $response); 84 | 85 | $this->assertCount(1, $this->historyContainer); 86 | $request = $this->historyContainer[0]['request']; 87 | $this->assertEquals('POST', $request->getMethod()); 88 | $this->assertEquals('/xml-api/createacct', $request->getUri()->getPath()); 89 | parse_str($request->getBody(), $postData); 90 | $this->assertEquals([ 91 | 'username' => $username, 92 | 'password' => $password, 93 | 'contactemail' => $email, 94 | 'domain' => $domain, 95 | 'plan' => $plan, 96 | ], $postData); 97 | } 98 | 99 | public function testSuspend() 100 | { 101 | $username = $this->faker->bothify('????_########'); 102 | $reason = $this->faker->sentence(); 103 | 104 | $this->guzzleMockHandler->append(new Response()); 105 | 106 | $response = $this->client->suspend($username, $reason, true); 107 | $this->assertInstanceOf(SuspendResponse::class, $response); 108 | 109 | $this->assertCount(1, $this->historyContainer); 110 | $request = $this->historyContainer[0]['request']; 111 | $this->assertEquals('POST', $request->getMethod()); 112 | $this->assertEquals('/xml-api/suspendacct', $request->getUri()->getPath()); 113 | parse_str($request->getBody(), $postData); 114 | $this->assertEquals([ 115 | 'user' => $username, 116 | 'reason' => $reason, 117 | 'linked' => '1', 118 | ], $postData); 119 | } 120 | 121 | public function testUnsuspend() 122 | { 123 | $username = $this->faker->bothify('????_########'); 124 | 125 | $this->guzzleMockHandler->append(new Response()); 126 | 127 | $response = $this->client->unsuspend($username); 128 | $this->assertInstanceOf(UnsuspendResponse::class, $response); 129 | 130 | $this->assertCount(1, $this->historyContainer); 131 | $request = $this->historyContainer[0]['request']; 132 | $this->assertEquals('POST', $request->getMethod()); 133 | $this->assertEquals('/xml-api/unsuspendacct', $request->getUri()->getPath()); 134 | parse_str($request->getBody(), $postData); 135 | $this->assertEquals([ 136 | 'user' => $username, 137 | ], $postData); 138 | } 139 | 140 | public function testPassword() 141 | { 142 | $username = $this->faker->bothify('????_########'); 143 | $password = $this->faker->regexify('[a-zA-Z0-9]{8,15}'); 144 | 145 | $this->guzzleMockHandler->append(new Response()); 146 | 147 | $response = $this->client->password($username, $password); 148 | $this->assertInstanceOf(PasswordResponse::class, $response); 149 | 150 | $this->assertCount(1, $this->historyContainer); 151 | $request = $this->historyContainer[0]['request']; 152 | $this->assertEquals('POST', $request->getMethod()); 153 | $this->assertEquals('/xml-api/passwd', $request->getUri()->getPath()); 154 | parse_str($request->getBody(), $postData); 155 | $this->assertEquals([ 156 | 'user' => $username, 157 | 'pass' => $password, 158 | ], $postData); 159 | } 160 | 161 | public function testPasswordHttpError() 162 | { 163 | $username = $this->faker->bothify('????_########'); 164 | $password = $this->faker->regexify('[a-zA-Z0-9]{8,15}'); 165 | 166 | $this->guzzleMockHandler->append(new Response(500)); 167 | 168 | $this->expectException(MofhClientHttpException::class); 169 | $this->client->password($username, $password); 170 | } 171 | 172 | public function testAvailability() 173 | { 174 | $domain = $this->faker->domainName(); 175 | 176 | $this->guzzleMockHandler->append(new Response()); 177 | 178 | $response = $this->client->availability($domain); 179 | $this->assertInstanceOf(AvailabilityResponse::class, $response); 180 | 181 | $this->assertCount(1, $this->historyContainer); 182 | $request = $this->historyContainer[0]['request']; 183 | $this->assertEquals('GET', $request->getMethod()); 184 | $this->assertEquals('/xml-api/checkavailable', $request->getUri()->getPath()); 185 | parse_str($request->getUri()->getQuery(), $queryData); 186 | $this->assertEquals([ 187 | 'api_user' => $this->apiUsername, 188 | 'api_key' => $this->apiPassword, 189 | 'domain' => $domain, 190 | ], $queryData); 191 | } 192 | 193 | public function testAvailabilityHttpError() 194 | { 195 | $domain = $this->faker->domainName(); 196 | 197 | $this->guzzleMockHandler->append(new Response(403)); 198 | 199 | $this->expectException(MofhClientHttpException::class); 200 | $this->client->availability($domain); 201 | } 202 | 203 | public function testGetUserDomains() 204 | { 205 | $username = $this->faker->bothify('????_########'); 206 | 207 | $this->guzzleMockHandler->append(new Response()); 208 | 209 | $response = $this->client->getUserDomains($username); 210 | $this->assertInstanceOf(GetUserDomainsResponse::class, $response); 211 | 212 | $this->assertCount(1, $this->historyContainer); 213 | $request = $this->historyContainer[0]['request']; 214 | $this->assertEquals('GET', $request->getMethod()); 215 | $this->assertEquals('/xml-api/getuserdomains', $request->getUri()->getPath()); 216 | parse_str($request->getUri()->getQuery(), $queryData); 217 | $this->assertEquals([ 218 | 'api_user' => $this->apiUsername, 219 | 'api_key' => $this->apiPassword, 220 | 'username' => $username, 221 | ], $queryData); 222 | } 223 | 224 | public function testGetDomainUser() 225 | { 226 | $domain = $this->faker->domainName(); 227 | 228 | $this->guzzleMockHandler->append(new Response()); 229 | 230 | $response = $this->client->getDomainUser($domain); 231 | $this->assertInstanceOf(GetDomainUserResponse::class, $response); 232 | 233 | $this->assertCount(1, $this->historyContainer); 234 | $request = $this->historyContainer[0]['request']; 235 | $this->assertEquals('GET', $request->getMethod()); 236 | $this->assertEquals('/xml-api/getdomainuser', $request->getUri()->getPath()); 237 | parse_str($request->getUri()->getQuery(), $queryData); 238 | $this->assertEquals([ 239 | 'api_user' => $this->apiUsername, 240 | 'api_key' => $this->apiPassword, 241 | 'domain' => $domain, 242 | ], $queryData); 243 | } 244 | 245 | public function testGetCname() 246 | { 247 | $username = $this->faker->bothify('????_########'); 248 | $domain = $this->faker->domainName(); 249 | 250 | $this->guzzleMockHandler->append(new Response(200, [], md5($domain))); 251 | 252 | $response = $this->client->getCname($username, $domain); 253 | $this->assertInstanceOf(GetCnameResponse::class, $response); 254 | 255 | $this->assertCount(1, $this->historyContainer); 256 | $request = $this->historyContainer[0]['request']; 257 | $this->assertEquals('POST', $request->getMethod()); 258 | $this->assertEquals('/xml-api/getcname', $request->getUri()->getPath()); 259 | parse_str($request->getBody(), $postData); 260 | $this->assertEquals([ 261 | 'api_user' => $this->apiUsername, 262 | 'api_key' => $this->apiPassword, 263 | 'domain_name' => $domain, 264 | 'username' => $username, 265 | ], $postData); 266 | } 267 | } 268 | -------------------------------------------------------------------------------- /tests/Message/AvailabilityResponseTest.php: -------------------------------------------------------------------------------- 1 | assertTrue($availabilityResponse->isSuccessful()); 18 | $this->assertTrue($availabilityResponse->isAvailable()); 19 | $this->assertNull($availabilityResponse->getMessage()); 20 | } 21 | 22 | public function testNotAvailableDomain() 23 | { 24 | $httpResponse = new Response(200, [], '0'); 25 | 26 | $availabilityResponse = new AvailabilityResponse($httpResponse); 27 | 28 | $this->assertTrue($availabilityResponse->isSuccessful()); 29 | $this->assertFalse($availabilityResponse->isAvailable()); 30 | $this->assertNull($availabilityResponse->getMessage()); 31 | } 32 | 33 | public function testError() 34 | { 35 | $message = 'ERROR :Illegal charachters in domain name . . This domain name does not appear to be valid (to short !). .'; 36 | 37 | $httpResponse = new Response(200, [], $message); 38 | 39 | $availabilityResponse = new AvailabilityResponse($httpResponse); 40 | 41 | $this->assertFalse($availabilityResponse->isSuccessful()); 42 | $this->assertFalse($availabilityResponse->isAvailable()); 43 | $this->assertEquals($message, $availabilityResponse->getMessage()); 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /tests/Message/CreateAccountResponseTest.php: -------------------------------------------------------------------------------- 1 | faker = FakerFactory::create(); 23 | } 24 | 25 | public function testSuccessful() 26 | { 27 | $username = $this->faker->bothify('????_########'); 28 | 29 | $httpResponse = new Response(200, [], " 30 | 31 | 32 | 33 | n 34 | {$username} 35 | ns1.byet.org 36 | ns2.byet.org 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | account added to queue to be added 51 | 52 | 1 53 | This account has been successfuly created 54 | 55 | 56 | "); 57 | 58 | $createAccountResponse = new CreateAccountResponse($httpResponse); 59 | $this->assertTrue($createAccountResponse->isSuccessful()); 60 | $this->assertEquals($username, $createAccountResponse->getVpUsername()); 61 | $this->assertNull($createAccountResponse->getMessage()); 62 | } 63 | 64 | public function testError() 65 | { 66 | $httpResponse = new Response(200, [], ' 67 | 68 | 69 | 70 | n 71 | ns1.byet.org 72 | ns2.byet.org 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | account added to queue to be added 87 | 88 | 0 89 | The API username you are using appears to be invalid 1 (0). . The API username you are using appears to be invalid 2. 90 | 91 | 92 | '); 93 | 94 | $createAccountResponse = new CreateAccountResponse($httpResponse); 95 | 96 | $this->assertFalse($createAccountResponse->isSuccessful()); 97 | $this->assertEquals('The API username you are using appears to be invalid 1 (0). . The API username you are using appears to be invalid 2.', $createAccountResponse->getMessage()); 98 | $this->assertNull($createAccountResponse->getVpUsername()); 99 | } 100 | 101 | public function testDuplicateXmlDocument() 102 | { 103 | $httpResponse = new Response(200, [], ' 104 | 105 | 106 | 107 | n 108 | ns1.byet.org 109 | ns2.byet.org 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | 123 | account added to queue to be added 124 | 125 | 0 126 | Name servers for domain are not valid 84572435 127 | 128 | 129 | 130 | 131 | n 132 | ns1.byet.org 133 | ns2.byet.org 134 | 135 | 136 | 137 | 138 | 139 | 140 | 141 | 142 | 143 | 144 | 145 | 146 | 147 | account added to queue to be added 148 | 149 | 0 150 | Name servers for domain are not valid 84572435 The name servers for example.com are not set to valid name servers. 151 | 152 | 153 | '); 154 | 155 | $createAccountResponse = new CreateAccountResponse($httpResponse); 156 | 157 | $this->assertFalse($createAccountResponse->isSuccessful()); 158 | $this->assertNull($createAccountResponse->getVpUsername()); 159 | } 160 | } 161 | -------------------------------------------------------------------------------- /tests/Message/GetCnameResponseTest.php: -------------------------------------------------------------------------------- 1 | faker = FakerFactory::create(); 23 | } 24 | 25 | public function testReturnsCnameDomain() 26 | { 27 | $cnameRecord = md5(time()); 28 | $httpResponse = new Response(200, [], $cnameRecord); 29 | 30 | $getCnameResponse = new GetCnameResponse($httpResponse); 31 | 32 | $this->assertTrue($getCnameResponse->isSuccessful()); 33 | $this->assertEquals($cnameRecord, $getCnameResponse->getCname()); 34 | $this->assertNull($getCnameResponse->getMessage()); 35 | } 36 | 37 | public function testError() 38 | { 39 | $message = 'ERROR :The username specified does not appear to be valid.'; 40 | 41 | $httpResponse = new Response(200, [], $message); 42 | 43 | $getCnameResponse = new GetCnameResponse($httpResponse); 44 | 45 | $this->assertFalse($getCnameResponse->isSuccessful()); 46 | $this->assertEquals($message, $getCnameResponse->getMessage()); 47 | $this->assertNull($getCnameResponse->getCname()); 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /tests/Message/GetDomainUserResponseTest.php: -------------------------------------------------------------------------------- 1 | faker = FakerFactory::create(); 23 | } 24 | 25 | public function testFound() 26 | { 27 | $domain = $this->faker->domainName(); 28 | $username = $this->faker->bothify('????_########'); 29 | $documentRoot = "/home/vol12_3/example.com/{$username}/{$domain}/htdocs"; 30 | 31 | $httpResponse = new Response(200, [], json_encode(['ACTIVE', $domain, $documentRoot, $username])); 32 | 33 | $getDomainUserResponse = new GetDomainUserResponse($httpResponse); 34 | 35 | $this->assertTrue($getDomainUserResponse->isSuccessful()); 36 | $this->assertTrue($getDomainUserResponse->isFound()); 37 | $this->assertEquals($domain, $getDomainUserResponse->getDomain()); 38 | $this->assertEquals($username, $getDomainUserResponse->getUsername()); 39 | $this->assertEquals($documentRoot, $getDomainUserResponse->getDocumentRoot()); 40 | $this->assertEquals('ACTIVE', $getDomainUserResponse->getStatus()); 41 | $this->assertNull($getDomainUserResponse->getMessage()); 42 | } 43 | 44 | public function testNotFound() 45 | { 46 | $httpResponse = new Response(200, [], 'null'); 47 | 48 | $getDomainUserResponse = new GetDomainUserResponse($httpResponse); 49 | 50 | $this->assertTrue($getDomainUserResponse->isSuccessful()); 51 | $this->assertFalse($getDomainUserResponse->isFound()); 52 | $this->assertNull($getDomainUserResponse->getDomain()); 53 | $this->assertNull($getDomainUserResponse->getUsername()); 54 | $this->assertNull($getDomainUserResponse->getDocumentRoot()); 55 | $this->assertNull($getDomainUserResponse->getStatus()); 56 | $this->assertNull($getDomainUserResponse->getMessage()); 57 | } 58 | 59 | public function testError() 60 | { 61 | $message = 'ERROR :The API key you are using appears to be invalid 1. . The API key you are using appears to be invalid 2. . '; 62 | $httpResponse = new Response(200, [], $message); 63 | 64 | $getDomainUserResponse = new GetDomainUserResponse($httpResponse); 65 | 66 | $this->assertFalse($getDomainUserResponse->isSuccessful()); 67 | $this->assertFalse($getDomainUserResponse->isFound()); 68 | $this->assertNull($getDomainUserResponse->getDomain()); 69 | $this->assertNull($getDomainUserResponse->getUsername()); 70 | $this->assertNull($getDomainUserResponse->getDocumentRoot()); 71 | $this->assertNull($getDomainUserResponse->getStatus()); 72 | $this->assertEquals(trim($message), $getDomainUserResponse->getMessage()); 73 | } 74 | } 75 | -------------------------------------------------------------------------------- /tests/Message/GetUserDomainsResponseTest.php: -------------------------------------------------------------------------------- 1 | faker = FakerFactory::create(); 24 | } 25 | 26 | public function testReturnsDomains() 27 | { 28 | $domain1 = $this->faker->domainName(); 29 | $domain2 = $this->faker->domainName(); 30 | 31 | $httpResponse = new Response(200, [], json_encode([ 32 | ['ACTIVE', $domain1], 33 | ['ACTIVE', $domain2], 34 | ])); 35 | 36 | $getUserDomainsResponse = new GetUserDomainsResponse($httpResponse); 37 | 38 | $this->assertTrue($getUserDomainsResponse->isSuccessful()); 39 | $this->assertEquals([$domain1, $domain2], $getUserDomainsResponse->getDomains()); 40 | $this->assertEquals('ACTIVE', $getUserDomainsResponse->getStatus()); 41 | $this->assertNull($getUserDomainsResponse->getMessage()); 42 | } 43 | 44 | public function testNoDomains() 45 | { 46 | $httpResponse = new Response(200, [], 'null'); 47 | 48 | $getUserDomainsResponse = new GetUserDomainsResponse($httpResponse); 49 | 50 | $this->assertTrue($getUserDomainsResponse->isSuccessful()); 51 | $this->assertEquals([], $getUserDomainsResponse->getDomains()); 52 | $this->assertEquals(null, $getUserDomainsResponse->getStatus()); 53 | $this->assertNull($getUserDomainsResponse->getMessage()); 54 | } 55 | 56 | public function testError() 57 | { 58 | $message = 'ERROR :The API key you are using appears to be invalid 1. . The API key you are using appears to be invalid 2. . '; 59 | $httpResponse = new Response(200, [], $message); 60 | 61 | $getUserDomainsResponse = new GetUserDomainsResponse($httpResponse); 62 | 63 | $this->assertFalse($getUserDomainsResponse->isSuccessful()); 64 | $this->assertEquals([], $getUserDomainsResponse->getDomains()); 65 | $this->assertEquals(null, $getUserDomainsResponse->getStatus()); 66 | $this->assertEquals(trim($message), $getUserDomainsResponse->getMessage()); 67 | } 68 | 69 | public function testMistmachedStatuses() 70 | { 71 | $domain1 = $this->faker->domainName(); 72 | $domain2 = $this->faker->domainName(); 73 | 74 | $httpResponse = new Response(200, [], json_encode([ 75 | ['ACTIVE', $domain1], 76 | ['SUSPENDED', $domain2], 77 | ])); 78 | 79 | $getUserDomainsResponse = new GetUserDomainsResponse($httpResponse); 80 | 81 | $this->assertTrue($getUserDomainsResponse->isSuccessful()); 82 | 83 | $this->expectException(MismatchedStatusesException::class); 84 | $getUserDomainsResponse->getStatus(); 85 | } 86 | } 87 | -------------------------------------------------------------------------------- /tests/Message/PasswordResponseTest.php: -------------------------------------------------------------------------------- 1 | faker = FakerFactory::create(); 23 | } 24 | 25 | public function testSuccess() 26 | { 27 | $shortUsername = $this->faker->lexify('????????'); 28 | 29 | $httpResponse = new Response(200, [], " 30 | 31 | 32 | 33 | Changing password for {$shortUsername} 34 | Password for {$shortUsername} has been changed 35 | Updating ftp passwords for {$shortUsername} 36 | Ftp password files updated. 37 | Ftp vhost passwords synced 38 | 39 | 40 | system 41 | 42 | 43 | ftp 44 | 45 | 46 | mail 47 | 48 | 49 | mySQL 50 | 51 | 1 52 | Password changed for user {$shortUsername} 53 | 54 | 55 | "); 56 | 57 | $passwordResponse = new PasswordResponse($httpResponse); 58 | 59 | $this->assertTrue($passwordResponse->isSuccessful()); 60 | $this->assertEquals('a', $passwordResponse->getStatus()); 61 | $this->assertNull($passwordResponse->getMessage()); 62 | } 63 | 64 | public function testPasswordIdentical() 65 | { 66 | $shortUsername = $this->faker->lexify('????????'); 67 | 68 | $httpResponse = new Response(200, [], " 69 | 70 | 71 | 72 | Changing password for {$shortUsername} 73 | Password for {$shortUsername} has been changed 74 | Updating ftp passwords for {$shortUsername} 75 | Ftp password files updated. 76 | Ftp vhost passwords synced 77 | 78 | 79 | system 80 | 81 | 82 | ftp 83 | 84 | 85 | mail 86 | 87 | 88 | mySQL 89 | 90 | 0 91 | An error occured changing this password. 92 | 93 | 94 | "); 95 | 96 | $passwordResponse = new PasswordResponse($httpResponse); 97 | 98 | $this->assertTrue($passwordResponse->isSuccessful()); 99 | $this->assertEquals('a', $passwordResponse->getStatus()); 100 | $this->assertNull($passwordResponse->getMessage()); 101 | } 102 | 103 | public function testAccountNotActive() 104 | { 105 | $shortUsername = $this->faker->lexify('????????'); 106 | 107 | $httpResponse = new Response(200, [], " 108 | 109 | 110 | 111 | Changing password for {$shortUsername} 112 | Password for {$shortUsername} has been changed 113 | Updating ftp passwords for {$shortUsername} 114 | Ftp password files updated. 115 | Ftp vhost passwords synced 116 | 117 | 118 | system 119 | 120 | 121 | ftp 122 | 123 | 124 | mail 125 | 126 | 127 | mySQL 128 | 129 | 0 130 | This account currently not active, the account must be active to change the password (x). . 131 | 132 | 133 | "); 134 | 135 | $passwordResponse = new PasswordResponse($httpResponse); 136 | 137 | $this->assertFalse($passwordResponse->isSuccessful()); 138 | $this->assertEquals('x', $passwordResponse->getStatus()); 139 | $this->assertEquals( 140 | 'This account currently not active, the account must be active to change the password (x). .', 141 | $passwordResponse->getMessage() 142 | ); 143 | } 144 | 145 | public function testError() 146 | { 147 | $httpResponse = new Response(200, [], 'The API username you are using appears to be invalid 1. . '); 148 | 149 | $passwordResponse = new PasswordResponse($httpResponse); 150 | 151 | $this->assertFalse($passwordResponse->isSuccessful()); 152 | $this->assertNull($passwordResponse->getStatus()); 153 | $this->assertEquals('The API username you are using appears to be invalid 1. .', $passwordResponse->getMessage()); 154 | } 155 | } 156 | -------------------------------------------------------------------------------- /tests/Message/SuspendResponseTest.php: -------------------------------------------------------------------------------- 1 | faker = FakerFactory::create(); 23 | } 24 | 25 | public function testSuccess() 26 | { 27 | $httpResponse = new Response(200, [], " 28 | 29 | 30 | 1 31 | 32 | 33 | Changing Shell to /bin/false...Changing shell for utlvigl7. 34 | Shell changed. 35 | Locking Password...Locking password for user utlvigl7. 36 | marking user vhosts / databases for suspension. 37 | .. 38 | .. 39 | Completed, this account will be fully suspended in 2 minutes. 40 | 41 | 42 | 43 | "); 44 | 45 | $suspendResponse = new SuspendResponse($httpResponse); 46 | 47 | $this->assertTrue($suspendResponse->isSuccessful()); 48 | $this->assertNull($suspendResponse->getStatus()); 49 | $this->assertNull($suspendResponse->getVpUsername()); 50 | $this->assertNull($suspendResponse->getReason()); 51 | $this->assertNull($suspendResponse->getMessage()); 52 | } 53 | 54 | public function testNotActive() 55 | { 56 | $username = $this->faker->bothify('????_########'); 57 | $reason = $this->faker->sentence(); 58 | 59 | $httpResponse = new Response(200, [], " 60 | 61 | 62 | 0 63 | 64 | This account is not active so can not be suspended ( vPuser : {$username} , status : x , reason : {$reason} ) .. 65 | 66 | 67 | 68 | "); 69 | 70 | $suspendResponse = new SuspendResponse($httpResponse); 71 | 72 | $this->assertFalse($suspendResponse->isSuccessful()); 73 | $this->assertEquals('x', $suspendResponse->getStatus()); 74 | $this->assertEquals($username, $suspendResponse->getVpUsername()); 75 | $this->assertEquals($reason, $suspendResponse->getReason()); 76 | $this->assertEquals( 77 | "This account is not active so can not be suspended ( vPuser : {$username} , status : x , reason : {$reason} ) ..", 78 | $suspendResponse->getMessage() 79 | ); 80 | } 81 | 82 | public function testSuspendedWithEmptyReason() 83 | { 84 | $username = $this->faker->bothify('????_########'); 85 | 86 | $httpResponse = new Response(200, [], " 87 | 88 | 89 | 0 90 | 91 | This account is not active so can not be suspended ( vPuser : {$username} , status : x , reason : ) .. 92 | 93 | 94 | 95 | "); 96 | 97 | $suspendResponse = new SuspendResponse($httpResponse); 98 | 99 | $this->assertFalse($suspendResponse->isSuccessful()); 100 | $this->assertEquals('x', $suspendResponse->getStatus()); 101 | $this->assertEquals($username, $suspendResponse->getVpUsername()); 102 | $this->assertEquals('', $suspendResponse->getReason()); 103 | $this->assertEquals( 104 | "This account is not active so can not be suspended ( vPuser : {$username} , status : x , reason : ) ..", 105 | $suspendResponse->getMessage() 106 | ); 107 | } 108 | 109 | public function testSuspendedWithReasonOnMultipleLines() 110 | { 111 | $username = $this->faker->bothify('????_########'); 112 | 113 | $reason = $this->faker->sentence." 114 |

115 | Hello world! 116 |

117 | 118 | "; 119 | $encodedReason = htmlspecialchars($reason); 120 | 121 | $httpResponse = new Response(200, [], " 122 | 123 | 124 | 0 125 | 126 | This account is not active so can not be suspended ( vPuser : {$username} , status : x , reason : {$encodedReason} ) .. 127 | 128 | 129 | 130 | "); 131 | 132 | $suspendResponse = new SuspendResponse($httpResponse); 133 | 134 | $this->assertFalse($suspendResponse->isSuccessful()); 135 | $this->assertEquals('x', $suspendResponse->getStatus()); 136 | $this->assertEquals($username, $suspendResponse->getVpUsername()); 137 | $this->assertEquals(trim($reason), $suspendResponse->getReason()); 138 | } 139 | 140 | public function testError() 141 | { 142 | $httpResponse = new Response(200, [], 'The API username you are using appears to be invalid 1. . '); 143 | 144 | $suspendResponse = new SuspendResponse($httpResponse); 145 | 146 | $this->assertFalse($suspendResponse->isSuccessful()); 147 | $this->assertNull($suspendResponse->getStatus()); 148 | $this->assertNull($suspendResponse->getVpUsername()); 149 | $this->assertNull($suspendResponse->getReason()); 150 | $this->assertEquals('The API username you are using appears to be invalid 1. .', $suspendResponse->getMessage()); 151 | } 152 | } 153 | -------------------------------------------------------------------------------- /tests/Message/UnsuspendResponseTest.php: -------------------------------------------------------------------------------- 1 | faker = FakerFactory::create(); 23 | } 24 | 25 | public function testSuccess() 26 | { 27 | $httpResponse = new Response(200, [], " 28 | 29 | 30 | 1 31 | 32 | 33 | abcd1234 account has been unsuspended 34 | 35 | 36 | 37 | "); 38 | 39 | $unsuspendResponse = new UnsuspendResponse($httpResponse); 40 | 41 | $this->assertTrue($unsuspendResponse->isSuccessful()); 42 | $this->assertNull($unsuspendResponse->getStatus()); 43 | $this->assertNull($unsuspendResponse->getMessage()); 44 | } 45 | 46 | public function testNotSuspended() 47 | { 48 | $httpResponse = new Response(200, [], ' 49 | 50 | 51 | 0 52 | 53 | This account is NOT currently suspended (status : r ) . . 54 | 55 | 56 | 57 | '); 58 | 59 | $unsuspendResponse = new UnsuspendResponse($httpResponse); 60 | 61 | $this->assertFalse($unsuspendResponse->isSuccessful()); 62 | $this->assertEquals('r', $unsuspendResponse->getStatus()); 63 | $this->assertEquals('This account is NOT currently suspended (status : r ) . .', $unsuspendResponse->getMessage()); 64 | } 65 | 66 | public function testNotFound() 67 | { 68 | $httpResponse = new Response(200, [], ' 69 | 70 | 71 | 0 72 | 73 | This account is NOT currently suspended (status : ) . . 74 | 75 | 76 | 77 | '); 78 | 79 | $unsuspendResponse = new UnsuspendResponse($httpResponse); 80 | 81 | $this->assertFalse($unsuspendResponse->isSuccessful()); 82 | $this->assertEquals('d', $unsuspendResponse->getStatus()); 83 | $this->assertEquals('This account is NOT currently suspended (status : ) . .', $unsuspendResponse->getMessage()); 84 | } 85 | 86 | public function testError() 87 | { 88 | $httpResponse = new Response(200, [], 'The API username you are using appears to be invalid 1. . '); 89 | 90 | $unsuspendResponse = new UnsuspendResponse($httpResponse); 91 | 92 | $this->assertFalse($unsuspendResponse->isSuccessful()); 93 | $this->assertNull($unsuspendResponse->getStatus()); 94 | $this->assertEquals('The API username you are using appears to be invalid 1. .', $unsuspendResponse->getMessage()); 95 | } 96 | } 97 | --------------------------------------------------------------------------------