siteBuilder.php 35 KB

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