cwp7.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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. const NS1 = '8.8.8.8';
  18. const NS2 = '9.9.9.10';
  19. function cwp7_MetaData() {
  20. return array(
  21. 'DisplayName' => 'CentOS Web Panel Provisioning',
  22. 'APIVersion' => '1.2',
  23. 'DefaultNonSSLPort' => '2031',
  24. 'DefaultSSLPort' => '2031',
  25. 'RequiresServer' => true,
  26. 'ServiceSingleSignOnLabel' => 'Login to CWP7',
  27. 'AdminSingleSignOnLabel' => 'Login to CWP7 Admin'
  28. );
  29. }
  30. function cwp7_Testconnection($params) {
  31. $cwp7 = new cwp7_Admin($params['serverhostname'], $params['serveraccesshash']);
  32. $response = $cwp7->getServerType();
  33. if($response['status'] == 'OK') {
  34. return array(
  35. 'success' => true,
  36. 'error' => '',
  37. );
  38. }
  39. return array(
  40. 'success' => false,
  41. 'error' => $response['msj'],
  42. );
  43. }
  44. function cwp7_ConfigOptions() {
  45. $whmcs = App::self();
  46. $serverGroupID = $whmcs->get_req_var('servergroup');
  47. $serverIDObj = Capsule::table('tblservergroupsrel')
  48. ->select('serverid')
  49. ->where('groupid', '=', $serverGroupID)
  50. ->get();
  51. $serverIDs = array();
  52. foreach($serverIDObj as $serverID) {
  53. array_push($serverIDs, $serverID->serverid);
  54. }
  55. $server = Capsule::table('tblservers')
  56. ->select('hostname', 'accesshash')
  57. ->where('id', $serverIDs)
  58. ->where('active', '=', 1)
  59. ->first();
  60. $cwp7 = new cwp7_Admin($server->hostname, $server->accesshash);
  61. $cwp7Packages = $cwp7->getPackages();
  62. if($cwp7Packages['status'] != 'OK') {
  63. logModuleCall(
  64. 'cwp7',
  65. __FUNCTION__,
  66. $cwp7Packages['status'],
  67. 'Could not fetch packages',
  68. $cwp7Packages['msj']
  69. );
  70. return false;
  71. }
  72. $cwp7PackageNames = array();
  73. foreach($cwp7Packages['msj'] as $cwp7Package) {
  74. array_push($cwp7PackageNames, $cwp7Package['package_name']);
  75. }
  76. $configOptions = array();
  77. $configOptions['package'] = array(
  78. 'FriendlyName' => 'CWP7 Package',
  79. 'Type' => 'dropdown',
  80. 'Options' => implode(',', $cwp7PackageNames),
  81. 'Description' => 'Select CWP7 Package',
  82. );
  83. $configOptions['inode'] = array( "Type" => "text" , "Description" => "Max of inode", "Default" => "0",);
  84. $configOptions['nofile'] = array( "Type" => "text", "Description" => "Max of nofile", "Default" => "100",);
  85. $configOptions['nproc'] = array( "Type" => "text" , "Description" => "Nproc limit - 40 suggested", "Default" => "40",);
  86. return $configOptions;
  87. }
  88. function cwp7_CreateAccount($params) {
  89. $username = strtolower(substr($params['clientsdetails']['firstname'],0,2) . substr($params['clientsdetails']['lastname'],0,3)) . $params['serviceid'];
  90. $userdomain = $username . '.local';
  91. try {
  92. Capsule::table('tblhosting')
  93. ->where('id', '=', $params['serviceid'])
  94. ->update(
  95. array(
  96. 'username' => $username,
  97. 'domain' => $userdomain,
  98. )
  99. );
  100. } catch (\Exception $e) {
  101. logModuleCall(
  102. 'cwp7',
  103. __FUNCTION__,
  104. $params,
  105. 'Error: could save username & domain in database',
  106. $e->getMessage()
  107. );
  108. return 'Error: could save username & password in database';
  109. }
  110. if ($params["server"] == 1) {
  111. $data = array(
  112. 'package' => $params['configoption1'],
  113. 'domain' => $userdomain,
  114. 'user' => $username,
  115. 'pass' => $params['password'],
  116. 'email' => $params['clientsdetails']['email'],
  117. 'inode' => $params["configoption2"],
  118. 'nofile' => $params["configoption3"],
  119. 'nproc' => $params["configoption4"],
  120. 'server_ips'=>$params["serverip"]
  121. );
  122. $cwp7 = new cwp7_Admin($params['serverhostname'], $params['serveraccesshash']);
  123. $response = $cwp7->createAccount($data);
  124. }
  125. if($response['status'] != 'OK') {
  126. return 'Error: ' . $response['msj'];
  127. }
  128. return 'success';
  129. }
  130. function cwp7_TerminateAccount($params) {
  131. $cwp7 = new cwp7_Admin($params['serverhostname'], $params['serveraccesshash']);
  132. $response = $cwp7->deleteAccount(array('user' => $params['username'], 'email' => $params['clientsdetails']['email']));
  133. if($response['status'] != 'OK') {
  134. return 'Error: ' . $response['msj'];
  135. }
  136. return 'success';
  137. }
  138. function cwp7_SuspendAccount($params) {
  139. $cwp7 = new cwp7_Admin($params['serverhostname'], $params['serveraccesshash']);
  140. $response = $cwp7->suspendAccount($params['username']);
  141. if($response['status'] != 'OK') {
  142. return 'Error: ' . $response['msj'];
  143. }
  144. return 'success';
  145. }
  146. function cwp7_UnsuspendAccount($params) {
  147. $cwp7 = new cwp7_Admin($params['serverhostname'], $params['serveraccesshash']);
  148. $response = $cwp7->unsuspendAccount($params['username']);
  149. if($response['status'] != 'OK') {
  150. return 'Error: ' . $response['msj'];
  151. }
  152. return 'success';
  153. }
  154. function cwp7_ClientArea($params){
  155. //debug
  156. // $params['username'] = 'ziilimmo';
  157. //debug
  158. $clientInfo = array('moduleclientarea' => '1');
  159. $cwp7 = new cwp7_Admin($params['serverhostname'], $params['serveraccesshash']);
  160. $response = $cwp7->getAutoSSL($params['username']);
  161. if($response['status'] == 'OK') {
  162. $sslSites = array();
  163. foreach($response['msj'] as $sslSite) {
  164. $sslSites[$sslSite['ssl']] = array(
  165. 'auotssl' => $sslSite['autossl'],
  166. 'expire' => $sslSite['exp'],
  167. );
  168. }
  169. }
  170. $response = $cwp7->getAccount($params['username']);
  171. if($response['status'] != 'OK') {
  172. logModuleCall(
  173. 'cwp7',
  174. __FUNCTION__,
  175. $params,
  176. 'debug',
  177. $response
  178. );
  179. }
  180. $domains = $response['result']['domains'];
  181. $subDomains = $response['result']['subdomins'];
  182. $clientInfo['domains'] = array();
  183. foreach($domains as $domain) {
  184. if($domain['path'] == '/home/' . $params['username'] . '/public_html') {
  185. $clientInfo['mgmtDomain'] = $domain['domain'];
  186. $clientInfo['mgmtEmail'] = $domain['email'];
  187. } else {
  188. if(array_key_exists($domain['domain'], $sslSites)) {
  189. $domain['ssl'] = 1;
  190. $domain['sslexpire'] = $sslSites[$domain['domain']]['expire'];
  191. $domain['autossl'] = $sslSites[$domain['domain']]['auotssl'];
  192. }
  193. if(cwp7CheckDNS($domain['domain']) == 1) {
  194. $domain['DNS'] = 1;
  195. }
  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(cwp7CheckDNS($domain['domain']) == 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. $clientInfo
  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 cwp7CheckDNS($domain) {
  310. $nameserver = array( NS1, NS2);
  311. $resolver = new Net_DNS2_Resolver(array('nameservers' => $nameserver));
  312. try {
  313. $result = $resolver->query($domain, 'A');
  314. } catch(Net_DNS2_Exception $e) {
  315. logModuleCall(
  316. 'cwp7',
  317. __FUNCTION__,
  318. $e,
  319. 'debug',
  320. $e->getMessage()
  321. );
  322. }
  323. $domainA = $result->answer;
  324. logModuleCall(
  325. 'cwp7',
  326. __FUNCTION__,
  327. $domainA,
  328. 'debug',
  329. $result
  330. );
  331. }