-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathjetserver_SmartIPAllocation.php
More file actions
113 lines (90 loc) · 3.45 KB
/
Copy pathjetserver_SmartIPAllocation.php
File metadata and controls
113 lines (90 loc) · 3.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
<?php
/*
*
* Smart IP Allocation
* Created By Idan Ben-Ezra
*
* Copyrights @ Jetserver Web Hosting
* www.jetserver.net
*
* Hook version 1.0.3
*
**/
if (!defined("WHMCS"))
die("This file cannot be accessed directly");
/*********************
Smart IP Allocation Settings
*********************/
function jetserverSmartIPAllocation_settings()
{
/*
* For more information and examples please go to https://docs.jetapps.com/category/whmcs-addons/whmcs-smart-ip-allocation
**/
$output['exclude_servers_ids'] = array(); // server ids to exclude from the IP check * leave empty to use all servers
$output['only_group_ids'] = array(); // use only servers on the following groups * leave empty to use all servers groups
return $output;
}
/********************/
function jetserverSmartIPAllocation_checkServerWithFreeIPs($server_details)
{
$url = "{$server_details['serverhttpprefix']}://{$server_details['serverhostname']}:{$server_details['serverport']}/scripts/rebuildpool";
$password_type = $server_details['serveraccesshash'] ? 'WHM' : 'Basic';
$password = $password_type == 'WHM' ? preg_replace("'(\r|\n)'", "", $server_details['serveraccesshash']) : $server_details['serverpassword'];
$authorization = "Authorization: {$password_type} {$server_details['serverusername']}:{$password}";
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HTTPHEADER, array($authorization));
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($curl);
if ($result === false)
{
logModuleCall("Smart IP Allocation", "Check For Free IPs", $url, curl_error($curl), $result);
return 0;
}
logModuleCall("Smart IP Allocation", "Check For Free IPs", $url, '', $result);
curl_close($curl);
if(preg_match("/system has ([0-9]+)/i", $result, $match))
{
return intval($match[1]);
}
return 0;
}
function jetserverSmartIPAllocation_selectServer($vars)
{
if($vars['params']['moduletype'] == 'cpanel' && $vars['params']['configoption6'] == 'on' && !jetserverSmartIPAllocation_checkServerWithFreeIPs($vars['params']))
{
$settings = jetserverSmartIPAllocation_settings();
$settings['exclude_servers_ids'][] = $vars['params']['serverid'];
$sql = "SELECT s.*, g.groupid as gid
FROM tblservers as s
INNER JOIN tblservergroupsrel as g
ON s.id = g.serverid
WHERE s.id NOT IN ('" . implode("','", $settings['exclude_servers_ids']) . "')
" . (sizeof($settings['only_group_ids']) ? "AND g.groupid IN ('" . implode("','", $settings['only_group_ids']) . "')" : '');
$result = mysql_query($sql);
while($server_details = mysql_fetch_assoc($result))
{
$check = jetserverSmartIPAllocation_checkServerWithFreeIPs(array(
'serverhttpprefix' => $server_details['secure'] == 'on' ? 'https' : 'http',
'serverhostname' => $server_details['hostname'],
'serverport' => $server_details['port'] ? $server_details['port'] : ($server_details['secure'] == 'on' ? '2087' : '2086'),
'serveraccesshash' => $server_details['accesshash'],
'serverpassword' => $server_details['password'],
'serverusername' => $server_details['username'],
));
if($check)
{
$sql = "UPDATE tblhosting
SET server = '{$server_details['id']}'
WHERE id = '{$vars['params']['serviceid']}'";
mysql_query($sql);
break;
}
}
mysql_free_result($result);
}
}
add_hook('PreModuleCreate', 0, 'jetserverSmartIPAllocation_selectServer');
?>