cwp7.php 34 KB

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