siteBuilder.php 35 KB

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