siteBuilder.php 30 KB

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