siteBuilder.php 34 KB

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