@@ -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}
0 commit comments