cwp7.php 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175
  1. <?php
  2. /**
  3. * WHMCS cwp7 Provisioning Module
  4. *
  5. * Provisioning for User Account on the cwp7 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/cwp7/Admin.php');
  14. if (!defined('WHMCS')) {
  15. die('This file cannot be accessed directly');
  16. }
  17. /**
  18. * Define CWP7 product metadata parameters.
  19. *
  20. * @see https://developers.whmcs.com/provisioning-modules/meta-data-params/
  21. *
  22. * @return array
  23. */
  24. function cwp7_MetaData() {
  25. return array(
  26. 'DisplayName' => 'CentOS Web Panel Provisioning',
  27. 'APIVersion' => '1.2',
  28. 'DefaultNonSSLPort' => '2031',
  29. 'DefaultSSLPort' => '2031',
  30. 'RequiresServer' => true,
  31. 'ServiceSingleSignOnLabel' => 'Login to CWP7',
  32. 'AdminSingleSignOnLabel' => 'Login to CWP7 Admin'
  33. );
  34. }
  35. /**
  36. * Test connection to a CWP7 server with the given server parameters.
  37. *
  38. * Allows an admin user to verify that an API connection can be
  39. * successfully made with the given configuration parameters for a
  40. * server.
  41. *
  42. * When defined in a module, a test connection button will appear
  43. * alongside the server type dropdown when adding or editing an
  44. * existing server.
  45. *
  46. * @param array $params common module parameters
  47. *
  48. * @see https://developers.whmcs.com/provisioning-modules/module-parameters/
  49. *
  50. * @return array
  51. */
  52. function cwp7_Testconnection($params) {
  53. $cwp7 = new cwp7_Admin($params['serverhostname'], $params['serveraccesshash']);
  54. $response = $cwp7->getServerType();
  55. if($response['status'] == 'OK') {
  56. return array(
  57. 'success' => true,
  58. 'error' => '',
  59. );
  60. }
  61. return array(
  62. 'success' => false,
  63. 'error' => $response['error_msg'],
  64. );
  65. }
  66. /**
  67. * Define CWP7 product configuration options.
  68. *
  69. * @see https://developers.whmcs.com/provisioning-modules/config-options/
  70. *
  71. * @return array
  72. */
  73. function cwp7_ConfigOptions() {
  74. $whmcs = App::self();
  75. $serverGroupID = $whmcs->get_req_var('servergroup');
  76. $serverIDObj = Capsule::table('tblservergroupsrel')
  77. ->select('serverid')
  78. ->where('groupid', '=', $serverGroupID)
  79. ->get();
  80. $serverIDs = array();
  81. foreach($serverIDObj as $serverID) {
  82. array_push($serverIDs, $serverID->serverid);
  83. }
  84. $server = Capsule::table('tblservers')
  85. ->select('hostname', 'accesshash')
  86. ->where('id', $serverIDs)
  87. ->where('active', '=', 1)
  88. ->first();
  89. $cwp7 = new cwp7_Admin($server->hostname, $server->accesshash);
  90. $cwp7Packages = $cwp7->getPackages();
  91. if($cwp7Packages['status'] != 'OK') {
  92. logModuleCall(
  93. 'cwp7',
  94. __FUNCTION__,
  95. $cwp7Packages['status'],
  96. 'Could not fetch packages',
  97. $cwp7Packages['error_msg']
  98. );
  99. return false;
  100. }
  101. $cwp7PackageNames = array();
  102. foreach($cwp7Packages['msj'] as $cwp7Package) {
  103. array_push($cwp7PackageNames, $cwp7Package['package_name']);
  104. }
  105. $configOptions = array();
  106. $configOptions['package'] = array(
  107. 'FriendlyName' => 'CWP7 Package',
  108. 'Type' => 'dropdown',
  109. 'Options' => implode(',', $cwp7PackageNames),
  110. 'Description' => 'Select CWP7 Package',
  111. );
  112. $configOptions['inode'] = array( "Type" => "text" , "Description" => "Max of inode", "Default" => "0",);
  113. $configOptions['nofile'] = array( "Type" => "text", "Description" => "Max of nofile", "Default" => "100",);
  114. $configOptions['nproc'] = array( "Type" => "text" , "Description" => "Nproc limit - 40 suggested", "Default" => "40",);
  115. $configOptions['Nameserver IP for lookups'] = array( "Type" => "text" , "Description" => "Name Server IP", "Default" => "185.163.51.142",);
  116. $configOptions['Name of own Nameserver'] = array( "Type" => "text" , "Description" => "Name Server Name", "Default" => "ns1.thurdata.ch",);
  117. return $configOptions;
  118. }
  119. /**
  120. * Provision a new account of a CWP7 server.
  121. *
  122. * Attempt to provision a new CWP7 account. This is
  123. * called any time provisioning is requested inside of WHMCS. Depending upon the
  124. * configuration, this can be any of:
  125. * * When a new order is placed
  126. * * When an invoice for a new order is paid
  127. * * Upon manual request by an admin user
  128. *
  129. * @param array $params common module parameters
  130. *
  131. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  132. *
  133. * @return string 'success' or an error message
  134. */
  135. function cwp7_CreateAccount($params) {
  136. $username = strtolower(substr($params['clientsdetails']['firstname'],0,2) . substr($params['clientsdetails']['lastname'],0,3)) . $params['serviceid'];
  137. $userdomain = $username . '.local';
  138. try {
  139. Capsule::table('tblhosting')
  140. ->where('id', '=', $params['serviceid'])
  141. ->update(
  142. array(
  143. 'username' => $username,
  144. 'domain' => $userdomain,
  145. )
  146. );
  147. } catch (\Exception $e) {
  148. logModuleCall(
  149. 'cwp7',
  150. __FUNCTION__,
  151. $params,
  152. 'Error: could save username & domain in database',
  153. $e->getMessage()
  154. );
  155. return 'Error: could save username & password in database';
  156. }
  157. if ($params["server"] == 1) {
  158. $data = array(
  159. 'package' => $params['configoption1'],
  160. 'domain' => $userdomain,
  161. 'user' => $username,
  162. 'pass' => $params['password'],
  163. 'email' => $params['clientsdetails']['email'],
  164. 'inode' => (int) $params["configoption2"],
  165. 'nofile' => (int) $params["configoption3"],
  166. 'nproc' => (int) $params["configoption4"],
  167. 'server_ips'=>$params["serverip"],
  168. );
  169. $cwp7 = new cwp7_Admin($params['serverhostname'], $params['serveraccesshash']);
  170. $response = $cwp7->createAccount($data);
  171. }
  172. if($response['status'] != 'OK') {
  173. return 'Error: ' . $response['error_msg'];
  174. }
  175. return 'success';
  176. }
  177. /**
  178. * Removes a CWP7 account.
  179. *
  180. * Called when a termination is requested. This can be invoked automatically for
  181. * overdue products if enabled, or requested manually by an admin user.
  182. *
  183. * @param array $params common module parameters
  184. *
  185. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  186. *
  187. * @return string 'success' or an error message
  188. */
  189. function cwp7_TerminateAccount($params) {
  190. $cwp7 = new cwp7_Admin($params['serverhostname'], $params['serveraccesshash']);
  191. $response = $cwp7->deleteAccount(array('user' => $params['username'], 'email' => $params['clientsdetails']['email']));
  192. if($response['status'] == 'Error') {
  193. return 'Error: ' . $response['msj'];
  194. }
  195. return 'success';
  196. }
  197. /**
  198. * Set a CWP7 account to status inactive.
  199. *
  200. * Called when a suspension is requested. This is invoked automatically by WHMCS
  201. * when a product becomes overdue on payment or can be called manually by admin
  202. * user.
  203. *
  204. * @param array $params common module parameters
  205. *
  206. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  207. *
  208. * @return string 'success' or an error message
  209. */
  210. function cwp7_SuspendAccount($params) {
  211. $cwp7 = new cwp7_Admin($params['serverhostname'], $params['serveraccesshash']);
  212. $response = $cwp7->suspendAccount($params['username']);
  213. if($response['status'] != 'OK') {
  214. return 'Error: ' . $response['error_msg'];
  215. }
  216. return 'success';
  217. }
  218. /**
  219. * Set a CWP7 account to status active.
  220. *
  221. * Called when an un-suspension is requested. This is invoked
  222. * automatically upon payment of an overdue invoice for a product, or
  223. * can be called manually by admin user.
  224. *
  225. * @param array $params common module parameters
  226. *
  227. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  228. *
  229. * @return string 'success' or an error message
  230. */
  231. function cwp7_UnsuspendAccount($params) {
  232. $cwp7 = new cwp7_Admin($params['serverhostname'], $params['serveraccesshash']);
  233. $response = $cwp7->unsuspendAccount($params['username']);
  234. if($response['status'] != 'OK') {
  235. return 'Error: ' . $response['error_msg'];
  236. }
  237. return 'success';
  238. }
  239. /**
  240. * Client area output logic handling.
  241. *
  242. * This function is used to define module specific client area output. It should
  243. * return an array consisting of a template file and optional additional
  244. * template variables to make available to that template.
  245. *
  246. * @param array $params common module parameters
  247. *
  248. * @see https://developers.whmcs.com/provisioning-modules/client-area-output/
  249. *
  250. * @return array
  251. */
  252. function cwp7_ClientArea($params) {
  253. $clientInfo = array('moduleclientarea' => '1');
  254. $cwp7 = new cwp7_Admin($params['serverhostname'], $params['serveraccesshash']);
  255. $response = $cwp7->getAutoSSL($params['username']);
  256. if($response['status'] == 'OK') {
  257. $sslSites = array();
  258. foreach($response['msj'] as $sslSite) {
  259. $sslSites[$sslSite['ssl']] = array(
  260. 'auotssl' => $sslSite['autossl'],
  261. 'expire' => $sslSite['exp'],
  262. );
  263. }
  264. }
  265. $response = $cwp7->getAccount($params['username']);
  266. if($response['status'] != 'OK') {
  267. logModuleCall(
  268. 'cwp7',
  269. __FUNCTION__,
  270. $params,
  271. 'debug',
  272. $response
  273. );
  274. }
  275. cwp7CheckLimit($params,'test');
  276. $domains = $response['result']['domains'];
  277. $subDomains = $response['result']['subdomins'];
  278. $clientInfo['domains'] = array();
  279. foreach($domains as $domain) {
  280. if($domain['path'] == '/home/' . $params['username'] . '/public_html') {
  281. $clientInfo['mgmtDomain'] = $domain['domain'];
  282. $clientInfo['mgmtEmail'] = $domain['email'];
  283. } else {
  284. $domain['relpath'] = str_replace('/home/' . $params['username'], '~', $domain['path']);
  285. if(array_key_exists($domain['domain'], $sslSites)) {
  286. $domain['ssl'] = 1;
  287. $domain['sslexpire'] = $sslSites[$domain['domain']]['expire'];
  288. $domain['autossl'] = $sslSites[$domain['domain']]['auotssl'];
  289. }
  290. if(cwp7CheckA($domain['domain'],$params['serverip'],$params['configoption5']) == 1) {
  291. $domain['DNS'] = 1;
  292. }
  293. $domain['domainNS'] = cwp7CheckSOA($domain['domain'],$params['configoption5'],$params['configoption6']);
  294. $domain['subdomains'] = array();
  295. foreach($subDomains as $subDomain) {
  296. if($subDomain['domain'] == $domain['domain']) {
  297. $subFQDN = $subDomain['subdomain'] . '.' . $subDomain['domain'];
  298. $subDomain['relpath'] = str_replace('/home/' . $params['username'], '~', $subDomain['path']);
  299. if(array_key_exists($subFQDN, $sslSites)) {
  300. $subDomain['ssl'] = 1;
  301. $subDomain['sslexpire'] = $sslSites[$subFQDN]['expire'];
  302. $subDomain['autossl'] = $sslSites[$subFQDN]['auotssl'];
  303. } else {
  304. unset($subDomain['ssl']);
  305. unset($subDomain['sslexpire']);
  306. unset($subDomain['autossl']);
  307. }
  308. if(cwp7CheckA($subFQDN,$params['serverip'],$params['configoption5']) == 1) {
  309. $subDomain['DNS'] = 1;
  310. } else {
  311. unset($subDomain['DNS']);
  312. }
  313. array_push($domain['subdomains'], $subDomain);
  314. }
  315. }
  316. array_push($clientInfo['domains'], $domain);
  317. }
  318. }
  319. return array(
  320. 'tabOverviewReplacementTemplate' => 'clientarea',
  321. 'vars' => $clientInfo,
  322. );
  323. }
  324. /**
  325. * Perform single sign-on for a CWP7 account.
  326. *
  327. * When successful, returns a URL to which the user should be redirected.
  328. *
  329. * @param array $params common module parameters
  330. *
  331. * @see https://developers.whmcs.com/provisioning-modules/single-sign-on/
  332. *
  333. * @return array
  334. */
  335. function cwp7_ServiceSingleSignOn($params) {
  336. $cwp7 = new cwp7_Admin($params['serverhostname'], $params['serveraccesshash']);
  337. $response = $cwp7->getLoginLink($params['username']);
  338. if($response['status'] == 'OK') {
  339. $link = $response['msj']['details'];
  340. $linkautologin = $link[0]['url'];
  341. return array(
  342. 'success' => true,
  343. 'redirectTo' => $linkautologin,
  344. );
  345. } else {
  346. return array(
  347. 'success' => false,
  348. 'redirectTo' => '',
  349. );
  350. }
  351. }
  352. /**
  353. * Change the password for a CWP7 account.
  354. *
  355. * Called when a password change is requested. This can occur either due to a
  356. * client requesting it via the client area or an admin requesting it from the
  357. * admin side.
  358. *
  359. * This option is only available to client end users when the product is in an
  360. * active status.
  361. *
  362. * @param array $params common module parameters
  363. *
  364. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  365. *
  366. * @return string "success" or an error message
  367. */
  368. function cwp7_ChangePassword($params) {
  369. $cwp7 = new cwp7_Admin($params['serverhostname'], $params['serveraccesshash']);
  370. $response = $cwp7->changePass(array('user' => $params['username'], 'password' => $params['password']));
  371. if($response['status'] != 'OK') {
  372. return 'Error: ' . $response['error_msg'];
  373. }
  374. return 'success';
  375. }
  376. /**
  377. * Upgrade or downgrade a CWP7 account by package.
  378. *
  379. * Called to apply any change in product assignment or parameters. It
  380. * is called to provision upgrade or downgrade orders, as well as being
  381. * able to be invoked manually by an admin user.
  382. *
  383. * This same function is called for upgrades and downgrades of both
  384. * products and configurable options.
  385. *
  386. * @param array $params common module parameters
  387. *
  388. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  389. *
  390. * @return string "success" or an error message
  391. */
  392. function cwp7_ChangePackage($params) {
  393. $cwp7 = new cwp7_Admin($params['serverhostname'], $params['serveraccesshash']);
  394. $data = array(
  395. 'user' => $params['username'],
  396. 'email' => $params['clientsdetails']['email'],
  397. 'package' => $params['configoption1'],
  398. 'inode' => (int) $params["configoption2"],
  399. 'openfiles' => (int) $params["configoption3"],
  400. 'processes' => (int) $params["configoption4"],
  401. 'server_ips'=> $params["serverip"],
  402. );
  403. $response = $cwp7->modifyAccount($data);
  404. if($response['status'] != 'OK') {
  405. return 'Error: ' . $response['error_msg'];
  406. }
  407. return 'success';
  408. }
  409. /**
  410. * Usage Update
  411. *
  412. * Important: Runs daily per server not per product
  413. * Run Manually: /admin/reports.php?report=disk_usage_summary&action=updatestats
  414. * @param array $params common module parameters
  415. *
  416. * @see https://developers.whmcs.com/provisioning-modules/usage-update/
  417. */
  418. function cwp7_UsageUpdate($params) {
  419. $cwp7 = new cwp7_Admin($params['serverhostname'], $params['serveraccesshash']);
  420. $response = $cwp7->getAllAccounts();
  421. if($response['status'] == 'OK'){
  422. $results = $response['msj'];
  423. for($i = 0; $i < count($results); $i++){
  424. if($results[$i]['diskusage'] == '') {
  425. $diskusage = 0;
  426. } else {
  427. $diskusage = trim($results[$i]['diskusage']);
  428. }
  429. if($results[$i]['disklimit'] == '') {
  430. $disklimit = 0;
  431. } else {
  432. $disklimit = trim($results[$i]['disklimit']);
  433. }
  434. if($results[$i]['bandwidth'] == '') {
  435. $bandwidth = 0;
  436. } else {
  437. $bandwidth =trim($results[$i]['bandwidth']);
  438. }
  439. if($results[$i]['bwlimit'] == '') {
  440. $bwlimit = 0;
  441. } else {
  442. $bwlimit = trim($results[$i]['bwlimit']);
  443. }
  444. $domain = trim($results[$i]['domain']);
  445. try {
  446. \WHMCS\Database\Capsule::table('tblhosting')
  447. ->where('server', $params['serverid'])
  448. ->where('domain', $domain)
  449. ->update([
  450. 'diskusage' => $diskusage,
  451. 'disklimit' => $disklimit,
  452. 'bwusage' => $bandwidth,
  453. 'bwlimit' => $bwlimit,
  454. 'lastupdate' => date('Y-m-d H:i:S'),
  455. ]);
  456. } catch (\Exception $e) {
  457. logActivity('ERROR: Unable to update server usage: ' . $e->getMessage());
  458. }
  459. }
  460. }
  461. }
  462. /**
  463. * Additional actions a client user can invoke.
  464. *
  465. * Define additional actions a client user can perform for an instance of a
  466. * product/service.
  467. *
  468. * Any actions you define here will be automatically displayed in the available
  469. * list of actions within the client area.
  470. *
  471. * @return array
  472. */
  473. function cwp7_ClientAreaCustomButtonArray ($params) {
  474. return array(
  475. 'Neue Domain' => 'newDomain',
  476. );
  477. }
  478. /**
  479. * Additional actions a client user can invoke.
  480. *
  481. * Define additional actions a client user is allowed to perform for an instance of a
  482. * product/service.
  483. *
  484. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  485. *
  486. * @return array
  487. */
  488. function cwp7_ClientAreaAllowedFunctions() {
  489. return array(
  490. "Enable SSL" => "enableSSL",
  491. "Renew SSL" => "renewSSL",
  492. "Set DNS" => "setDNS",
  493. "Unset DNS" => "unsetDNS",
  494. "Confirm Enable SSL" => "enableSSLConfirm",
  495. "Confirm Renew SSL" => "renewSSLConfirm",
  496. "Confirm Set DNS" => "setDNSConfirm",
  497. "Confirm Unset DNS" => "unsetDNSConfirm",
  498. "Info DNS" => "infoDNS",
  499. "Info SSL" => "infoSSL",
  500. "Add Domain" => "addDomain",
  501. "Add Subdomain" => "addSubdomain",
  502. "New Subdomain" => "newSubdomain",
  503. "Confirm Delete Domain" => "delDomainConfirm",
  504. "Delete Domain" => "delDomain",
  505. "Confirm Delete Subdomain" => "delSubdomainConfirm",
  506. "Delete Subdomain" => "delSubdomain",
  507. );
  508. }
  509. /**
  510. * Opens a form to add a new domain.
  511. *
  512. * @param array $params common module parameters
  513. *
  514. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  515. *
  516. * @return array template information
  517. */
  518. function cwp7_newDomain($params) {
  519. return array(
  520. 'breadcrumb' => array(
  521. 'clientarea.php?action=productdetails&id=' . $params['serviceid'] . '&modop=custom&a=newDomain' => 'Neue Domain',
  522. ),
  523. 'templatefile' => 'cwp7_add_domain',
  524. );
  525. }
  526. /**
  527. * Adds a new domain to a CWP7 account.
  528. *
  529. * @param array $params common module parameters
  530. *
  531. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  532. *
  533. * @return string "success" or an error message
  534. */
  535. function cwp7_addDomain($params) {
  536. if(!filter_var($_POST['d'], FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME)){
  537. return 'Error: invalid domain name';
  538. }
  539. $vars['user'] = $params['username'];
  540. $vars['name'] = $_POST['d'];
  541. $vars['type'] = 'domain';
  542. $cwp7 = new cwp7_Admin($params['serverhostname'], $params['serveraccesshash']);
  543. $response = $cwp7->addDomain($vars);
  544. if($response['status'] != 'OK') {
  545. return 'Error: ' . $response['error_msg'];
  546. }
  547. return 'success';
  548. }
  549. /**
  550. * Opens a form to add a new subdomain to a domain.
  551. *
  552. * @param array $params common module parameters
  553. *
  554. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  555. *
  556. * @return array template information
  557. */
  558. function cwp7_newSubdomain($params) {
  559. if(!filter_var($_POST['d'], FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME)){
  560. return 'Error: invalid domain name';
  561. }
  562. return array(
  563. 'breadcrumb' => array(
  564. 'clientarea.php?action=productdetails&id=' . $params['serviceid'] . '&modop=custom&a=newSubdomain' => 'Neue Subdomain',
  565. ),
  566. 'templatefile' => 'cwp7_add_subdomain',
  567. 'vars' => array(
  568. 'domainselected' => $_POST['d'],
  569. ),
  570. );
  571. }
  572. /**
  573. * Adds a new subdomain to domain of a CWP7 account.
  574. *
  575. * @param array $params common module parameters
  576. *
  577. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  578. *
  579. * @return string "success" or an error message
  580. */
  581. function cwp7_addSubdomain($params) {
  582. if(!filter_var($_POST['d'], FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME)){
  583. return 'Error: invalid domain name';
  584. }
  585. if(!filter_var($_POST['s'], FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME)){
  586. return 'Error: invalid subdomain name';
  587. }
  588. if($_POST['s'] == 'www') {
  589. return 'Error: default Subdomain www wurde bereits automatisch erstellt' ;
  590. }
  591. $vars['user'] = $params['username'];
  592. $vars['name'] = $_POST['s'] . '.' . $_POST['d'];
  593. $vars['type'] = 'subdomain';
  594. $cwp7 = new cwp7_Admin($params['serverhostname'], $params['serveraccesshash']);
  595. $response = $cwp7->addDomain($vars);
  596. if($response['status'] != 'OK') {
  597. return 'Error: ' . $response['error_msg'];
  598. }
  599. return 'success';
  600. }
  601. /**
  602. * Opens a form to delete a domain from a CWP7 account.
  603. *
  604. * @param array $params common module parameters
  605. *
  606. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  607. *
  608. * @return array template information
  609. */
  610. function cwp7_delDomainConfirm($params) {
  611. return array(
  612. 'templatefile' => 'cwp7_del_domain_confirm',
  613. 'vars' => array(
  614. 'deldomain' => $_POST['d'],
  615. ),
  616. );
  617. }
  618. /**
  619. * Removes a domain from a CWP7 account.
  620. *
  621. * @param array $params common module parameters
  622. *
  623. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  624. *
  625. * @return string "success" or an error message
  626. */
  627. function cwp7_delDomain($params) {
  628. if(!filter_var($_POST['d'], FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME)){
  629. return 'Error: invalid domain name';
  630. }
  631. $cwp7 = new cwp7_Admin($params['serverhostname'], $params['serveraccesshash']);
  632. $response = $cwp7->getAccount($params['username']);
  633. if($response['status'] != 'OK') {
  634. logModuleCall(
  635. 'cwp7',
  636. __FUNCTION__,
  637. $params,
  638. 'debug',
  639. $response
  640. );
  641. }
  642. $domains = $response['result']['domains'];
  643. $clientdomains = array();
  644. foreach($domains as $domain){
  645. if($domain['domain'] != $params['domain']) {
  646. array_push($clientdomains, $domain['domain']);
  647. }
  648. }
  649. if(!in_array($_POST['d'], $clientdomains)) {
  650. logModuleCall(
  651. 'cwp7',
  652. __FUNCTION__,
  653. $_POST,
  654. 'POST DATA VIOLATION',
  655. $params
  656. );
  657. return 'Error: ' . $_POST['d'] . ' not in client domains';
  658. }
  659. // do delete domain
  660. $vars['user'] = $params['username'];
  661. $vars['name'] = $_POST['d'];
  662. $vars['type'] = 'domain';
  663. $response = $cwp7->deleteDomain($vars);
  664. if($response['status'] != 'OK') {
  665. return 'Error: ' . $response['error_msg'];
  666. }
  667. return 'success';
  668. }
  669. /**
  670. * Opens a form to delete a subdomain from domain of a CWP7 account.
  671. *
  672. * @param array $params common module parameters
  673. *
  674. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  675. *
  676. * @return array template information
  677. */
  678. function cwp7_delSubdomainConfirm($params) {
  679. return array(
  680. 'templatefile' => 'cwp7_del_subdomain_confirm',
  681. 'vars' => array(
  682. 'delsubdomain' => $_POST['d'],
  683. ),
  684. );
  685. }
  686. /**
  687. * Removes a subdomain from a domain of a CWP7 account.
  688. *
  689. * @param array $params common module parameters
  690. *
  691. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  692. *
  693. * @return string "success" or an error message
  694. */
  695. function cwp7_delSubdomain($params) {
  696. if(!filter_var($_POST['d'], FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME)){
  697. return 'Error: invalid domain name';
  698. }
  699. $cwp7 = new cwp7_Admin($params['serverhostname'], $params['serveraccesshash']);
  700. $response = $cwp7->getAccount($params['username']);
  701. if($response['status'] != 'OK') {
  702. logModuleCall(
  703. 'cwp7',
  704. __FUNCTION__,
  705. $params,
  706. 'debug',
  707. $response
  708. );
  709. }
  710. $subdomains = $response['result']['subdomins'];
  711. $clientsubdomains = array();
  712. foreach($subdomains as $subdomain){
  713. if($subdomain['domain'] != $params['domain']) {
  714. array_push($clientsubdomains, $subdomain['subdomain'] . "." . $subdomain['domain']);
  715. }
  716. }
  717. if(!in_array($_POST['d'], $clientsubdomains)) {
  718. logModuleCall(
  719. 'cwp7',
  720. __FUNCTION__,
  721. $_POST,
  722. 'POST DATA VIOLATION',
  723. $params
  724. );
  725. return 'Error: ' . $_POST['d'] . ' not in client subdomains';
  726. }
  727. // do delete subdomain
  728. $vars['user'] = $params['username'];
  729. $vars['name'] = $_POST['d'];
  730. $vars['type'] = 'subdomain';
  731. $response = $cwp7->deleteDomain($vars);
  732. if($response['status'] != 'OK') {
  733. return 'Error: ' . $response['error_msg'];
  734. }
  735. return 'success';
  736. }
  737. /**
  738. * Opens a form to enable SSL for a subdomain or domain of a CWP7 account.
  739. *
  740. * @param array $params common module parameters
  741. *
  742. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  743. *
  744. * @return array template information
  745. */
  746. function cwp7_enableSSLConfirm($params) {
  747. return array(
  748. 'templatefile' => 'cwp7_enable_SSL_confirm',
  749. 'vars' => array(
  750. 'SSLdomain' => $_POST['d'],
  751. ),
  752. );
  753. }
  754. /**
  755. * Aktivate CWP7 AutoSSL for a subdomain or domain of a CWP7 account.
  756. *
  757. * @param array $params common module parameters
  758. *
  759. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  760. *
  761. * @return string "success" or an error message
  762. */
  763. function cwp7_enableSSL($params) {
  764. if(!filter_var($_POST['d'], FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME)){
  765. return 'Error: invalid domain name';
  766. }
  767. $vars['user'] = $params['username'];
  768. $vars['name'] = $_POST['d'];
  769. $cwp7 = new cwp7_Admin($params['serverhostname'], $params['serveraccesshash']);
  770. $response = $cwp7->addAutoSSL($vars);
  771. if($response['status'] != 'OK') {
  772. return 'Error: ' . $response['error_msg'];
  773. }
  774. return 'success';
  775. }
  776. /**
  777. * Opens a form to renew a SSL certificate for a subdomain or domain of a CWP7 account.
  778. *
  779. * @param array $params common module parameters
  780. *
  781. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  782. *
  783. * @return array template information
  784. */
  785. function cwp7_renewSSLConfirm($params) {
  786. return array(
  787. 'templatefile' => 'cwp7_renew_SSL_confirm',
  788. 'vars' => array(
  789. 'SSLdomain' => $_POST['d'],
  790. ),
  791. );
  792. }
  793. /**
  794. * Renews a SSL certificate for a subdomain or domain of a CWP7 account.
  795. *
  796. * @param array $params common module parameters
  797. *
  798. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  799. *
  800. * @return string "success" or an error message
  801. */
  802. function cwp7_renewSSL($params) {
  803. if(!filter_var($_POST['d'], FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME)){
  804. return 'Error: invalid domain name';
  805. }
  806. $vars['user'] = $params['username'];
  807. $vars['name'] = $_POST['d'];
  808. $cwp7 = new cwp7_Admin($params['serverhostname'], $params['serveraccesshash']);
  809. $response = $cwp7->updateAutoSSL($vars);
  810. if($response['status'] != 'OK') {
  811. return 'Error: ' . $response['error_msg'];
  812. }
  813. return 'success';
  814. }
  815. /**
  816. * Opens a form to set a DNS record for a subdomain or domain of a CWP7 account.
  817. *
  818. * @param array $params common module parameters
  819. *
  820. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  821. *
  822. * @return array template information
  823. */
  824. function cwp7_setDNSConfirm($params) {
  825. if(isset($_POST['s'])){
  826. return array(
  827. 'templatefile' => 'cwp7_set_DNS_confirm',
  828. 'vars' => array(
  829. 'DNSdomain' => $_POST['d'],
  830. 'DNSsubdomain' => $_POST['s'],
  831. ),
  832. );
  833. }
  834. return array(
  835. 'templatefile' => 'cwp7_set_DNS_confirm',
  836. 'vars' => array(
  837. 'DNSdomain' => $_POST['d'],
  838. ),
  839. );
  840. }
  841. /**
  842. * Opens a form to unsset a DNS record for a subdomain or domain of a CWP7 account.
  843. *
  844. * @param array $params common module parameters
  845. *
  846. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  847. *
  848. * @return array template information
  849. */
  850. function cwp7_unsetDNSConfirm($params) {
  851. if(isset($_POST['s'])){
  852. return array(
  853. 'templatefile' => 'cwp7_unset_DNS_confirm',
  854. 'vars' => array(
  855. 'DNSdomain' => $_POST['d'],
  856. 'DNSsubdomain' => $_POST['s'],
  857. ),
  858. );
  859. }
  860. return array(
  861. 'templatefile' => 'cwp7_unset_DNS_confirm',
  862. 'vars' => array(
  863. 'DNSdomain' => $_POST['d'],
  864. ),
  865. );
  866. }
  867. /**
  868. * Update a DNS zone for a domain setting a new record for a domain or subdomain of a CWP7 account.
  869. *
  870. * @param array $params common module parameters
  871. *
  872. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  873. *
  874. * @return string "success" or an error message
  875. */
  876. function cwp7_setDNS($params) {
  877. if(!filter_var($_POST['d'], FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME)){
  878. return 'Error: invalid domain name';
  879. }
  880. $domainName = $_POST['d'];
  881. $zoneRecords = array();
  882. if(isset($_POST['s'])){
  883. if(!filter_var($_POST['s'], FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME)){
  884. return 'Error: invalid subdomain name';
  885. }
  886. $hostName = $_POST['s'] . '.' . $domainName . '.';
  887. $newRecord = array(
  888. 'line' => $hostName.'|A|0',
  889. 'name' => $hostName,
  890. 'type' => 'A',
  891. 'class' => 'IN',
  892. 'data' => array(
  893. 'address' => $params['serverip'],
  894. ),
  895. );
  896. array_push($zoneRecords, $newRecord);
  897. } else {
  898. $hostName = $domainName . '.';
  899. $domainRecord = array(
  900. 'line' => $hostName.'|A|0',
  901. 'name' => $hostName,
  902. 'type' => 'A',
  903. 'class' => 'IN',
  904. 'data' => array(
  905. 'address' => $params['serverip'],
  906. ),
  907. );
  908. array_push($zoneRecords, $domainRecord);
  909. $wwwRecord = array(
  910. 'line' => 'www'.$hostName.'|A|0',
  911. 'name' => 'www'.$hostName,
  912. 'type' => 'A',
  913. 'class' => 'IN',
  914. 'data' => array(
  915. 'address' => $params['serverip'],
  916. ),
  917. );
  918. array_push($zoneRecords, $wwwRecord);
  919. }
  920. $zoneIDcollection = Capsule::table('dns_manager2_zone')
  921. ->select('id')
  922. ->where('name', '=', $domainName)
  923. ->where('clientid', '=', $params['userid'])
  924. ->get();
  925. $zoneIDobj = $zoneIDcollection[0];
  926. $zoneID = $zoneIDobj->{'id'};
  927. if(!isset($zoneID)) {
  928. return 'Error: Zone for domain ' . $domainName . ' or not owned by client';
  929. }
  930. $dnsZone = localAPI('dnsmanager', array( 'dnsaction' => 'getZone', 'zone_id' => $zoneID));
  931. foreach($dnsZone['data']->records as $record) {
  932. if(($record->name != $hostName) || ($record->type != 'A' && $record->type != 'CNAME')) {
  933. array_push($zoneRecords, $record);
  934. };
  935. }
  936. $result = localAPI('dnsmanager' ,
  937. array(
  938. 'dnsaction' => 'updateZone',
  939. 'zone_id' => $zoneID,
  940. 'records' => $zoneRecords,
  941. )
  942. );
  943. if($result['result'] != 'success') {
  944. return 'Error: ' . $result['message'];
  945. }
  946. return 'success';
  947. }
  948. /**
  949. * Removing a DNS record for a domain or subdomain of a CWP7 account.
  950. *
  951. * @param array $params common module parameters
  952. *
  953. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  954. *
  955. * @return string "success" or an error message
  956. */
  957. function cwp7_unsetDNS($params) {
  958. if(!filter_var($_POST['d'], FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME)){
  959. return 'Error: invalid domain name';
  960. }
  961. $domainName = $_POST['d'];
  962. $zoneRecords = array();
  963. if(isset($_POST['s'])){
  964. if(!filter_var($_POST['s'], FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME)){
  965. return 'Error: invalid subdomain name';
  966. }
  967. $hostName = $_POST['s'] . '.' . $domainName . '.';
  968. } else {
  969. $hostName = $domainName . '.';
  970. }
  971. $zoneIDcollection = Capsule::table('dns_manager2_zone')
  972. ->select('id')
  973. ->where('name', '=', $domainName)
  974. ->where('clientid', '=', $params['userid'])
  975. ->get();
  976. $zoneIDobj = $zoneIDcollection[0];
  977. $zoneID = $zoneIDobj->{'id'};
  978. if(!isset($zoneID)) {
  979. return 'Error: Zone for domain ' . $domainName . ' or not owned by client';
  980. }
  981. $dnsZone = localAPI('dnsmanager', array( 'dnsaction' => 'getZone', 'zone_id' => $zoneID));
  982. foreach($dnsZone['data']->records as $record) {
  983. if(($record->name != $hostName) || ($record->type != 'A' && $record->type != 'CNAME')) {
  984. array_push($zoneRecords, $record);
  985. };
  986. }
  987. $result = localAPI('dnsmanager' ,
  988. array(
  989. 'dnsaction' => 'updateZone',
  990. 'zone_id' => $zoneID,
  991. 'records' => $zoneRecords,
  992. )
  993. );
  994. if($result['result'] != 'success') {
  995. return 'Error: ' . $result['message'];
  996. }
  997. return 'success';
  998. }
  999. /**
  1000. * Opens a form to inform about the DNS status of a subdomain or domain of a CWP7 account.
  1001. *
  1002. * @param array $params common module parameters
  1003. *
  1004. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  1005. *
  1006. * @return array template information
  1007. */
  1008. function cwp7_infoDNS($params) {
  1009. if(!filter_var($_POST['d'], FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME)){
  1010. return 'Error: invalid domain name';
  1011. }
  1012. $cwp7nameserver = cwp7CheckSOA($_POST['d'],$params['configoption5'],$params['configoption6']);
  1013. return array(
  1014. 'templatefile' => 'cwp7_help_dns',
  1015. 'vars' => array(
  1016. 'infodomain' => $_POST['d'],
  1017. 'cwp7nameserver' => $cwp7nameserver,
  1018. ),
  1019. );
  1020. }
  1021. /**
  1022. * Opens a form to inform about the SSL status of a subdomain or domain of a CWP7 account.
  1023. *
  1024. * @param array $params common module parameters
  1025. *
  1026. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  1027. *
  1028. * @return array template information
  1029. */
  1030. function cwp7_infoSSL($params) {
  1031. if(!filter_var($_POST['d'], FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME)){
  1032. return 'Error: invalid domain name';
  1033. }
  1034. return array(
  1035. 'templatefile' => 'cwp7_help_ssl',
  1036. 'vars' => array(
  1037. 'infodomain' => $_POST['d'],
  1038. ),
  1039. );
  1040. }
  1041. /**
  1042. * Ask nameservers for a IP adress of a given host.
  1043. *
  1044. * @param string $host hostname
  1045. * @param string $serverIP CWP7 server IP
  1046. * @param string $nameserverIP polled name server IP
  1047. * @param int $recurse optional -> used to follow CNAME responses
  1048. *
  1049. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  1050. *
  1051. * @return bool
  1052. */
  1053. function cwp7CheckA($host, $serverIP, $nameserverIP, $recurse = 0) {
  1054. if($recurse > 3) {
  1055. return false;
  1056. }
  1057. $nameserver = array($nameserverIP);
  1058. $resolver = new Net_DNS2_Resolver(array('nameservers' => $nameserver));
  1059. try {
  1060. $result = $resolver->query($host, 'A');
  1061. } catch(Net_DNS2_Exception $e) {
  1062. logModuleCall(
  1063. 'cwp7',
  1064. __FUNCTION__,
  1065. $e,
  1066. 'DNS lookup exception',
  1067. $e->getMessage()
  1068. );
  1069. }
  1070. $hostA = $result->answer;
  1071. if($hostA[0]->type == 'CNAME') {
  1072. if(cwp7CheckA($hostA[0]->cname, $serverIP, $nameserverIP, $recurse++)) {
  1073. return true;
  1074. }
  1075. }
  1076. if($hostA[0]->type == 'A') {
  1077. if($hostA[0]->address == $serverIP){
  1078. return true;
  1079. }
  1080. }
  1081. return false;
  1082. }
  1083. /**
  1084. * Ask nameservers for the authority of a domain.
  1085. *
  1086. * @param string $domain domain name
  1087. * @param string $nameserverIP polled name server IP
  1088. * @param string $nameserverName name of the own namesever
  1089. *
  1090. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  1091. *
  1092. * @return string 'none' -> not registered, 'self' -> registered at own or the name of an other responsible nameserver
  1093. */
  1094. function cwp7CheckSOA($domain, $nameserverIP, $nameserverName ) {
  1095. $nameserver = array($nameserverIP);
  1096. $resolver = new Net_DNS2_Resolver(array('nameservers' => $nameserver));
  1097. try {
  1098. $result = $resolver->query($domain, 'SOA');
  1099. } catch(Net_DNS2_Exception $e) {
  1100. logModuleCall(
  1101. 'cwp7',
  1102. __FUNCTION__,
  1103. $e,
  1104. 'DNS lookup exception',
  1105. $e->getMessage()
  1106. );
  1107. return 'none';
  1108. }
  1109. if($result->answer[0]->mname == $nameserverName) {
  1110. return 'self';
  1111. }
  1112. return $result->answer[0]->mname;
  1113. }
  1114. /**
  1115. * Check limits for a service of an account .
  1116. *
  1117. * @param array $params common module parameters
  1118. * @param string $service target service for check
  1119. *
  1120. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  1121. *
  1122. * @return bool 'none' -> not registered, 'self' -> registered at own or the name of an other responsible nameserver
  1123. */
  1124. function cwp7CheckLimit($params, $service) {
  1125. $cwp7 = new cwp7_Admin($params['serverhostname'], $params['serveraccesshash']);
  1126. $response = $cwp7->getQuota($params['username']);
  1127. logModuleCall(
  1128. 'cwp7',
  1129. __FUNCTION__,
  1130. $response,
  1131. 'debug',
  1132. $service
  1133. );
  1134. }