package org.keycloak.theme;

import org.keycloak.freemarker.Theme;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.Locale;
import java.util.Properties;

/**
 * @author <a href="mailto:sthorger@redhat.com">Stian Thorgersen</a>
 */
public class FolderTheme implements Theme {

    private String parentName;
    private String importName;
    private File themeDir;
    private String name;
    private Type type;
    private final Properties properties;

    public FolderTheme(File themeDir, String name, Type type) throws IOException {
        this.themeDir = themeDir;
        this.name = name;
        this.type = type;
        this.properties = new Properties();

        File propertiesFile = new File(themeDir, "theme.properties");
        if (propertiesFile .isFile()) {
            properties.load(new FileInputStream(propertiesFile));
            parentName = properties.getProperty("parent");
            importName = properties.getProperty("import");
        }
    }

    @Override
    public String getName() {
        return name;
    }

    @Override
    public String getParentName() {
        return parentName;
    }

    @Override
    public String getImportName() {
        return importName;
    }

    @Override
    public Type getType() {
        return type;
    }

    @Override
    public URL getTemplate(String name) throws IOException {
        File file = new File(themeDir, name);
        return file.isFile() ? file.toURI().toURL() : null;
    }

    @Override
    public InputStream getTemplateAsStream(String name) throws IOException {
        URL url = getTemplate(name);
        return url != null ? url.openStream() : null;
    }

    @Override
    public URL getResource(String path) throws IOException {
        if (File.separatorChar != '/') {
            path = path.replace('/', File.separatorChar);
        }
        File file = new File(themeDir, "/resources/" + path);
        return file.isFile() ? file.toURI().toURL() : null;
    }

    @Override
    public InputStream getResourceAsStream(String path) throws IOException {
        URL url = getResource(path);
        return url != null ? url.openStream() : null;
    }

    @Override
    public Properties getMessages(Locale locale) throws IOException {
        if(locale == null){
            return null;
        }

        Properties m = new Properties();

        File file = new File(themeDir, "messages" + File.separator + "messages_" + locale.toString() + ".properties");
        if (file.isFile()) {
            m.load(new FileInputStream(file));
        }
        return m;
    }

    @Override
    public Properties getProperties() {
        return properties;
    }
}

@@ -453,24 +453,55 @@
 e themeDir;%0A
+    private File resourcesDir;%0A
     private 
@@ -509,24 +509,24 @@
 tring name;%0A
-
     private 
@@ -1091,24 +1091,81 @@
 );%0A        %7D
+%0A%0A        resourcesDir = new File(themeDir, %22resources%22);
 %0A    %7D%0A%0A    
@@ -2023,32 +2023,33 @@
 har);%0A        %7D%0A
+%0A
         File fil
@@ -2065,71 +2065,191 @@
 ile(
-themeDir, %22/resources/%22 + path);%0A        return file.isFile() ?
+resourcesDir, path);%0A        if (!file.isFile() %7C%7C !file.getCanonicalPath().startsWith(resourcesDir.getCanonicalPath())) %7B%0A            return null;%0A        %7D else %7B%0A            return
  fil
@@ -2261,32 +2261,35 @@
 RI().toURL()
- : null;
+;%0A        %7D
 %0A    %7D%0A%0A    

Fix loading resources from theme