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