From 84d87adb9b1c8a820d9c0fec871e04e970dd3b9f Mon Sep 17 00:00:00 2001 From: Axel Guckelsberger Date: Sat, 27 Apr 2013 09:32:28 +0300 Subject: [PATCH] Update cron_tasks.inc.http.30.nginx.php --- scripts/jobs/cron_tasks.inc.http.30.nginx.php | 492 +++++++----------- 1 file changed, 199 insertions(+), 293 deletions(-) diff --git a/scripts/jobs/cron_tasks.inc.http.30.nginx.php b/scripts/jobs/cron_tasks.inc.http.30.nginx.php index 8665ace2..0efb66b6 100644 --- a/scripts/jobs/cron_tasks.inc.http.30.nginx.php +++ b/scripts/jobs/cron_tasks.inc.http.30.nginx.php @@ -38,7 +38,7 @@ class nginx protected $auth_backend_loaded = false; protected $htpasswds_data = array(); protected $known_htpasswdsfilenames = array(); - protected $mod_accesslog_loaded = "0"; + protected $mod_accesslog_loaded = '0'; protected $vhost_root_autoindex = false; protected $known_vhostfilenames = array(); /** @@ -79,9 +79,7 @@ class nginx fwrite($this->debugHandler, ' nginx::reload: restarting php processes' . "\n"); $this->logger->logAction(CRON_ACTION, LOG_INFO, 'restarting php processes'); safe_exec($this->settings['system']['phpreload_command']); - } - elseif((int)$this->settings['phpfpm']['enabled'] == 1) - { + } elseif ((int)$this->settings['phpfpm']['enabled'] == 1) { fwrite($this->debugHandler, ' nginx::reload: reloading php-fpm' . "\n"); $this->logger->logAction(CRON_ACTION, LOG_INFO, 'reloading php-fpm'); safe_exec(escapeshellcmd($this->settings['phpfpm']['reload'])); @@ -94,15 +92,14 @@ class nginx */ private function _createStandardErrorHandler() { - if($this->settings['defaultwebsrverrhandler']['enabled'] == '1' + if ($this->settings['defaultwebsrverrhandler']['enabled'] == '1' && ($this->settings['defaultwebsrverrhandler']['err401'] != '' || $this->settings['defaultwebsrverrhandler']['err403'] != '' || $this->settings['defaultwebsrverrhandler']['err404'] != '' || $this->settings['defaultwebsrverrhandler']['err500'] != '') ) { $vhosts_folder = ''; - if(is_dir($this->settings['system']['apacheconf_vhost'])) - { + if (is_dir($this->settings['system']['apacheconf_vhost'])) { $vhosts_folder = makeCorrectDir($this->settings['system']['apacheconf_vhost']); } else { $vhosts_folder = makeCorrectDir(dirname($this->settings['system']['apacheconf_vhost'])); @@ -110,48 +107,29 @@ class nginx $vhosts_filename = makeCorrectFile($vhosts_folder . '/05_froxlor_default_errorhandler.conf'); - if(!isset($this->nginx_data[$vhosts_filename])) - { + if (!isset($this->nginx_data[$vhosts_filename])) { $this->nginx_data[$vhosts_filename] = ''; } - - if ($this->settings['defaultwebsrverrhandler']['err401'] != '') { - $defhandler = $this->settings['defaultwebsrverrhandler']['err401']; - if (!validateUrl($defhandler)) { - $defhandler = makeCorrectFile($defhandler); - } - $this->nginx_data[$vhosts_filename].= 'error_page 401 ' . $defhandler . ';' . "\n"; - } - if ($this->settings['defaultwebsrverrhandler']['err403'] != '') { - $defhandler = $this->settings['defaultwebsrverrhandler']['err403']; - if (!validateUrl($defhandler)) { - $defhandler = makeCorrectFile($defhandler); + $statusCodes = array('401', '403', '404', '500'); + foreach ($statusCodes as $statusCode) { + if ($this->settings['defaultwebsrverrhandler']['err' . $statusCode] != '') { + $defhandler = $this->settings['defaultwebsrverrhandler']['err' . $statusCode]; + if (!validateUrl($defhandler)) { + $defhandler = makeCorrectFile($defhandler); + } + $this->nginx_data[$vhosts_filename].= 'error_page ' . $statusCode . ' ' . $defhandler . ';' . "\n"; } - $this->nginx_data[$vhosts_filename].= 'error_page 403 ' . $defhandler . ';' . "\n"; } - - if ($this->settings['defaultwebsrverrhandler']['err404'] != '') { - $defhandler = $this->settings['defaultwebsrverrhandler']['err404']; - if (!validateUrl($defhandler)) { - $defhandler = makeCorrectFile($defhandler); - } - $this->nginx_data[$vhosts_filename].= 'error_page 404 ' . $defhandler . ';' . "\n"; - } - - if ($this->settings['defaultwebsrverrhandler']['err500'] != '') { - $defhandler = $this->settings['defaultwebsrverrhandler']['err500']; - if (!validateUrl($defhandler)) { - $defhandler = makeCorrectFile($defhandler); - } - $this->nginx_data[$vhosts_filename].= 'error_page 500 ' . $defhandler . ';' . "\n"; - } - } } - public function createVirtualHosts(){ + + public function createVirtualHosts() + { } - public function createFileDirOptions(){ + + public function createFileDirOptions() + { } public function createIpPort() @@ -160,7 +138,6 @@ class nginx $result_ipsandports = $this->db->query($query); while ($row_ipsandports = $this->db->fetch_array($result_ipsandports)) { - if (filter_var($row_ipsandports['ip'], FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) { $ip = '[' . $row_ipsandports['ip'] . ']'; } else { @@ -178,17 +155,17 @@ class nginx if ($row_ipsandports['vhostcontainer'] == '1') { - $this->nginx_data[$vhost_filename].= 'server { ' . "\n"; + $this->nginx_data[$vhost_filename] .= 'server { ' . "\n"; /** * this HAS to be set for the default host in nginx or else no vhost will work */ - $this->nginx_data[$vhost_filename].= "\t". 'listen ' . $ip . ':' . $port . ' default;' . "\n"; + $this->nginx_data[$vhost_filename] .= "\t". 'listen ' . $ip . ':' . $port . ' default;' . "\n"; - $this->nginx_data[$vhost_filename].= "\t".'# Froxlor default vhost' . "\n"; - $this->nginx_data[$vhost_filename].= "\t".'server_name ' . $this->settings['system']['hostname'] . ';' . "\n"; + $this->nginx_data[$vhost_filename] .= "\t".'# Froxlor default vhost' . "\n"; + $this->nginx_data[$vhost_filename] .= "\t".'server_name ' . $this->settings['system']['hostname'] . ';' . "\n"; - $this->nginx_data[$vhost_filename].= "\t".'access_log /var/log/nginx/access.log;' . "\n"; + $this->nginx_data[$vhost_filename] .= "\t".'access_log /var/log/nginx/access.log;' . "\n"; $mypath = ''; @@ -206,10 +183,10 @@ class nginx $mypath = makeCorrectDir($row_ipsandports['docroot']); } - $this->nginx_data[$vhost_filename].= "\t".'root '.$mypath.';'."\n"; - $this->nginx_data[$vhost_filename].= "\t".'location / {'."\n"; - $this->nginx_data[$vhost_filename].= "\t\t".'index index.php index.html index.htm;'."\n"; - $this->nginx_data[$vhost_filename].= "\t".'}'."\n"; + $this->nginx_data[$vhost_filename] .= "\t".'root '.$mypath.';'."\n"; + $this->nginx_data[$vhost_filename] .= "\t".'location / {'."\n"; + $this->nginx_data[$vhost_filename] .= "\t\t".'index index.php index.html index.htm;'."\n"; + $this->nginx_data[$vhost_filename] .= "\t".'}'."\n"; if ($row_ipsandports['specialsettings'] != '') { $this->nginx_data[$vhost_filename].= $row_ipsandports['specialsettings'] . "\n"; @@ -229,26 +206,26 @@ class nginx $row_ipsandports['ssl_ca_file'] = $this->settings['system']['ssl_ca_file']; } if ($row_ipsandports['ssl_cert_file'] != '') { - $this->nginx_data[$vhost_filename].= "\t" . 'ssl on;' . "\n"; - $this->nginx_data[$vhost_filename].= "\t" . 'ssl_certificate ' . makeCorrectFile($row_ipsandports['ssl_cert_file']) . ';' . "\n"; + $this->nginx_data[$vhost_filename] .= "\t" . 'ssl on;' . "\n"; + $this->nginx_data[$vhost_filename] .= "\t" . 'ssl_certificate ' . makeCorrectFile($row_ipsandports['ssl_cert_file']) . ';' . "\n"; } if ($row_ipsandports['ssl_key_file'] != '') { - $this->nginx_data[$vhost_filename].= "\t" . 'ssl_certificate_key ' .makeCorrectFile($row_ipsandports['ssl_key_file']) . ';' . "\n"; + $this->nginx_data[$vhost_filename] .= "\t" . 'ssl_certificate_key ' .makeCorrectFile($row_ipsandports['ssl_key_file']) . ';' . "\n"; } if ($row_ipsandports['ssl_ca_file'] != '') { - $this->nginx_data[$vhost_filename].= 'ssl_client_certificate ' . makeCorrectFile($row_ipsandports['ssl_ca_file']) . ';' . "\n"; + $this->nginx_data[$vhost_filename] .= 'ssl_client_certificate ' . makeCorrectFile($row_ipsandports['ssl_ca_file']) . ';' . "\n"; } } - $this->nginx_data[$vhost_filename].= "\t".'location ~ \.php$ {'."\n"; - $this->nginx_data[$vhost_filename].= "\t\t".' if (!-f $request_filename) {'."\n"; - $this->nginx_data[$vhost_filename].= "\t\t\t".'return 404;'."\n"; - $this->nginx_data[$vhost_filename].= "\t\t".'}'."\n"; - $this->nginx_data[$vhost_filename].= "\t\t".'fastcgi_index index.php;'."\n"; - $this->nginx_data[$vhost_filename].= "\t\t".'include '.$this->settings['nginx']['fastcgiparams'].';'."\n"; - $this->nginx_data[$vhost_filename].= "\t\t".'fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;'."\n"; + $this->nginx_data[$vhost_filename] .= "\t".'location ~ \.php$ {'."\n"; + $this->nginx_data[$vhost_filename] .= "\t\t".' if (!-f $request_filename) {'."\n"; + $this->nginx_data[$vhost_filename] .= "\t\t\t".'return 404;'."\n"; + $this->nginx_data[$vhost_filename] .= "\t\t".'}'."\n"; + $this->nginx_data[$vhost_filename] .= "\t\t".'fastcgi_index index.php;'."\n"; + $this->nginx_data[$vhost_filename] .= "\t\t".'include '.$this->settings['nginx']['fastcgiparams'].';'."\n"; + $this->nginx_data[$vhost_filename] .= "\t\t".'fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;'."\n"; if ($row_ipsandports['ssl'] == '1') { - $this->nginx_data[$vhost_filename].= "\t\t".'fastcgi_param HTTPS on;'."\n"; + $this->nginx_data[$vhost_filename] .= "\t\t".'fastcgi_param HTTPS on;'."\n"; } if ((int)$this->settings['phpfpm']['enabled'] == 1 && (int)$this->settings['phpfpm']['enabled_ownvhost'] == 1 @@ -267,13 +244,12 @@ class nginx ); $php = new phpinterface($this->getDB(), $this->settings, $domain); - $this->nginx_data[$vhost_filename].= "\t\t".'fastcgi_pass unix:' . $php->getInterface()->getSocketFile() . ';' . "\n"; - + $this->nginx_data[$vhost_filename] .= "\t\t".'fastcgi_pass unix:' . $php->getInterface()->getSocketFile() . ';' . "\n"; } else { - $this->nginx_data[$vhost_filename].= "\t\t".'fastcgi_pass ' . $this->settings['system']['nginx_php_backend'] . ';' . "\n"; + $this->nginx_data[$vhost_filename] .= "\t\t".'fastcgi_pass ' . $this->settings['system']['nginx_php_backend'] . ';' . "\n"; } - $this->nginx_data[$vhost_filename].= "\t".'}'."\n"; - $this->nginx_data[$vhost_filename].= '}' . "\n\n"; + $this->nginx_data[$vhost_filename] .= "\t".'}'."\n"; + $this->nginx_data[$vhost_filename] .= '}' . "\n\n"; // End of Froxlor server{}-part } $this->createNginxHosts($row_ipsandports['ip'], $row_ipsandports['port'], $row_ipsandports['ssl'], $vhost_filename); @@ -287,30 +263,26 @@ class nginx protected function createNginxHosts($ip, $port, $ssl, $vhost_filename) { - $query = "SELECT * FROM " . TABLE_PANEL_IPSANDPORTS . " WHERE `ip`='" . $ip . "' AND `port`='" . $port . "'"; + $query = "SELECT * FROM " . TABLE_PANEL_IPSANDPORTS . " WHERE `ip` = '" . $ip . "' AND `port` = '" . $port . "'"; $ipandport = $this->db->query_first($query); - if($ssl == '0') - { - $query2 = "SELECT `d`.*, `pd`.`domain` AS `parentdomain`, `c`.`loginname`, `c`.`guid`, `c`.`email`, `c`.`documentroot` AS `customerroot`, `c`.`deactivated`, `c`.`phpenabled` AS `phpenabled` FROM `" . TABLE_PANEL_DOMAINS . "` `d` LEFT JOIN `" . TABLE_PANEL_CUSTOMERS . "` `c` USING(`customerid`) LEFT JOIN `" . TABLE_PANEL_DOMAINS . "` `pd` ON (`pd`.`id` = `d`.`parentdomainid`) WHERE `d`.`ipandport`='" . $ipandport['id'] . "' AND `d`.`aliasdomain` IS NULL AND `d`.`email_only` <> 1 ORDER BY `d`.`iswildcarddomain`, `d`.`domain` ASC"; - } - else - { - $query2 = "SELECT `d`.*, `pd`.`domain` AS `parentdomain`, `c`.`loginname`, `c`.`guid`, `c`.`email`, `c`.`documentroot` AS `customerroot`, `c`.`deactivated`, `c`.`phpenabled` AS `phpenabled` FROM `" . TABLE_PANEL_DOMAINS . "` `d` LEFT JOIN `" . TABLE_PANEL_CUSTOMERS . "` `c` USING(`customerid`) LEFT JOIN `" . TABLE_PANEL_DOMAINS . "` `pd` ON (`pd`.`id` = `d`.`parentdomainid`) WHERE `d`.`ssl_ipandport`='" . $ipandport['id'] . "' AND `d`.`aliasdomain` IS NULL AND `d`.`email_only` <> 1 ORDER BY `d`.`iswildcarddomain`, `d`.`domain` ASC"; + $query2 = "SELECT `d`.*, `pd`.`domain` AS `parentdomain`, `c`.`loginname`, `c`.`guid`, `c`.`email`, `c`.`documentroot` AS `customerroot`, `c`.`deactivated`, `c`.`phpenabled` AS `phpenabled` FROM `" . TABLE_PANEL_DOMAINS . "` `d` LEFT JOIN `" . TABLE_PANEL_CUSTOMERS . "` `c` USING(`customerid`) LEFT JOIN `" . TABLE_PANEL_DOMAINS . "` `pd` ON (`pd`.`id` = `d`.`parentdomainid`) "; + if ($ssl == '0') { + $query2 .= "WHERE `d`.`ipandport` = '" . $ipandport['id'] . "' "; + } else { + $query2 .= "WHERE `d`.`ssl_ipandport` = '" . $ipandport['id'] . "' "; } + $query2 .= "AND `d`.`aliasdomain` IS NULL AND `d`.`email_only` <> 1 ORDER BY `d`.`iswildcarddomain`, `d`.`domain` ASC"; $included_vhosts = array(); $result_domains = $this->db->query($query2); - while($domain = $this->db->fetch_array($result_domains)) - { - if (is_dir($this->settings['system']['apacheconf_vhost'])) - { + while ($domain = $this->db->fetch_array($result_domains)) { + if (is_dir($this->settings['system']['apacheconf_vhost'])) { safe_exec('mkdir -p '.escapeshellarg(makeCorrectDir($this->settings['system']['apacheconf_vhost']))); $vhost_filename = $this->getVhostFilename($domain); } - if(!isset($this->nginx_data[$vhost_filename])) - { + if (!isset($this->nginx_data[$vhost_filename])) { $this->nginx_data[$vhost_filename] = ''; } @@ -320,16 +292,13 @@ class nginx $domain['port'] = $ipandport['port']; $domain['ssl_cert_file'] = $ipandport['ssl_cert_file']; - if( (!empty($this->nginx_data[$vhost_filename]) && !is_dir($this->settings['system']['apacheconf_vhost'])) - || is_dir($this->settings['system']['apacheconf_vhost'])) - { - if($ssl == '1') - { + if ((!empty($this->nginx_data[$vhost_filename]) && !is_dir($this->settings['system']['apacheconf_vhost'])) + || is_dir($this->settings['system']['apacheconf_vhost']) + ) { + if ($ssl == '1') { $ssl_vhost = true; $ips_and_ports_index = 'ssl_ipandport'; - } - else - { + } else { $ssl_vhost = false; $ips_and_ports_index = 'ipandport'; } @@ -342,30 +311,24 @@ class nginx protected function getVhostFilename($domain, $ssl_vhost = false) { - if((int)$domain['parentdomainid'] == 0 - && isCustomerStdSubdomain((int)$domain['id']) == false - && ((int)$domain['ismainbutsubto'] == 0 - || domainMainToSubExists($domain['ismainbutsubto']) == false) + if ((int)$domain['parentdomainid'] == 0 + && isCustomerStdSubdomain((int)$domain['id']) == false + && ((int)$domain['ismainbutsubto'] == 0 + || domainMainToSubExists($domain['ismainbutsubto']) == false) ) { $vhost_no = '22'; - } - elseif((int)$domain['parentdomainid'] == 0 - && isCustomerStdSubdomain((int)$domain['id']) == false - && (int)$domain['ismainbutsubto'] > 0 + } elseif ((int)$domain['parentdomainid'] == 0 + && isCustomerStdSubdomain((int)$domain['id']) == false + && (int)$domain['ismainbutsubto'] > 0 ) { $vhost_no = '21'; - } - else - { + } else { $vhost_no = '20'; } - if($ssl_vhost === true) - { + if ($ssl_vhost === true) { $vhost_filename = makeCorrectFile($this->settings['system']['apacheconf_vhost'] . '/'.$vhost_no.'_froxlor_ssl_vhost_' . $domain['domain'] . '.conf'); - } - else - { + } else { $vhost_filename = makeCorrectFile($this->settings['system']['apacheconf_vhost'] . '/'.$vhost_no.'_froxlor_normal_vhost_' . $domain['domain'] . '.conf'); } @@ -374,19 +337,17 @@ class nginx protected function getVhostContent($domain, $ssl_vhost = false) { - if($ssl_vhost === true - && $domain['ssl'] != '1') - { + if ($ssl_vhost === true + && $domain['ssl'] != '1' + ) { return ''; } - if($ssl_vhost === true - && $domain['ssl'] == '1') - { + if ($ssl_vhost === true + && $domain['ssl'] == '1' + ) { $query = "SELECT * FROM " . TABLE_PANEL_IPSANDPORTS . " WHERE `id`='" . $domain['ssl_ipandport'] . "'"; - } - else - { + } else { $query = "SELECT * FROM " . TABLE_PANEL_IPSANDPORTS . " WHERE `id`='" . $domain['ipandport'] . "'"; } @@ -395,27 +356,24 @@ class nginx $domain['port'] = $ipandport['port']; $domain['ssl_cert_file'] = $ipandport['ssl_cert_file']; - if(filter_var($domain['ip'], FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) - { + if (filter_var($domain['ip'], FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) { $ipport = '[' . $domain['ip'] . ']:' . $domain['port']; - } - else - { + } else { $ipport = $domain['ip'] . ':' . $domain['port']; } $vhost_content = ''; // open vhost-container - $vhost_content.= 'server { ' . "\n"; + $vhost_content .= 'server { ' . "\n"; // listening statement (required) - $vhost_content.= "\t" . 'listen ' . $ipport . ';' . "\n"; + $vhost_content .= "\t" . 'listen ' . $ipport . ';' . "\n"; // get all server-names - $vhost_content.= $this->getServerNames($domain); + $vhost_content .= $this->getServerNames($domain); // respect ssl_redirect settings, #542 - if($ssl_vhost == false + if ($ssl_vhost == false && $domain['ssl'] == '1' && $domain['ssl_redirect'] == '1' ) { @@ -424,49 +382,50 @@ class nginx // if the documentroot is an URL we just redirect if (preg_match('/^https?\:\/\//', $domain['documentroot'])) { - $vhost_content.= "\t".'rewrite ^(.*) '.$this->idnaConvert->encode($domain['documentroot']).'$1 permanent;'."\n"; + $vhost_content .= "\t".'rewrite ^(.*) '.$this->idnaConvert->encode($domain['documentroot']).'$1 permanent;'."\n"; } else { mkDirWithCorrectOwnership($domain['customerroot'], $domain['documentroot'], $domain['guid'], $domain['guid'], true); - $vhost_content.= $this->getLogFiles($domain); - $vhost_content.= $this->getWebroot($domain, $ssl_vhost); + $vhost_content .= $this->getLogFiles($domain); + $vhost_content .= $this->getWebroot($domain, $ssl_vhost); if ($this->_deactivated == false) { - $vhost_content.= $this->create_pathOptions($domain); - $vhost_content.= $this->composePhpOptions($domain, $ssl_vhost); + $vhost_content .= $this->create_pathOptions($domain); + $vhost_content .= $this->composePhpOptions($domain, $ssl_vhost); if ($domain['specialsettings'] != "") { - $vhost_content.= $domain['specialsettings'] . "\n"; + $vhost_content .= $domain['specialsettings'] . "\n"; } if ($ipandport['default_vhostconf_domain'] != '') { - $vhost_content.= $ipandport['default_vhostconf_domain'] . "\n"; + $vhost_content .= $ipandport['default_vhostconf_domain'] . "\n"; } if ($this->settings['system']['default_vhostconf'] != '') { - $vhost_content.= $this->settings['system']['default_vhostconf'] . "\n"; + $vhost_content .= $this->settings['system']['default_vhostconf'] . "\n"; } } // merge duplicate / sections, #1193 - $l_regex1="/(location\ \/\ \{)(.*)(\})/smU"; - $l_regex2="/(location\ \/\ \{.*\})/smU"; - $replace_by=""; - $replacements=preg_match_all($l_regex1,$vhost_content,$out); + $l_regex1 = "/(location\ \/\ \{)(.*)(\})/smU"; + $l_regex2 = "/(location\ \/\ \{.*\})/smU"; + $replace_by = ''; + $replacements = preg_match_all($l_regex1,$vhost_content,$out); if ($replacements > 1) { foreach ($out[2] as $val) { - $replace_by.=$val."\n"; + $replace_by .= $val."\n"; } - $vhost_content=preg_replace($l_regex2, "", $vhost_content, $replacements-1); - $vhost_content=preg_replace($l_regex2, "location / {\n\t\t". $replace_by ."\t}\n", $vhost_content); + $vhost_content = preg_replace($l_regex2, "", $vhost_content, $replacements-1); + $vhost_content = preg_replace($l_regex2, "location / {\n\t\t". $replace_by ."\t}\n", $vhost_content); } } - $vhost_content.= '}' . "\n\n"; + $vhost_content .= '}' . "\n\n"; return $vhost_content; } - protected function create_pathOptions($domain) { + protected function create_pathOptions($domain) + { $has_location = false; $query = "SELECT * FROM " . TABLE_PANEL_HTACCESS . " WHERE `path` LIKE '" . $domain['documentroot'] . "%'"; @@ -477,13 +436,12 @@ class nginx // for each entry in the htaccess table while ($row = $this->db->fetch_array($result)) { - if (!empty($row['error404path'])) { $defhandler = $row['error404path']; if (!validateUrl($defhandler)) { $defhandler = makeCorrectFile($defhandler); } - $path_options.= "\t".'error_page 404 ' . $defhandler . ';' . "\n"; + $path_options .= "\t".'error_page 404 ' . $defhandler . ';' . "\n"; } if (!empty($row['error403path'])) { @@ -491,7 +449,7 @@ class nginx if (!validateUrl($defhandler)) { $defhandler = makeCorrectFile($defhandler); } - $path_options.= "\t".'error_page 403 ' . $defhandler . ';' . "\n"; + $path_options .= "\t".'error_page 403 ' . $defhandler . ';' . "\n"; } if (!empty($row['error500path'])) { @@ -499,21 +457,20 @@ class nginx if (!validateUrl($defhandler)) { $defhandler = makeCorrectFile($defhandler); } - $path_options.= "\t".'error_page 500 502 503 504 ' . $defhandler . ';' . "\n"; + $path_options .= "\t".'error_page 500 502 503 504 ' . $defhandler . ';' . "\n"; } -// if($row['options_indexes'] != '0') -// { +// if ($row['options_indexes'] != '0') { $path = makeCorrectDir(substr($row['path'], strlen($domain['documentroot']) - 1)); mkDirWithCorrectOwnership($domain['documentroot'], $row['path'], $domain['guid'], $domain['guid']); - $path_options.= "\t".'# '.$path."\n"; + $path_options .= "\t".'# '.$path."\n"; if ($path == '/') { $this->vhost_root_autoindex = true; - $path_options.= "\t".'location ' . $path . ' {' . "\n"; + $path_options .= "\t".'location ' . $path . ' {' . "\n"; if($this->vhost_root_autoindex) { - $path_options.= "\t\t" . 'autoindex on;' . "\n"; + $path_options .= "\t\t" . 'autoindex on;' . "\n"; $this->vhost_root_autoindex = false; } $path_options.= "\t\t" . 'index index.php index.html index.htm;'."\n"; @@ -521,38 +478,34 @@ class nginx // check if we have a htpasswd for this path // (damn nginx does not like more than one // 'location'-part with the same path) - if(count($htpasswds) > 0) - { - foreach($htpasswds as $idx => $single) - { - switch($single['path']){ + if (count($htpasswds) > 0) { + foreach ($htpasswds as $idx => $single) { + switch ($single['path']) { case '/awstats/': case '/webalizer/': // no stats-alias in "location /"-context break; default: - if ($single['path']=='/'){ - $path_options.= "\t\t" . 'auth_basic "Restricted Area";' . "\n"; - $path_options.= "\t\t" . 'auth_basic_user_file ' . makeCorrectFile($single['usrf']) . ';'."\n"; + if ($single['path'] == '/') { + $path_options .= "\t\t" . 'auth_basic "Restricted Area";' . "\n"; + $path_options .= "\t\t" . 'auth_basic_user_file ' . makeCorrectFile($single['usrf']) . ';'."\n"; // remove already used entries so we do not have doubles unset($htpasswds[$idx]); } } } } - $path_options.= "\t".'}' . "\n"; + $path_options .= "\t".'}' . "\n"; $this->vhost_root_autoindex = false; - } - else - { - $path_options.= "\t".'location ' . $path . ' {' . "\n"; - if($this->vhost_root_autoindex) { - $path_options.= "\t\t" . 'autoindex on;' . "\n"; + } else { + $path_options .= "\t".'location ' . $path . ' {' . "\n"; + if ($this->vhost_root_autoindex) { + $path_options .= "\t\t" . 'autoindex on;' . "\n"; $this->vhost_root_autoindex = false; } - $path_options.= "\t\t" . 'index index.php index.html index.htm;'."\n"; - $path_options.= "\t".'} ' . "\n"; + $path_options .= "\t\t" . 'index index.php index.html index.htm;'."\n"; + $path_options .= "\t".'} ' . "\n"; } // } @@ -560,22 +513,22 @@ class nginx * Perl support * required the fastCGI wrapper to be running to receive the CGI requests. */ - if(customerHasPerlEnabled($domain['customerid']) - && $row['options_cgi'] != '0') - { + if (customerHasPerlEnabled($domain['customerid']) + && $row['options_cgi'] != '0' + ) { $path = makeCorrectDir(substr($row['path'], strlen($domain['documentroot']) - 1)); mkDirWithCorrectOwnership($domain['documentroot'], $row['path'], $domain['guid'], $domain['guid']); // We need to remove the last slash, otherwise the regex wouldn't work - if($row['path'] != $domain['documentroot']) { + if ($row['path'] != $domain['documentroot']) { $path = substr($path, 0, -1); } - $path_options.= "\t" . 'location ~ \(.pl|.cgi)$ {' . "\n"; - $path_options.= "\t\t" . 'gzip off; #gzip makes scripts feel slower since they have to complete before getting gzipped' . "\n"; - $path_options.= "\t\t" . 'fastcgi_pass '. $this->settings['system']['perl_server'] . ';' . "\n"; - $path_options.= "\t\t" . 'fastcgi_index index.cgi;' . "\n"; - $path_options.= "\t\t" . 'include '.$this->settings['nginx']['fastcgiparams'].';'."\n"; - $path_options.= "\t" . '}' . "\n"; + $path_options .= "\t" . 'location ~ \(.pl|.cgi)$ {' . "\n"; + $path_options .= "\t\t" . 'gzip off; #gzip makes scripts feel slower since they have to complete before getting gzipped' . "\n"; + $path_options .= "\t\t" . 'fastcgi_pass '. $this->settings['system']['perl_server'] . ';' . "\n"; + $path_options .= "\t\t" . 'fastcgi_index index.cgi;' . "\n"; + $path_options .= "\t\t" . 'include '.$this->settings['nginx']['fastcgiparams'].';'."\n"; + $path_options .= "\t" . '}' . "\n"; } } @@ -583,24 +536,20 @@ class nginx /* * now the rest of the htpasswds */ - if(count($htpasswds) > 0) - { - foreach($htpasswds as $idx => $single) - { - //if($single['path'] != "/") - //{ - switch($single['path']) - { + if (count($htpasswds) > 0) { + foreach ($htpasswds as $idx => $single) { + //if ($single['path'] != '/') { + switch ($single['path']) { case '/awstats/': case '/webalizer/': - $path_options.= $this->getStats($domain,$single); + $path_options .= $this->getStats($domain,$single); unset($htpasswds[$idx]); break; default: - $path_options.= "\t" . 'location ' . makeCorrectDir($single['path']) . ' {' . "\n"; - $path_options.= "\t\t" . 'auth_basic "Restricted Area";' . "\n"; - $path_options.= "\t\t" . 'auth_basic_user_file ' . makeCorrectFile($single['usrf']) . ';'."\n"; - $path_options.= "\t".'}' . "\n"; + $path_options .= "\t" . 'location ' . makeCorrectDir($single['path']) . ' {' . "\n"; + $path_options .= "\t\t" . 'auth_basic "Restricted Area";' . "\n"; + $path_options .= "\t\t" . 'auth_basic_user_file ' . makeCorrectFile($single['usrf']) . ';'."\n"; + $path_options .= "\t".'}' . "\n"; } //} unset($htpasswds[$idx]); @@ -623,7 +572,6 @@ class nginx $returnval = array(); $x = 0; while ($row_htpasswds = $this->db->fetch_array($result)) { - if (count($row_htpasswds) > 0) { $htpasswd_filename = makeCorrectFile($this->settings['system']['apacheconf_htpasswddir'] . '/' . $row_htpasswds['customerid'] . '-' . md5($row_htpasswds['path']) . '.htpasswd'); @@ -657,8 +605,7 @@ class nginx protected function composePhpOptions($domain, $ssl_vhost = false) { $phpopts = ''; - if($domain['phpenabled'] == '1') - { + if ($domain['phpenabled'] == '1') { $phpopts = "\t".'location ~ \.php$ {'."\n"; $phpopts.= "\t\t".'try_files $uri =404;'."\n"; $phpopts.= "\t\t".'fastcgi_split_path_info ^(.+\.php)(/.+)$;'."\n"; @@ -678,38 +625,36 @@ class nginx { $webroot_text = ''; - if($domain['deactivated'] == '1' - && $this->settings['system']['deactivateddocroot'] != '') - { - $webroot_text.= "\t".'# Using docroot for deactivated users...' . "\n"; - $webroot_text.= "\t".'root '.makeCorrectDir($this->settings['system']['deactivateddocroot']).';'."\n"; + if ($domain['deactivated'] == '1' + && $this->settings['system']['deactivateddocroot'] != '' + ) { + $webroot_text .= "\t".'# Using docroot for deactivated users...' . "\n"; + $webroot_text .= "\t".'root '.makeCorrectDir($this->settings['system']['deactivateddocroot']).';'."\n"; $this->_deactivated = true; - } - else - { - $webroot_text.= "\t".'root '.makeCorrectDir($domain['documentroot']).';'."\n"; + } else { + $webroot_text .= "\t".'root '.makeCorrectDir($domain['documentroot']).';'."\n"; $this->_deactivated = false; } - $webroot_text.= "\n\t".'location / {'."\n"; - $webroot_text.= "\t\t".'index index.php index.html index.htm;'."\n"; - $webroot_text.= "\t\t" . 'try_files $uri $uri/ @rewrites;'."\n"; + $webroot_text .= "\n\t".'location / {'."\n"; + $webroot_text .= "\t\t".'index index.php index.html index.htm;'."\n"; + $webroot_text .= "\t\t" . 'try_files $uri $uri/ @rewrites;'."\n"; - if($this->vhost_root_autoindex) { - $webroot_text.= "\t\t".'autoindex on;'."\n"; + if ($this->vhost_root_autoindex) { + $webroot_text .= "\t\t".'autoindex on;'."\n"; $this->vhost_root_autoindex = false; } - $webroot_text.= "\t".'}'."\n\n"; - $webroot_text.= "\tlocation @rewrites {\n"; - $webroot_text.= "\t\trewrite ^ /index.php last;\n"; - $webroot_text.= "\t}\n\n"; + $webroot_text .= "\t".'}'."\n\n"; + $webroot_text .= "\tlocation @rewrites {\n"; + $webroot_text .= "\t\trewrite ^ /index.php last;\n"; + $webroot_text .= "\t}\n\n"; return $webroot_text; } - protected function getStats($domain, $single) { - + protected function getStats($domain, $single) + { $stats_text = ''; // define basic path to the stats @@ -728,33 +673,31 @@ class nginx if ($this->settings['system']['awstats_enabled'] == '1') { // awstats - $stats_text.= "\t" . 'location /awstats {' . "\n"; + $stats_text .= "\t" . 'location /awstats {' . "\n"; } else { // webalizer - $stats_text.= "\t" . 'location /webalizer {' . "\n"; + $stats_text .= "\t" . 'location /webalizer {' . "\n"; } - $stats_text.= "\t\t" . 'alias ' . $alias_dir . ';' . "\n"; - $stats_text.= "\t\t" . 'auth_basic "Restricted Area";' . "\n"; - $stats_text.= "\t\t" . 'auth_basic_user_file ' . makeCorrectFile($single['usrf']) . ';'."\n"; - $stats_text.= "\t" . '}' . "\n\n"; + $stats_text .= "\t\t" . 'alias ' . $alias_dir . ';' . "\n"; + $stats_text .= "\t\t" . 'auth_basic "Restricted Area";' . "\n"; + $stats_text .= "\t\t" . 'auth_basic_user_file ' . makeCorrectFile($single['usrf']) . ';'."\n"; + $stats_text .= "\t" . '}' . "\n\n"; return $stats_text; } - protected function getLogFiles($domain) { - + protected function getLogFiles($domain) + { $logfiles_text = ''; + $speciallogfile = ''; if ($domain['speciallogfile'] == '1') { - if ($domain['parentdomainid'] == '0') { $speciallogfile = '-' . $domain['domain']; } else { $speciallogfile = '-' . $domain['parentdomain']; } - } else { - $speciallogfile = ''; } // The normal access/error - logging is enabled @@ -770,8 +713,8 @@ class nginx chown($access_log, $this->settings['system']['httpuser']); chgrp($access_log, $this->settings['system']['httpgroup']); - $logfiles_text.= "\t".'access_log ' . $access_log . ' combined;' . "\n"; - $logfiles_text.= "\t".'error_log ' . $error_log . ' error;' . "\n"; + $logfiles_text .= "\t".'access_log ' . $access_log . ' combined;' . "\n"; + $logfiles_text .= "\t".'error_log ' . $error_log . ' error;' . "\n"; if ($this->settings['system']['awstats_enabled'] == '1') { if ((int)$domain['parentdomainid'] == 0) { @@ -782,27 +725,24 @@ class nginx OR `parentdomainid` =\''. $domain['id']. '\''); while (($alias_domain = $this->db->fetch_array($alias_domains)) !== false) { - $server_alias.= ' ' . $alias_domain['domain'] . ' '; + $server_alias .= ' ' . $alias_domain['domain'] . ' '; if ($alias_domain['iswildcarddomain'] == '1') { - $server_alias.= '*.' . $domain['domain']; + $server_alias .= '*.' . $domain['domain']; } else { if ($alias_domain['wwwserveralias'] == '1') { - $server_alias.= 'www.' . $alias_domain['domain']; + $server_alias .= 'www.' . $alias_domain['domain']; } else { - $server_alias.= ''; + $server_alias .= ''; } } } + $alias = ''; if ($domain['iswildcarddomain'] == '1') { $alias = '*.' . $domain['domain']; - } else { - if ($domain['wwwserveralias'] == '1') { - $alias = 'www.' . $domain['domain']; - } else { - $alias = ''; - } + } elseif ($domain['wwwserveralias'] == '1') { + $alias = 'www.' . $domain['domain']; } // After inserting the AWStats information, @@ -824,47 +764,29 @@ class nginx { $server_alias = ''; - if($domain['iswildcarddomain'] == '1') - { + if ($domain['iswildcarddomain'] == '1') { $server_alias = '*.' . $domain['domain']; - } - else - { - if($domain['wwwserveralias'] == '1') - { - $server_alias = 'www.' . $domain['domain']; - } - else - { - $server_alias = ''; - } + } elseif ($domain['wwwserveralias'] == '1') { + $server_alias = 'www.' . $domain['domain']; } $alias_domains = $this->db->query('SELECT `domain`, `iswildcarddomain`, `wwwserveralias` FROM `' . TABLE_PANEL_DOMAINS . '` WHERE `aliasdomain`=\'' . $domain['id'] . '\''); - while(($alias_domain = $this->db->fetch_array($alias_domains)) !== false) - { - $server_alias.= ' ' . $alias_domain['domain']; + while (($alias_domain = $this->db->fetch_array($alias_domains)) !== false) { + $server_alias .= ' ' . $alias_domain['domain']; - if($alias_domain['iswildcarddomain'] == '1') - { - $server_alias.= ' *.' . $alias_domain['domain']; - } - else - { - if($alias_domain['wwwserveralias'] == '1') - { - $server_alias.= ' www.' . $alias_domain['domain']; - } + if ($alias_domain['iswildcarddomain'] == '1') { + $server_alias .= ' *.' . $alias_domain['domain']; + } elseif ($alias_domain['wwwserveralias'] == '1') { + $server_alias.= ' www.' . $alias_domain['domain']; } } $servernames_text = "\t".'server_name '.$domain['domain']; - if(trim($server_alias) != '') - { - $servernames_text.= ' '.$server_alias; + if (trim($server_alias) != '') { + $servernames_text .= ' '.$server_alias; } - $servernames_text.= ';' . "\n"; + $servernames_text .= ';' . "\n"; return $servernames_text; } @@ -874,8 +796,7 @@ class nginx fwrite($this->debugHandler, ' nginx::writeConfigs: rebuilding ' . $this->settings['system']['apacheconf_vhost'] . "\n"); $this->logger->logAction(CRON_ACTION, LOG_INFO, "rebuilding " . $this->settings['system']['apacheconf_vhost']); - if(!isConfigDir($this->settings['system']['apacheconf_vhost'])) - { + if (!isConfigDir($this->settings['system']['apacheconf_vhost'])) { // Save one big file $vhosts_file = ''; @@ -885,41 +806,32 @@ class nginx // 3. main-domains ksort($this->nginx_data); - foreach($this->nginx_data as $vhosts_filename => $vhost_content) - { + foreach ($this->nginx_data as $vhosts_filename => $vhost_content) { $vhosts_file.= $vhost_content . "\n\n"; } $vhosts_filename = $this->settings['system']['apacheconf_vhost']; // Apply header - $vhosts_file = '# ' . basename($vhosts_filename) . "\n" . '# Created ' . date('d.m.Y H:i') . "\n" . '# Do NOT manually edit this file, all changes will be deleted after the next domain change at the panel.' . "\n" . "\n" . $vhosts_file; $vhosts_file_handler = fopen($vhosts_filename, 'w'); fwrite($vhosts_file_handler, $vhosts_file); fclose($vhosts_file_handler); - } - else - { - if(!file_exists($this->settings['system']['apacheconf_vhost'])) - { + } else { + if (!file_exists($this->settings['system']['apacheconf_vhost'])) { fwrite($this->debugHandler, ' nginx::writeConfigs: mkdir ' . escapeshellarg(makeCorrectDir($this->settings['system']['apacheconf_vhost'])) . "\n"); $this->logger->logAction(CRON_ACTION, LOG_NOTICE, 'mkdir ' . escapeshellarg(makeCorrectDir($this->settings['system']['apacheconf_vhost']))); safe_exec('mkdir -p ' . escapeshellarg(makeCorrectDir($this->settings['system']['apacheconf_vhost']))); } // Write a single file for every vhost - - foreach($this->nginx_data as $vhosts_filename => $vhosts_file) - { + foreach ($this->nginx_data as $vhosts_filename => $vhosts_file) { $this->known_filenames[] = basename($vhosts_filename); // Apply header - $vhosts_file = '# ' . basename($vhosts_filename) . "\n" . '# Created ' . date('d.m.Y H:i') . "\n" . '# Do NOT manually edit this file, all changes will be deleted after the next domain change at the panel.' . "\n" . "\n" . $vhosts_file; - if(!empty($vhosts_filename)) - { + if (!empty($vhosts_filename)) { $vhosts_file_handler = fopen($vhosts_filename, 'w'); fwrite($vhosts_file_handler, $vhosts_file); fclose($vhosts_file_handler); @@ -931,26 +843,20 @@ class nginx /* * htaccess stuff */ - if(count($this->htpasswds_data) > 0) - { - if(!file_exists($this->settings['system']['apacheconf_htpasswddir'])) - { + if (count($this->htpasswds_data) > 0) { + if (!file_exists($this->settings['system']['apacheconf_htpasswddir'])) { $umask = umask(); umask(0000); mkdir($this->settings['system']['apacheconf_htpasswddir'], 0751); umask($umask); - } - elseif(!is_dir($this->settings['system']['apacheconf_htpasswddir'])) - { + } elseif (!is_dir($this->settings['system']['apacheconf_htpasswddir'])) { fwrite($this->debugHandler, ' cron_tasks: WARNING!!! ' . $this->settings['system']['apacheconf_htpasswddir'] . ' is not a directory. htpasswd directory protection is disabled!!!' . "\n"); echo 'WARNING!!! ' . $this->settings['system']['apacheconf_htpasswddir'] . ' is not a directory. htpasswd directory protection is disabled!!!'; $this->logger->logAction(CRON_ACTION, LOG_WARNING, 'WARNING!!! ' . $this->settings['system']['apacheconf_htpasswddir'] . ' is not a directory. htpasswd directory protection is disabled!!!'); } - if(is_dir($this->settings['system']['apacheconf_htpasswddir'])) - { - foreach($this->htpasswds_data as $htpasswd_filename => $htpasswd_file) - { + if (is_dir($this->settings['system']['apacheconf_htpasswddir'])) { + foreach ($this->htpasswds_data as $htpasswd_filename => $htpasswd_file) { $this->known_htpasswdsfilenames[] = basename($htpasswd_filename); $htpasswd_file_handler = fopen($htpasswd_filename, 'w'); fwrite($htpasswd_file_handler, $htpasswd_file);