Skip to content

Commit a415f19

Browse files
author
Pascal Dreissen
committed
Modified IP suggestions to only view the IP from the logged in customer. Added hints (description) to A or AAAA records. Fixes #4529 #4386
1 parent fc968e2 commit a415f19

File tree

5 files changed

+94
-34
lines changed

5 files changed

+94
-34
lines changed

interface/lib/classes/functions.inc.php

Lines changed: 64 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -182,11 +182,25 @@ public function json_encode($data) {
182182
}
183183
}
184184

185+
186+
/**
187+
* Function to suggest IP addresses in selectbox with hints, limited to the client logged in.
188+
*
189+
* @access public
190+
* @param string $type (default: 'IPv4')
191+
* @return void
192+
*/
185193
public function suggest_ips($type = 'IPv4'){
186194
global $app;
187195

196+
$use_suggestions = $app->getconf->get_global_config('misc')['use_ipsuggestions'] == 'y' ? true : false;;
197+
if(!$use_suggestions) {return array('cheader' => array(), 'cdata' => array());}
198+
199+
$suggestions_max = $app->getconf->get_global_config('misc')['ipsuggestions_max'];
200+
$groupid = intval($_SESSION["s"]["user"]["default_group"]);
201+
188202
if($type == 'IPv4'){
189-
// $regex = "/^[0-9]{1,3}(\.)[0-9]{1,3}(\.)[0-9]{1,3}(\.)[0-9]{1,3}$/";
203+
// $regex = "/^[0-9]{1,3}(\.)[0-9]{1,3}(\.)[0-9]{1,3}(\.)[0-9]{1,3}$/";
190204
$regex = "/^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/";
191205
} else {
192206
// IPv6
@@ -198,58 +212,75 @@ public function suggest_ips($type = 'IPv4'){
198212
$servers = $app->db->queryAllRecords("SELECT * FROM server");
199213
if(is_array($servers) && !empty($servers)){
200214
foreach($servers as $server){
201-
$server_by_id[$server['server_id']] = $server['server_name'];
215+
$server_by_id[$server['server_id']] = '<span class="ip_suggestion_server">server: ' . $server['server_name'] . '</span>';
202216
}
203217
}
204218

219+
$localips = array();
205220
$ips = array();
206221
$results = $app->db->queryAllRecords("SELECT ip_address AS ip, server_id FROM server_ip WHERE ip_type = ?", $type);
207222
if(!empty($results) && is_array($results)){
208223
foreach($results as $result){
209224
if(preg_match($regex, $result['ip'])){
210-
$ips[] = $result['ip'];
225+
$localips[] = $result['ip'];
211226
$server_by_ip[$result['ip']] = $server_by_id[$result['server_id']];
212227
}
213228
}
214229
}
230+
215231
$results = $app->db->queryAllRecords("SELECT ip_address AS ip FROM openvz_ip");
216232
if(!empty($results) && is_array($results)){
217233
foreach($results as $result){
218234
if(preg_match($regex, $result['ip'])) $ips[] = $result['ip'];
219235
}
220236
}
221-
$results = $app->db->queryAllRecords("SELECT data AS ip FROM dns_rr WHERE type = 'A' OR type = 'AAAA'");
237+
$results = $groupid != 1 ? $app->db->queryAllRecords("SELECT rr.data AS server_ip, rr.name as server_name, soa.origin as domain FROM dns_rr as rr, dns_soa as soa WHERE (rr.type = 'A' OR rr.type = 'AAAA') AND soa.id = rr.zone AND rr.sys_groupid = ?", $groupid) : $results = $app->db->queryAllRecords("SELECT rr.data AS server_ip, rr.name as server_name, soa.origin as domain FROM dns_rr as rr, dns_soa as soa WHERE (rr.type = 'A' OR rr.type = 'AAAA') AND soa.id = rr.zone");
238+
222239
if(!empty($results) && is_array($results)){
223240
foreach($results as $result){
224-
if(preg_match($regex, $result['ip'])) $ips[] = $result['ip'];
241+
$result['server_name'] = substr($result['server_name'], -1) == '.' ? $result['server_name'] : $result['server_name'] . '.' . $result['domain'];
242+
if (!array_key_exists($result['server_ip'],$server_by_ip)) {
243+
$server_by_ip[$result['server_ip']] = 'dns: ' . $result['server_name'];
244+
if(preg_match($regex, $result['server_ip'])) $ips[] = $result['server_ip'];
245+
}
225246
}
226247
}
227-
$results = $app->db->queryAllRecords("SELECT ns AS ip FROM dns_slave");
248+
249+
$results = $groupid != 1 ? $app->db->queryAllRecords("SELECT ns AS ip FROM dns_slave WHERE sys_groupid = ?", $groupid) : $results = $app->db->queryAllRecords("SELECT ns AS ip FROM dns_slave");
250+
228251
if(!empty($results) && is_array($results)){
229252
foreach($results as $result){
230-
if(preg_match($regex, $result['ip'])) $ips[] = $result['ip'];
253+
if (!array_key_exists($result['ip'],$server_by_ip)) {
254+
if(preg_match($regex, $result['ip'])) $ips[] = $result['ip'];
255+
}
231256
}
232257
}
233-
234-
$results = $app->db->queryAllRecords("SELECT remote_ips FROM web_database WHERE remote_ips != ''");
258+
259+
$results = $groupid != 1 ? $app->db->queryAllRecords("SELECT database_name as name,remote_ips as ip FROM web_database WHERE remote_ips != '' AND sys_groupid = ?", $groupid) : $results = $app->db->queryAllRecords("SELECT database_name as name,remote_ips as ip FROM web_database WHERE remote_ips != ''");
260+
235261
if(!empty($results) && is_array($results)){
236262
foreach($results as $result){
237-
$tmp_ips = explode(',', $result['remote_ips']);
263+
$tmp_ips = explode(',', $result['ip']);
238264
foreach($tmp_ips as $tmp_ip){
239265
$tmp_ip = trim($tmp_ip);
240-
if(preg_match($regex, $tmp_ip)) $ips[] = $tmp_ip;
266+
if (!array_key_exists($tmp_ip,$server_by_ip)) {
267+
$server_by_ip[$tmp_ip] = 'database: ' . $result['name'];
268+
if(preg_match($regex, $tmp_ip)) $ips[] = $tmp_ip;
269+
}
241270
}
242271
}
243272
}
244273
$ips = array_unique($ips);
274+
sort($localips, SORT_NUMERIC);
245275
sort($ips, SORT_NUMERIC);
246-
276+
$ips = array_merge($localips,$ips);
277+
$ips = array_slice($ips, 0, $suggestions_max);
247278
$result_array = array('cheader' => array(), 'cdata' => array());
248279

249280
if(!empty($ips)){
250281
$result_array['cheader'] = array('title' => 'IPs',
251282
'total' => count($ips),
252-
'limit' => count($ips)
283+
'limit' => count($ips),
253284
);
254285

255286
foreach($ips as $ip){
@@ -282,7 +313,7 @@ public function intval($string, $force_numeric = false) {
282313
*/
283314
public function formatBytes($size, $precision = 2) {
284315
$suffixes=array('', ' kB', ' MB', ' GB', ' TB');
285-
if($size != 0 && !is_nan($size)) {
316+
if($size != 0 && !is_nan($size)) {
286317
$base=log($size)/log(1024);
287318
$tmpoutput = round(pow(1024, $base-floor($base)), $precision).$suffixes[floor($base)];
288319
} else {
@@ -371,42 +402,42 @@ public function idn_decode($domain) {
371402

372403
public function is_allowed_user($username, $restrict_names = false) {
373404
global $app;
374-
405+
375406
$name_blacklist = array('root','ispconfig','vmail','getmail');
376407
if(in_array($username,$name_blacklist)) return false;
377-
408+
378409
if(preg_match('/^[a-zA-Z0-9\.\-_]{1,32}$/', $username) == false) return false;
379-
410+
380411
if($restrict_names == true && preg_match('/^web\d+$/', $username) == false) return false;
381-
412+
382413
return true;
383414
}
384-
415+
385416
public function is_allowed_group($groupname, $restrict_names = false) {
386417
global $app;
387-
418+
388419
$name_blacklist = array('root','ispconfig','vmail','getmail');
389420
if(in_array($groupname,$name_blacklist)) return false;
390-
421+
391422
if(preg_match('/^[a-zA-Z0-9\.\-_]{1,32}$/', $groupname) == false) return false;
392-
423+
393424
if($restrict_names == true && preg_match('/^client\d+$/', $groupname) == false) return false;
394-
425+
395426
return true;
396427
}
397-
428+
398429
public function getimagesizefromstring($string){
399430
if (!function_exists('getimagesizefromstring')) {
400431
$uri = 'data://application/octet-stream;base64,' . base64_encode($string);
401432
return getimagesize($uri);
402433
} else {
403434
return getimagesizefromstring($string);
404-
}
435+
}
405436
}
406-
437+
407438
public function password($minLength = 10, $special = false){
408439
global $app;
409-
440+
410441
$iteration = 0;
411442
$password = "";
412443
$maxLength = $minLength + 5;
@@ -431,21 +462,21 @@ public function password($minLength = 10, $special = false){
431462
public function getRandomInt($min, $max){
432463
return floor((mt_rand() / mt_getrandmax()) * ($max - $min + 1)) + $min;
433464
}
434-
465+
435466
public function generate_customer_no(){
436467
global $app;
437468
// generate customer no.
438469
$customer_no = mt_rand(100000, 999999);
439470
while($app->db->queryOneRecord("SELECT client_id FROM client WHERE customer_no = ?", $customer_no)) {
440471
$customer_no = mt_rand(100000, 999999);
441472
}
442-
473+
443474
return $customer_no;
444475
}
445-
476+
446477
public function generate_ssh_key($client_id, $username = ''){
447478
global $app;
448-
479+
449480
// generate the SSH key pair for the client
450481
$id_rsa_file = '/tmp/'.uniqid('',true);
451482
$id_rsa_pub_file = $id_rsa_file.'.pub';
@@ -459,7 +490,7 @@ public function generate_ssh_key($client_id, $username = ''){
459490
$app->log("Failed to create SSH keypair for ".$username, LOGLEVEL_WARN);
460491
}
461492
}
462-
493+
463494
public function htmlentities($value) {
464495
global $conf;
465496

@@ -475,7 +506,7 @@ public function htmlentities($value) {
475506
} else {
476507
$out = htmlentities($value, ENT_QUOTES, $conf["html_content_encoding"]);
477508
}
478-
509+
479510
return $out;
480511
}
481512
}

interface/web/admin/form/system_config.tform.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -602,6 +602,20 @@
602602
'default' => 'y',
603603
'value' => array(0 => 'n', 1 => 'y')
604604
),
605+
'use_ipsuggestions' => array (
606+
'datatype' => 'VARCHAR',
607+
'formtype' => 'CHECKBOX',
608+
'default' => 'y',
609+
'value' => array(0 => 'n', 1 => 'y')
610+
),
611+
'ipsuggestions_max' => array (
612+
'datatype' => 'INTEGER',
613+
'formtype' => 'TEXT',
614+
'default' => '',
615+
'value' => '',
616+
'width' => '30',
617+
'maxlength' => '255'
618+
),
605619
'maintenance_mode' => array (
606620
'datatype' => 'VARCHAR',
607621
'formtype' => 'CHECKBOX',

interface/web/admin/lib/lang/nl_system_config.lng

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ $wb['customer_no_template_error_regex_txt'] = 'The customer No. template contain
6868
$wb['customer_no_start_txt'] = 'Customer No. start value';
6969
$wb['customer_no_counter_txt'] = 'Customer No. counter';
7070
$wb['session_timeout_txt'] = 'Session timeout (minutes)';
71-
$wb['session_allow_endless_txt'] = 'Enable \\"stay logged in\\"';
71+
$wb['session_allow_endless_txt'] = 'Enable "stay logged in"';
7272
$wb['No'] = 'No';
7373
$wb['min_password_length_txt'] = 'Minimum password length';
7474
$wb['min_password_strength_txt'] = 'Minimum password strength';
@@ -91,4 +91,7 @@ $wb['active_txt'] = 'Aktive';
9191
$wb['btn_save_txt'] = 'Save';
9292
$wb['btn_cancel_txt'] = 'Cancel';
9393
$wb['asp_new_package_disabled_txt'] = 'Disable new aps packages';
94+
$wb['use_ipsuggestions_txt'] = 'IP adres suggesties inschakelen';
95+
$wb['use_ipsuggestions_descr_txt'] = 'Toont een dropdown box met een lijst van IP adressen voor IPv4 en IPv6 adressen.';
96+
$wb['ipsuggestions_max_txt'] = 'Max aantal IP suggesties';
9497
?>

interface/web/admin/templates/system_config_misc_edit.htm

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,15 @@ <h1><tmpl_var name="list_head_txt"></h1>
8181
</div>
8282
</div>
8383
<div class="form-group">
84+
<label class="col-sm-3 control-label">{tmpl_var name='use_ipsuggestions_txt'}</label>
85+
<div class="col-sm-9">
86+
{tmpl_var name='use_ipsuggestions'}<br/>{tmpl_var name='use_ipsuggestions_descr_txt'}
87+
</div>
88+
</div>
89+
<div class="form-group">
90+
<label for="ipsuggestions_max" class="col-sm-3 control-label">{tmpl_var name='ipsuggestions_max_txt'}</label>
91+
<div class="col-sm-9"><input type="text" name="ipsuggestions_max" id="ipsuggestions_max" value="{tmpl_var name='ipsuggestions_max'}" class="form-control" /></div></div>
92+
<div class="form-group">
8493
<label for="customer_no_template" class="col-sm-3 control-label">{tmpl_var name='customer_no_template_txt'}</label>
8594
<div class="col-sm-9"><input type="text" name="customer_no_template" id="customer_no_template" value="{tmpl_var name='customer_no_template'}" class="form-control" /></div></div>
8695
<div class="form-group">

interface/web/themes/default/assets/stylesheets/ispconfig.css

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -800,3 +800,6 @@ span.pbvaluemargin {
800800
margin-left: 5px;
801801
}
802802

803+
.ip_suggestion_server {
804+
font-weight: bold;
805+
}

0 commit comments

Comments
 (0)