siteBuilder.php 35 KB

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