Rewrite of merging special vhost settings, fixes #1430

Signed-off-by: Roman Schmerold (BNoiZe) <bnoize@froxlor.org>
This commit is contained in:
Roman Schmerold (BNoiZe)
2014-10-12 15:34:40 +02:00
parent 911d3769f6
commit 1b00a816e3

View File

@@ -425,7 +425,7 @@ class nginx {
$vhost_content.= isset($this->needed_htpasswds[$domain['id']]) ? $this->needed_htpasswds[$domain['id']] . "\n" : ''; $vhost_content.= isset($this->needed_htpasswds[$domain['id']]) ? $this->needed_htpasswds[$domain['id']] . "\n" : '';
if ($domain['specialsettings'] != "") { if ($domain['specialsettings'] != "") {
$vhost_content .= $domain['specialsettings'] . "\n"; $vhost_content = $this->mergeVhostCustom($vhost_content, $domain['specialsettings']);
} }
if ($_vhost_content != '') { if ($_vhost_content != '') {
@@ -439,76 +439,74 @@ class nginx {
} }
$vhost_content .= '}' . "\n\n"; $vhost_content .= '}' . "\n\n";
return $this->mergeVhostBlocks($vhost_content); return $vhost_content;
} }
protected function mergeVhostCustom($vhost_frx, $vhost_usr) {
// Clean froxlor defined settings
$vhost_frx = explode("\n", preg_replace('/[ \t]+/', ' ', trim(preg_replace('/\t+/', '', $vhost_frx)))); // Break into array items
protected function mergeVhostBlocks($vhost_content) { // Clean user defined settings
$vhost_content = str_replace("\r", "\n", $vhost_content); // Remove windows linebreaks $vhost_usr = str_replace("\r", "\n", $vhost_usr); // Remove windows linebreaks
$vhost_content = preg_replace('/^[\s\t]*#.*/m', "", $vhost_content); // Remove comments $vhost_usr = preg_replace('/^[\s\t]*#.*/m', "", $vhost_usr); // Remove comments
$vhost_content = str_replace(array("{", "}"), array("{\n", "\n}"), $vhost_content); // Break blocks into lines $vhost_usr = str_replace(array("{", "}"), array("{\n", "\n}"), $vhost_usr); // Break blocks into lines
$vhost_content = explode("\n", preg_replace('/[ \t]+/', ' ', trim(preg_replace('/\t+/', '', $vhost_content)))); $vhost_usr = explode("\n", preg_replace('/[ \t]+/', ' ', trim(preg_replace('/\t+/', '', $vhost_usr)))); // Break into array items
$vhost_content = array_filter($vhost_content, create_function('$a','return preg_match("#\S#", $a);')); $vhost_usr = array_filter($vhost_usr, create_function('$a','return preg_match("#\S#", $a);')); // Remove empty lines
// Merge similar blocks // Cycle through the user defined settings
$new_vhost_content = array(); $currentBlock = array();
$isOpen = false; $blockLevel = 0;
$addAfter = false; foreach ($vhost_usr as $line) {
foreach ($vhost_content as $line) {
$line = trim($line); $line = trim($line);
$currentBlock[] = $line;
if (substr_count($line, "{") != 0 && substr_count($line, "}") == 0 && substr_count($line, "server") == 0 && $isOpen === false) { if (strpos($line, "{") !== false) {
$isOpen = true; $blockLevel++;
$addAfter = array_search($line, $new_vhost_content); }
if ($addAfter === false) { if (strpos($line, "}") !== false && $blockLevel > 0) {
$new_vhost_content[] = $line; $blockLevel--;
} }
} elseif ($isOpen === true) {
if (substr_count($line, "}") != 0 && substr_count($line, "{") == 0) { if ($line == "}" && $blockLevel == 0) {
$isOpen = false; if (in_array($currentBlock[0], $vhost_frx)) {
if ($addAfter === false) { // Add to existing block
$new_vhost_content[] = "}"; $pos = array_search($currentBlock[0], $vhost_frx);
} else { do {
$addAfter = false; $pos++;
} while ($vhost_frx[$pos] != "}");
for ($i = 1; $i < count($currentBlock) - 1; $i++) {
array_splice($vhost_frx, $pos + $i - 2, 0, $currentBlock[$i]);
} }
} else { } else {
if ($addAfter != false) { // Add to end
for ($i = $addAfter; $i < count($new_vhost_content); $i++) { array_splice($vhost_frx, count($vhost_frx), 0, $currentBlock);
if ($new_vhost_content[$i] == "}") {
$addAt = $i;
break;
}
}
array_splice($new_vhost_content, $addAt, 0, $line);
} else {
$new_vhost_content[] = $line;
}
} }
} else { $currentBlock = array();
$new_vhost_content[] = $line; } elseif ($blockLevel == 0) {
array_splice($vhost_frx, count($vhost_frx), 0, $currentBlock);
$currentBlock = array();
} }
} }
// Fix idention
$nextLevel = 0; $nextLevel = 0;
for ($i = 0; $i < count($new_vhost_content); $i++) { for ($i = 0; $i < count($vhost_frx); $i++) {
if (substr_count($new_vhost_content[$i], "}") != 0 && substr_count($new_vhost_content[$i], "{") == 0) { if (substr_count($vhost_frx[$i], "}") != 0 && substr_count($vhost_frx[$i], "{") == 0) {
$nextLevel -= 1; $nextLevel -= 1;
} }
if ($nextLevel > 0) { if ($nextLevel > 0) {
for ($j = 0; $j < $nextLevel; $j++) { for ($j = 0; $j < $nextLevel; $j++) {
$new_vhost_content[$i] = " " . $new_vhost_content[$i]; $vhost_frx[$i] = " " . $vhost_frx[$i];
} }
} }
if (substr_count($new_vhost_content[$i], "{") != 0 && substr_count($new_vhost_content[$i], "}") == 0) { if (substr_count($vhost_frx[$i], "{") != 0 && substr_count($vhost_frx[$i], "}") == 0) {
$nextLevel += 1; $nextLevel += 1;
} }
} }
return implode("\n", $new_vhost_content); return implode("\n", $vhost_frx);
} }
protected function composeSslSettings($domain) { protected function composeSslSettings($domain) {
$sslsettings = ''; $sslsettings = '';