siteBuilder.php 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165
  1. <?php
  2. /**
  3. * WHMCS siteBuilder Provisioning Module
  4. *
  5. * Provisioning for User Account 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. if (!defined('WHMCS')) {
  15. die('This file cannot be accessed directly');
  16. }
  17. /**
  18. * Define siteBuilder product metadata parameters.
  19. *
  20. * @see https://developers.whmcs.com/provisioning-modules/meta-data-params/
  21. *
  22. * @return array
  23. */
  24. function siteBuilder_MetaData() {
  25. return array(
  26. 'DisplayName' => 'ThurData SiteBuilder Provisioning',
  27. 'APIVersion' => '1.2',
  28. 'DefaultNonSSLPort' => '80',
  29. 'DefaultSSLPort' => '443',
  30. 'RequiresServer' => true,
  31. 'ServiceSingleSignOnLabel' => 'Login to siteBuilder',
  32. 'AdminSingleSignOnLabel' => 'Login to siteBuilder Admin'
  33. );
  34. }
  35. function siteBuilder_ConfigOptions() {
  36. return ["BuilderURL" => [
  37. "FriendlyName" => "Builder URL", # Full Builder URL (prefix//hostname:port/)
  38. "Type" => "text", # Text Box
  39. "Size" => "25", # Defines the Field Width
  40. "Description" => "Textbox",
  41. "Default" => "https://builder.thurdata.ch/",
  42. ],
  43. ];
  44. }
  45. /**
  46. * Test connection to a siteBuilder server with the given server parameters.
  47. *
  48. * Allows an admin user to verify that an API connection can be
  49. * successfully made with the given configuration parameters for a
  50. * server.
  51. *
  52. * When defined in a module, a test connection button will appear
  53. * alongside the server type dropdown when adding or editing an
  54. * existing server.
  55. *
  56. * @param array $params common module parameters
  57. *
  58. * @see https://developers.whmcs.com/provisioning-modules/module-parameters/
  59. *
  60. * @return array
  61. */
  62. function siteBuilder_Testconnection($params) {
  63. $siteBuilder = new ApiClient(getSiteBuilderApiURL($params), $params['serveraccesshash']);
  64. $response = $siteBuilder->ping($params['serverusername'], $params['serverpassword']);
  65. if($response['response']['answer'] == 'pong') {
  66. return array(
  67. 'success' => true,
  68. 'error' => '',
  69. );
  70. }
  71. return array(
  72. 'success' => false,
  73. 'error' => $response,
  74. );
  75. }
  76. /**
  77. * Provision a new account of a siteBuilder server.
  78. *
  79. * Attempt to provision a new siteBuilder account. This is
  80. * called any time provisioning is requested inside of WHMCS. Depending upon the
  81. * configuration, this can be any of:
  82. * * When a new order is placed
  83. * * When an invoice for a new order is paid
  84. * * Upon manual request by an admin user
  85. *
  86. * @param array $params common module parameters
  87. *
  88. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  89. *
  90. * @return string 'success' or an error message
  91. */
  92. function siteBuilder_CreateAccount($params) {
  93. $username = strtolower(substr($params['clientsdetails']['firstname'],0,2) . substr($params['clientsdetails']['lastname'],0,3)) . $params['serviceid'];
  94. $userdomain = $params['domain'];
  95. try {
  96. Capsule::table('tblhosting')
  97. ->where('id', '=', $params['serviceid'])
  98. ->update(
  99. array(
  100. 'username' => $username,
  101. 'domain' => $userdomain,
  102. )
  103. );
  104. } catch (\Exception $e) {
  105. logModuleCall(
  106. 'siteBuilder',
  107. __FUNCTION__,
  108. $params,
  109. 'Error: could save username & domain in database',
  110. $e->getMessage()
  111. );
  112. return 'Error: could save username & password in database';
  113. }
  114. if ($params["server"] == 1) {
  115. $siteBuilder = new ApiClient(getSiteBuilderApiURL($params), $params['serveraccesshash']);
  116. $response = $siteBuilder->deployDev($username, $domain, $params['serverusername'], $params['serverpassword']);
  117. }
  118. if($response['status'] != '200') {
  119. return 'Error: ' . $response['response'];
  120. }
  121. return 'success';
  122. }
  123. /**
  124. * Removes a siteBuilder account.
  125. *
  126. * Called when a termination is requested. This can be invoked automatically for
  127. * overdue products if enabled, or requested manually by an admin user.
  128. *
  129. * @param array $params common module parameters
  130. *
  131. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  132. *
  133. * @return string 'success' or an error message
  134. */
  135. function siteBuilder_TerminateAccount($params) {
  136. $siteBuilder = new ApiClient(getSiteBuilderApiURL($params), $params['serveraccesshash']);
  137. $response = $siteBuilder->terminate($params['domain'],$params['username']);
  138. if($response['status'] != '200') {
  139. return 'Error: ' . $response['response'];
  140. }
  141. return 'success';
  142. }
  143. /**
  144. * Set a siteBuilder account to status inactive.
  145. *
  146. * Called when a suspension is requested. This is invoked automatically by WHMCS
  147. * when a product becomes overdue on payment or can be called manually by admin
  148. * user.
  149. *
  150. * @param array $params common module parameters
  151. *
  152. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  153. *
  154. * @return string 'success' or an error message
  155. */
  156. function siteBuilder_SuspendAccount($params) {
  157. $siteBuilder = new ApiClient(getSiteBuilderApiURL($params), $params['serveraccesshash']);
  158. $response = $siteBuilder->disableprod($params['domain'],$params['username']);
  159. if($response['status'] != 'OK') {
  160. return 'Error: ' . $response['error_msg'];
  161. }
  162. return 'success';
  163. }
  164. /**
  165. * Set a siteBuilder account to status active.
  166. *
  167. * Called when an un-suspension is requested. This is invoked
  168. * automatically upon payment of an overdue invoice for a product, or
  169. * can be called manually by admin user.
  170. *
  171. * @param array $params common module parameters
  172. *
  173. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  174. *
  175. * @return string 'success' or an error message
  176. */
  177. function siteBuilder_UnsuspendAccount($params) {
  178. $siteBuilder = new ApiClient(getSiteBuilderApiURL($params), $params['serveraccesshash']);
  179. $response = $siteBuilder->enableprod($params['domain'],$params['username']);
  180. if($response['status'] != 'OK') {
  181. return 'Error: ' . $response['error_msg'];
  182. }
  183. return 'success';
  184. }
  185. /**
  186. * Client area output logic handling.
  187. *
  188. * This function is used to define module specific client area output. It should
  189. * return an array consisting of a template file and optional additional
  190. * template variables to make available to that template.
  191. *
  192. * @param array $params common module parameters
  193. *
  194. * @see https://developers.whmcs.com/provisioning-modules/client-area-output/
  195. *
  196. * @return array
  197. */
  198. function siteBuilder_ClientArea($params) {
  199. $clientInfo = array('moduleclientarea' => '1');
  200. $siteBuilder = new ApiClient(getSiteBuilderApiURL($params), $params['serveraccesshash']);
  201. $response = $siteBuilder->getSSLDays($params['domain'],$params['username']);
  202. if($response['status'] == 'OK') {
  203. $sslSites = array();
  204. foreach($response['msj'] as $sslSite) {
  205. $sslSites[$sslSite['ssl']] = array(
  206. 'auotssl' => $sslSite['autossl'],
  207. 'expire' => $sslSite['exp'],
  208. );
  209. }
  210. }
  211. $response = $siteBuilder->getAccount($params['username']);
  212. if($response['status'] != 'OK') {
  213. logModuleCall(
  214. 'siteBuilder',
  215. __FUNCTION__,
  216. $params,
  217. 'debug',
  218. $response
  219. );
  220. }
  221. if(siteBuilderCheckLimit($params,'domains')){
  222. $clientInfo['domainlimit'] = 1;
  223. } else {
  224. $clientInfo['domainlimit'] = 0;
  225. };
  226. if(siteBuilderCheckLimit($params,'subdomins')){
  227. $clientInfo['subdomainlimit'] = 1;
  228. } else {
  229. $clientInfo['subdomainlimit'] = 0;
  230. };
  231. $clientInfo['db_max'] = $response['result']['account_info']['db_max'];
  232. $clientInfo['db_used'] = $response['result']['account_info']['db_used'];
  233. $clientInfo['ftp_accounts'] = $response['result']['account_info']['ftp_accounts'];
  234. $clientInfo['ftp_accounts_used'] = $response['result']['account_info']['ftp_accounts_used'];
  235. $clientInfo['addons_domains'] = $response['result']['account_info']['addons_domains'];
  236. $clientInfo['addons_domains_used'] = $response['result']['account_info']['addons_domains_used'];
  237. $clientInfo['sub_domains'] = $response['result']['account_info']['sub_domains'];
  238. $clientInfo['sub_domains_used'] = $response['result']['account_info']['sub_domains_used'];
  239. $clientInfo['space_usage'] = $response['result']['account_info']['space_usage'];
  240. $clientInfo['space_disk'] = $response['result']['account_info']['space_disk'];
  241. $clientInfo['bandwidth_used'] = $response['result']['account_info']['bandwidth_used'];
  242. $clientInfo['bandwidth'] = $response['result']['account_info']['bandwidth'];
  243. $domains = $response['result']['domains'];
  244. $subDomains = $response['result']['subdomins'];
  245. $clientInfo['domains'] = array();
  246. foreach($domains as $domain) {
  247. if($domain['path'] == '/home/' . $params['username'] . '/public_html') {
  248. $clientInfo['mgmtDomain'] = $domain['domain'];
  249. $clientInfo['mgmtEmail'] = $domain['email'];
  250. } else {
  251. $domain['relpath'] = str_replace('/home/' . $params['username'], '~', $domain['path']);
  252. if(array_key_exists($domain['domain'], $sslSites)) {
  253. $domain['ssl'] = 1;
  254. $domain['sslexpire'] = $sslSites[$domain['domain']]['expire'];
  255. $domain['autossl'] = $sslSites[$domain['domain']]['auotssl'];
  256. }
  257. if(siteBuilderCheckA($domain['domain'],$params['serverip'],$params['configoption5']) == 1) {
  258. $domain['DNS'] = 1;
  259. }
  260. $domain['domainNS'] = siteBuilderCheckSOA($domain['domain'],$params['configoption5']);
  261. $domain['subdomains'] = array();
  262. foreach($subDomains as $subDomain) {
  263. if($subDomain['domain'] == $domain['domain']) {
  264. $subFQDN = $subDomain['subdomain'] . '.' . $subDomain['domain'];
  265. $subDomain['relpath'] = str_replace('/home/' . $params['username'], '~', $subDomain['path']);
  266. if(array_key_exists($subFQDN, $sslSites)) {
  267. $subDomain['ssl'] = 1;
  268. $subDomain['sslexpire'] = $sslSites[$subFQDN]['expire'];
  269. $subDomain['autossl'] = $sslSites[$subFQDN]['auotssl'];
  270. } else {
  271. unset($subDomain['ssl']);
  272. unset($subDomain['sslexpire']);
  273. unset($subDomain['autossl']);
  274. }
  275. if(siteBuilderCheckA($subFQDN,$params['serverip'],$params['configoption5']) == 1) {
  276. $subDomain['DNS'] = 1;
  277. } else {
  278. unset($subDomain['DNS']);
  279. }
  280. array_push($domain['subdomains'], $subDomain);
  281. }
  282. }
  283. array_push($clientInfo['domains'], $domain);
  284. }
  285. }
  286. return array(
  287. 'tabOverviewReplacementTemplate' => 'clientarea',
  288. 'vars' => $clientInfo,
  289. );
  290. }
  291. /**
  292. * Perform single sign-on for a siteBuilder account.
  293. *
  294. * When successful, returns a URL to which the user should be redirected.
  295. *
  296. * @param array $params common module parameters
  297. *
  298. * @see https://developers.whmcs.com/provisioning-modules/single-sign-on/
  299. *
  300. * @return array
  301. */
  302. function siteBuilder_ServiceSingleSignOn($params) {
  303. $siteBuilder = new ApiClient(getSiteBuilderApiURL($params), $params['serveraccesshash']);
  304. $response = $siteBuilder->getLoginLink($params['username']);
  305. if($response['status'] == 'OK') {
  306. $link = $response['msj']['details'];
  307. $linkautologin = $link[0]['url'];
  308. return array(
  309. 'success' => true,
  310. 'redirectTo' => $linkautologin,
  311. );
  312. } else {
  313. return array(
  314. 'success' => false,
  315. 'redirectTo' => '',
  316. );
  317. }
  318. }
  319. /**
  320. * Change the password for a siteBuilder account.
  321. *
  322. * Called when a password change is requested. This can occur either due to a
  323. * client requesting it via the client area or an admin requesting it from the
  324. * admin side.
  325. *
  326. * This option is only available to client end users when the product is in an
  327. * active status.
  328. *
  329. * @param array $params common module parameters
  330. *
  331. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  332. *
  333. * @return string "success" or an error message
  334. */
  335. function siteBuilder_ChangePassword($params) {
  336. $siteBuilder = new siteBuilder_Admin($params['serverhostname'], $params['serveraccesshash']);
  337. $response = $siteBuilder->changePass(array('user' => $params['username'], 'password' => $params['password']));
  338. if($response['status'] != 'OK') {
  339. return 'Error: ' . $response['error_msg'];
  340. }
  341. return 'success';
  342. }
  343. /**
  344. * Upgrade or downgrade a siteBuilder account by package.
  345. *
  346. * Called to apply any change in product assignment or parameters. It
  347. * is called to provision upgrade or downgrade orders, as well as being
  348. * able to be invoked manually by an admin user.
  349. *
  350. * This same function is called for upgrades and downgrades of both
  351. * products and configurable options.
  352. *
  353. * @param array $params common module parameters
  354. *
  355. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  356. *
  357. * @return string "success" or an error message
  358. */
  359. function siteBuilder_ChangePackage($params) {
  360. $siteBuilder = new siteBuilder_Admin($params['serverhostname'], $params['serveraccesshash']);
  361. $data = array(
  362. 'user' => $params['username'],
  363. 'email' => $params['clientsdetails']['email'],
  364. 'package' => $params['configoption1'],
  365. 'inode' => (int) $params["configoption2"],
  366. 'openfiles' => (int) $params["configoption3"],
  367. 'processes' => (int) $params["configoption4"],
  368. 'server_ips'=> $params["serverip"],
  369. );
  370. $response = $siteBuilder->modifyAccount($data);
  371. if($response['status'] != 'OK') {
  372. return 'Error: ' . $response['error_msg'];
  373. }
  374. return 'success';
  375. }
  376. /**
  377. * Usage Update
  378. *
  379. * Important: Runs daily per server not per product
  380. * Run Manually: /admin/reports.php?report=disk_usage_summary&action=updatestats
  381. * @param array $params common module parameters
  382. *
  383. * @see https://developers.whmcs.com/provisioning-modules/usage-update/
  384. */
  385. function siteBuilder_UsageUpdate($params) {
  386. $siteBuilder = new siteBuilder_Admin($params['serverhostname'], $params['serveraccesshash']);
  387. $response = $siteBuilder->getAllAccounts();
  388. if($response['status'] == 'OK'){
  389. $results = $response['msj'];
  390. for($i = 0; $i < count($results); $i++){
  391. if($results[$i]['diskusage'] == '') {
  392. $diskusage = 0;
  393. } else {
  394. $diskusage = trim($results[$i]['diskusage']);
  395. }
  396. if($results[$i]['disklimit'] == '') {
  397. $disklimit = 0;
  398. } else {
  399. $disklimit = trim($results[$i]['disklimit']);
  400. }
  401. if($results[$i]['bandwidth'] == '') {
  402. $bandwidth = 0;
  403. } else {
  404. $bandwidth =trim($results[$i]['bandwidth']);
  405. }
  406. if($results[$i]['bwlimit'] == '') {
  407. $bwlimit = 0;
  408. } else {
  409. $bwlimit = trim($results[$i]['bwlimit']);
  410. }
  411. $domain = trim($results[$i]['domain']);
  412. try {
  413. \WHMCS\Database\Capsule::table('tblhosting')
  414. ->where('server', $params['serverid'])
  415. ->where('domain', $domain)
  416. ->update([
  417. 'diskusage' => $diskusage,
  418. 'disklimit' => $disklimit,
  419. 'bwusage' => $bandwidth,
  420. 'bwlimit' => $bwlimit,
  421. 'lastupdate' => date('Y-m-d H:i:S'),
  422. ]);
  423. } catch (\Exception $e) {
  424. logActivity('ERROR: Unable to update server usage: ' . $e->getMessage());
  425. }
  426. }
  427. }
  428. }
  429. /**
  430. * Additional actions a client user can invoke.
  431. *
  432. * Define additional actions a client user can perform for an instance of a
  433. * product/service.
  434. *
  435. * Any actions you define here will be automatically displayed in the available
  436. * list of actions within the client area.
  437. *
  438. * @return array
  439. */
  440. function siteBuilder_ClientAreaCustomButtonArray ($params) {
  441. if(siteBuilderCheckLimit($params, 'domains')) {
  442. return array();
  443. }
  444. return array(
  445. 'Neue Domain' => 'newDomain',
  446. );
  447. }
  448. /**
  449. * Additional actions a client user can invoke.
  450. *
  451. * Define additional actions a client user is allowed to perform for an instance of a
  452. * product/service.
  453. *
  454. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  455. *
  456. * @return array
  457. */
  458. function siteBuilder_ClientAreaAllowedFunctions() {
  459. return array(
  460. "Enable SSL" => "enableSSL",
  461. "Renew SSL" => "renewSSL",
  462. "Set DNS" => "setDNS",
  463. "Unset DNS" => "unsetDNS",
  464. "Confirm Enable SSL" => "enableSSLConfirm",
  465. "Confirm Renew SSL" => "renewSSLConfirm",
  466. "Confirm Set DNS" => "setDNSConfirm",
  467. "Confirm Unset DNS" => "unsetDNSConfirm",
  468. "Info DNS" => "infoDNS",
  469. "Info SSL" => "infoSSL",
  470. "Add Domain" => "addDomain",
  471. "new Domain" => "newDomain",
  472. "Add Subdomain" => "addSubdomain",
  473. "New Subdomain" => "newSubdomain",
  474. "Confirm Delete Domain" => "delDomainConfirm",
  475. "Delete Domain" => "delDomain",
  476. "Confirm Delete Subdomain" => "delSubdomainConfirm",
  477. "Delete Subdomain" => "delSubdomain",
  478. );
  479. }
  480. /**
  481. * Opens a form to add a new domain.
  482. *
  483. * @param array $params common module parameters
  484. *
  485. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  486. *
  487. * @return array template information
  488. */
  489. function siteBuilder_newDomain($params) {
  490. return array(
  491. 'breadcrumb' => array(
  492. 'clientarea.php?action=productdetails&id=' . $params['serviceid'] . '&modop=custom&a=newDomain' => 'Neue Domain',
  493. ),
  494. 'templatefile' => 'siteBuilder_add_domain',
  495. );
  496. }
  497. /**
  498. * Adds a new domain to a siteBuilder account.
  499. *
  500. * @param array $params common module parameters
  501. *
  502. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  503. *
  504. * @return string "success" or an error message
  505. */
  506. function siteBuilder_addDomain($params) {
  507. if(!filter_var($_POST['d'], FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME)){
  508. return 'Error: invalid domain name';
  509. }
  510. if(siteBuilderCheckLimit($params, 'domains')) {
  511. return 'Error: domain limit exceeded';
  512. }
  513. $vars['user'] = $params['username'];
  514. $vars['name'] = $_POST['d'];
  515. $vars['type'] = 'domain';
  516. $siteBuilder = new siteBuilder_Admin($params['serverhostname'], $params['serveraccesshash']);
  517. $response = $siteBuilder->addDomain($vars);
  518. if($response['status'] != 'OK') {
  519. return 'Error: ' . $response['error_msg'];
  520. }
  521. return 'success';
  522. }
  523. /**
  524. * Opens a form to add a new subdomain to a domain.
  525. *
  526. * @param array $params common module parameters
  527. *
  528. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  529. *
  530. * @return array template information
  531. */
  532. function siteBuilder_newSubdomain($params) {
  533. if(!filter_var($_POST['d'], FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME)){
  534. return 'Error: invalid domain name';
  535. }
  536. return array(
  537. 'breadcrumb' => array(
  538. 'clientarea.php?action=productdetails&id=' . $params['serviceid'] . '&modop=custom&a=newSubdomain' => 'Neue Subdomain',
  539. ),
  540. 'templatefile' => 'siteBuilder_add_subdomain',
  541. 'vars' => array(
  542. 'domainselected' => $_POST['d'],
  543. ),
  544. );
  545. }
  546. /**
  547. * Adds a new subdomain to domain of a siteBuilder account.
  548. *
  549. * @param array $params common module parameters
  550. *
  551. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  552. *
  553. * @return string "success" or an error message
  554. */
  555. function siteBuilder_addSubdomain($params) {
  556. if(!filter_var($_POST['d'], FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME)){
  557. return 'Error: invalid domain name';
  558. }
  559. if(!filter_var($_POST['s'], FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME)){
  560. return 'Error: invalid subdomain name';
  561. }
  562. if($_POST['s'] == 'www') {
  563. return 'Error: default Subdomain www wurde bereits automatisch erstellt' ;
  564. }
  565. if(siteBuilderCheckLimit($params, 'subdomins')) {
  566. return 'Error: subdomain limit exceeded';
  567. }
  568. $vars['user'] = $params['username'];
  569. $vars['name'] = $_POST['s'] . '.' . $_POST['d'];
  570. $vars['type'] = 'subdomain';
  571. $siteBuilder = new siteBuilder_Admin($params['serverhostname'], $params['serveraccesshash']);
  572. $response = $siteBuilder->addDomain($vars);
  573. if($response['status'] != 'OK') {
  574. return 'Error: ' . $response['error_msg'];
  575. }
  576. return 'success';
  577. }
  578. /**
  579. * Opens a form to delete a domain from a siteBuilder account.
  580. *
  581. * @param array $params common module parameters
  582. *
  583. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  584. *
  585. * @return array template information
  586. */
  587. function siteBuilder_delDomainConfirm($params) {
  588. return array(
  589. 'templatefile' => 'siteBuilder_del_domain_confirm',
  590. 'vars' => array(
  591. 'deldomain' => $_POST['d'],
  592. ),
  593. );
  594. }
  595. /**
  596. * Removes a domain from a siteBuilder account.
  597. *
  598. * @param array $params common module parameters
  599. *
  600. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  601. *
  602. * @return string "success" or an error message
  603. */
  604. function siteBuilder_delDomain($params) {
  605. if(!filter_var($_POST['d'], FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME)){
  606. return 'Error: invalid domain name';
  607. }
  608. $siteBuilder = new siteBuilder_Admin($params['serverhostname'], $params['serveraccesshash']);
  609. $response = $siteBuilder->getAccount($params['username']);
  610. if($response['status'] != 'OK') {
  611. logModuleCall(
  612. 'siteBuilder',
  613. __FUNCTION__,
  614. $params,
  615. 'debug',
  616. $response
  617. );
  618. }
  619. $domains = $response['result']['domains'];
  620. $clientdomains = array();
  621. foreach($domains as $domain){
  622. if($domain['domain'] != $params['domain']) {
  623. array_push($clientdomains, $domain['domain']);
  624. }
  625. }
  626. if(!in_array($_POST['d'], $clientdomains)) {
  627. logModuleCall(
  628. 'siteBuilder',
  629. __FUNCTION__,
  630. $_POST,
  631. 'POST DATA VIOLATION',
  632. $params
  633. );
  634. return 'Error: ' . $_POST['d'] . ' not in client domains';
  635. }
  636. // do delete domain
  637. $vars['user'] = $params['username'];
  638. $vars['name'] = $_POST['d'];
  639. $vars['type'] = 'domain';
  640. $response = $siteBuilder->deleteDomain($vars);
  641. if($response['status'] != 'OK') {
  642. return 'Error: ' . $response['error_msg'];
  643. }
  644. return 'success';
  645. }
  646. /**
  647. * Opens a form to delete a subdomain from domain of a siteBuilder account.
  648. *
  649. * @param array $params common module parameters
  650. *
  651. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  652. *
  653. * @return array template information
  654. */
  655. function siteBuilder_delSubdomainConfirm($params) {
  656. return array(
  657. 'templatefile' => 'siteBuilder_del_subdomain_confirm',
  658. 'vars' => array(
  659. 'delsubdomain' => $_POST['d'],
  660. ),
  661. );
  662. }
  663. /**
  664. * Removes a subdomain from a domain of a siteBuilder account.
  665. *
  666. * @param array $params common module parameters
  667. *
  668. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  669. *
  670. * @return string "success" or an error message
  671. */
  672. function siteBuilder_delSubdomain($params) {
  673. if(!filter_var($_POST['d'], FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME)){
  674. return 'Error: invalid domain name';
  675. }
  676. $siteBuilder = new siteBuilder_Admin($params['serverhostname'], $params['serveraccesshash']);
  677. $response = $siteBuilder->getAccount($params['username']);
  678. if($response['status'] != 'OK') {
  679. logModuleCall(
  680. 'siteBuilder',
  681. __FUNCTION__,
  682. $params,
  683. 'debug',
  684. $response
  685. );
  686. }
  687. $subdomains = $response['result']['subdomins'];
  688. $clientsubdomains = array();
  689. foreach($subdomains as $subdomain){
  690. if($subdomain['domain'] != $params['domain']) {
  691. array_push($clientsubdomains, $subdomain['subdomain'] . "." . $subdomain['domain']);
  692. }
  693. }
  694. if(!in_array($_POST['d'], $clientsubdomains)) {
  695. logModuleCall(
  696. 'siteBuilder',
  697. __FUNCTION__,
  698. $_POST,
  699. 'POST DATA VIOLATION',
  700. $params
  701. );
  702. return 'Error: ' . $_POST['d'] . ' not in client subdomains';
  703. }
  704. // do delete subdomain
  705. $vars['user'] = $params['username'];
  706. $vars['name'] = $_POST['d'];
  707. $vars['type'] = 'subdomain';
  708. $response = $siteBuilder->deleteDomain($vars);
  709. if($response['status'] != 'OK') {
  710. return 'Error: ' . $response['error_msg'];
  711. }
  712. return 'success';
  713. }
  714. /**
  715. * Opens a form to enable SSL for a subdomain or domain of a siteBuilder account.
  716. *
  717. * @param array $params common module parameters
  718. *
  719. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  720. *
  721. * @return array template information
  722. */
  723. function siteBuilder_enableSSLConfirm($params) {
  724. return array(
  725. 'templatefile' => 'siteBuilder_enable_SSL_confirm',
  726. 'vars' => array(
  727. 'SSLdomain' => $_POST['d'],
  728. ),
  729. );
  730. }
  731. /**
  732. * Aktivate siteBuilder AutoSSL for a subdomain or domain of a siteBuilder account.
  733. *
  734. * @param array $params common module parameters
  735. *
  736. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  737. *
  738. * @return string "success" or an error message
  739. */
  740. function siteBuilder_enableSSL($params) {
  741. if(!filter_var($_POST['d'], FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME)){
  742. return 'Error: invalid domain name';
  743. }
  744. $vars['user'] = $params['username'];
  745. $vars['name'] = $_POST['d'];
  746. $siteBuilder = new siteBuilder_Admin($params['serverhostname'], $params['serveraccesshash']);
  747. $response = $siteBuilder->addAutoSSL($vars);
  748. if($response['status'] != 'OK') {
  749. return 'Error: ' . $response['error_msg'];
  750. }
  751. return 'success';
  752. }
  753. /**
  754. * Opens a form to renew a SSL certificate for a subdomain or domain of a siteBuilder account.
  755. *
  756. * @param array $params common module parameters
  757. *
  758. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  759. *
  760. * @return array template information
  761. */
  762. function siteBuilder_renewSSLConfirm($params) {
  763. return array(
  764. 'templatefile' => 'siteBuilder_renew_SSL_confirm',
  765. 'vars' => array(
  766. 'SSLdomain' => $_POST['d'],
  767. ),
  768. );
  769. }
  770. /**
  771. * Renews a SSL certificate for a subdomain or domain of a siteBuilder account.
  772. *
  773. * @param array $params common module parameters
  774. *
  775. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  776. *
  777. * @return string "success" or an error message
  778. */
  779. function siteBuilder_renewSSL($params) {
  780. if(!filter_var($_POST['d'], FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME)){
  781. return 'Error: invalid domain name';
  782. }
  783. $vars['user'] = $params['username'];
  784. $vars['name'] = $_POST['d'];
  785. $siteBuilder = new siteBuilder_Admin($params['serverhostname'], $params['serveraccesshash']);
  786. $response = $siteBuilder->updateAutoSSL($vars);
  787. if($response['status'] != 'OK') {
  788. return 'Error: ' . $response['error_msg'];
  789. }
  790. return 'success';
  791. }
  792. /**
  793. * Opens a form to set a DNS record for a subdomain or domain of a siteBuilder account.
  794. *
  795. * @param array $params common module parameters
  796. *
  797. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  798. *
  799. * @return array template information
  800. */
  801. function siteBuilder_setDNSConfirm($params) {
  802. if(isset($_POST['s'])){
  803. return array(
  804. 'templatefile' => 'siteBuilder_set_DNS_confirm',
  805. 'vars' => array(
  806. 'DNSdomain' => $_POST['d'],
  807. 'DNSsubdomain' => $_POST['s'],
  808. ),
  809. );
  810. }
  811. return array(
  812. 'templatefile' => 'siteBuilder_set_DNS_confirm',
  813. 'vars' => array(
  814. 'DNSdomain' => $_POST['d'],
  815. ),
  816. );
  817. }
  818. /**
  819. * Opens a form to unsset a DNS record for a subdomain or domain of a siteBuilder account.
  820. *
  821. * @param array $params common module parameters
  822. *
  823. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  824. *
  825. * @return array template information
  826. */
  827. function siteBuilder_unsetDNSConfirm($params) {
  828. if(isset($_POST['s'])){
  829. return array(
  830. 'templatefile' => 'siteBuilder_unset_DNS_confirm',
  831. 'vars' => array(
  832. 'DNSdomain' => $_POST['d'],
  833. 'DNSsubdomain' => $_POST['s'],
  834. ),
  835. );
  836. }
  837. return array(
  838. 'templatefile' => 'siteBuilder_unset_DNS_confirm',
  839. 'vars' => array(
  840. 'DNSdomain' => $_POST['d'],
  841. ),
  842. );
  843. }
  844. /**
  845. * Update a DNS zone for a domain setting a new record for a domain or subdomain of a siteBuilder account.
  846. *
  847. * @param array $params common module parameters
  848. *
  849. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  850. *
  851. * @return string "success" or an error message
  852. */
  853. function siteBuilder_setDNS($params) {
  854. if(!filter_var($_POST['d'], FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME)){
  855. return 'Error: invalid domain name';
  856. }
  857. $domainName = $_POST['d'];
  858. $zoneRecords = array();
  859. if(isset($_POST['s'])){
  860. if(!filter_var($_POST['s'], FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME)){
  861. return 'Error: invalid subdomain name';
  862. }
  863. $hostName = $_POST['s'] . '.' . $domainName . '.';
  864. $newRecord = array(
  865. 'line' => $hostName.'|A|0',
  866. 'name' => $hostName,
  867. 'type' => 'A',
  868. 'class' => 'IN',
  869. 'data' => array(
  870. 'address' => $params['serverip'],
  871. ),
  872. );
  873. array_push($zoneRecords, $newRecord);
  874. } else {
  875. $hostName = $domainName . '.';
  876. $domainRecord = array(
  877. 'line' => $hostName.'|A|0',
  878. 'name' => $hostName,
  879. 'type' => 'A',
  880. 'class' => 'IN',
  881. 'data' => array(
  882. 'address' => $params['serverip'],
  883. ),
  884. );
  885. array_push($zoneRecords, $domainRecord);
  886. $wwwRecord = array(
  887. 'line' => 'www'.$hostName.'|A|0',
  888. 'name' => 'www'.$hostName,
  889. 'type' => 'A',
  890. 'class' => 'IN',
  891. 'data' => array(
  892. 'address' => $params['serverip'],
  893. ),
  894. );
  895. array_push($zoneRecords, $wwwRecord);
  896. }
  897. $zoneIDcollection = Capsule::table('dns_manager2_zone')
  898. ->select('id')
  899. ->where('name', '=', $domainName)
  900. ->where('clientid', '=', $params['userid'])
  901. ->get();
  902. $zoneIDobj = $zoneIDcollection[0];
  903. $zoneID = $zoneIDobj->{'id'};
  904. if(!isset($zoneID)) {
  905. return 'Error: Zone for domain ' . $domainName . ' or not owned by client';
  906. }
  907. $dnsZone = localAPI('dnsmanager', array( 'dnsaction' => 'getZone', 'zone_id' => $zoneID));
  908. foreach($dnsZone['data']->records as $record) {
  909. if(($record->name != $hostName) || ($record->type != 'A' && $record->type != 'CNAME')) {
  910. array_push($zoneRecords, $record);
  911. };
  912. }
  913. $result = localAPI('dnsmanager' ,
  914. array(
  915. 'dnsaction' => 'updateZone',
  916. 'zone_id' => $zoneID,
  917. 'records' => $zoneRecords,
  918. )
  919. );
  920. if($result['result'] != 'success') {
  921. return 'Error: ' . $result['message'];
  922. }
  923. return 'success';
  924. }
  925. /**
  926. * Removing a DNS record for a domain or subdomain of a siteBuilder account.
  927. *
  928. * @param array $params common module parameters
  929. *
  930. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  931. *
  932. * @return string "success" or an error message
  933. */
  934. function siteBuilder_unsetDNS($params) {
  935. if(!filter_var($_POST['d'], FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME)){
  936. return 'Error: invalid domain name';
  937. }
  938. $domainName = $_POST['d'];
  939. $zoneRecords = array();
  940. if(isset($_POST['s'])){
  941. if(!filter_var($_POST['s'], FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME)){
  942. return 'Error: invalid subdomain name';
  943. }
  944. $hostName = $_POST['s'] . '.' . $domainName . '.';
  945. } else {
  946. $hostName = $domainName . '.';
  947. }
  948. $zoneIDcollection = Capsule::table('dns_manager2_zone')
  949. ->select('id')
  950. ->where('name', '=', $domainName)
  951. ->where('clientid', '=', $params['userid'])
  952. ->get();
  953. $zoneIDobj = $zoneIDcollection[0];
  954. $zoneID = $zoneIDobj->{'id'};
  955. if(!isset($zoneID)) {
  956. return 'Error: Zone for domain ' . $domainName . ' or not owned by client';
  957. }
  958. $dnsZone = localAPI('dnsmanager', array( 'dnsaction' => 'getZone', 'zone_id' => $zoneID));
  959. foreach($dnsZone['data']->records as $record) {
  960. if(($record->name != $hostName) || ($record->type != 'A' && $record->type != 'CNAME')) {
  961. array_push($zoneRecords, $record);
  962. };
  963. }
  964. $result = localAPI('dnsmanager' ,
  965. array(
  966. 'dnsaction' => 'updateZone',
  967. 'zone_id' => $zoneID,
  968. 'records' => $zoneRecords,
  969. )
  970. );
  971. if($result['result'] != 'success') {
  972. return 'Error: ' . $result['message'];
  973. }
  974. return 'success';
  975. }
  976. /**
  977. * Opens a form to inform about the DNS status of a subdomain or domain of a siteBuilder account.
  978. *
  979. * @param array $params common module parameters
  980. *
  981. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  982. *
  983. * @return array template information
  984. */
  985. function siteBuilder_infoDNS($params) {
  986. if(!filter_var($_POST['d'], FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME)){
  987. return 'Error: invalid domain name';
  988. }
  989. $siteBuildernameserver = siteBuilderCheckSOA($_POST['d'],$params['configoption5']);
  990. return array(
  991. 'templatefile' => 'siteBuilder_help_dns',
  992. 'vars' => array(
  993. 'infodomain' => $_POST['d'],
  994. 'siteBuildernameserver' => $siteBuildernameserver,
  995. ),
  996. );
  997. }
  998. /**
  999. * Opens a form to inform about the SSL status of a subdomain or domain of a siteBuilder account.
  1000. *
  1001. * @param array $params common module parameters
  1002. *
  1003. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  1004. *
  1005. * @return array template information
  1006. */
  1007. function siteBuilder_infoSSL($params) {
  1008. if(!filter_var($_POST['d'], FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME)){
  1009. return 'Error: invalid domain name';
  1010. }
  1011. return array(
  1012. 'templatefile' => 'siteBuilder_help_ssl',
  1013. 'vars' => array(
  1014. 'infodomain' => $_POST['d'],
  1015. ),
  1016. );
  1017. }
  1018. /**
  1019. * Ask nameservers for a IP adress of a given host.
  1020. *
  1021. * @param string $host hostname
  1022. * @param string $serverIP siteBuilder server IP
  1023. * @param string $nameserverIP polled name server IP
  1024. * @param int $recurse optional -> used to follow CNAME responses
  1025. *
  1026. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  1027. *
  1028. * @return bool
  1029. */
  1030. function siteBuilderCheckA($host, $serverIP, $nameserverIP, $recurse = 0) {
  1031. if($recurse > 3) {
  1032. return false;
  1033. }
  1034. $nameserver = array($nameserverIP);
  1035. # try NS1
  1036. $resolver = new Net_DNS2_Resolver(array('nameservers' => $nameserver));
  1037. try {
  1038. $result = $resolver->query($host, 'A');
  1039. } catch(Net_DNS2_Exception $e) {
  1040. # try default nameserver
  1041. $resolver = new Net_DNS2_Resolver();
  1042. try {
  1043. $result = $resolver->query($host, 'A');
  1044. } catch(Net_DNS2_Exception $e) {
  1045. logModuleCall(
  1046. 'siteBuilder',
  1047. __FUNCTION__,
  1048. $e,
  1049. 'DNS lookup exception',
  1050. $e->getMessage()
  1051. );
  1052. return false;
  1053. }
  1054. }
  1055. $hostA = $result->answer;
  1056. if($hostA[0]->type == 'CNAME') {
  1057. if(siteBuilderCheckA($hostA[0]->cname, $serverIP, $nameserverIP, $recurse++)) {
  1058. return true;
  1059. }
  1060. }
  1061. if($hostA[0]->type == 'A') {
  1062. if($hostA[0]->address == $serverIP){
  1063. return true;
  1064. }
  1065. }
  1066. return false;
  1067. }
  1068. /**
  1069. * Ask nameservers for the authority of a domain.
  1070. *
  1071. * @param string $domain domain name
  1072. * @param string $nameserverIP polled name server IP
  1073. * @param string $nameserverName name of the own namesever
  1074. *
  1075. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  1076. *
  1077. * @return string 'none' -> not registered, 'self' -> registered at own or the name of an other responsible nameserver
  1078. */
  1079. function siteBuilderCheckSOA($domain, $nameserverIP) {
  1080. $nameserver = array($nameserverIP);
  1081. # try NS1
  1082. $resolver = new Net_DNS2_Resolver(array('nameservers' => $nameserver));
  1083. try {
  1084. $result = $resolver->query($domain, 'SOA');
  1085. return 'self';
  1086. } catch(Net_DNS2_Exception $e) {
  1087. # try default NS
  1088. $resolver = new Net_DNS2_Resolver();
  1089. try {
  1090. $result = $resolver->query($domain, 'SOA');
  1091. } catch(Net_DNS2_Exception $e) {
  1092. return 'none';
  1093. }
  1094. }
  1095. return $result->answer[0]->mname;
  1096. }
  1097. /**
  1098. * Check limits for a service of an account .
  1099. *
  1100. * @param array $params common module parameters
  1101. * @param string $type domains|subdomins
  1102. *
  1103. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  1104. *
  1105. * @return bool true -> limit reached, false -> limit not reached
  1106. */
  1107. function siteBuilderCheckLimit($params, $type) {
  1108. $siteBuilder = new siteBuilder_Admin($params['serverhostname'], $params['serveraccesshash']);
  1109. $response = $siteBuilder->getQuota($params['username']);
  1110. if($response[$type]['sw'] < 1) {
  1111. return true;
  1112. }
  1113. return false;
  1114. }
  1115. /**
  1116. * Returns API Url .
  1117. *
  1118. * @param string $params common module parameters
  1119. * @param string $user
  1120. * @param string $params common module parameters
  1121. *
  1122. * @return string $apiUrl
  1123. */
  1124. function getSiteBuilderApiURL($params) {
  1125. $httpPrefix = $params['serversecure'] ? 'https://' : 'http://';
  1126. $serverPort = $params['serverport'] ? ':' . $params['serverport'] . '/' : '/';
  1127. return $httpPrefix . $params['serverhostname'] . $serverPort;
  1128. }