cwp7.php 35 KB

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