cwp7.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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. $cwp7 = new cwp7_Admin($params['serverhostname'], $params['serveraccesshash']);
  153. $response = $cwp7->getAccount($params['username']);
  154. logModuleCall(
  155. 'cwp7',
  156. __FUNCTION__,
  157. $params,
  158. 'debug',
  159. $response
  160. );
  161. $clientInfo = array('moduleclientarea' => '1');
  162. return array(
  163. 'tabOverviewReplacementTemplate' => 'clientarea',
  164. 'vars' => $clientInfo,
  165. );
  166. }
  167. function cwp7_ServiceSingleSignOn($params) {
  168. $cwp7 = new cwp7_Admin($params['serverhostname'], $params['serveraccesshash']);
  169. $response = $cwp7->getLoginLink($params['username']);
  170. if($response['status'] == 'OK') {
  171. $link = $response['msj']['details'];
  172. $linkautologin = $link[0]['url'];
  173. return array(
  174. 'success' => true,
  175. 'redirectTo' => $linkautologin,
  176. );
  177. } else {
  178. return array(
  179. 'success' => false,
  180. 'redirectTo' => '',
  181. );
  182. }
  183. }
  184. function cwp7_ChangePassword($params){
  185. $cwp7 = new cwp7_Admin($params['serverhostname'], $params['serveraccesshash']);
  186. $response = $cwp7->changePass(array('user' => $params['username'], 'password' => $params['password']));
  187. if($response['status'] != 'OK') {
  188. return 'Error: ' . $response['msj'];
  189. }
  190. return 'success';
  191. }
  192. function cwp7_ChangePackage($params){
  193. $cwp7 = new cwp7_Admin($params['serverhostname'], $params['serveraccesshash']);
  194. $response = $cwp7->modifyAccount(array('user' => $params['username'], 'email' => $params['clientdetails']['email'], 'package' => $params['configoption1']));
  195. if($response['status'] != 'OK') {
  196. return 'Error: ' . $response['msj'];
  197. }
  198. return 'success';
  199. }
  200. function cwp7_UsageUpdate($params) {
  201. $cwp7 = new cwp7_Admin($params['serverhostname'], $params['serveraccesshash']);
  202. $response = $cwp7->getAllAccounts();
  203. if($response['status'] == 'OK'){
  204. $results = $response['msj'];
  205. for($i = 0; $i < count($results); $i++){
  206. if($results[$i]['diskusage'] == '') {
  207. $diskusage = 0;
  208. } else {
  209. $diskusage = trim($results[$i]['diskusage']);
  210. }
  211. if($results[$i]['disklimit'] == '') {
  212. $disklimit = 0;
  213. } else {
  214. $disklimit = trim($results[$i]['disklimit']);
  215. }
  216. if($results[$i]['bandwidth'] == '') {
  217. $bandwidth = 0;
  218. } else {
  219. $bandwidth =trim($results[$i]['bandwidth']);
  220. }
  221. if($results[$i]['bwlimit'] == '') {
  222. $bwlimit = 0;
  223. } else {
  224. $bwlimit = trim($results[$i]['bwlimit']);
  225. }
  226. $domain = trim($results[$i]['domain']);
  227. try {
  228. \WHMCS\Database\Capsule::table('tblhosting')
  229. ->where('server', $params['serverid'])
  230. ->where('domain', $domain)
  231. ->update([
  232. 'diskusage' => $diskusage,
  233. 'disklimit' => $disklimit,
  234. 'bwusage' => $bandwidth,
  235. 'bwlimit' => $bwlimit,
  236. 'lastupdate' => date('Y-m-d H:i:S'),
  237. ]);
  238. } catch (\Exception $e) {
  239. logActivity('ERROR: Unable to update server usage: ' . $e->getMessage());
  240. }
  241. }
  242. }
  243. }