Docker formatting / cleanup

This commit is contained in:
Eric Schultz
2016-03-21 19:10:28 -05:00
parent 45ed2b32c7
commit c8a595f51e

View File

@@ -387,12 +387,12 @@ class DockerUpdate{
private function xml_encode($string) {
return htmlspecialchars($string, ENT_XML1, 'UTF-8');
return htmlspecialchars($string, ENT_XML1, 'UTF-8');
}
private function xml_decode($string) {
return strval(html_entity_decode($string, ENT_XML1, 'UTF-8'));
return strval(html_entity_decode($string, ENT_XML1, 'UTF-8'));
}
@@ -525,80 +525,85 @@ class DockerUpdate{
public function updateUserTemplate($Container) {
$changed = false;
$DockerTemplates = new DockerTemplates();
$validElements = array( 0 => "Support",
1 => "Overview",
2 => "Category",
3 => "WebUI",
4 => "Icon");
$validElements = [
0 => "Support",
1 => "Overview",
2 => "Category",
3 => "WebUI",
4 => "Icon"
];
$validAttributes = [
0 => "Name",
1 => "Default",
2 => "Description",
3 => "Display",
4 => "Required",
5 => "Mask"
];
$validAttributes = array( 0 => "Name",
1 => "Default",
2 => "Description",
3 => "Display",
4 => "Required",
5 => "Mask");
// Get user template file and abort if fail
if ( ! $file = $DockerTemplates->getUserTemplate($Container) ) return null;
// Load user template XML, verify if it's valid and abort if doesn't have TemplateURL element
$template = @simplexml_load_file($file);
if ( $template && ! isset($template->TemplateURL) ) return null;
// Load a user template DOM for import remote template new Config
if ( $template && ! isset($template->TemplateURL) ) return null;
// Load a user template DOM for import remote template new Config
$dom_local = dom_import_simplexml($template);
// Try to download the remote template and abort if it fail.
if (! $dl = $this->download_url($this->xml_decode($template->TemplateURL))) return null;
// Try to load the downloaded template and abort if fail.
if (! $remote_template = @simplexml_load_string($dl)) return null;
// Loop through remote template elements and compare them to local ones
foreach ($remote_template->children() as $name => $remote_element) {
$name = $this->xml_decode($name);
if (! $dl = $this->download_url($this->xml_decode($template->TemplateURL))) return null;
// Try to load the downloaded template and abort if fail.
if (! $remote_template = @simplexml_load_string($dl)) return null;
// Loop through remote template elements and compare them to local ones
foreach ($remote_template->children() as $name => $remote_element) {
$name = $this->xml_decode($name);
// Compare through validElements
if ($name != "Config" && in_array($name, $validElements)) {
if ($name != "Config" && in_array($name, $validElements)) {
$local_element = $template->xpath("//$name")[0];
$rvalue = $this->xml_decode($remote_element);
$value = $this->xml_decode($local_element);
// Values changed, updating.
if ( $value != $rvalue) {
$local_element->{0} = $this->xml_encode($rvalue);
$changed = true;
}
// Compare atributes on Config if they are in the validAttributes list
} else if ($name == "Config"){
if ($value != $rvalue) {
$local_element->{0} = $this->xml_encode($rvalue);
$changed = true;
}
// Compare atributes on Config if they are in the validAttributes list
} else if ($name == "Config") {
$type = $this->xml_decode($remote_element['Type']);
$target = $this->xml_decode($remote_element['Target']);
if ($type == "Port") {
$mode = $this->xml_decode($remote_element['Mode']);
$local_element = $template->xpath("//Config[@Type='$type'][@Target='$target'][@Mode='$mode']")[0];
} else {
$local_element = $template->xpath("//Config[@Type='$type'][@Target='$target']")[0];
}
// If the local template already have the pertinent Config element, loop through it's attributes and update those on validAttributes
if (! empty($local_element)) {
foreach ($remote_element->attributes() as $key => $value) {
$rvalue = $this->xml_decode($value);
$value = $this->xml_decode($local_element[$key]);
// Values changed, updating.
if ($value != $rvalue && in_array($key, $validAttributes)) {
$local_element[$key] = $this->xml_encode($rvalue);
$changed = true;
}
}
// New Config element, add it to the local template
} else {
if ($type == "Port") {
$mode = $this->xml_decode($remote_element['Mode']);
$local_element = $template->xpath("//Config[@Type='$type'][@Target='$target'][@Mode='$mode']")[0];
} else {
$local_element = $template->xpath("//Config[@Type='$type'][@Target='$target']")[0];
}
// If the local template already have the pertinent Config element, loop through it's attributes and update those on validAttributes
if (! empty($local_element)) {
foreach ($remote_element->attributes() as $key => $value) {
$rvalue = $this->xml_decode($value);
$value = $this->xml_decode($local_element[$key]);
// Values changed, updating.
if ($value != $rvalue && in_array($key, $validAttributes)) {
$local_element[$key] = $this->xml_encode($rvalue);
$changed = true;
}
}
// New Config element, add it to the local template
} else {
$dom_remote = dom_import_simplexml($remote_element);
$new_element = $dom_local->ownerDocument->importNode($dom_remote, TRUE);
$new_element = $dom_local->ownerDocument->importNode($dom_remote, true);
$dom_local->appendChild($new_element);
$changed = true;
}
}
}
if ($changed) {
// Format output and save to file if there were any commited changes
$dom = new DOMDocument('1.0');
$dom->preserveWhiteSpace = false;
$dom->formatOutput = true;
$dom->loadXML($template->asXML());
file_put_contents($file, $dom->saveXML());
}
$changed = true;
}
}
}
if ($changed) {
// Format output and save to file if there were any commited changes
$dom = new DOMDocument('1.0');
$dom->preserveWhiteSpace = false;
$dom->formatOutput = true;
$dom->loadXML($template->asXML());
file_put_contents($file, $dom->saveXML());
}
}
}