siteBuilder.php 28 KB

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