siteBuilder.php 31 KB

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