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