siteBuilder.php 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992
  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']['error'];
  139. }
  140. $response = $siteBuilder->init($params['username'], $params['domain'], $params['serverusername'], $params['serverpassword']);
  141. if($response['status'] != '200') {
  142. return 'Error: ' . $response['response']['error'];
  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']['error'];
  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['response']['error'];
  247. }
  248. if($response['response']['isenabled'] == 'YES'){
  249. $response = $siteBuilder->disable($params['username'],$params['domain']);
  250. if($response['status'] != '200') {
  251. return 'Error: ' . $response['response']['error'];
  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 unsuspend restoring
  271. $sites = getSites($params['serviceid']);
  272. if(!empty($sites)) {
  273. $siteBuilder = new ApiClient(getSiteBuilderApiURL($params), $params['serveraccesshash']);
  274. foreach($sites as $site) {
  275. $response = $siteBuilder->disable($params['username'], $site, $params['serverusername'], $params['serverpassword']);
  276. if($response['status'] != '200') {
  277. return 'Error: ' . $response['response']['error'];
  278. }
  279. }
  280. }
  281. return 'success';
  282. }
  283. /**
  284. * Set a siteBuilder account to status active.
  285. *
  286. * Called when an un-suspension is requested. This is invoked
  287. * automatically upon payment of an overdue invoice for a product, or
  288. * can be called manually by admin user.
  289. *
  290. * @param array $params common module parameters
  291. *
  292. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  293. *
  294. * @return string 'success' or an error message
  295. */
  296. function siteBuilder_UnsuspendAccount($params) {
  297. try {
  298. Capsule::table('sitePro_acc')
  299. ->where('account',$params['username'])
  300. ->update(array(
  301. 'enabled' => true,
  302. ));
  303. } catch (\Exception $e) {
  304. logModuleCall(
  305. 'siteBuilder',
  306. __FUNCTION__,
  307. $params,
  308. 'Error: could update account in database',
  309. $e->getMessage()
  310. );
  311. return 'Error: could update account in database';
  312. }
  313. // enable active sites
  314. $sites = getSitesEnabled($params['serviceid']);
  315. if(!empty($sites)) {
  316. $siteBuilder = new ApiClient(getSiteBuilderApiURL($params), $params['serveraccesshash']);
  317. foreach($sites as $site) {
  318. $response = $siteBuilder->enable($params['username'], $site, $params['serverusername'], $params['serverpassword']);
  319. if($response['status'] != '200') {
  320. return 'Error: ' . $response['response']['error'];
  321. }
  322. }
  323. }
  324. return 'success';
  325. }
  326. /**
  327. * Client area output logic handling.
  328. *
  329. * This function is used to define module specific client area output. It should
  330. * return an array consisting of a template file and optional additional
  331. * template variables to make available to that template.
  332. *
  333. * @param array $params common module parameters
  334. *
  335. * @see https://developers.whmcs.com/provisioning-modules/client-area-output/
  336. *
  337. * @return array
  338. */
  339. function siteBuilder_ClientArea($params) {
  340. $siteBuilder = new ApiClient(getSiteBuilderApiURL($params), $params['serveraccesshash']);
  341. $clientInfo = array('moduleclientarea' => '1');
  342. $clientInfo['domain'] = $params['domain'];
  343. $accEnabled = Capsule::table('sitePro_acc')
  344. ->where('pid', $params['serviceid'])
  345. ->value('enabled');
  346. $sitesObj = Capsule::table('sitePro_site')
  347. ->where('relid', $params['serviceid'])
  348. ->get();
  349. $clientInfo['account'] = ['enabled' => $accEnabled];
  350. $clientInfo['sites'] = [];
  351. $sslSite = 0;
  352. foreach($sitesObj as $site){
  353. $response = $siteBuilder->getSSLDays($params['username'], $site->name);
  354. if($response['status'] == '200') {
  355. $sslSite = $response['response']['ssl_remaining'];
  356. }
  357. array_push($clientInfo['sites'],['name' => $site->name, 'sslSite' => $sslSite, 'enabled' => $site->enabled]);
  358. }
  359. logModuleCall(
  360. 'siteBuilder',
  361. __FUNCTION__,
  362. $clientInfo,
  363. 'debug',
  364. $sitesObj
  365. );
  366. return array(
  367. 'tabOverviewReplacementTemplate' => 'clientarea',
  368. 'vars' => $clientInfo,
  369. );
  370. }
  371. /**
  372. * Perform single sign-on for a siteBuilder account.
  373. *
  374. * When successful, returns a URL to which the user should be redirected.
  375. *
  376. * @param array $params common module parameters
  377. *
  378. * @see https://developers.whmcs.com/provisioning-modules/single-sign-on/
  379. *
  380. * @return array
  381. */
  382. function siteBuilder_ServiceSingleSignOn($params) {
  383. }
  384. /**
  385. * Upgrade or downgrade a siteBuilder account by package.
  386. *
  387. * Called to apply any change in product assignment or parameters. It
  388. * is called to provision upgrade or downgrade orders, as well as being
  389. * able to be invoked manually by an admin user.
  390. *
  391. * This same function is called for upgrades and downgrades of both
  392. * products and configurable options.
  393. *
  394. * @param array $params common module parameters
  395. *
  396. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  397. *
  398. * @return string "success" or an error message
  399. */
  400. function siteBuilder_ChangePackage($params) {
  401. return 'success';
  402. }
  403. /**
  404. * Usage Update
  405. *
  406. * Important: Runs daily per server not per product
  407. * Run Manually: /admin/reports.php?report=disk_usage_summary&action=updatestats
  408. * @param array $params common module parameters
  409. *
  410. * @see https://developers.whmcs.com/provisioning-modules/usage-update/
  411. */
  412. function siteBuilder_UsageUpdate($params) {
  413. }
  414. /**
  415. * Additional actions a client user can invoke.
  416. *
  417. * Define additional actions a client user can perform for an instance of a
  418. * product/service.
  419. *
  420. * Any actions you define here will be automatically displayed in the available
  421. * list of actions within the client area.
  422. *
  423. * @return array
  424. */
  425. function siteBuilder_ClientAreaCustomButtonArray ($params) {
  426. return array(
  427. 'Neue Webseite' => 'newSite',
  428. );
  429. }
  430. /**
  431. * Additional actions a client user can invoke.
  432. *
  433. * Define additional actions a client user is allowed to perform for an instance of a
  434. * product/service.
  435. *
  436. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  437. *
  438. * @return array
  439. */
  440. function siteBuilder_ClientAreaAllowedFunctions() {
  441. return array(
  442. 'Add Site' => 'addSite',
  443. 'New Site' => 'newSite',
  444. 'Confirm Delete Site' => 'delSiteConfirm',
  445. 'Delete Site' => 'delSite',
  446. 'Edit Site' => 'editSite',
  447. 'Conform Revert Site' => 'revSiteConfirm',
  448. 'Revert Site' => 'revSite',
  449. 'Disable Site' => 'disableSite',
  450. 'Enable Site' => 'enableSite'
  451. );
  452. }
  453. /**
  454. * Opens a form to add a new domain.
  455. *
  456. * @param array $params common module parameters
  457. *
  458. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  459. *
  460. * @return array template information
  461. */
  462. function siteBuilder_newSite($params) {
  463. return array(
  464. 'breadcrumb' => array(
  465. 'clientarea.php?action=productdetails&id=' . $params['serviceid'] . '&modop=custom&a=newSite' => 'Neue Webseite',
  466. ),
  467. 'templatefile' => 'siteBuilder_new_site',
  468. );
  469. }
  470. /**
  471. * Adds a new domain to a siteBuilder account.
  472. *
  473. * @param array $params common module parameters
  474. *
  475. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  476. *
  477. * @return string "success" or an error message
  478. */
  479. function siteBuilder_addSite($params) {
  480. if(empty($_POST['d'])) {
  481. $site = $params['domain'];
  482. } else {
  483. if(!filter_var($_POST['d'], FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME)){
  484. return 'Error: invalid site name';
  485. }
  486. $site = $_POST['d'] . '.' . $params['domain'];
  487. }
  488. $siteBuilder = new ApiClient(getSiteBuilderApiURL($params), $params['serveraccesshash']);
  489. // init prod
  490. $response = $siteBuilder->init($params['username'], $site, $params['serverusername'], $params['serverpassword']);
  491. if($response['status'] != '200') {
  492. return 'Error: ' . $response['response']['error'];
  493. }
  494. // update DB
  495. try {
  496. Capsule::table('sitePro_site')
  497. ->insert(
  498. array(
  499. 'relid' => $params['serviceid'],
  500. 'name' => $site,
  501. 'enabled' => true,
  502. )
  503. );
  504. } catch (\Exception $e) {
  505. logModuleCall(
  506. 'siteBuilder',
  507. __FUNCTION__,
  508. $params,
  509. 'Error: could save site & serviceid in database',
  510. $e->getMessage()
  511. );
  512. return 'Error: could save site & serviceid in database';
  513. }
  514. return 'success';
  515. }
  516. function siteBuilder_editSite($params) {
  517. if(!filter_var($_POST['s'], FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME)){
  518. return 'Error: invalid site name';
  519. }
  520. $site = $_POST['s'];
  521. $api = new SiteProApiClient('https://builder.thurdata.ch/api/', 'apikey0', '993yVHwC05TLsx2JI2XFlAhkkPUxR6JbQUYbI.a5HiRtmNV9');
  522. // use this for enterprise licenses and change 'your-bulder-domain.com' to your builder domain
  523. //$api = new SiteProApiClient('http://your-bulder-domain.com/api/', 'your_api_username', 'your_api_password');
  524. try {
  525. // this call is used to open builder, so you need to set correct parameters to represent users website you want to open
  526. // this data usually comes from your user/hosting manager system
  527. $res = $api->remoteCall('requestLogin', array(
  528. 'type' => 'internal', // (required) 'internal'
  529. 'domain' => $site, // (required) domain of the user website you want to edit
  530. 'lang' => 'de', // (optional) 2-letter language code, set language code you whant builder to open in
  531. 'apiUrl' => getSiteBuilderApiURL($params) . 'deploy/' . $params['username'] . '/' . $site, // (required) API endpoint URL
  532. 'resellerClientAccountId' => $params['serviceid'], // (required) ID of website/user in your system
  533. 'username' => $params['serverusername'], // (optional) authorization username to be used with API endpoint
  534. 'password' => 'your-secure-password', // (optional) authorization password to be used with API endpoint
  535. ));
  536. if (!$res || !is_object($res)) {
  537. logModuleCall(
  538. 'siteBuilder',
  539. __FUNCTION__,
  540. $params,
  541. 'Error: Response format error',
  542. $res
  543. );
  544. return 'Error: Response format error';
  545. } else if (isset($res->url) && $res->url) {
  546. logModuleCall(
  547. 'siteBuilder',
  548. __FUNCTION__,
  549. $params,
  550. 'Debug',
  551. $res
  552. );
  553. // on success redirect to builder URL
  554. header('Location: '.$res->url, true);
  555. exit();
  556. } else {
  557. logModuleCall(
  558. 'siteBuilder',
  559. __FUNCTION__,
  560. $params,
  561. 'Error: Unknown error',
  562. $res
  563. );
  564. return 'Error: Unknown error';
  565. }
  566. } catch (\Exception $e) {
  567. logModuleCall(
  568. 'siteBuilder',
  569. __FUNCTION__,
  570. $params,
  571. 'Error: Request error',
  572. $e->getMessage()
  573. );
  574. return 'Error: Request error';
  575. }
  576. return 'success';
  577. }
  578. /**
  579. * Opens a form to delete a domain from a siteBuilder account.
  580. *
  581. * @param array $params common module parameters
  582. *
  583. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  584. *
  585. * @return array template information
  586. */
  587. function siteBuilder_delSiteConfirm($params) {
  588. return array(
  589. 'templatefile' => 'siteBuilder_del_site_confirm',
  590. 'vars' => array(
  591. 'delsite' => $_POST['s'],
  592. ),
  593. );
  594. }
  595. /**
  596. * Removes a domain from a siteBuilder account.
  597. *
  598. * @param array $params common module parameters
  599. *
  600. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  601. *
  602. * @return string "success" or an error message
  603. */
  604. function siteBuilder_delSite($params) {
  605. if(!filter_var($_POST['s'], FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME)){
  606. return 'Error: invalid domain name';
  607. }
  608. $site = $_POST['s'];
  609. $siteBuilder = new ApiClient(getSiteBuilderApiURL($params), $params['serveraccesshash']);
  610. // undeploy
  611. $response = $siteBuilder->undeploy($params['username'], $site, $params['serverusername'], $params['serverpassword']);
  612. if($response['status'] != '200') {
  613. return 'Error: ' . $response['response']['error'];
  614. }
  615. // remove builder session
  616. $api = new SiteProApiClient('https://builder.thurdata.ch/api/', 'apikey0', '993yVHwC05TLsx2JI2XFlAhkkPUxR6JbQUYbI.a5HiRtmNV9');
  617. // use this for enterprise licenses and change 'your-bulder-domain.com' to your builder domain
  618. //$api = new SiteProApiClient('http://your-bulder-domain.com/api/', 'your_api_username', 'your_api_password');
  619. try {
  620. // this call is used to open builder, so you need to set correct parameters to represent users website you want to open
  621. // this data usually comes from your user/hosting manager system
  622. $res = $api->remoteCall('requestLogin', array(
  623. 'type' => 'internal', // (required) 'internal'
  624. 'domain' => $site, // (required) domain of the user website you want to edit
  625. 'lang' => 'de', // (optional) 2-letter language code, set language code you whant builder to open in
  626. 'apiUrl' => getSiteBuilderApiURL($params) . 'deploy/' . $params['username'] . '/' . $site, // (required) API endpoint URL
  627. 'resellerClientAccountId' => $params['serviceid'], // (required) ID of website/user in your system
  628. 'username' => $params['serverusername'], // (optional) authorization username to be used with API endpoint
  629. 'password' => 'your-secure-password', // (optional) authorization password to be used with API endpoint
  630. ));
  631. if (!$res || !is_object($res)) {
  632. logModuleCall(
  633. 'siteBuilder',
  634. __FUNCTION__,
  635. $params,
  636. 'Error: Response format error',
  637. $res
  638. );
  639. return 'Error: Response format error';
  640. } else if (isset($res->url) && $res->url) {
  641. $result = $api->remoteCall('delete-site', array(
  642. 'domain' => $site
  643. ));
  644. if (!$result || !is_object($result)) {
  645. logModuleCall(
  646. 'siteBuilder',
  647. __FUNCTION__,
  648. $params,
  649. 'Error: Response format error',
  650. $result
  651. );
  652. return 'Error: Response format error';
  653. } else if (isset($result->ok) && $res->ok) {
  654. return 'success';
  655. }
  656. } else {
  657. logModuleCall(
  658. 'siteBuilder',
  659. __FUNCTION__,
  660. $params,
  661. 'Error: Unknown error',
  662. $res
  663. );
  664. return 'Error: Unknown error';
  665. }
  666. } catch (\Exception $e) {
  667. logModuleCall(
  668. 'siteBuilder',
  669. __FUNCTION__,
  670. $params,
  671. 'Error: Request error',
  672. $e->getMessage()
  673. );
  674. return 'Error: Request error';
  675. }
  676. // update DB
  677. try {
  678. Capsule::table('sitePro_site')
  679. ->where('name', $site)
  680. ->delete();
  681. } catch (\Exception $e) {
  682. logModuleCall(
  683. 'siteBuilder',
  684. __FUNCTION__,
  685. $params,
  686. 'Error: could remove site from database',
  687. $e->getMessage()
  688. );
  689. return 'Error: could remove site from database';
  690. }
  691. return 'success';
  692. }
  693. /**
  694. * Opens a form to delete a domain from a siteBuilder account.
  695. *
  696. * @param array $params common module parameters
  697. *
  698. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  699. *
  700. * @return array template information
  701. */
  702. function siteBuilder_revSiteConfirm($params) {
  703. return array(
  704. 'templatefile' => 'siteBuilder_rev_site_confirm',
  705. 'vars' => array(
  706. 'revSite' => $_POST['s'],
  707. ),
  708. );
  709. }
  710. /**
  711. * Revert all Changes of the development Site.
  712. *
  713. * @param array $params common module parameters
  714. *
  715. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  716. *
  717. * @return string "success" or an error message
  718. */
  719. function siteBuilder_revSite($params) {
  720. if(!filter_var($_POST['s'], FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME)){
  721. return 'Error: invalid site name';
  722. }
  723. $site = $_POST['s'];
  724. $siteBuilder = new ApiClient(getSiteBuilderApiURL($params), $params['serveraccesshash']);
  725. $response = $siteBuilder->revert($params['username'], $site, $params['serverusername'], $params['serverpassword']);
  726. if($response['status'] != '200') {
  727. return 'Error: ' . $response['response']['error'];
  728. }
  729. // remove builder session
  730. $api = new SiteProApiClient('https://builder.thurdata.ch/api/', 'apikey0', '993yVHwC05TLsx2JI2XFlAhkkPUxR6JbQUYbI.a5HiRtmNV9');
  731. // use this for enterprise licenses and change 'your-bulder-domain.com' to your builder domain
  732. //$api = new SiteProApiClient('http://your-bulder-domain.com/api/', 'your_api_username', 'your_api_password');
  733. try {
  734. // this call is used to open builder, so you need to set correct parameters to represent users website you want to open
  735. // this data usually comes from your user/hosting manager system
  736. $res = $api->remoteCall('requestLogin', array(
  737. 'type' => 'internal', // (required) 'internal'
  738. 'domain' => $site, // (required) domain of the user website you want to edit
  739. 'lang' => 'de', // (optional) 2-letter language code, set language code you whant builder to open in
  740. 'apiUrl' => getSiteBuilderApiURL($params) . 'deploy/' . $params['username'] . '/' . $site, // (required) API endpoint URL
  741. 'resellerClientAccountId' => $params['serviceid'], // (required) ID of website/user in your system
  742. 'username' => $params['serverusername'], // (optional) authorization username to be used with API endpoint
  743. 'password' => 'your-secure-password', // (optional) authorization password to be used with API endpoint
  744. ));
  745. if (!$res || !is_object($res)) {
  746. logModuleCall(
  747. 'siteBuilder',
  748. __FUNCTION__,
  749. $params,
  750. 'Error: Response format error',
  751. $res
  752. );
  753. return 'Error: Response format error';
  754. } else if (isset($res->url) && $res->url) {
  755. $result = $api->remoteCall('delete-site', array(
  756. 'domain' => $site
  757. ));
  758. if (!$result || !is_object($result)) {
  759. logModuleCall(
  760. 'siteBuilder',
  761. __FUNCTION__,
  762. $params,
  763. 'Error: Response format error',
  764. $result
  765. );
  766. return 'Error: Response format error';
  767. } else if (isset($result->ok) && $res->ok) {
  768. return 'success';
  769. }
  770. } else {
  771. logModuleCall(
  772. 'siteBuilder',
  773. __FUNCTION__,
  774. $params,
  775. 'Error: Unknown error',
  776. $res
  777. );
  778. return 'Error: Unknown error';
  779. }
  780. } catch (\Exception $e) {
  781. logModuleCall(
  782. 'siteBuilder',
  783. __FUNCTION__,
  784. $params,
  785. 'Error: Request error',
  786. $e->getMessage()
  787. );
  788. return 'Error: Request error';
  789. }
  790. return 'success';
  791. }
  792. function siteBuilder_enableSite($params) {
  793. if(!filter_var($_POST['s'], FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME)){
  794. return 'Error: invalid site name';
  795. }
  796. $site = $_POST['s'];
  797. $siteBuilder = new ApiClient(getSiteBuilderApiURL($params), $params['serveraccesshash']);
  798. // enable
  799. $response = $siteBuilder->enable($params['username'], $site, $params['serverusername'], $params['serverpassword']);
  800. if($response['status'] != '200') {
  801. return 'Error: ' . $response['response']['error'];
  802. }
  803. // update DB
  804. try {
  805. Capsule::table('sitePro_site')
  806. ->where('relid',$params['serviceid'])
  807. ->where('name',$site)
  808. ->update(array(
  809. 'enabled' => true,
  810. ));
  811. } catch (\Exception $e) {
  812. logModuleCall(
  813. 'siteBuilder',
  814. __FUNCTION__,
  815. $params,
  816. 'Error: could save site status in database',
  817. $e->getMessage()
  818. );
  819. return 'Error: could save site status in database';
  820. }
  821. return 'success';
  822. }
  823. function siteBuilder_disableSite($params) {
  824. if(!filter_var($_POST['s'], FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME)){
  825. return 'Error: invalid site name';
  826. }
  827. $site = $_POST['s'];
  828. $siteBuilder = new ApiClient(getSiteBuilderApiURL($params), $params['serveraccesshash']);
  829. // disable
  830. $response = $siteBuilder->enable($params['username'], $site, $params['serverusername'], $params['serverpassword']);
  831. logModuleCall(
  832. 'siteBuilder',
  833. __FUNCTION__,
  834. $params,
  835. 'Debug',
  836. $response
  837. );
  838. if($response['status'] != '200') {
  839. return 'Error: ' . $response['response']['error'];
  840. }
  841. // update DB
  842. try {
  843. Capsule::table('sitePro_site')
  844. ->where('relid',$params['serviceid'])
  845. ->where('name',$site)
  846. ->update(array(
  847. 'enabled' => false,
  848. ));
  849. } catch (\Exception $e) {
  850. logModuleCall(
  851. 'siteBuilder',
  852. __FUNCTION__,
  853. $params,
  854. 'Error: could save site status in database',
  855. $e->getMessage()
  856. );
  857. return 'Error: could save site status in database';
  858. }
  859. return 'success';
  860. }
  861. /**
  862. * Returns API Url .
  863. *
  864. * @param string $params common module parameters
  865. * @param string $user
  866. * @param string $params common module parameters
  867. *
  868. * @return string $apiUrl
  869. */
  870. function getSiteBuilderApiURL($params) {
  871. $httpPrefix = $params['serversecure'] ? 'https://' : 'http://';
  872. $serverPort = $params['serverport'] ? ':' . $params['serverport'] . '/' : '/';
  873. return $httpPrefix . $params['serverhostname'] . $serverPort;
  874. }
  875. function getSites($serviceID) {
  876. try {
  877. $sites = Capsule::table('sitePro_site')
  878. ->where('relid',$serviceID)
  879. ->value('name');
  880. } catch (\Exception $e) {
  881. logModuleCall(
  882. 'siteBuilder',
  883. __FUNCTION__,
  884. $params,
  885. 'Error: could fetch sites from database',
  886. $e->getMessage()
  887. );
  888. return 'Error: could fetch sites from database';
  889. }
  890. return $sites;
  891. }
  892. function getSitesEnabled($serviceID) {
  893. try {
  894. $sites = Capsule::table('sitePro_site')
  895. ->where('relid',$serviceID)
  896. ->where('enabled', 1)
  897. ->value('name');
  898. } catch (\Exception $e) {
  899. logModuleCall(
  900. 'siteBuilder',
  901. __FUNCTION__,
  902. $params,
  903. 'Error: could fetch sites from database',
  904. $e->getMessage()
  905. );
  906. return 'Error: could fetch sites from database';
  907. }
  908. return $sites;
  909. }
  910. function siteBuilderCreateTables() {
  911. // Create a new table.
  912. if (!Capsule::schema()->hasTable('sitePro_acc')) {
  913. try {
  914. Capsule::schema()->create(
  915. 'sitePro_acc',
  916. function ($table) { logModuleCall(
  917. 'siteBuilder',
  918. __FUNCTION__,
  919. $params,
  920. 'Debug',
  921. $site
  922. );
  923. /** @var \Illuminate\Database\Schema\Blueprint $table */
  924. $table->increments('id');
  925. $table->string('account');
  926. $table->integer('pid');
  927. $table->boolean('enabled');
  928. }
  929. );
  930. } catch (\Exception $e) {
  931. echo "Unable to create sitePro_acc: {$e->getMessage()}";
  932. }
  933. }
  934. if (!Capsule::schema()->hasTable('sitePro_site')) {
  935. try {
  936. Capsule::schema()->create(
  937. 'sitePro_site',
  938. function ($table) {
  939. /** @var \Illuminate\Database\Schema\Blueprint $table */
  940. $table->increments('id');
  941. $table->integer('relid');
  942. $table->string('name');
  943. $table->boolean('enabled');
  944. }
  945. );
  946. } catch (\Exception $e) {
  947. echo "Unable to create sitePro_site: {$e->getMessage()}";
  948. }
  949. }
  950. }