cwp7.php 30 KB

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