siteBuilder.php 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021
  1. <?php
  2. /**
  3. * WHMCS siteBuilder Provisioning Module
  4. *
  5. * Provisioning for User Account on the siteBuilder Server
  6. *
  7. * @see https://centos-webpanel.com/
  8. * @copyright Copyright (c) Thurdata GmbH 2022
  9. * @license GPL
  10. */
  11. use WHMCS\Database\Capsule;
  12. require_once 'Net/DNS2.php';
  13. require_once(__DIR__ . '/api/sitebuilder.php');
  14. require_once(__DIR__ . '/api/SiteProApiClient.php');
  15. if (!defined('WHMCS')) {
  16. die('This file cannot be accessed directly');
  17. }
  18. /**
  19. * Define siteBuilder product metadata parameters.
  20. *
  21. * @see https://developers.whmcs.com/provisioning-modules/meta-data-params/
  22. *
  23. * @return array
  24. */
  25. function siteBuilder_MetaData() {
  26. return array(
  27. 'DisplayName' => 'ThurData SiteBuilder Provisioning',
  28. 'APIVersion' => '1.2',
  29. 'DefaultNonSSLPort' => '80',
  30. 'DefaultSSLPort' => '443',
  31. 'RequiresServer' => true,
  32. 'ServiceSingleSignOnLabel' => 'Login to siteBuilder',
  33. 'AdminSingleSignOnLabel' => 'Login to siteBuilder Admin'
  34. );
  35. }
  36. function siteBuilder_ConfigOptions() {
  37. siteBuilderCreateTables();
  38. return ["BuilderURL" => [
  39. "FriendlyName" => "Builder URL", # Full Builder URL (prefix//hostname:port/)
  40. "Type" => "text", # Text Box
  41. "Size" => "25", # Defines the Field Width
  42. "Description" => "Full Builder URL (prefix//hostname:port/)",
  43. "Default" => "https://builder.thurdata.ch/",
  44. ],
  45. ];
  46. }
  47. /**
  48. * Test connection to a siteBuilder server with the given server parameters.
  49. *
  50. * Allows an admin user to verify that an API connection can be
  51. * successfully made with the given configuration parameters for a
  52. * server.
  53. *
  54. * When defined in a module, a test connection button will appear
  55. * alongside the server type dropdown when adding or editing an
  56. * existing server.
  57. *
  58. * @param array $params common module parameters
  59. *
  60. * @see https://developers.whmcs.com/provisioning-modules/module-parameters/
  61. *
  62. * @return array
  63. */
  64. function siteBuilder_Testconnection($params) {
  65. $siteBuilder = new ApiClient(getSiteBuilderApiURL($params), $params['serveraccesshash']);
  66. $response = $siteBuilder->ping($params['serverusername'], $params['serverpassword']);
  67. if($response['response']['answer'] == 'pong') {
  68. return array(
  69. 'success' => true,
  70. 'error' => '',
  71. );
  72. }
  73. return array(
  74. 'success' => false,
  75. 'error' => $response,
  76. );
  77. }
  78. /**
  79. * Provision a new account of a siteBuilder server.
  80. *
  81. * Attempt to provision a new siteBuilder account. This is
  82. * called any time provisioning is requested inside of WHMCS. Depending upon the
  83. * configuration, this can be any of:
  84. * * When a new order is placed
  85. * * When an invoice for a new order is paid
  86. * * Upon manual request by an admin user
  87. *
  88. * @param array $params common module parameters
  89. *
  90. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  91. *
  92. * @return string 'success' or an error message
  93. */
  94. function siteBuilder_CreateAccount($params) {
  95. $username = strtolower(substr($params['clientsdetails']['firstname'],0,2) . substr($params['clientsdetails']['lastname'],0,3)) . $params['serviceid'];
  96. $userdomain = $params['domain'];
  97. try {
  98. Capsule::table('tblhosting')
  99. ->where('id', '=', $params['serviceid'])
  100. ->update(
  101. array(
  102. 'username' => $username,
  103. 'domain' => $userdomain,
  104. )
  105. );
  106. } catch (\Exception $e) {
  107. logModuleCall(
  108. 'siteBuilder',
  109. __FUNCTION__,
  110. $params,
  111. 'Error: could save username & domain in database',
  112. $e->getMessage()
  113. );
  114. return 'Error: could save username & password in database';
  115. }
  116. try {
  117. Capsule::table('sitePro_acc')
  118. ->insert(
  119. array(
  120. 'account' => $username,
  121. 'pid' => $params['serviceid'],
  122. 'enabled' => true,
  123. )
  124. );
  125. } catch (\Exception $e) {
  126. logModuleCall(
  127. 'siteBuilder',
  128. __FUNCTION__,
  129. $params,
  130. 'Error: could save username & serviceid in database',
  131. $e->getMessage()
  132. );
  133. return 'Error: could save username & serviceid in database';
  134. }
  135. $siteBuilder = new ApiClient(getSiteBuilderApiURL($params), $params['serveraccesshash']);
  136. $response = $siteBuilder->create($params['username'], $params['domain'], $params['serverusername'], $params['serverpassword']);
  137. if($response['status'] != '200') {
  138. return 'Error: ' . $response['response']['error'];
  139. }
  140. $response = $siteBuilder->init($params['username'], $params['domain'], $params['serverusername'], $params['serverpassword']);
  141. if($response['status'] != '200') {
  142. return 'Error: ' . $response['response']['error'];
  143. }
  144. return 'success';
  145. }
  146. /**
  147. * Removes a siteBuilder account.
  148. *
  149. * Called when a termination is requested. This can be invoked automatically for
  150. * overdue products if enabled, or requested manually by an admin user.
  151. *
  152. * @param array $params common module parameters
  153. *
  154. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  155. *
  156. * @return string 'success' or an error message
  157. */
  158. function siteBuilder_TerminateAccount($params) {
  159. try {
  160. $active = Capsule::table('sitePro_acc')
  161. ->where('account',$params['username'])
  162. ->value('enabled');
  163. } catch (\Exception $e) {
  164. logModuleCall(
  165. 'siteBuilder',
  166. __FUNCTION__,
  167. $params,
  168. 'Error: could fetch account from database',
  169. $e->getMessage()
  170. );
  171. return 'Error: could fetch account from database';
  172. }
  173. if($active == true) {
  174. return 'Error: Account is active, please suspend account first';
  175. }
  176. // undeploy all related sites
  177. $sites = getSites($params['serviceid']);
  178. $siteBuilder = new ApiClient(getSiteBuilderApiURL($params), $params['serveraccesshash']);
  179. if(!empty($sites)) {
  180. foreach($sites as $site) {
  181. /* $response = $siteBuilder->undeploy($params['username'], $site, $params['serverusername'], $params['serverpassword']);
  182. if($response['status'] != '200') {
  183. return 'Error: ' . $response['response'];
  184. } */
  185. logModuleCall(
  186. 'siteBuilder',
  187. __FUNCTION__,
  188. $params,
  189. 'debug',
  190. $site
  191. );
  192. }
  193. try {
  194. Capsule::table('sitePro_site')
  195. ->where('relid',$params['serviceid'])
  196. ->delete();
  197. } catch (\Exception $e) {
  198. logModuleCall(
  199. 'siteBuilder',
  200. __FUNCTION__,
  201. $params,
  202. 'Error: could remove domains from database',
  203. $e->getMessage()
  204. );
  205. return 'Error: could remove domains from database';
  206. }
  207. }
  208. // terminate account
  209. $response = $siteBuilder->terminate($params['username'], $params['domain']);
  210. if($response['status'] != '200') {
  211. return 'Error: ' . $response['response']['error'];
  212. }
  213. try {
  214. Capsule::table('sitePro_acc')
  215. ->where('account',$params['username'])
  216. ->delete();
  217. } catch (\Exception $e) {
  218. logModuleCall(
  219. 'siteBuilder',
  220. __FUNCTION__,
  221. $params,
  222. 'Error: could remove account from database',
  223. $e->getMessage()
  224. );
  225. return 'Error: could remove account from database';
  226. }
  227. return 'success';
  228. }
  229. /**
  230. * Set a siteBuilder account to status inactive.
  231. *
  232. * Called when a suspension is requested. This is invoked automatically by WHMCS
  233. * when a product becomes overdue on payment or can be called manually by admin
  234. * user.
  235. *
  236. * @param array $params common module parameters
  237. *
  238. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  239. *
  240. * @return string 'success' or an error message
  241. */
  242. function siteBuilder_SuspendAccount($params) {
  243. $siteBuilder = new ApiClient(getSiteBuilderApiURL($params), $params['serveraccesshash']);
  244. $status = $siteBuilder->isenabled($params['username'],$params['domain']);
  245. if($status['status'] != '200') {
  246. return 'Error: ' . $status['response']['error'];
  247. }
  248. if($response['response']['isenabled'] == 'YES'){
  249. $response = $siteBuilder->disable($params['username'],$params['domain']);
  250. if($response['status'] != '200') {
  251. return 'Error: ' . $response['response']['error'];
  252. }
  253. }
  254. try {
  255. Capsule::table('sitePro_acc')
  256. ->where('account',$params['username'])
  257. ->update(array(
  258. 'enabled' => false,
  259. ));
  260. } catch (\Exception $e) {
  261. logModuleCall(
  262. 'siteBuilder',
  263. __FUNCTION__,
  264. $params,
  265. 'Error: could not disable account in database',
  266. $e->getMessage()
  267. );
  268. return 'Error: could not disable account in database';
  269. }
  270. // disable all sites but not change status in DB for unsuspend restoring
  271. $sites = getSites($params['serviceid']);
  272. if(!empty($sites)) {
  273. $siteBuilder = new ApiClient(getSiteBuilderApiURL($params), $params['serveraccesshash']);
  274. foreach($sites as $site) {
  275. $response = $siteBuilder->disable($params['username'], $site, $params['serverusername'], $params['serverpassword']);
  276. if($response['status'] != '200') {
  277. return 'Error: ' . $response['response']['error'];
  278. }
  279. }
  280. }
  281. return 'success';
  282. }
  283. /**
  284. * Set a siteBuilder account to status active.
  285. *
  286. * Called when an un-suspension is requested. This is invoked
  287. * automatically upon payment of an overdue invoice for a product, or
  288. * can be called manually by admin user.
  289. *
  290. * @param array $params common module parameters
  291. *
  292. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  293. *
  294. * @return string 'success' or an error message
  295. */
  296. function siteBuilder_UnsuspendAccount($params) {
  297. try {
  298. Capsule::table('sitePro_acc')
  299. ->where('account',$params['username'])
  300. ->update(array(
  301. 'enabled' => true,
  302. ));
  303. } catch (\Exception $e) {
  304. logModuleCall(
  305. 'siteBuilder',
  306. __FUNCTION__,
  307. $params,
  308. 'Error: could update account in database',
  309. $e->getMessage()
  310. );
  311. return 'Error: could update account in database';
  312. }
  313. // enable active sites
  314. $sites = getSitesEnabled($params['serviceid']);
  315. if(!empty($sites)) {
  316. $siteBuilder = new ApiClient(getSiteBuilderApiURL($params), $params['serveraccesshash']);
  317. foreach($sites as $site) {
  318. $response = $siteBuilder->enable($params['username'], $site, $params['serverusername'], $params['serverpassword']);
  319. if($response['status'] != '200') {
  320. return 'Error: ' . $response['response']['error'];
  321. }
  322. }
  323. }
  324. return 'success';
  325. }
  326. /**
  327. * Client area output logic handling.
  328. *
  329. * This function is used to define module specific client area output. It should
  330. * return an array consisting of a template file and optional additional
  331. * template variables to make available to that template.
  332. *
  333. * @param array $params common module parameters
  334. *
  335. * @see https://developers.whmcs.com/provisioning-modules/client-area-output/
  336. *
  337. * @return array
  338. */
  339. function siteBuilder_ClientArea($params) {
  340. $siteBuilder = new ApiClient(getSiteBuilderApiURL($params), $params['serveraccesshash']);
  341. $clientInfo = array('moduleclientarea' => '1');
  342. $clientInfo['domain'] = $params['domain'];
  343. $accEnabled = Capsule::table('sitePro_acc')
  344. ->where('pid', $params['serviceid'])
  345. ->value('enabled');
  346. $sitesObj = Capsule::table('sitePro_site')
  347. ->where('relid', $params['serviceid'])
  348. ->get();
  349. $clientInfo['account'] = ['enabled' => $accEnabled];
  350. $clientInfo['sites'] = [];
  351. $sslSite = 0;
  352. foreach($sitesObj as $site){
  353. $response = $siteBuilder->getSSLDays($params['username'], $site->name);
  354. if($response['status'] == '200') {
  355. $sslSite = $response['response']['ssl_remaining'];
  356. }
  357. array_push($clientInfo['sites'],['name' => $site->name, 'sslSite' => $sslSite, 'enabled' => $site->enabled]);
  358. }
  359. $quota = $siteBuilder->getQuota($params['username']);
  360. logModuleCall(
  361. 'siteBuilder',
  362. __FUNCTION__,
  363. $clientInfo,
  364. 'debug',
  365. $quota
  366. );
  367. $stats = $siteBuilder->getStats();
  368. logModuleCall(
  369. 'siteBuilder',
  370. __FUNCTION__,
  371. $clientInfo,
  372. 'debug',
  373. $stats
  374. );
  375. return array(
  376. 'tabOverviewReplacementTemplate' => 'clientarea',
  377. 'vars' => $clientInfo,
  378. );
  379. }
  380. /**
  381. * Perform single sign-on for a siteBuilder account.
  382. *
  383. * When successful, returns a URL to which the user should be redirected.
  384. *
  385. * @param array $params common module parameters
  386. *
  387. * @see https://developers.whmcs.com/provisioning-modules/single-sign-on/
  388. *
  389. * @return array
  390. */
  391. function siteBuilder_ServiceSingleSignOn($params) {
  392. }
  393. /**
  394. * Upgrade or downgrade a siteBuilder account by package.
  395. *
  396. * Called to apply any change in product assignment or parameters. It
  397. * is called to provision upgrade or downgrade orders, as well as being
  398. * able to be invoked manually by an admin user.
  399. *
  400. * This same function is called for upgrades and downgrades of both
  401. * products and configurable options.
  402. *
  403. * @param array $params common module parameters
  404. *
  405. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  406. *
  407. * @return string "success" or an error message
  408. */
  409. function siteBuilder_ChangePackage($params) {
  410. return 'success';
  411. }
  412. /**
  413. * Usage Update
  414. *
  415. * Important: Runs daily per server not per product
  416. * Run Manually: /admin/reports.php?report=disk_usage_summary&action=updatestats
  417. * @param array $params common module parameters
  418. *
  419. * @see https://developers.whmcs.com/provisioning-modules/usage-update/
  420. */
  421. function siteBuilder_UsageUpdate($params) {
  422. $siteBuilder = new ApiClient(getSiteBuilderApiURL($params), $params['serveraccesshash']);
  423. $response = $siteBuilder->getStats();
  424. if($response['status'] != '200') {
  425. logActivity('ERROR: Unable to update sitebuilder server usage: ' . implode('#',[$response]));
  426. }
  427. $stats = $response['response']['quota'];
  428. $productsObj = Capsule::table('tblhosting')
  429. ->select('*')
  430. ->where('server', '=', $params['serverid'])
  431. ->where('domainstatus', '=', 'Active')
  432. ->get();
  433. logModuleCall(
  434. 'siteBuilder',
  435. __FUNCTION__,
  436. $stats,
  437. 'debug',
  438. $productsObj
  439. );
  440. }
  441. /**
  442. * Additional actions a client user can invoke.
  443. *
  444. * Define additional actions a client user can perform for an instance of a
  445. * product/service.
  446. *
  447. * Any actions you define here will be automatically displayed in the available
  448. * list of actions within the client area.
  449. *
  450. * @return array
  451. */
  452. function siteBuilder_ClientAreaCustomButtonArray ($params) {
  453. return array(
  454. 'Neue Webseite' => 'newSite',
  455. );
  456. }
  457. /**
  458. * Additional actions a client user can invoke.
  459. *
  460. * Define additional actions a client user is allowed to perform for an instance of a
  461. * product/service.
  462. *
  463. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  464. *
  465. * @return array
  466. */
  467. function siteBuilder_ClientAreaAllowedFunctions() {
  468. return array(
  469. 'Add Site' => 'addSite',
  470. 'New Site' => 'newSite',
  471. 'Confirm Delete Site' => 'delSiteConfirm',
  472. 'Delete Site' => 'delSite',
  473. 'Edit Site' => 'editSite',
  474. 'Conform Revert Site' => 'revSiteConfirm',
  475. 'Revert Site' => 'revSite',
  476. 'Disable Site' => 'disableSite',
  477. 'Enable Site' => 'enableSite'
  478. );
  479. }
  480. /**
  481. * Opens a form to add a new domain.
  482. *
  483. * @param array $params common module parameters
  484. *
  485. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  486. *
  487. * @return array template information
  488. */
  489. function siteBuilder_newSite($params) {
  490. return array(
  491. 'breadcrumb' => array(
  492. 'clientarea.php?action=productdetails&id=' . $params['serviceid'] . '&modop=custom&a=newSite' => 'Neue Webseite',
  493. ),
  494. 'templatefile' => 'siteBuilder_new_site',
  495. );
  496. }
  497. /**
  498. * Adds a new domain to a siteBuilder account.
  499. *
  500. * @param array $params common module parameters
  501. *
  502. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  503. *
  504. * @return string "success" or an error message
  505. */
  506. function siteBuilder_addSite($params) {
  507. if(empty($_POST['d'])) {
  508. $site = $params['domain'];
  509. } else {
  510. if(!filter_var($_POST['d'], FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME)){
  511. return 'Error: invalid site name';
  512. }
  513. $site = $_POST['d'] . '.' . $params['domain'];
  514. }
  515. $siteBuilder = new ApiClient(getSiteBuilderApiURL($params), $params['serveraccesshash']);
  516. // init prod
  517. $response = $siteBuilder->init($params['username'], $site, $params['serverusername'], $params['serverpassword']);
  518. if($response['status'] != '200') {
  519. return 'Error: ' . $response['response']['error'];
  520. }
  521. // update DB
  522. try {
  523. Capsule::table('sitePro_site')
  524. ->insert(
  525. array(
  526. 'relid' => $params['serviceid'],
  527. 'name' => $site,
  528. 'enabled' => true,
  529. )
  530. );
  531. } catch (\Exception $e) {
  532. logModuleCall(
  533. 'siteBuilder',
  534. __FUNCTION__,
  535. $params,
  536. 'Error: could save site & serviceid in database',
  537. $e->getMessage()
  538. );
  539. return 'Error: could save site & serviceid in database';
  540. }
  541. return 'success';
  542. }
  543. function siteBuilder_editSite($params) {
  544. if(!filter_var($_POST['s'], FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME)){
  545. return 'Error: invalid site name';
  546. }
  547. $site = $_POST['s'];
  548. $api = new SiteProApiClient('https://builder.thurdata.ch/api/', 'apikey0', '993yVHwC05TLsx2JI2XFlAhkkPUxR6JbQUYbI.a5HiRtmNV9');
  549. // use this for enterprise licenses and change 'your-bulder-domain.com' to your builder domain
  550. //$api = new SiteProApiClient('http://your-bulder-domain.com/api/', 'your_api_username', 'your_api_password');
  551. try {
  552. // this call is used to open builder, so you need to set correct parameters to represent users website you want to open
  553. // this data usually comes from your user/hosting manager system
  554. $res = $api->remoteCall('requestLogin', array(
  555. 'type' => 'internal', // (required) 'internal'
  556. 'domain' => $site, // (required) domain of the user website you want to edit
  557. 'lang' => 'de', // (optional) 2-letter language code, set language code you whant builder to open in
  558. 'apiUrl' => getSiteBuilderApiURL($params) . 'deploy/' . $params['username'] . '/' . $site, // (required) API endpoint URL
  559. 'resellerClientAccountId' => $params['serviceid'], // (required) ID of website/user in your system
  560. 'username' => $params['serverusername'], // (optional) authorization username to be used with API endpoint
  561. 'password' => 'your-secure-password', // (optional) authorization password to be used with API endpoint
  562. ));
  563. if (!$res || !is_object($res)) {
  564. logModuleCall(
  565. 'siteBuilder',
  566. __FUNCTION__,
  567. $params,
  568. 'Error: Response format error',
  569. $res
  570. );
  571. return 'Error: Response format error';
  572. } else if (isset($res->url) && $res->url) {
  573. logModuleCall(
  574. 'siteBuilder',
  575. __FUNCTION__,
  576. $params,
  577. 'Debug',
  578. $res
  579. );
  580. // on success redirect to builder URL
  581. header('Location: '.$res->url, true);
  582. exit();
  583. } else {
  584. logModuleCall(
  585. 'siteBuilder',
  586. __FUNCTION__,
  587. $params,
  588. 'Error: Unknown error',
  589. $res
  590. );
  591. return 'Error: Unknown error';
  592. }
  593. } catch (\Exception $e) {
  594. logModuleCall(
  595. 'siteBuilder',
  596. __FUNCTION__,
  597. $params,
  598. 'Error: Request error',
  599. $e->getMessage()
  600. );
  601. return 'Error: Request error';
  602. }
  603. return 'success';
  604. }
  605. /**
  606. * Opens a form to delete a domain from a siteBuilder account.
  607. *
  608. * @param array $params common module parameters
  609. *
  610. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  611. *
  612. * @return array template information
  613. */
  614. function siteBuilder_delSiteConfirm($params) {
  615. return array(
  616. 'templatefile' => 'siteBuilder_del_site_confirm',
  617. 'vars' => array(
  618. 'delsite' => $_POST['s'],
  619. ),
  620. );
  621. }
  622. /**
  623. * Removes a domain from a siteBuilder account.
  624. *
  625. * @param array $params common module parameters
  626. *
  627. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  628. *
  629. * @return string "success" or an error message
  630. */
  631. function siteBuilder_delSite($params) {
  632. if(!filter_var($_POST['s'], FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME)){
  633. return 'Error: invalid domain name';
  634. }
  635. $site = $_POST['s'];
  636. $siteBuilder = new ApiClient(getSiteBuilderApiURL($params), $params['serveraccesshash']);
  637. // undeploy
  638. $response = $siteBuilder->undeploy($params['username'], $site, $params['serverusername'], $params['serverpassword']);
  639. if($response['status'] != '200') {
  640. return 'Error: ' . $response['response']['error'];
  641. }
  642. // remove builder session
  643. $api = new SiteProApiClient('https://builder.thurdata.ch/api/', 'apikey0', '993yVHwC05TLsx2JI2XFlAhkkPUxR6JbQUYbI.a5HiRtmNV9');
  644. // use this for enterprise licenses and change 'your-bulder-domain.com' to your builder domain
  645. //$api = new SiteProApiClient('http://your-bulder-domain.com/api/', 'your_api_username', 'your_api_password');
  646. try {
  647. // this call is used to open builder, so you need to set correct parameters to represent users website you want to open
  648. // this data usually comes from your user/hosting manager system
  649. $res = $api->remoteCall('requestLogin', array(
  650. 'type' => 'internal', // (required) 'internal'
  651. 'domain' => $site, // (required) domain of the user website you want to edit
  652. 'lang' => 'de', // (optional) 2-letter language code, set language code you whant builder to open in
  653. 'apiUrl' => getSiteBuilderApiURL($params) . 'deploy/' . $params['username'] . '/' . $site, // (required) API endpoint URL
  654. 'resellerClientAccountId' => $params['serviceid'], // (required) ID of website/user in your system
  655. 'username' => $params['serverusername'], // (optional) authorization username to be used with API endpoint
  656. 'password' => 'your-secure-password', // (optional) authorization password to be used with API endpoint
  657. ));
  658. if (!$res || !is_object($res)) {
  659. logModuleCall(
  660. 'siteBuilder',
  661. __FUNCTION__,
  662. $params,
  663. 'Error: Response format error',
  664. $res
  665. );
  666. return 'Error: Response format error';
  667. } else if (isset($res->url) && $res->url) {
  668. $result = $api->remoteCall('delete-site', array(
  669. 'domain' => $site
  670. ));
  671. if (!$result || !is_object($result)) {
  672. logModuleCall(
  673. 'siteBuilder',
  674. __FUNCTION__,
  675. $params,
  676. 'Error: Response format error',
  677. $result
  678. );
  679. return 'Error: Response format error';
  680. } else if (isset($result->ok) && $res->ok) {
  681. return 'success';
  682. }
  683. } else {
  684. logModuleCall(
  685. 'siteBuilder',
  686. __FUNCTION__,
  687. $params,
  688. 'Error: Unknown error',
  689. $res
  690. );
  691. return 'Error: Unknown error';
  692. }
  693. } catch (\Exception $e) {
  694. logModuleCall(
  695. 'siteBuilder',
  696. __FUNCTION__,
  697. $params,
  698. 'Error: Request error',
  699. $e->getMessage()
  700. );
  701. return 'Error: Request error';
  702. }
  703. // update DB
  704. try {
  705. Capsule::table('sitePro_site')
  706. ->where('name', $site)
  707. ->delete();
  708. } catch (\Exception $e) {
  709. logModuleCall(
  710. 'siteBuilder',
  711. __FUNCTION__,
  712. $params,
  713. 'Error: could remove site from database',
  714. $e->getMessage()
  715. );
  716. return 'Error: could remove site from database';
  717. }
  718. return 'success';
  719. }
  720. /**
  721. * Opens a form to delete a domain from a siteBuilder account.
  722. *
  723. * @param array $params common module parameters
  724. *
  725. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  726. *
  727. * @return array template information
  728. */
  729. function siteBuilder_revSiteConfirm($params) {
  730. return array(
  731. 'templatefile' => 'siteBuilder_rev_site_confirm',
  732. 'vars' => array(
  733. 'revSite' => $_POST['s'],
  734. ),
  735. );
  736. }
  737. /**
  738. * Revert all Changes of the development Site.
  739. *
  740. * @param array $params common module parameters
  741. *
  742. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  743. *
  744. * @return string "success" or an error message
  745. */
  746. function siteBuilder_revSite($params) {
  747. if(!filter_var($_POST['s'], FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME)){
  748. return 'Error: invalid site name';
  749. }
  750. $site = $_POST['s'];
  751. $siteBuilder = new ApiClient(getSiteBuilderApiURL($params), $params['serveraccesshash']);
  752. $response = $siteBuilder->revert($params['username'], $site, $params['serverusername'], $params['serverpassword']);
  753. if($response['status'] != '200') {
  754. return 'Error: ' . $response['response']['error'];
  755. }
  756. // remove builder session
  757. $api = new SiteProApiClient('https://builder.thurdata.ch/api/', 'apikey0', '993yVHwC05TLsx2JI2XFlAhkkPUxR6JbQUYbI.a5HiRtmNV9');
  758. // use this for enterprise licenses and change 'your-bulder-domain.com' to your builder domain
  759. //$api = new SiteProApiClient('http://your-bulder-domain.com/api/', 'your_api_username', 'your_api_password');
  760. try {
  761. // this call is used to open builder, so you need to set correct parameters to represent users website you want to open
  762. // this data usually comes from your user/hosting manager system
  763. $res = $api->remoteCall('requestLogin', array(
  764. 'type' => 'internal', // (required) 'internal'
  765. 'domain' => $site, // (required) domain of the user website you want to edit
  766. 'lang' => 'de', // (optional) 2-letter language code, set language code you whant builder to open in
  767. 'apiUrl' => getSiteBuilderApiURL($params) . 'deploy/' . $params['username'] . '/' . $site, // (required) API endpoint URL
  768. 'resellerClientAccountId' => $params['serviceid'], // (required) ID of website/user in your system
  769. 'username' => $params['serverusername'], // (optional) authorization username to be used with API endpoint
  770. 'password' => 'your-secure-password', // (optional) authorization password to be used with API endpoint
  771. ));
  772. if (!$res || !is_object($res)) {
  773. logModuleCall(
  774. 'siteBuilder',
  775. __FUNCTION__,
  776. $params,
  777. 'Error: Response format error',
  778. $res
  779. );
  780. return 'Error: Response format error';
  781. } else if (isset($res->url) && $res->url) {
  782. $result = $api->remoteCall('delete-site', array(
  783. 'domain' => $site
  784. ));
  785. if (!$result || !is_object($result)) {
  786. logModuleCall(
  787. 'siteBuilder',
  788. __FUNCTION__,
  789. $params,
  790. 'Error: Response format error',
  791. $result
  792. );
  793. return 'Error: Response format error';
  794. } else if (isset($result->ok) && $res->ok) {
  795. return 'success';
  796. }
  797. } else {
  798. logModuleCall(
  799. 'siteBuilder',
  800. __FUNCTION__,
  801. $params,
  802. 'Error: Unknown error',
  803. $res
  804. );
  805. return 'Error: Unknown error';
  806. }
  807. } catch (\Exception $e) {
  808. logModuleCall(
  809. 'siteBuilder',
  810. __FUNCTION__,
  811. $params,
  812. 'Error: Request error',
  813. $e->getMessage()
  814. );
  815. return 'Error: Request error';
  816. }
  817. return 'success';
  818. }
  819. function siteBuilder_enableSite($params) {
  820. if(!filter_var($_POST['s'], FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME)){
  821. return 'Error: invalid site name';
  822. }
  823. $site = $_POST['s'];
  824. $siteBuilder = new ApiClient(getSiteBuilderApiURL($params), $params['serveraccesshash']);
  825. // enable
  826. $response = $siteBuilder->enable($params['username'], $site, $params['serverusername'], $params['serverpassword']);
  827. if($response['status'] != '200') {
  828. return 'Error: ' . $response['response']['error'];
  829. }
  830. // update DB
  831. try {
  832. Capsule::table('sitePro_site')
  833. ->where('relid',$params['serviceid'])
  834. ->where('name',$site)
  835. ->update(array(
  836. 'enabled' => true,
  837. ));
  838. } catch (\Exception $e) {
  839. logModuleCall(
  840. 'siteBuilder',
  841. __FUNCTION__,
  842. $params,
  843. 'Error: could save site status in database',
  844. $e->getMessage()
  845. );
  846. return 'Error: could save site status in database';
  847. }
  848. return 'success';
  849. }
  850. function siteBuilder_disableSite($params) {
  851. if(!filter_var($_POST['s'], FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME)){
  852. return 'Error: invalid site name';
  853. }
  854. $site = $_POST['s'];
  855. $siteBuilder = new ApiClient(getSiteBuilderApiURL($params), $params['serveraccesshash']);
  856. // disable
  857. $response = $siteBuilder->disable($params['username'], $site, $params['serverusername'], $params['serverpassword']);
  858. logModuleCall(
  859. 'siteBuilder',
  860. __FUNCTION__,
  861. $params,
  862. 'Debug',
  863. $response
  864. );
  865. if($response['status'] != '200') {
  866. return 'Error: ' . $response['response']['error'];
  867. }
  868. // update DB
  869. try {
  870. Capsule::table('sitePro_site')
  871. ->where('relid',$params['serviceid'])
  872. ->where('name',$site)
  873. ->update(array(
  874. 'enabled' => false,
  875. ));
  876. } catch (\Exception $e) {
  877. logModuleCall(
  878. 'siteBuilder',
  879. __FUNCTION__,
  880. $params,
  881. 'Error: could save site status in database',
  882. $e->getMessage()
  883. );
  884. return 'Error: could save site status in database';
  885. }
  886. return 'success';
  887. }
  888. /**
  889. * Returns API Url .
  890. *
  891. * @param string $params common module parameters
  892. * @param string $user
  893. * @param string $params common module parameters
  894. *
  895. * @return string $apiUrl
  896. */
  897. function getSiteBuilderApiURL($params) {
  898. $httpPrefix = $params['serversecure'] ? 'https://' : 'http://';
  899. $serverPort = $params['serverport'] ? ':' . $params['serverport'] . '/' : '/';
  900. return $httpPrefix . $params['serverhostname'] . $serverPort;
  901. }
  902. function getSites($serviceID) {
  903. try {
  904. $sites = Capsule::table('sitePro_site')
  905. ->where('relid',$serviceID)
  906. ->value('name');
  907. } catch (\Exception $e) {
  908. logModuleCall(
  909. 'siteBuilder',
  910. __FUNCTION__,
  911. $params,
  912. 'Error: could fetch sites from database',
  913. $e->getMessage()
  914. );
  915. return 'Error: could fetch sites from database';
  916. }
  917. return $sites;
  918. }
  919. function getSitesEnabled($serviceID) {
  920. try {
  921. $sites = Capsule::table('sitePro_site')
  922. ->where('relid',$serviceID)
  923. ->where('enabled', 1)
  924. ->value('name');
  925. } catch (\Exception $e) {
  926. logModuleCall(
  927. 'siteBuilder',
  928. __FUNCTION__,
  929. $params,
  930. 'Error: could fetch sites from database',
  931. $e->getMessage()
  932. );
  933. return 'Error: could fetch sites from database';
  934. }
  935. return $sites;
  936. }
  937. function siteBuilderCreateTables() {
  938. // Create a new table.
  939. if (!Capsule::schema()->hasTable('sitePro_acc')) {
  940. try {
  941. Capsule::schema()->create(
  942. 'sitePro_acc',
  943. function ($table) { logModuleCall(
  944. 'siteBuilder',
  945. __FUNCTION__,
  946. $params,
  947. 'Debug',
  948. $site
  949. );
  950. /** @var \Illuminate\Database\Schema\Blueprint $table */
  951. $table->increments('id');
  952. $table->string('account');
  953. $table->integer('pid');
  954. $table->boolean('enabled');
  955. }
  956. );
  957. } catch (\Exception $e) {
  958. echo "Unable to create sitePro_acc: {$e->getMessage()}";
  959. }
  960. }
  961. if (!Capsule::schema()->hasTable('sitePro_site')) {
  962. try {
  963. Capsule::schema()->create(
  964. 'sitePro_site',
  965. function ($table) {
  966. /** @var \Illuminate\Database\Schema\Blueprint $table */
  967. $table->increments('id');
  968. $table->integer('relid');
  969. $table->string('name');
  970. $table->boolean('enabled');
  971. }
  972. );
  973. } catch (\Exception $e) {
  974. echo "Unable to create sitePro_site: {$e->getMessage()}";
  975. }
  976. }
  977. }