modules/vhosts/save.php
8d92bc6d
 <?php
c208bd90
 /*
 This file belongs to the Webinterface of schokokeks.org Hosting
 
cf54502a
 Written 2008-2018 by schokokeks.org Hosting, namely
c208bd90
   Bernd Wurst <bernd@schokokeks.org>
   Hanno Böck <hanno@schokokeks.org>
 
 To the extent possible under law, the author(s) have dedicated all copyright and related and neighboring rights to this software to the public domain worldwide. This software is distributed without any warranty.
 
2626dd47
 You should have received a copy of the CC0 Public Domain Dedication along with this software. If not, see
c208bd90
 http://creativecommons.org/publicdomain/zero/1.0/
 
 Nevertheless, in case you use a significant part of this code, we ask (but not require, see the license) that you keep the authors' names in place and return your changes to the public. We would be especially happy if you tell us what you're going to do with this code.
 */
8d92bc6d
 
 require_once('session/start.php');
 
 require_once('vhosts.php');
 
0434646d
 require_once('inc/error.php');
8d92bc6d
 require_once('inc/security.php');
 require_once('class/domain.php');
 
 require_role(ROLE_SYSTEMUSER);
 
 require_once("inc/debug.php");
 global $debugmode;
 
 
2626dd47
 if ($_GET['action'] == 'edit') {
     check_form_token('vhosts_edit_vhost');
     $id = (int) $_GET['vhost'];
     $vhost = empty_vhost();
     if ($id != 0) {
         $vhost = get_vhost_details($id);
     }
     DEBUG($vhost);
 
     $hostname = filter_input_hostname($_POST['hostname'], true);
 
     $domainname = null;
     $domain_id = (int) $_POST['domain'];
     if ($domain_id >= 0) {
         $domain = new Domain((int) $_POST['domain']);
         $domain->ensure_userdomain();
         $domain_id = $domain->id;
         $domainname = $domain->fqdn;
     } elseif ($domain_id == -1) {
         # use configured user_vhosts_domain
         $userdomain = userdomain();
         $domain = new Domain((int) $userdomain['id']);
         $domain_id = $domain->id;
         $domainname = $domain->fqdn;
         $hostname = $hostname.'.'.$_SESSION['userinfo']['username'];
         $hostname = trim($hostname, " .-");
     } elseif ($domain_id == -2) {
         # use system masterdomain
         $domainname = $_SESSION['userinfo']['username'].".".config('masterdomain');
     }
8d92bc6d
 
2626dd47
     $aliaswww = (isset($_POST['aliaswww']) && $_POST['aliaswww'] == 'aliaswww');
     $forwardwww = null;
     if ($aliaswww && isset($_POST['forwardwww'])) {
         if ($_POST['forwardwww'] == 'forwardwww') {
             $forwardwww = 'forwardwww';
         } elseif ($_POST['forwardwww'] == 'forwardnowww') {
             $forwardwww = 'forwardnowww';
         }
50033c32
     }
8d92bc6d
 
da0e59cc
     $docroot = '';
2626dd47
     if ($_POST['vhost_type'] == 'regular' || $_POST['vhost_type'] == 'dav') {
         $defaultdocroot = $vhost['homedir'].'/websites/'.((strlen($hostname) > 0) ? $hostname.'.' : '').($domainname).'/htdocs';
9086c9ad
 
2626dd47
         $docroot = '';
         if (isset($_POST['docroot'])) {
             if (! check_path($_POST['docroot'])) {
                 system_failure("Eingegebener Pfad enthält ungültige Angaben");
             }
             $docroot = $vhost['homedir'].'/websites/'.$_POST['docroot'];
         }
         if ((isset($_POST['use_default_docroot']) && $_POST['use_default_docroot'] == '1') || ($docroot == $defaultdocroot)) {
             $docroot = null;
         }
9086c9ad
 
2626dd47
         DEBUG("Document-Root: ".$docroot);
     }
     $php = null;
     if ($_POST['vhost_type'] == 'regular' && isset($_POST['php'])) {
4b53992a
         $phpinfo = valid_php_versions();
         if (array_key_exists($_POST['php'], $phpinfo)) {
             $php = $_POST['php'];
         } else {
             $php = null;
         }
2626dd47
     }
     $cgi = 1;
     if (isset($_POST['safemode']) && $_POST['safemode'] == 'yes') {
         $cgi = 0;
     }
926f25db
 
2626dd47
     if (isset($_POST['suexec_user'])) {
         $vhost['suexec_user'] = $_POST['suexec_user'];
     }
8160df9c
 
2626dd47
     if (isset($_POST['server'])) {
         $vhost['server'] = $_POST['server'];
     }
5231fb2f
 
2626dd47
     if ($_POST['vhost_type'] == 'regular') {
         $vhost['is_dav'] = 0;
         $vhost['is_svn'] = 0;
         $vhost['is_webapp'] = 0;
     } elseif ($_POST['vhost_type'] == 'dav') {
         $vhost['is_dav'] = 1;
         $vhost['is_svn'] = 0;
         $vhost['is_webapp'] = 0;
     } elseif ($_POST['vhost_type'] == 'svn') {
         $vhost['is_dav'] = 0;
         $vhost['is_svn'] = 1;
         $vhost['is_webapp'] = 0;
     } elseif ($_POST['vhost_type'] == 'webapp') {
         $vhost['is_dav'] = 0;
         $vhost['is_svn'] = 0;
         $vhost['is_webapp'] = 1;
         $vhost['webapp_id'] = (int) $_POST['webapp'];
     }
8d92bc6d
 
9086c9ad
 
2626dd47
     $ssl = null;
     switch ($_POST['ssl']) {
fc35e698
     case 'http':
       $ssl = 'http';
       break;
     case 'https':
       $ssl = 'https';
       break;
     case 'forward':
       $ssl = 'forward';
       break;
     /* Wenn etwas anderes kommt, ist das "beides". So einfach ist das. */
   }
 
2626dd47
     $hsts = null;
     $hsts_subdomains = false;
     $hsts_preload = false;
     if (isset($_POST['hsts'])) {
         if (is_numeric($_POST['hsts']) && (int) $_POST['hsts'] > -2) {
             $hsts = (int) $_POST['hsts'];
         } else {
             system_failure('Es wurde ein ungültiger HSTS-Wert eingegeben. Dort sind nur Sekunden erlaubt.');
         }
         if (isset($_POST['hsts_subdomains']) and $_POST['hsts_subdomains'] == 1) {
             $hsts_subdomains = true;
             if (isset($_POST['hsts_preload']) and $_POST['hsts_preload'] == 1) {
                 $hsts_preload = true;
             }
         }
c5d2cce4
     }
9086c9ad
 
2626dd47
     $cert = (isset($_POST['cert']) ? (int) $_POST['cert'] : null);
1ab9db55
 
2626dd47
     $ipv4 = (isset($_POST['ipv4']) ? $_POST['ipv4'] : null);
918446fe
 
2626dd47
     if (isset($_POST['ipv6']) && $_POST['ipv6'] == 'yes') {
         $vhost['autoipv6'] = 1;
         if (isset($_POST['ipv6_separate']) && $_POST['ipv6_separate'] = 'yes') {
             $vhost['autoipv6'] = 2;
         }
     } else {
         $vhost['autoipv6'] = 0;
6c54f23e
     }
9086c9ad
 
56c4c2ac
 
2626dd47
     $logtype = '';
     switch ($_POST['logtype']) {
8d92bc6d
     case 'anonymous':
       $logtype = 'anonymous';
       break;
     case 'default':
       $logtype = 'default';
       break;
     /* Wenn etwas anderes kommt, ist das "kein Logging". So einfach ist das. */
   }
 
2626dd47
     $errorlog = 0;
     if (isset($_POST['errorlog']) and ($_POST['errorlog'] == 1)) {
         $errorlog = 1;
     }
2b282fad
 
56c4c2ac
 
2626dd47
     if (isset($_POST['stats']) && $_POST['stats'] == 1) {
         if ($vhost['stats'] == null) {
             $vhost['stats'] = 'private';
         }
     } else {
         $vhost['stats'] = null;
     }
56c4c2ac
 
2626dd47
     if ($logtype == '') {
         $vhost['stats'] = null;
     }
9086c9ad
 
2626dd47
     DEBUG("PHP: {$php} / Logging: {$logtype}");
 
     $old_options = explode(',', $vhost['options']);
     $new_options = array();
     foreach ($old_options as $op) {
         if (! in_array($op, array('aliaswww', 'forwardwww', 'forwardnowww', 'hsts_subdomains', 'hsts_preload'))) {
             array_push($new_options, $op);
         }
eaac3bfa
     }
2626dd47
     if ($aliaswww) {
         array_push($new_options, 'aliaswww');
         if ($forwardwww) {
             array_push($new_options, $forwardwww);
         }
50033c32
     }
2626dd47
     if ($hsts_subdomains) {
         array_push($new_options, 'hsts_subdomains');
     }
     if ($hsts_preload) {
         array_push($new_options, 'hsts_preload');
     }
     $letsencrypt = ($cert == 0 ? false : ($cert == -1 || cert_is_letsencrypt($cert)));
 
     if ($letsencrypt) {
         array_push($new_options, 'letsencrypt');
         if ($vhost['cert'] != 0) {
             # Wenn der User manuell von einem gültigen Cert auf "letsencrypt" umgestellt hat,
             # dann sollte das alte Cert noch so lange eingetragen bleiben bis das neue da ist.
             $cert = $vhost['cert'];
         } elseif ($cert > 0) {
             # Das Cert was der user gewählt hat, ist von Lets encrypt
bc103625
       # tu nix, $cert ist schon korrekt
2626dd47
         } else {
             # Wenn vorher kein Zertifikat aktiv war, dann setze jetzt auch keines.
             # Der letsencrypt-Automatismus macht das dann schon.
             $cert = 0;
         }
bc103625
     } else {
2626dd47
         # Wenn kein Letsencrypt gewünscht ist, entferne die Letsencrypt-Option
         $key = array_search('letsencrypt', $new_options);
         if ($key !== false) {
             unset($new_options[$key]);
         }
bc103625
     }
8d92bc6d
 
2626dd47
     DEBUG($old_options);
     DEBUG($new_options);
     $options = implode(',', $new_options);
     DEBUG('New options: '.$options);
 
     $vhost['hostname'] = $hostname;
     $vhost['domain_id'] = $domain_id;
     $vhost['docroot'] = $docroot;
     $vhost['php'] = $php;
     $vhost['cgi'] = $cgi;
     $vhost['ssl'] = $ssl;
     $vhost['hsts'] = $hsts;
     $vhost['cert'] = $cert;
     $vhost['ipv4'] = $ipv4;
     $vhost['logtype'] = $logtype;
     $vhost['errorlog'] = $errorlog;
     $vhost['options'] = $options;
9086c9ad
 
2626dd47
     DEBUG($vhost);
     save_vhost($vhost);
     success_msg("Ihre Einstellungen wurden gespeichert. Es dauert jedoch einige Minuten bis die Änderungen wirksam werden.");
8d92bc6d
 
2626dd47
     if (! $debugmode) {
         header('Location: vhosts');
     }
 } elseif ($_GET['action'] == 'addalias') {
     check_form_token('vhosts_add_alias');
     $id = (int) $_GET['vhost'];
     $vhost = get_vhost_details($id);
     DEBUG($vhost);
709c9ea2
 
2626dd47
     $alias = empty_alias();
     $alias['vhost'] = $vhost['id'];
709c9ea2
 
9086c9ad
 
2626dd47
     $hostname = filter_input_hostname($_POST['hostname'], true);
     $domainid = (int) $_POST['domain'];
     if ($domainid >= 0) {
         $domain = new Domain((int) $_POST['domain']);
         $domain->ensure_userdomain();
         $domainid = $domain->id;
     }
     if ($domainid == -1) {
         # use configured user_vhosts_domain
         $userdomain = userdomain();
         $domain = new Domain((int) $userdomain['id']);
         $domainid = $domain->id;
         $hostname = $hostname.'.'.$_SESSION['userinfo']['username'];
         $hostname = trim($hostname, " .-");
     }
 
     if (! is_array($_POST['options'])) {
         $_POST['options'] = array();
     }
     $aliaswww = in_array('aliaswww', $_POST['options']);
     $forward = in_array('forward', $_POST['options']);
709c9ea2
 
2626dd47
     $new_options = array();
     if ($aliaswww) {
         array_push($new_options, 'aliaswww');
     }
     if ($forward) {
         array_push($new_options, 'forward');
     }
     DEBUG($new_options);
     $options = implode(',', $new_options);
     DEBUG('New options: '.$options);
 
     $alias['hostname'] = $hostname;
     $alias['domain_id'] = $domainid;
9086c9ad
 
2626dd47
     $alias ['options'] = $options;
9086c9ad
 
2626dd47
     save_alias($alias);
709c9ea2
 
2626dd47
     if (! $debugmode) {
         header('Location: aliases?vhost='.$vhost['id']);
     }
 } elseif ($_GET['action'] == 'deletealias') {
     $title = "Subdomain löschen";
     $section = 'vhosts_vhosts';
9086c9ad
 
2626dd47
     $alias = get_alias_details((int) $_GET['alias']);
     DEBUG($alias);
     $alias_string = $alias['fqdn'];
9086c9ad
 
2626dd47
     $vhost = get_vhost_details($alias['vhost']);
     DEBUG($vhost);
     $vhost_string = $vhost['fqdn'];
9086c9ad
 
2626dd47
     $sure = user_is_sure();
     if ($sure === null) {
         are_you_sure("action=deletealias&alias={$_GET['alias']}", "Möchten Sie das Alias »{$alias_string}« für die Subdomain »{$vhost_string}« wirklich löschen?");
     } elseif ($sure === true) {
         delete_alias($alias['id']);
         if (! $debugmode) {
             header('Location: aliases?vhost='.$vhost['id']);
         }
     } elseif ($sure === false) {
         if (! $debugmode) {
             header('Location: aliases?vhost='.$vhost['id']);
         }
     }
 } elseif ($_GET['action'] == 'delete') {
     $title = "Subdomain löschen";
     $section = 'vhosts_vhosts';
9086c9ad
 
2626dd47
     $vhost = get_vhost_details((int) $_GET['vhost']);
     $vhost_string = $vhost['fqdn'];
9086c9ad
 
2626dd47
     $sure = user_is_sure();
     if ($sure === null) {
         are_you_sure("action=delete&vhost={$_GET['vhost']}", "Möchten Sie die Subdomain »{$vhost_string}« wirklich löschen?");
     } elseif ($sure === true) {
         delete_vhost($vhost['id']);
         if (! $debugmode) {
             header("Location: vhosts");
         }
     } elseif ($sure === false) {
         if (! $debugmode) {
             header("Location: vhosts");
         }
     }
 } else {
     system_failure("Unimplemented action");
8d92bc6d
 }
 
 output('');