cwp7.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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(__DIR__ . '/api/cwp7/Admin.php');
  13. if (!defined('WHMCS')) {
  14. die('This file cannot be accessed directly');
  15. }
  16. function cwp7_MetaData() {
  17. return array(
  18. 'DisplayName' => 'CentOS Web Panel Provisioning',
  19. 'APIVersion' => '1.2',
  20. 'DefaultNonSSLPort' => '2031',
  21. 'DefaultSSLPort' => '2031',
  22. 'RequiresServer' => true,
  23. 'ServiceSingleSignOnLabel' => 'Login to CWP7',
  24. 'AdminSingleSignOnLabel' => 'Login to CWP7 Admin'
  25. );
  26. }
  27. function cwp7_Testconnection($params) {
  28. $cwp7 = new cwp7_Admin($params['serverhostname'], $params['serveraccesshash']);
  29. $response = $cwp7->getServerType();
  30. if($response['status'] == 'OK') {
  31. return array(
  32. 'success' => true,
  33. 'error' => '',
  34. );
  35. }
  36. return array(
  37. 'success' => false,
  38. 'error' => $response['msj'],
  39. );
  40. }
  41. function cwp7_ConfigOptions() {
  42. $whmcs = App::self();
  43. $serverGroupID = $whmcs->get_req_var('servergroup');
  44. $serverIDObj = Capsule::table('tblservergroupsrel')
  45. ->select('serverid')
  46. ->where('groupid', '=', $serverGroupID)
  47. ->get();
  48. $serverIDs = array();
  49. foreach($serverIDObj as $serverID) {
  50. array_push($serverIDs, $serverID->serverid);
  51. }
  52. $server = Capsule::table('tblservers')
  53. ->select('hostname', 'accesshash')
  54. ->where('id', $serverIDs)
  55. ->where('active', '=', 1)
  56. ->first();
  57. $cwp7 = new cwp7_Admin($server->hostname, $server->accesshash);
  58. $cwp7Packages = $cwp7->getPackages();
  59. if($cwp7Packages['status'] != 'OK') {
  60. logModuleCall(
  61. 'cwp7',
  62. __FUNCTION__,
  63. $cwp7Packages['status'],
  64. 'Could not fetch packages',
  65. $cwp7Packages['msj']
  66. );
  67. return false;
  68. }
  69. $cwp7PackageNames = array();
  70. foreach($cwp7Packages['msj'] as $cwp7Package) {
  71. array_push($cwp7PackageNames, $cwp7Package['package_name']);
  72. }
  73. $configOptions = array();
  74. $configOptions['package'] = array(
  75. 'FriendlyName' => 'CWP7 Package',
  76. 'Type' => 'dropdown',
  77. 'Options' => implode(',', $cwp7PackageNames),
  78. 'Description' => 'Select CWP7 Package',
  79. );
  80. $configOptions['inode'] = array( "Type" => "text" , "Description" => "Max of inode", "Default" => "0",);
  81. $configOptions['nofile'] = array( "Type" => "text", "Description" => "Max of nofile", "Default" => "100",);
  82. $configOptions['nproc'] = array( "Type" => "text" , "Description" => "Nproc limit - 40 suggested", "Default" => "40",);
  83. return $configOptions;
  84. }
  85. function cwp7_CreateAccount($params) {
  86. $username = strtolower(substr($params['clientsdetails']['firstname'],0,2) . substr($params['clientsdetails']['lastname'],0,3)) . $params['serviceid'];
  87. $userdomain = $username . '.local';
  88. try {
  89. Capsule::table('tblhosting')
  90. ->where('id', '=', $params['serviceid'])
  91. ->update(
  92. array(
  93. 'username' => $username,
  94. 'domain' => $userdomain,
  95. )
  96. );
  97. } catch (\Exception $e) {
  98. logModuleCall(
  99. 'cwp7',
  100. __FUNCTION__,
  101. $params,
  102. 'Error: could save username & domain in database',
  103. $e->getMessage()
  104. );
  105. return 'Error: could save username & password in database';
  106. }
  107. if ($params["server"] == 1) {
  108. $data = array(
  109. 'package' => $params['configoption1'],
  110. 'domain' => $userdomain,
  111. 'user' => $username,
  112. 'pass' => $params['password'],
  113. 'email' => $params['clientsdetails']['email'],
  114. 'inode' => $params["configoption2"],
  115. 'nofile' => $params["configoption3"],
  116. 'nproc' => $params["configoption4"],
  117. 'server_ips'=>$params["serverip"]
  118. );
  119. $cwp7 = new cwp7_Admin($params['serverhostname'], $params['serveraccesshash']);
  120. $response = $cwp7->createAccount($data);
  121. }
  122. if($response['status'] != 'OK') {
  123. return 'Error: ' . $response['msj'];
  124. }
  125. return 'success';
  126. }
  127. function cwp7_TerminateAccount($params) {
  128. $cwp7 = new cwp7_Admin($params['serverhostname'], $params['serveraccesshash']);
  129. $response = $cwp7->deleteAccount(array('user' => $params['username'], 'email' => $params['clientsdetails']['email']));
  130. if($response['status'] != 'OK') {
  131. return 'Error: ' . $response['msj'];
  132. }
  133. return 'success';
  134. }
  135. function cwp7_SuspendAccount($params) {
  136. $cwp7 = new cwp7_Admin($params['serverhostname'], $params['serveraccesshash']);
  137. $response = $cwp7->suspendAccount($params['username']);
  138. if($response['status'] != 'OK') {
  139. return 'Error: ' . $response['msj'];
  140. }
  141. return 'success';
  142. }
  143. function cwp7_UnsuspendAccount($params) {
  144. $cwp7 = new cwp7_Admin($params['serverhostname'], $params['serveraccesshash']);
  145. $response = $cwp7->unsuspendAccount($params['username']);
  146. if($response['status'] != 'OK') {
  147. return 'Error: ' . $response['msj'];
  148. }
  149. return 'success';
  150. }
  151. function cwp7_ClientArea($params){
  152. $clientInfo = array('moduleclientarea' => '1');
  153. $cwp7 = new cwp7_Admin($params['serverhostname'], $params['serveraccesshash']);
  154. $response = $cwp7->getAccount($params['username']);
  155. if($response['status'] != 'OK') {
  156. logModuleCall(
  157. 'cwp7',
  158. __FUNCTION__,
  159. $params,
  160. 'debug',
  161. $response
  162. );
  163. }
  164. logModuleCall(
  165. 'cwp7',
  166. __FUNCTION__,
  167. $params,
  168. 'debug',
  169. $response
  170. );
  171. return array(
  172. 'tabOverviewReplacementTemplate' => 'clientarea',
  173. 'vars' => $clientInfo,
  174. );
  175. }
  176. function cwp7_ServiceSingleSignOn($params) {
  177. $cwp7 = new cwp7_Admin($params['serverhostname'], $params['serveraccesshash']);
  178. $response = $cwp7->getLoginLink($params['username']);
  179. if($response['status'] == 'OK') {
  180. $link = $response['msj']['details'];
  181. $linkautologin = $link[0]['url'];
  182. return array(
  183. 'success' => true,
  184. 'redirectTo' => $linkautologin,
  185. );
  186. } else {
  187. return array(
  188. 'success' => false,
  189. 'redirectTo' => '',
  190. );
  191. }
  192. }
  193. function cwp7_ChangePassword($params){
  194. $cwp7 = new cwp7_Admin($params['serverhostname'], $params['serveraccesshash']);
  195. $response = $cwp7->changePass(array('user' => $params['username'], 'password' => $params['password']));
  196. if($response['status'] != 'OK') {
  197. return 'Error: ' . $response['msj'];
  198. }
  199. return 'success';
  200. }
  201. function cwp7_ChangePackage($params){
  202. $cwp7 = new cwp7_Admin($params['serverhostname'], $params['serveraccesshash']);
  203. $response = $cwp7->modifyAccount(array('user' => $params['username'], 'email' => $params['clientdetails']['email'], 'package' => $params['configoption1']));
  204. if($response['status'] != 'OK') {
  205. return 'Error: ' . $response['msj'];
  206. }
  207. return 'success';
  208. }
  209. function cwp7_UsageUpdate($params) {
  210. $cwp7 = new cwp7_Admin($params['serverhostname'], $params['serveraccesshash']);
  211. $response = $cwp7->getAllAccounts();
  212. if($response['status'] == 'OK'){
  213. $results = $response['msj'];
  214. for($i = 0; $i < count($results); $i++){
  215. if($results[$i]['diskusage'] == '') {
  216. $diskusage = 0;
  217. } else {
  218. $diskusage = trim($results[$i]['diskusage']);
  219. }
  220. if($results[$i]['disklimit'] == '') {
  221. $disklimit = 0;
  222. } else {
  223. $disklimit = trim($results[$i]['disklimit']);
  224. }
  225. if($results[$i]['bandwidth'] == '') {
  226. $bandwidth = 0;
  227. } else {
  228. $bandwidth =trim($results[$i]['bandwidth']);
  229. }
  230. if($results[$i]['bwlimit'] == '') {
  231. $bwlimit = 0;
  232. } else {
  233. $bwlimit = trim($results[$i]['bwlimit']);
  234. }
  235. $domain = trim($results[$i]['domain']);
  236. try {
  237. \WHMCS\Database\Capsule::table('tblhosting')
  238. ->where('server', $params['serverid'])
  239. ->where('domain', $domain)
  240. ->update([
  241. 'diskusage' => $diskusage,
  242. 'disklimit' => $disklimit,
  243. 'bwusage' => $bandwidth,
  244. 'bwlimit' => $bwlimit,
  245. 'lastupdate' => date('Y-m-d H:i:S'),
  246. ]);
  247. } catch (\Exception $e) {
  248. logActivity('ERROR: Unable to update server usage: ' . $e->getMessage());
  249. }
  250. }
  251. }
  252. }