cwp7.php 16 KB

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