siteBuilder.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659
  1. <?php
  2. /**
  3. * WHMCS siteBuilder Provisioning Module
  4. *
  5. * Provisioning for User Account on the siteBuilder Server
  6. *
  7. * @see https://centos-webpanel.com/
  8. * @copyright Copyright (c) Thurdata GmbH 2022
  9. * @license GPL
  10. */
  11. use WHMCS\Database\Capsule;
  12. require_once 'Net/DNS2.php';
  13. require_once(__DIR__ . '/api/sitebuilder.php');
  14. if (!defined('WHMCS')) {
  15. die('This file cannot be accessed directly');
  16. }
  17. /**
  18. * Define siteBuilder product metadata parameters.
  19. *
  20. * @see https://developers.whmcs.com/provisioning-modules/meta-data-params/
  21. *
  22. * @return array
  23. */
  24. function siteBuilder_MetaData() {
  25. return array(
  26. 'DisplayName' => 'ThurData SiteBuilder Provisioning',
  27. 'APIVersion' => '1.2',
  28. 'DefaultNonSSLPort' => '80',
  29. 'DefaultSSLPort' => '443',
  30. 'RequiresServer' => true,
  31. 'ServiceSingleSignOnLabel' => 'Login to siteBuilder',
  32. 'AdminSingleSignOnLabel' => 'Login to siteBuilder Admin'
  33. );
  34. }
  35. function siteBuilder_ConfigOptions() {
  36. siteBuilderCreateTables();
  37. return ["BuilderURL" => [
  38. "FriendlyName" => "Builder URL", # Full Builder URL (prefix//hostname:port/)
  39. "Type" => "text", # Text Box
  40. "Size" => "25", # Defines the Field Width
  41. "Description" => "Full Builder URL (prefix//hostname:port/)",
  42. "Default" => "https://builder.thurdata.ch/",
  43. ],
  44. ];
  45. }
  46. /**
  47. * Test connection to a siteBuilder server with the given server parameters.
  48. *
  49. * Allows an admin user to verify that an API connection can be
  50. * successfully made with the given configuration parameters for a
  51. * server.
  52. *
  53. * When defined in a module, a test connection button will appear
  54. * alongside the server type dropdown when adding or editing an
  55. * existing server.
  56. *
  57. * @param array $params common module parameters
  58. *
  59. * @see https://developers.whmcs.com/provisioning-modules/module-parameters/
  60. *
  61. * @return array
  62. */
  63. function siteBuilder_Testconnection($params) {
  64. $siteBuilder = new ApiClient(getSiteBuilderApiURL($params), $params['serveraccesshash']);
  65. $response = $siteBuilder->ping($params['serverusername'], $params['serverpassword']);
  66. if($response['response']['answer'] == 'pong') {
  67. return array(
  68. 'success' => true,
  69. 'error' => '',
  70. );
  71. }
  72. return array(
  73. 'success' => false,
  74. 'error' => $response,
  75. );
  76. }
  77. /**
  78. * Provision a new account of a siteBuilder server.
  79. *
  80. * Attempt to provision a new siteBuilder account. This is
  81. * called any time provisioning is requested inside of WHMCS. Depending upon the
  82. * configuration, this can be any of:
  83. * * When a new order is placed
  84. * * When an invoice for a new order is paid
  85. * * Upon manual request by an admin user
  86. *
  87. * @param array $params common module parameters
  88. *
  89. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  90. *
  91. * @return string 'success' or an error message
  92. */
  93. function siteBuilder_CreateAccount($params) {
  94. $username = strtolower(substr($params['clientsdetails']['firstname'],0,2) . substr($params['clientsdetails']['lastname'],0,3)) . $params['serviceid'];
  95. $userdomain = $params['domain'];
  96. try {
  97. Capsule::table('tblhosting')
  98. ->where('id', '=', $params['serviceid'])
  99. ->update(
  100. array(
  101. 'username' => $username,
  102. 'domain' => $userdomain,
  103. )
  104. );
  105. } catch (\Exception $e) {
  106. logModuleCall(
  107. 'siteBuilder',
  108. __FUNCTION__,
  109. $params,
  110. 'Error: could save username & domain in database',
  111. $e->getMessage()
  112. );
  113. return 'Error: could save username & password in database';
  114. }
  115. try {
  116. Capsule::table('sitePro_acc')
  117. ->insert(
  118. array(
  119. 'account' => $username,
  120. 'pid' => $params['serviceid'],
  121. 'enabled' => true,
  122. )
  123. );
  124. } catch (\Exception $e) {
  125. logModuleCall(
  126. 'siteBuilder',
  127. __FUNCTION__,
  128. $params,
  129. 'Error: could save username & serviceid in database',
  130. $e->getMessage()
  131. );
  132. return 'Error: could save username & serviceid in database';
  133. }
  134. try {
  135. Capsule::table('sitePro_dom')
  136. ->insert(
  137. array(
  138. 'relid' => $params['serviceid'],
  139. 'domain' => $userdomain,
  140. 'enabled' => true,
  141. )
  142. );
  143. } catch (\Exception $e) {
  144. logModuleCall(
  145. 'siteBuilder',
  146. __FUNCTION__,
  147. $params,
  148. 'Error: could save domain & serviceid in database',
  149. $e->getMessage()
  150. );
  151. return 'Error: could save domain & serviceid in database';
  152. }
  153. if ($params["server"] == 1) {
  154. $siteBuilder = new ApiClient(getSiteBuilderApiURL($params), $params['serveraccesshash']);
  155. $response = $siteBuilder->deployDev($username, $userdomain, $params['serverusername'], $params['serverpassword']);
  156. }
  157. logModuleCall(
  158. 'siteBuilder',
  159. __FUNCTION__,
  160. $params,
  161. 'debug',
  162. $response
  163. );
  164. if($response['status'] != '200') {
  165. return 'Error: ' . $response['response'];
  166. }
  167. return 'success';
  168. }
  169. /**
  170. * Removes a siteBuilder account.
  171. *
  172. * Called when a termination is requested. This can be invoked automatically for
  173. * overdue products if enabled, or requested manually by an admin user.
  174. *
  175. * @param array $params common module parameters
  176. *
  177. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  178. *
  179. * @return string 'success' or an error message
  180. */
  181. function siteBuilder_TerminateAccount($params) {
  182. try {
  183. $active = Capsule::table('sitePro_acc')
  184. ->where('account',$params['username'])
  185. ->value('enabled');
  186. } catch (\Exception $e) {
  187. logModuleCall(
  188. 'siteBuilder',
  189. __FUNCTION__,
  190. $params,
  191. 'Error: could remove account from database',
  192. $e->getMessage()
  193. );
  194. return 'Error: could remove account from database';
  195. }
  196. if($active == true) {
  197. return 'Error: Account is active, please suspend account first';
  198. }
  199. try {
  200. Capsule::table('sitePro_dom')
  201. ->where('relid',$params['serviceid'])
  202. ->delete();
  203. } catch (\Exception $e) {
  204. logModuleCall(
  205. 'siteBuilder',
  206. __FUNCTION__,
  207. $params,
  208. 'Error: could remove domains from database',
  209. $e->getMessage()
  210. );
  211. return 'Error: could remove domains from database';
  212. }
  213. try {
  214. Capsule::table('sitePro_acc')
  215. ->where('account',$params['username'])
  216. ->delete();
  217. } catch (\Exception $e) {
  218. logModuleCall(
  219. 'siteBuilder',
  220. __FUNCTION__,
  221. $params,
  222. 'Error: could remove account from database',
  223. $e->getMessage()
  224. );
  225. return 'Error: could remove account from database';
  226. }
  227. $siteBuilder = new ApiClient(getSiteBuilderApiURL($params), $params['serveraccesshash']);
  228. $response = $siteBuilder->terminate($params['domain'],$params['username']);
  229. if($response['status'] != '200') {
  230. return 'Error: ' . $response['response'];
  231. }
  232. return 'success';
  233. }
  234. /**
  235. * Set a siteBuilder account to status inactive.
  236. *
  237. * Called when a suspension is requested. This is invoked automatically by WHMCS
  238. * when a product becomes overdue on payment or can be called manually by admin
  239. * user.
  240. *
  241. * @param array $params common module parameters
  242. *
  243. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  244. *
  245. * @return string 'success' or an error message
  246. */
  247. function siteBuilder_SuspendAccount($params) {
  248. $siteBuilder = new ApiClient(getSiteBuilderApiURL($params), $params['serveraccesshash']);
  249. $status = $siteBuilder->isprodenabled($params['domain'],$params['username']);
  250. if($status['status'] != '200') {
  251. return 'Error: ' . $status['error_msg'];
  252. }
  253. if($response['response']['isenabled'] == 'YES'){
  254. $response = $siteBuilder->disableprod($params['domain'],$params['username']);
  255. if($response['status'] != '200') {
  256. return 'Error: ' . $response['error_msg'];
  257. }
  258. }
  259. try {
  260. Capsule::table('sitePro_acc')
  261. ->where('account',$params['username'])
  262. ->update(array(
  263. 'enabled' => false,
  264. ));
  265. } catch (\Exception $e) {
  266. logModuleCall(
  267. 'siteBuilder',
  268. __FUNCTION__,
  269. $params,
  270. 'Error: could remove account from database',
  271. $e->getMessage()
  272. );
  273. return 'Error: could remove account from database';
  274. }
  275. return 'success';
  276. }
  277. /**
  278. * Set a siteBuilder account to status active.
  279. *
  280. * Called when an un-suspension is requested. This is invoked
  281. * automatically upon payment of an overdue invoice for a product, or
  282. * can be called manually by admin user.
  283. *
  284. * @param array $params common module parameters
  285. *
  286. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  287. *
  288. * @return string 'success' or an error message
  289. */
  290. function siteBuilder_UnsuspendAccount($params) {
  291. try {
  292. Capsule::table('sitePro_acc')
  293. ->where('account',$params['username'])
  294. ->update(array(
  295. 'enabled' => true,
  296. ));
  297. } catch (\Exception $e) {
  298. logModuleCall(
  299. 'siteBuilder',
  300. __FUNCTION__,
  301. $params,
  302. 'Error: could remove account from database',
  303. $e->getMessage()
  304. );
  305. return 'Error: could remove account from database';
  306. }
  307. return 'success';
  308. }
  309. /**
  310. * Client area output logic handling.
  311. *
  312. * This function is used to define module specific client area output. It should
  313. * return an array consisting of a template file and optional additional
  314. * template variables to make available to that template.
  315. *
  316. * @param array $params common module parameters
  317. *
  318. * @see https://developers.whmcs.com/provisioning-modules/client-area-output/
  319. *
  320. * @return array
  321. */
  322. function siteBuilder_ClientArea($params) {
  323. $clientInfo = array('moduleclientarea' => '1');
  324. $siteBuilder = new ApiClient(getSiteBuilderApiURL($params), $params['serveraccesshash']);
  325. $responseSSL = $siteBuilder->getSSLDays($params['domain'],$params['username']);
  326. logModuleCall(
  327. 'siteBuilder',
  328. __FUNCTION__,
  329. $params,
  330. 'debug',
  331. $responseSSL
  332. );
  333. $accountObj = Capsule::table('sitePro_acc')
  334. ->where('pid', $params['serverid'])
  335. ->get();
  336. foreach($accountObj as $account){
  337. logModuleCall(
  338. 'siteBuilder',
  339. __FUNCTION__,
  340. $params,
  341. 'debug',
  342. $account
  343. );
  344. }
  345. $domainsObj = Capsule::table('sitePro_dom')
  346. ->where('relid', $params['serverid'])
  347. ->get();
  348. foreach($domainsObj as $domain){
  349. logModuleCall(
  350. 'siteBuilder',
  351. __FUNCTION__,
  352. $params,
  353. 'debug',
  354. $domain
  355. );
  356. }
  357. $clientInfo['domains'] = ['domain' => $params['domain']];
  358. return array(
  359. 'tabOverviewReplacementTemplate' => 'clientarea',
  360. 'vars' => $clientInfo,
  361. );
  362. }
  363. /**
  364. * Perform single sign-on for a siteBuilder account.
  365. *
  366. * When successful, returns a URL to which the user should be redirected.
  367. *
  368. * @param array $params common module parameters
  369. *
  370. * @see https://developers.whmcs.com/provisioning-modules/single-sign-on/
  371. *
  372. * @return array
  373. */
  374. function siteBuilder_ServiceSingleSignOn($params) {
  375. }
  376. /**
  377. * Upgrade or downgrade a siteBuilder account by package.
  378. *
  379. * Called to apply any change in product assignment or parameters. It
  380. * is called to provision upgrade or downgrade orders, as well as being
  381. * able to be invoked manually by an admin user.
  382. *
  383. * This same function is called for upgrades and downgrades of both
  384. * products and configurable options.
  385. *
  386. * @param array $params common module parameters
  387. *
  388. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  389. *
  390. * @return string "success" or an error message
  391. */
  392. function siteBuilder_ChangePackage($params) {
  393. return 'success';
  394. }
  395. /**
  396. * Usage Update
  397. *
  398. * Important: Runs daily per server not per product
  399. * Run Manually: /admin/reports.php?report=disk_usage_summary&action=updatestats
  400. * @param array $params common module parameters
  401. *
  402. * @see https://developers.whmcs.com/provisioning-modules/usage-update/
  403. */
  404. function siteBuilder_UsageUpdate($params) {
  405. }
  406. /**
  407. * Additional actions a client user can invoke.
  408. *
  409. * Define additional actions a client user can perform for an instance of a
  410. * product/service.
  411. *
  412. * Any actions you define here will be automatically displayed in the available
  413. * list of actions within the client area.
  414. *
  415. * @return array
  416. */
  417. function siteBuilder_ClientAreaCustomButtonArray ($params) {
  418. return array(
  419. 'Neue Domain' => 'newDomain',
  420. );
  421. }
  422. /**
  423. * Additional actions a client user can invoke.
  424. *
  425. * Define additional actions a client user is allowed to perform for an instance of a
  426. * product/service.
  427. *
  428. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  429. *
  430. * @return array
  431. */
  432. function siteBuilder_ClientAreaAllowedFunctions() {
  433. return array(
  434. "Add Domain" => "addDomain",
  435. "new Domain" => "newDomain",
  436. "Add Subdomain" => "addSubdomain",
  437. "New Subdomain" => "newSubdomain",
  438. "Confirm Delete Domain" => "delDomainConfirm",
  439. "Delete Domain" => "delDomain",
  440. "Confirm Delete Subdomain" => "delSubdomainConfirm",
  441. "Delete Subdomain" => "delSubdomain",
  442. );
  443. }
  444. /**
  445. * Opens a form to add a new domain.
  446. *
  447. * @param array $params common module parameters
  448. *
  449. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  450. *
  451. * @return array template information
  452. */
  453. function siteBuilder_newDomain($params) {
  454. return array(
  455. 'breadcrumb' => array(
  456. 'clientarea.php?action=productdetails&id=' . $params['serviceid'] . '&modop=custom&a=newDomain' => 'Neue Domain',
  457. ),
  458. 'templatefile' => 'siteBuilder_add_domain',
  459. );
  460. }
  461. /**
  462. * Adds a new domain to a siteBuilder account.
  463. *
  464. * @param array $params common module parameters
  465. *
  466. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  467. *
  468. * @return string "success" or an error message
  469. */
  470. function siteBuilder_addDomain($params) {
  471. if(!filter_var($_POST['d'], FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME)){
  472. return 'Error: invalid domain name';
  473. }
  474. return 'success';
  475. }
  476. /**
  477. * Opens a form to add a new subdomain to a domain.
  478. *
  479. * @param array $params common module parameters
  480. *
  481. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  482. *
  483. * @return array template information
  484. */
  485. function siteBuilder_newSubdomain($params) {
  486. if(!filter_var($_POST['d'], FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME)){
  487. return 'Error: invalid domain name';
  488. }
  489. return array(
  490. 'breadcrumb' => array(
  491. 'clientarea.php?action=productdetails&id=' . $params['serviceid'] . '&modop=custom&a=newSubdomain' => 'Neue Subdomain',
  492. ),
  493. 'templatefile' => 'siteBuilder_add_subdomain',
  494. 'vars' => array(
  495. 'domainselected' => $_POST['d'],
  496. ),
  497. );
  498. }
  499. /**
  500. * Adds a new subdomain to domain of a siteBuilder account.
  501. *
  502. * @param array $params common module parameters
  503. *
  504. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  505. *
  506. * @return string "success" or an error message
  507. */
  508. function siteBuilder_addSubdomain($params) {
  509. if(!filter_var($_POST['d'], FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME)){
  510. return 'Error: invalid domain name';
  511. }
  512. if(!filter_var($_POST['s'], FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME)){
  513. return 'Error: invalid subdomain name';
  514. }
  515. if($_POST['s'] == 'www') {
  516. return 'Error: default Subdomain www wurde bereits automatisch erstellt' ;
  517. }
  518. return 'success';
  519. }
  520. /**
  521. * Opens a form to delete a domain from a siteBuilder account.
  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_delDomainConfirm($params) {
  530. return array(
  531. 'templatefile' => 'siteBuilder_del_domain_confirm',
  532. 'vars' => array(
  533. 'deldomain' => $_POST['d'],
  534. ),
  535. );
  536. }
  537. /**
  538. * Removes a domain from a siteBuilder account.
  539. *
  540. * @param array $params common module parameters
  541. *
  542. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  543. *
  544. * @return string "success" or an error message
  545. */
  546. function siteBuilder_delDomain($params) {
  547. if(!filter_var($_POST['d'], FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME)){
  548. return 'Error: invalid domain name';
  549. }
  550. return 'success';
  551. }
  552. /**
  553. * Opens a form to delete a subdomain from domain of a siteBuilder account.
  554. *
  555. * @param array $params common module parameters
  556. *
  557. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  558. *
  559. * @return array template information
  560. */
  561. function siteBuilder_delSubdomainConfirm($params) {
  562. return array(
  563. 'templatefile' => 'siteBuilder_del_subdomain_confirm',
  564. 'vars' => array(
  565. 'delsubdomain' => $_POST['d'],
  566. ),
  567. );
  568. }
  569. /**
  570. * Removes a subdomain from a domain of a siteBuilder account.
  571. *
  572. * @param array $params common module parameters
  573. *
  574. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  575. *
  576. * @return string "success" or an error message
  577. */
  578. function siteBuilder_delSubdomain($params) {
  579. if(!filter_var($_POST['d'], FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME)){
  580. return 'Error: invalid domain name';
  581. }
  582. return 'success';
  583. }
  584. /**
  585. * Returns API Url .
  586. *
  587. * @param string $params common module parameters
  588. * @param string $user
  589. * @param string $params common module parameters
  590. *
  591. * @return string $apiUrl
  592. */
  593. function getSiteBuilderApiURL($params) {
  594. $httpPrefix = $params['serversecure'] ? 'https://' : 'http://';
  595. $serverPort = $params['serverport'] ? ':' . $params['serverport'] . '/' : '/';
  596. return $httpPrefix . $params['serverhostname'] . $serverPort;
  597. }
  598. function siteBuilderCreateTables() {
  599. // Create a new table.
  600. if (!Capsule::schema()->hasTable('sitePro_acc')) {
  601. try {
  602. Capsule::schema()->create(
  603. 'sitePro_acc',
  604. function ($table) {
  605. /** @var \Illuminate\Database\Schema\Blueprint $table */
  606. $table->increments('id');
  607. $table->string('account');
  608. $table->integer('pid');
  609. $table->boolean('enabled');
  610. }
  611. );
  612. } catch (\Exception $e) {
  613. echo "Unable to create sitePro_acc: {$e->getMessage()}";
  614. }
  615. }
  616. if (!Capsule::schema()->hasTable('sitePro_dom')) {
  617. try {
  618. Capsule::schema()->create(
  619. 'sitePro_dom',
  620. function ($table) {
  621. /** @var \Illuminate\Database\Schema\Blueprint $table */
  622. $table->increments('id');
  623. $table->integer('relid');
  624. $table->string('domain');
  625. $table->boolean('enabled');
  626. }
  627. );
  628. } catch (\Exception $e) {
  629. echo "Unable to create sitePro_dom: {$e->getMessage()}";
  630. }
  631. }
  632. }