siteBuilder.php 29 KB

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