siteBuilder.php 34 KB

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