siteBuilder.php 35 KB

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