cwp7.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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. ->get();
  43. logModuleCall(
  44. 'cwp7',
  45. __FUNCTION__,
  46. $server,
  47. 'Debug',
  48. $whmcs
  49. );
  50. $configarray = array(
  51. "Package" => array( "Type" => "text", "Description" => "Package ID", "Default" => "1"),
  52. "inode" => array( "Type" => "text" , "Description" => "Max of inode", "Default" => "0",),
  53. "nofile" => array( "Type" => "text", "Description" => "Max of nofile", "Default" => "100", ),
  54. "nproc" => array( "Type" => "text" , "Description" => "Nproc limit - 40 suggested", "Default" => "40",),
  55. );
  56. return $configarray;
  57. }
  58. function cwp7_CreateAccount($params) {
  59. if ($params["server"] == 1) {
  60. $postvars = array(
  61. 'package' => $params["configoption1"],
  62. 'domain' => $params["domain"],
  63. 'key' => $params["serveraccesshash"],
  64. 'action' => 'add',
  65. 'username' => $params["username"],
  66. 'user' => $params["username"],
  67. 'pass' => $params["password"],
  68. 'email' => $params["clientsdetails"]["email"],
  69. 'inode' => $params["configoption2"],
  70. 'nofile' => $params["configoption3"],
  71. 'nproc' => $params["configoption4"],
  72. 'server_ips'=>$params["serverip"]
  73. );
  74. $postdata = http_build_query($postvars);
  75. $curl = curl_init();
  76. curl_setopt($curl, CURLOPT_URL, 'https://' . $params["serverhostname"] . ':2304/v1/account');
  77. curl_setopt($curl, CURLOPT_RETURNTRANSFER,true);
  78. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
  79. curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
  80. curl_setopt($curl, CURLOPT_POST, true);
  81. curl_setopt($curl, CURLOPT_POSTFIELDS, $postdata);
  82. $answer = curl_exec($curl);
  83. logModuleCall('cwpwhmcs','CreateAccount_UserAccount','https://' . $params["serverhostname"] . ':2304/v1/account/'.$postdata,$answer);
  84. }
  85. if(strpos($answer,"OK")!==false){$result='success';}else{$result=json_decode($answer,true); $result=$result['msj'];}
  86. return $result;
  87. }
  88. function cwp7_TerminateAccount($params) {
  89. if ($params["server"] == 1) {
  90. $postvars = array('key' => $params["serveraccesshash"],'action' => 'del','user' => $params["username"]);
  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','TerminateAccount','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_SuspendAccount($params) {
  106. if ($params["server"] == 1) {
  107. $postvars = array('key' => $params["serveraccesshash"],'action' => 'susp','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. }
  118. if(strpos($answer,"OK")!==false){$result='success';}else{$result=json_decode($answer,true); $result=$result['msj'];}
  119. logModuleCall('cwpwhmcs','SuspendAccount','https://' . $params["serverhostname"] . ':2304/v1/account/'.$postdata,$result);
  120. return $result;
  121. }
  122. function cwp7_UnsuspendAccount($params) {
  123. if ($params["server"] == 1) {
  124. $postvars = array('key' => $params["serveraccesshash"],'action' => 'unsp','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','UnsuspendAccount','https://' . $params["serverhostname"] . ':2304/v1/account'.$postdata,$result);
  137. return $result;
  138. }
  139. function cwp7_ClientArea($params){
  140. $postvars = array('key' => $params["serveraccesshash"], 'action' => 'list', 'user' => $params["username"], 'timer' => 5);
  141. $postdata = http_build_query($postvars);
  142. $curl = curl_init();
  143. curl_setopt($curl, CURLOPT_URL, 'https://' . $params["serverhostname"] . ':2304/v1/user_session');
  144. curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  145. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
  146. curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
  147. curl_setopt($curl, CURLOPT_POST, true);
  148. curl_setopt($curl, CURLOPT_POSTFIELDS, $postdata);
  149. $answer = curl_exec($curl);
  150. $arry = (json_decode($answer, true)); //die;F
  151. $link = $arry['msj']['details'];
  152. $linkautologin = $link[0]['url'];
  153. logModuleCall('cwpwhmcs', 'cwp7_LoginLink', 'https://' . $params["serverhostname"] . ':2304/v1/user_session' . $postdata, $answer);
  154. return "<a href=\"{$linkautologin}\" target=\"_blank\" style=\"color:#cc0000\">Login to Control Panel</a>";
  155. }
  156. function cwp7_AdminLink($params) {
  157. $code = '<form action="https://'.$params["serverhostname"].':2031" method="post" target="_blank">
  158. <input type="submit" value="Login to Control Panel" />
  159. </form>';
  160. return $code;
  161. }
  162. function cwp7_LoginLink($params) {
  163. $postvars = array('key' => $params["serveraccesshash"],'action' => 'list','user' => $params["username"],'timer'=>5);
  164. $postdata = http_build_query($postvars);
  165. $curl = curl_init();
  166. curl_setopt($curl, CURLOPT_URL, 'https://' . $params["serverhostname"] . ':2304/v1/user_session');
  167. curl_setopt($curl, CURLOPT_RETURNTRANSFER,true);
  168. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
  169. curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
  170. curl_setopt($curl, CURLOPT_POST, true);
  171. curl_setopt($curl, CURLOPT_POSTFIELDS, $postdata);
  172. $answer = curl_exec($curl);
  173. $arry=(json_decode($answer,true)); //die;F
  174. $link=$arry['msj']['details'];
  175. $linkautologin=$link[0]['url'];
  176. logModuleCall('cwpwhmcs','cwp7_LoginLink','https://' . $params["serverhostname"] . ':2304/v1/user_session'.$postdata,$answer);
  177. echo "<a href=\"{$linkautologin}\" target=\"_blank\" style=\"color:#cc0000\">Control Panel</a>";
  178. }
  179. function cwp7_ChangePassword($params){
  180. $postvars = array('key' => $params["serveraccesshash"],'acction' => 'udp','user' => $params["username"], 'pass' =>$params["password"]);
  181. $postdata = http_build_query($postvars);
  182. $curl = curl_init();
  183. curl_setopt($curl, CURLOPT_URL, 'https://'. $params["serverhostname"] . ':2304/v1/changepass');
  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. if(strpos($answer,"OK")!==false){$result='success';}else{$result=json_decode($answer,true); $result=$result['msj'];}
  191. logModuleCall('cwpwhmcs','ChangePassword','https://' . $params["serverhostname"] . ':2304/v1/changepass'.$postdata,$result);
  192. return $result;
  193. }
  194. function cwp7_ChangePackage($params){
  195. $postvars = array("key" => $params["serveraccesshash"],"action"=>'udp','user' => $params["username"],'package'=>$params["configoption1"].'@');
  196. $postdata = http_build_query($postvars);
  197. $curl = curl_init();
  198. curl_setopt($curl, CURLOPT_URL, 'https://'. $params["serverhostname"] . ':2304/v1/account');
  199. curl_setopt($curl, CURLOPT_RETURNTRANSFER,true);
  200. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
  201. curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
  202. curl_setopt($curl, CURLOPT_POST, true);
  203. curl_setopt($curl, CURLOPT_POSTFIELDS, $postdata);
  204. $answer = curl_exec($curl);
  205. curl_close($curl);
  206. if(strpos($answer,"OK")!==false){$result='success';}else{$result=json_decode($answer,true); $result=$result['msj'];}
  207. logModuleCall('cwpwhmcs','ChangePackage','https://' . $params["serverhostname"] . ':2304/v1/packages'.$postdata,$answer);
  208. return $result;
  209. }
  210. function cwp7_UsageUpdate($params) {
  211. $postvars = array('key' => $params["serveraccesshash"],'action' => 'list');
  212. $postdata = http_build_query($postvars);
  213. $curl = curl_init();
  214. curl_setopt($curl, CURLOPT_URL, 'https://'. $params["serverhostname"] . ':2304/v1/account');
  215. curl_setopt($curl, CURLOPT_RETURNTRANSFER,true);
  216. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
  217. curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
  218. curl_setopt($curl, CURLOPT_POST, true);
  219. curl_setopt($curl, CURLOPT_POSTFIELDS, $postdata);
  220. if (curl_errno($curl)) {
  221. $error_msg = curl_error($curl);
  222. }
  223. $answer = curl_exec($curl);
  224. $resp=json_decode($answer,true);
  225. if($resp['status']=='OK'){
  226. $results=$resp['msj'];
  227. for($i=0;$i<count($results);$i++){
  228. $date=date('Y-m-d H:i:s');
  229. if($results[$i]['diskusage']==''){$diskusage=0;}else{$diskusage=trim($results[$i]['diskusage']);}
  230. if($results[$i]['disklimit']==''){$disklimit=0;}else{$disklimit=trim($results[$i]['disklimit']);}
  231. if($results[$i]['bandwidth']==''){$bandwidth=0;}else{$bandwidth=trim($results[$i]['bandwidth']);}
  232. if($results[$i]['bwlimit']==''){$bwlimit=0;}else{$bwlimit=trim($results[$i]['bwlimit']);}
  233. $domian=trim($results[$i]['domain']);
  234. try {
  235. \WHMCS\Database\Capsule::table('tblhosting')
  236. ->where('dedicatedip', $results[$i]['ip_address'])
  237. ->where('domain', $domian)
  238. ->update([
  239. 'diskusage' => $diskusage,
  240. 'disklimit' => $disklimit,
  241. 'bwusage' => $bandwidth,
  242. 'bwlimit' => $bwlimit,
  243. 'lastupdate' => date('Y-m-d H:i:S'),
  244. ]);
  245. } catch (\Exception $e) {
  246. logActivity('ERROR: Unable to update server usage: ' . $e->getMessage());
  247. }
  248. }
  249. }
  250. }