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