'ThurData SiteBuilder Provisioning', 'DefaultNonSSLPort' => '80', 'DefaultSSLPort' => '443', 'RequiresServer' => true ); } /** * Create tables if neccessary * Define siteBuilder product configuration options. * * @see https://developers.whmcs.com/provisioning-modules/config-options/ * * @return array */ function siteBuilder_ConfigOptions() { // check for tables and create if neccessary siteBuilderCreateTables(); // return ConfigOptions return ["BuilderURL" => [ "FriendlyName" => "Builder URL", # Full Builder URL (prefix//hostname:port/) "Type" => "text", # Text Box "Size" => "25", # Defines the Field Width "Description" => "Full Builder URL (prefix//hostname:port/)", "Default" => "https://builder.thurdata.ch/", ], [ "FriendlyName" => "Hosting Plan ID", "Type" => "text", # Text Box "Size" => "25", # Defines the Field Width "Description" => "Set the hostingPlan ID for this Product", "Default" => "Free", ], [ "FriendlyName" => "Quota in MB", "Type" => "text", # Text Box "Size" => "25", # Defines the Field Width "Description" => "Set the Quoat matching Your HostingPlan (MB)", "Default" => "512", ] ]; } /** * Test connection to a siteBuilder server with the given server parameters. * * Allows an admin user to verify that an API connection can be * successfully made with the given configuration parameters for a * server. * * When defined in a module, a test connection button will appear * alongside the server type dropdown when adding or editing an * existing server. * * @param array $params common module parameters * * @see https://developers.whmcs.com/provisioning-modules/module-parameters/ * * @return array */ function siteBuilder_Testconnection($params) { $siteBuilder = new ApiClient(getSiteBuilderApiURL($params), $params['serveraccesshash']); // ping remota API $response = $siteBuilder->ping($params['serverusername'], $params['serverpassword']); if($response['response']['answer'] == 'pong') { return array( 'success' => true, 'error' => '', ); } return array( 'success' => false, 'error' => $response, ); } /** * Provision a new siteBuilder account * * Attempt to provision a new siteBuilder account. This is * called any time provisioning is requested inside of WHMCS. Depending upon the * configuration, this can be any of: * * When a new order is placed * * When an invoice for a new order is paid * * Upon manual request by an admin user * * @param array $params common module parameters * * @see https://developers.whmcs.com/provisioning-modules/supported-functions/ * * @return string 'success' or an error message */ function siteBuilder_CreateAccount($params) { $username = strtolower(substr($params['clientsdetails']['firstname'],0,2) . substr($params['clientsdetails']['lastname'],0,3)) . $params['serviceid']; $userdomain = $params['domain']; // set DNS /* disabled on dev, has to be already set in test env $response = siteBuildersetDNS($params, $userdomain); if($response != 'success') { return $response; } */ // update service try { Capsule::table('tblhosting') ->where('id', '=', $params['serviceid']) ->update( array( 'username' => $username, 'domain' => $userdomain, ) ); } catch (\Exception $e) { logModuleCall( 'siteBuilder', __FUNCTION__, $params, 'Error: could save username & domain in database', $e->getMessage() ); return 'Error: could save username & password in database'; } // add account to database try { Capsule::table('sitePro_acc') ->insert( array( 'account' => $username, 'pid' => $params['serviceid'], 'enabled' => true, ) ); } catch (\Exception $e) { logModuleCall( 'siteBuilder', __FUNCTION__, $params, 'Error: could save username & serviceid in database', $e->getMessage() ); return 'Error: could save username & serviceid in database'; } $siteBuilder = new ApiClient(getSiteBuilderApiURL($params), $params['serveraccesshash']); // create siteBuilder account $response = $siteBuilder->create($params['username'], $params['domain'], $params['serverusername'], $params['serverpassword']); if($response['status'] != '200') { return 'Error: ' . $response['response']['error']; } // create siteBuilder base config for new account $response = $siteBuilder->init($params['username'], $params['domain'], $params['serverusername'], $params['serverpassword']); if($response['status'] != '200') { return 'Error: ' . $response['response']['error']; } // set quota for new account $response = $siteBuilder->setQuota($params['username'], $params['configoption3'], $params['serverusername'], $params['serverpassword']); if($response['status'] != '200') { return 'Error: ' . $response['response']['error']; } return 'success'; } /** * Removes a siteBuilder account and undeploy all related sites * * Called when a termination is requested. This can be invoked automatically for * overdue products if enabled, or requested manually by an admin user. * * @param array $params common module parameters * * @see https://developers.whmcs.com/provisioning-modules/supported-functions/ * * @return string 'success' or an error message */ function siteBuilder_TerminateAccount($params) { // check if account is suspended try { $active = Capsule::table('sitePro_acc') ->where('account',$params['username']) ->value('enabled'); } catch (\Exception $e) { logModuleCall( 'siteBuilder', __FUNCTION__, $params, 'Error: could fetch account from database', $e->getMessage() ); return 'Error: could fetch account from database'; } if($active == true) { return 'Error: Account is active, please suspend account first'; } // undeploy all related sites $sites = getSites($params['serviceid']); $siteBuilder = new ApiClient(getSiteBuilderApiURL($params), $params['serveraccesshash']); if(!empty($sites)) { foreach($sites as $site) { $response = $siteBuilder->undeploy($params['username'], $site, $params['serverusername'], $params['serverpassword']); if($response['status'] != '200') { return 'Error: ' . $response['response']; } } } // cleanup database try { Capsule::table('sitePro_site') ->where('relid',$params['serviceid']) ->delete(); } catch (\Exception $e) { logModuleCall( 'siteBuilder', __FUNCTION__, $params, 'Error: could remove site from database', $e->getMessage() ); return 'Error: could remove site from database'; } // terminate account $response = $siteBuilder->terminate($params['username'], $params['domain']); if($response['status'] != '200') { return 'Error: ' . $response['response']['error']; } try { Capsule::table('sitePro_acc') ->where('account',$params['username']) ->delete(); } catch (\Exception $e) { logModuleCall( 'siteBuilder', __FUNCTION__, $params, 'Error: could remove account from database', $e->getMessage() ); return 'Error: could remove account from database'; } return 'success'; } /** * Set a siteBuilder account to status inactive. * * Called when a suspension is requested. This is invoked automatically by WHMCS * when a product becomes overdue on payment or can be called manually by admin * user. * * @param array $params common module parameters * * @see https://developers.whmcs.com/provisioning-modules/supported-functions/ * * @return string 'success' or an error message */ function siteBuilder_SuspendAccount($params) { // set account to disabled in database try { Capsule::table('sitePro_acc') ->where('account',$params['username']) ->update(array( 'enabled' => false, )); } catch (\Exception $e) { logModuleCall( 'siteBuilder', __FUNCTION__, $params, 'Error: could not disable account in database', $e->getMessage() ); return 'Error: could not disable account in database'; } // disable all sites but not change status in DB for unsuspend restoring $sites = getSites($params['serviceid']); $siteBuilder = new ApiClient(getSiteBuilderApiURL($params), $params['serveraccesshash']); if(!empty($sites)) { foreach($sites as $site) { $response = $siteBuilder->disable($params['username'], $site, $params['serverusername'], $params['serverpassword']); if($response['status'] != '200') { return 'Error: ' . $response['response']['error']; } } } return 'success'; } /** * Set a siteBuilder account to status active and enable active sites * * Called when an un-suspension is requested. This is invoked * automatically upon payment of an overdue invoice for a product, or * can be called manually by admin user. * * @param array $params common module parameters * * @see https://developers.whmcs.com/provisioning-modules/supported-functions/ * * @return string 'success' or an error message */ function siteBuilder_UnsuspendAccount($params) { // set account to enabled in database try { Capsule::table('sitePro_acc') ->where('account',$params['username']) ->update(array( 'enabled' => true, )); } catch (\Exception $e) { logModuleCall( 'siteBuilder', __FUNCTION__, $params, 'Error: could update account in database', $e->getMessage() ); return 'Error: could update account in database'; } // enable active sites $sites = getSitesEnabled($params['serviceid']); $siteBuilder = new ApiClient(getSiteBuilderApiURL($params), $params['serveraccesshash']); if(!empty($sites)) { foreach($sites as $site) { $response = $siteBuilder->enable($params['username'], $site, $params['serverusername'], $params['serverpassword']); if($response['status'] != '200') { return 'Error: ' . $response['response']['error']; } } } return 'success'; } /** * Client area output logic handling. * * This function is used to define module specific client area output. It should * return an array consisting of a template file and optional additional * template variables to make available to that template. * * @param array $params common module parameters * * @see https://developers.whmcs.com/provisioning-modules/client-area-output/ * * @return array */ function siteBuilder_ClientArea($params) { $siteBuilder = new ApiClient(getSiteBuilderApiURL($params), $params['serveraccesshash']); $clientInfo = array('moduleclientarea' => '1'); $clientInfo['domain'] = $params['domain']; // Client status $accEnabled = Capsule::table('sitePro_acc') ->where('pid', $params['serviceid']) ->value('enabled'); $clientInfo['account'] = ['enabled' => $accEnabled]; $clientInfo['sites'] = []; // Client sites $sites = getSites($params['serviceid']); foreach($sites as $site){ $response = $siteBuilder->getSSLDays($params['username'], $site); if($response['status'] == '200') { $sslSite = $response['response']['ssl_remaining']; } $response = $siteBuilder->isenabled($params['username'], $site); if($response['status'] == '200') { $enabled = $response['response']['isenabled']; } array_push($clientInfo['sites'],['name' => $site, 'sslSite' => $sslSite, 'enabled' => $enabled]); } // Client Quota $response = $siteBuilder->getQuota($params['username']); if($response['status'] != '200') { logModuleCall( 'siteBuilder', __FUNCTION__, $params, 'Error getting Quota', $response ); } $clientInfo['quota'] = round($response['response']['quota'][0]['blocks']/1024); $clientInfo['limit'] = round($response['response']['quota'][0]['hard']/1024); // return template vars return array( 'tabOverviewReplacementTemplate' => 'clientarea', 'vars' => $clientInfo, ); } /** * Upgrade or downgrade a siteBuilder account by package. * * Called to apply any change in product assignment or parameters. It * is called to provision upgrade or downgrade orders, as well as being * able to be invoked manually by an admin user. * * This same function is called for upgrades and downgrades of both * products and configurable options. * * @param array $params common module parameters * * @see https://developers.whmcs.com/provisioning-modules/supported-functions/ * * @return string "success" or an error message */ function siteBuilder_ChangePackage($params) { $siteBuilder = new ApiClient(getSiteBuilderApiURL($params), $params['serveraccesshash']); // configoption3 contains quota in MB $response = $siteBuilder->setQuota($params['username'], $params['configoption3'], $params['serverusername'], $params['serverpassword']); if($response['status'] != '200') { return 'Error: ' . $response['response']['error']; } return 'success'; } /** * Usage Update * * Important: Runs daily per server not per product * Run Manually: /admin/reports.php?report=disk_usage_summary&action=updatestats * @param array $params common module parameters * * @see https://developers.whmcs.com/provisioning-modules/usage-update/ */ function siteBuilder_UsageUpdate($params) { $siteBuilder = new ApiClient(getSiteBuilderApiURL($params), $params['serveraccesshash']); $response = $siteBuilder->getStats(); if($response['status'] != '200') { logActivity('ERROR: Unable to update sitebuilder server usage: ' . implode('#',[$response])); } $stats = $response['response']['quota']; foreach($stats as $stat){ try { Capsule::table('tblhosting') ->where('server', $params['serverid']) ->where('username', $stat['user']) ->update([ 'diskusage' => $stat['used']/1024, 'disklimit' => $stat['hard']/1024, 'lastupdate' => Capsule::raw('now()'), ]); } catch (\Exception $e) { logActivity('ERROR: Unable to update sitebuilder server usage: ' . $e->getMessage()); } logModuleCall( 'siteBuilder', __FUNCTION__, $stat, 'debug', $params ); } } /** * Additional actions a client user can invoke. * * Define additional actions a client user can perform for an instance of a * product/service. * * Any actions you define here will be automatically displayed in the available * list of actions within the client area. * * @return array */ function siteBuilder_ClientAreaCustomButtonArray ($params) { return array( 'Neue Webseite' => 'newSite', ); } /** * Additional actions a client user can invoke. * * Define additional actions a client user is allowed to perform for an instance of a * product/service. * * @see https://developers.whmcs.com/provisioning-modules/supported-functions/ * * @return array */ function siteBuilder_ClientAreaAllowedFunctions() { return array( 'Add Site' => 'addSite', 'New Site' => 'newSite', 'Confirm Delete Site' => 'delSiteConfirm', 'Delete Site' => 'delSite', 'Edit Site' => 'editSite', 'Conform Revert Site' => 'revSiteConfirm', 'Revert Site' => 'revSite', 'Disable Site' => 'disableSite', 'Enable Site' => 'enableSite' ); } /** * Opens a form to add a new domain. * * @param array $params common module parameters * * @see https://developers.whmcs.com/provisioning-modules/supported-functions/ * * @return array template information */ function siteBuilder_newSite($params) { return array( 'breadcrumb' => array( 'clientarea.php?action=productdetails&id=' . $params['serviceid'] . '&modop=custom&a=newSite' => 'Neue Webseite', ), 'templatefile' => 'siteBuilder_new_site', ); } /** * Adds a new domain to a siteBuilder account. * * @param array $params common module parameters * * @see https://developers.whmcs.com/provisioning-modules/supported-functions/ * * @return string "success" or an error message */ function siteBuilder_addSite($params) { if(empty($_POST['d'])) { $site = $params['domain']; } else { if(!filter_var($_POST['d'], FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME)){ return 'Error: invalid site name'; } $site = $_POST['d'] . '.' . $params['domain']; } // set DNS /* disabled on dev, has to be already set in test env $response = siteBuildersetDNS($params, $site); if($response != 'success') { return $response; } */ $siteBuilder = new ApiClient(getSiteBuilderApiURL($params), $params['serveraccesshash']); // set up webconfig $response = $siteBuilder->init($params['username'], $site, $params['serverusername'], $params['serverpassword']); if($response['status'] != '200') { return 'Error: ' . $response['response']['error']; } // update DB try { Capsule::table('sitePro_site') ->insert( array( 'relid' => $params['serviceid'], 'name' => $site, 'enabled' => true, ) ); } catch (\Exception $e) { logModuleCall( 'siteBuilder', __FUNCTION__, $params, 'Error: could save site & serviceid in database', $e->getMessage() ); return 'Error: could save site & serviceid in database'; } return 'success'; } /** * Creates a sitePro editor session and redirect on success * * @param array $params common module parameters * * @see https://developers.whmcs.com/provisioning-modules/supported-functions/ * * @return string "success" or an error message */ function siteBuilder_editSite($params) { if(!filter_var($_POST['s'], FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME)){ return 'Error: invalid site name'; } $site = $_POST['s']; $api = new SiteProApiClient('https://builder.thurdata.ch/api/', 'apikey0', '993yVHwC05TLsx2JI2XFlAhkkPUxR6JbQUYbI.a5HiRtmNV9'); // use this for enterprise licenses and change 'your-bulder-domain.com' to your builder domain //$api = new SiteProApiClient('http://your-bulder-domain.com/api/', 'your_api_username', 'your_api_password'); try { // this call is used to open builder, so you need to set correct parameters to represent users website you want to open // this data usually comes from your user/hosting manager system $res = $api->remoteCall('requestLogin', array( 'type' => 'internal', // (required) 'internal' 'domain' => $site, // (required) domain of the user website you want to edit 'lang' => 'de', // (optional) 2-letter language code, set language code you whant builder to open in 'apiUrl' => getSiteBuilderApiURL($params) . 'deploy/' . $params['username'] . '/' . $site, // (required) API endpoint URL 'resellerClientAccountId' => $params['serviceid'], // (required) ID of website/user in your system 'username' => $params['serverusername'], // (optional) authorization username to be used with API endpoint 'password' => 'your-secure-password', // (optional) authorization password to be used with API endpoint 'hostingPlan' => $params['configoption2'], )); if (!$res || !is_object($res)) { logModuleCall( 'siteBuilder', __FUNCTION__, $params, 'Error: Response format error', $res ); return 'Error: Response format error'; } else if (isset($res->url) && $res->url) { logModuleCall( 'siteBuilder', __FUNCTION__, $params, 'Debug', $res ); // on success redirect to builder URL header('Location: '.$res->url, true); exit(); } else { logModuleCall( 'siteBuilder', __FUNCTION__, $params, 'Error: Unknown error', $res ); return 'Error: Unknown error'; } } catch (\Exception $e) { logModuleCall( 'siteBuilder', __FUNCTION__, $params, 'Error: Request error', $e->getMessage() ); return 'Error: Request error'; } return 'success'; } /** * Opens a form to delete a domain from a siteBuilder account. * * @param array $params common module parameters * * @see https://developers.whmcs.com/provisioning-modules/supported-functions/ * * @return array template information */ function siteBuilder_delSiteConfirm() { return array( 'templatefile' => 'siteBuilder_del_site_confirm', 'vars' => array( 'delsite' => $_POST['s'], ), ); } /** * Removes a site from a siteBuilder account. * * @param array $params common module parameters * * @see https://developers.whmcs.com/provisioning-modules/supported-functions/ * * @return string "success" or an error message */ function siteBuilder_delSite($params) { if(!filter_var($_POST['s'], FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME)){ return 'Error: invalid domain name'; } $site = $_POST['s']; $siteBuilder = new ApiClient(getSiteBuilderApiURL($params), $params['serveraccesshash']); // undeploy $response = $siteBuilder->undeploy($params['username'], $site, $params['serverusername'], $params['serverpassword']); if($response['status'] != '200') { return 'Error: ' . $response['response']['error']; } // remove builder session $api = new SiteProApiClient('https://builder.thurdata.ch/api/', 'apikey0', '993yVHwC05TLsx2JI2XFlAhkkPUxR6JbQUYbI.a5HiRtmNV9'); try { // this call is used to open builder, so you need to set correct parameters to represent users website you want to open // this data usually comes from your user/hosting manager system $res = $api->remoteCall('requestLogin', array( 'type' => 'internal', // (required) 'internal' 'domain' => $site, // (required) domain of the user website you want to edit 'lang' => 'de', // (optional) 2-letter language code, set language code you whant builder to open in 'apiUrl' => getSiteBuilderApiURL($params) . 'deploy/' . $params['username'] . '/' . $site, // (required) API endpoint URL 'resellerClientAccountId' => $params['serviceid'], // (required) ID of website/user in your system 'username' => $params['serverusername'], // (optional) authorization username to be used with API endpoint 'password' => 'your-secure-password', // (optional) authorization password to be used with API endpoint )); if (!$res || !is_object($res)) { logModuleCall( 'siteBuilder', __FUNCTION__, $params, 'Error: Response format error', $res ); return 'Error: Response format error'; } else if (isset($res->url) && $res->url) { $result = $api->remoteCall('delete-site', array( 'domain' => $site )); if (!$result || !is_object($result)) { logModuleCall( 'siteBuilder', __FUNCTION__, $params, 'Error: Response format error', $result ); return 'Error: Response format error'; } else if (isset($result->ok) && $res->ok) { return 'success'; } } else { logModuleCall( 'siteBuilder', __FUNCTION__, $params, 'Error: Unknown error', $res ); return 'Error: Unknown error'; } } catch (\Exception $e) { logModuleCall( 'siteBuilder', __FUNCTION__, $params, 'Error: Request error', $e->getMessage() ); return 'Error: Request error'; } // update DB try { Capsule::table('sitePro_site') ->where('name', $site) ->delete(); } catch (\Exception $e) { logModuleCall( 'siteBuilder', __FUNCTION__, $params, 'Error: could not remove site from database', $e->getMessage() ); return 'Error: could not remove site from database'; } // unset DNS /* disabled on dev, has to be already set in test env $response = siteBuilderunsetDNS($params, $site); if($response != 'success') { return $response; } */ return 'success'; } /** * Opens a form to re-init a website. * * @param array $params common module parameters * * @see https://developers.whmcs.com/provisioning-modules/supported-functions/ * * @return array template information */ function siteBuilder_revSiteConfirm($params) { return array( 'templatefile' => 'siteBuilder_rev_site_confirm', 'vars' => array( 'revSite' => $_POST['s'], ), ); } /** * Revert all Changes (re-init) of the Site. * * @param array $params common module parameters * * @see https://developers.whmcs.com/provisioning-modules/supported-functions/ * * @return string "success" or an error message */ function siteBuilder_revSite($params) { if(!filter_var($_POST['s'], FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME)){ return 'Error: invalid site name'; } $site = $_POST['s']; $siteBuilder = new ApiClient(getSiteBuilderApiURL($params), $params['serveraccesshash']); // re-init site on webhost $response = $siteBuilder->revert($params['username'], $site, $params['serverusername'], $params['serverpassword']); if($response['status'] != '200') { return 'Error: ' . $response['response']['error']; } // remove builder session $api = new SiteProApiClient('https://builder.thurdata.ch/api/', 'apikey0', '993yVHwC05TLsx2JI2XFlAhkkPUxR6JbQUYbI.a5HiRtmNV9'); try { // this call is used to open builder, so you need to set correct parameters to represent users website you want to open // this data usually comes from your user/hosting manager system $res = $api->remoteCall('requestLogin', array( 'type' => 'internal', // (required) 'internal' 'domain' => $site, // (required) domain of the user website you want to edit 'lang' => 'de', // (optional) 2-letter language code, set language code you whant builder to open in 'apiUrl' => getSiteBuilderApiURL($params) . 'deploy/' . $params['username'] . '/' . $site, // (required) API endpoint URL 'resellerClientAccountId' => $params['serviceid'], // (required) ID of website/user in your system 'username' => $params['serverusername'], // (optional) authorization username to be used with API endpoint 'password' => 'your-secure-password', // (optional) authorization password to be used with API endpoint )); if (!$res || !is_object($res)) { logModuleCall( 'siteBuilder', __FUNCTION__, $params, 'Error: Response format error', $res ); return 'Error: Response format error'; } else if (isset($res->url) && $res->url) { $result = $api->remoteCall('delete-site', array( 'domain' => $site )); if (!$result || !is_object($result)) { logModuleCall( 'siteBuilder', __FUNCTION__, $params, 'Error: Response format error', $result ); return 'Error: Response format error'; } else if (isset($result->ok) && $res->ok) { return 'success'; } } else { logModuleCall( 'siteBuilder', __FUNCTION__, $params, 'Error: Unknown error', $res ); return 'Error: Unknown error'; } } catch (\Exception $e) { logModuleCall( 'siteBuilder', __FUNCTION__, $params, 'Error: Request error', $e->getMessage() ); return 'Error: Request error'; } return 'success'; } /** * Enables a website. * * @param array $params common module parameters * * @see https://developers.whmcs.com/provisioning-modules/supported-functions/ * * @return string "success" or an error message */ function siteBuilder_enableSite($params) { if(!filter_var($_POST['s'], FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME)){ return 'Error: invalid site name'; } $site = $_POST['s']; $siteBuilder = new ApiClient(getSiteBuilderApiURL($params), $params['serveraccesshash']); // enable $response = $siteBuilder->enable($params['username'], $site, $params['serverusername'], $params['serverpassword']); if($response['status'] != '200') { return 'Error: ' . $response['response']['error']; } // update DB try { Capsule::table('sitePro_site') ->where('relid',$params['serviceid']) ->where('name',$site) ->update(array( 'enabled' => true, )); } catch (\Exception $e) { logModuleCall( 'siteBuilder', __FUNCTION__, $params, 'Error: could save site status in database', $e->getMessage() ); return 'Error: could save site status in database'; } return 'success'; } /** * Disables a website. * * @param array $params common module parameters * * @see https://developers.whmcs.com/provisioning-modules/supported-functions/ * * @return string "success" or an error message */ function siteBuilder_disableSite($params) { if(!filter_var($_POST['s'], FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME)){ return 'Error: invalid site name'; } $site = $_POST['s']; $siteBuilder = new ApiClient(getSiteBuilderApiURL($params), $params['serveraccesshash']); // disable $response = $siteBuilder->disable($params['username'], $site, $params['serverusername'], $params['serverpassword']); if($response['status'] != '200') { return 'Error: ' . $response['response']['error']; } // update DB try { Capsule::table('sitePro_site') ->where('relid',$params['serviceid']) ->where('name',$site) ->update(array( 'enabled' => false, )); } catch (\Exception $e) { logModuleCall( 'siteBuilder', __FUNCTION__, $params, 'Error: could save site status in database', $e->getMessage() ); return 'Error: could save site status in database'; } return 'success'; } // Helpers /** * Update a DNS zone for a domain setting a new record for a domain or subdomain of a CWP7 account. * * @param array $params common module parameters * * @see https://developers.whmcs.com/provisioning-modules/supported-functions/ * * @return string "success" or an error message */ function siteBuildersetDNS($params, $site) { $siteName = $site . '.'; $zoneRecords = array(); $domainRecord = array( 'line' => $siteName.'|A|0', 'name' => $siteName, 'type' => 'A', 'class' => 'IN', 'data' => array( 'address' => $params['serverip'], ), ); array_push($zoneRecords, $domainRecord); $zoneIDcollection = Capsule::table('dns_manager2_zone') ->select('id') ->where('name', '=', $params['domain']) ->where('clientid', '=', $params['userid']) ->get(); $zoneIDobj = $zoneIDcollection[0]; $zoneID = $zoneIDobj->{'id'}; if(!isset($zoneID)) { return 'Error: Zone for domain ' . $params['domain'] . ' or not owned by client'; } $dnsZone = localAPI('dnsmanager', array( 'dnsaction' => 'getZone', 'zone_id' => $zoneID)); foreach($dnsZone['data']->records as $record) { if(($record->name != $siteName) || ($record->type != 'A' && $record->type != 'CNAME')) { array_push($zoneRecords, $record); }; } $result = localAPI('dnsmanager' , array( 'dnsaction' => 'updateZone', 'zone_id' => $zoneID, 'records' => $zoneRecords, ) ); if($result['result'] != 'success') { return 'Error: ' . $result['message']; } return 'success'; } /** * Removing a DNS record for a site of a siteBuilder account. * * @param array $params common module parameters * * @see https://developers.whmcs.com/provisioning-modules/supported-functions/ * * @return string "success" or an error message */ function siteBuilderunsetDNS($params, $site) { $siteName = $site . '.'; $zoneRecords = array(); $zoneIDcollection = Capsule::table('dns_manager2_zone') ->select('id') ->where('name', '=', $params['domain']) ->where('clientid', '=', $params['userid']) ->get(); $zoneIDobj = $zoneIDcollection[0]; $zoneID = $zoneIDobj->{'id'}; if(!isset($zoneID)) { return 'Error: Zone for domain ' . $params['domain'] . ' or not owned by client'; } $dnsZone = localAPI('dnsmanager', array( 'dnsaction' => 'getZone', 'zone_id' => $zoneID)); foreach($dnsZone['data']->records as $record) { if(($record->name != $siteName) || ($record->type != 'A' && $record->type != 'CNAME')) { array_push($zoneRecords, $record); }; } $result = localAPI('dnsmanager' , array( 'dnsaction' => 'updateZone', 'zone_id' => $zoneID, 'records' => $zoneRecords, ) ); if($result['result'] != 'success') { return 'Error: ' . $result['message']; } return 'success'; } /** * Returns API Url . * * @param string $params common module parameters * * @see https://developers.whmcs.com/provisioning-modules/supported-functions/ * * @return string $apiUrl */ function getSiteBuilderApiURL($params) { $httpPrefix = $params['serversecure'] ? 'https://' : 'http://'; $serverPort = $params['serverport'] ? ':' . $params['serverport'] . '/' : '/'; return $httpPrefix . $params['serverhostname'] . $serverPort; } /** * Returns all sitenames of an account. * * @param string $params common module parameters * * @see https://developers.whmcs.com/provisioning-modules/supported-functions/ * * @return array $sites array of sitenames */ function getSites($serviceID) { $sitesObj = Capsule::table('sitePro_site') ->where('relid', $serviceID) ->get(); $sites = []; foreach($sitesObj as $site){ array_push($sites, $site->name); } return $sites; } /** * Returns all names of enabled sites of an account. * * @param string $params common module parameters * * @see https://developers.whmcs.com/provisioning-modules/supported-functions/ * * @return array $sites array of sitenames */ function getSitesEnabled($serviceID) { $sitesObj = Capsule::table('sitePro_site') ->where('relid', $serviceID) ->where('enabled', 1) ->get(); $sites = []; foreach($sitesObj as $site){ array_push($sites, $site->name); } return $sites; } /** * Creates tables for account & site management if not exists */ function siteBuilderCreateTables() { // Create a new table. if (!Capsule::schema()->hasTable('sitePro_acc')) { try { Capsule::schema()->create( 'sitePro_acc', function ($table) { /** @var \Illuminate\Database\Schema\Blueprint $table */ $table->increments('id'); $table->string('account'); $table->integer('pid'); $table->boolean('enabled'); } ); } catch (\Exception $e) { echo "Unable to create sitePro_acc: {$e->getMessage()}"; } } if (!Capsule::schema()->hasTable('sitePro_site')) { try { Capsule::schema()->create( 'sitePro_site', function ($table) { /** @var \Illuminate\Database\Schema\Blueprint $table */ $table->increments('id'); $table->integer('relid'); $table->string('name'); $table->boolean('enabled'); } ); } catch (\Exception $e) { echo "Unable to create sitePro_site: {$e->getMessage()}"; } } }