PageRenderTime 47ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/utils/whmcs/owp/owp.php

http://ovz-web-panel.googlecode.com/
PHP | 261 lines | 193 code | 55 blank | 13 comment | 20 complexity | 207dcfe6da173649999298994fff89ad MD5 | raw file
Possible License(s): GPL-2.0, GPL-3.0
  1. <?php
  2. /**
  3. * Description: Module for OpenVZ Web Panel integration.
  4. * Site: http://code.google.com/p/ovz-web-panel/
  5. */
  6. /**
  7. * Module public methods
  8. */
  9. function owp_ConfigOptions()
  10. {
  11. $nodes = array();
  12. $serverTemplates = array();
  13. $osTempates = array();
  14. $result = _owp_apiCall('hardware_servers/list');
  15. foreach ($result->hardware_server as $hardwareServer) {
  16. $nodes[] = $hardwareServer->host;
  17. $serverTemplatesResult = _owp_apiCall('hardware_servers/server_templates', array('id' => (int)$hardwareServer->id));
  18. foreach ($serverTemplatesResult as $serverTemplate) {
  19. $serverTemplates[] = (string)$serverTemplate->name;
  20. }
  21. $osTemplatesResult = _owp_apiCall('hardware_servers/os_templates', array('id' => (int)$hardwareServer->id));
  22. foreach ($osTemplatesResult as $osTemplate) {
  23. $osTemplates[] = (string)$osTemplate->name;
  24. }
  25. }
  26. $serverTemplates = array_unique($serverTemplates);
  27. $osTemplates = array_unique($osTemplates);
  28. $userRoles = array();
  29. $result = _owp_apiCall('roles/list');
  30. foreach ($result->role as $role) {
  31. $userRoles[] = $role->name;
  32. }
  33. $configarray = array(
  34. "Node" => array("Type" => "dropdown", "Options" => join(',', $nodes)),
  35. "Server Template" => array("Type" => "dropdown", "Options" => join(',', $serverTemplates)),
  36. "OS" => array("Type" => "dropdown", "Options" => join(',', $osTemplates)),
  37. "User Role" => array("Type" => "dropdown", "Options" => join(',', $userRoles)),
  38. );
  39. return $configarray;
  40. }
  41. function owp_CreateAccount($params)
  42. {
  43. # ** The variables listed below are passed into all module functions **
  44. $serviceid = $params["serviceid"]; # Unique ID of the product/service in the WHMCS Database
  45. $pid = $params["pid"]; # Product/Service ID
  46. $producttype = $params["producttype"]; # Product Type: hostingaccount, reselleraccount, server or other
  47. $domain = $params["domain"];
  48. $username = $params["username"];
  49. $password = $params["password"];
  50. $clientsdetails = $params["clientsdetails"]; # Array of clients details - firstname, lastname, email, country, etc...
  51. $customfields = $params["customfields"]; # Array of custom field values for the product
  52. $configoptions = $params["configoptions"]; # Array of configurable option values for the product
  53. # Product module option settings from ConfigOptions array above
  54. $node = $params["configoption1"];
  55. $result = _owp_apiCall('hardware_servers/get_by_host', array('host' => $node));
  56. $hardwareServerId = (int)$result->id;
  57. $osTemplate = $params["configoption3"];
  58. $serverTemplate = $params["configoption2"];
  59. $userRole = $params["configoption4"];
  60. $result = _owp_apiCall('roles/get_by_name', array('name' => $userRole));
  61. $userRoleId = (int)$result->id;
  62. # Additional variables if the product/service is linked to a server
  63. $server = $params["server"]; # True if linked to a server
  64. $serverid = $params["serverid"];
  65. $serverip = $params["serverip"];
  66. $serverusername = $params["serverusername"];
  67. $serverpassword = $params["serverpassword"];
  68. $serveraccesshash = $params["serveraccesshash"];
  69. $serversecure = $params["serversecure"]; # If set, SSL Mode is enabled in the server config
  70. $query = mysql_query("SELECT dedicatedip FROM `tblhosting` WHERE `id` = '$serviceid'");
  71. $result = mysql_fetch_array($query);
  72. $ipAddress = $result['dedicatedip'];
  73. $result = _owp_apiCall('users/create', array(
  74. 'login' => $username,
  75. 'password' => $password,
  76. 'role_id' => $userRoleId,
  77. 'contact_name' => $clientsdetails['firstname'] . ' ' . $clientsdetails['lastname'],
  78. 'email' => $clientsdetails['email'],
  79. ));
  80. if ('error' == $result->getName()) {
  81. return (string)$result->message;
  82. }
  83. $userId = (int)$result->details->id;
  84. $result = _owp_apiCall('virtual_servers/create', array(
  85. 'hardware_server_id' => $hardwareServerId,
  86. 'orig_os_template' => $osTemplate,
  87. 'orig_server_template' => $serverTemplate,
  88. 'host_name' => $domain,
  89. 'ip_address' => $ipAddress,
  90. 'password' => $password,
  91. 'user_id' => $userId,
  92. ));
  93. if ('error' == $result->getName()) {
  94. return (string)$result->message;
  95. }
  96. $virtualServerId = (int)$result->details->id;
  97. $result = _owp_apiCall('virtual_servers/start', array('id' => $virtualServerId));
  98. if ('error' == $result->getName()) {
  99. return (string)$result->message;
  100. }
  101. return "success";
  102. }
  103. function owp_TerminateAccount($params)
  104. {
  105. $username = $params["username"];
  106. $result = _owp_apiCall('users/get_by_login', array('login' => $username));
  107. $userId = (int)$result->id;
  108. $result = _owp_apiCall('users/delete', array('id' => $userId));
  109. if ('error' == $result->getName()) {
  110. return (string)$result->message;
  111. }
  112. $domain = $params["domain"];
  113. $result = _owp_apiCall('virtual_servers/get_by_host', array('host' => $domain));
  114. $serverId = (int)$result->id;
  115. $result = _owp_apiCall('virtual_servers/delete', array('id' => $serverId));
  116. if ('error' == $result->getName()) {
  117. return (string)$result->message;
  118. }
  119. return "success";
  120. }
  121. function owp_SuspendAccount($params)
  122. {
  123. $username = $params["username"];
  124. $result = _owp_apiCall('users/get_by_login', array('login' => $username));
  125. $userId = (int)$result->id;
  126. $result = _owp_apiCall('users/disable', array('id' => $userId));
  127. if ('error' == $result->getName()) {
  128. return (string)$result->message;
  129. }
  130. $domain = $params["domain"];
  131. $result = _owp_apiCall('virtual_servers/get_by_host', array('host' => $domain));
  132. $serverId = (int)$result->id;
  133. $result = _owp_apiCall('virtual_servers/stop', array('id' => $serverId));
  134. if ('error' == $result->getName()) {
  135. return (string)$result->message;
  136. }
  137. return "success";
  138. }
  139. function owp_UnsuspendAccount($params)
  140. {
  141. $username = $params["username"];
  142. $result = _owp_apiCall('users/get_by_login', array('login' => $username));
  143. $userId = (int)$result->id;
  144. $result = _owp_apiCall('users/enable', array('id' => $userId));
  145. if ('error' == $result->getName()) {
  146. return (string)$result->message;
  147. }
  148. $domain = $params["domain"];
  149. $result = _owp_apiCall('virtual_servers/get_by_host', array('host' => $domain));
  150. $serverId = (int)$result->id;
  151. $result = _owp_apiCall('virtual_servers/start', array('id' => $serverId));
  152. if ('error' == $result->getName()) {
  153. return (string)$result->message;
  154. }
  155. return "success";
  156. }
  157. function owp_ClientArea($params)
  158. {
  159. $code = '<form action="http://' . _owp_getHost($params) . '/login" method="post" target="_blank">
  160. <input type="hidden" name="login" value="'.$params["username"].'" />
  161. <input type="hidden" name="password" value="'.$params["password"].'" />
  162. <input type="hidden" name="plain_post" value="1" />
  163. <input type="submit" value="Login to Control Panel" />
  164. </form>';
  165. return $code;
  166. }
  167. function owp_AdminLink($params)
  168. {
  169. $code = '<form action="http://' . _owp_getHost($params) . '/login" method="post" target="_blank">
  170. <input type="hidden" name="login" value="'.$params["serverusername"].'" />
  171. <input type="hidden" name="password" value="'.$params["serverpassword"].'" />
  172. <input type="hidden" name="plain_post" value="1" />
  173. <input type="submit" value="Login to Control Panel" />
  174. </form>';
  175. return $code;
  176. }
  177. function owp_LoginLink($params)
  178. {
  179. echo "<a href=\"http://" . _owp_getHost($params) . "/login?login=".$params["username"]."\" target=\"_blank\" style=\"color:#cc0000\">login to control panel</a>";
  180. }
  181. /**
  182. * Module private methods
  183. */
  184. function _owp_getHost($params)
  185. {
  186. return ('' != $params['serverhostname']) ? $params['serverhostname'] : $params['serverip'];
  187. }
  188. function _owp_apiCall($method, $params = '')
  189. {
  190. $queryResult = mysql_query("SELECT * FROM `tblservers` WHERE `type` = 'owp' LIMIT 1");
  191. $serverInfo = mysql_fetch_array($queryResult);
  192. $host = $serverInfo['hostname'];
  193. $user = $serverInfo['username'];
  194. $password = decrypt($serverInfo['password']);
  195. if (is_array($params)) {
  196. $params = http_build_query($params);
  197. }
  198. $context = stream_context_create(array(
  199. 'http' => array(
  200. 'header' => "Authorization: Basic " . base64_encode("$user:$password")
  201. )
  202. ));
  203. $result = file_get_contents("http://$host/api/$method?$params", false, $context);
  204. $doc = simplexml_load_string($result);
  205. return $doc;
  206. }