cwp7.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603
  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. $vars['user'] = $params['username'];
  333. $vars['name'] = $_POST['d'];
  334. $vars['type'] = 'domain';
  335. $cwp7 = new cwp7_Admin($params['serverhostname'], $params['serveraccesshash']);
  336. $response = $cwp7->addDomain($vars);
  337. logModuleCall(
  338. 'cwp7',
  339. __FUNCTION__,
  340. $_POST,
  341. 'debug addDomain',
  342. $response
  343. );
  344. return 'success';
  345. }
  346. function cwp7_newSubdomain($params) {
  347. $cwp7 = new cwp7_Admin($params['serverhostname'], $params['serveraccesshash']);
  348. $response = $cwp7->getAccount($params['username']);
  349. if($response['status'] != 'OK') {
  350. logModuleCall(
  351. 'cwp7',
  352. __FUNCTION__,
  353. $params,
  354. 'debug',
  355. $response
  356. );
  357. }
  358. $domains = $response['result']['domains'];
  359. $clientdomains = array();
  360. foreach($domains as $domain){
  361. if($domain['domain'] != $params['domain']) {
  362. array_push($clientdomains, $domain['domain']);
  363. }
  364. }
  365. return array(
  366. 'breadcrumb' => array(
  367. 'clientarea.php?action=productdetails&id=' . $params['serviceid'] . '&modop=custom&a=newSubdomain' => 'Neue Subdomaine',
  368. ),
  369. 'templatefile' => 'cwp7_add_subdomain',
  370. 'vars' => array(
  371. 'domains' => $clientdomains,
  372. ),
  373. );
  374. }
  375. function cwp7_addSubdomain($params) {
  376. if(!filter_var($_POST['d'], FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME)){
  377. return 'Error: invalid domain name';
  378. }
  379. logModuleCall(
  380. 'cwp7',
  381. __FUNCTION__,
  382. $_POST,
  383. 'debug addSubdomain',
  384. filter_var($_POST['d'], FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME)
  385. );
  386. return 'success';
  387. }
  388. function cwp7_delDomainConfirm($params) {
  389. return array(
  390. 'templatefile' => 'cwp7_del_domain_confirm',
  391. 'vars' => array(
  392. 'deldomain' => $_POST['d'],
  393. ),
  394. );
  395. }
  396. function cwp7_delDomain($params) {
  397. if(!filter_var($_POST['d'], FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME)){
  398. return 'Error: invalid domain name';
  399. }
  400. $cwp7 = new cwp7_Admin($params['serverhostname'], $params['serveraccesshash']);
  401. $response = $cwp7->getAccount($params['username']);
  402. if($response['status'] != 'OK') {
  403. logModuleCall(
  404. 'cwp7',
  405. __FUNCTION__,
  406. $params,
  407. 'debug',
  408. $response
  409. );
  410. }
  411. $domains = $response['result']['domains'];
  412. $clientdomains = array();
  413. foreach($domains as $domain){
  414. if($domain['domain'] != $params['domain']) {
  415. array_push($clientdomains, $domain['domain']);
  416. }
  417. }
  418. if(!in_array($_POST['d'], $clientdomains)) {
  419. logModuleCall(
  420. 'cwp7',
  421. __FUNCTION__,
  422. $_POST,
  423. 'POST DATA VIOLATION',
  424. $params
  425. );
  426. return 'Error: ' . $_POST['d'] . ' not in client domains';
  427. }
  428. // do delete domain
  429. return 'success';
  430. }
  431. function cwp7_delSubdomainConfirm($params) {
  432. return array(
  433. 'templatefile' => 'cwp7_del_subdomain_confirm',
  434. 'vars' => array(
  435. 'delsubdomain' => $_POST['d'],
  436. ),
  437. );
  438. }
  439. function cwp7_delSubdomain($params) {
  440. if(!filter_var($_POST['d'], FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME)){
  441. return 'Error: invalid domain name';
  442. }
  443. $cwp7 = new cwp7_Admin($params['serverhostname'], $params['serveraccesshash']);
  444. $response = $cwp7->getAccount($params['username']);
  445. if($response['status'] != 'OK') {
  446. logModuleCall(
  447. 'cwp7',
  448. __FUNCTION__,
  449. $params,
  450. 'debug',
  451. $response
  452. );
  453. }
  454. $subdomains = $response['result']['subdomins'];
  455. $clientsubdomains = array();
  456. foreach($subdomains as $subdomain){
  457. if($subdomain['domain'] != $params['domain']) {
  458. array_push($clientsubdomains, $subdomain['subdomain'] . "." . $subdomain['domain']);
  459. }
  460. }
  461. if(!in_array($_POST['d'], $clientsubdomains)) {
  462. logModuleCall(
  463. 'cwp7',
  464. __FUNCTION__,
  465. $_POST,
  466. 'POST DATA VIOLATION',
  467. $params
  468. );
  469. return 'Error: ' . $_POST['d'] . ' not in client subdomains';
  470. }
  471. // do delete subdomain
  472. return 'success';
  473. }
  474. function cwp7_enableSSL($params) {
  475. logModuleCall(
  476. 'cwp7',
  477. __FUNCTION__,
  478. $_POST,
  479. 'debug custom',
  480. $params
  481. );
  482. return 'success';
  483. }
  484. function cwp7_renewSSL($params) {
  485. logModuleCall(
  486. 'cwp7',
  487. __FUNCTION__,
  488. $_POST,
  489. 'debug custom',
  490. $params
  491. );
  492. return 'success';
  493. }
  494. function cwp7_setDNS($params) {
  495. logModuleCall(
  496. 'cwp7',
  497. __FUNCTION__,
  498. $_POST,
  499. 'debug custom',
  500. $params
  501. );
  502. return 'success';
  503. }
  504. function cwp7_infoDNS($params) {
  505. if(!filter_var($_POST['d'], FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME)){
  506. return 'Error: invalid domain name';
  507. }
  508. $cwp7nameserver = cwp7CheckSOA($_POST['d']);
  509. return array(
  510. 'templatefile' => 'cwp7_help_dns',
  511. 'vars' => array(
  512. 'infodomain' => $_POST['d'],
  513. 'cwp7nameserver' => $cwp7nameserver,
  514. ),
  515. );
  516. }
  517. function cwp7CheckA($domain, $serverIP, $recurse = 0) {
  518. if($recurse > 3) {
  519. return false;
  520. }
  521. $nameserver = array(NS1);
  522. $resolver = new Net_DNS2_Resolver(array('nameservers' => $nameserver));
  523. try {
  524. $result = $resolver->query($domain, 'A');
  525. } catch(Net_DNS2_Exception $e) {
  526. logModuleCall(
  527. 'cwp7',
  528. __FUNCTION__,
  529. $e,
  530. 'DNS lookup exception',
  531. $e->getMessage()
  532. );
  533. }
  534. $domainA = $result->answer;
  535. if($domainA[0]->type == 'CNAME') {
  536. if(cwp7CheckA($domainA[0]->cname, $serverIP, $recurse++)) {
  537. return true;
  538. }
  539. }
  540. if($domainA[0]->type == 'A') {
  541. if($domainA[0]->address == $serverIP){
  542. return true;
  543. }
  544. }
  545. }
  546. function cwp7CheckSOA($domain) {
  547. $nameserver = array(NS1);
  548. $resolver = new Net_DNS2_Resolver(array('nameservers' => $nameserver));
  549. try {
  550. $result = $resolver->query($domain, 'SOA');
  551. } catch(Net_DNS2_Exception $e) {
  552. logModuleCall(
  553. 'cwp7',
  554. __FUNCTION__,
  555. $e,
  556. 'DNS lookup exception',
  557. $e->getMessage()
  558. );
  559. return 'none';
  560. }
  561. if($result->answer[0]->mname == NSTHURDATA) {
  562. return 'self';
  563. }
  564. return $result->answer[0]->mname;
  565. }