diff --git a/src/bb-library/Server/Manager/MOFH.php b/src/bb-library/Server/Manager/MOFH.php index 5d9244f39..06e5ceb89 100644 --- a/src/bb-library/Server/Manager/MOFH.php +++ b/src/bb-library/Server/Manager/MOFH.php @@ -2,23 +2,29 @@ /** * BoxBilling * - * LICENSE + * @copyright BoxBilling, Inc (http://www.boxbilling.com) + * @license Apache-2.0 * - * This source file is subject to the license that is bundled - * with this package in the file LICENSE.txt - * It is also available through the world-wide-web at this URL: - * http://www.boxbilling.com/LICENSE.txt - * If you did not receive a copy of the license and are unable to - * obtain it through the world-wide-web, please send an email - * to license@boxbilling.com so we can send you a copy immediately. - * - * @copyright Copyright (c) 2010-2020 BoxBilling (http://www.boxbilling.com) - * @license http://www.boxbilling.com/LICENSE.txt - * @version $Id$ + * Copyright BoxBilling, Inc + * This source file is subject to the Apache-2.0 License that is bundled + * with this source code in the file LICENSE */ + use \InfinityFree\MofhClient\Client; class Server_Manager_MOFH extends Server_Manager { + // First, let's start with some information. + // + // MyOwnFreeHost has it's own (and weird) system of storing user identifiers. + // When making API calls, you *must* use the 8-digit username that you've generated and submitted to MOFH while creating the account (a1b2c3d4) + // Then, it generates *another* username, which is basically like "<3 lettered reseller identifier>_" (abc_12345678) + // This makes everything weird, the user logs into the control panel with the abc_12345678 username, but we still need to store the other one somewhere + // While making API calls, the username you submit is not the one which user uses during their sign-in process, it's the one *you* generated in the beginning. + // Hence, you need to store it, or hash the domain as we did. + // + // We used the "domainToID()" function to generate a unique hash for every domain, and we can be sure that it won't change unless the domain was changed + // Changing the domain is not supported by MyOwnFreeHost's API, so the domain probably won't be changed afterwards. + public function init() { $this->MOFHclient = Client::create([ @@ -31,6 +37,29 @@ class Server_Manager_MOFH extends Server_Manager } } + public static function domainToID($domain) + { + // So, here's how this works. + // We first generate a SHA-256 hash with the domain + // And then, strip out the first 8 characters to + // generate a unique ID. + // + // Let's understand it better with an example. + // So, here's the SHA-256 hash of 'boxbilling.com': + // 0d3019bc07e517c2158a2c7eaccdf286fdececa78de7f736f2d1d602522e80c7 + // + // We get the first 8 characters out of that, and it produces: + // 0d3019bc + // + // Since it's a hash, it doesn't matter whether portions are discarded, + // but rather that the same input will produce the same hash. + // + // And the nice part, we don't need to store this in a database. + // As long as it's the same domain, it will always generate the same hash. + + return mb_substr(hash('sha256', $domain), 0, 8); + } + public static function getForm() { return array( @@ -75,42 +104,58 @@ class Server_Manager_MOFH extends Server_Manager return $a; } + /** + * Create an account + * + * We'll use the provided Server_Account object to use the data from the order, + * and call the MyOwnFreeHost API to create an account. + * Then, we'll store the returned vPanel username to our local database. + * + * @param Server_Account $a + */ + public function createAccount(Server_Account $a) { - $p = $a->getPackage(); + // Temporarily suspending the account while it's being set up. + $a->setSuspended(true); $this->client = $a->getClient(); $request = $this->MOFHclient->createAccount([ - 'username' => $a->getAPIdef(), + 'username' => $this->domainToID($a->getDomain()), 'password' => $a->getPassword(), 'domain' => $a->getDomain(), 'email' => $this->client->getEmail(), 'plan' => 'boxbilling', ]); + // Let's send the request, and store the response. $response = $request->send(); - $this->client = $a->getClient(); - $this->client->setAPIdef($a->getUsername()); - $this->client->setUsername($response->getVpUsername()); + // Setting the username to the one that is being used in the client area. + $a->setUsername($response->getVpUsername()); + // If something went wrong, we'll catch the error, and display it. if (!$response->isSuccessful()) { throw new Server_Exception($response->getMessage()); } + // It's all done. We can now unsuspend the account. + $a->setSuspended(false); + return true; } public function suspendAccount(Server_Account $a, $suspend = true) { $request = $this->MOFHclient->suspend([ - 'username' => $a->getAPIdef(), + 'username' => $this->domainToID($a->getDomain()), 'reason' => $a->getNote(), 'linked' => false ]); $response = $request->send(); + // If something went wrong, we'll catch the error, and display it. if (!$response->isSuccessful()) { throw new Server_Exception('MOFH error: ' . $response->getMessage()); } @@ -121,11 +166,13 @@ class Server_Manager_MOFH extends Server_Manager public function unsuspendAccount(Server_Account $a) { $request = $this->MOFHclient->unsuspend([ - 'username' => $a->getAPIdef() + 'username' => $this->domainToID($a->getDomain()) ]); + // Let's send the request, and store the response. $response = $request->send(); + // If something went wrong, we'll catch the error, and display it. if (!$response->isSuccessful()) { throw new Server_Exception('MOFH error: ' . $response->getMessage()); } @@ -146,7 +193,7 @@ class Server_Manager_MOFH extends Server_Manager public function changeAccountPassword(Server_Account $a, $new) { $request = $this->MOFHclient->password([ - 'username' => $a->getAPIdef(), + 'username' => $this->domainToID($a->getDomain()), 'password' => $new, ]);