siteBuilder.php 34 KB

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