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. logModuleCall(
  337. 'siteBuilder',
  338. __FUNCTION__,
  339. $params,
  340. 'debug',
  341. $accountObj
  342. );
  343. $domainsObj = Capsule::table('sitePro_dom')
  344. ->where('relid', $params['serverid'])
  345. ->get();
  346. logModuleCall(
  347. 'siteBuilder',
  348. __FUNCTION__,
  349. $params,
  350. 'debug',
  351. $domainsObj
  352. );
  353. $clientInfo['domains'] = ['domain' => $params['domain']];
  354. return array(
  355. 'tabOverviewReplacementTemplate' => 'clientarea',
  356. 'vars' => $clientInfo,
  357. );
  358. }
  359. /**
  360. * Perform single sign-on for a siteBuilder account.
  361. *
  362. * When successful, returns a URL to which the user should be redirected.
  363. *
  364. * @param array $params common module parameters
  365. *
  366. * @see https://developers.whmcs.com/provisioning-modules/single-sign-on/
  367. *
  368. * @return array
  369. */
  370. function siteBuilder_ServiceSingleSignOn($params) {
  371. }
  372. /**
  373. * Upgrade or downgrade a siteBuilder account by package.
  374. *
  375. * Called to apply any change in product assignment or parameters. It
  376. * is called to provision upgrade or downgrade orders, as well as being
  377. * able to be invoked manually by an admin user.
  378. *
  379. * This same function is called for upgrades and downgrades of both
  380. * products and configurable options.
  381. *
  382. * @param array $params common module parameters
  383. *
  384. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  385. *
  386. * @return string "success" or an error message
  387. */
  388. function siteBuilder_ChangePackage($params) {
  389. return 'success';
  390. }
  391. /**
  392. * Usage Update
  393. *
  394. * Important: Runs daily per server not per product
  395. * Run Manually: /admin/reports.php?report=disk_usage_summary&action=updatestats
  396. * @param array $params common module parameters
  397. *
  398. * @see https://developers.whmcs.com/provisioning-modules/usage-update/
  399. */
  400. function siteBuilder_UsageUpdate($params) {
  401. }
  402. /**
  403. * Additional actions a client user can invoke.
  404. *
  405. * Define additional actions a client user can perform for an instance of a
  406. * product/service.
  407. *
  408. * Any actions you define here will be automatically displayed in the available
  409. * list of actions within the client area.
  410. *
  411. * @return array
  412. */
  413. function siteBuilder_ClientAreaCustomButtonArray ($params) {
  414. return array(
  415. 'Neue Domain' => 'newDomain',
  416. );
  417. }
  418. /**
  419. * Additional actions a client user can invoke.
  420. *
  421. * Define additional actions a client user is allowed to perform for an instance of a
  422. * product/service.
  423. *
  424. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  425. *
  426. * @return array
  427. */
  428. function siteBuilder_ClientAreaAllowedFunctions() {
  429. return array(
  430. "Add Domain" => "addDomain",
  431. "new Domain" => "newDomain",
  432. "Add Subdomain" => "addSubdomain",
  433. "New Subdomain" => "newSubdomain",
  434. "Confirm Delete Domain" => "delDomainConfirm",
  435. "Delete Domain" => "delDomain",
  436. "Confirm Delete Subdomain" => "delSubdomainConfirm",
  437. "Delete Subdomain" => "delSubdomain",
  438. );
  439. }
  440. /**
  441. * Opens a form to add a new domain.
  442. *
  443. * @param array $params common module parameters
  444. *
  445. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  446. *
  447. * @return array template information
  448. */
  449. function siteBuilder_newDomain($params) {
  450. return array(
  451. 'breadcrumb' => array(
  452. 'clientarea.php?action=productdetails&id=' . $params['serviceid'] . '&modop=custom&a=newDomain' => 'Neue Domain',
  453. ),
  454. 'templatefile' => 'siteBuilder_add_domain',
  455. );
  456. }
  457. /**
  458. * Adds a new domain to a siteBuilder account.
  459. *
  460. * @param array $params common module parameters
  461. *
  462. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  463. *
  464. * @return string "success" or an error message
  465. */
  466. function siteBuilder_addDomain($params) {
  467. if(!filter_var($_POST['d'], FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME)){
  468. return 'Error: invalid domain name';
  469. }
  470. return 'success';
  471. }
  472. /**
  473. * Opens a form to add a new subdomain to a domain.
  474. *
  475. * @param array $params common module parameters
  476. *
  477. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  478. *
  479. * @return array template information
  480. */
  481. function siteBuilder_newSubdomain($params) {
  482. if(!filter_var($_POST['d'], FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME)){
  483. return 'Error: invalid domain name';
  484. }
  485. return array(
  486. 'breadcrumb' => array(
  487. 'clientarea.php?action=productdetails&id=' . $params['serviceid'] . '&modop=custom&a=newSubdomain' => 'Neue Subdomain',
  488. ),
  489. 'templatefile' => 'siteBuilder_add_subdomain',
  490. 'vars' => array(
  491. 'domainselected' => $_POST['d'],
  492. ),
  493. );
  494. }
  495. /**
  496. * Adds a new subdomain to domain of a siteBuilder account.
  497. *
  498. * @param array $params common module parameters
  499. *
  500. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  501. *
  502. * @return string "success" or an error message
  503. */
  504. function siteBuilder_addSubdomain($params) {
  505. if(!filter_var($_POST['d'], FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME)){
  506. return 'Error: invalid domain name';
  507. }
  508. if(!filter_var($_POST['s'], FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME)){
  509. return 'Error: invalid subdomain name';
  510. }
  511. if($_POST['s'] == 'www') {
  512. return 'Error: default Subdomain www wurde bereits automatisch erstellt' ;
  513. }
  514. return 'success';
  515. }
  516. /**
  517. * Opens a form to delete a domain from a siteBuilder account.
  518. *
  519. * @param array $params common module parameters
  520. *
  521. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  522. *
  523. * @return array template information
  524. */
  525. function siteBuilder_delDomainConfirm($params) {
  526. return array(
  527. 'templatefile' => 'siteBuilder_del_domain_confirm',
  528. 'vars' => array(
  529. 'deldomain' => $_POST['d'],
  530. ),
  531. );
  532. }
  533. /**
  534. * Removes a domain from a siteBuilder account.
  535. *
  536. * @param array $params common module parameters
  537. *
  538. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  539. *
  540. * @return string "success" or an error message
  541. */
  542. function siteBuilder_delDomain($params) {
  543. if(!filter_var($_POST['d'], FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME)){
  544. return 'Error: invalid domain name';
  545. }
  546. return 'success';
  547. }
  548. /**
  549. * Opens a form to delete a subdomain from domain of a siteBuilder account.
  550. *
  551. * @param array $params common module parameters
  552. *
  553. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  554. *
  555. * @return array template information
  556. */
  557. function siteBuilder_delSubdomainConfirm($params) {
  558. return array(
  559. 'templatefile' => 'siteBuilder_del_subdomain_confirm',
  560. 'vars' => array(
  561. 'delsubdomain' => $_POST['d'],
  562. ),
  563. );
  564. }
  565. /**
  566. * Removes a subdomain from a domain of a siteBuilder account.
  567. *
  568. * @param array $params common module parameters
  569. *
  570. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  571. *
  572. * @return string "success" or an error message
  573. */
  574. function siteBuilder_delSubdomain($params) {
  575. if(!filter_var($_POST['d'], FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME)){
  576. return 'Error: invalid domain name';
  577. }
  578. return 'success';
  579. }
  580. /**
  581. * Returns API Url .
  582. *
  583. * @param string $params common module parameters
  584. * @param string $user
  585. * @param string $params common module parameters
  586. *
  587. * @return string $apiUrl
  588. */
  589. function getSiteBuilderApiURL($params) {
  590. $httpPrefix = $params['serversecure'] ? 'https://' : 'http://';
  591. $serverPort = $params['serverport'] ? ':' . $params['serverport'] . '/' : '/';
  592. return $httpPrefix . $params['serverhostname'] . $serverPort;
  593. }
  594. function siteBuilderCreateTables() {
  595. // Create a new table.
  596. if (!Capsule::schema()->hasTable('sitePro_acc')) {
  597. try {
  598. Capsule::schema()->create(
  599. 'sitePro_acc',
  600. function ($table) {
  601. /** @var \Illuminate\Database\Schema\Blueprint $table */
  602. $table->increments('id');
  603. $table->string('account');
  604. $table->integer('pid');
  605. $table->boolean('enabled');
  606. }
  607. );
  608. } catch (\Exception $e) {
  609. echo "Unable to create sitePro_acc: {$e->getMessage()}";
  610. }
  611. }
  612. if (!Capsule::schema()->hasTable('sitePro_dom')) {
  613. try {
  614. Capsule::schema()->create(
  615. 'sitePro_dom',
  616. function ($table) {
  617. /** @var \Illuminate\Database\Schema\Blueprint $table */
  618. $table->increments('id');
  619. $table->integer('relid');
  620. $table->string('domain');
  621. $table->boolean('enabled');
  622. }
  623. );
  624. } catch (\Exception $e) {
  625. echo "Unable to create sitePro_dom: {$e->getMessage()}";
  626. }
  627. }
  628. }