siteBuilder.php 35 KB

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