cwp7.php 11 KB

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