siteBuilder.php 31 KB

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