Ermögliche dem user Hosts mit der Domain aus user_vhosts_domain
Bernd Wurst

Bernd Wurst commited on 2014-12-17 08:35:33
Zeige 3 geänderte Dateien mit 66 Einfügungen und 9 Löschungen.

... ...
@@ -46,8 +46,12 @@ else {
46 46
 }
47 47
 
48 48
 $defaultdocroot = $vhost['domain'];
49
-if (! $vhost['domain'])
49
+if (! $vhost['domain']) {
50 50
   $defaultdocroot = $_SESSION['userinfo']['username'].'.'.config('masterdomain');
51
+}
52
+if ($vhost['domain_id'] == -1) {
53
+  $defaultdocroot = $_SESSION['userinfo']['username'].'.'.config('user_vhosts_domain');
54
+}
51 55
 if ($vhost['hostname'])
52 56
   $defaultdocroot = $vhost['hostname'].'.'.$defaultdocroot;
53 57
 
... ...
@@ -80,8 +80,8 @@ function empty_vhost()
80 80
   $vhost['id'] = NULL;
81 81
   $vhost['hostname'] = NULL;
82 82
   
83
-  $vhost['domain_id'] = -1;
84
-  $vhost['domain'] = $_SESSION['userinfo']['username'].'.'.config('masterdomain');
83
+  $vhost['domain_id'] = NULL;
84
+  $vhost['domain'] = NULL;
85 85
   
86 86
   $vhost['homedir'] = $_SESSION['userinfo']['homedir'];
87 87
   $vhost['docroot'] = NULL;
... ...
@@ -121,6 +121,16 @@ function empty_alias()
121 121
 }
122 122
 
123 123
 
124
+function userdomain() {
125
+  if (config('user_vhosts_domain') === NULL) {
126
+    return NULL;
127
+  }
128
+  $result = db_query("SELECT id,name FROM vhosts.v_domains WHERE name=:dom", array(":dom" => config('user_vhosts_domain')));
129
+  $res = $result->fetch();
130
+  return $res;
131
+}
132
+
133
+
124 134
 function domainselect($selected = NULL, $selectattribute = '')
125 135
 {
126 136
   global $domainlist, $config;
... ...
@@ -130,8 +140,6 @@ function domainselect($selected = NULL, $selectattribute = '')
130 140
   $selected = (int) $selected;
131 141
 
132 142
   $ret = '<select id="domain" name="domain" size="1" '.$selectattribute.' >';
133
-  $ret .= ' <option value="-1">'.$_SESSION['userinfo']['username'].'.'.config('masterdomain').'</option>';
134
-  $ret .= ' <option value="" disabled="disabled">--------------------------------</option>';
135 143
   $found = false;
136 144
   foreach ($domainlist as $dom)
137 145
   {
... ...
@@ -142,6 +150,16 @@ function domainselect($selected = NULL, $selectattribute = '')
142 150
     }
143 151
     $ret .= "<option value=\"{$dom->id}\"{$s}>{$dom->fqdn}</option>\n";
144 152
   }
153
+  if (count($domainlist) > 0) {
154
+    $ret .= ' <option value="" disabled="disabled">--------------------------------</option>';
155
+  }
156
+  $userdomain = userdomain();
157
+  if ($userdomain) {
158
+    $s = ($selected == -1 ? ' selected="selected"' : '');
159
+    $ret .= ' <option value="-1"'.$s.'>'.$_SESSION['userinfo']['username'].'.'.$userdomain['name'].'</option>';
160
+  }
161
+  $s = ($selected == -2 ? ' selected="selected"' : '');
162
+  $ret .= ' <option value="-2"'.$s.'>'.$_SESSION['userinfo']['username'].'.'.config('masterdomain').'</option>';
145 163
   $ret .= '</select>';
146 164
   if ($selected > 0 and ! $found) {
147 165
     system_failure("Hier wird eine Domain benutzt, die nicht zu diesem Benutzeraccount gehört. Bearbeiten würde Daten zerstören!");
... ...
@@ -161,6 +179,21 @@ function get_vhost_details($id)
161 179
 
162 180
   $ret = $result->fetch();
163 181
 
182
+  if ($ret['domain_id'] === NULL) {
183
+    $ret['domain_id'] = -2;
184
+  }
185
+  $userdomain = userdomain();
186
+  if ($ret['domain_id'] == $userdomain['id']) {
187
+    $user = $_SESSION['userinfo']['username'];
188
+    $ret['domain_id'] = -1;
189
+    if ($ret['hostname'] == $user) {
190
+      $ret['hostname'] = NULL;
191
+    } elseif (substr($ret['hostname'], -count($user), count($user)) == $user) {
192
+      $ret['hostname'] = substr($ret['hostname'], 0, -count($user)-1); // Punkt mit entfernen!
193
+    } else {
194
+      system_failure('Userdomain ohne Username!');
195
+    }
196
+  }
164 197
   if ($ret['hsts'] === NULL) {
165 198
     DEBUG('HSTS: '.$ret['hsts']);
166 199
     $ret['hsts'] = -1;
... ...
@@ -282,6 +315,10 @@ function check_hostname_collision($hostname, $domain)
282 315
   }
283 316
   $domaincheck = "domain=:domain";
284 317
   if ($domain == -1) {
318
+    $args[":uid"] = $uid;
319
+    $domaincheck = "domain=-1 AND user=:uid";
320
+  }
321
+  if ($domain == -2) {
285 322
     $args[":uid"] = $uid;
286 323
     unset($args[":domain"]);
287 324
     $domaincheck = "domain IS NULL AND user=:uid";
... ...
@@ -310,7 +347,7 @@ function save_vhost($vhost)
310 347
   $domain = (int) $vhost['domain_id'];
311 348
   if ($domain == 0)
312 349
     system_failure('$domain == 0');
313
-  if ($vhost['domain_id'] == -1)
350
+  if ($vhost['domain_id'] == -2)
314 351
     $domain = NULL;
315 352
   if ($id == 0) {
316 353
     check_hostname_collision($vhost['hostname'], $vhost['domain_id']);
... ...
@@ -450,7 +487,7 @@ function save_alias($alias)
450 487
   $domain = (int) $alias['domain_id'];
451 488
   if ($domain == 0)
452 489
     system_failure('$domain == 0');
453
-  if ($alias['domain_id'] == -1)
490
+  if ($alias['domain_id'] == -2)
454 491
     $domain = NULL;
455 492
   $vhost = get_vhost_details( (int) $alias['vhost']);
456 493
   if (! $alias['hostname']) {
... ...
@@ -40,11 +40,19 @@ if ($_GET['action'] == 'edit')
40 40
   $hostname = filter_input_hostname($_POST['hostname'], true);
41 41
 
42 42
   $domain_id = (int) $_POST['domain'];
43
-  if ($domain_id != -1) {
43
+  if ($domain_id >= 0) {
44 44
     $domain = new Domain( (int) $_POST['domain'] );
45 45
     $domain->ensure_userdomain();
46 46
     $domain_id = $domain->id;
47 47
   }
48
+  if ($domain_id == -1) {
49
+    # use configured user_vhosts_domain
50
+    $userdomain = userdomain();
51
+    $domain = new Domain( (int) $userdomain['id'] );
52
+    $domain_id = $domain->id;
53
+    $hostname = $hostname.'.'.$_SESSION['userinfo']['username'];
54
+    $hostname = trim($hostname, " .-");
55
+  }
48 56
 
49 57
   if (! (isset($_POST['options']) && is_array($_POST['options'])))
50 58
     $_POST['options'] = array();
... ...
@@ -236,11 +244,19 @@ elseif ($_GET['action'] == 'addalias')
236 244
   
237 245
   $hostname = filter_input_hostname($_POST['hostname'], true);
238 246
   $domainid = (int) $_POST['domain'];
239
-  if ($domainid != -1) {
247
+  if ($domainid >= 0) {
240 248
     $domain = new Domain( (int) $_POST['domain'] );
241 249
     $domain->ensure_userdomain();
242 250
     $domainid = $domain->id;
243 251
   }
252
+  if ($domainid == -1) {
253
+    # use configured user_vhosts_domain
254
+    $userdomain = userdomain();
255
+    $domain = new Domain( (int) $userdomain['id'] );
256
+    $domainid = $domain->id;
257
+    $hostname = $hostname.'.'.$_SESSION['userinfo']['username'];
258
+    $hostname = trim($hostname, " .-");
259
+  }
244 260
 
245 261
   if (! is_array($_POST['options']))
246 262
     $_POST['options'] = array();
247 263