siteBuilder.php 30 KB

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