siteBuilder.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601
  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. return 'success';
  135. }
  136. /**
  137. * Removes a siteBuilder account.
  138. *
  139. * Called when a termination is requested. This can be invoked automatically for
  140. * overdue products if enabled, or requested manually by an admin user.
  141. *
  142. * @param array $params common module parameters
  143. *
  144. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  145. *
  146. * @return string 'success' or an error message
  147. */
  148. function siteBuilder_TerminateAccount($params) {
  149. try {
  150. $active = Capsule::table('sitePro_acc')
  151. ->where('account',$params['username'])
  152. ->value('enabled');
  153. } catch (\Exception $e) {
  154. logModuleCall(
  155. 'siteBuilder',
  156. __FUNCTION__,
  157. $params,
  158. 'Error: could remove account from database',
  159. $e->getMessage()
  160. );
  161. return 'Error: could remove account from database';
  162. }
  163. if($active == true) {
  164. return 'Error: Account is active, please suspend account first';
  165. }
  166. try {
  167. Capsule::table('sitePro_site')
  168. ->where('relid',$params['serviceid'])
  169. ->delete();
  170. } catch (\Exception $e) {
  171. logModuleCall(
  172. 'siteBuilder',
  173. __FUNCTION__,
  174. $params,
  175. 'Error: could remove domains from database',
  176. $e->getMessage()
  177. );
  178. return 'Error: could remove domains from database';
  179. }
  180. try {
  181. Capsule::table('sitePro_acc')
  182. ->where('account',$params['username'])
  183. ->delete();
  184. } catch (\Exception $e) {
  185. logModuleCall(
  186. 'siteBuilder',
  187. __FUNCTION__,
  188. $params,
  189. 'Error: could remove account from database',
  190. $e->getMessage()
  191. );
  192. return 'Error: could remove account from database';
  193. }
  194. $siteBuilder = new ApiClient(getSiteBuilderApiURL($params), $params['serveraccesshash']);
  195. $response = $siteBuilder->terminate($params['domain'],$params['username']);
  196. if($response['status'] != '200') {
  197. return 'Error: ' . $response['response'];
  198. }
  199. return 'success';
  200. }
  201. /**
  202. * Set a siteBuilder account to status inactive.
  203. *
  204. * Called when a suspension is requested. This is invoked automatically by WHMCS
  205. * when a product becomes overdue on payment or can be called manually by admin
  206. * user.
  207. *
  208. * @param array $params common module parameters
  209. *
  210. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  211. *
  212. * @return string 'success' or an error message
  213. */
  214. function siteBuilder_SuspendAccount($params) {
  215. $siteBuilder = new ApiClient(getSiteBuilderApiURL($params), $params['serveraccesshash']);
  216. $status = $siteBuilder->isprodenabled($params['domain'],$params['username']);
  217. if($status['status'] != '200') {
  218. return 'Error: ' . $status['error_msg'];
  219. }
  220. if($response['response']['isenabled'] == 'YES'){
  221. $response = $siteBuilder->disableprod($params['domain'],$params['username']);
  222. if($response['status'] != '200') {
  223. return 'Error: ' . $response['error_msg'];
  224. }
  225. }
  226. try {
  227. Capsule::table('sitePro_acc')
  228. ->where('account',$params['username'])
  229. ->update(array(
  230. 'enabled' => false,
  231. ));
  232. } catch (\Exception $e) {
  233. logModuleCall(
  234. 'siteBuilder',
  235. __FUNCTION__,
  236. $params,
  237. 'Error: could remove account from database',
  238. $e->getMessage()
  239. );
  240. return 'Error: could remove account from database';
  241. }
  242. return 'success';
  243. }
  244. /**
  245. * Set a siteBuilder account to status active.
  246. *
  247. * Called when an un-suspension is requested. This is invoked
  248. * automatically upon payment of an overdue invoice for a product, or
  249. * can be called manually by admin user.
  250. *
  251. * @param array $params common module parameters
  252. *
  253. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  254. *
  255. * @return string 'success' or an error message
  256. */
  257. function siteBuilder_UnsuspendAccount($params) {
  258. try {
  259. Capsule::table('sitePro_acc')
  260. ->where('account',$params['username'])
  261. ->update(array(
  262. 'enabled' => true,
  263. ));
  264. } catch (\Exception $e) {
  265. logModuleCall(
  266. 'siteBuilder',
  267. __FUNCTION__,
  268. $params,
  269. 'Error: could remove account from database',
  270. $e->getMessage()
  271. );
  272. return 'Error: could remove account from database';
  273. }
  274. return 'success';
  275. }
  276. /**
  277. * Client area output logic handling.
  278. *
  279. * This function is used to define module specific client area output. It should
  280. * return an array consisting of a template file and optional additional
  281. * template variables to make available to that template.
  282. *
  283. * @param array $params common module parameters
  284. *
  285. * @see https://developers.whmcs.com/provisioning-modules/client-area-output/
  286. *
  287. * @return array
  288. */
  289. function siteBuilder_ClientArea($params) {
  290. $clientInfo = array('moduleclientarea' => '1');
  291. $clientInfo['domain'] = $params['domain'];
  292. $accountObj = Capsule::table('sitePro_acc')
  293. ->where('pid', $params['serviceid'])
  294. ->get();
  295. $sitesObj = Capsule::table('sitePro_site')
  296. ->where('relid', $params['serviceid'])
  297. ->get();
  298. $clientInfo['sites'] = [];
  299. foreach($sitesObj as $site){
  300. array_push($clientInfo['sites'],['name' => $site->name, 'ssl' => '123']);
  301. }
  302. return array(
  303. 'tabOverviewReplacementTemplate' => 'clientarea',
  304. 'vars' => $clientInfo,
  305. );
  306. }
  307. /**
  308. * Perform single sign-on for a siteBuilder account.
  309. *
  310. * When successful, returns a URL to which the user should be redirected.
  311. *
  312. * @param array $params common module parameters
  313. *
  314. * @see https://developers.whmcs.com/provisioning-modules/single-sign-on/
  315. *
  316. * @return array
  317. */
  318. function siteBuilder_ServiceSingleSignOn($params) {
  319. }
  320. /**
  321. * Upgrade or downgrade a siteBuilder account by package.
  322. *
  323. * Called to apply any change in product assignment or parameters. It
  324. * is called to provision upgrade or downgrade orders, as well as being
  325. * able to be invoked manually by an admin user.
  326. *
  327. * This same function is called for upgrades and downgrades of both
  328. * products and configurable options.
  329. *
  330. * @param array $params common module parameters
  331. *
  332. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  333. *
  334. * @return string "success" or an error message
  335. */
  336. function siteBuilder_ChangePackage($params) {
  337. return 'success';
  338. }
  339. /**
  340. * Usage Update
  341. *
  342. * Important: Runs daily per server not per product
  343. * Run Manually: /admin/reports.php?report=disk_usage_summary&action=updatestats
  344. * @param array $params common module parameters
  345. *
  346. * @see https://developers.whmcs.com/provisioning-modules/usage-update/
  347. */
  348. function siteBuilder_UsageUpdate($params) {
  349. }
  350. /**
  351. * Additional actions a client user can invoke.
  352. *
  353. * Define additional actions a client user can perform for an instance of a
  354. * product/service.
  355. *
  356. * Any actions you define here will be automatically displayed in the available
  357. * list of actions within the client area.
  358. *
  359. * @return array
  360. */
  361. function siteBuilder_ClientAreaCustomButtonArray ($params) {
  362. return array(
  363. 'Neue Domain' => 'newDomain',
  364. );
  365. }
  366. /**
  367. * Additional actions a client user can invoke.
  368. *
  369. * Define additional actions a client user is allowed to perform for an instance of a
  370. * product/service.
  371. *
  372. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  373. *
  374. * @return array
  375. */
  376. function siteBuilder_ClientAreaAllowedFunctions() {
  377. return array(
  378. "Add Domain" => "addDomain",
  379. "new Domain" => "newDomain",
  380. "Add Subdomain" => "addSubdomain",
  381. "New Subdomain" => "newSubdomain",
  382. "Confirm Delete Domain" => "delDomainConfirm",
  383. "Delete Domain" => "delDomain",
  384. "Confirm Delete Subdomain" => "delSubdomainConfirm",
  385. "Delete Subdomain" => "delSubdomain",
  386. );
  387. }
  388. /**
  389. * Opens a form to add a new domain.
  390. *
  391. * @param array $params common module parameters
  392. *
  393. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  394. *
  395. * @return array template information
  396. */
  397. function siteBuilder_newDomain($params) {
  398. return array(
  399. 'breadcrumb' => array(
  400. 'clientarea.php?action=productdetails&id=' . $params['serviceid'] . '&modop=custom&a=newDomain' => 'Neue Domain',
  401. ),
  402. 'templatefile' => 'siteBuilder_add_domain',
  403. );
  404. }
  405. /**
  406. * Adds a new domain to a siteBuilder account.
  407. *
  408. * @param array $params common module parameters
  409. *
  410. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  411. *
  412. * @return string "success" or an error message
  413. */
  414. function siteBuilder_addDomain($params) {
  415. if(!filter_var($_POST['d'], FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME)){
  416. return 'Error: invalid domain name';
  417. }
  418. return 'success';
  419. }
  420. /**
  421. * Opens a form to add a new subdomain to a domain.
  422. *
  423. * @param array $params common module parameters
  424. *
  425. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  426. *
  427. * @return array template information
  428. */
  429. function siteBuilder_newSubdomain($params) {
  430. if(!filter_var($_POST['d'], FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME)){
  431. return 'Error: invalid domain name';
  432. }
  433. return array(
  434. 'breadcrumb' => array(
  435. 'clientarea.php?action=productdetails&id=' . $params['serviceid'] . '&modop=custom&a=newSubdomain' => 'Neue Subdomain',
  436. ),
  437. 'templatefile' => 'siteBuilder_add_subdomain',
  438. 'vars' => array(
  439. 'domainselected' => $_POST['d'],
  440. ),
  441. );
  442. }
  443. /**
  444. * Adds a new subdomain to domain of a siteBuilder account.
  445. *
  446. * @param array $params common module parameters
  447. *
  448. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  449. *
  450. * @return string "success" or an error message
  451. */
  452. function siteBuilder_addSubdomain($params) {
  453. if(!filter_var($_POST['d'], FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME)){
  454. return 'Error: invalid domain name';
  455. }
  456. if(!filter_var($_POST['s'], FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME)){
  457. return 'Error: invalid subdomain name';
  458. }
  459. if($_POST['s'] == 'www') {
  460. return 'Error: default Subdomain www wurde bereits automatisch erstellt' ;
  461. }
  462. return 'success';
  463. }
  464. /**
  465. * Opens a form to delete a domain from a siteBuilder account.
  466. *
  467. * @param array $params common module parameters
  468. *
  469. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  470. *
  471. * @return array template information
  472. */
  473. function siteBuilder_delDomainConfirm($params) {
  474. return array(
  475. 'templatefile' => 'siteBuilder_del_domain_confirm',
  476. 'vars' => array(
  477. 'deldomain' => $_POST['d'],
  478. ),
  479. );
  480. }
  481. /**
  482. * Removes a domain from a siteBuilder account.
  483. *
  484. * @param array $params common module parameters
  485. *
  486. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  487. *
  488. * @return string "success" or an error message
  489. */
  490. function siteBuilder_delDomain($params) {
  491. if(!filter_var($_POST['d'], FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME)){
  492. return 'Error: invalid domain name';
  493. }
  494. return 'success';
  495. }
  496. /**
  497. * Opens a form to delete a subdomain from domain of a siteBuilder account.
  498. *
  499. * @param array $params common module parameters
  500. *
  501. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  502. *
  503. * @return array template information
  504. */
  505. function siteBuilder_delSubdomainConfirm($params) {
  506. return array(
  507. 'templatefile' => 'siteBuilder_del_subdomain_confirm',
  508. 'vars' => array(
  509. 'delsubdomain' => $_POST['d'],
  510. ),
  511. );
  512. }
  513. /**
  514. * Removes a subdomain from a domain of a siteBuilder account.
  515. *
  516. * @param array $params common module parameters
  517. *
  518. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  519. *
  520. * @return string "success" or an error message
  521. */
  522. function siteBuilder_delSubdomain($params) {
  523. if(!filter_var($_POST['d'], FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME)){
  524. return 'Error: invalid domain name';
  525. }
  526. return 'success';
  527. }
  528. /**
  529. * Returns API Url .
  530. *
  531. * @param string $params common module parameters
  532. * @param string $user
  533. * @param string $params common module parameters
  534. *
  535. * @return string $apiUrl
  536. */
  537. function getSiteBuilderApiURL($params) {
  538. $httpPrefix = $params['serversecure'] ? 'https://' : 'http://';
  539. $serverPort = $params['serverport'] ? ':' . $params['serverport'] . '/' : '/';
  540. return $httpPrefix . $params['serverhostname'] . $serverPort;
  541. }
  542. function siteBuilderCreateTables() {
  543. // Create a new table.
  544. if (!Capsule::schema()->hasTable('sitePro_acc')) {
  545. try {
  546. Capsule::schema()->create(
  547. 'sitePro_acc',
  548. function ($table) {
  549. /** @var \Illuminate\Database\Schema\Blueprint $table */
  550. $table->increments('id');
  551. $table->string('account');
  552. $table->integer('pid');
  553. $table->boolean('enabled');
  554. }
  555. );
  556. } catch (\Exception $e) {
  557. echo "Unable to create sitePro_acc: {$e->getMessage()}";
  558. }
  559. }
  560. if (!Capsule::schema()->hasTable('sitePro_site')) {
  561. try {
  562. Capsule::schema()->create(
  563. 'sitePro_site',
  564. function ($table) {
  565. /** @var \Illuminate\Database\Schema\Blueprint $table */
  566. $table->increments('id');
  567. $table->integer('relid');
  568. $table->string('name');
  569. $table->boolean('enabled');
  570. }
  571. );
  572. } catch (\Exception $e) {
  573. echo "Unable to create sitePro_site: {$e->getMessage()}";
  574. }
  575. }
  576. }