siteBuilder.php 34 KB

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