siteBuilder.php 31 KB

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