siteBuilder.php 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036
  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" => "https://builder.thurdata.ch/",
  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. ));
  579. if (!$res || !is_object($res)) {
  580. logModuleCall(
  581. 'siteBuilder',
  582. __FUNCTION__,
  583. $params,
  584. 'Error: Response format error',
  585. $res
  586. );
  587. return 'Error: Response format error';
  588. } else if (isset($res->url) && $res->url) {
  589. logModuleCall(
  590. 'siteBuilder',
  591. __FUNCTION__,
  592. $params,
  593. 'Debug',
  594. $res
  595. );
  596. // on success redirect to builder URL
  597. header('Location: '.$res->url, true);
  598. exit();
  599. } else {
  600. logModuleCall(
  601. 'siteBuilder',
  602. __FUNCTION__,
  603. $params,
  604. 'Error: Unknown error',
  605. $res
  606. );
  607. return 'Error: Unknown error';
  608. }
  609. } catch (\Exception $e) {
  610. logModuleCall(
  611. 'siteBuilder',
  612. __FUNCTION__,
  613. $params,
  614. 'Error: Request error',
  615. $e->getMessage()
  616. );
  617. return 'Error: Request error';
  618. }
  619. return 'success';
  620. }
  621. /**
  622. * Opens a form to delete a domain from a siteBuilder account.
  623. *
  624. * @param array $params common module parameters
  625. *
  626. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  627. *
  628. * @return array template information
  629. */
  630. function siteBuilder_delSiteConfirm($params) {
  631. return array(
  632. 'templatefile' => 'siteBuilder_del_site_confirm',
  633. 'vars' => array(
  634. 'delsite' => $_POST['s'],
  635. ),
  636. );
  637. }
  638. /**
  639. * Removes a domain from a siteBuilder account.
  640. *
  641. * @param array $params common module parameters
  642. *
  643. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  644. *
  645. * @return string "success" or an error message
  646. */
  647. function siteBuilder_delSite($params) {
  648. if(!filter_var($_POST['s'], FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME)){
  649. return 'Error: invalid domain name';
  650. }
  651. $site = $_POST['s'];
  652. $siteBuilder = new ApiClient(getSiteBuilderApiURL($params), $params['serveraccesshash']);
  653. // undeploy
  654. $response = $siteBuilder->undeploy($params['username'], $site, $params['serverusername'], $params['serverpassword']);
  655. if($response['status'] != '200') {
  656. return 'Error: ' . $response['response']['error'];
  657. }
  658. // remove builder session
  659. $api = new SiteProApiClient('https://builder.thurdata.ch/api/', 'apikey0', '993yVHwC05TLsx2JI2XFlAhkkPUxR6JbQUYbI.a5HiRtmNV9');
  660. // use this for enterprise licenses and change 'your-bulder-domain.com' to your builder domain
  661. //$api = new SiteProApiClient('http://your-bulder-domain.com/api/', 'your_api_username', 'your_api_password');
  662. try {
  663. // this call is used to open builder, so you need to set correct parameters to represent users website you want to open
  664. // this data usually comes from your user/hosting manager system
  665. $res = $api->remoteCall('requestLogin', array(
  666. 'type' => 'internal', // (required) 'internal'
  667. 'domain' => $site, // (required) domain of the user website you want to edit
  668. 'lang' => 'de', // (optional) 2-letter language code, set language code you whant builder to open in
  669. 'apiUrl' => getSiteBuilderApiURL($params) . 'deploy/' . $params['username'] . '/' . $site, // (required) API endpoint URL
  670. 'resellerClientAccountId' => $params['serviceid'], // (required) ID of website/user in your system
  671. 'username' => $params['serverusername'], // (optional) authorization username to be used with API endpoint
  672. 'password' => 'your-secure-password', // (optional) authorization password to be used with API endpoint
  673. ));
  674. if (!$res || !is_object($res)) {
  675. logModuleCall(
  676. 'siteBuilder',
  677. __FUNCTION__,
  678. $params,
  679. 'Error: Response format error',
  680. $res
  681. );
  682. return 'Error: Response format error';
  683. } else if (isset($res->url) && $res->url) {
  684. $result = $api->remoteCall('delete-site', array(
  685. 'domain' => $site
  686. ));
  687. if (!$result || !is_object($result)) {
  688. logModuleCall(
  689. 'siteBuilder',
  690. __FUNCTION__,
  691. $params,
  692. 'Error: Response format error',
  693. $result
  694. );
  695. return 'Error: Response format error';
  696. } else if (isset($result->ok) && $res->ok) {
  697. return 'success';
  698. }
  699. } else {
  700. logModuleCall(
  701. 'siteBuilder',
  702. __FUNCTION__,
  703. $params,
  704. 'Error: Unknown error',
  705. $res
  706. );
  707. return 'Error: Unknown error';
  708. }
  709. } catch (\Exception $e) {
  710. logModuleCall(
  711. 'siteBuilder',
  712. __FUNCTION__,
  713. $params,
  714. 'Error: Request error',
  715. $e->getMessage()
  716. );
  717. return 'Error: Request error';
  718. }
  719. // update DB
  720. try {
  721. Capsule::table('sitePro_site')
  722. ->where('name', $site)
  723. ->delete();
  724. } catch (\Exception $e) {
  725. logModuleCall(
  726. 'siteBuilder',
  727. __FUNCTION__,
  728. $params,
  729. 'Error: could remove site from database',
  730. $e->getMessage()
  731. );
  732. return 'Error: could remove site from database';
  733. }
  734. return 'success';
  735. }
  736. /**
  737. * Opens a form to delete a domain from a siteBuilder account.
  738. *
  739. * @param array $params common module parameters
  740. *
  741. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  742. *
  743. * @return array template information
  744. */
  745. function siteBuilder_revSiteConfirm($params) {
  746. return array(
  747. 'templatefile' => 'siteBuilder_rev_site_confirm',
  748. 'vars' => array(
  749. 'revSite' => $_POST['s'],
  750. ),
  751. );
  752. }
  753. /**
  754. * Revert all Changes of the development Site.
  755. *
  756. * @param array $params common module parameters
  757. *
  758. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  759. *
  760. * @return string "success" or an error message
  761. */
  762. function siteBuilder_revSite($params) {
  763. if(!filter_var($_POST['s'], FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME)){
  764. return 'Error: invalid site name';
  765. }
  766. $site = $_POST['s'];
  767. $siteBuilder = new ApiClient(getSiteBuilderApiURL($params), $params['serveraccesshash']);
  768. $response = $siteBuilder->revert($params['username'], $site, $params['serverusername'], $params['serverpassword']);
  769. if($response['status'] != '200') {
  770. return 'Error: ' . $response['response']['error'];
  771. }
  772. // remove builder session
  773. $api = new SiteProApiClient('https://builder.thurdata.ch/api/', 'apikey0', '993yVHwC05TLsx2JI2XFlAhkkPUxR6JbQUYbI.a5HiRtmNV9');
  774. // use this for enterprise licenses and change 'your-bulder-domain.com' to your builder domain
  775. //$api = new SiteProApiClient('http://your-bulder-domain.com/api/', 'your_api_username', 'your_api_password');
  776. try {
  777. // this call is used to open builder, so you need to set correct parameters to represent users website you want to open
  778. // this data usually comes from your user/hosting manager system
  779. $res = $api->remoteCall('requestLogin', array(
  780. 'type' => 'internal', // (required) 'internal'
  781. 'domain' => $site, // (required) domain of the user website you want to edit
  782. 'lang' => 'de', // (optional) 2-letter language code, set language code you whant builder to open in
  783. 'apiUrl' => getSiteBuilderApiURL($params) . 'deploy/' . $params['username'] . '/' . $site, // (required) API endpoint URL
  784. 'resellerClientAccountId' => $params['serviceid'], // (required) ID of website/user in your system
  785. 'username' => $params['serverusername'], // (optional) authorization username to be used with API endpoint
  786. 'password' => 'your-secure-password', // (optional) authorization password to be used with API endpoint
  787. ));
  788. if (!$res || !is_object($res)) {
  789. logModuleCall(
  790. 'siteBuilder',
  791. __FUNCTION__,
  792. $params,
  793. 'Error: Response format error',
  794. $res
  795. );
  796. return 'Error: Response format error';
  797. } else if (isset($res->url) && $res->url) {
  798. $result = $api->remoteCall('delete-site', array(
  799. 'domain' => $site
  800. ));
  801. if (!$result || !is_object($result)) {
  802. logModuleCall(
  803. 'siteBuilder',
  804. __FUNCTION__,
  805. $params,
  806. 'Error: Response format error',
  807. $result
  808. );
  809. return 'Error: Response format error';
  810. } else if (isset($result->ok) && $res->ok) {
  811. return 'success';
  812. }
  813. } else {
  814. logModuleCall(
  815. 'siteBuilder',
  816. __FUNCTION__,
  817. $params,
  818. 'Error: Unknown error',
  819. $res
  820. );
  821. return 'Error: Unknown error';
  822. }
  823. } catch (\Exception $e) {
  824. logModuleCall(
  825. 'siteBuilder',
  826. __FUNCTION__,
  827. $params,
  828. 'Error: Request error',
  829. $e->getMessage()
  830. );
  831. return 'Error: Request error';
  832. }
  833. return 'success';
  834. }
  835. function siteBuilder_enableSite($params) {
  836. if(!filter_var($_POST['s'], FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME)){
  837. return 'Error: invalid site name';
  838. }
  839. $site = $_POST['s'];
  840. $siteBuilder = new ApiClient(getSiteBuilderApiURL($params), $params['serveraccesshash']);
  841. // enable
  842. $response = $siteBuilder->enable($params['username'], $site, $params['serverusername'], $params['serverpassword']);
  843. if($response['status'] != '200') {
  844. return 'Error: ' . $response['response']['error'];
  845. }
  846. // update DB
  847. try {
  848. Capsule::table('sitePro_site')
  849. ->where('relid',$params['serviceid'])
  850. ->where('name',$site)
  851. ->update(array(
  852. 'enabled' => true,
  853. ));
  854. } catch (\Exception $e) {
  855. logModuleCall(
  856. 'siteBuilder',
  857. __FUNCTION__,
  858. $params,
  859. 'Error: could save site status in database',
  860. $e->getMessage()
  861. );
  862. return 'Error: could save site status in database';
  863. }
  864. return 'success';
  865. }
  866. function siteBuilder_disableSite($params) {
  867. if(!filter_var($_POST['s'], FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME)){
  868. return 'Error: invalid site name';
  869. }
  870. $site = $_POST['s'];
  871. $siteBuilder = new ApiClient(getSiteBuilderApiURL($params), $params['serveraccesshash']);
  872. // disable
  873. $response = $siteBuilder->disable($params['username'], $site, $params['serverusername'], $params['serverpassword']);
  874. logModuleCall(
  875. 'siteBuilder',
  876. __FUNCTION__,
  877. $params,
  878. 'Debug',
  879. $response
  880. );
  881. if($response['status'] != '200') {
  882. return 'Error: ' . $response['response']['error'];
  883. }
  884. // update DB
  885. try {
  886. Capsule::table('sitePro_site')
  887. ->where('relid',$params['serviceid'])
  888. ->where('name',$site)
  889. ->update(array(
  890. 'enabled' => false,
  891. ));
  892. } catch (\Exception $e) {
  893. logModuleCall(
  894. 'siteBuilder',
  895. __FUNCTION__,
  896. $params,
  897. 'Error: could save site status in database',
  898. $e->getMessage()
  899. );
  900. return 'Error: could save site status in database';
  901. }
  902. return 'success';
  903. }
  904. /**
  905. * Returns API Url .
  906. *
  907. * @param string $params common module parameters
  908. * @param string $user
  909. * @param string $params common module parameters
  910. *
  911. * @return string $apiUrl
  912. */
  913. function getSiteBuilderApiURL($params) {
  914. $httpPrefix = $params['serversecure'] ? 'https://' : 'http://';
  915. $serverPort = $params['serverport'] ? ':' . $params['serverport'] . '/' : '/';
  916. return $httpPrefix . $params['serverhostname'] . $serverPort;
  917. }
  918. function getSites($serviceID) {
  919. try {
  920. $sites = Capsule::table('sitePro_site')
  921. ->where('relid',$serviceID)
  922. ->value('name');
  923. } catch (\Exception $e) {
  924. logModuleCall(
  925. 'siteBuilder',
  926. __FUNCTION__,
  927. $params,
  928. 'Error: could fetch sites from database',
  929. $e->getMessage()
  930. );
  931. return 'Error: could fetch sites from database';
  932. }
  933. return $sites;
  934. }
  935. function getSitesEnabled($serviceID) {
  936. try {
  937. $sites = Capsule::table('sitePro_site')
  938. ->where('relid',$serviceID)
  939. ->where('enabled', 1)
  940. ->value('name');
  941. } catch (\Exception $e) {
  942. logModuleCall(
  943. 'siteBuilder',
  944. __FUNCTION__,
  945. $params,
  946. 'Error: could fetch sites from database',
  947. $e->getMessage()
  948. );
  949. return 'Error: could fetch sites from database';
  950. }
  951. return $sites;
  952. }
  953. function siteBuilderCreateTables() {
  954. // Create a new table.
  955. if (!Capsule::schema()->hasTable('sitePro_acc')) {
  956. try {
  957. Capsule::schema()->create(
  958. 'sitePro_acc',
  959. function ($table) { logModuleCall(
  960. 'siteBuilder',
  961. __FUNCTION__,
  962. $params,
  963. 'Debug',
  964. $site
  965. );
  966. /** @var \Illuminate\Database\Schema\Blueprint $table */
  967. $table->increments('id');
  968. $table->string('account');
  969. $table->integer('pid');
  970. $table->boolean('enabled');
  971. }
  972. );
  973. } catch (\Exception $e) {
  974. echo "Unable to create sitePro_acc: {$e->getMessage()}";
  975. }
  976. }
  977. if (!Capsule::schema()->hasTable('sitePro_site')) {
  978. try {
  979. Capsule::schema()->create(
  980. 'sitePro_site',
  981. function ($table) {
  982. /** @var \Illuminate\Database\Schema\Blueprint $table */
  983. $table->increments('id');
  984. $table->integer('relid');
  985. $table->string('name');
  986. $table->boolean('enabled');
  987. }
  988. );
  989. } catch (\Exception $e) {
  990. echo "Unable to create sitePro_site: {$e->getMessage()}";
  991. }
  992. }
  993. }