siteBuilder.php 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159
  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($status['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. logModuleCall(
  309. 'siteBuilder',
  310. __FUNCTION__,
  311. $params,
  312. 'Debug',
  313. $response
  314. );
  315. if($response['status'] != '200') {
  316. return 'Error: ' . $response['response']['error'];
  317. }
  318. }
  319. }
  320. return 'success';
  321. }
  322. /**
  323. * Set a siteBuilder account to status active and enable active sites
  324. *
  325. * Called when an un-suspension is requested. This is invoked
  326. * automatically upon payment of an overdue invoice for a product, or
  327. * can be called manually by admin user.
  328. *
  329. * @param array $params common module parameters
  330. *
  331. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  332. *
  333. * @return string 'success' or an error message
  334. */
  335. function siteBuilder_UnsuspendAccount($params) {
  336. $siteBuilder = new ApiClient(getSiteBuilderApiURL($params), $params['serveraccesshash']);
  337. // enable deafult site
  338. $response = $siteBuilder->enable($params['username'], $params['domain'], $params['serverusername'], $params['serverpassword']);
  339. if($response['status'] != '200') {
  340. return 'Error: ' . $response['response']['error'];
  341. }
  342. // set account to enabled in database
  343. try {
  344. Capsule::table('sitePro_acc')
  345. ->where('account',$params['username'])
  346. ->update(array(
  347. 'enabled' => true,
  348. ));
  349. } catch (\Exception $e) {
  350. logModuleCall(
  351. 'siteBuilder',
  352. __FUNCTION__,
  353. $params,
  354. 'Error: could update account in database',
  355. $e->getMessage()
  356. );
  357. return 'Error: could update account in database';
  358. }
  359. // enable active sites
  360. $sites = getSitesEnabled($params['serviceid']);
  361. logModuleCall(
  362. 'siteBuilder',
  363. __FUNCTION__,
  364. $params,
  365. 'Debug',
  366. $site
  367. );
  368. if(!empty($sites)) {
  369. foreach($sites as $site) {
  370. $response = $siteBuilder->enable($params['username'], $site, $params['serverusername'], $params['serverpassword']);
  371. if($response['status'] != '200') {
  372. return 'Error: ' . $response['response']['error'];
  373. }
  374. }
  375. }
  376. return 'success';
  377. }
  378. /**
  379. * Client area output logic handling.
  380. *
  381. * This function is used to define module specific client area output. It should
  382. * return an array consisting of a template file and optional additional
  383. * template variables to make available to that template.
  384. *
  385. * @param array $params common module parameters
  386. *
  387. * @see https://developers.whmcs.com/provisioning-modules/client-area-output/
  388. *
  389. * @return array
  390. */
  391. function siteBuilder_ClientArea($params) {
  392. $siteBuilder = new ApiClient(getSiteBuilderApiURL($params), $params['serveraccesshash']);
  393. $clientInfo = array('moduleclientarea' => '1');
  394. $clientInfo['domain'] = $params['domain'];
  395. $accEnabled = Capsule::table('sitePro_acc')
  396. ->where('pid', $params['serviceid'])
  397. ->value('enabled');
  398. $clientInfo['account'] = ['enabled' => $accEnabled];
  399. $clientInfo['sites'] = [];
  400. $sites = getSites($params['serviceid']);
  401. foreach($sites as $site){
  402. $response = $siteBuilder->getSSLDays($params['username'], $site);
  403. if($response['status'] == '200') {
  404. $sslSite = $response['response']['ssl_remaining'];
  405. }
  406. $response = $siteBuilder->isenabled($params['username'], $site);
  407. if($response['status'] == '200') {
  408. $enabled = $response['isenabled'];
  409. }
  410. array_push($clientInfo['sites'],['name' => $site->name, 'sslSite' => $sslSite, 'enabled' => $enabled]);
  411. }
  412. $response = $siteBuilder->getQuota($params['username']);
  413. if($response['status'] != '200') {
  414. logModuleCall(
  415. 'siteBuilder',
  416. __FUNCTION__,
  417. $params,
  418. 'Error getting Quota',
  419. $response
  420. );
  421. }
  422. $clientInfo['quota'] = round($response['response']['quota'][0]['blocks']/1024);
  423. $clientInfo['limit'] = round($response['response']['quota'][0]['hard']/1024);
  424. return array(
  425. 'tabOverviewReplacementTemplate' => 'clientarea',
  426. 'vars' => $clientInfo,
  427. );
  428. }
  429. /**
  430. * Upgrade or downgrade a siteBuilder account by package.
  431. *
  432. * Called to apply any change in product assignment or parameters. It
  433. * is called to provision upgrade or downgrade orders, as well as being
  434. * able to be invoked manually by an admin user.
  435. *
  436. * This same function is called for upgrades and downgrades of both
  437. * products and configurable options.
  438. *
  439. * @param array $params common module parameters
  440. *
  441. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  442. *
  443. * @return string "success" or an error message
  444. */
  445. function siteBuilder_ChangePackage($params) {
  446. $siteBuilder = new ApiClient(getSiteBuilderApiURL($params), $params['serveraccesshash']);
  447. $response = $siteBuilder->setQuota($params['username'], $params['configoption3'], $params['serverusername'], $params['serverpassword']);
  448. if($response['status'] != '200') {
  449. return 'Error: ' . $response['response']['error'];
  450. }
  451. return 'success';
  452. }
  453. /**
  454. * Usage Update
  455. *
  456. * Important: Runs daily per server not per product
  457. * Run Manually: /admin/reports.php?report=disk_usage_summary&action=updatestats
  458. * @param array $params common module parameters
  459. *
  460. * @see https://developers.whmcs.com/provisioning-modules/usage-update/
  461. */
  462. function siteBuilder_UsageUpdate($params) {
  463. $siteBuilder = new ApiClient(getSiteBuilderApiURL($params), $params['serveraccesshash']);
  464. $response = $siteBuilder->getStats();
  465. if($response['status'] != '200') {
  466. logActivity('ERROR: Unable to update sitebuilder server usage: ' . implode('#',[$response]));
  467. }
  468. $stats = $response['response']['quota'];
  469. foreach($stats as $stat){
  470. try {
  471. Capsule::table('tblhosting')
  472. ->where('server', $params['serverid'])
  473. ->where('username', $stat['user'])
  474. ->update([
  475. 'diskusage' => $stat['used']/1024,
  476. 'disklimit' => $stat['hard']/1024,
  477. 'lastupdate' => Capsule::raw('now()'),
  478. ]);
  479. } catch (\Exception $e) {
  480. logActivity('ERROR: Unable to update sitebuilder server usage: ' . $e->getMessage());
  481. }
  482. logModuleCall(
  483. 'siteBuilder',
  484. __FUNCTION__,
  485. $stat,
  486. 'debug',
  487. $params
  488. );
  489. }
  490. }
  491. /**
  492. * Additional actions a client user can invoke.
  493. *
  494. * Define additional actions a client user can perform for an instance of a
  495. * product/service.
  496. *
  497. * Any actions you define here will be automatically displayed in the available
  498. * list of actions within the client area.
  499. *
  500. * @return array
  501. */
  502. function siteBuilder_ClientAreaCustomButtonArray ($params) {
  503. return array(
  504. 'Neue Webseite' => 'newSite',
  505. );
  506. }
  507. /**
  508. * Additional actions a client user can invoke.
  509. *
  510. * Define additional actions a client user is allowed to perform for an instance of a
  511. * product/service.
  512. *
  513. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  514. *
  515. * @return array
  516. */
  517. function siteBuilder_ClientAreaAllowedFunctions() {
  518. return array(
  519. 'Add Site' => 'addSite',
  520. 'New Site' => 'newSite',
  521. 'Confirm Delete Site' => 'delSiteConfirm',
  522. 'Delete Site' => 'delSite',
  523. 'Edit Site' => 'editSite',
  524. 'Conform Revert Site' => 'revSiteConfirm',
  525. 'Revert Site' => 'revSite',
  526. 'Disable Site' => 'disableSite',
  527. 'Enable Site' => 'enableSite'
  528. );
  529. }
  530. /**
  531. * Opens a form to add a new domain.
  532. *
  533. * @param array $params common module parameters
  534. *
  535. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  536. *
  537. * @return array template information
  538. */
  539. function siteBuilder_newSite($params) {
  540. return array(
  541. 'breadcrumb' => array(
  542. 'clientarea.php?action=productdetails&id=' . $params['serviceid'] . '&modop=custom&a=newSite' => 'Neue Webseite',
  543. ),
  544. 'templatefile' => 'siteBuilder_new_site',
  545. );
  546. }
  547. /**
  548. * Adds a new domain to a siteBuilder account.
  549. *
  550. * @param array $params common module parameters
  551. *
  552. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  553. *
  554. * @return string "success" or an error message
  555. */
  556. function siteBuilder_addSite($params) {
  557. if(empty($_POST['d'])) {
  558. $site = $params['domain'];
  559. } else {
  560. if(!filter_var($_POST['d'], FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME)){
  561. return 'Error: invalid site name';
  562. }
  563. $site = $_POST['d'] . '.' . $params['domain'];
  564. }
  565. // set DNS
  566. /* disabled on dev, has to be already set in test env
  567. $response = siteBuildersetDNS($params, $site);
  568. if($response != 'success') {
  569. return $response;
  570. }
  571. */
  572. $siteBuilder = new ApiClient(getSiteBuilderApiURL($params), $params['serveraccesshash']);
  573. // init prod
  574. $response = $siteBuilder->init($params['username'], $site, $params['serverusername'], $params['serverpassword']);
  575. if($response['status'] != '200') {
  576. return 'Error: ' . $response['response']['error'];
  577. }
  578. // update DB
  579. try {
  580. Capsule::table('sitePro_site')
  581. ->insert(
  582. array(
  583. 'relid' => $params['serviceid'],
  584. 'name' => $site,
  585. 'enabled' => true,
  586. )
  587. );
  588. } catch (\Exception $e) {
  589. logModuleCall(
  590. 'siteBuilder',
  591. __FUNCTION__,
  592. $params,
  593. 'Error: could save site & serviceid in database',
  594. $e->getMessage()
  595. );
  596. return 'Error: could save site & serviceid in database';
  597. }
  598. return 'success';
  599. }
  600. function siteBuilder_editSite($params) {
  601. if(!filter_var($_POST['s'], FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME)){
  602. return 'Error: invalid site name';
  603. }
  604. $site = $_POST['s'];
  605. $api = new SiteProApiClient('https://builder.thurdata.ch/api/', 'apikey0', '993yVHwC05TLsx2JI2XFlAhkkPUxR6JbQUYbI.a5HiRtmNV9');
  606. // use this for enterprise licenses and change 'your-bulder-domain.com' to your builder domain
  607. //$api = new SiteProApiClient('http://your-bulder-domain.com/api/', 'your_api_username', 'your_api_password');
  608. try {
  609. // this call is used to open builder, so you need to set correct parameters to represent users website you want to open
  610. // this data usually comes from your user/hosting manager system
  611. $res = $api->remoteCall('requestLogin', array(
  612. 'type' => 'internal', // (required) 'internal'
  613. 'domain' => $site, // (required) domain of the user website you want to edit
  614. 'lang' => 'de', // (optional) 2-letter language code, set language code you whant builder to open in
  615. 'apiUrl' => getSiteBuilderApiURL($params) . 'deploy/' . $params['username'] . '/' . $site, // (required) API endpoint URL
  616. 'resellerClientAccountId' => $params['serviceid'], // (required) ID of website/user in your system
  617. 'username' => $params['serverusername'], // (optional) authorization username to be used with API endpoint
  618. 'password' => 'your-secure-password', // (optional) authorization password to be used with API endpoint
  619. 'hostingPlan' => $params['configoption2'],
  620. ));
  621. if (!$res || !is_object($res)) {
  622. logModuleCall(
  623. 'siteBuilder',
  624. __FUNCTION__,
  625. $params,
  626. 'Error: Response format error',
  627. $res
  628. );
  629. return 'Error: Response format error';
  630. } else if (isset($res->url) && $res->url) {
  631. logModuleCall(
  632. 'siteBuilder',
  633. __FUNCTION__,
  634. $params,
  635. 'Debug',
  636. $res
  637. );
  638. // on success redirect to builder URL
  639. header('Location: '.$res->url, true);
  640. exit();
  641. } else {
  642. logModuleCall(
  643. 'siteBuilder',
  644. __FUNCTION__,
  645. $params,
  646. 'Error: Unknown error',
  647. $res
  648. );
  649. return 'Error: Unknown error';
  650. }
  651. } catch (\Exception $e) {
  652. logModuleCall(
  653. 'siteBuilder',
  654. __FUNCTION__,
  655. $params,
  656. 'Error: Request error',
  657. $e->getMessage()
  658. );
  659. return 'Error: Request error';
  660. }
  661. return 'success';
  662. }
  663. /**
  664. * Opens a form to delete a domain from a siteBuilder account.
  665. *
  666. * @param array $params common module parameters
  667. *
  668. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  669. *
  670. * @return array template information
  671. */
  672. function siteBuilder_delSiteConfirm($params) {
  673. return array(
  674. 'templatefile' => 'siteBuilder_del_site_confirm',
  675. 'vars' => array(
  676. 'delsite' => $_POST['s'],
  677. ),
  678. );
  679. }
  680. /**
  681. * Removes a domain from a siteBuilder account.
  682. *
  683. * @param array $params common module parameters
  684. *
  685. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  686. *
  687. * @return string "success" or an error message
  688. */
  689. function siteBuilder_delSite($params) {
  690. if(!filter_var($_POST['s'], FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME)){
  691. return 'Error: invalid domain name';
  692. }
  693. $site = $_POST['s'];
  694. $siteBuilder = new ApiClient(getSiteBuilderApiURL($params), $params['serveraccesshash']);
  695. // undeploy
  696. $response = $siteBuilder->undeploy($params['username'], $site, $params['serverusername'], $params['serverpassword']);
  697. if($response['status'] != '200') {
  698. return 'Error: ' . $response['response']['error'];
  699. }
  700. // remove builder session
  701. $api = new SiteProApiClient('https://builder.thurdata.ch/api/', 'apikey0', '993yVHwC05TLsx2JI2XFlAhkkPUxR6JbQUYbI.a5HiRtmNV9');
  702. // use this for enterprise licenses and change 'your-bulder-domain.com' to your builder domain
  703. //$api = new SiteProApiClient('http://your-bulder-domain.com/api/', 'your_api_username', 'your_api_password');
  704. try {
  705. // this call is used to open builder, so you need to set correct parameters to represent users website you want to open
  706. // this data usually comes from your user/hosting manager system
  707. $res = $api->remoteCall('requestLogin', array(
  708. 'type' => 'internal', // (required) 'internal'
  709. 'domain' => $site, // (required) domain of the user website you want to edit
  710. 'lang' => 'de', // (optional) 2-letter language code, set language code you whant builder to open in
  711. 'apiUrl' => getSiteBuilderApiURL($params) . 'deploy/' . $params['username'] . '/' . $site, // (required) API endpoint URL
  712. 'resellerClientAccountId' => $params['serviceid'], // (required) ID of website/user in your system
  713. 'username' => $params['serverusername'], // (optional) authorization username to be used with API endpoint
  714. 'password' => 'your-secure-password', // (optional) authorization password to be used with API endpoint
  715. ));
  716. if (!$res || !is_object($res)) {
  717. logModuleCall(
  718. 'siteBuilder',
  719. __FUNCTION__,
  720. $params,
  721. 'Error: Response format error',
  722. $res
  723. );
  724. return 'Error: Response format error';
  725. } else if (isset($res->url) && $res->url) {
  726. $result = $api->remoteCall('delete-site', array(
  727. 'domain' => $site
  728. ));
  729. if (!$result || !is_object($result)) {
  730. logModuleCall(
  731. 'siteBuilder',
  732. __FUNCTION__,
  733. $params,
  734. 'Error: Response format error',
  735. $result
  736. );
  737. return 'Error: Response format error';
  738. } else if (isset($result->ok) && $res->ok) {
  739. return 'success';
  740. }
  741. } else {
  742. logModuleCall(
  743. 'siteBuilder',
  744. __FUNCTION__,
  745. $params,
  746. 'Error: Unknown error',
  747. $res
  748. );
  749. return 'Error: Unknown error';
  750. }
  751. } catch (\Exception $e) {
  752. logModuleCall(
  753. 'siteBuilder',
  754. __FUNCTION__,
  755. $params,
  756. 'Error: Request error',
  757. $e->getMessage()
  758. );
  759. return 'Error: Request error';
  760. }
  761. // update DB
  762. try {
  763. Capsule::table('sitePro_site')
  764. ->where('name', $site)
  765. ->delete();
  766. } catch (\Exception $e) {
  767. logModuleCall(
  768. 'siteBuilder',
  769. __FUNCTION__,
  770. $params,
  771. 'Error: could remove site from database',
  772. $e->getMessage()
  773. );
  774. return 'Error: could remove site from database';
  775. }
  776. // unset DNS
  777. /* disabled on dev, has to be already set in test env
  778. $response = siteBuilderunsetDNS($params, $site);
  779. if($response != 'success') {
  780. return $response;
  781. }
  782. */
  783. return 'success';
  784. }
  785. /**
  786. * Opens a form to delete a domain from a siteBuilder account.
  787. *
  788. * @param array $params common module parameters
  789. *
  790. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  791. *
  792. * @return array template information
  793. */
  794. function siteBuilder_revSiteConfirm($params) {
  795. return array(
  796. 'templatefile' => 'siteBuilder_rev_site_confirm',
  797. 'vars' => array(
  798. 'revSite' => $_POST['s'],
  799. ),
  800. );
  801. }
  802. /**
  803. * Revert all Changes of the development Site.
  804. *
  805. * @param array $params common module parameters
  806. *
  807. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  808. *
  809. * @return string "success" or an error message
  810. */
  811. function siteBuilder_revSite($params) {
  812. if(!filter_var($_POST['s'], FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME)){
  813. return 'Error: invalid site name';
  814. }
  815. $site = $_POST['s'];
  816. $siteBuilder = new ApiClient(getSiteBuilderApiURL($params), $params['serveraccesshash']);
  817. $response = $siteBuilder->revert($params['username'], $site, $params['serverusername'], $params['serverpassword']);
  818. if($response['status'] != '200') {
  819. return 'Error: ' . $response['response']['error'];
  820. }
  821. // remove builder session
  822. $api = new SiteProApiClient('https://builder.thurdata.ch/api/', 'apikey0', '993yVHwC05TLsx2JI2XFlAhkkPUxR6JbQUYbI.a5HiRtmNV9');
  823. // use this for enterprise licenses and change 'your-bulder-domain.com' to your builder domain
  824. //$api = new SiteProApiClient('http://your-bulder-domain.com/api/', 'your_api_username', 'your_api_password');
  825. try {
  826. // this call is used to open builder, so you need to set correct parameters to represent users website you want to open
  827. // this data usually comes from your user/hosting manager system
  828. $res = $api->remoteCall('requestLogin', array(
  829. 'type' => 'internal', // (required) 'internal'
  830. 'domain' => $site, // (required) domain of the user website you want to edit
  831. 'lang' => 'de', // (optional) 2-letter language code, set language code you whant builder to open in
  832. 'apiUrl' => getSiteBuilderApiURL($params) . 'deploy/' . $params['username'] . '/' . $site, // (required) API endpoint URL
  833. 'resellerClientAccountId' => $params['serviceid'], // (required) ID of website/user in your system
  834. 'username' => $params['serverusername'], // (optional) authorization username to be used with API endpoint
  835. 'password' => 'your-secure-password', // (optional) authorization password to be used with API endpoint
  836. ));
  837. if (!$res || !is_object($res)) {
  838. logModuleCall(
  839. 'siteBuilder',
  840. __FUNCTION__,
  841. $params,
  842. 'Error: Response format error',
  843. $res
  844. );
  845. return 'Error: Response format error';
  846. } else if (isset($res->url) && $res->url) {
  847. $result = $api->remoteCall('delete-site', array(
  848. 'domain' => $site
  849. ));
  850. if (!$result || !is_object($result)) {
  851. logModuleCall(
  852. 'siteBuilder',
  853. __FUNCTION__,
  854. $params,
  855. 'Error: Response format error',
  856. $result
  857. );
  858. return 'Error: Response format error';
  859. } else if (isset($result->ok) && $res->ok) {
  860. return 'success';
  861. }
  862. } else {
  863. logModuleCall(
  864. 'siteBuilder',
  865. __FUNCTION__,
  866. $params,
  867. 'Error: Unknown error',
  868. $res
  869. );
  870. return 'Error: Unknown error';
  871. }
  872. } catch (\Exception $e) {
  873. logModuleCall(
  874. 'siteBuilder',
  875. __FUNCTION__,
  876. $params,
  877. 'Error: Request error',
  878. $e->getMessage()
  879. );
  880. return 'Error: Request error';
  881. }
  882. return 'success';
  883. }
  884. function siteBuilder_enableSite($params) {
  885. if(!filter_var($_POST['s'], FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME)){
  886. return 'Error: invalid site name';
  887. }
  888. $site = $_POST['s'];
  889. $siteBuilder = new ApiClient(getSiteBuilderApiURL($params), $params['serveraccesshash']);
  890. // enable
  891. $response = $siteBuilder->enable($params['username'], $site, $params['serverusername'], $params['serverpassword']);
  892. if($response['status'] != '200') {
  893. return 'Error: ' . $response['response']['error'];
  894. }
  895. // update DB
  896. try {
  897. Capsule::table('sitePro_site')
  898. ->where('relid',$params['serviceid'])
  899. ->where('name',$site)
  900. ->update(array(
  901. 'enabled' => true,
  902. ));
  903. } catch (\Exception $e) {
  904. logModuleCall(
  905. 'siteBuilder',
  906. __FUNCTION__,
  907. $params,
  908. 'Error: could save site status in database',
  909. $e->getMessage()
  910. );
  911. return 'Error: could save site status in database';
  912. }
  913. return 'success';
  914. }
  915. function siteBuilder_disableSite($params) {
  916. if(!filter_var($_POST['s'], FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME)){
  917. return 'Error: invalid site name';
  918. }
  919. $site = $_POST['s'];
  920. $siteBuilder = new ApiClient(getSiteBuilderApiURL($params), $params['serveraccesshash']);
  921. // disable
  922. $response = $siteBuilder->disable($params['username'], $site, $params['serverusername'], $params['serverpassword']);
  923. logModuleCall(
  924. 'siteBuilder',
  925. __FUNCTION__,
  926. $params,
  927. 'Debug',
  928. $response
  929. );
  930. if($response['status'] != '200') {
  931. return 'Error: ' . $response['response']['error'];
  932. }
  933. // update DB
  934. try {
  935. Capsule::table('sitePro_site')
  936. ->where('relid',$params['serviceid'])
  937. ->where('name',$site)
  938. ->update(array(
  939. 'enabled' => false,
  940. ));
  941. } catch (\Exception $e) {
  942. logModuleCall(
  943. 'siteBuilder',
  944. __FUNCTION__,
  945. $params,
  946. 'Error: could save site status in database',
  947. $e->getMessage()
  948. );
  949. return 'Error: could save site status in database';
  950. }
  951. return 'success';
  952. }
  953. // Helpers
  954. /**
  955. * Update a DNS zone for a domain setting a new record for a domain or subdomain of a CWP7 account.
  956. *
  957. * @param array $params common module parameters
  958. *
  959. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  960. *
  961. * @return string "success" or an error message
  962. */
  963. function siteBuildersetDNS($params, $site) {
  964. $siteName = $site . '.';
  965. $zoneRecords = array();
  966. $domainRecord = array(
  967. 'line' => $siteName.'|A|0',
  968. 'name' => $siteName,
  969. 'type' => 'A',
  970. 'class' => 'IN',
  971. 'data' => array(
  972. 'address' => $params['serverip'],
  973. ),
  974. );
  975. array_push($zoneRecords, $domainRecord);
  976. $zoneIDcollection = Capsule::table('dns_manager2_zone')
  977. ->select('id')
  978. ->where('name', '=', $params['domain'])
  979. ->where('clientid', '=', $params['userid'])
  980. ->get();
  981. $zoneIDobj = $zoneIDcollection[0];
  982. $zoneID = $zoneIDobj->{'id'};
  983. if(!isset($zoneID)) {
  984. return 'Error: Zone for domain ' . $params['domain'] . ' or not owned by client';
  985. }
  986. $dnsZone = localAPI('dnsmanager', array( 'dnsaction' => 'getZone', 'zone_id' => $zoneID));
  987. foreach($dnsZone['data']->records as $record) {
  988. if(($record->name != $siteName) || ($record->type != 'A' && $record->type != 'CNAME')) {
  989. array_push($zoneRecords, $record);
  990. };
  991. }
  992. $result = localAPI('dnsmanager' ,
  993. array(
  994. 'dnsaction' => 'updateZone',
  995. 'zone_id' => $zoneID,
  996. 'records' => $zoneRecords,
  997. )
  998. );
  999. if($result['result'] != 'success') {
  1000. return 'Error: ' . $result['message'];
  1001. }
  1002. return 'success';
  1003. }
  1004. /**
  1005. * Removing a DNS record for a site of a siteBuilder account.
  1006. *
  1007. * @param array $params common module parameters
  1008. *
  1009. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  1010. *
  1011. * @return string "success" or an error message
  1012. */
  1013. function siteBuilderunsetDNS($params, $site) {
  1014. $siteName = $site . '.';
  1015. $zoneRecords = array();
  1016. $zoneIDcollection = Capsule::table('dns_manager2_zone')
  1017. ->select('id')
  1018. ->where('name', '=', $params['domain'])
  1019. ->where('clientid', '=', $params['userid'])
  1020. ->get();
  1021. $zoneIDobj = $zoneIDcollection[0];
  1022. $zoneID = $zoneIDobj->{'id'};
  1023. if(!isset($zoneID)) {
  1024. return 'Error: Zone for domain ' . $params['domain'] . ' or not owned by client';
  1025. }
  1026. $dnsZone = localAPI('dnsmanager', array( 'dnsaction' => 'getZone', 'zone_id' => $zoneID));
  1027. foreach($dnsZone['data']->records as $record) {
  1028. if(($record->name != $siteName) || ($record->type != 'A' && $record->type != 'CNAME')) {
  1029. array_push($zoneRecords, $record);
  1030. };
  1031. }
  1032. $result = localAPI('dnsmanager' ,
  1033. array(
  1034. 'dnsaction' => 'updateZone',
  1035. 'zone_id' => $zoneID,
  1036. 'records' => $zoneRecords,
  1037. )
  1038. );
  1039. if($result['result'] != 'success') {
  1040. return 'Error: ' . $result['message'];
  1041. }
  1042. return 'success';
  1043. }
  1044. /**
  1045. * Returns API Url .
  1046. *
  1047. * @param string $params common module parameters
  1048. * @param string $user
  1049. * @param string $params common module parameters
  1050. *
  1051. * @return string $apiUrl
  1052. */
  1053. function getSiteBuilderApiURL($params) {
  1054. $httpPrefix = $params['serversecure'] ? 'https://' : 'http://';
  1055. $serverPort = $params['serverport'] ? ':' . $params['serverport'] . '/' : '/';
  1056. return $httpPrefix . $params['serverhostname'] . $serverPort;
  1057. }
  1058. function getSites($serviceID) {
  1059. $sitesObj = Capsule::table('sitePro_site')
  1060. ->where('relid', $serviceID)
  1061. ->get();
  1062. $sites = [];
  1063. foreach($sitesObj as $site){
  1064. array_push($sites, $site->name);
  1065. }
  1066. return $sites;
  1067. }
  1068. function getSitesEnabled($serviceID) {
  1069. $sitesObj = Capsule::table('sitePro_site')
  1070. ->where('relid', $serviceID)
  1071. ->where('enabled', 1)
  1072. ->get();
  1073. $sites = [];
  1074. foreach($sitesObj as $site){
  1075. array_push($sites, $site->name);
  1076. }
  1077. return $sites;
  1078. }
  1079. function siteBuilderCreateTables() {
  1080. // Create a new table.
  1081. if (!Capsule::schema()->hasTable('sitePro_acc')) {
  1082. try {
  1083. Capsule::schema()->create(
  1084. 'sitePro_acc',
  1085. function ($table) { logModuleCall(
  1086. 'siteBuilder',
  1087. __FUNCTION__,
  1088. $params,
  1089. 'Debug',
  1090. $site
  1091. );
  1092. /** @var \Illuminate\Database\Schema\Blueprint $table */
  1093. $table->increments('id');
  1094. $table->string('account');
  1095. $table->integer('pid');
  1096. $table->boolean('enabled');
  1097. }
  1098. );
  1099. } catch (\Exception $e) {
  1100. echo "Unable to create sitePro_acc: {$e->getMessage()}";
  1101. }
  1102. }
  1103. if (!Capsule::schema()->hasTable('sitePro_site')) {
  1104. try {
  1105. Capsule::schema()->create(
  1106. 'sitePro_site',
  1107. function ($table) {
  1108. /** @var \Illuminate\Database\Schema\Blueprint $table */
  1109. $table->increments('id');
  1110. $table->integer('relid');
  1111. $table->string('name');
  1112. $table->boolean('enabled');
  1113. }
  1114. );
  1115. } catch (\Exception $e) {
  1116. echo "Unable to create sitePro_site: {$e->getMessage()}";
  1117. }
  1118. }
  1119. }