siteBuilder.php 31 KB

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