| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180 |
- <?php
- /**
- * WHMCS siteBuilder Provisioning Module
- *
- * Provisioning User Accounts & manage Websites on the siteBuilder Server
- *
- * @see https://centos-webpanel.com/
- * @copyright Copyright (c) Thurdata GmbH 2022
- * @license GPL
- */
- use WHMCS\Database\Capsule;
- require_once 'Net/DNS2.php';
- require_once(__DIR__ . '/api/sitebuilder.php');
- require_once(__DIR__ . '/api/SiteProApiClient.php');
- if (!defined('WHMCS')) {
- die('This file cannot be accessed directly');
- }
- /**
- * Define siteBuilder product metadata parameters.
- *
- * @see https://developers.whmcs.com/provisioning-modules/meta-data-params/
- *
- * @return array
- */
- function siteBuilder_MetaData() {
- return array(
- 'DisplayName' => '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) {
- // disable default site
- $siteBuilder = new ApiClient(getSiteBuilderApiURL($params), $params['serveraccesshash']);
- $status = $siteBuilder->isenabled($params['username'],$params['domain']);
- if($status['status'] != '200') {
- return 'Error: ' . $status['response']['error'];
- }
- if($status['response']['isenabled'] == 'YES'){
- $response = $siteBuilder->disable($params['username'],$params['domain']);
- if($response['status'] != '200') {
- return 'Error: ' . $response['response']['error'];
- }
- }
- // 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']);
- if(!empty($sites)) {
- foreach($sites as $site) {
- $response = $siteBuilder->disable($params['username'], $site, $params['serverusername'], $params['serverpassword']);
- logModuleCall(
- 'siteBuilder',
- __FUNCTION__,
- $params,
- 'Debug',
- $response
- );
- 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) {
- $siteBuilder = new ApiClient(getSiteBuilderApiURL($params), $params['serveraccesshash']);
- // enable deafult site
- $response = $siteBuilder->enable($params['username'], $params['domain'], $params['serverusername'], $params['serverpassword']);
- if($response['status'] != '200') {
- return 'Error: ' . $response['response']['error'];
- }
- // 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']);
- logModuleCall(
- 'siteBuilder',
- __FUNCTION__,
- $params,
- 'Debug',
- $site
- );
- 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'];
- $accEnabled = Capsule::table('sitePro_acc')
- ->where('pid', $params['serviceid'])
- ->value('enabled');
- $sitesObj = Capsule::table('sitePro_site')
- ->where('relid', $params['serviceid'])
- ->get();
- $clientInfo['account'] = ['enabled' => $accEnabled];
- $clientInfo['sites'] = [];
- foreach($sitesObj as $site){
- $response = $siteBuilder->getSSLDays($params['username'], $site->name);
- if($response['status'] == '200') {
- $sslSite = $response['response']['ssl_remaining'];
- }
- array_push($clientInfo['sites'],['name' => $site->name, 'sslSite' => $sslSite, 'enabled' => $site->enabled]);
- }
- $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 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']);
- $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']);
- // init prod
- $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';
- }
- 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($params) {
- return array(
- 'templatefile' => 'siteBuilder_del_site_confirm',
- 'vars' => array(
- 'delsite' => $_POST['s'],
- ),
- );
- }
- /**
- * Removes a domain 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');
- // 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
- ));
- 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 remove site from database',
- $e->getMessage()
- );
- return 'Error: could 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 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_revSiteConfirm($params) {
- return array(
- 'templatefile' => 'siteBuilder_rev_site_confirm',
- 'vars' => array(
- 'revSite' => $_POST['s'],
- ),
- );
- }
- /**
- * Revert all Changes of the development 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']);
- $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');
- // 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
- ));
- 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';
- }
- 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';
- }
- 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']);
- logModuleCall(
- 'siteBuilder',
- __FUNCTION__,
- $params,
- 'Debug',
- $response
- );
- 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
- * @param string $user
- * @param string $params common module parameters
- *
- * @return string $apiUrl
- */
- function getSiteBuilderApiURL($params) {
- $httpPrefix = $params['serversecure'] ? 'https://' : 'http://';
- $serverPort = $params['serverport'] ? ':' . $params['serverport'] . '/' : '/';
- return $httpPrefix . $params['serverhostname'] . $serverPort;
- }
- function getSites($serviceID) {
- try {
- $sites = Capsule::table('sitePro_site')
- ->where('relid',$serviceID)
- ->select('name');
- } catch (\Exception $e) {
- logModuleCall(
- 'siteBuilder',
- __FUNCTION__,
- $params,
- 'Error: could fetch sites from database',
- $e->getMessage()
- );
- return 'Error: could fetch sites from database';
- }
- logModuleCall(
- 'siteBuilder',
- __FUNCTION__,
- $serviceID,
- 'Getsites',
- $sites
- );
- return $sites;
- }
- function getSitesEnabled($serviceID) {
- try {
- $sites = Capsule::table('sitePro_site')
- ->where('relid',$serviceID)
- ->where('enabled', 1)
- ->value('name');
- } catch (\Exception $e) {
- logModuleCall(
- 'siteBuilder',
- __FUNCTION__,
- $params,
- 'Error: could fetch sites from database',
- $e->getMessage()
- );
- return 'Error: could fetch sites from database';
- }
- return $sites;
- }
- function siteBuilderCreateTables() {
- // Create a new table.
- if (!Capsule::schema()->hasTable('sitePro_acc')) {
- try {
- Capsule::schema()->create(
- 'sitePro_acc',
- function ($table) { logModuleCall(
- 'siteBuilder',
- __FUNCTION__,
- $params,
- 'Debug',
- $site
- );
-
- /** @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()}";
- }
- }
- }
|