cwp7.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599
  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 2020
  9. * @license GPL
  10. */
  11. use WHMCS\Database\Capsule;
  12. require_once 'Net/DNS2.php';
  13. require_once 'Net/Whois.php';
  14. require_once(__DIR__ . '/api/cwp7/Admin.php');
  15. if (!defined('WHMCS')) {
  16. die('This file cannot be accessed directly');
  17. }
  18. const NS1 = '10.200.22.100';
  19. const NSTHURDATA = 'ns1.thurdata.ch';
  20. function cwp7_MetaData() {
  21. return array(
  22. 'DisplayName' => 'CentOS Web Panel Provisioning',
  23. 'APIVersion' => '1.2',
  24. 'DefaultNonSSLPort' => '2031',
  25. 'DefaultSSLPort' => '2031',
  26. 'RequiresServer' => true,
  27. 'ServiceSingleSignOnLabel' => 'Login to CWP7',
  28. 'AdminSingleSignOnLabel' => 'Login to CWP7 Admin'
  29. );
  30. }
  31. function cwp7_Testconnection($params) {
  32. $cwp7 = new cwp7_Admin($params['serverhostname'], $params['serveraccesshash']);
  33. $response = $cwp7->getServerType();
  34. if($response['status'] == 'OK') {
  35. return array(
  36. 'success' => true,
  37. 'error' => '',
  38. );
  39. }
  40. return array(
  41. 'success' => false,
  42. 'error' => $response['msj'],
  43. );
  44. }
  45. function cwp7_ConfigOptions() {
  46. $whmcs = App::self();
  47. $serverGroupID = $whmcs->get_req_var('servergroup');
  48. $serverIDObj = Capsule::table('tblservergroupsrel')
  49. ->select('serverid')
  50. ->where('groupid', '=', $serverGroupID)
  51. ->get();
  52. $serverIDs = array();
  53. foreach($serverIDObj as $serverID) {
  54. array_push($serverIDs, $serverID->serverid);
  55. }
  56. $server = Capsule::table('tblservers')
  57. ->select('hostname', 'accesshash')
  58. ->where('id', $serverIDs)
  59. ->where('active', '=', 1)
  60. ->first();
  61. $cwp7 = new cwp7_Admin($server->hostname, $server->accesshash);
  62. $cwp7Packages = $cwp7->getPackages();
  63. if($cwp7Packages['status'] != 'OK') {
  64. logModuleCall(
  65. 'cwp7',
  66. __FUNCTION__,
  67. $cwp7Packages['status'],
  68. 'Could not fetch packages',
  69. $cwp7Packages['msj']
  70. );
  71. return false;
  72. }
  73. $cwp7PackageNames = array();
  74. foreach($cwp7Packages['msj'] as $cwp7Package) {
  75. array_push($cwp7PackageNames, $cwp7Package['package_name']);
  76. }
  77. $configOptions = array();
  78. $configOptions['package'] = array(
  79. 'FriendlyName' => 'CWP7 Package',
  80. 'Type' => 'dropdown',
  81. 'Options' => implode(',', $cwp7PackageNames),
  82. 'Description' => 'Select CWP7 Package',
  83. );
  84. $configOptions['inode'] = array( "Type" => "text" , "Description" => "Max of inode", "Default" => "0",);
  85. $configOptions['nofile'] = array( "Type" => "text", "Description" => "Max of nofile", "Default" => "100",);
  86. $configOptions['nproc'] = array( "Type" => "text" , "Description" => "Nproc limit - 40 suggested", "Default" => "40",);
  87. return $configOptions;
  88. }
  89. function cwp7_CreateAccount($params) {
  90. $username = strtolower(substr($params['clientsdetails']['firstname'],0,2) . substr($params['clientsdetails']['lastname'],0,3)) . $params['serviceid'];
  91. $userdomain = $username . '.local';
  92. try {
  93. Capsule::table('tblhosting')
  94. ->where('id', '=', $params['serviceid'])
  95. ->update(
  96. array(
  97. 'username' => $username,
  98. 'domain' => $userdomain,
  99. )
  100. );
  101. } catch (\Exception $e) {
  102. logModuleCall(
  103. 'cwp7',
  104. __FUNCTION__,
  105. $params,
  106. 'Error: could save username & domain in database',
  107. $e->getMessage()
  108. );
  109. return 'Error: could save username & password in database';
  110. }
  111. if ($params["server"] == 1) {
  112. $data = array(
  113. 'package' => $params['configoption1'],
  114. 'domain' => $userdomain,
  115. 'user' => $username,
  116. 'pass' => $params['password'],
  117. 'email' => $params['clientsdetails']['email'],
  118. 'inode' => $params["configoption2"],
  119. 'nofile' => $params["configoption3"],
  120. 'nproc' => $params["configoption4"],
  121. 'server_ips'=>$params["serverip"]
  122. );
  123. $cwp7 = new cwp7_Admin($params['serverhostname'], $params['serveraccesshash']);
  124. $response = $cwp7->createAccount($data);
  125. }
  126. if($response['status'] != 'OK') {
  127. return 'Error: ' . $response['msj'];
  128. }
  129. return 'success';
  130. }
  131. function cwp7_TerminateAccount($params) {
  132. $cwp7 = new cwp7_Admin($params['serverhostname'], $params['serveraccesshash']);
  133. $response = $cwp7->deleteAccount(array('user' => $params['username'], 'email' => $params['clientsdetails']['email']));
  134. if($response['status'] != 'OK') {
  135. return 'Error: ' . $response['msj'];
  136. }
  137. return 'success';
  138. }
  139. function cwp7_SuspendAccount($params) {
  140. $cwp7 = new cwp7_Admin($params['serverhostname'], $params['serveraccesshash']);
  141. $response = $cwp7->suspendAccount($params['username']);
  142. if($response['status'] != 'OK') {
  143. return 'Error: ' . $response['msj'];
  144. }
  145. return 'success';
  146. }
  147. function cwp7_UnsuspendAccount($params) {
  148. $cwp7 = new cwp7_Admin($params['serverhostname'], $params['serveraccesshash']);
  149. $response = $cwp7->unsuspendAccount($params['username']);
  150. if($response['status'] != 'OK') {
  151. return 'Error: ' . $response['msj'];
  152. }
  153. return 'success';
  154. }
  155. function cwp7_ClientArea($params){
  156. $clientInfo = array('moduleclientarea' => '1');
  157. $cwp7 = new cwp7_Admin($params['serverhostname'], $params['serveraccesshash']);
  158. $response = $cwp7->getAutoSSL($params['username']);
  159. if($response['status'] == 'OK') {
  160. $sslSites = array();
  161. foreach($response['msj'] as $sslSite) {
  162. $sslSites[$sslSite['ssl']] = array(
  163. 'auotssl' => $sslSite['autossl'],
  164. 'expire' => $sslSite['exp'],
  165. );
  166. }
  167. }
  168. $response = $cwp7->getAccount($params['username']);
  169. if($response['status'] != 'OK') {
  170. logModuleCall(
  171. 'cwp7',
  172. __FUNCTION__,
  173. $params,
  174. 'debug',
  175. $response
  176. );
  177. }
  178. $domains = $response['result']['domains'];
  179. $subDomains = $response['result']['subdomins'];
  180. $clientInfo['domains'] = array();
  181. foreach($domains as $domain) {
  182. if($domain['path'] == '/home/' . $params['username'] . '/public_html') {
  183. $clientInfo['mgmtDomain'] = $domain['domain'];
  184. $clientInfo['mgmtEmail'] = $domain['email'];
  185. } else {
  186. if(array_key_exists($domain['domain'], $sslSites)) {
  187. $domain['ssl'] = 1;
  188. $domain['sslexpire'] = $sslSites[$domain['domain']]['expire'];
  189. $domain['autossl'] = $sslSites[$domain['domain']]['auotssl'];
  190. }
  191. if(cwp7CheckA($domain['domain'],$params['serverip']) == 1) {
  192. $domain['DNS'] = 1;
  193. }
  194. $clientInfo['domainNS'] = cwp7CheckSOA($domain['domain']);
  195. $domain['subdomains'] = array();
  196. foreach($subDomains as $subDomain) {
  197. if($subDomain['domain'] == $domain['domain']) {
  198. $subFQDN = $subDomain['subdomain'] . '.' . $subDomain['domain'];
  199. if(array_key_exists($subFQDN, $sslSites)) {
  200. $subDomain['ssl'] = 1;
  201. $subDomain['sslexpire'] = $sslSites[$subFQDN]['expire'];
  202. $subDomain['autossl'] = $sslSites[$subFQDN]['auotssl'];
  203. } else {
  204. unset($subDomain['ssl']);
  205. unset($subDomain['sslexpire']);
  206. unset($subDomain['autossl']);
  207. }
  208. if(cwp7CheckA($subFQDN,$params['serverip']) == 1) {
  209. $subDomain['DNS'] = 1;
  210. } else {
  211. unset($subDomain['DNS']);
  212. }
  213. array_push($domain['subdomains'], $subDomain);
  214. }
  215. }
  216. array_push($clientInfo['domains'], $domain);
  217. }
  218. }
  219. return array(
  220. 'tabOverviewReplacementTemplate' => 'clientarea',
  221. 'vars' => $clientInfo,
  222. );
  223. }
  224. function cwp7_ServiceSingleSignOn($params) {
  225. $cwp7 = new cwp7_Admin($params['serverhostname'], $params['serveraccesshash']);
  226. $response = $cwp7->getLoginLink($params['username']);
  227. if($response['status'] == 'OK') {
  228. $link = $response['msj']['details'];
  229. $linkautologin = $link[0]['url'];
  230. return array(
  231. 'success' => true,
  232. 'redirectTo' => $linkautologin,
  233. );
  234. } else {
  235. return array(
  236. 'success' => false,
  237. 'redirectTo' => '',
  238. );
  239. }
  240. }
  241. function cwp7_ChangePassword($params){
  242. $cwp7 = new cwp7_Admin($params['serverhostname'], $params['serveraccesshash']);
  243. $response = $cwp7->changePass(array('user' => $params['username'], 'password' => $params['password']));
  244. if($response['status'] != 'OK') {
  245. return 'Error: ' . $response['msj'];
  246. }
  247. return 'success';
  248. }
  249. function cwp7_ChangePackage($params){
  250. $cwp7 = new cwp7_Admin($params['serverhostname'], $params['serveraccesshash']);
  251. $response = $cwp7->modifyAccount(array('user' => $params['username'], 'email' => $params['clientdetails']['email'], 'package' => $params['configoption1']));
  252. if($response['status'] != 'OK') {
  253. return 'Error: ' . $response['msj'];
  254. }
  255. return 'success';
  256. }
  257. function cwp7_UsageUpdate($params) {
  258. $cwp7 = new cwp7_Admin($params['serverhostname'], $params['serveraccesshash']);
  259. $response = $cwp7->getAllAccounts();
  260. if($response['status'] == 'OK'){
  261. $results = $response['msj'];
  262. for($i = 0; $i < count($results); $i++){
  263. if($results[$i]['diskusage'] == '') {
  264. $diskusage = 0;
  265. } else {
  266. $diskusage = trim($results[$i]['diskusage']);
  267. }
  268. if($results[$i]['disklimit'] == '') {
  269. $disklimit = 0;
  270. } else {
  271. $disklimit = trim($results[$i]['disklimit']);
  272. }
  273. if($results[$i]['bandwidth'] == '') {
  274. $bandwidth = 0;
  275. } else {
  276. $bandwidth =trim($results[$i]['bandwidth']);
  277. }
  278. if($results[$i]['bwlimit'] == '') {
  279. $bwlimit = 0;
  280. } else {
  281. $bwlimit = trim($results[$i]['bwlimit']);
  282. }
  283. $domain = trim($results[$i]['domain']);
  284. try {
  285. \WHMCS\Database\Capsule::table('tblhosting')
  286. ->where('server', $params['serverid'])
  287. ->where('domain', $domain)
  288. ->update([
  289. 'diskusage' => $diskusage,
  290. 'disklimit' => $disklimit,
  291. 'bwusage' => $bandwidth,
  292. 'bwlimit' => $bwlimit,
  293. 'lastupdate' => date('Y-m-d H:i:S'),
  294. ]);
  295. } catch (\Exception $e) {
  296. logActivity('ERROR: Unable to update server usage: ' . $e->getMessage());
  297. }
  298. }
  299. }
  300. }
  301. function cwp7_ClientAreaCustomButtonArray ($params)
  302. {
  303. return array(
  304. 'Neue Domaine' => 'newDomain',
  305. 'Neue Subdomaine' => 'newSubdomain',
  306. );
  307. }
  308. function cwp7_ClientAreaAllowedFunctions() {
  309. return array(
  310. "Enable SSL" => "enableSSL",
  311. "Set DNS" => "setDNS",
  312. "Info DNS" => "infoDNS",
  313. "Add Domain" => "addDomain",
  314. "Confirm Delete Domain" => "delDomainConfirm",
  315. "Delete Domain" => "delDomain",
  316. "Confirm Delete Subdomain" => "delSubdomainConfirm",
  317. "Delete Subdomain" => "delSubdomain",
  318. );
  319. }
  320. function cwp7_newDomain($params) {
  321. return array(
  322. 'breadcrumb' => array(
  323. 'clientarea.php?action=productdetails&id=' . $params['serviceid'] . '&modop=custom&a=newDomain' => 'Neue Domaine',
  324. ),
  325. 'templatefile' => 'cwp7_add_domain',
  326. );
  327. }
  328. function cwp7_addDomain($params) {
  329. if(!filter_var($_POST['d'], FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME)){
  330. return 'Error: invalid domain name';
  331. }
  332. logModuleCall(
  333. 'cwp7',
  334. __FUNCTION__,
  335. $_POST,
  336. 'debug addDomain',
  337. filter_var($_POST['d'], FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME)
  338. );
  339. return 'success';
  340. }
  341. function cwp7_newSubdomain($params) {
  342. $cwp7 = new cwp7_Admin($params['serverhostname'], $params['serveraccesshash']);
  343. $response = $cwp7->getAccount($params['username']);
  344. if($response['status'] != 'OK') {
  345. logModuleCall(
  346. 'cwp7',
  347. __FUNCTION__,
  348. $params,
  349. 'debug',
  350. $response
  351. );
  352. }
  353. $domains = $response['result']['domains'];
  354. $clientdomains = array();
  355. foreach($domains as $domain){
  356. if($domain['domain'] != $params['domain']) {
  357. array_push($clientdomains, $domain['domain']);
  358. }
  359. }
  360. return array(
  361. 'breadcrumb' => array(
  362. 'clientarea.php?action=productdetails&id=' . $params['serviceid'] . '&modop=custom&a=newSubdomain' => 'Neue Subdomaine',
  363. ),
  364. 'templatefile' => 'cwp7_add_subdomain',
  365. 'vars' => array(
  366. 'domains' => $clientdomains,
  367. ),
  368. );
  369. }
  370. function cwp7_addSubdomain($params) {
  371. if(!filter_var($_POST['d'], FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME)){
  372. return 'Error: invalid domain name';
  373. }
  374. logModuleCall(
  375. 'cwp7',
  376. __FUNCTION__,
  377. $_POST,
  378. 'debug addSubdomain',
  379. filter_var($_POST['d'], FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME)
  380. );
  381. return 'success';
  382. }
  383. function cwp7_delDomainConfirm($params) {
  384. return array(
  385. 'templatefile' => 'cwp7_del_domain_confirm',
  386. 'vars' => array(
  387. 'deldomain' => $_POST['d'],
  388. ),
  389. );
  390. }
  391. function cwp7_delDomain($params) {
  392. if(!filter_var($_POST['d'], FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME)){
  393. return 'Error: invalid domain name';
  394. }
  395. $cwp7 = new cwp7_Admin($params['serverhostname'], $params['serveraccesshash']);
  396. $response = $cwp7->getAccount($params['username']);
  397. if($response['status'] != 'OK') {
  398. logModuleCall(
  399. 'cwp7',
  400. __FUNCTION__,
  401. $params,
  402. 'debug',
  403. $response
  404. );
  405. }
  406. $domains = $response['result']['domains'];
  407. $clientdomains = array();
  408. foreach($domains as $domain){
  409. if($domain['domain'] != $params['domain']) {
  410. array_push($clientdomains, $domain['domain']);
  411. }
  412. }
  413. if(!in_array($_POST['d'], $clientdomains)) {
  414. logModuleCall(
  415. 'cwp7',
  416. __FUNCTION__,
  417. $_POST,
  418. 'POST DATA VIOLATION',
  419. $params
  420. );
  421. return 'Error: ' . $_POST['d'] . ' not in client domains';
  422. }
  423. // do delete domain
  424. return 'success';
  425. }
  426. function cwp7_delSubdomainConfirm($params) {
  427. return array(
  428. 'templatefile' => 'cwp7_del_subdomain_confirm',
  429. 'vars' => array(
  430. 'delsubdomain' => $_POST['d'],
  431. ),
  432. );
  433. }
  434. function cwp7_delSubdomain($params) {
  435. if(!filter_var($_POST['d'], FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME)){
  436. return 'Error: invalid domain name';
  437. }
  438. $cwp7 = new cwp7_Admin($params['serverhostname'], $params['serveraccesshash']);
  439. $response = $cwp7->getAccount($params['username']);
  440. if($response['status'] != 'OK') {
  441. logModuleCall(
  442. 'cwp7',
  443. __FUNCTION__,
  444. $params,
  445. 'debug',
  446. $response
  447. );
  448. }
  449. $subdomains = $response['result']['subdomins'];
  450. $clientsubdomains = array();
  451. foreach($subdomains as $subdomain){
  452. if($subdomain['domain'] != $params['domain']) {
  453. array_push($clientsubdomains, $subdomain['subdomain'] . "." . $subdomain['domain']);
  454. }
  455. }
  456. if(!in_array($_POST['d'], $clientsubdomains)) {
  457. logModuleCall(
  458. 'cwp7',
  459. __FUNCTION__,
  460. $_POST,
  461. 'POST DATA VIOLATION',
  462. $params
  463. );
  464. return 'Error: ' . $_POST['d'] . ' not in client subdomains';
  465. }
  466. // do delete subdomain
  467. return 'success';
  468. }
  469. function cwp7_enableSSL($params) {
  470. logModuleCall(
  471. 'cwp7',
  472. __FUNCTION__,
  473. $_POST,
  474. 'debug custom',
  475. $params
  476. );
  477. return 'success';
  478. }
  479. function cwp7_renewSSL($params) {
  480. logModuleCall(
  481. 'cwp7',
  482. __FUNCTION__,
  483. $_POST,
  484. 'debug custom',
  485. $params
  486. );
  487. return 'success';
  488. }
  489. function cwp7_setDNS($params) {
  490. logModuleCall(
  491. 'cwp7',
  492. __FUNCTION__,
  493. $_POST,
  494. 'debug custom',
  495. $params
  496. );
  497. return 'success';
  498. }
  499. function cwp7_infoDNS($params) {
  500. if(!filter_var($_POST['d'], FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME)){
  501. return 'Error: invalid domain name';
  502. }
  503. $cwp7nameserver = cwp7CheckSOA($_POST['d']);
  504. return array(
  505. 'templatefile' => 'cwp7_help_dns',
  506. 'vars' => array(
  507. 'infodomain' => $_POST['d'],
  508. 'cwp7nameserver' => $cwp7nameserver,
  509. ),
  510. );
  511. }
  512. function cwp7CheckA($domain, $serverIP, $recurse = 0) {
  513. if($recurse > 3) {
  514. return false;
  515. }
  516. $nameserver = array(NS1);
  517. $resolver = new Net_DNS2_Resolver(array('nameservers' => $nameserver));
  518. try {
  519. $result = $resolver->query($domain, 'A');
  520. } catch(Net_DNS2_Exception $e) {
  521. logModuleCall(
  522. 'cwp7',
  523. __FUNCTION__,
  524. $e,
  525. 'DNS lookup exception',
  526. $e->getMessage()
  527. );
  528. }
  529. $domainA = $result->answer;
  530. if($domainA[0]->type == 'CNAME') {
  531. if(cwp7CheckA($domainA[0]->cname, $serverIP, $recurse++)) {
  532. return true;
  533. }
  534. }
  535. if($domainA[0]->type == 'A') {
  536. if($domainA[0]->address == $serverIP){
  537. return true;
  538. }
  539. }
  540. }
  541. function cwp7CheckSOA($domain) {
  542. $nameserver = array(NS1);
  543. $resolver = new Net_DNS2_Resolver(array('nameservers' => $nameserver));
  544. try {
  545. $result = $resolver->query($domain, 'SOA');
  546. } catch(Net_DNS2_Exception $e) {
  547. logModuleCall(
  548. 'cwp7',
  549. __FUNCTION__,
  550. $e,
  551. 'DNS lookup exception',
  552. $e->getMessage()
  553. );
  554. return 'none';
  555. }
  556. if($result->answer[0]->mname == NSTHURDATA) {
  557. return 'self';
  558. }
  559. return $result->answer[0]->mname;
  560. }