siteBuilder.php 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024
  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. $response = $siteBuilder->getQuota($params['username']);
  360. if($response['status'] != '200') {
  361. logModuleCall(
  362. 'siteBuilder',
  363. __FUNCTION__,
  364. $params,
  365. 'Error getting Quota',
  366. $response
  367. );
  368. }
  369. $clientInfo['quota'] = round($response['response']['quota'][0]['blocks']/1024);
  370. $clientInfo['limit'] = round($response['response']['quota'][0]['hard']/1024);
  371. return array(
  372. 'tabOverviewReplacementTemplate' => 'clientarea',
  373. 'vars' => $clientInfo,
  374. );
  375. }
  376. /**
  377. * Perform single sign-on for a siteBuilder account.
  378. *
  379. * When successful, returns a URL to which the user should be redirected.
  380. *
  381. * @param array $params common module parameters
  382. *
  383. * @see https://developers.whmcs.com/provisioning-modules/single-sign-on/
  384. *
  385. * @return array
  386. */
  387. function siteBuilder_ServiceSingleSignOn($params) {
  388. }
  389. /**
  390. * Upgrade or downgrade a siteBuilder account by package.
  391. *
  392. * Called to apply any change in product assignment or parameters. It
  393. * is called to provision upgrade or downgrade orders, as well as being
  394. * able to be invoked manually by an admin user.
  395. *
  396. * This same function is called for upgrades and downgrades of both
  397. * products and configurable options.
  398. *
  399. * @param array $params common module parameters
  400. *
  401. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  402. *
  403. * @return string "success" or an error message
  404. */
  405. function siteBuilder_ChangePackage($params) {
  406. return 'success';
  407. }
  408. /**
  409. * Usage Update
  410. *
  411. * Important: Runs daily per server not per product
  412. * Run Manually: /admin/reports.php?report=disk_usage_summary&action=updatestats
  413. * @param array $params common module parameters
  414. *
  415. * @see https://developers.whmcs.com/provisioning-modules/usage-update/
  416. */
  417. function siteBuilder_UsageUpdate($params) {
  418. $siteBuilder = new ApiClient(getSiteBuilderApiURL($params), $params['serveraccesshash']);
  419. $response = $siteBuilder->getStats();
  420. if($response['status'] != '200') {
  421. logActivity('ERROR: Unable to update sitebuilder server usage: ' . implode('#',[$response]));
  422. }
  423. $stats = $response['response']['quota'];
  424. foreach($stats as $stat){
  425. try {
  426. Capsule::table('tblhosting')
  427. ->where('server', $params['serverid'])
  428. ->where('username', $stat['user'])
  429. ->update([
  430. 'diskusage' => $stat['used']/1024,
  431. 'disklimit' => $stat['hard']/1024,
  432. 'lastupdate' => Capsule::raw('now()'),
  433. ]);
  434. } catch (\Exception $e) {
  435. logActivity('ERROR: Unable to update sitebuilder server usage: ' . $e->getMessage());
  436. }
  437. logModuleCall(
  438. 'siteBuilder',
  439. __FUNCTION__,
  440. $stat,
  441. 'debug',
  442. $params
  443. );
  444. }
  445. }
  446. /**
  447. * Additional actions a client user can invoke.
  448. *
  449. * Define additional actions a client user can perform for an instance of a
  450. * product/service.
  451. *
  452. * Any actions you define here will be automatically displayed in the available
  453. * list of actions within the client area.
  454. *
  455. * @return array
  456. */
  457. function siteBuilder_ClientAreaCustomButtonArray ($params) {
  458. return array(
  459. 'Neue Webseite' => 'newSite',
  460. );
  461. }
  462. /**
  463. * Additional actions a client user can invoke.
  464. *
  465. * Define additional actions a client user is allowed to perform for an instance of a
  466. * product/service.
  467. *
  468. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  469. *
  470. * @return array
  471. */
  472. function siteBuilder_ClientAreaAllowedFunctions() {
  473. return array(
  474. 'Add Site' => 'addSite',
  475. 'New Site' => 'newSite',
  476. 'Confirm Delete Site' => 'delSiteConfirm',
  477. 'Delete Site' => 'delSite',
  478. 'Edit Site' => 'editSite',
  479. 'Conform Revert Site' => 'revSiteConfirm',
  480. 'Revert Site' => 'revSite',
  481. 'Disable Site' => 'disableSite',
  482. 'Enable Site' => 'enableSite'
  483. );
  484. }
  485. /**
  486. * Opens a form to add a new domain.
  487. *
  488. * @param array $params common module parameters
  489. *
  490. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  491. *
  492. * @return array template information
  493. */
  494. function siteBuilder_newSite($params) {
  495. return array(
  496. 'breadcrumb' => array(
  497. 'clientarea.php?action=productdetails&id=' . $params['serviceid'] . '&modop=custom&a=newSite' => 'Neue Webseite',
  498. ),
  499. 'templatefile' => 'siteBuilder_new_site',
  500. );
  501. }
  502. /**
  503. * Adds a new domain to a siteBuilder account.
  504. *
  505. * @param array $params common module parameters
  506. *
  507. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  508. *
  509. * @return string "success" or an error message
  510. */
  511. function siteBuilder_addSite($params) {
  512. if(empty($_POST['d'])) {
  513. $site = $params['domain'];
  514. } else {
  515. if(!filter_var($_POST['d'], FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME)){
  516. return 'Error: invalid site name';
  517. }
  518. $site = $_POST['d'] . '.' . $params['domain'];
  519. }
  520. $siteBuilder = new ApiClient(getSiteBuilderApiURL($params), $params['serveraccesshash']);
  521. // init prod
  522. $response = $siteBuilder->init($params['username'], $site, $params['serverusername'], $params['serverpassword']);
  523. if($response['status'] != '200') {
  524. return 'Error: ' . $response['response']['error'];
  525. }
  526. // update DB
  527. try {
  528. Capsule::table('sitePro_site')
  529. ->insert(
  530. array(
  531. 'relid' => $params['serviceid'],
  532. 'name' => $site,
  533. 'enabled' => true,
  534. )
  535. );
  536. } catch (\Exception $e) {
  537. logModuleCall(
  538. 'siteBuilder',
  539. __FUNCTION__,
  540. $params,
  541. 'Error: could save site & serviceid in database',
  542. $e->getMessage()
  543. );
  544. return 'Error: could save site & serviceid in database';
  545. }
  546. return 'success';
  547. }
  548. function siteBuilder_editSite($params) {
  549. if(!filter_var($_POST['s'], FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME)){
  550. return 'Error: invalid site name';
  551. }
  552. $site = $_POST['s'];
  553. $api = new SiteProApiClient('https://builder.thurdata.ch/api/', 'apikey0', '993yVHwC05TLsx2JI2XFlAhkkPUxR6JbQUYbI.a5HiRtmNV9');
  554. // use this for enterprise licenses and change 'your-bulder-domain.com' to your builder domain
  555. //$api = new SiteProApiClient('http://your-bulder-domain.com/api/', 'your_api_username', 'your_api_password');
  556. try {
  557. // this call is used to open builder, so you need to set correct parameters to represent users website you want to open
  558. // this data usually comes from your user/hosting manager system
  559. $res = $api->remoteCall('requestLogin', array(
  560. 'type' => 'internal', // (required) 'internal'
  561. 'domain' => $site, // (required) domain of the user website you want to edit
  562. 'lang' => 'de', // (optional) 2-letter language code, set language code you whant builder to open in
  563. 'apiUrl' => getSiteBuilderApiURL($params) . 'deploy/' . $params['username'] . '/' . $site, // (required) API endpoint URL
  564. 'resellerClientAccountId' => $params['serviceid'], // (required) ID of website/user in your system
  565. 'username' => $params['serverusername'], // (optional) authorization username to be used with API endpoint
  566. 'password' => 'your-secure-password', // (optional) authorization password to be used with API endpoint
  567. ));
  568. if (!$res || !is_object($res)) {
  569. logModuleCall(
  570. 'siteBuilder',
  571. __FUNCTION__,
  572. $params,
  573. 'Error: Response format error',
  574. $res
  575. );
  576. return 'Error: Response format error';
  577. } else if (isset($res->url) && $res->url) {
  578. logModuleCall(
  579. 'siteBuilder',
  580. __FUNCTION__,
  581. $params,
  582. 'Debug',
  583. $res
  584. );
  585. // on success redirect to builder URL
  586. header('Location: '.$res->url, true);
  587. exit();
  588. } else {
  589. logModuleCall(
  590. 'siteBuilder',
  591. __FUNCTION__,
  592. $params,
  593. 'Error: Unknown error',
  594. $res
  595. );
  596. return 'Error: Unknown error';
  597. }
  598. } catch (\Exception $e) {
  599. logModuleCall(
  600. 'siteBuilder',
  601. __FUNCTION__,
  602. $params,
  603. 'Error: Request error',
  604. $e->getMessage()
  605. );
  606. return 'Error: Request error';
  607. }
  608. return 'success';
  609. }
  610. /**
  611. * Opens a form to delete a domain from a siteBuilder account.
  612. *
  613. * @param array $params common module parameters
  614. *
  615. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  616. *
  617. * @return array template information
  618. */
  619. function siteBuilder_delSiteConfirm($params) {
  620. return array(
  621. 'templatefile' => 'siteBuilder_del_site_confirm',
  622. 'vars' => array(
  623. 'delsite' => $_POST['s'],
  624. ),
  625. );
  626. }
  627. /**
  628. * Removes a domain from a siteBuilder account.
  629. *
  630. * @param array $params common module parameters
  631. *
  632. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  633. *
  634. * @return string "success" or an error message
  635. */
  636. function siteBuilder_delSite($params) {
  637. if(!filter_var($_POST['s'], FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME)){
  638. return 'Error: invalid domain name';
  639. }
  640. $site = $_POST['s'];
  641. $siteBuilder = new ApiClient(getSiteBuilderApiURL($params), $params['serveraccesshash']);
  642. // undeploy
  643. $response = $siteBuilder->undeploy($params['username'], $site, $params['serverusername'], $params['serverpassword']);
  644. if($response['status'] != '200') {
  645. return 'Error: ' . $response['response']['error'];
  646. }
  647. // remove builder session
  648. $api = new SiteProApiClient('https://builder.thurdata.ch/api/', 'apikey0', '993yVHwC05TLsx2JI2XFlAhkkPUxR6JbQUYbI.a5HiRtmNV9');
  649. // use this for enterprise licenses and change 'your-bulder-domain.com' to your builder domain
  650. //$api = new SiteProApiClient('http://your-bulder-domain.com/api/', 'your_api_username', 'your_api_password');
  651. try {
  652. // this call is used to open builder, so you need to set correct parameters to represent users website you want to open
  653. // this data usually comes from your user/hosting manager system
  654. $res = $api->remoteCall('requestLogin', array(
  655. 'type' => 'internal', // (required) 'internal'
  656. 'domain' => $site, // (required) domain of the user website you want to edit
  657. 'lang' => 'de', // (optional) 2-letter language code, set language code you whant builder to open in
  658. 'apiUrl' => getSiteBuilderApiURL($params) . 'deploy/' . $params['username'] . '/' . $site, // (required) API endpoint URL
  659. 'resellerClientAccountId' => $params['serviceid'], // (required) ID of website/user in your system
  660. 'username' => $params['serverusername'], // (optional) authorization username to be used with API endpoint
  661. 'password' => 'your-secure-password', // (optional) authorization password to be used with API endpoint
  662. ));
  663. if (!$res || !is_object($res)) {
  664. logModuleCall(
  665. 'siteBuilder',
  666. __FUNCTION__,
  667. $params,
  668. 'Error: Response format error',
  669. $res
  670. );
  671. return 'Error: Response format error';
  672. } else if (isset($res->url) && $res->url) {
  673. $result = $api->remoteCall('delete-site', array(
  674. 'domain' => $site
  675. ));
  676. if (!$result || !is_object($result)) {
  677. logModuleCall(
  678. 'siteBuilder',
  679. __FUNCTION__,
  680. $params,
  681. 'Error: Response format error',
  682. $result
  683. );
  684. return 'Error: Response format error';
  685. } else if (isset($result->ok) && $res->ok) {
  686. return 'success';
  687. }
  688. } else {
  689. logModuleCall(
  690. 'siteBuilder',
  691. __FUNCTION__,
  692. $params,
  693. 'Error: Unknown error',
  694. $res
  695. );
  696. return 'Error: Unknown error';
  697. }
  698. } catch (\Exception $e) {
  699. logModuleCall(
  700. 'siteBuilder',
  701. __FUNCTION__,
  702. $params,
  703. 'Error: Request error',
  704. $e->getMessage()
  705. );
  706. return 'Error: Request error';
  707. }
  708. // update DB
  709. try {
  710. Capsule::table('sitePro_site')
  711. ->where('name', $site)
  712. ->delete();
  713. } catch (\Exception $e) {
  714. logModuleCall(
  715. 'siteBuilder',
  716. __FUNCTION__,
  717. $params,
  718. 'Error: could remove site from database',
  719. $e->getMessage()
  720. );
  721. return 'Error: could remove site from database';
  722. }
  723. return 'success';
  724. }
  725. /**
  726. * Opens a form to delete a domain from a siteBuilder account.
  727. *
  728. * @param array $params common module parameters
  729. *
  730. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  731. *
  732. * @return array template information
  733. */
  734. function siteBuilder_revSiteConfirm($params) {
  735. return array(
  736. 'templatefile' => 'siteBuilder_rev_site_confirm',
  737. 'vars' => array(
  738. 'revSite' => $_POST['s'],
  739. ),
  740. );
  741. }
  742. /**
  743. * Revert all Changes of the development Site.
  744. *
  745. * @param array $params common module parameters
  746. *
  747. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  748. *
  749. * @return string "success" or an error message
  750. */
  751. function siteBuilder_revSite($params) {
  752. if(!filter_var($_POST['s'], FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME)){
  753. return 'Error: invalid site name';
  754. }
  755. $site = $_POST['s'];
  756. $siteBuilder = new ApiClient(getSiteBuilderApiURL($params), $params['serveraccesshash']);
  757. $response = $siteBuilder->revert($params['username'], $site, $params['serverusername'], $params['serverpassword']);
  758. if($response['status'] != '200') {
  759. return 'Error: ' . $response['response']['error'];
  760. }
  761. // remove builder session
  762. $api = new SiteProApiClient('https://builder.thurdata.ch/api/', 'apikey0', '993yVHwC05TLsx2JI2XFlAhkkPUxR6JbQUYbI.a5HiRtmNV9');
  763. // use this for enterprise licenses and change 'your-bulder-domain.com' to your builder domain
  764. //$api = new SiteProApiClient('http://your-bulder-domain.com/api/', 'your_api_username', 'your_api_password');
  765. try {
  766. // this call is used to open builder, so you need to set correct parameters to represent users website you want to open
  767. // this data usually comes from your user/hosting manager system
  768. $res = $api->remoteCall('requestLogin', array(
  769. 'type' => 'internal', // (required) 'internal'
  770. 'domain' => $site, // (required) domain of the user website you want to edit
  771. 'lang' => 'de', // (optional) 2-letter language code, set language code you whant builder to open in
  772. 'apiUrl' => getSiteBuilderApiURL($params) . 'deploy/' . $params['username'] . '/' . $site, // (required) API endpoint URL
  773. 'resellerClientAccountId' => $params['serviceid'], // (required) ID of website/user in your system
  774. 'username' => $params['serverusername'], // (optional) authorization username to be used with API endpoint
  775. 'password' => 'your-secure-password', // (optional) authorization password to be used with API endpoint
  776. ));
  777. if (!$res || !is_object($res)) {
  778. logModuleCall(
  779. 'siteBuilder',
  780. __FUNCTION__,
  781. $params,
  782. 'Error: Response format error',
  783. $res
  784. );
  785. return 'Error: Response format error';
  786. } else if (isset($res->url) && $res->url) {
  787. $result = $api->remoteCall('delete-site', array(
  788. 'domain' => $site
  789. ));
  790. if (!$result || !is_object($result)) {
  791. logModuleCall(
  792. 'siteBuilder',
  793. __FUNCTION__,
  794. $params,
  795. 'Error: Response format error',
  796. $result
  797. );
  798. return 'Error: Response format error';
  799. } else if (isset($result->ok) && $res->ok) {
  800. return 'success';
  801. }
  802. } else {
  803. logModuleCall(
  804. 'siteBuilder',
  805. __FUNCTION__,
  806. $params,
  807. 'Error: Unknown error',
  808. $res
  809. );
  810. return 'Error: Unknown error';
  811. }
  812. } catch (\Exception $e) {
  813. logModuleCall(
  814. 'siteBuilder',
  815. __FUNCTION__,
  816. $params,
  817. 'Error: Request error',
  818. $e->getMessage()
  819. );
  820. return 'Error: Request error';
  821. }
  822. return 'success';
  823. }
  824. function siteBuilder_enableSite($params) {
  825. if(!filter_var($_POST['s'], FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME)){
  826. return 'Error: invalid site name';
  827. }
  828. $site = $_POST['s'];
  829. $siteBuilder = new ApiClient(getSiteBuilderApiURL($params), $params['serveraccesshash']);
  830. // enable
  831. $response = $siteBuilder->enable($params['username'], $site, $params['serverusername'], $params['serverpassword']);
  832. if($response['status'] != '200') {
  833. return 'Error: ' . $response['response']['error'];
  834. }
  835. // update DB
  836. try {
  837. Capsule::table('sitePro_site')
  838. ->where('relid',$params['serviceid'])
  839. ->where('name',$site)
  840. ->update(array(
  841. 'enabled' => true,
  842. ));
  843. } catch (\Exception $e) {
  844. logModuleCall(
  845. 'siteBuilder',
  846. __FUNCTION__,
  847. $params,
  848. 'Error: could save site status in database',
  849. $e->getMessage()
  850. );
  851. return 'Error: could save site status in database';
  852. }
  853. return 'success';
  854. }
  855. function siteBuilder_disableSite($params) {
  856. if(!filter_var($_POST['s'], FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME)){
  857. return 'Error: invalid site name';
  858. }
  859. $site = $_POST['s'];
  860. $siteBuilder = new ApiClient(getSiteBuilderApiURL($params), $params['serveraccesshash']);
  861. // disable
  862. $response = $siteBuilder->disable($params['username'], $site, $params['serverusername'], $params['serverpassword']);
  863. logModuleCall(
  864. 'siteBuilder',
  865. __FUNCTION__,
  866. $params,
  867. 'Debug',
  868. $response
  869. );
  870. if($response['status'] != '200') {
  871. return 'Error: ' . $response['response']['error'];
  872. }
  873. // update DB
  874. try {
  875. Capsule::table('sitePro_site')
  876. ->where('relid',$params['serviceid'])
  877. ->where('name',$site)
  878. ->update(array(
  879. 'enabled' => false,
  880. ));
  881. } catch (\Exception $e) {
  882. logModuleCall(
  883. 'siteBuilder',
  884. __FUNCTION__,
  885. $params,
  886. 'Error: could save site status in database',
  887. $e->getMessage()
  888. );
  889. return 'Error: could save site status in database';
  890. }
  891. return 'success';
  892. }
  893. /**
  894. * Returns API Url .
  895. *
  896. * @param string $params common module parameters
  897. * @param string $user
  898. * @param string $params common module parameters
  899. *
  900. * @return string $apiUrl
  901. */
  902. function getSiteBuilderApiURL($params) {
  903. $httpPrefix = $params['serversecure'] ? 'https://' : 'http://';
  904. $serverPort = $params['serverport'] ? ':' . $params['serverport'] . '/' : '/';
  905. return $httpPrefix . $params['serverhostname'] . $serverPort;
  906. }
  907. function getSites($serviceID) {
  908. try {
  909. $sites = Capsule::table('sitePro_site')
  910. ->where('relid',$serviceID)
  911. ->value('name');
  912. } catch (\Exception $e) {
  913. logModuleCall(
  914. 'siteBuilder',
  915. __FUNCTION__,
  916. $params,
  917. 'Error: could fetch sites from database',
  918. $e->getMessage()
  919. );
  920. return 'Error: could fetch sites from database';
  921. }
  922. return $sites;
  923. }
  924. function getSitesEnabled($serviceID) {
  925. try {
  926. $sites = Capsule::table('sitePro_site')
  927. ->where('relid',$serviceID)
  928. ->where('enabled', 1)
  929. ->value('name');
  930. } catch (\Exception $e) {
  931. logModuleCall(
  932. 'siteBuilder',
  933. __FUNCTION__,
  934. $params,
  935. 'Error: could fetch sites from database',
  936. $e->getMessage()
  937. );
  938. return 'Error: could fetch sites from database';
  939. }
  940. return $sites;
  941. }
  942. function siteBuilderCreateTables() {
  943. // Create a new table.
  944. if (!Capsule::schema()->hasTable('sitePro_acc')) {
  945. try {
  946. Capsule::schema()->create(
  947. 'sitePro_acc',
  948. function ($table) { logModuleCall(
  949. 'siteBuilder',
  950. __FUNCTION__,
  951. $params,
  952. 'Debug',
  953. $site
  954. );
  955. /** @var \Illuminate\Database\Schema\Blueprint $table */
  956. $table->increments('id');
  957. $table->string('account');
  958. $table->integer('pid');
  959. $table->boolean('enabled');
  960. }
  961. );
  962. } catch (\Exception $e) {
  963. echo "Unable to create sitePro_acc: {$e->getMessage()}";
  964. }
  965. }
  966. if (!Capsule::schema()->hasTable('sitePro_site')) {
  967. try {
  968. Capsule::schema()->create(
  969. 'sitePro_site',
  970. function ($table) {
  971. /** @var \Illuminate\Database\Schema\Blueprint $table */
  972. $table->increments('id');
  973. $table->integer('relid');
  974. $table->string('name');
  975. $table->boolean('enabled');
  976. }
  977. );
  978. } catch (\Exception $e) {
  979. echo "Unable to create sitePro_site: {$e->getMessage()}";
  980. }
  981. }
  982. }