cwp7.php 33 KB

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