siteBuilder.php 34 KB

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