Marius Cramer
2014-04-23 9b3069a1eacd3cbda0bfe565925e80f667f6c96d
server/plugins-available/nginx_plugin.inc.php
@@ -102,7 +102,7 @@
         $app->log("CA path error, file does not exist:".$web_config['CA_path'].'/openssl.cnf', LOGLEVEL_ERROR);
      //* Only vhosts can have a ssl cert
      if($data["new"]["type"] != "vhost" && $data["new"]["type"] != "vhostsubdomain") return;
      if($data["new"]["type"] != "vhost" && $data["new"]["type"] != "vhostsubdomain" && $data["new"]["type"] != "vhostalias") return;
      // if(!is_dir($data['new']['document_root'].'/ssl')) exec('mkdir -p '.$data['new']['document_root'].'/ssl');
      if(!is_dir($data['new']['document_root'].'/ssl')) $app->system->mkdirpath($data['new']['document_root'].'/ssl');
@@ -314,7 +314,7 @@
      if($this->action != 'insert') $this->action = 'update';
      if($data['new']['type'] != 'vhost' && $data['new']['type'] != 'vhostsubdomain' && $data['new']['parent_domain_id'] > 0) {
      if($data['new']['type'] != 'vhost' && $data['new']['type'] != 'vhostsubdomain' && $data['new']['type'] != 'vhostalias' && $data['new']['parent_domain_id'] > 0) {
         $old_parent_domain_id = intval($data['old']['parent_domain_id']);
         $new_parent_domain_id = intval($data['new']['parent_domain_id']);
@@ -348,7 +348,7 @@
      }
      if($data['new']['document_root'] == '') {
         if($data['new']['type'] == 'vhost' || $data['new']['type'] == 'vhostsubdomain') $app->log('document_root not set', LOGLEVEL_WARN);
         if($data['new']['type'] == 'vhost' || $data['new']['type'] == 'vhostsubdomain' || $data['new']['type'] == 'vhostalias') $app->log('document_root not set', LOGLEVEL_WARN);
         return 0;
      }
      if($data['new']['system_user'] == 'root' or $data['new']['system_group'] == 'root') {
@@ -362,13 +362,26 @@
      $web_folder = 'web';
      $log_folder = 'log';
      if($data['new']['type'] == 'vhostsubdomain') {
      $old_web_folder = 'web';
      $old_log_folder = 'log';
      if($data['new']['type'] == 'vhostsubdomain' || $data['new']['type'] == 'vhostalias') {
         // new one
         $tmp = $app->db->queryOneRecord('SELECT `domain` FROM web_domain WHERE domain_id = '.intval($data['new']['parent_domain_id']));
         $subdomain_host = preg_replace('/^(.*)\.' . preg_quote($tmp['domain'], '/') . '$/', '$1', $data['new']['domain']);
         if($subdomain_host == '') $subdomain_host = 'web'.$data['new']['domain_id'];
         $web_folder = $data['new']['web_folder'];
         $log_folder .= '/' . $subdomain_host;
         unset($tmp);
         if(isset($data['old']['parent_domain_id'])) {
            // old one
            $tmp = $app->db->queryOneRecord('SELECT `domain` FROM web_domain WHERE domain_id = '.intval($data['old']['parent_domain_id']));
            $subdomain_host = preg_replace('/^(.*)\.' . preg_quote($tmp['domain'], '/') . '$/', '$1', $data['old']['domain']);
            if($subdomain_host == '') $subdomain_host = 'web'.$data['old']['domain_id'];
            $old_web_folder = $data['old']['web_folder'];
            $old_log_folder .= '/' . $subdomain_host;
            unset($tmp);
         }
      }
      // Create group and user, if not exist
@@ -436,7 +449,7 @@
            }
         }
         if($data["new"]["type"] != "vhostsubdomain") {
         if($data["new"]["type"] != "vhostsubdomain" && $data["new"]["type"] != "vhostalias") {
            //* Move the site data
            $tmp_docroot = explode('/', $data['new']['document_root']);
            unset($tmp_docroot[count($tmp_docroot)-1]);
@@ -477,9 +490,9 @@
         if($nginx_chrooted) $this->_exec('chroot '.escapeshellcmd($web_config['website_basedir']).' '.$command);
         //* Change the log mount
         $fstab_line = '/var/log/ispconfig/httpd/'.$data['old']['domain'].' '.$data['old']['document_root'].'/'.$log_folder.'    none    bind';
         $fstab_line = '/var/log/ispconfig/httpd/'.$data['old']['domain'].' '.$data['old']['document_root'].'/'.$old_log_folder.'    none    bind';
         $app->system->removeLine('/etc/fstab', $fstab_line);
         $fstab_line = '/var/log/ispconfig/httpd/'.$data['old']['domain'].' '.$data['old']['document_root'].'/'.$log_folder.'    none    bind,nobootwait';
         $fstab_line = '/var/log/ispconfig/httpd/'.$data['old']['domain'].' '.$data['old']['document_root'].'/'.$old_log_folder.'    none    bind,nobootwait';
         $app->system->removeLine('/etc/fstab', $fstab_line);
         $fstab_line = '/var/log/ispconfig/httpd/'.$data['new']['domain'].' '.$data['new']['document_root'].'/'.$log_folder.'    none    bind,nobootwait,_netdev    0 0';
         $app->system->replaceLine('/etc/fstab', $fstab_line, $fstab_line, 1, 1);
@@ -511,14 +524,14 @@
      // Remove the symlink for the site, if site is renamed
      if($this->action == 'update' && $data['old']['domain'] != '' && $data['new']['domain'] != $data['old']['domain']) {
         if(is_dir('/var/log/ispconfig/httpd/'.$data['old']['domain'])) exec('rm -rf /var/log/ispconfig/httpd/'.$data['old']['domain']);
         if(is_link($data['old']['document_root'].'/'.$log_folder)) $app->system->unlink($data['old']['document_root'].'/'.$log_folder);
         if(is_link($data['old']['document_root'].'/'.$old_log_folder)) $app->system->unlink($data['old']['document_root'].'/'.$old_log_folder);
         //* remove old log mount
         $fstab_line = '/var/log/ispconfig/httpd/'.$data['old']['domain'].' '.$data['old']['document_root'].'/'.$log_folder.'    none    bind';
         $fstab_line = '/var/log/ispconfig/httpd/'.$data['old']['domain'].' '.$data['old']['document_root'].'/'.$old_log_folder.'    none    bind';
         $app->system->removeLine('/etc/fstab', $fstab_line);
         //* Unmount log directory
         exec('umount '.escapeshellarg($data['old']['document_root'].'/'.$log_folder));
         exec('umount '.escapeshellarg($data['old']['document_root'].'/'.$old_log_folder));
      }
      //* Create the log dir if nescessary and mount it
@@ -594,7 +607,7 @@
      // setting a local var here
      // normally $conf['templates'] = "/usr/local/ispconfig/server/conf";
      if($this->action == 'insert' && ($data['new']['type'] == 'vhost' || $data['new']['type'] == 'vhostsubdomain')) {
      if($this->action == 'insert' && ($data['new']['type'] == 'vhost' || $data['new']['type'] == 'vhostsubdomain' || $data['new']['type'] == 'vhostalias')) {
         // Copy the error pages
         if($data['new']['errordocs']) {
@@ -640,7 +653,7 @@
         exec('chmod -R a+r '.escapeshellcmd($data['new']['document_root']).'/' . $web_folder . '/');
         //** Copy the error documents on update when the error document checkbox has been activated and was deactivated before
      } elseif ($this->action == 'update' && ($data['new']['type'] == 'vhost' || $data['new']['type'] == 'vhostsubdomain') && $data['old']['errordocs'] == 0 && $data['new']['errordocs'] == 1) {
      } elseif ($this->action == 'update' && ($data['new']['type'] == 'vhost' || $data['new']['type'] == 'vhostsubdomain' || $data['new']['type'] == 'vhostalias') && $data['old']['errordocs'] == 0 && $data['new']['errordocs'] == 1) {
         $error_page_path = escapeshellcmd($data['new']['document_root']).'/' . $web_folder . '/error/';
         if (file_exists($conf['rootpath'] . '/conf-custom/error/'.substr(escapeshellcmd($conf['language']), 0, 2))) {
@@ -658,7 +671,7 @@
         exec('chown -R '.$data['new']['system_user'].':'.$data['new']['system_group'].' '.$error_page_path);
      }  // end copy error docs
      // Set the quota for the user, but only for vhosts, not vhostsubdomains
      // Set the quota for the user, but only for vhosts, not vhostsubdomains or vhostalias
      if($username != '' && $app->system->is_user($username) && $data['new']['type'] == 'vhost') {
         if($data['new']['hd_quota'] > 0) {
            $blocks_soft = $data['new']['hd_quota'] * 1024;
@@ -790,7 +803,9 @@
            //$app->system->chown($data['new']['document_root'].'/webdav',$username);
            //$app->system->chgrp($data['new']['document_root'].'/webdav',$groupname);
         }
      } elseif(($this->action == 'insert' && $data['new']['type'] == 'vhostsubdomain') or ($web_config['set_folder_permissions_on_update'] == 'y' && $data['new']['type'] == 'vhostsubdomain')) {
      } elseif((($data['new']['type'] == 'vhostsubdomain') || ($data['new']['type'] == 'vhostalias')) &&
             (($this->action == 'insert') || ($web_config['set_folder_permissions_on_update'] == 'y'))) {
         if($web_config['security_level'] == 20) {
            $app->system->chmod($data['new']['document_root'].'/' . $web_folder, 0710);
            $app->system->chown($data['new']['document_root'].'/' . $web_folder, $username);
@@ -1307,7 +1322,7 @@
      }
      // get alias domains (co-domains and subdomains)
      $aliases = $app->db->queryAllRecords('SELECT * FROM web_domain WHERE parent_domain_id = '.$data['new']['domain_id']." AND active = 'y' AND type != 'vhostsubdomain'");
      $aliases = $app->db->queryAllRecords('SELECT * FROM web_domain WHERE parent_domain_id = '.$data['new']['domain_id']." AND active = 'y' AND (type != 'vhostsubdomain' AND type != 'vhostalias')");
      $alias_seo_redirects = array();
      if(is_array($aliases)) {
         foreach($aliases as $alias) {
@@ -1624,7 +1639,7 @@
      }
      //* Create awstats configuration
      if($data['new']['stats_type'] == 'awstats' && ($data['new']['type'] == 'vhost' || $data['new']['type'] == 'vhostsubdomain')) {
      if($data['new']['stats_type'] == 'awstats' && ($data['new']['type'] == 'vhost' || $data['new']['type'] == 'vhostsubdomain' || $data['new']['type'] == 'vhostalias')) {
         $this->awstats_update($data, $web_config);
      }
@@ -1742,7 +1757,7 @@
      $app->uses('system');
      $web_config = $app->getconf->get_server_config($conf['server_id'], 'web');
      if($data['old']['type'] == 'vhost' || $data['old']['type'] == 'vhostsubdomain') $app->system->web_folder_protection($data['old']['document_root'], false);
      if($data['old']['type'] == 'vhost' || $data['old']['type'] == 'vhostsubdomain' || $data['old']['type'] == 'vhostalias') $app->system->web_folder_protection($data['old']['document_root'], false);
      //* Check if this is a chrooted setup
      if($web_config['website_basedir'] != '' && @is_file($web_config['website_basedir'].'/etc/passwd')) {
@@ -1754,7 +1769,7 @@
      //* Remove the mounts
      $log_folder = 'log';
      $web_folder = '';
      if($data['old']['type'] == 'vhostsubdomain') {
      if($data['old']['type'] == 'vhostsubdomain' || $data['old']['type'] == 'vhostalias') {
         $tmp = $app->db->queryOneRecord('SELECT `domain`,`document_root` FROM web_domain WHERE domain_id = '.intval($data['old']['parent_domain_id']));
         if($tmp['domain'] != ''){
            $subdomain_host = preg_replace('/^(.*)\.' . preg_quote($tmp['domain'], '/') . '$/', '$1', $data['old']['domain']);
@@ -1800,7 +1815,7 @@
         unset($subdomain_hosts);
      }
      if($data['old']['type'] == 'vhost' || $data['old']['type'] == 'vhostsubdomain'){
      if($data['old']['type'] == 'vhost' || $data['old']['type'] == 'vhostsubdomain' || $data['old']['type'] == 'vhostalias'){
         if(is_array($log_folders) && !empty($log_folders)){
            foreach($log_folders as $log_folder){
               //if($app->system->is_mounted($data['old']['document_root'].'/'.$log_folder)) exec('umount '.escapeshellarg($data['old']['document_root'].'/'.$log_folder));
@@ -1824,7 +1839,7 @@
      }
      unset($log_folders);
      if($data['old']['type'] != 'vhost' && $data['old']['type'] != 'vhostsubdomain' && $data['old']['parent_domain_id'] > 0) {
      if($data['old']['type'] != 'vhost' && $data['old']['type'] != 'vhostsubdomain' && $data['old']['type'] != 'vhostalias' && $data['old']['parent_domain_id'] > 0) {
         //* This is a alias domain or subdomain, so we have to update the website instead
         $parent_domain_id = intval($data['old']['parent_domain_id']);
         $tmp = $app->db->queryOneRecord('SELECT * FROM web_domain WHERE domain_id = '.$parent_domain_id." AND active = 'y'");
@@ -1858,7 +1873,7 @@
         $app->system->unlink($vhost_file);
         $app->log('Removing vhost file: '.$vhost_file, LOGLEVEL_DEBUG);
         if($data['old']['type'] == 'vhost' || $data['old']['type'] == 'vhostsubdomain') {
         if($data['old']['type'] == 'vhost' || $data['old']['type'] == 'vhostsubdomain' || $data['old']['type'] == 'vhostalias') {
            $docroot = escapeshellcmd($data['old']['document_root']);
            if($docroot != '' && !stristr($docroot, '..')) {
               if($data['old']['type'] == 'vhost') {
@@ -1881,7 +1896,7 @@
                  } else {
                     // read all vhost subdomains with same parent domain
                     $used_paths = array();
                     $tmp = $app->db->queryAllRecords("SELECT `web_folder` FROM web_domain WHERE type = 'vhostsubdomain' AND parent_domain_id = ".intval($data['old']['parent_domain_id'])." AND domain_id != ".intval($data['old']['domain_id']));
                     $tmp = $app->db->queryAllRecords("SELECT `web_folder` FROM web_domain WHERE (type = 'vhostsubdomain' OR type = 'vhostalias') AND parent_domain_id = ".intval($data['old']['parent_domain_id'])." AND domain_id != ".intval($data['old']['domain_id']));
                     foreach($tmp as $tmprec) {
                        // we normalize the folder entries because we need to compare them
                        $tmp_folder = preg_replace('/[\/]{2,}/', '/', $tmprec['web_folder']); // replace / occuring multiple times
@@ -2037,7 +2052,7 @@
      }
      $web_folder = 'web';
      if($website['type'] == 'vhostsubdomain') $web_folder = $website['web_folder'];
      if($website['type'] == 'vhostsubdomain' || $website['type'] == 'vhostalias') $web_folder = $website['web_folder'];
      //* Get the folder path.
      if(substr($folder['path'], 0, 1) == '/') $folder['path'] = substr($folder['path'], 1);
@@ -2053,9 +2068,7 @@
      //* Create the folder path, if it does not exist
      if(!is_dir($folder_path)) {
         $app->system->mkdirpath($folder_path);
         $app->system->chown($folder_path, $website['system_user']);
         $app->system->chgrp($folder_path, $website['system_group']);
         $app->system->mkdirpath($folder_path, 0755, $website['system_user'], $website['system_group']);
      }
      //* Create empty .htpasswd file, if it does not exist
@@ -2116,7 +2129,7 @@
      }
      $web_folder = 'web';
      if($website['type'] == 'vhostsubdomain') $web_folder = $website['web_folder'];
      if($website['type'] == 'vhostsubdomain' || $website['type'] == 'vhostalias') $web_folder = $website['web_folder'];
      //* Get the folder path.
      if(substr($folder['path'], 0, 1) == '/') $folder['path'] = substr($folder['path'], 1);
@@ -2153,7 +2166,7 @@
      }
      $web_folder = 'web';
      if($website['type'] == 'vhostsubdomain') $web_folder = $website['web_folder'];
      if($website['type'] == 'vhostsubdomain' || $website['type'] == 'vhostalias') $web_folder = $website['web_folder'];
      //* Get the folder path.
      if(substr($data['old']['path'], 0, 1) == '/') $data['old']['path'] = substr($data['old']['path'], 1);
@@ -2224,7 +2237,7 @@
               $website_auth_location['path'] .= '/';
            }
            $basic_auth_locations[] = array('htpasswd_location' => '/'.$website_auth_location['path'],
               'htpasswd_path' => $website['document_root'].'/' . ($website['type'] == 'vhostsubdomain' ? $website['web_folder'] : 'web') . '/'.$website_auth_location['path']);
               'htpasswd_path' => $website['document_root'].'/' . (($website['type'] == 'vhostsubdomain' || $website['type'] == 'vhostalias') ? $website['web_folder'] : 'web') . '/'.$website_auth_location['path']);
         }
      }
      return $basic_auth_locations;
@@ -2250,13 +2263,22 @@
         }
         $content = '';
         $content .= "Include \"".$awstats_conf_dir."/awstats.conf\"\n";
         if (is_file($awstats_conf_dir."/awstats.conf")) {
            $include_file = $awstats_conf_dir."/awstats.conf";
         } elseif (is_file($awstats_conf_dir."/awstats.model.conf")) {
            $include_file = $awstats_conf_dir."/awstats.model.conf";
         }
         $content .= "Include \"".$include_file."\"\n";
         $content .= "LogFile=\"/var/log/ispconfig/httpd/".$data['new']['domain']."/access.log\"\n";
         $content .= "SiteDomain=\"".$data['new']['domain']."\"\n";
         $content .= "HostAliases=\"www.".$data['new']['domain']."  localhost 127.0.0.1\"\n";
         $app->system->file_put_contents($awstats_conf_dir.'/awstats.'.$data['new']['domain'].'.conf', $content);
         $app->log('Created AWStats config file: '.$awstats_conf_dir.'/awstats.'.$data['new']['domain'].'.conf', LOGLEVEL_DEBUG);
         if (isset($include_file)) {
            $app->system->file_put_contents($awstats_conf_dir.'/awstats.'.$data['new']['domain'].'.conf', $content);
            $app->log('Created AWStats config file: '.$awstats_conf_dir.'/awstats.'.$data['new']['domain'].'.conf', LOGLEVEL_DEBUG);
         } else {
            $app->log("No awstats base config found. Either awstats.conf or awstats.model.conf must exist in ".$awstats_conf_dir.".", LOGLEVEL_WARN);
         }
      }
      if(is_file($data['new']['document_root']."/" . $web_folder . "/stats/index.html")) $app->system->unlink($data['new']['document_root']."/" . $web_folder . "/stats/index.html");
@@ -2729,7 +2751,7 @@
            // web domain doesn't match hostname
            if(substr($hostname, -strlen($web['domain'])) != $web['domain']) continue;
            // own vhost and therefore server {} container of its own
            //if($web['type'] == 'vhostsubdomain') continue;
            //if($web['type'] == 'vhostsubdomain' || $web['type'] == 'vhostalias') continue;
            // alias domains/subdomains using rewrites and therefore a server {} container of their own
            //if(($web['type'] == 'alias' || $web['type'] == 'subdomain') && $web['redirect_type'] != '' && $web['redirect_path'] != '') continue;
@@ -2740,7 +2762,7 @@
               if($web['domain'] == $hostname){
                  if($web['domain_id'] == $domain_id || $web['parent_domain_id'] == $domain_id){
                     // own vhost and therefore server {} container of its own
                     if($web['type'] == 'vhostsubdomain') return false;
                     if($web['type'] == 'vhostsubdomain' || $web['type'] == 'vhostalias') return false;
                     // alias domains/subdomains using rewrites and therefore a server {} container of their own
                     if(($web['type'] == 'alias' || $web['type'] == 'subdomain') && $web['redirect_type'] != '' && $web['redirect_path'] != '') return false;
                     return true;
@@ -2754,7 +2776,7 @@
               if($web['domain'] == $hostname || $web['subdomain'].'.'.$web['domain'] == $hostname){
                  if($web['domain_id'] == $domain_id || $web['parent_domain_id'] == $domain_id){
                     // own vhost and therefore server {} container of its own
                     if($web['type'] == 'vhostsubdomain') return false;
                     if($web['type'] == 'vhostsubdomain' || $web['type'] == 'vhostalias') return false;
                     // alias domains/subdomains using rewrites and therefore a server {} container of their own
                     if(($web['type'] == 'alias' || $web['type'] == 'subdomain') && $web['redirect_type'] != '' && $web['redirect_path'] != '') return false;
                     return true;
@@ -2767,7 +2789,7 @@
            if(preg_match($pattern, $hostname)){
               if($web['domain_id'] == $domain_id || $web['parent_domain_id'] == $domain_id){
                  // own vhost and therefore server {} container of its own
                  if($web['type'] == 'vhostsubdomain') return false;
                  if($web['type'] == 'vhostsubdomain' || $web['type'] == 'vhostalias') return false;
                  // alias domains/subdomains using rewrites and therefore a server {} container of their own
                  if(($web['type'] == 'alias' || $web['type'] == 'subdomain') && $web['redirect_type'] != '' && $web['redirect_path'] != '') return false;
                  return true;