| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200 |
- <?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',
- 'DefaultSSLPort' => '2443',
- '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/",
- ],
- "HostingPlanID" => [
- "FriendlyName" => "Hosting Plan ID",
- "Type" => "text", # Text Box
- "Size" => "25", # Defines the Field Width
- "Description" => "Set the hostingPlan ID for this Product",
- "Default" => "Free",
- ],
- "Quota" => [
- "FriendlyName" => "Quota in MB",
- "Type" => "text", # Text Box
- "Size" => "25", # Defines the Field Width
- "Description" => "Set the Quoat matching Your HostingPlan (MB)",
- "Default" => "512",
- ],
- "BuilderIP" => [
- "FriendlyName" => "Builder Public IP",
- "Type" => "text", # Text Box
- "Size" => "25", # Defines the Field Width
- "Description" => "Set the public IP of the Webserver for DNS handling",
- "Default" => "185.163.51.234",
- ]
- ];
- }
- /**
- * 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'];
- // 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($username, $params['domain'], $params['serverusername'], $params['serverpassword']);
- if($response['status'] != '200') {
- return 'Error: ' . $response['response']['error'];
- }
- // set quota for new account
- $response = $siteBuilder->setQuota($username, $params['configoption3'], $params['serverusername'], $params['serverpassword']);
- if($response['status'] != '200') {
- return 'Error: ' . $response['response']['error'];
- }
- /* enable this block to get a default domain website without hostname (otherwise the customer is able to do that)
- // create default domain site
- $response = $siteBuilder->init($username, $params['domain'], $params['serverusername'], $params['serverpassword']);
- if($response['status'] != '200') {
- return 'Error: ' . $response['response']['error'];
- }
- // add default site to database
- try {
- Capsule::table('sitePro_site')
- ->insert(
- array(
- 'relid' => $params['serviceid'],
- 'name' => $params['domain'],
- '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';
- }
- /**
- * 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'];
- }
- // remove sitebuilder session
- $response = siteBuilderRemoveSession($params,$site);
- if($response != 'success') {
- return 'Error: ' . $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());
- }
- }
- }
- /**
- * 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'];
- }
- // check OSI-8
- if(in_array($site,getSites($params['serviceid']))) {
- return 'Error: OSI-8 (Seite existiert bereits)';
- }
- // 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'];
- }
- // 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;
- }
- */
- $response = siteBuilderRemoveSession($params,$site);
- if($response != 'success') {
- return 'Error: ' . $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
- /**
- * Removes the sitePro builder session for a site
- *
- * @param array $params common module parameters
- * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
- * @param string $site sitename
- *
- * @return string "success" or an error message
- */
- function siteBuilderRemoveSession($params, $site) {
- // 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';
- }
- /**
- * 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['configoption4'],
- ),
- );
- 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()}";
- }
- }
- }
|