cwp7.php 35 KB

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