cwp7.php 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190
  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. if(cwp7CheckLimit($params,'domain')){
  276. $clientInfo['domainlimit'] = 1;
  277. } else {
  278. $clientInfo['domainlimit'] = 0;
  279. };
  280. if(cwp7CheckLimit($params,'subdomain')){
  281. $clientInfo['subdomainlimit'] = 1;
  282. } else {
  283. $clientInfo['subdomainlimit'] = 0;
  284. };
  285. $domains = $response['result']['domains'];
  286. $subDomains = $response['result']['subdomins'];
  287. $clientInfo['domains'] = array();
  288. foreach($domains as $domain) {
  289. if($domain['path'] == '/home/' . $params['username'] . '/public_html') {
  290. $clientInfo['mgmtDomain'] = $domain['domain'];
  291. $clientInfo['mgmtEmail'] = $domain['email'];
  292. } else {
  293. $domain['relpath'] = str_replace('/home/' . $params['username'], '~', $domain['path']);
  294. if(array_key_exists($domain['domain'], $sslSites)) {
  295. $domain['ssl'] = 1;
  296. $domain['sslexpire'] = $sslSites[$domain['domain']]['expire'];
  297. $domain['autossl'] = $sslSites[$domain['domain']]['auotssl'];
  298. }
  299. if(cwp7CheckA($domain['domain'],$params['serverip'],$params['configoption5']) == 1) {
  300. $domain['DNS'] = 1;
  301. }
  302. $domain['domainNS'] = cwp7CheckSOA($domain['domain'],$params['configoption5'],$params['configoption6']);
  303. $domain['subdomains'] = array();
  304. foreach($subDomains as $subDomain) {
  305. if($subDomain['domain'] == $domain['domain']) {
  306. $subFQDN = $subDomain['subdomain'] . '.' . $subDomain['domain'];
  307. $subDomain['relpath'] = str_replace('/home/' . $params['username'], '~', $subDomain['path']);
  308. if(array_key_exists($subFQDN, $sslSites)) {
  309. $subDomain['ssl'] = 1;
  310. $subDomain['sslexpire'] = $sslSites[$subFQDN]['expire'];
  311. $subDomain['autossl'] = $sslSites[$subFQDN]['auotssl'];
  312. } else {
  313. unset($subDomain['ssl']);
  314. unset($subDomain['sslexpire']);
  315. unset($subDomain['autossl']);
  316. }
  317. if(cwp7CheckA($subFQDN,$params['serverip'],$params['configoption5']) == 1) {
  318. $subDomain['DNS'] = 1;
  319. } else {
  320. unset($subDomain['DNS']);
  321. }
  322. array_push($domain['subdomains'], $subDomain);
  323. }
  324. }
  325. array_push($clientInfo['domains'], $domain);
  326. }
  327. }
  328. return array(
  329. 'tabOverviewReplacementTemplate' => 'clientarea',
  330. 'vars' => $clientInfo,
  331. );
  332. }
  333. /**
  334. * Perform single sign-on for a CWP7 account.
  335. *
  336. * When successful, returns a URL to which the user should be redirected.
  337. *
  338. * @param array $params common module parameters
  339. *
  340. * @see https://developers.whmcs.com/provisioning-modules/single-sign-on/
  341. *
  342. * @return array
  343. */
  344. function cwp7_ServiceSingleSignOn($params) {
  345. $cwp7 = new cwp7_Admin($params['serverhostname'], $params['serveraccesshash']);
  346. $response = $cwp7->getLoginLink($params['username']);
  347. if($response['status'] == 'OK') {
  348. $link = $response['msj']['details'];
  349. $linkautologin = $link[0]['url'];
  350. return array(
  351. 'success' => true,
  352. 'redirectTo' => $linkautologin,
  353. );
  354. } else {
  355. return array(
  356. 'success' => false,
  357. 'redirectTo' => '',
  358. );
  359. }
  360. }
  361. /**
  362. * Change the password for a CWP7 account.
  363. *
  364. * Called when a password change is requested. This can occur either due to a
  365. * client requesting it via the client area or an admin requesting it from the
  366. * admin side.
  367. *
  368. * This option is only available to client end users when the product is in an
  369. * active status.
  370. *
  371. * @param array $params common module parameters
  372. *
  373. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  374. *
  375. * @return string "success" or an error message
  376. */
  377. function cwp7_ChangePassword($params) {
  378. $cwp7 = new cwp7_Admin($params['serverhostname'], $params['serveraccesshash']);
  379. $response = $cwp7->changePass(array('user' => $params['username'], 'password' => $params['password']));
  380. if($response['status'] != 'OK') {
  381. return 'Error: ' . $response['error_msg'];
  382. }
  383. return 'success';
  384. }
  385. /**
  386. * Upgrade or downgrade a CWP7 account by package.
  387. *
  388. * Called to apply any change in product assignment or parameters. It
  389. * is called to provision upgrade or downgrade orders, as well as being
  390. * able to be invoked manually by an admin user.
  391. *
  392. * This same function is called for upgrades and downgrades of both
  393. * products and configurable options.
  394. *
  395. * @param array $params common module parameters
  396. *
  397. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  398. *
  399. * @return string "success" or an error message
  400. */
  401. function cwp7_ChangePackage($params) {
  402. $cwp7 = new cwp7_Admin($params['serverhostname'], $params['serveraccesshash']);
  403. $data = array(
  404. 'user' => $params['username'],
  405. 'email' => $params['clientsdetails']['email'],
  406. 'package' => $params['configoption1'],
  407. 'inode' => (int) $params["configoption2"],
  408. 'openfiles' => (int) $params["configoption3"],
  409. 'processes' => (int) $params["configoption4"],
  410. 'server_ips'=> $params["serverip"],
  411. );
  412. $response = $cwp7->modifyAccount($data);
  413. if($response['status'] != 'OK') {
  414. return 'Error: ' . $response['error_msg'];
  415. }
  416. return 'success';
  417. }
  418. /**
  419. * Usage Update
  420. *
  421. * Important: Runs daily per server not per product
  422. * Run Manually: /admin/reports.php?report=disk_usage_summary&action=updatestats
  423. * @param array $params common module parameters
  424. *
  425. * @see https://developers.whmcs.com/provisioning-modules/usage-update/
  426. */
  427. function cwp7_UsageUpdate($params) {
  428. $cwp7 = new cwp7_Admin($params['serverhostname'], $params['serveraccesshash']);
  429. $response = $cwp7->getAllAccounts();
  430. if($response['status'] == 'OK'){
  431. $results = $response['msj'];
  432. for($i = 0; $i < count($results); $i++){
  433. if($results[$i]['diskusage'] == '') {
  434. $diskusage = 0;
  435. } else {
  436. $diskusage = trim($results[$i]['diskusage']);
  437. }
  438. if($results[$i]['disklimit'] == '') {
  439. $disklimit = 0;
  440. } else {
  441. $disklimit = trim($results[$i]['disklimit']);
  442. }
  443. if($results[$i]['bandwidth'] == '') {
  444. $bandwidth = 0;
  445. } else {
  446. $bandwidth =trim($results[$i]['bandwidth']);
  447. }
  448. if($results[$i]['bwlimit'] == '') {
  449. $bwlimit = 0;
  450. } else {
  451. $bwlimit = trim($results[$i]['bwlimit']);
  452. }
  453. $domain = trim($results[$i]['domain']);
  454. try {
  455. \WHMCS\Database\Capsule::table('tblhosting')
  456. ->where('server', $params['serverid'])
  457. ->where('domain', $domain)
  458. ->update([
  459. 'diskusage' => $diskusage,
  460. 'disklimit' => $disklimit,
  461. 'bwusage' => $bandwidth,
  462. 'bwlimit' => $bwlimit,
  463. 'lastupdate' => date('Y-m-d H:i:S'),
  464. ]);
  465. } catch (\Exception $e) {
  466. logActivity('ERROR: Unable to update server usage: ' . $e->getMessage());
  467. }
  468. }
  469. }
  470. }
  471. /**
  472. * Additional actions a client user can invoke.
  473. *
  474. * Define additional actions a client user can perform for an instance of a
  475. * product/service.
  476. *
  477. * Any actions you define here will be automatically displayed in the available
  478. * list of actions within the client area.
  479. *
  480. * @return array
  481. */
  482. function cwp7_ClientAreaCustomButtonArray ($params) {
  483. if(cwp7CheckLimit($params, 'domain')) {
  484. return array();
  485. }
  486. return array(
  487. 'Neue Domain' => 'newDomain',
  488. );
  489. }
  490. /**
  491. * Additional actions a client user can invoke.
  492. *
  493. * Define additional actions a client user is allowed to perform for an instance of a
  494. * product/service.
  495. *
  496. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  497. *
  498. * @return array
  499. */
  500. function cwp7_ClientAreaAllowedFunctions() {
  501. return array(
  502. "Enable SSL" => "enableSSL",
  503. "Renew SSL" => "renewSSL",
  504. "Set DNS" => "setDNS",
  505. "Unset DNS" => "unsetDNS",
  506. "Confirm Enable SSL" => "enableSSLConfirm",
  507. "Confirm Renew SSL" => "renewSSLConfirm",
  508. "Confirm Set DNS" => "setDNSConfirm",
  509. "Confirm Unset DNS" => "unsetDNSConfirm",
  510. "Info DNS" => "infoDNS",
  511. "Info SSL" => "infoSSL",
  512. "Add Domain" => "addDomain",
  513. "Add Subdomain" => "addSubdomain",
  514. "New Subdomain" => "newSubdomain",
  515. "Confirm Delete Domain" => "delDomainConfirm",
  516. "Delete Domain" => "delDomain",
  517. "Confirm Delete Subdomain" => "delSubdomainConfirm",
  518. "Delete Subdomain" => "delSubdomain",
  519. );
  520. }
  521. /**
  522. * Opens a form to add a new domain.
  523. *
  524. * @param array $params common module parameters
  525. *
  526. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  527. *
  528. * @return array template information
  529. */
  530. function cwp7_newDomain($params) {
  531. return array(
  532. 'breadcrumb' => array(
  533. 'clientarea.php?action=productdetails&id=' . $params['serviceid'] . '&modop=custom&a=newDomain' => 'Neue Domain',
  534. ),
  535. 'templatefile' => 'cwp7_add_domain',
  536. );
  537. }
  538. /**
  539. * Adds a new domain to a CWP7 account.
  540. *
  541. * @param array $params common module parameters
  542. *
  543. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  544. *
  545. * @return string "success" or an error message
  546. */
  547. function cwp7_addDomain($params) {
  548. if(!filter_var($_POST['d'], FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME)){
  549. return 'Error: invalid domain name';
  550. }
  551. $vars['user'] = $params['username'];
  552. $vars['name'] = $_POST['d'];
  553. $vars['type'] = 'domain';
  554. $cwp7 = new cwp7_Admin($params['serverhostname'], $params['serveraccesshash']);
  555. $response = $cwp7->addDomain($vars);
  556. if($response['status'] != 'OK') {
  557. return 'Error: ' . $response['error_msg'];
  558. }
  559. return 'success';
  560. }
  561. /**
  562. * Opens a form to add a new subdomain to a domain.
  563. *
  564. * @param array $params common module parameters
  565. *
  566. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  567. *
  568. * @return array template information
  569. */
  570. function cwp7_newSubdomain($params) {
  571. if(!filter_var($_POST['d'], FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME)){
  572. return 'Error: invalid domain name';
  573. }
  574. return array(
  575. 'breadcrumb' => array(
  576. 'clientarea.php?action=productdetails&id=' . $params['serviceid'] . '&modop=custom&a=newSubdomain' => 'Neue Subdomain',
  577. ),
  578. 'templatefile' => 'cwp7_add_subdomain',
  579. 'vars' => array(
  580. 'domainselected' => $_POST['d'],
  581. ),
  582. );
  583. }
  584. /**
  585. * Adds a new subdomain to domain of a CWP7 account.
  586. *
  587. * @param array $params common module parameters
  588. *
  589. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  590. *
  591. * @return string "success" or an error message
  592. */
  593. function cwp7_addSubdomain($params) {
  594. if(!filter_var($_POST['d'], FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME)){
  595. return 'Error: invalid domain name';
  596. }
  597. if(!filter_var($_POST['s'], FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME)){
  598. return 'Error: invalid subdomain name';
  599. }
  600. if($_POST['s'] == 'www') {
  601. return 'Error: default Subdomain www wurde bereits automatisch erstellt' ;
  602. }
  603. $vars['user'] = $params['username'];
  604. $vars['name'] = $_POST['s'] . '.' . $_POST['d'];
  605. $vars['type'] = 'subdomain';
  606. $cwp7 = new cwp7_Admin($params['serverhostname'], $params['serveraccesshash']);
  607. $response = $cwp7->addDomain($vars);
  608. if($response['status'] != 'OK') {
  609. return 'Error: ' . $response['error_msg'];
  610. }
  611. return 'success';
  612. }
  613. /**
  614. * Opens a form to delete a domain from a CWP7 account.
  615. *
  616. * @param array $params common module parameters
  617. *
  618. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  619. *
  620. * @return array template information
  621. */
  622. function cwp7_delDomainConfirm($params) {
  623. return array(
  624. 'templatefile' => 'cwp7_del_domain_confirm',
  625. 'vars' => array(
  626. 'deldomain' => $_POST['d'],
  627. ),
  628. );
  629. }
  630. /**
  631. * Removes a domain from a CWP7 account.
  632. *
  633. * @param array $params common module parameters
  634. *
  635. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  636. *
  637. * @return string "success" or an error message
  638. */
  639. function cwp7_delDomain($params) {
  640. if(!filter_var($_POST['d'], FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME)){
  641. return 'Error: invalid domain name';
  642. }
  643. $cwp7 = new cwp7_Admin($params['serverhostname'], $params['serveraccesshash']);
  644. $response = $cwp7->getAccount($params['username']);
  645. if($response['status'] != 'OK') {
  646. logModuleCall(
  647. 'cwp7',
  648. __FUNCTION__,
  649. $params,
  650. 'debug',
  651. $response
  652. );
  653. }
  654. $domains = $response['result']['domains'];
  655. $clientdomains = array();
  656. foreach($domains as $domain){
  657. if($domain['domain'] != $params['domain']) {
  658. array_push($clientdomains, $domain['domain']);
  659. }
  660. }
  661. if(!in_array($_POST['d'], $clientdomains)) {
  662. logModuleCall(
  663. 'cwp7',
  664. __FUNCTION__,
  665. $_POST,
  666. 'POST DATA VIOLATION',
  667. $params
  668. );
  669. return 'Error: ' . $_POST['d'] . ' not in client domains';
  670. }
  671. // do delete domain
  672. $vars['user'] = $params['username'];
  673. $vars['name'] = $_POST['d'];
  674. $vars['type'] = 'domain';
  675. $response = $cwp7->deleteDomain($vars);
  676. if($response['status'] != 'OK') {
  677. return 'Error: ' . $response['error_msg'];
  678. }
  679. return 'success';
  680. }
  681. /**
  682. * Opens a form to delete a subdomain from domain of a CWP7 account.
  683. *
  684. * @param array $params common module parameters
  685. *
  686. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  687. *
  688. * @return array template information
  689. */
  690. function cwp7_delSubdomainConfirm($params) {
  691. return array(
  692. 'templatefile' => 'cwp7_del_subdomain_confirm',
  693. 'vars' => array(
  694. 'delsubdomain' => $_POST['d'],
  695. ),
  696. );
  697. }
  698. /**
  699. * Removes a subdomain from a domain of a CWP7 account.
  700. *
  701. * @param array $params common module parameters
  702. *
  703. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  704. *
  705. * @return string "success" or an error message
  706. */
  707. function cwp7_delSubdomain($params) {
  708. if(!filter_var($_POST['d'], FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME)){
  709. return 'Error: invalid domain name';
  710. }
  711. $cwp7 = new cwp7_Admin($params['serverhostname'], $params['serveraccesshash']);
  712. $response = $cwp7->getAccount($params['username']);
  713. if($response['status'] != 'OK') {
  714. logModuleCall(
  715. 'cwp7',
  716. __FUNCTION__,
  717. $params,
  718. 'debug',
  719. $response
  720. );
  721. }
  722. $subdomains = $response['result']['subdomins'];
  723. $clientsubdomains = array();
  724. foreach($subdomains as $subdomain){
  725. if($subdomain['domain'] != $params['domain']) {
  726. array_push($clientsubdomains, $subdomain['subdomain'] . "." . $subdomain['domain']);
  727. }
  728. }
  729. if(!in_array($_POST['d'], $clientsubdomains)) {
  730. logModuleCall(
  731. 'cwp7',
  732. __FUNCTION__,
  733. $_POST,
  734. 'POST DATA VIOLATION',
  735. $params
  736. );
  737. return 'Error: ' . $_POST['d'] . ' not in client subdomains';
  738. }
  739. // do delete subdomain
  740. $vars['user'] = $params['username'];
  741. $vars['name'] = $_POST['d'];
  742. $vars['type'] = 'subdomain';
  743. $response = $cwp7->deleteDomain($vars);
  744. if($response['status'] != 'OK') {
  745. return 'Error: ' . $response['error_msg'];
  746. }
  747. return 'success';
  748. }
  749. /**
  750. * Opens a form to enable SSL for a subdomain or domain of a CWP7 account.
  751. *
  752. * @param array $params common module parameters
  753. *
  754. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  755. *
  756. * @return array template information
  757. */
  758. function cwp7_enableSSLConfirm($params) {
  759. return array(
  760. 'templatefile' => 'cwp7_enable_SSL_confirm',
  761. 'vars' => array(
  762. 'SSLdomain' => $_POST['d'],
  763. ),
  764. );
  765. }
  766. /**
  767. * Aktivate CWP7 AutoSSL for a subdomain or domain of a CWP7 account.
  768. *
  769. * @param array $params common module parameters
  770. *
  771. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  772. *
  773. * @return string "success" or an error message
  774. */
  775. function cwp7_enableSSL($params) {
  776. if(!filter_var($_POST['d'], FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME)){
  777. return 'Error: invalid domain name';
  778. }
  779. $vars['user'] = $params['username'];
  780. $vars['name'] = $_POST['d'];
  781. $cwp7 = new cwp7_Admin($params['serverhostname'], $params['serveraccesshash']);
  782. $response = $cwp7->addAutoSSL($vars);
  783. if($response['status'] != 'OK') {
  784. return 'Error: ' . $response['error_msg'];
  785. }
  786. return 'success';
  787. }
  788. /**
  789. * Opens a form to renew a SSL certificate for a subdomain or domain of a CWP7 account.
  790. *
  791. * @param array $params common module parameters
  792. *
  793. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  794. *
  795. * @return array template information
  796. */
  797. function cwp7_renewSSLConfirm($params) {
  798. return array(
  799. 'templatefile' => 'cwp7_renew_SSL_confirm',
  800. 'vars' => array(
  801. 'SSLdomain' => $_POST['d'],
  802. ),
  803. );
  804. }
  805. /**
  806. * Renews a SSL certificate for a subdomain or domain of a CWP7 account.
  807. *
  808. * @param array $params common module parameters
  809. *
  810. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  811. *
  812. * @return string "success" or an error message
  813. */
  814. function cwp7_renewSSL($params) {
  815. if(!filter_var($_POST['d'], FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME)){
  816. return 'Error: invalid domain name';
  817. }
  818. $vars['user'] = $params['username'];
  819. $vars['name'] = $_POST['d'];
  820. $cwp7 = new cwp7_Admin($params['serverhostname'], $params['serveraccesshash']);
  821. $response = $cwp7->updateAutoSSL($vars);
  822. if($response['status'] != 'OK') {
  823. return 'Error: ' . $response['error_msg'];
  824. }
  825. return 'success';
  826. }
  827. /**
  828. * Opens a form to set a DNS record for a subdomain or domain of a CWP7 account.
  829. *
  830. * @param array $params common module parameters
  831. *
  832. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  833. *
  834. * @return array template information
  835. */
  836. function cwp7_setDNSConfirm($params) {
  837. if(isset($_POST['s'])){
  838. return array(
  839. 'templatefile' => 'cwp7_set_DNS_confirm',
  840. 'vars' => array(
  841. 'DNSdomain' => $_POST['d'],
  842. 'DNSsubdomain' => $_POST['s'],
  843. ),
  844. );
  845. }
  846. return array(
  847. 'templatefile' => 'cwp7_set_DNS_confirm',
  848. 'vars' => array(
  849. 'DNSdomain' => $_POST['d'],
  850. ),
  851. );
  852. }
  853. /**
  854. * Opens a form to unsset a DNS record for a subdomain or domain of a CWP7 account.
  855. *
  856. * @param array $params common module parameters
  857. *
  858. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  859. *
  860. * @return array template information
  861. */
  862. function cwp7_unsetDNSConfirm($params) {
  863. if(isset($_POST['s'])){
  864. return array(
  865. 'templatefile' => 'cwp7_unset_DNS_confirm',
  866. 'vars' => array(
  867. 'DNSdomain' => $_POST['d'],
  868. 'DNSsubdomain' => $_POST['s'],
  869. ),
  870. );
  871. }
  872. return array(
  873. 'templatefile' => 'cwp7_unset_DNS_confirm',
  874. 'vars' => array(
  875. 'DNSdomain' => $_POST['d'],
  876. ),
  877. );
  878. }
  879. /**
  880. * Update a DNS zone for a domain setting a new record for a domain or subdomain of a CWP7 account.
  881. *
  882. * @param array $params common module parameters
  883. *
  884. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  885. *
  886. * @return string "success" or an error message
  887. */
  888. function cwp7_setDNS($params) {
  889. if(!filter_var($_POST['d'], FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME)){
  890. return 'Error: invalid domain name';
  891. }
  892. $domainName = $_POST['d'];
  893. $zoneRecords = array();
  894. if(isset($_POST['s'])){
  895. if(!filter_var($_POST['s'], FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME)){
  896. return 'Error: invalid subdomain name';
  897. }
  898. $hostName = $_POST['s'] . '.' . $domainName . '.';
  899. $newRecord = 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, $newRecord);
  909. } else {
  910. $hostName = $domainName . '.';
  911. $domainRecord = array(
  912. 'line' => $hostName.'|A|0',
  913. 'name' => $hostName,
  914. 'type' => 'A',
  915. 'class' => 'IN',
  916. 'data' => array(
  917. 'address' => $params['serverip'],
  918. ),
  919. );
  920. array_push($zoneRecords, $domainRecord);
  921. $wwwRecord = array(
  922. 'line' => 'www'.$hostName.'|A|0',
  923. 'name' => 'www'.$hostName,
  924. 'type' => 'A',
  925. 'class' => 'IN',
  926. 'data' => array(
  927. 'address' => $params['serverip'],
  928. ),
  929. );
  930. array_push($zoneRecords, $wwwRecord);
  931. }
  932. $zoneIDcollection = Capsule::table('dns_manager2_zone')
  933. ->select('id')
  934. ->where('name', '=', $domainName)
  935. ->where('clientid', '=', $params['userid'])
  936. ->get();
  937. $zoneIDobj = $zoneIDcollection[0];
  938. $zoneID = $zoneIDobj->{'id'};
  939. if(!isset($zoneID)) {
  940. return 'Error: Zone for domain ' . $domainName . ' or not owned by client';
  941. }
  942. $dnsZone = localAPI('dnsmanager', array( 'dnsaction' => 'getZone', 'zone_id' => $zoneID));
  943. foreach($dnsZone['data']->records as $record) {
  944. if(($record->name != $hostName) || ($record->type != 'A' && $record->type != 'CNAME')) {
  945. array_push($zoneRecords, $record);
  946. };
  947. }
  948. $result = localAPI('dnsmanager' ,
  949. array(
  950. 'dnsaction' => 'updateZone',
  951. 'zone_id' => $zoneID,
  952. 'records' => $zoneRecords,
  953. )
  954. );
  955. if($result['result'] != 'success') {
  956. return 'Error: ' . $result['message'];
  957. }
  958. return 'success';
  959. }
  960. /**
  961. * Removing a DNS record for a domain or subdomain of a CWP7 account.
  962. *
  963. * @param array $params common module parameters
  964. *
  965. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  966. *
  967. * @return string "success" or an error message
  968. */
  969. function cwp7_unsetDNS($params) {
  970. if(!filter_var($_POST['d'], FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME)){
  971. return 'Error: invalid domain name';
  972. }
  973. $domainName = $_POST['d'];
  974. $zoneRecords = array();
  975. if(isset($_POST['s'])){
  976. if(!filter_var($_POST['s'], FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME)){
  977. return 'Error: invalid subdomain name';
  978. }
  979. $hostName = $_POST['s'] . '.' . $domainName . '.';
  980. } else {
  981. $hostName = $domainName . '.';
  982. }
  983. $zoneIDcollection = Capsule::table('dns_manager2_zone')
  984. ->select('id')
  985. ->where('name', '=', $domainName)
  986. ->where('clientid', '=', $params['userid'])
  987. ->get();
  988. $zoneIDobj = $zoneIDcollection[0];
  989. $zoneID = $zoneIDobj->{'id'};
  990. if(!isset($zoneID)) {
  991. return 'Error: Zone for domain ' . $domainName . ' or not owned by client';
  992. }
  993. $dnsZone = localAPI('dnsmanager', array( 'dnsaction' => 'getZone', 'zone_id' => $zoneID));
  994. foreach($dnsZone['data']->records as $record) {
  995. if(($record->name != $hostName) || ($record->type != 'A' && $record->type != 'CNAME')) {
  996. array_push($zoneRecords, $record);
  997. };
  998. }
  999. $result = localAPI('dnsmanager' ,
  1000. array(
  1001. 'dnsaction' => 'updateZone',
  1002. 'zone_id' => $zoneID,
  1003. 'records' => $zoneRecords,
  1004. )
  1005. );
  1006. if($result['result'] != 'success') {
  1007. return 'Error: ' . $result['message'];
  1008. }
  1009. return 'success';
  1010. }
  1011. /**
  1012. * Opens a form to inform about the DNS status of a subdomain or domain of a CWP7 account.
  1013. *
  1014. * @param array $params common module parameters
  1015. *
  1016. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  1017. *
  1018. * @return array template information
  1019. */
  1020. function cwp7_infoDNS($params) {
  1021. if(!filter_var($_POST['d'], FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME)){
  1022. return 'Error: invalid domain name';
  1023. }
  1024. $cwp7nameserver = cwp7CheckSOA($_POST['d'],$params['configoption5'],$params['configoption6']);
  1025. return array(
  1026. 'templatefile' => 'cwp7_help_dns',
  1027. 'vars' => array(
  1028. 'infodomain' => $_POST['d'],
  1029. 'cwp7nameserver' => $cwp7nameserver,
  1030. ),
  1031. );
  1032. }
  1033. /**
  1034. * Opens a form to inform about the SSL status of a subdomain or domain of a CWP7 account.
  1035. *
  1036. * @param array $params common module parameters
  1037. *
  1038. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  1039. *
  1040. * @return array template information
  1041. */
  1042. function cwp7_infoSSL($params) {
  1043. if(!filter_var($_POST['d'], FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME)){
  1044. return 'Error: invalid domain name';
  1045. }
  1046. return array(
  1047. 'templatefile' => 'cwp7_help_ssl',
  1048. 'vars' => array(
  1049. 'infodomain' => $_POST['d'],
  1050. ),
  1051. );
  1052. }
  1053. /**
  1054. * Ask nameservers for a IP adress of a given host.
  1055. *
  1056. * @param string $host hostname
  1057. * @param string $serverIP CWP7 server IP
  1058. * @param string $nameserverIP polled name server IP
  1059. * @param int $recurse optional -> used to follow CNAME responses
  1060. *
  1061. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  1062. *
  1063. * @return bool
  1064. */
  1065. function cwp7CheckA($host, $serverIP, $nameserverIP, $recurse = 0) {
  1066. if($recurse > 3) {
  1067. return false;
  1068. }
  1069. $nameserver = array($nameserverIP);
  1070. $resolver = new Net_DNS2_Resolver(array('nameservers' => $nameserver));
  1071. try {
  1072. $result = $resolver->query($host, 'A');
  1073. } catch(Net_DNS2_Exception $e) {
  1074. logModuleCall(
  1075. 'cwp7',
  1076. __FUNCTION__,
  1077. $e,
  1078. 'DNS lookup exception',
  1079. $e->getMessage()
  1080. );
  1081. }
  1082. $hostA = $result->answer;
  1083. if($hostA[0]->type == 'CNAME') {
  1084. if(cwp7CheckA($hostA[0]->cname, $serverIP, $nameserverIP, $recurse++)) {
  1085. return true;
  1086. }
  1087. }
  1088. if($hostA[0]->type == 'A') {
  1089. if($hostA[0]->address == $serverIP){
  1090. return true;
  1091. }
  1092. }
  1093. return false;
  1094. }
  1095. /**
  1096. * Ask nameservers for the authority of a domain.
  1097. *
  1098. * @param string $domain domain name
  1099. * @param string $nameserverIP polled name server IP
  1100. * @param string $nameserverName name of the own namesever
  1101. *
  1102. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  1103. *
  1104. * @return string 'none' -> not registered, 'self' -> registered at own or the name of an other responsible nameserver
  1105. */
  1106. function cwp7CheckSOA($domain, $nameserverIP, $nameserverName ) {
  1107. $nameserver = array($nameserverIP);
  1108. $resolver = new Net_DNS2_Resolver(array('nameservers' => $nameserver));
  1109. try {
  1110. $result = $resolver->query($domain, 'SOA');
  1111. } catch(Net_DNS2_Exception $e) {
  1112. logModuleCall(
  1113. 'cwp7',
  1114. __FUNCTION__,
  1115. $e,
  1116. 'DNS lookup exception',
  1117. $e->getMessage()
  1118. );
  1119. return 'none';
  1120. }
  1121. if($result->answer[0]->mname == $nameserverName) {
  1122. return 'self';
  1123. }
  1124. return $result->answer[0]->mname;
  1125. }
  1126. /**
  1127. * Check limits for a service of an account .
  1128. *
  1129. * @param array $params common module parameters
  1130. * @param string $type domain|subdomain
  1131. *
  1132. * @see https://developers.whmcs.com/provisioning-modules/supported-functions/
  1133. *
  1134. * @return bool true -> limit reached, false -> limit noch reached
  1135. */
  1136. function cwp7CheckLimit($params, $type) {
  1137. $cwp7 = new cwp7_Admin($params['serverhostname'], $params['serveraccesshash']);
  1138. $response = $cwp7->getQuota($params['username']);
  1139. logModuleCall(
  1140. 'cwp7',
  1141. __FUNCTION__,
  1142. $response,
  1143. 'debug',
  1144. $type
  1145. );
  1146. if($response[$type]['sw'] < 1) {
  1147. return true;
  1148. }
  1149. return false;
  1150. }