cwp7.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  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. function cwp7_MetaData() {
  19. return array(
  20. 'DisplayName' => 'CentOS Web Panel Provisioning',
  21. 'APIVersion' => '1.2',
  22. 'DefaultNonSSLPort' => '2031',
  23. 'DefaultSSLPort' => '2031',
  24. 'RequiresServer' => true,
  25. 'ServiceSingleSignOnLabel' => 'Login to CWP7',
  26. 'AdminSingleSignOnLabel' => 'Login to CWP7 Admin'
  27. );
  28. }
  29. function cwp7_Testconnection($params) {
  30. $cwp7 = new cwp7_Admin($params['serverhostname'], $params['serveraccesshash']);
  31. $response = $cwp7->getServerType();
  32. if($response['status'] == 'OK') {
  33. return array(
  34. 'success' => true,
  35. 'error' => '',
  36. );
  37. }
  38. return array(
  39. 'success' => false,
  40. 'error' => $response['msj'],
  41. );
  42. }
  43. function cwp7_ConfigOptions() {
  44. $whmcs = App::self();
  45. $serverGroupID = $whmcs->get_req_var('servergroup');
  46. $serverIDObj = Capsule::table('tblservergroupsrel')
  47. ->select('serverid')
  48. ->where('groupid', '=', $serverGroupID)
  49. ->get();
  50. $serverIDs = array();
  51. foreach($serverIDObj as $serverID) {
  52. array_push($serverIDs, $serverID->serverid);
  53. }
  54. $server = Capsule::table('tblservers')
  55. ->select('hostname', 'accesshash')
  56. ->where('id', $serverIDs)
  57. ->where('active', '=', 1)
  58. ->first();
  59. $cwp7 = new cwp7_Admin($server->hostname, $server->accesshash);
  60. $cwp7Packages = $cwp7->getPackages();
  61. if($cwp7Packages['status'] != 'OK') {
  62. logModuleCall(
  63. 'cwp7',
  64. __FUNCTION__,
  65. $cwp7Packages['status'],
  66. 'Could not fetch packages',
  67. $cwp7Packages['msj']
  68. );
  69. return false;
  70. }
  71. $cwp7PackageNames = array();
  72. foreach($cwp7Packages['msj'] as $cwp7Package) {
  73. array_push($cwp7PackageNames, $cwp7Package['package_name']);
  74. }
  75. $configOptions = array();
  76. $configOptions['package'] = array(
  77. 'FriendlyName' => 'CWP7 Package',
  78. 'Type' => 'dropdown',
  79. 'Options' => implode(',', $cwp7PackageNames),
  80. 'Description' => 'Select CWP7 Package',
  81. );
  82. $configOptions['inode'] = array( "Type" => "text" , "Description" => "Max of inode", "Default" => "0",);
  83. $configOptions['nofile'] = array( "Type" => "text", "Description" => "Max of nofile", "Default" => "100",);
  84. $configOptions['nproc'] = array( "Type" => "text" , "Description" => "Nproc limit - 40 suggested", "Default" => "40",);
  85. return $configOptions;
  86. }
  87. function cwp7_CreateAccount($params) {
  88. $username = strtolower(substr($params['clientsdetails']['firstname'],0,2) . substr($params['clientsdetails']['lastname'],0,3)) . $params['serviceid'];
  89. $userdomain = $username . '.local';
  90. try {
  91. Capsule::table('tblhosting')
  92. ->where('id', '=', $params['serviceid'])
  93. ->update(
  94. array(
  95. 'username' => $username,
  96. 'domain' => $userdomain,
  97. )
  98. );
  99. } catch (\Exception $e) {
  100. logModuleCall(
  101. 'cwp7',
  102. __FUNCTION__,
  103. $params,
  104. 'Error: could save username & domain in database',
  105. $e->getMessage()
  106. );
  107. return 'Error: could save username & password in database';
  108. }
  109. if ($params["server"] == 1) {
  110. $data = array(
  111. 'package' => $params['configoption1'],
  112. 'domain' => $userdomain,
  113. 'user' => $username,
  114. 'pass' => $params['password'],
  115. 'email' => $params['clientsdetails']['email'],
  116. 'inode' => $params["configoption2"],
  117. 'nofile' => $params["configoption3"],
  118. 'nproc' => $params["configoption4"],
  119. 'server_ips'=>$params["serverip"]
  120. );
  121. $cwp7 = new cwp7_Admin($params['serverhostname'], $params['serveraccesshash']);
  122. $response = $cwp7->createAccount($data);
  123. }
  124. if($response['status'] != 'OK') {
  125. return 'Error: ' . $response['msj'];
  126. }
  127. return 'success';
  128. }
  129. function cwp7_TerminateAccount($params) {
  130. $cwp7 = new cwp7_Admin($params['serverhostname'], $params['serveraccesshash']);
  131. $response = $cwp7->deleteAccount(array('user' => $params['username'], 'email' => $params['clientsdetails']['email']));
  132. if($response['status'] != 'OK') {
  133. return 'Error: ' . $response['msj'];
  134. }
  135. return 'success';
  136. }
  137. function cwp7_SuspendAccount($params) {
  138. $cwp7 = new cwp7_Admin($params['serverhostname'], $params['serveraccesshash']);
  139. $response = $cwp7->suspendAccount($params['username']);
  140. if($response['status'] != 'OK') {
  141. return 'Error: ' . $response['msj'];
  142. }
  143. return 'success';
  144. }
  145. function cwp7_UnsuspendAccount($params) {
  146. $cwp7 = new cwp7_Admin($params['serverhostname'], $params['serveraccesshash']);
  147. $response = $cwp7->unsuspendAccount($params['username']);
  148. if($response['status'] != 'OK') {
  149. return 'Error: ' . $response['msj'];
  150. }
  151. return 'success';
  152. }
  153. function cwp7_ClientArea($params){
  154. //debug
  155. // $params['username'] = 'thurdata';
  156. //debug
  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. $clientInfo['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. logModuleCall(
  221. 'cwp7',
  222. __FUNCTION__,
  223. $response,
  224. 'debug',
  225. $params
  226. );
  227. return array(
  228. 'tabOverviewReplacementTemplate' => 'clientarea',
  229. 'vars' => $clientInfo,
  230. );
  231. }
  232. function cwp7_ServiceSingleSignOn($params) {
  233. $cwp7 = new cwp7_Admin($params['serverhostname'], $params['serveraccesshash']);
  234. $response = $cwp7->getLoginLink($params['username']);
  235. if($response['status'] == 'OK') {
  236. $link = $response['msj']['details'];
  237. $linkautologin = $link[0]['url'];
  238. return array(
  239. 'success' => true,
  240. 'redirectTo' => $linkautologin,
  241. );
  242. } else {
  243. return array(
  244. 'success' => false,
  245. 'redirectTo' => '',
  246. );
  247. }
  248. }
  249. function cwp7_ChangePassword($params){
  250. $cwp7 = new cwp7_Admin($params['serverhostname'], $params['serveraccesshash']);
  251. $response = $cwp7->changePass(array('user' => $params['username'], 'password' => $params['password']));
  252. if($response['status'] != 'OK') {
  253. return 'Error: ' . $response['msj'];
  254. }
  255. return 'success';
  256. }
  257. function cwp7_ChangePackage($params){
  258. $cwp7 = new cwp7_Admin($params['serverhostname'], $params['serveraccesshash']);
  259. $response = $cwp7->modifyAccount(array('user' => $params['username'], 'email' => $params['clientdetails']['email'], 'package' => $params['configoption1']));
  260. if($response['status'] != 'OK') {
  261. return 'Error: ' . $response['msj'];
  262. }
  263. return 'success';
  264. }
  265. function cwp7_UsageUpdate($params) {
  266. $cwp7 = new cwp7_Admin($params['serverhostname'], $params['serveraccesshash']);
  267. $response = $cwp7->getAllAccounts();
  268. if($response['status'] == 'OK'){
  269. $results = $response['msj'];
  270. for($i = 0; $i < count($results); $i++){
  271. if($results[$i]['diskusage'] == '') {
  272. $diskusage = 0;
  273. } else {
  274. $diskusage = trim($results[$i]['diskusage']);
  275. }
  276. if($results[$i]['disklimit'] == '') {
  277. $disklimit = 0;
  278. } else {
  279. $disklimit = trim($results[$i]['disklimit']);
  280. }
  281. if($results[$i]['bandwidth'] == '') {
  282. $bandwidth = 0;
  283. } else {
  284. $bandwidth =trim($results[$i]['bandwidth']);
  285. }
  286. if($results[$i]['bwlimit'] == '') {
  287. $bwlimit = 0;
  288. } else {
  289. $bwlimit = trim($results[$i]['bwlimit']);
  290. }
  291. $domain = trim($results[$i]['domain']);
  292. try {
  293. \WHMCS\Database\Capsule::table('tblhosting')
  294. ->where('server', $params['serverid'])
  295. ->where('domain', $domain)
  296. ->update([
  297. 'diskusage' => $diskusage,
  298. 'disklimit' => $disklimit,
  299. 'bwusage' => $bandwidth,
  300. 'bwlimit' => $bwlimit,
  301. 'lastupdate' => date('Y-m-d H:i:S'),
  302. ]);
  303. } catch (\Exception $e) {
  304. logActivity('ERROR: Unable to update server usage: ' . $e->getMessage());
  305. }
  306. }
  307. }
  308. }
  309. function cwp7_ClientAreaAllowedFunctions() {
  310. return array(
  311. "Enable SSL" => "enableSSL",
  312. );
  313. }
  314. function cwp7_enableSSL($params) {
  315. logModuleCall(
  316. 'cwp7',
  317. __FUNCTION__,
  318. $_POST,
  319. 'debug custom',
  320. $params
  321. );
  322. return 'success';
  323. }
  324. function cwp7_InfoDNS($params) {
  325. return array(
  326. 'breadcrumb' => array(
  327. 'clientarea.php?action=productdetails&id=' . $params['serviceid'] . '&modop=custom&a=infoDNS' => 'DNS Info',
  328. ),
  329. 'templatefile' => 'cwp7_help_dns',
  330. 'vars' => array(
  331. 'domain' => 'test',
  332. 'action' => 'action',
  333. 'nameserver' => (isset($nameserver) ? $nameserver : 'false'),
  334. ),
  335. );
  336. }
  337. function cwp7CheckA($domain, $serverIP, $recurse = 0) {
  338. if($recurse > 3) {
  339. return false;
  340. }
  341. $nameserver = array('10.200.22.100');
  342. $resolver = new Net_DNS2_Resolver(array('nameservers' => $nameserver));
  343. try {
  344. $result = $resolver->query($domain, 'A');
  345. } catch(Net_DNS2_Exception $e) {
  346. logModuleCall(
  347. 'cwp7',
  348. __FUNCTION__,
  349. $e,
  350. 'DNS lookup exception',
  351. $e->getMessage()
  352. );
  353. }
  354. $domainA = $result->answer;
  355. if($domainA[0]->type == 'CNAME') {
  356. if(cwp7CheckA($domainA[0]->cname, $serverIP, $recurse++)) {
  357. return true;
  358. }
  359. }
  360. if($domainA[0]->type == 'A') {
  361. if($domainA[0]->address == $serverIP){
  362. return true;
  363. }
  364. }
  365. }
  366. function cwp7CheckSOA($domain) {
  367. $tld = array_slice(explode('.', $domain), -1, 1)[0];
  368. $conn = fsockopen ('whois.iana.org', 43, $errno, $errstr);
  369. if($conn) {
  370. fputs($conn, $tld . "\r\n");
  371. while(!feof($conn)) {
  372. $response .= fgets($conn,128);
  373. }
  374. fclose($conn);
  375. $responseArray = str_getcsv($response, "\n");
  376. foreach($responseArray as $row) {
  377. if(str_starts_with($row, 'whois')) {
  378. $whoisServer = array_slice(explode(' ', $row), -1, 1)[0];
  379. }
  380. }
  381. } else {
  382. logModuleCall(
  383. 'cwp7',
  384. __FUNCTION__,
  385. $errno,
  386. 'debug2',
  387. $errstr
  388. );
  389. return 'error';
  390. }
  391. logModuleCall(
  392. 'cwp7',
  393. __FUNCTION__,
  394. $tld,
  395. 'debug2',
  396. $whoisServer
  397. );
  398. return 'none';
  399. }