siteBuilder.php 34 KB

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