siteBuilder.php 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166
  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($status['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. logModuleCall(
  309. 'siteBuilder',
  310. __FUNCTION__,
  311. $params,
  312. 'Debug',
  313. $response
  314. );
  315. if($response['status'] != '200') {
  316. return 'Error: ' . $response['response']['error'];
  317. }
  318. }
  319. }
  320. return 'success';
  321. }
  322. /**
  323. * Set a siteBuilder account to status active and enable active sites
  324. *
  325. * Called when an un-suspension is requested. This is invoked
  326. * automatically upon payment of an overdue invoice for a product, or
  327. * can be called manually by admin user.
  328. *
  329. * @param array $params common module parameters
  330. *
  331. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  332. *
  333. * @return string 'success' or an error message
  334. */
  335. function siteBuilder_UnsuspendAccount($params) {
  336. $siteBuilder = new ApiClient(getSiteBuilderApiURL($params), $params['serveraccesshash']);
  337. // enable deafult site
  338. $response = $siteBuilder->enable($params['username'], $params['domain'], $params['serverusername'], $params['serverpassword']);
  339. if($response['status'] != '200') {
  340. return 'Error: ' . $response['response']['error'];
  341. }
  342. // set account to enabled in database
  343. try {
  344. Capsule::table('sitePro_acc')
  345. ->where('account',$params['username'])
  346. ->update(array(
  347. 'enabled' => true,
  348. ));
  349. } catch (\Exception $e) {
  350. logModuleCall(
  351. 'siteBuilder',
  352. __FUNCTION__,
  353. $params,
  354. 'Error: could update account in database',
  355. $e->getMessage()
  356. );
  357. return 'Error: could update account in database';
  358. }
  359. // enable active sites
  360. $sites = getSitesEnabled($params['serviceid']);
  361. logModuleCall(
  362. 'siteBuilder',
  363. __FUNCTION__,
  364. $params,
  365. 'Debug',
  366. $site
  367. );
  368. if(!empty($sites)) {
  369. foreach($sites as $site) {
  370. $response = $siteBuilder->enable($params['username'], $site, $params['serverusername'], $params['serverpassword']);
  371. if($response['status'] != '200') {
  372. return 'Error: ' . $response['response']['error'];
  373. }
  374. }
  375. }
  376. return 'success';
  377. }
  378. /**
  379. * Client area output logic handling.
  380. *
  381. * This function is used to define module specific client area output. It should
  382. * return an array consisting of a template file and optional additional
  383. * template variables to make available to that template.
  384. *
  385. * @param array $params common module parameters
  386. *
  387. * @see https://developers.whmcs.com/provisioning-modules/client-area-output/
  388. *
  389. * @return array
  390. */
  391. function siteBuilder_ClientArea($params) {
  392. $siteBuilder = new ApiClient(getSiteBuilderApiURL($params), $params['serveraccesshash']);
  393. $clientInfo = array('moduleclientarea' => '1');
  394. $clientInfo['domain'] = $params['domain'];
  395. $accEnabled = Capsule::table('sitePro_acc')
  396. ->where('pid', $params['serviceid'])
  397. ->value('enabled');
  398. $clientInfo['account'] = ['enabled' => $accEnabled];
  399. $clientInfo['sites'] = [];
  400. $sites = getSites($params['serviceid']);
  401. foreach($sites as $site){
  402. $response = $siteBuilder->getSSLDays($params['username'], $site);
  403. if($response['status'] == '200') {
  404. $sslSite = $response['response']['ssl_remaining'];
  405. }
  406. $response = $siteBuilder->isenabled($params['username'], $site);
  407. if($response['status'] == '200') {
  408. $enabled = $response['isenabled'];
  409. }
  410. logModuleCall(
  411. 'siteBuilder',
  412. __FUNCTION__,
  413. $site,
  414. 'Debug sites',
  415. $response
  416. );
  417. array_push($clientInfo['sites'],['name' => $site, 'sslSite' => $sslSite, 'enabled' => $enabled]);
  418. }
  419. $response = $siteBuilder->getQuota($params['username']);
  420. if($response['status'] != '200') {
  421. logModuleCall(
  422. 'siteBuilder',
  423. __FUNCTION__,
  424. $params,
  425. 'Error getting Quota',
  426. $response
  427. );
  428. }
  429. $clientInfo['quota'] = round($response['response']['quota'][0]['blocks']/1024);
  430. $clientInfo['limit'] = round($response['response']['quota'][0]['hard']/1024);
  431. return array(
  432. 'tabOverviewReplacementTemplate' => 'clientarea',
  433. 'vars' => $clientInfo,
  434. );
  435. }
  436. /**
  437. * Upgrade or downgrade a siteBuilder account by package.
  438. *
  439. * Called to apply any change in product assignment or parameters. It
  440. * is called to provision upgrade or downgrade orders, as well as being
  441. * able to be invoked manually by an admin user.
  442. *
  443. * This same function is called for upgrades and downgrades of both
  444. * products and configurable options.
  445. *
  446. * @param array $params common module parameters
  447. *
  448. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  449. *
  450. * @return string "success" or an error message
  451. */
  452. function siteBuilder_ChangePackage($params) {
  453. $siteBuilder = new ApiClient(getSiteBuilderApiURL($params), $params['serveraccesshash']);
  454. $response = $siteBuilder->setQuota($params['username'], $params['configoption3'], $params['serverusername'], $params['serverpassword']);
  455. if($response['status'] != '200') {
  456. return 'Error: ' . $response['response']['error'];
  457. }
  458. return 'success';
  459. }
  460. /**
  461. * Usage Update
  462. *
  463. * Important: Runs daily per server not per product
  464. * Run Manually: /admin/reports.php?report=disk_usage_summary&action=updatestats
  465. * @param array $params common module parameters
  466. *
  467. * @see https://developers.whmcs.com/provisioning-modules/usage-update/
  468. */
  469. function siteBuilder_UsageUpdate($params) {
  470. $siteBuilder = new ApiClient(getSiteBuilderApiURL($params), $params['serveraccesshash']);
  471. $response = $siteBuilder->getStats();
  472. if($response['status'] != '200') {
  473. logActivity('ERROR: Unable to update sitebuilder server usage: ' . implode('#',[$response]));
  474. }
  475. $stats = $response['response']['quota'];
  476. foreach($stats as $stat){
  477. try {
  478. Capsule::table('tblhosting')
  479. ->where('server', $params['serverid'])
  480. ->where('username', $stat['user'])
  481. ->update([
  482. 'diskusage' => $stat['used']/1024,
  483. 'disklimit' => $stat['hard']/1024,
  484. 'lastupdate' => Capsule::raw('now()'),
  485. ]);
  486. } catch (\Exception $e) {
  487. logActivity('ERROR: Unable to update sitebuilder server usage: ' . $e->getMessage());
  488. }
  489. logModuleCall(
  490. 'siteBuilder',
  491. __FUNCTION__,
  492. $stat,
  493. 'debug',
  494. $params
  495. );
  496. }
  497. }
  498. /**
  499. * Additional actions a client user can invoke.
  500. *
  501. * Define additional actions a client user can perform for an instance of a
  502. * product/service.
  503. *
  504. * Any actions you define here will be automatically displayed in the available
  505. * list of actions within the client area.
  506. *
  507. * @return array
  508. */
  509. function siteBuilder_ClientAreaCustomButtonArray ($params) {
  510. return array(
  511. 'Neue Webseite' => 'newSite',
  512. );
  513. }
  514. /**
  515. * Additional actions a client user can invoke.
  516. *
  517. * Define additional actions a client user is allowed to perform for an instance of a
  518. * product/service.
  519. *
  520. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  521. *
  522. * @return array
  523. */
  524. function siteBuilder_ClientAreaAllowedFunctions() {
  525. return array(
  526. 'Add Site' => 'addSite',
  527. 'New Site' => 'newSite',
  528. 'Confirm Delete Site' => 'delSiteConfirm',
  529. 'Delete Site' => 'delSite',
  530. 'Edit Site' => 'editSite',
  531. 'Conform Revert Site' => 'revSiteConfirm',
  532. 'Revert Site' => 'revSite',
  533. 'Disable Site' => 'disableSite',
  534. 'Enable Site' => 'enableSite'
  535. );
  536. }
  537. /**
  538. * Opens a form to add a new domain.
  539. *
  540. * @param array $params common module parameters
  541. *
  542. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  543. *
  544. * @return array template information
  545. */
  546. function siteBuilder_newSite($params) {
  547. return array(
  548. 'breadcrumb' => array(
  549. 'clientarea.php?action=productdetails&id=' . $params['serviceid'] . '&modop=custom&a=newSite' => 'Neue Webseite',
  550. ),
  551. 'templatefile' => 'siteBuilder_new_site',
  552. );
  553. }
  554. /**
  555. * Adds a new domain to a siteBuilder account.
  556. *
  557. * @param array $params common module parameters
  558. *
  559. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  560. *
  561. * @return string "success" or an error message
  562. */
  563. function siteBuilder_addSite($params) {
  564. if(empty($_POST['d'])) {
  565. $site = $params['domain'];
  566. } else {
  567. if(!filter_var($_POST['d'], FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME)){
  568. return 'Error: invalid site name';
  569. }
  570. $site = $_POST['d'] . '.' . $params['domain'];
  571. }
  572. // set DNS
  573. /* disabled on dev, has to be already set in test env
  574. $response = siteBuildersetDNS($params, $site);
  575. if($response != 'success') {
  576. return $response;
  577. }
  578. */
  579. $siteBuilder = new ApiClient(getSiteBuilderApiURL($params), $params['serveraccesshash']);
  580. // init prod
  581. $response = $siteBuilder->init($params['username'], $site, $params['serverusername'], $params['serverpassword']);
  582. if($response['status'] != '200') {
  583. return 'Error: ' . $response['response']['error'];
  584. }
  585. // update DB
  586. try {
  587. Capsule::table('sitePro_site')
  588. ->insert(
  589. array(
  590. 'relid' => $params['serviceid'],
  591. 'name' => $site,
  592. 'enabled' => true,
  593. )
  594. );
  595. } catch (\Exception $e) {
  596. logModuleCall(
  597. 'siteBuilder',
  598. __FUNCTION__,
  599. $params,
  600. 'Error: could save site & serviceid in database',
  601. $e->getMessage()
  602. );
  603. return 'Error: could save site & serviceid in database';
  604. }
  605. return 'success';
  606. }
  607. function siteBuilder_editSite($params) {
  608. if(!filter_var($_POST['s'], FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME)){
  609. return 'Error: invalid site name';
  610. }
  611. $site = $_POST['s'];
  612. $api = new SiteProApiClient('https://builder.thurdata.ch/api/', 'apikey0', '993yVHwC05TLsx2JI2XFlAhkkPUxR6JbQUYbI.a5HiRtmNV9');
  613. // use this for enterprise licenses and change 'your-bulder-domain.com' to your builder domain
  614. //$api = new SiteProApiClient('http://your-bulder-domain.com/api/', 'your_api_username', 'your_api_password');
  615. try {
  616. // this call is used to open builder, so you need to set correct parameters to represent users website you want to open
  617. // this data usually comes from your user/hosting manager system
  618. $res = $api->remoteCall('requestLogin', array(
  619. 'type' => 'internal', // (required) 'internal'
  620. 'domain' => $site, // (required) domain of the user website you want to edit
  621. 'lang' => 'de', // (optional) 2-letter language code, set language code you whant builder to open in
  622. 'apiUrl' => getSiteBuilderApiURL($params) . 'deploy/' . $params['username'] . '/' . $site, // (required) API endpoint URL
  623. 'resellerClientAccountId' => $params['serviceid'], // (required) ID of website/user in your system
  624. 'username' => $params['serverusername'], // (optional) authorization username to be used with API endpoint
  625. 'password' => 'your-secure-password', // (optional) authorization password to be used with API endpoint
  626. 'hostingPlan' => $params['configoption2'],
  627. ));
  628. if (!$res || !is_object($res)) {
  629. logModuleCall(
  630. 'siteBuilder',
  631. __FUNCTION__,
  632. $params,
  633. 'Error: Response format error',
  634. $res
  635. );
  636. return 'Error: Response format error';
  637. } else if (isset($res->url) && $res->url) {
  638. logModuleCall(
  639. 'siteBuilder',
  640. __FUNCTION__,
  641. $params,
  642. 'Debug',
  643. $res
  644. );
  645. // on success redirect to builder URL
  646. header('Location: '.$res->url, true);
  647. exit();
  648. } else {
  649. logModuleCall(
  650. 'siteBuilder',
  651. __FUNCTION__,
  652. $params,
  653. 'Error: Unknown error',
  654. $res
  655. );
  656. return 'Error: Unknown error';
  657. }
  658. } catch (\Exception $e) {
  659. logModuleCall(
  660. 'siteBuilder',
  661. __FUNCTION__,
  662. $params,
  663. 'Error: Request error',
  664. $e->getMessage()
  665. );
  666. return 'Error: Request error';
  667. }
  668. return 'success';
  669. }
  670. /**
  671. * Opens a form to delete a domain from a siteBuilder account.
  672. *
  673. * @param array $params common module parameters
  674. *
  675. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  676. *
  677. * @return array template information
  678. */
  679. function siteBuilder_delSiteConfirm($params) {
  680. return array(
  681. 'templatefile' => 'siteBuilder_del_site_confirm',
  682. 'vars' => array(
  683. 'delsite' => $_POST['s'],
  684. ),
  685. );
  686. }
  687. /**
  688. * Removes a domain from a siteBuilder account.
  689. *
  690. * @param array $params common module parameters
  691. *
  692. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  693. *
  694. * @return string "success" or an error message
  695. */
  696. function siteBuilder_delSite($params) {
  697. if(!filter_var($_POST['s'], FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME)){
  698. return 'Error: invalid domain name';
  699. }
  700. $site = $_POST['s'];
  701. $siteBuilder = new ApiClient(getSiteBuilderApiURL($params), $params['serveraccesshash']);
  702. // undeploy
  703. $response = $siteBuilder->undeploy($params['username'], $site, $params['serverusername'], $params['serverpassword']);
  704. if($response['status'] != '200') {
  705. return 'Error: ' . $response['response']['error'];
  706. }
  707. // remove builder session
  708. $api = new SiteProApiClient('https://builder.thurdata.ch/api/', 'apikey0', '993yVHwC05TLsx2JI2XFlAhkkPUxR6JbQUYbI.a5HiRtmNV9');
  709. // use this for enterprise licenses and change 'your-bulder-domain.com' to your builder domain
  710. //$api = new SiteProApiClient('http://your-bulder-domain.com/api/', 'your_api_username', 'your_api_password');
  711. try {
  712. // this call is used to open builder, so you need to set correct parameters to represent users website you want to open
  713. // this data usually comes from your user/hosting manager system
  714. $res = $api->remoteCall('requestLogin', array(
  715. 'type' => 'internal', // (required) 'internal'
  716. 'domain' => $site, // (required) domain of the user website you want to edit
  717. 'lang' => 'de', // (optional) 2-letter language code, set language code you whant builder to open in
  718. 'apiUrl' => getSiteBuilderApiURL($params) . 'deploy/' . $params['username'] . '/' . $site, // (required) API endpoint URL
  719. 'resellerClientAccountId' => $params['serviceid'], // (required) ID of website/user in your system
  720. 'username' => $params['serverusername'], // (optional) authorization username to be used with API endpoint
  721. 'password' => 'your-secure-password', // (optional) authorization password to be used with API endpoint
  722. ));
  723. if (!$res || !is_object($res)) {
  724. logModuleCall(
  725. 'siteBuilder',
  726. __FUNCTION__,
  727. $params,
  728. 'Error: Response format error',
  729. $res
  730. );
  731. return 'Error: Response format error';
  732. } else if (isset($res->url) && $res->url) {
  733. $result = $api->remoteCall('delete-site', array(
  734. 'domain' => $site
  735. ));
  736. if (!$result || !is_object($result)) {
  737. logModuleCall(
  738. 'siteBuilder',
  739. __FUNCTION__,
  740. $params,
  741. 'Error: Response format error',
  742. $result
  743. );
  744. return 'Error: Response format error';
  745. } else if (isset($result->ok) && $res->ok) {
  746. return 'success';
  747. }
  748. } else {
  749. logModuleCall(
  750. 'siteBuilder',
  751. __FUNCTION__,
  752. $params,
  753. 'Error: Unknown error',
  754. $res
  755. );
  756. return 'Error: Unknown error';
  757. }
  758. } catch (\Exception $e) {
  759. logModuleCall(
  760. 'siteBuilder',
  761. __FUNCTION__,
  762. $params,
  763. 'Error: Request error',
  764. $e->getMessage()
  765. );
  766. return 'Error: Request error';
  767. }
  768. // update DB
  769. try {
  770. Capsule::table('sitePro_site')
  771. ->where('name', $site)
  772. ->delete();
  773. } catch (\Exception $e) {
  774. logModuleCall(
  775. 'siteBuilder',
  776. __FUNCTION__,
  777. $params,
  778. 'Error: could remove site from database',
  779. $e->getMessage()
  780. );
  781. return 'Error: could remove site from database';
  782. }
  783. // unset DNS
  784. /* disabled on dev, has to be already set in test env
  785. $response = siteBuilderunsetDNS($params, $site);
  786. if($response != 'success') {
  787. return $response;
  788. }
  789. */
  790. return 'success';
  791. }
  792. /**
  793. * Opens a form to delete a domain from a siteBuilder account.
  794. *
  795. * @param array $params common module parameters
  796. *
  797. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  798. *
  799. * @return array template information
  800. */
  801. function siteBuilder_revSiteConfirm($params) {
  802. return array(
  803. 'templatefile' => 'siteBuilder_rev_site_confirm',
  804. 'vars' => array(
  805. 'revSite' => $_POST['s'],
  806. ),
  807. );
  808. }
  809. /**
  810. * Revert all Changes of the development Site.
  811. *
  812. * @param array $params common module parameters
  813. *
  814. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  815. *
  816. * @return string "success" or an error message
  817. */
  818. function siteBuilder_revSite($params) {
  819. if(!filter_var($_POST['s'], FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME)){
  820. return 'Error: invalid site name';
  821. }
  822. $site = $_POST['s'];
  823. $siteBuilder = new ApiClient(getSiteBuilderApiURL($params), $params['serveraccesshash']);
  824. $response = $siteBuilder->revert($params['username'], $site, $params['serverusername'], $params['serverpassword']);
  825. if($response['status'] != '200') {
  826. return 'Error: ' . $response['response']['error'];
  827. }
  828. // remove builder session
  829. $api = new SiteProApiClient('https://builder.thurdata.ch/api/', 'apikey0', '993yVHwC05TLsx2JI2XFlAhkkPUxR6JbQUYbI.a5HiRtmNV9');
  830. // use this for enterprise licenses and change 'your-bulder-domain.com' to your builder domain
  831. //$api = new SiteProApiClient('http://your-bulder-domain.com/api/', 'your_api_username', 'your_api_password');
  832. try {
  833. // this call is used to open builder, so you need to set correct parameters to represent users website you want to open
  834. // this data usually comes from your user/hosting manager system
  835. $res = $api->remoteCall('requestLogin', array(
  836. 'type' => 'internal', // (required) 'internal'
  837. 'domain' => $site, // (required) domain of the user website you want to edit
  838. 'lang' => 'de', // (optional) 2-letter language code, set language code you whant builder to open in
  839. 'apiUrl' => getSiteBuilderApiURL($params) . 'deploy/' . $params['username'] . '/' . $site, // (required) API endpoint URL
  840. 'resellerClientAccountId' => $params['serviceid'], // (required) ID of website/user in your system
  841. 'username' => $params['serverusername'], // (optional) authorization username to be used with API endpoint
  842. 'password' => 'your-secure-password', // (optional) authorization password to be used with API endpoint
  843. ));
  844. if (!$res || !is_object($res)) {
  845. logModuleCall(
  846. 'siteBuilder',
  847. __FUNCTION__,
  848. $params,
  849. 'Error: Response format error',
  850. $res
  851. );
  852. return 'Error: Response format error';
  853. } else if (isset($res->url) && $res->url) {
  854. $result = $api->remoteCall('delete-site', array(
  855. 'domain' => $site
  856. ));
  857. if (!$result || !is_object($result)) {
  858. logModuleCall(
  859. 'siteBuilder',
  860. __FUNCTION__,
  861. $params,
  862. 'Error: Response format error',
  863. $result
  864. );
  865. return 'Error: Response format error';
  866. } else if (isset($result->ok) && $res->ok) {
  867. return 'success';
  868. }
  869. } else {
  870. logModuleCall(
  871. 'siteBuilder',
  872. __FUNCTION__,
  873. $params,
  874. 'Error: Unknown error',
  875. $res
  876. );
  877. return 'Error: Unknown error';
  878. }
  879. } catch (\Exception $e) {
  880. logModuleCall(
  881. 'siteBuilder',
  882. __FUNCTION__,
  883. $params,
  884. 'Error: Request error',
  885. $e->getMessage()
  886. );
  887. return 'Error: Request error';
  888. }
  889. return 'success';
  890. }
  891. function siteBuilder_enableSite($params) {
  892. if(!filter_var($_POST['s'], FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME)){
  893. return 'Error: invalid site name';
  894. }
  895. $site = $_POST['s'];
  896. $siteBuilder = new ApiClient(getSiteBuilderApiURL($params), $params['serveraccesshash']);
  897. // enable
  898. $response = $siteBuilder->enable($params['username'], $site, $params['serverusername'], $params['serverpassword']);
  899. if($response['status'] != '200') {
  900. return 'Error: ' . $response['response']['error'];
  901. }
  902. // update DB
  903. try {
  904. Capsule::table('sitePro_site')
  905. ->where('relid',$params['serviceid'])
  906. ->where('name',$site)
  907. ->update(array(
  908. 'enabled' => true,
  909. ));
  910. } catch (\Exception $e) {
  911. logModuleCall(
  912. 'siteBuilder',
  913. __FUNCTION__,
  914. $params,
  915. 'Error: could save site status in database',
  916. $e->getMessage()
  917. );
  918. return 'Error: could save site status in database';
  919. }
  920. return 'success';
  921. }
  922. function siteBuilder_disableSite($params) {
  923. if(!filter_var($_POST['s'], FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME)){
  924. return 'Error: invalid site name';
  925. }
  926. $site = $_POST['s'];
  927. $siteBuilder = new ApiClient(getSiteBuilderApiURL($params), $params['serveraccesshash']);
  928. // disable
  929. $response = $siteBuilder->disable($params['username'], $site, $params['serverusername'], $params['serverpassword']);
  930. logModuleCall(
  931. 'siteBuilder',
  932. __FUNCTION__,
  933. $params,
  934. 'Debug',
  935. $response
  936. );
  937. if($response['status'] != '200') {
  938. return 'Error: ' . $response['response']['error'];
  939. }
  940. // update DB
  941. try {
  942. Capsule::table('sitePro_site')
  943. ->where('relid',$params['serviceid'])
  944. ->where('name',$site)
  945. ->update(array(
  946. 'enabled' => false,
  947. ));
  948. } catch (\Exception $e) {
  949. logModuleCall(
  950. 'siteBuilder',
  951. __FUNCTION__,
  952. $params,
  953. 'Error: could save site status in database',
  954. $e->getMessage()
  955. );
  956. return 'Error: could save site status in database';
  957. }
  958. return 'success';
  959. }
  960. // Helpers
  961. /**
  962. * Update a DNS zone for a domain setting a new record for a domain or subdomain of a CWP7 account.
  963. *
  964. * @param array $params common module parameters
  965. *
  966. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  967. *
  968. * @return string "success" or an error message
  969. */
  970. function siteBuildersetDNS($params, $site) {
  971. $siteName = $site . '.';
  972. $zoneRecords = array();
  973. $domainRecord = array(
  974. 'line' => $siteName.'|A|0',
  975. 'name' => $siteName,
  976. 'type' => 'A',
  977. 'class' => 'IN',
  978. 'data' => array(
  979. 'address' => $params['serverip'],
  980. ),
  981. );
  982. array_push($zoneRecords, $domainRecord);
  983. $zoneIDcollection = Capsule::table('dns_manager2_zone')
  984. ->select('id')
  985. ->where('name', '=', $params['domain'])
  986. ->where('clientid', '=', $params['userid'])
  987. ->get();
  988. $zoneIDobj = $zoneIDcollection[0];
  989. $zoneID = $zoneIDobj->{'id'};
  990. if(!isset($zoneID)) {
  991. return 'Error: Zone for domain ' . $params['domain'] . ' or not owned by client';
  992. }
  993. $dnsZone = localAPI('dnsmanager', array( 'dnsaction' => 'getZone', 'zone_id' => $zoneID));
  994. foreach($dnsZone['data']->records as $record) {
  995. if(($record->name != $siteName) || ($record->type != 'A' && $record->type != 'CNAME')) {
  996. array_push($zoneRecords, $record);
  997. };
  998. }
  999. $result = localAPI('dnsmanager' ,
  1000. array(
  1001. 'dnsaction' => 'updateZone',
  1002. 'zone_id' => $zoneID,
  1003. 'records' => $zoneRecords,
  1004. )
  1005. );
  1006. if($result['result'] != 'success') {
  1007. return 'Error: ' . $result['message'];
  1008. }
  1009. return 'success';
  1010. }
  1011. /**
  1012. * Removing a DNS record for a site of a siteBuilder account.
  1013. *
  1014. * @param array $params common module parameters
  1015. *
  1016. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  1017. *
  1018. * @return string "success" or an error message
  1019. */
  1020. function siteBuilderunsetDNS($params, $site) {
  1021. $siteName = $site . '.';
  1022. $zoneRecords = array();
  1023. $zoneIDcollection = Capsule::table('dns_manager2_zone')
  1024. ->select('id')
  1025. ->where('name', '=', $params['domain'])
  1026. ->where('clientid', '=', $params['userid'])
  1027. ->get();
  1028. $zoneIDobj = $zoneIDcollection[0];
  1029. $zoneID = $zoneIDobj->{'id'};
  1030. if(!isset($zoneID)) {
  1031. return 'Error: Zone for domain ' . $params['domain'] . ' or not owned by client';
  1032. }
  1033. $dnsZone = localAPI('dnsmanager', array( 'dnsaction' => 'getZone', 'zone_id' => $zoneID));
  1034. foreach($dnsZone['data']->records as $record) {
  1035. if(($record->name != $siteName) || ($record->type != 'A' && $record->type != 'CNAME')) {
  1036. array_push($zoneRecords, $record);
  1037. };
  1038. }
  1039. $result = localAPI('dnsmanager' ,
  1040. array(
  1041. 'dnsaction' => 'updateZone',
  1042. 'zone_id' => $zoneID,
  1043. 'records' => $zoneRecords,
  1044. )
  1045. );
  1046. if($result['result'] != 'success') {
  1047. return 'Error: ' . $result['message'];
  1048. }
  1049. return 'success';
  1050. }
  1051. /**
  1052. * Returns API Url .
  1053. *
  1054. * @param string $params common module parameters
  1055. * @param string $user
  1056. * @param string $params common module parameters
  1057. *
  1058. * @return string $apiUrl
  1059. */
  1060. function getSiteBuilderApiURL($params) {
  1061. $httpPrefix = $params['serversecure'] ? 'https://' : 'http://';
  1062. $serverPort = $params['serverport'] ? ':' . $params['serverport'] . '/' : '/';
  1063. return $httpPrefix . $params['serverhostname'] . $serverPort;
  1064. }
  1065. function getSites($serviceID) {
  1066. $sitesObj = Capsule::table('sitePro_site')
  1067. ->where('relid', $serviceID)
  1068. ->get();
  1069. $sites = [];
  1070. foreach($sitesObj as $site){
  1071. array_push($sites, $site->name);
  1072. }
  1073. return $sites;
  1074. }
  1075. function getSitesEnabled($serviceID) {
  1076. $sitesObj = Capsule::table('sitePro_site')
  1077. ->where('relid', $serviceID)
  1078. ->where('enabled', 1)
  1079. ->get();
  1080. $sites = [];
  1081. foreach($sitesObj as $site){
  1082. array_push($sites, $site->name);
  1083. }
  1084. return $sites;
  1085. }
  1086. function siteBuilderCreateTables() {
  1087. // Create a new table.
  1088. if (!Capsule::schema()->hasTable('sitePro_acc')) {
  1089. try {
  1090. Capsule::schema()->create(
  1091. 'sitePro_acc',
  1092. function ($table) { logModuleCall(
  1093. 'siteBuilder',
  1094. __FUNCTION__,
  1095. $params,
  1096. 'Debug',
  1097. $site
  1098. );
  1099. /** @var \Illuminate\Database\Schema\Blueprint $table */
  1100. $table->increments('id');
  1101. $table->string('account');
  1102. $table->integer('pid');
  1103. $table->boolean('enabled');
  1104. }
  1105. );
  1106. } catch (\Exception $e) {
  1107. echo "Unable to create sitePro_acc: {$e->getMessage()}";
  1108. }
  1109. }
  1110. if (!Capsule::schema()->hasTable('sitePro_site')) {
  1111. try {
  1112. Capsule::schema()->create(
  1113. 'sitePro_site',
  1114. function ($table) {
  1115. /** @var \Illuminate\Database\Schema\Blueprint $table */
  1116. $table->increments('id');
  1117. $table->integer('relid');
  1118. $table->string('name');
  1119. $table->boolean('enabled');
  1120. }
  1121. );
  1122. } catch (\Exception $e) {
  1123. echo "Unable to create sitePro_site: {$e->getMessage()}";
  1124. }
  1125. }
  1126. }