siteBuilder.php 17 KB

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