siteBuilder.php 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041
  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. "FriendlyName" => "Hosting Plan ID",
  46. "Type" => "text", # Text Box
  47. "Size" => "25", # Defines the Field Width
  48. "Description" => "Set the hostingPlan ID for this Product",
  49. "Default" => "Free",
  50. ], [
  51. "FriendlyName" => "Quota in MB",
  52. "Type" => "text", # Text Box
  53. "Size" => "25", # Defines the Field Width
  54. "Description" => "Set the Quoat matching Your HostingPlan (MB)",
  55. "Default" => "512",
  56. ]
  57. ];
  58. }
  59. /**
  60. * Test connection to a siteBuilder server with the given server parameters.
  61. *
  62. * Allows an admin user to verify that an API connection can be
  63. * successfully made with the given configuration parameters for a
  64. * server.
  65. *
  66. * When defined in a module, a test connection button will appear
  67. * alongside the server type dropdown when adding or editing an
  68. * existing server.
  69. *
  70. * @param array $params common module parameters
  71. *
  72. * @see https://developers.whmcs.com/provisioning-modules/module-parameters/
  73. *
  74. * @return array
  75. */
  76. function siteBuilder_Testconnection($params) {
  77. $siteBuilder = new ApiClient(getSiteBuilderApiURL($params), $params['serveraccesshash']);
  78. $response = $siteBuilder->ping($params['serverusername'], $params['serverpassword']);
  79. if($response['response']['answer'] == 'pong') {
  80. return array(
  81. 'success' => true,
  82. 'error' => '',
  83. );
  84. }
  85. return array(
  86. 'success' => false,
  87. 'error' => $response,
  88. );
  89. }
  90. /**
  91. * Provision a new account of a siteBuilder server.
  92. *
  93. * Attempt to provision a new siteBuilder account. This is
  94. * called any time provisioning is requested inside of WHMCS. Depending upon the
  95. * configuration, this can be any of:
  96. * * When a new order is placed
  97. * * When an invoice for a new order is paid
  98. * * Upon manual request by an admin user
  99. *
  100. * @param array $params common module parameters
  101. *
  102. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  103. *
  104. * @return string 'success' or an error message
  105. */
  106. function siteBuilder_CreateAccount($params) {
  107. $username = strtolower(substr($params['clientsdetails']['firstname'],0,2) . substr($params['clientsdetails']['lastname'],0,3)) . $params['serviceid'];
  108. $userdomain = $params['domain'];
  109. try {
  110. Capsule::table('tblhosting')
  111. ->where('id', '=', $params['serviceid'])
  112. ->update(
  113. array(
  114. 'username' => $username,
  115. 'domain' => $userdomain,
  116. )
  117. );
  118. } catch (\Exception $e) {
  119. logModuleCall(
  120. 'siteBuilder',
  121. __FUNCTION__,
  122. $params,
  123. 'Error: could save username & domain in database',
  124. $e->getMessage()
  125. );
  126. return 'Error: could save username & password in database';
  127. }
  128. try {
  129. Capsule::table('sitePro_acc')
  130. ->insert(
  131. array(
  132. 'account' => $username,
  133. 'pid' => $params['serviceid'],
  134. 'enabled' => true,
  135. )
  136. );
  137. } catch (\Exception $e) {
  138. logModuleCall(
  139. 'siteBuilder',
  140. __FUNCTION__,
  141. $params,
  142. 'Error: could save username & serviceid in database',
  143. $e->getMessage()
  144. );
  145. return 'Error: could save username & serviceid in database';
  146. }
  147. $siteBuilder = new ApiClient(getSiteBuilderApiURL($params), $params['serveraccesshash']);
  148. $response = $siteBuilder->create($params['username'], $params['domain'], $params['serverusername'], $params['serverpassword']);
  149. if($response['status'] != '200') {
  150. return 'Error: ' . $response['response']['error'];
  151. }
  152. $response = $siteBuilder->init($params['username'], $params['domain'], $params['serverusername'], $params['serverpassword']);
  153. if($response['status'] != '200') {
  154. return 'Error: ' . $response['response']['error'];
  155. }
  156. $response = $siteBuilder->setQuota($params['username'], $params['configoption3'], $params['serverusername'], $params['serverpassword']);
  157. if($response['status'] != '200') {
  158. return 'Error: ' . $response['response']['error'];
  159. }
  160. return 'success';
  161. }
  162. /**
  163. * Removes a siteBuilder account.
  164. *
  165. * Called when a termination is requested. This can be invoked automatically for
  166. * overdue products if enabled, or requested manually by an admin user.
  167. *
  168. * @param array $params common module parameters
  169. *
  170. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  171. *
  172. * @return string 'success' or an error message
  173. */
  174. function siteBuilder_TerminateAccount($params) {
  175. try {
  176. $active = Capsule::table('sitePro_acc')
  177. ->where('account',$params['username'])
  178. ->value('enabled');
  179. } catch (\Exception $e) {
  180. logModuleCall(
  181. 'siteBuilder',
  182. __FUNCTION__,
  183. $params,
  184. 'Error: could fetch account from database',
  185. $e->getMessage()
  186. );
  187. return 'Error: could fetch account from database';
  188. }
  189. if($active == true) {
  190. return 'Error: Account is active, please suspend account first';
  191. }
  192. // undeploy all related sites
  193. $sites = getSites($params['serviceid']);
  194. $siteBuilder = new ApiClient(getSiteBuilderApiURL($params), $params['serveraccesshash']);
  195. if(!empty($sites)) {
  196. foreach($sites as $site) {
  197. /* $response = $siteBuilder->undeploy($params['username'], $site, $params['serverusername'], $params['serverpassword']);
  198. if($response['status'] != '200') {
  199. return 'Error: ' . $response['response'];
  200. } */
  201. logModuleCall(
  202. 'siteBuilder',
  203. __FUNCTION__,
  204. $params,
  205. 'debug',
  206. $site
  207. );
  208. }
  209. try {
  210. Capsule::table('sitePro_site')
  211. ->where('relid',$params['serviceid'])
  212. ->delete();
  213. } catch (\Exception $e) {
  214. logModuleCall(
  215. 'siteBuilder',
  216. __FUNCTION__,
  217. $params,
  218. 'Error: could remove domains from database',
  219. $e->getMessage()
  220. );
  221. return 'Error: could remove domains from database';
  222. }
  223. }
  224. // terminate account
  225. $response = $siteBuilder->terminate($params['username'], $params['domain']);
  226. if($response['status'] != '200') {
  227. return 'Error: ' . $response['response']['error'];
  228. }
  229. try {
  230. Capsule::table('sitePro_acc')
  231. ->where('account',$params['username'])
  232. ->delete();
  233. } catch (\Exception $e) {
  234. logModuleCall(
  235. 'siteBuilder',
  236. __FUNCTION__,
  237. $params,
  238. 'Error: could remove account from database',
  239. $e->getMessage()
  240. );
  241. return 'Error: could remove account from database';
  242. }
  243. return 'success';
  244. }
  245. /**
  246. * Set a siteBuilder account to status inactive.
  247. *
  248. * Called when a suspension is requested. This is invoked automatically by WHMCS
  249. * when a product becomes overdue on payment or can be called manually by admin
  250. * user.
  251. *
  252. * @param array $params common module parameters
  253. *
  254. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  255. *
  256. * @return string 'success' or an error message
  257. */
  258. function siteBuilder_SuspendAccount($params) {
  259. $siteBuilder = new ApiClient(getSiteBuilderApiURL($params), $params['serveraccesshash']);
  260. $status = $siteBuilder->isenabled($params['username'],$params['domain']);
  261. if($status['status'] != '200') {
  262. return 'Error: ' . $status['response']['error'];
  263. }
  264. if($response['response']['isenabled'] == 'YES'){
  265. $response = $siteBuilder->disable($params['username'],$params['domain']);
  266. if($response['status'] != '200') {
  267. return 'Error: ' . $response['response']['error'];
  268. }
  269. }
  270. try {
  271. Capsule::table('sitePro_acc')
  272. ->where('account',$params['username'])
  273. ->update(array(
  274. 'enabled' => false,
  275. ));
  276. } catch (\Exception $e) {
  277. logModuleCall(
  278. 'siteBuilder',
  279. __FUNCTION__,
  280. $params,
  281. 'Error: could not disable account in database',
  282. $e->getMessage()
  283. );
  284. return 'Error: could not disable account in database';
  285. }
  286. // disable all sites but not change status in DB for unsuspend restoring
  287. $sites = getSites($params['serviceid']);
  288. if(!empty($sites)) {
  289. $siteBuilder = new ApiClient(getSiteBuilderApiURL($params), $params['serveraccesshash']);
  290. foreach($sites as $site) {
  291. $response = $siteBuilder->disable($params['username'], $site, $params['serverusername'], $params['serverpassword']);
  292. if($response['status'] != '200') {
  293. return 'Error: ' . $response['response']['error'];
  294. }
  295. }
  296. }
  297. return 'success';
  298. }
  299. /**
  300. * Set a siteBuilder account to status active.
  301. *
  302. * Called when an un-suspension is requested. This is invoked
  303. * automatically upon payment of an overdue invoice for a product, or
  304. * can be called manually by admin user.
  305. *
  306. * @param array $params common module parameters
  307. *
  308. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  309. *
  310. * @return string 'success' or an error message
  311. */
  312. function siteBuilder_UnsuspendAccount($params) {
  313. try {
  314. Capsule::table('sitePro_acc')
  315. ->where('account',$params['username'])
  316. ->update(array(
  317. 'enabled' => true,
  318. ));
  319. } catch (\Exception $e) {
  320. logModuleCall(
  321. 'siteBuilder',
  322. __FUNCTION__,
  323. $params,
  324. 'Error: could update account in database',
  325. $e->getMessage()
  326. );
  327. return 'Error: could update account in database';
  328. }
  329. // enable active sites
  330. $sites = getSitesEnabled($params['serviceid']);
  331. if(!empty($sites)) {
  332. $siteBuilder = new ApiClient(getSiteBuilderApiURL($params), $params['serveraccesshash']);
  333. foreach($sites as $site) {
  334. $response = $siteBuilder->enable($params['username'], $site, $params['serverusername'], $params['serverpassword']);
  335. if($response['status'] != '200') {
  336. return 'Error: ' . $response['response']['error'];
  337. }
  338. }
  339. }
  340. return 'success';
  341. }
  342. /**
  343. * Client area output logic handling.
  344. *
  345. * This function is used to define module specific client area output. It should
  346. * return an array consisting of a template file and optional additional
  347. * template variables to make available to that template.
  348. *
  349. * @param array $params common module parameters
  350. *
  351. * @see https://developers.whmcs.com/provisioning-modules/client-area-output/
  352. *
  353. * @return array
  354. */
  355. function siteBuilder_ClientArea($params) {
  356. $siteBuilder = new ApiClient(getSiteBuilderApiURL($params), $params['serveraccesshash']);
  357. $clientInfo = array('moduleclientarea' => '1');
  358. $clientInfo['domain'] = $params['domain'];
  359. $accEnabled = Capsule::table('sitePro_acc')
  360. ->where('pid', $params['serviceid'])
  361. ->value('enabled');
  362. $sitesObj = Capsule::table('sitePro_site')
  363. ->where('relid', $params['serviceid'])
  364. ->get();
  365. $clientInfo['account'] = ['enabled' => $accEnabled];
  366. $clientInfo['sites'] = [];
  367. foreach($sitesObj as $site){
  368. $response = $siteBuilder->getSSLDays($params['username'], $site->name);
  369. if($response['status'] == '200') {
  370. $sslSite = $response['response']['ssl_remaining'];
  371. }
  372. array_push($clientInfo['sites'],['name' => $site->name, 'sslSite' => $sslSite, 'enabled' => $site->enabled]);
  373. }
  374. $response = $siteBuilder->getQuota($params['username']);
  375. if($response['status'] != '200') {
  376. logModuleCall(
  377. 'siteBuilder',
  378. __FUNCTION__,
  379. $params,
  380. 'Error getting Quota',
  381. $response
  382. );
  383. }
  384. $clientInfo['quota'] = round($response['response']['quota'][0]['blocks']/1024);
  385. $clientInfo['limit'] = round($response['response']['quota'][0]['hard']/1024);
  386. return array(
  387. 'tabOverviewReplacementTemplate' => 'clientarea',
  388. 'vars' => $clientInfo,
  389. );
  390. }
  391. /**
  392. * Perform single sign-on for a siteBuilder account.
  393. *
  394. * When successful, returns a URL to which the user should be redirected.
  395. *
  396. * @param array $params common module parameters
  397. *
  398. * @see https://developers.whmcs.com/provisioning-modules/single-sign-on/
  399. *
  400. * @return array
  401. */
  402. function siteBuilder_ServiceSingleSignOn($params) {
  403. }
  404. /**
  405. * Upgrade or downgrade a siteBuilder account by package.
  406. *
  407. * Called to apply any change in product assignment or parameters. It
  408. * is called to provision upgrade or downgrade orders, as well as being
  409. * able to be invoked manually by an admin user.
  410. *
  411. * This same function is called for upgrades and downgrades of both
  412. * products and configurable options.
  413. *
  414. * @param array $params common module parameters
  415. *
  416. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  417. *
  418. * @return string "success" or an error message
  419. */
  420. function siteBuilder_ChangePackage($params) {
  421. return 'success';
  422. }
  423. /**
  424. * Usage Update
  425. *
  426. * Important: Runs daily per server not per product
  427. * Run Manually: /admin/reports.php?report=disk_usage_summary&action=updatestats
  428. * @param array $params common module parameters
  429. *
  430. * @see https://developers.whmcs.com/provisioning-modules/usage-update/
  431. */
  432. function siteBuilder_UsageUpdate($params) {
  433. $siteBuilder = new ApiClient(getSiteBuilderApiURL($params), $params['serveraccesshash']);
  434. $response = $siteBuilder->getStats();
  435. if($response['status'] != '200') {
  436. logActivity('ERROR: Unable to update sitebuilder server usage: ' . implode('#',[$response]));
  437. }
  438. $stats = $response['response']['quota'];
  439. foreach($stats as $stat){
  440. try {
  441. Capsule::table('tblhosting')
  442. ->where('server', $params['serverid'])
  443. ->where('username', $stat['user'])
  444. ->update([
  445. 'diskusage' => $stat['used']/1024,
  446. 'disklimit' => $stat['hard']/1024,
  447. 'lastupdate' => Capsule::raw('now()'),
  448. ]);
  449. } catch (\Exception $e) {
  450. logActivity('ERROR: Unable to update sitebuilder server usage: ' . $e->getMessage());
  451. }
  452. logModuleCall(
  453. 'siteBuilder',
  454. __FUNCTION__,
  455. $stat,
  456. 'debug',
  457. $params
  458. );
  459. }
  460. }
  461. /**
  462. * Additional actions a client user can invoke.
  463. *
  464. * Define additional actions a client user can perform for an instance of a
  465. * product/service.
  466. *
  467. * Any actions you define here will be automatically displayed in the available
  468. * list of actions within the client area.
  469. *
  470. * @return array
  471. */
  472. function siteBuilder_ClientAreaCustomButtonArray ($params) {
  473. return array(
  474. 'Neue Webseite' => 'newSite',
  475. );
  476. }
  477. /**
  478. * Additional actions a client user can invoke.
  479. *
  480. * Define additional actions a client user is allowed to perform for an instance of a
  481. * product/service.
  482. *
  483. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  484. *
  485. * @return array
  486. */
  487. function siteBuilder_ClientAreaAllowedFunctions() {
  488. return array(
  489. 'Add Site' => 'addSite',
  490. 'New Site' => 'newSite',
  491. 'Confirm Delete Site' => 'delSiteConfirm',
  492. 'Delete Site' => 'delSite',
  493. 'Edit Site' => 'editSite',
  494. 'Conform Revert Site' => 'revSiteConfirm',
  495. 'Revert Site' => 'revSite',
  496. 'Disable Site' => 'disableSite',
  497. 'Enable Site' => 'enableSite'
  498. );
  499. }
  500. /**
  501. * Opens a form to add a new domain.
  502. *
  503. * @param array $params common module parameters
  504. *
  505. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  506. *
  507. * @return array template information
  508. */
  509. function siteBuilder_newSite($params) {
  510. return array(
  511. 'breadcrumb' => array(
  512. 'clientarea.php?action=productdetails&id=' . $params['serviceid'] . '&modop=custom&a=newSite' => 'Neue Webseite',
  513. ),
  514. 'templatefile' => 'siteBuilder_new_site',
  515. );
  516. }
  517. /**
  518. * Adds a new domain to a siteBuilder account.
  519. *
  520. * @param array $params common module parameters
  521. *
  522. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  523. *
  524. * @return string "success" or an error message
  525. */
  526. function siteBuilder_addSite($params) {
  527. if(empty($_POST['d'])) {
  528. $site = $params['domain'];
  529. } else {
  530. if(!filter_var($_POST['d'], FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME)){
  531. return 'Error: invalid site name';
  532. }
  533. $site = $_POST['d'] . '.' . $params['domain'];
  534. }
  535. $siteBuilder = new ApiClient(getSiteBuilderApiURL($params), $params['serveraccesshash']);
  536. // init prod
  537. $response = $siteBuilder->init($params['username'], $site, $params['serverusername'], $params['serverpassword']);
  538. if($response['status'] != '200') {
  539. return 'Error: ' . $response['response']['error'];
  540. }
  541. // update DB
  542. try {
  543. Capsule::table('sitePro_site')
  544. ->insert(
  545. array(
  546. 'relid' => $params['serviceid'],
  547. 'name' => $site,
  548. 'enabled' => true,
  549. )
  550. );
  551. } catch (\Exception $e) {
  552. logModuleCall(
  553. 'siteBuilder',
  554. __FUNCTION__,
  555. $params,
  556. 'Error: could save site & serviceid in database',
  557. $e->getMessage()
  558. );
  559. return 'Error: could save site & serviceid in database';
  560. }
  561. return 'success';
  562. }
  563. function siteBuilder_editSite($params) {
  564. if(!filter_var($_POST['s'], FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME)){
  565. return 'Error: invalid site name';
  566. }
  567. $site = $_POST['s'];
  568. $api = new SiteProApiClient('https://builder.thurdata.ch/api/', 'apikey0', '993yVHwC05TLsx2JI2XFlAhkkPUxR6JbQUYbI.a5HiRtmNV9');
  569. // use this for enterprise licenses and change 'your-bulder-domain.com' to your builder domain
  570. //$api = new SiteProApiClient('http://your-bulder-domain.com/api/', 'your_api_username', 'your_api_password');
  571. try {
  572. // this call is used to open builder, so you need to set correct parameters to represent users website you want to open
  573. // this data usually comes from your user/hosting manager system
  574. $res = $api->remoteCall('requestLogin', array(
  575. 'type' => 'internal', // (required) 'internal'
  576. 'domain' => $site, // (required) domain of the user website you want to edit
  577. 'lang' => 'de', // (optional) 2-letter language code, set language code you whant builder to open in
  578. 'apiUrl' => getSiteBuilderApiURL($params) . 'deploy/' . $params['username'] . '/' . $site, // (required) API endpoint URL
  579. 'resellerClientAccountId' => $params['serviceid'], // (required) ID of website/user in your system
  580. 'username' => $params['serverusername'], // (optional) authorization username to be used with API endpoint
  581. 'password' => 'your-secure-password', // (optional) authorization password to be used with API endpoint
  582. 'hostingPlan' => $params['configoption2'],
  583. ));
  584. if (!$res || !is_object($res)) {
  585. logModuleCall(
  586. 'siteBuilder',
  587. __FUNCTION__,
  588. $params,
  589. 'Error: Response format error',
  590. $res
  591. );
  592. return 'Error: Response format error';
  593. } else if (isset($res->url) && $res->url) {
  594. logModuleCall(
  595. 'siteBuilder',
  596. __FUNCTION__,
  597. $params,
  598. 'Debug',
  599. $res
  600. );
  601. // on success redirect to builder URL
  602. header('Location: '.$res->url, true);
  603. exit();
  604. } else {
  605. logModuleCall(
  606. 'siteBuilder',
  607. __FUNCTION__,
  608. $params,
  609. 'Error: Unknown error',
  610. $res
  611. );
  612. return 'Error: Unknown error';
  613. }
  614. } catch (\Exception $e) {
  615. logModuleCall(
  616. 'siteBuilder',
  617. __FUNCTION__,
  618. $params,
  619. 'Error: Request error',
  620. $e->getMessage()
  621. );
  622. return 'Error: Request error';
  623. }
  624. return 'success';
  625. }
  626. /**
  627. * Opens a form to delete a domain from a siteBuilder account.
  628. *
  629. * @param array $params common module parameters
  630. *
  631. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  632. *
  633. * @return array template information
  634. */
  635. function siteBuilder_delSiteConfirm($params) {
  636. return array(
  637. 'templatefile' => 'siteBuilder_del_site_confirm',
  638. 'vars' => array(
  639. 'delsite' => $_POST['s'],
  640. ),
  641. );
  642. }
  643. /**
  644. * Removes a domain from a siteBuilder account.
  645. *
  646. * @param array $params common module parameters
  647. *
  648. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  649. *
  650. * @return string "success" or an error message
  651. */
  652. function siteBuilder_delSite($params) {
  653. if(!filter_var($_POST['s'], FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME)){
  654. return 'Error: invalid domain name';
  655. }
  656. $site = $_POST['s'];
  657. $siteBuilder = new ApiClient(getSiteBuilderApiURL($params), $params['serveraccesshash']);
  658. // undeploy
  659. $response = $siteBuilder->undeploy($params['username'], $site, $params['serverusername'], $params['serverpassword']);
  660. if($response['status'] != '200') {
  661. return 'Error: ' . $response['response']['error'];
  662. }
  663. // remove builder session
  664. $api = new SiteProApiClient('https://builder.thurdata.ch/api/', 'apikey0', '993yVHwC05TLsx2JI2XFlAhkkPUxR6JbQUYbI.a5HiRtmNV9');
  665. // use this for enterprise licenses and change 'your-bulder-domain.com' to your builder domain
  666. //$api = new SiteProApiClient('http://your-bulder-domain.com/api/', 'your_api_username', 'your_api_password');
  667. try {
  668. // this call is used to open builder, so you need to set correct parameters to represent users website you want to open
  669. // this data usually comes from your user/hosting manager system
  670. $res = $api->remoteCall('requestLogin', array(
  671. 'type' => 'internal', // (required) 'internal'
  672. 'domain' => $site, // (required) domain of the user website you want to edit
  673. 'lang' => 'de', // (optional) 2-letter language code, set language code you whant builder to open in
  674. 'apiUrl' => getSiteBuilderApiURL($params) . 'deploy/' . $params['username'] . '/' . $site, // (required) API endpoint URL
  675. 'resellerClientAccountId' => $params['serviceid'], // (required) ID of website/user in your system
  676. 'username' => $params['serverusername'], // (optional) authorization username to be used with API endpoint
  677. 'password' => 'your-secure-password', // (optional) authorization password to be used with API endpoint
  678. ));
  679. if (!$res || !is_object($res)) {
  680. logModuleCall(
  681. 'siteBuilder',
  682. __FUNCTION__,
  683. $params,
  684. 'Error: Response format error',
  685. $res
  686. );
  687. return 'Error: Response format error';
  688. } else if (isset($res->url) && $res->url) {
  689. $result = $api->remoteCall('delete-site', array(
  690. 'domain' => $site
  691. ));
  692. if (!$result || !is_object($result)) {
  693. logModuleCall(
  694. 'siteBuilder',
  695. __FUNCTION__,
  696. $params,
  697. 'Error: Response format error',
  698. $result
  699. );
  700. return 'Error: Response format error';
  701. } else if (isset($result->ok) && $res->ok) {
  702. return 'success';
  703. }
  704. } else {
  705. logModuleCall(
  706. 'siteBuilder',
  707. __FUNCTION__,
  708. $params,
  709. 'Error: Unknown error',
  710. $res
  711. );
  712. return 'Error: Unknown error';
  713. }
  714. } catch (\Exception $e) {
  715. logModuleCall(
  716. 'siteBuilder',
  717. __FUNCTION__,
  718. $params,
  719. 'Error: Request error',
  720. $e->getMessage()
  721. );
  722. return 'Error: Request error';
  723. }
  724. // update DB
  725. try {
  726. Capsule::table('sitePro_site')
  727. ->where('name', $site)
  728. ->delete();
  729. } catch (\Exception $e) {
  730. logModuleCall(
  731. 'siteBuilder',
  732. __FUNCTION__,
  733. $params,
  734. 'Error: could remove site from database',
  735. $e->getMessage()
  736. );
  737. return 'Error: could remove site from database';
  738. }
  739. return 'success';
  740. }
  741. /**
  742. * Opens a form to delete a domain from a siteBuilder account.
  743. *
  744. * @param array $params common module parameters
  745. *
  746. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  747. *
  748. * @return array template information
  749. */
  750. function siteBuilder_revSiteConfirm($params) {
  751. return array(
  752. 'templatefile' => 'siteBuilder_rev_site_confirm',
  753. 'vars' => array(
  754. 'revSite' => $_POST['s'],
  755. ),
  756. );
  757. }
  758. /**
  759. * Revert all Changes of the development Site.
  760. *
  761. * @param array $params common module parameters
  762. *
  763. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  764. *
  765. * @return string "success" or an error message
  766. */
  767. function siteBuilder_revSite($params) {
  768. if(!filter_var($_POST['s'], FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME)){
  769. return 'Error: invalid site name';
  770. }
  771. $site = $_POST['s'];
  772. $siteBuilder = new ApiClient(getSiteBuilderApiURL($params), $params['serveraccesshash']);
  773. $response = $siteBuilder->revert($params['username'], $site, $params['serverusername'], $params['serverpassword']);
  774. if($response['status'] != '200') {
  775. return 'Error: ' . $response['response']['error'];
  776. }
  777. // remove builder session
  778. $api = new SiteProApiClient('https://builder.thurdata.ch/api/', 'apikey0', '993yVHwC05TLsx2JI2XFlAhkkPUxR6JbQUYbI.a5HiRtmNV9');
  779. // use this for enterprise licenses and change 'your-bulder-domain.com' to your builder domain
  780. //$api = new SiteProApiClient('http://your-bulder-domain.com/api/', 'your_api_username', 'your_api_password');
  781. try {
  782. // this call is used to open builder, so you need to set correct parameters to represent users website you want to open
  783. // this data usually comes from your user/hosting manager system
  784. $res = $api->remoteCall('requestLogin', array(
  785. 'type' => 'internal', // (required) 'internal'
  786. 'domain' => $site, // (required) domain of the user website you want to edit
  787. 'lang' => 'de', // (optional) 2-letter language code, set language code you whant builder to open in
  788. 'apiUrl' => getSiteBuilderApiURL($params) . 'deploy/' . $params['username'] . '/' . $site, // (required) API endpoint URL
  789. 'resellerClientAccountId' => $params['serviceid'], // (required) ID of website/user in your system
  790. 'username' => $params['serverusername'], // (optional) authorization username to be used with API endpoint
  791. 'password' => 'your-secure-password', // (optional) authorization password to be used with API endpoint
  792. ));
  793. if (!$res || !is_object($res)) {
  794. logModuleCall(
  795. 'siteBuilder',
  796. __FUNCTION__,
  797. $params,
  798. 'Error: Response format error',
  799. $res
  800. );
  801. return 'Error: Response format error';
  802. } else if (isset($res->url) && $res->url) {
  803. $result = $api->remoteCall('delete-site', array(
  804. 'domain' => $site
  805. ));
  806. if (!$result || !is_object($result)) {
  807. logModuleCall(
  808. 'siteBuilder',
  809. __FUNCTION__,
  810. $params,
  811. 'Error: Response format error',
  812. $result
  813. );
  814. return 'Error: Response format error';
  815. } else if (isset($result->ok) && $res->ok) {
  816. return 'success';
  817. }
  818. } else {
  819. logModuleCall(
  820. 'siteBuilder',
  821. __FUNCTION__,
  822. $params,
  823. 'Error: Unknown error',
  824. $res
  825. );
  826. return 'Error: Unknown error';
  827. }
  828. } catch (\Exception $e) {
  829. logModuleCall(
  830. 'siteBuilder',
  831. __FUNCTION__,
  832. $params,
  833. 'Error: Request error',
  834. $e->getMessage()
  835. );
  836. return 'Error: Request error';
  837. }
  838. return 'success';
  839. }
  840. function siteBuilder_enableSite($params) {
  841. if(!filter_var($_POST['s'], FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME)){
  842. return 'Error: invalid site name';
  843. }
  844. $site = $_POST['s'];
  845. $siteBuilder = new ApiClient(getSiteBuilderApiURL($params), $params['serveraccesshash']);
  846. // enable
  847. $response = $siteBuilder->enable($params['username'], $site, $params['serverusername'], $params['serverpassword']);
  848. if($response['status'] != '200') {
  849. return 'Error: ' . $response['response']['error'];
  850. }
  851. // update DB
  852. try {
  853. Capsule::table('sitePro_site')
  854. ->where('relid',$params['serviceid'])
  855. ->where('name',$site)
  856. ->update(array(
  857. 'enabled' => true,
  858. ));
  859. } catch (\Exception $e) {
  860. logModuleCall(
  861. 'siteBuilder',
  862. __FUNCTION__,
  863. $params,
  864. 'Error: could save site status in database',
  865. $e->getMessage()
  866. );
  867. return 'Error: could save site status in database';
  868. }
  869. return 'success';
  870. }
  871. function siteBuilder_disableSite($params) {
  872. if(!filter_var($_POST['s'], FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME)){
  873. return 'Error: invalid site name';
  874. }
  875. $site = $_POST['s'];
  876. $siteBuilder = new ApiClient(getSiteBuilderApiURL($params), $params['serveraccesshash']);
  877. // disable
  878. $response = $siteBuilder->disable($params['username'], $site, $params['serverusername'], $params['serverpassword']);
  879. logModuleCall(
  880. 'siteBuilder',
  881. __FUNCTION__,
  882. $params,
  883. 'Debug',
  884. $response
  885. );
  886. if($response['status'] != '200') {
  887. return 'Error: ' . $response['response']['error'];
  888. }
  889. // update DB
  890. try {
  891. Capsule::table('sitePro_site')
  892. ->where('relid',$params['serviceid'])
  893. ->where('name',$site)
  894. ->update(array(
  895. 'enabled' => false,
  896. ));
  897. } catch (\Exception $e) {
  898. logModuleCall(
  899. 'siteBuilder',
  900. __FUNCTION__,
  901. $params,
  902. 'Error: could save site status in database',
  903. $e->getMessage()
  904. );
  905. return 'Error: could save site status in database';
  906. }
  907. return 'success';
  908. }
  909. /**
  910. * Returns API Url .
  911. *
  912. * @param string $params common module parameters
  913. * @param string $user
  914. * @param string $params common module parameters
  915. *
  916. * @return string $apiUrl
  917. */
  918. function getSiteBuilderApiURL($params) {
  919. $httpPrefix = $params['serversecure'] ? 'https://' : 'http://';
  920. $serverPort = $params['serverport'] ? ':' . $params['serverport'] . '/' : '/';
  921. return $httpPrefix . $params['serverhostname'] . $serverPort;
  922. }
  923. function getSites($serviceID) {
  924. try {
  925. $sites = Capsule::table('sitePro_site')
  926. ->where('relid',$serviceID)
  927. ->value('name');
  928. } catch (\Exception $e) {
  929. logModuleCall(
  930. 'siteBuilder',
  931. __FUNCTION__,
  932. $params,
  933. 'Error: could fetch sites from database',
  934. $e->getMessage()
  935. );
  936. return 'Error: could fetch sites from database';
  937. }
  938. return $sites;
  939. }
  940. function getSitesEnabled($serviceID) {
  941. try {
  942. $sites = Capsule::table('sitePro_site')
  943. ->where('relid',$serviceID)
  944. ->where('enabled', 1)
  945. ->value('name');
  946. } catch (\Exception $e) {
  947. logModuleCall(
  948. 'siteBuilder',
  949. __FUNCTION__,
  950. $params,
  951. 'Error: could fetch sites from database',
  952. $e->getMessage()
  953. );
  954. return 'Error: could fetch sites from database';
  955. }
  956. return $sites;
  957. }
  958. function siteBuilderCreateTables() {
  959. // Create a new table.
  960. if (!Capsule::schema()->hasTable('sitePro_acc')) {
  961. try {
  962. Capsule::schema()->create(
  963. 'sitePro_acc',
  964. function ($table) { logModuleCall(
  965. 'siteBuilder',
  966. __FUNCTION__,
  967. $params,
  968. 'Debug',
  969. $site
  970. );
  971. /** @var \Illuminate\Database\Schema\Blueprint $table */
  972. $table->increments('id');
  973. $table->string('account');
  974. $table->integer('pid');
  975. $table->boolean('enabled');
  976. }
  977. );
  978. } catch (\Exception $e) {
  979. echo "Unable to create sitePro_acc: {$e->getMessage()}";
  980. }
  981. }
  982. if (!Capsule::schema()->hasTable('sitePro_site')) {
  983. try {
  984. Capsule::schema()->create(
  985. 'sitePro_site',
  986. function ($table) {
  987. /** @var \Illuminate\Database\Schema\Blueprint $table */
  988. $table->increments('id');
  989. $table->integer('relid');
  990. $table->string('name');
  991. $table->boolean('enabled');
  992. }
  993. );
  994. } catch (\Exception $e) {
  995. echo "Unable to create sitePro_site: {$e->getMessage()}";
  996. }
  997. }
  998. }