siteBuilder.php 17 KB

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