prevent duplicate vhost hostnames also when changing the name of a configuration
Bernd Wurst

Bernd Wurst commited on 2022-02-17 19:41:57
Zeige 1 geänderte Dateien mit 10 Einfügungen und 2 Löschungen.

... ...
@@ -365,7 +365,7 @@ function make_webapp_vhost($id, $webapp)
365 365
 }
366 366
 
367 367
 
368
-function check_hostname_collision($hostname, $domain)
368
+function check_hostname_collision($hostname, $domain, $id=NULL)
369 369
 {
370 370
     $uid = (int) $_SESSION['userinfo']['uid'];
371 371
     # Neuer vhost => Prüfe Duplikat
... ...
@@ -387,7 +387,12 @@ function check_hostname_collision($hostname, $domain)
387 387
         $hostnamecheck = "hostname IS NULL";
388 388
         unset($args[":hostname"]);
389 389
     }
390
-    $result = db_query("SELECT id FROM vhosts.vhost WHERE {$hostnamecheck} AND {$domaincheck} AND user=:uid", $args);
390
+    $idcheck = '';
391
+    if ($id) {
392
+        $idcheck = ' AND id != :id';
393
+        $args[':id'] = (int) $id;
394
+    }
395
+    $result = db_query("SELECT id FROM vhosts.vhost WHERE {$hostnamecheck} AND {$domaincheck} AND user=:uid ".$idcheck, $args);
391 396
     if ($result->rowCount() > 0) {
392 397
         system_failure('Eine Konfiguration mit diesem Namen gibt es bereits.');
393 398
     }
... ...
@@ -395,6 +400,7 @@ function check_hostname_collision($hostname, $domain)
395 400
         return ;
396 401
     }
397 402
     unset($args[":uid"]);
403
+    unset($args[":id"]);
398 404
     $result = db_query("SELECT id, vhost FROM vhosts.v_alias WHERE {$hostnamecheck} AND {$domaincheck}", $args);
399 405
     if ($result->rowCount() > 0) {
400 406
         $data = $result->fetch();
... ...
@@ -419,6 +425,8 @@ function save_vhost($vhost)
419 425
     }
420 426
     if ($id == 0) {
421 427
         check_hostname_collision($vhost['hostname'], $vhost['domain_id']);
428
+    } else {
429
+        check_hostname_collision($vhost['hostname'], $vhost['domain_id'], $id);
422 430
     }
423 431
     $hsts = (int) $vhost['hsts'];
424 432
     if ($hsts < 0) {
425 433