siteBuilder.php 34 KB

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