siteBuilder.php 35 KB

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