siteBuilder.php 29 KB

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