siteBuilder.php 35 KB

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