siteBuilder.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805
  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. require_once(__DIR__ . '/api/SiteProApiClient.php');
  15. if (!defined('WHMCS')) {
  16. die('This file cannot be accessed directly');
  17. }
  18. /**
  19. * Define siteBuilder product metadata parameters.
  20. *
  21. * @see https://developers.whmcs.com/provisioning-modules/meta-data-params/
  22. *
  23. * @return array
  24. */
  25. function siteBuilder_MetaData() {
  26. return array(
  27. 'DisplayName' => 'ThurData SiteBuilder Provisioning',
  28. 'APIVersion' => '1.2',
  29. 'DefaultNonSSLPort' => '80',
  30. 'DefaultSSLPort' => '443',
  31. 'RequiresServer' => true,
  32. 'ServiceSingleSignOnLabel' => 'Login to siteBuilder',
  33. 'AdminSingleSignOnLabel' => 'Login to siteBuilder Admin'
  34. );
  35. }
  36. function siteBuilder_ConfigOptions() {
  37. siteBuilderCreateTables();
  38. return ["BuilderURL" => [
  39. "FriendlyName" => "Builder URL", # Full Builder URL (prefix//hostname:port/)
  40. "Type" => "text", # Text Box
  41. "Size" => "25", # Defines the Field Width
  42. "Description" => "Full Builder URL (prefix//hostname:port/)",
  43. "Default" => "https://builder.thurdata.ch/",
  44. ],
  45. ];
  46. }
  47. /**
  48. * Test connection to a siteBuilder server with the given server parameters.
  49. *
  50. * Allows an admin user to verify that an API connection can be
  51. * successfully made with the given configuration parameters for a
  52. * server.
  53. *
  54. * When defined in a module, a test connection button will appear
  55. * alongside the server type dropdown when adding or editing an
  56. * existing server.
  57. *
  58. * @param array $params common module parameters
  59. *
  60. * @see https://developers.whmcs.com/provisioning-modules/module-parameters/
  61. *
  62. * @return array
  63. */
  64. function siteBuilder_Testconnection($params) {
  65. $siteBuilder = new ApiClient(getSiteBuilderApiURL($params), $params['serveraccesshash']);
  66. $response = $siteBuilder->ping($params['serverusername'], $params['serverpassword']);
  67. if($response['response']['answer'] == 'pong') {
  68. return array(
  69. 'success' => true,
  70. 'error' => '',
  71. );
  72. }
  73. return array(
  74. 'success' => false,
  75. 'error' => $response,
  76. );
  77. }
  78. /**
  79. * Provision a new account of a siteBuilder server.
  80. *
  81. * Attempt to provision a new siteBuilder account. This is
  82. * called any time provisioning is requested inside of WHMCS. Depending upon the
  83. * configuration, this can be any of:
  84. * * When a new order is placed
  85. * * When an invoice for a new order is paid
  86. * * Upon manual request by an admin user
  87. *
  88. * @param array $params common module parameters
  89. *
  90. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  91. *
  92. * @return string 'success' or an error message
  93. */
  94. function siteBuilder_CreateAccount($params) {
  95. $username = strtolower(substr($params['clientsdetails']['firstname'],0,2) . substr($params['clientsdetails']['lastname'],0,3)) . $params['serviceid'];
  96. $userdomain = $params['domain'];
  97. try {
  98. Capsule::table('tblhosting')
  99. ->where('id', '=', $params['serviceid'])
  100. ->update(
  101. array(
  102. 'username' => $username,
  103. 'domain' => $userdomain,
  104. )
  105. );
  106. } catch (\Exception $e) {
  107. logModuleCall(
  108. 'siteBuilder',
  109. __FUNCTION__,
  110. $params,
  111. 'Error: could save username & domain in database',
  112. $e->getMessage()
  113. );
  114. return 'Error: could save username & password in database';
  115. }
  116. try {
  117. Capsule::table('sitePro_acc')
  118. ->insert(
  119. array(
  120. 'account' => $username,
  121. 'pid' => $params['serviceid'],
  122. 'enabled' => true,
  123. )
  124. );
  125. } catch (\Exception $e) {
  126. logModuleCall(
  127. 'siteBuilder',
  128. __FUNCTION__,
  129. $params,
  130. 'Error: could save username & serviceid in database',
  131. $e->getMessage()
  132. );
  133. return 'Error: could save username & serviceid in database';
  134. }
  135. $siteBuilder = new ApiClient(getSiteBuilderApiURL($params), $params['serveraccesshash']);
  136. $response = $siteBuilder->create($params['username'], $params['domain'], $params['serverusername'], $params['serverpassword']);
  137. if($response['status'] != '200') {
  138. return 'Error: ' . $response['response'];
  139. }
  140. $response = $siteBuilder->init($params['username'], $params['domain'], $params['serverusername'], $params['serverpassword']);
  141. if($response['status'] != '200') {
  142. return 'Error: ' . $response['response'];
  143. }
  144. return 'success';
  145. }
  146. /**
  147. * Removes a siteBuilder account.
  148. *
  149. * Called when a termination is requested. This can be invoked automatically for
  150. * overdue products if enabled, or requested manually by an admin user.
  151. *
  152. * @param array $params common module parameters
  153. *
  154. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  155. *
  156. * @return string 'success' or an error message
  157. */
  158. function siteBuilder_TerminateAccount($params) {
  159. try {
  160. $active = Capsule::table('sitePro_acc')
  161. ->where('account',$params['username'])
  162. ->value('enabled');
  163. } catch (\Exception $e) {
  164. logModuleCall(
  165. 'siteBuilder',
  166. __FUNCTION__,
  167. $params,
  168. 'Error: could fetch account from database',
  169. $e->getMessage()
  170. );
  171. return 'Error: could fetch account from database';
  172. }
  173. if($active == true) {
  174. return 'Error: Account is active, please suspend account first';
  175. }
  176. // undeploy all related sites
  177. $sites = getSites($params['serviceid']);
  178. $siteBuilder = new ApiClient(getSiteBuilderApiURL($params), $params['serveraccesshash']);
  179. if(!empty($sites)) {
  180. foreach($sites as $site) {
  181. /* $response = $siteBuilder->undeploy($params['username'], $site, $params['serverusername'], $params['serverpassword']);
  182. if($response['status'] != '200') {
  183. return 'Error: ' . $response['response'];
  184. } */
  185. logModuleCall(
  186. 'siteBuilder',
  187. __FUNCTION__,
  188. $params,
  189. 'debug',
  190. $site
  191. );
  192. }
  193. try {
  194. Capsule::table('sitePro_site')
  195. ->where('relid',$params['serviceid'])
  196. ->delete();
  197. } catch (\Exception $e) {
  198. logModuleCall(
  199. 'siteBuilder',
  200. __FUNCTION__,
  201. $params,
  202. 'Error: could remove domains from database',
  203. $e->getMessage()
  204. );
  205. return 'Error: could remove domains from database';
  206. }
  207. }
  208. // terminate account
  209. $response = $siteBuilder->terminate($params['username'], $params['domain']);
  210. if($response['status'] != '200') {
  211. return 'Error: ' . $response['response'];
  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. return 'success';
  228. }
  229. /**
  230. * Set a siteBuilder account to status inactive.
  231. *
  232. * Called when a suspension is requested. This is invoked automatically by WHMCS
  233. * when a product becomes overdue on payment or can be called manually by admin
  234. * user.
  235. *
  236. * @param array $params common module parameters
  237. *
  238. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  239. *
  240. * @return string 'success' or an error message
  241. */
  242. function siteBuilder_SuspendAccount($params) {
  243. $siteBuilder = new ApiClient(getSiteBuilderApiURL($params), $params['serveraccesshash']);
  244. $status = $siteBuilder->isenabled($params['username'],$params['domain']);
  245. if($status['status'] != '200') {
  246. return 'Error: ' . $status['error_msg'];
  247. }
  248. if($response['response']['isenabled'] == 'YES'){
  249. $response = $siteBuilder->disable($params['username'],$params['domain']);
  250. if($response['status'] != '200') {
  251. return 'Error: ' . $response['error_msg'];
  252. }
  253. }
  254. try {
  255. Capsule::table('sitePro_acc')
  256. ->where('account',$params['username'])
  257. ->update(array(
  258. 'enabled' => false,
  259. ));
  260. } catch (\Exception $e) {
  261. logModuleCall(
  262. 'siteBuilder',
  263. __FUNCTION__,
  264. $params,
  265. 'Error: could not disable account in database',
  266. $e->getMessage()
  267. );
  268. return 'Error: could not disable account in database';
  269. }
  270. // disable all sites but not change status in DB for restoring
  271. $sites = getSites($params['serviceid']);
  272. if(!empty($sites)) {
  273. foreach($sites as $site) {
  274. /* $response = $siteBuilder->disable($params['username'], $site, $params['serverusername'], $params['serverpassword']);
  275. if($response['status'] != '200') {
  276. return 'Error: ' . $response['response'];
  277. } */
  278. logModuleCall(
  279. 'siteBuilder',
  280. __FUNCTION__,
  281. $params,
  282. 'debug',
  283. $site
  284. );
  285. }
  286. }
  287. return 'success';
  288. }
  289. /**
  290. * Set a siteBuilder account to status active.
  291. *
  292. * Called when an un-suspension is requested. This is invoked
  293. * automatically upon payment of an overdue invoice for a product, or
  294. * can be called manually by admin user.
  295. *
  296. * @param array $params common module parameters
  297. *
  298. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  299. *
  300. * @return string 'success' or an error message
  301. */
  302. function siteBuilder_UnsuspendAccount($params) {
  303. try {
  304. Capsule::table('sitePro_acc')
  305. ->where('account',$params['username'])
  306. ->update(array(
  307. 'enabled' => true,
  308. ));
  309. } catch (\Exception $e) {
  310. logModuleCall(
  311. 'siteBuilder',
  312. __FUNCTION__,
  313. $params,
  314. 'Error: could update account in database',
  315. $e->getMessage()
  316. );
  317. return 'Error: could update account in database';
  318. }
  319. // enable active sites
  320. $sites = getSites($params['serviceid']);
  321. if(!empty($sites)) {
  322. foreach($sites as $site) {
  323. /* $response = $siteBuilder->enable($params['username'], $site, $params['serverusername'], $params['serverpassword']);
  324. if($response['status'] != '200') {
  325. return 'Error: ' . $response['response'];
  326. } */
  327. logModuleCall(
  328. 'siteBuilder',
  329. __FUNCTION__,
  330. $params,
  331. 'debug',
  332. $site
  333. );
  334. }
  335. }
  336. return 'success';
  337. }
  338. /**
  339. * Client area output logic handling.
  340. *
  341. * This function is used to define module specific client area output. It should
  342. * return an array consisting of a template file and optional additional
  343. * template variables to make available to that template.
  344. *
  345. * @param array $params common module parameters
  346. *
  347. * @see https://developers.whmcs.com/provisioning-modules/client-area-output/
  348. *
  349. * @return array
  350. */
  351. function siteBuilder_ClientArea($params) {
  352. $siteBuilder = new ApiClient(getSiteBuilderApiURL($params), $params['serveraccesshash']);
  353. $clientInfo = array('moduleclientarea' => '1');
  354. $clientInfo['domain'] = $params['domain'];
  355. $accountObj = Capsule::table('sitePro_acc')
  356. ->where('pid', $params['serviceid'])
  357. ->get();
  358. $sitesObj = Capsule::table('sitePro_site')
  359. ->where('relid', $params['serviceid'])
  360. ->get();
  361. $clientInfo['sites'] = [];
  362. $sslProd = 0;
  363. $sslDev = 0;
  364. foreach($sitesObj as $site){
  365. $response = $siteBuilder->getSSLDays($params['username'], $site->name);
  366. if($response['status'] == '200') {
  367. $sslProd = $response['response']['ssl_remaining'];
  368. }
  369. $response = $siteBuilder->getSSLDays($params['username'], 'dev.' . $site->name);
  370. if($response['status'] == '200') {
  371. $sslDev = $response['response']['ssl_remaining'];
  372. }
  373. array_push($clientInfo['sites'],['name' => $site->name, 'sslProd' => $sslProd, 'sslDev' => $sslDev]);
  374. }
  375. logModuleCall(
  376. 'siteBuilder',
  377. __FUNCTION__,
  378. $params,
  379. 'debug',
  380. $clientInfo
  381. );
  382. return array(
  383. 'tabOverviewReplacementTemplate' => 'clientarea',
  384. 'vars' => $clientInfo,
  385. );
  386. }
  387. /**
  388. * Perform single sign-on for a siteBuilder account.
  389. *
  390. * When successful, returns a URL to which the user should be redirected.
  391. *
  392. * @param array $params common module parameters
  393. *
  394. * @see https://developers.whmcs.com/provisioning-modules/single-sign-on/
  395. *
  396. * @return array
  397. */
  398. function siteBuilder_ServiceSingleSignOn($params) {
  399. }
  400. /**
  401. * Upgrade or downgrade a siteBuilder account by package.
  402. *
  403. * Called to apply any change in product assignment or parameters. It
  404. * is called to provision upgrade or downgrade orders, as well as being
  405. * able to be invoked manually by an admin user.
  406. *
  407. * This same function is called for upgrades and downgrades of both
  408. * products and configurable options.
  409. *
  410. * @param array $params common module parameters
  411. *
  412. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  413. *
  414. * @return string "success" or an error message
  415. */
  416. function siteBuilder_ChangePackage($params) {
  417. return 'success';
  418. }
  419. /**
  420. * Usage Update
  421. *
  422. * Important: Runs daily per server not per product
  423. * Run Manually: /admin/reports.php?report=disk_usage_summary&action=updatestats
  424. * @param array $params common module parameters
  425. *
  426. * @see https://developers.whmcs.com/provisioning-modules/usage-update/
  427. */
  428. function siteBuilder_UsageUpdate($params) {
  429. }
  430. /**
  431. * Additional actions a client user can invoke.
  432. *
  433. * Define additional actions a client user can perform for an instance of a
  434. * product/service.
  435. *
  436. * Any actions you define here will be automatically displayed in the available
  437. * list of actions within the client area.
  438. *
  439. * @return array
  440. */
  441. function siteBuilder_ClientAreaCustomButtonArray ($params) {
  442. return array(
  443. 'Neue Webseite' => 'newSite',
  444. );
  445. }
  446. /**
  447. * Additional actions a client user can invoke.
  448. *
  449. * Define additional actions a client user is allowed to perform for an instance of a
  450. * product/service.
  451. *
  452. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  453. *
  454. * @return array
  455. */
  456. function siteBuilder_ClientAreaAllowedFunctions() {
  457. return array(
  458. "Add Site" => "addSite",
  459. "new Site" => "newSite",
  460. "Confirm Delete Site" => "delSiteConfirm",
  461. "Delete Site" => "delSite",
  462. "Edit Site" => "editSite",
  463. 'Conform Revert Site' => 'revSiteConfirm',
  464. 'Revert Site' => 'revSite',
  465. 'Publish Site' => 'pubSite',
  466. 'Activate Prod' => 'enableProd',
  467. 'Deactivate Prod' => 'disableProd'
  468. );
  469. }
  470. /**
  471. * Opens a form to add a new domain.
  472. *
  473. * @param array $params common module parameters
  474. *
  475. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  476. *
  477. * @return array template information
  478. */
  479. function siteBuilder_newSite($params) {
  480. return array(
  481. 'breadcrumb' => array(
  482. 'clientarea.php?action=productdetails&id=' . $params['serviceid'] . '&modop=custom&a=newSite' => 'Neue Webseite',
  483. ),
  484. 'templatefile' => 'siteBuilder_new_site',
  485. );
  486. }
  487. /**
  488. * Adds a new domain to a siteBuilder account.
  489. *
  490. * @param array $params common module parameters
  491. *
  492. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  493. *
  494. * @return string "success" or an error message
  495. */
  496. function siteBuilder_addSite($params) {
  497. if(empty($_POST['d'])) {
  498. $site = $params['domain'];
  499. } else {
  500. if(!filter_var($_POST['d'], FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME)){
  501. return 'Error: invalid site name';
  502. }
  503. $site = $_POST['d'] . '.' . $params['domain'];
  504. }
  505. $siteBuilder = new ApiClient(getSiteBuilderApiURL($params), $params['serveraccesshash']);
  506. // init dev
  507. $response = $siteBuilder->init($params['username'], 'dev.' . $site, $params['serverusername'], $params['serverpassword']);
  508. if($response['status'] != '200') {
  509. return 'Error: ' . $response['response'];
  510. }
  511. // init prod
  512. $response = $siteBuilder->init($params['username'], $site, $params['serverusername'], $params['serverpassword']);
  513. if($response['status'] != '200') {
  514. return 'Error: ' . $response['response'];
  515. }
  516. // update DB
  517. try {
  518. Capsule::table('sitePro_site')
  519. ->insert(
  520. array(
  521. 'relid' => $params['serviceid'],
  522. 'name' => $site,
  523. 'enabled' => true,
  524. )
  525. );
  526. } catch (\Exception $e) {
  527. logModuleCall(
  528. 'siteBuilder',
  529. __FUNCTION__,
  530. $params,
  531. 'Error: could save site & serviceid in database',
  532. $e->getMessage()
  533. );
  534. return 'Error: could save site & serviceid in database';
  535. }
  536. return 'success';
  537. }
  538. function siteBuilder_editSite($params) {
  539. if(!filter_var($_POST['s'], FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME)){
  540. return 'Error: invalid site name';
  541. }
  542. $site = $_POST['s'];
  543. $api = new SiteProApiClient('https://builder.thurdata.ch/api/', 'apikey0', '993yVHwC05TLsx2JI2XFlAhkkPUxR6JbQUYbI.a5HiRtmNV9');
  544. // use this for enterprise licenses and change 'your-bulder-domain.com' to your builder domain
  545. //$api = new SiteProApiClient('http://your-bulder-domain.com/api/', 'your_api_username', 'your_api_password');
  546. try {
  547. // this call is used to open builder, so you need to set correct parameters to represent users website you want to open
  548. // this data usually comes from your user/hosting manager system
  549. $res = $api->remoteCall('requestLogin', array(
  550. 'type' => 'internal', // (required) 'internal'
  551. 'domain' => $site, // (required) domain of the user website you want to edit
  552. 'lang' => 'de', // (optional) 2-letter language code, set language code you whant builder to open in
  553. 'apiUrl' => getSiteBuilderApiURL($params) . 'deploy/' . $params['username'] . '/' . $site, // (required) API endpoint URL
  554. 'resellerClientAccountId' => $params['serviceid'], // (required) ID of website/user in your system
  555. // 'username' => 'example_user', // (optional) authorization username to be used with API endpoint
  556. // 'password' => 'example_password', // (optional) authorization password to be used with API endpoint
  557. ));
  558. if (!$res || !is_object($res)) {
  559. logModuleCall(
  560. 'siteBuilder',
  561. __FUNCTION__,
  562. $params,
  563. 'Error: Response format error',
  564. $res
  565. );
  566. return 'Error: Response format error';
  567. } else if (isset($res->url) && $res->url) {
  568. logModuleCall(
  569. 'siteBuilder',
  570. __FUNCTION__,
  571. $params,
  572. 'Debug',
  573. $res
  574. );
  575. // on success redirect to builder URL
  576. // header('Location: '.$res->url, true);
  577. // exit();
  578. } else {
  579. logModuleCall(
  580. 'siteBuilder',
  581. __FUNCTION__,
  582. $params,
  583. 'Error: Unknown error',
  584. $res
  585. );
  586. return 'Error: Unknown error';
  587. }
  588. } catch (\Exception $e) {
  589. logModuleCall(
  590. 'siteBuilder',
  591. __FUNCTION__,
  592. $params,
  593. 'Error: Request error',
  594. $e->getMessage()
  595. );
  596. return 'Error: Request error';
  597. }
  598. return 'success';
  599. }
  600. /**
  601. * Opens a form to delete a domain from a siteBuilder account.
  602. *
  603. * @param array $params common module parameters
  604. *
  605. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  606. *
  607. * @return array template information
  608. */
  609. function siteBuilder_delSiteConfirm($params) {
  610. return array(
  611. 'templatefile' => 'siteBuilder_del_site_confirm',
  612. 'vars' => array(
  613. 'delsite' => $_POST['s'],
  614. ),
  615. );
  616. }
  617. /**
  618. * Removes a domain from a siteBuilder account.
  619. *
  620. * @param array $params common module parameters
  621. *
  622. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  623. *
  624. * @return string "success" or an error message
  625. */
  626. function siteBuilder_delSite($params) {
  627. if(!filter_var($_POST['s'], FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME)){
  628. return 'Error: invalid domain name';
  629. }
  630. $site = $_POST['s'];
  631. $siteBuilder = new ApiClient(getSiteBuilderApiURL($params), $params['serveraccesshash']);
  632. // undeploy dev
  633. $response = $siteBuilder->undeploy($params['username'], 'dev.' . $site, $params['serverusername'], $params['serverpassword']);
  634. if($response['status'] != '200') {
  635. return 'Error: ' . $response['response'];
  636. }
  637. // undeploy prod
  638. $response = $siteBuilder->undeploy($params['username'], $site, $params['serverusername'], $params['serverpassword']);
  639. if($response['status'] != '200') {
  640. return 'Error: ' . $response['response'];
  641. }
  642. // update DB
  643. try {
  644. Capsule::table('sitePro_site')
  645. ->where('name', $site)
  646. ->delete();
  647. } catch (\Exception $e) {
  648. logModuleCall(
  649. 'siteBuilder',
  650. __FUNCTION__,
  651. $params,
  652. 'Error: could remove site from database',
  653. $e->getMessage()
  654. );
  655. return 'Error: could remove site from database';
  656. }
  657. return 'success';
  658. }
  659. /**
  660. * Opens a form to delete a domain from a siteBuilder account.
  661. *
  662. * @param array $params common module parameters
  663. *
  664. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  665. *
  666. * @return array template information
  667. */
  668. function siteBuilder_revSiteConfirm($params) {
  669. return array(
  670. 'templatefile' => 'siteBuilder_rev_site_confirm',
  671. 'vars' => array(
  672. 'delSite' => $_POST['s'],
  673. ),
  674. );
  675. }
  676. /**
  677. * Revert all Changes of the development Site.
  678. *
  679. * @param array $params common module parameters
  680. *
  681. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  682. *
  683. * @return string "success" or an error message
  684. */
  685. function siteBuilder_revSite($params) {
  686. if(!filter_var($_POST['s'], FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME)){
  687. return 'Error: invalid site name';
  688. }
  689. $site = $_POST['s'];
  690. $siteBuilder = new ApiClient(getSiteBuilderApiURL($params), $params['serveraccesshash']);
  691. $response = $siteBuilder->revertDev($params['username'], $site, $params['serverusername'], $params['serverpassword']);
  692. if($response['status'] != '200') {
  693. return 'Error: ' . $response['response'];
  694. }
  695. return 'success';
  696. }
  697. /**
  698. * Returns API Url .
  699. *
  700. * @param string $params common module parameters
  701. * @param string $user
  702. * @param string $params common module parameters
  703. *
  704. * @return string $apiUrl
  705. */
  706. function getSiteBuilderApiURL($params) {
  707. $httpPrefix = $params['serversecure'] ? 'https://' : 'http://';
  708. $serverPort = $params['serverport'] ? ':' . $params['serverport'] . '/' : '/';
  709. return $httpPrefix . $params['serverhostname'] . $serverPort;
  710. }
  711. function getSites($serviceID) {
  712. try {
  713. $sites = Capsule::table('sitePro_site')
  714. ->where('relid',$serviceID)
  715. ->value('name');
  716. } catch (\Exception $e) {
  717. logModuleCall(
  718. 'siteBuilder',
  719. __FUNCTION__,
  720. $params,
  721. 'Error: could fetch sites from database',
  722. $e->getMessage()
  723. );
  724. return 'Error: could fetch sites from database';
  725. }
  726. return $sites;
  727. }
  728. function siteBuilderCreateTables() {
  729. // Create a new table.
  730. if (!Capsule::schema()->hasTable('sitePro_acc')) {
  731. try {
  732. Capsule::schema()->create(
  733. 'sitePro_acc',
  734. function ($table) { logModuleCall(
  735. 'siteBuilder',
  736. __FUNCTION__,
  737. $params,
  738. 'Debug',
  739. $site
  740. );
  741. /** @var \Illuminate\Database\Schema\Blueprint $table */
  742. $table->increments('id');
  743. $table->string('account');
  744. $table->integer('pid');
  745. $table->boolean('enabled');
  746. }
  747. );
  748. } catch (\Exception $e) {
  749. echo "Unable to create sitePro_acc: {$e->getMessage()}";
  750. }
  751. }
  752. if (!Capsule::schema()->hasTable('sitePro_site')) {
  753. try {
  754. Capsule::schema()->create(
  755. 'sitePro_site',
  756. function ($table) {
  757. /** @var \Illuminate\Database\Schema\Blueprint $table */
  758. $table->increments('id');
  759. $table->integer('relid');
  760. $table->string('name');
  761. $table->boolean('enabled');
  762. }
  763. );
  764. } catch (\Exception $e) {
  765. echo "Unable to create sitePro_site: {$e->getMessage()}";
  766. }
  767. }
  768. }