siteBuilder.php 29 KB

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