cwp7.php 33 KB

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