siteBuilder.php 35 KB

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