cwp7.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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_ConfigOptions() {
  28. $whmcs = App::self();
  29. $serverGroupID = $whmcs->get_req_var('servergroup');
  30. $serverIDObj = Capsule::table('tblservergroupsrel')
  31. ->select('serverid')
  32. ->where('groupid', '=', $serverGroupID)
  33. ->get();
  34. $serverIDs = array();
  35. foreach($serverIDObj as $serverID) {
  36. array_push($serverIDs, $serverID->serverid);
  37. }
  38. $server = Capsule::table('tblservers')
  39. ->select('hostname', 'accesshash')
  40. ->where('id', $serverIDs)
  41. ->where('active', '=', 1)
  42. ->first();
  43. $cwp7 = new cwp7_Admin($server->hostname, $server->accesshash);
  44. $cwp7Packages = $cwp7->getPackages();
  45. if($cwp7Packages['status'] != 'OK') {
  46. logModuleCall(
  47. 'cwp7',
  48. __FUNCTION__,
  49. $cwp7Packages['status'],
  50. 'Could not fetch packages',
  51. $cwp7Packages['msj']
  52. );
  53. return false;
  54. }
  55. $cwp7PackageNames = array();
  56. foreach($cwp7Packages['msj'] as $cwp7Package) {
  57. array_push($cwp7PackageNames, $cwp7Package['package_name']);
  58. }
  59. logModuleCall(
  60. 'cwp7',
  61. __FUNCTION__,
  62. $cwp7PackageNames,
  63. 'Debug',
  64. $cwp7Packages
  65. );
  66. $configOptions = array();
  67. $configOptions['package'] = array(
  68. 'FriendlyName' => 'CWP7 Package',
  69. 'Type' => 'dropdown',
  70. 'Options' => implode(',', $cwp7PackageNames),
  71. 'Description' => 'Select CWP7 Package',
  72. );
  73. return $configOptions;
  74. }
  75. function cwp7_CreateAccount($params) {
  76. if ($params["server"] == 1) {
  77. $postvars = array(
  78. 'package' => $params["configoption1"],
  79. 'domain' => $params["domain"],
  80. 'key' => $params["serveraccesshash"],
  81. 'action' => 'add',
  82. 'username' => $params["username"],
  83. 'user' => $params["username"],
  84. 'pass' => $params["password"],
  85. 'email' => $params["clientsdetails"]["email"],
  86. 'inode' => $params["configoption2"],
  87. 'nofile' => $params["configoption3"],
  88. 'nproc' => $params["configoption4"],
  89. 'server_ips'=>$params["serverip"]
  90. );
  91. $postdata = http_build_query($postvars);
  92. $curl = curl_init();
  93. curl_setopt($curl, CURLOPT_URL, 'https://' . $params["serverhostname"] . ':2304/v1/account');
  94. curl_setopt($curl, CURLOPT_RETURNTRANSFER,true);
  95. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
  96. curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
  97. curl_setopt($curl, CURLOPT_POST, true);
  98. curl_setopt($curl, CURLOPT_POSTFIELDS, $postdata);
  99. $answer = curl_exec($curl);
  100. logModuleCall('cwpwhmcs','CreateAccount_UserAccount','https://' . $params["serverhostname"] . ':2304/v1/account/'.$postdata,$answer);
  101. }
  102. if(strpos($answer,"OK")!==false){$result='success';}else{$result=json_decode($answer,true); $result=$result['msj'];}
  103. return $result;
  104. }
  105. function cwp7_TerminateAccount($params) {
  106. if ($params["server"] == 1) {
  107. $postvars = array('key' => $params["serveraccesshash"],'action' => 'del','user' => $params["username"]);
  108. $postdata = http_build_query($postvars);
  109. $curl = curl_init();
  110. curl_setopt($curl, CURLOPT_URL, 'https://' . $params["serverhostname"] . ':2304/v1/account');
  111. curl_setopt($curl, CURLOPT_RETURNTRANSFER,true);
  112. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
  113. curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
  114. curl_setopt($curl, CURLOPT_POST, true);
  115. curl_setopt($curl, CURLOPT_POSTFIELDS, $postdata);
  116. $answer = curl_exec($curl);
  117. logModuleCall('cwpwhmcs','TerminateAccount','https://' . $params["serverhostname"] . ':2304/v1/account/'.$postdata,$answer);
  118. }
  119. if(strpos($answer,"OK")!==false){$result='success';}else{$result=json_decode($answer,true); $result=$result['msj'];}
  120. return $result;
  121. }
  122. function cwp7_SuspendAccount($params) {
  123. if ($params["server"] == 1) {
  124. $postvars = array('key' => $params["serveraccesshash"],'action' => 'susp','user' => $params["username"]);
  125. $postdata = http_build_query($postvars);
  126. $curl = curl_init();
  127. curl_setopt($curl, CURLOPT_URL, 'https://' . $params["serverhostname"] . ':2304/v1/account');
  128. curl_setopt($curl, CURLOPT_RETURNTRANSFER,true);
  129. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
  130. curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
  131. curl_setopt($curl, CURLOPT_POST, true);
  132. curl_setopt($curl, CURLOPT_POSTFIELDS, $postdata);
  133. $answer = curl_exec($curl);
  134. }
  135. if(strpos($answer,"OK")!==false){$result='success';}else{$result=json_decode($answer,true); $result=$result['msj'];}
  136. logModuleCall('cwpwhmcs','SuspendAccount','https://' . $params["serverhostname"] . ':2304/v1/account/'.$postdata,$result);
  137. return $result;
  138. }
  139. function cwp7_UnsuspendAccount($params) {
  140. if ($params["server"] == 1) {
  141. $postvars = array('key' => $params["serveraccesshash"],'action' => 'unsp','user' => $params["username"]);
  142. $postdata = http_build_query($postvars);
  143. $curl = curl_init();
  144. curl_setopt($curl, CURLOPT_URL, 'https://' . $params["serverhostname"] . ':2304/v1/account');
  145. curl_setopt($curl, CURLOPT_RETURNTRANSFER,true);
  146. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
  147. curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
  148. curl_setopt($curl, CURLOPT_POST, true);
  149. curl_setopt($curl, CURLOPT_POSTFIELDS, $postdata);
  150. $answer = curl_exec($curl);
  151. }
  152. if(strpos($answer,"OK")!==false){$result='success';}else{$result=json_decode($answer,true); $result=$result['msj'];}
  153. logModuleCall('cwpwhmcs','UnsuspendAccount','https://' . $params["serverhostname"] . ':2304/v1/account'.$postdata,$result);
  154. return $result;
  155. }
  156. function cwp7_ClientArea($params){
  157. $postvars = array('key' => $params["serveraccesshash"], 'action' => 'list', 'user' => $params["username"], 'timer' => 5);
  158. $postdata = http_build_query($postvars);
  159. $curl = curl_init();
  160. curl_setopt($curl, CURLOPT_URL, 'https://' . $params["serverhostname"] . ':2304/v1/user_session');
  161. curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  162. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
  163. curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
  164. curl_setopt($curl, CURLOPT_POST, true);
  165. curl_setopt($curl, CURLOPT_POSTFIELDS, $postdata);
  166. $answer = curl_exec($curl);
  167. $arry = (json_decode($answer, true)); //die;F
  168. $link = $arry['msj']['details'];
  169. $linkautologin = $link[0]['url'];
  170. logModuleCall('cwpwhmcs', 'cwp7_LoginLink', 'https://' . $params["serverhostname"] . ':2304/v1/user_session' . $postdata, $answer);
  171. return "<a href=\"{$linkautologin}\" target=\"_blank\" style=\"color:#cc0000\">Login to Control Panel</a>";
  172. }
  173. function cwp7_AdminLink($params) {
  174. $code = '<form action="https://'.$params["serverhostname"].':2031" method="post" target="_blank">
  175. <input type="submit" value="Login to Control Panel" />
  176. </form>';
  177. return $code;
  178. }
  179. function cwp7_LoginLink($params) {
  180. $postvars = array('key' => $params["serveraccesshash"],'action' => 'list','user' => $params["username"],'timer'=>5);
  181. $postdata = http_build_query($postvars);
  182. $curl = curl_init();
  183. curl_setopt($curl, CURLOPT_URL, 'https://' . $params["serverhostname"] . ':2304/v1/user_session');
  184. curl_setopt($curl, CURLOPT_RETURNTRANSFER,true);
  185. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
  186. curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
  187. curl_setopt($curl, CURLOPT_POST, true);
  188. curl_setopt($curl, CURLOPT_POSTFIELDS, $postdata);
  189. $answer = curl_exec($curl);
  190. $arry=(json_decode($answer,true)); //die;F
  191. $link=$arry['msj']['details'];
  192. $linkautologin=$link[0]['url'];
  193. logModuleCall('cwpwhmcs','cwp7_LoginLink','https://' . $params["serverhostname"] . ':2304/v1/user_session'.$postdata,$answer);
  194. echo "<a href=\"{$linkautologin}\" target=\"_blank\" style=\"color:#cc0000\">Control Panel</a>";
  195. }
  196. function cwp7_ChangePassword($params){
  197. $postvars = array('key' => $params["serveraccesshash"],'acction' => 'udp','user' => $params["username"], 'pass' =>$params["password"]);
  198. $postdata = http_build_query($postvars);
  199. $curl = curl_init();
  200. curl_setopt($curl, CURLOPT_URL, 'https://'. $params["serverhostname"] . ':2304/v1/changepass');
  201. curl_setopt($curl, CURLOPT_RETURNTRANSFER,true);
  202. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
  203. curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
  204. curl_setopt($curl, CURLOPT_POST, true);
  205. curl_setopt($curl, CURLOPT_POSTFIELDS, $postdata);
  206. $answer = curl_exec($curl);
  207. if(strpos($answer,"OK")!==false){$result='success';}else{$result=json_decode($answer,true); $result=$result['msj'];}
  208. logModuleCall('cwpwhmcs','ChangePassword','https://' . $params["serverhostname"] . ':2304/v1/changepass'.$postdata,$result);
  209. return $result;
  210. }
  211. function cwp7_ChangePackage($params){
  212. $postvars = array("key" => $params["serveraccesshash"],"action"=>'udp','user' => $params["username"],'package'=>$params["configoption1"].'@');
  213. $postdata = http_build_query($postvars);
  214. $curl = curl_init();
  215. curl_setopt($curl, CURLOPT_URL, 'https://'. $params["serverhostname"] . ':2304/v1/account');
  216. curl_setopt($curl, CURLOPT_RETURNTRANSFER,true);
  217. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
  218. curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
  219. curl_setopt($curl, CURLOPT_POST, true);
  220. curl_setopt($curl, CURLOPT_POSTFIELDS, $postdata);
  221. $answer = curl_exec($curl);
  222. curl_close($curl);
  223. if(strpos($answer,"OK")!==false){$result='success';}else{$result=json_decode($answer,true); $result=$result['msj'];}
  224. logModuleCall('cwpwhmcs','ChangePackage','https://' . $params["serverhostname"] . ':2304/v1/packages'.$postdata,$answer);
  225. return $result;
  226. }
  227. function cwp7_UsageUpdate($params) {
  228. $postvars = array('key' => $params["serveraccesshash"],'action' => 'list');
  229. $postdata = http_build_query($postvars);
  230. $curl = curl_init();
  231. curl_setopt($curl, CURLOPT_URL, 'https://'. $params["serverhostname"] . ':2304/v1/account');
  232. curl_setopt($curl, CURLOPT_RETURNTRANSFER,true);
  233. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
  234. curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
  235. curl_setopt($curl, CURLOPT_POST, true);
  236. curl_setopt($curl, CURLOPT_POSTFIELDS, $postdata);
  237. if (curl_errno($curl)) {
  238. $error_msg = curl_error($curl);
  239. }
  240. $answer = curl_exec($curl);
  241. $resp=json_decode($answer,true);
  242. if($resp['status']=='OK'){
  243. $results=$resp['msj'];
  244. for($i=0;$i<count($results);$i++){
  245. $date=date('Y-m-d H:i:s');
  246. if($results[$i]['diskusage']==''){$diskusage=0;}else{$diskusage=trim($results[$i]['diskusage']);}
  247. if($results[$i]['disklimit']==''){$disklimit=0;}else{$disklimit=trim($results[$i]['disklimit']);}
  248. if($results[$i]['bandwidth']==''){$bandwidth=0;}else{$bandwidth=trim($results[$i]['bandwidth']);}
  249. if($results[$i]['bwlimit']==''){$bwlimit=0;}else{$bwlimit=trim($results[$i]['bwlimit']);}
  250. $domian=trim($results[$i]['domain']);
  251. try {
  252. \WHMCS\Database\Capsule::table('tblhosting')
  253. ->where('dedicatedip', $results[$i]['ip_address'])
  254. ->where('domain', $domian)
  255. ->update([
  256. 'diskusage' => $diskusage,
  257. 'disklimit' => $disklimit,
  258. 'bwusage' => $bandwidth,
  259. 'bwlimit' => $bwlimit,
  260. 'lastupdate' => date('Y-m-d H:i:S'),
  261. ]);
  262. } catch (\Exception $e) {
  263. logActivity('ERROR: Unable to update server usage: ' . $e->getMessage());
  264. }
  265. }
  266. }
  267. }