Skip to content

Commit

Permalink
Add whitelist function
Browse files Browse the repository at this point in the history
  • Loading branch information
SharkHunter committed Oct 23, 2013
1 parent a2a0f95 commit 564af02
Show file tree
Hide file tree
Showing 8 changed files with 122 additions and 39 deletions.
8 changes: 6 additions & 2 deletions src/main/java/net/pms/PMS.java
Expand Up @@ -861,13 +861,17 @@ public File[] getFoldersConf() {
* @return {@link java.io.File}[] Array of directories.
* @throws java.io.IOException
*/
public File[] getSharedFoldersArray(boolean monitored) {
public File[] getSharedFoldersArray(boolean monitored) {
return getSharedFoldersArray(monitored, null);
}

public File[] getSharedFoldersArray(boolean monitored, ArrayList<String> tags) {
String folders;

if (monitored) {
folders = getConfiguration().getFoldersMonitored();
} else {
folders = getConfiguration().getFolders();
folders = getConfiguration().getFolders(tags);
}

if (folders == null || folders.length() == 0) {
Expand Down
12 changes: 6 additions & 6 deletions src/main/java/net/pms/configuration/MapFileConfiguration.java
Expand Up @@ -64,15 +64,15 @@ public MapFileConfiguration() {

@Deprecated
public static List<MapFileConfiguration> parse(String conf) {
return parseVirtualFolders();
return parseVirtualFolders(null);
}

public static List<MapFileConfiguration> parseVirtualFolders() {
public static List<MapFileConfiguration> parseVirtualFolders(ArrayList<String> tags) {
String conf;

if (configuration.getVirtualFoldersFile().trim().length() > 0) {
if (configuration.getVirtualFoldersFile(tags).trim().length() > 0) {
// Get the virtual folder info from the user's file
conf = configuration.getVirtualFoldersFile().trim().replaceAll("&comma;", ",");
conf = configuration.getVirtualFoldersFile(tags).trim().replaceAll("&comma;", ",");
File file = new File(conf);
conf = null;

Expand All @@ -92,9 +92,9 @@ public static List<MapFileConfiguration> parseVirtualFolders() {
Type listType = (new TypeToken<ArrayList<MapFileConfiguration>>() { }).getType();
List<MapFileConfiguration> out = gson.fromJson(conf, listType);
return out;
} else if (configuration.getVirtualFolders().trim().length() > 0) {
} else if (configuration.getVirtualFolders(tags).trim().length() > 0) {
// Get the virtual folder info from the config string
conf = configuration.getVirtualFolders().trim().replaceAll("&comma;", ",");
conf = configuration.getVirtualFolders(tags).trim().replaceAll("&comma;", ",");
String jsonStringFromConf = "";

// Convert our syntax into JSON syntax
Expand Down
58 changes: 46 additions & 12 deletions src/main/java/net/pms/configuration/PmsConfiguration.java
Expand Up @@ -1557,8 +1557,8 @@ public void setAutoloadExternalSubtitles(boolean value) {
*
* @return True if PMS should hide the folder, false othewise.
*/
public boolean getHideVideoSettings() {
return getBoolean(KEY_HIDE_VIDEO_SETTINGS, true);
public boolean getHideVideoSettings(ArrayList<String> tags) {
return tagLoopBool(tags, ".hide_set", KEY_HIDE_VIDEO_SETTINGS, true);
}

/**
Expand Down Expand Up @@ -1966,8 +1966,8 @@ public void save() throws ConfigurationException {
LOGGER.info("Configuration saved to: " + PROFILE_PATH);
}

public String getFolders() {
return getString(KEY_FOLDERS, "");
public String getFolders(ArrayList<String> tags) {
return tagLoop(tags, ".folders", KEY_FOLDERS);
}

public void setFolders(String value) {
Expand Down Expand Up @@ -2369,16 +2369,16 @@ public void setRunWizard(boolean value) {
configuration.setProperty(KEY_RUN_WIZARD, value);
}

public boolean isHideNewMediaFolder() {
return getBoolean(KEY_HIDE_NEW_MEDIA_FOLDER, false);
public boolean isHideNewMediaFolder(ArrayList<String> tags) {
return tagLoopBool(tags, ".new_media", KEY_HIDE_NEW_MEDIA_FOLDER, false);
}

public void setHideNewMediaFolder(final boolean value) {
this.configuration.setProperty(KEY_HIDE_NEW_MEDIA_FOLDER, value);
}

public boolean isHideRecentlyPlayedFolder() {
return getBoolean(PmsConfiguration.KEY_HIDE_RECENTLY_PLAYED_FOLDER, false);
public boolean isHideRecentlyPlayedFolder(ArrayList<String> tags) {
return tagLoopBool(tags, ".recent", KEY_HIDE_RECENTLY_PLAYED_FOLDER, false);
}

public void setHideRecentlyPlayedFolder(final boolean value) {
Expand Down Expand Up @@ -2439,12 +2439,12 @@ public void setRendererForceDefault(boolean value) {
configuration.setProperty(KEY_RENDERER_FORCE_DEFAULT, value);
}

public String getVirtualFolders() {
return getString(KEY_VIRTUAL_FOLDERS, "");
public String getVirtualFolders(ArrayList<String> tags) {
return tagLoop(tags, ".vfolders", KEY_VIRTUAL_FOLDERS);
}

public String getVirtualFoldersFile() {
return getString(KEY_VIRTUAL_FOLDERS_FILE, "");
public String getVirtualFoldersFile(ArrayList<String> tags) {
return tagLoop(tags, ".vfolders.file", KEY_VIRTUAL_FOLDERS_FILE);
}

public String getProfilePath() {
Expand Down Expand Up @@ -2839,4 +2839,38 @@ public int getResumeKeepTime() {
public boolean hideSubInfo() {
return getBoolean(KEY_HIDE_SUBS_INFO, false);
}

public String getPlugins(ArrayList<String> tags) {
return tagLoop(tags, ".plugins", "dummy");
}

public boolean isHideWebFolder(ArrayList<String> tags) {
return tagLoopBool(tags, ".web", "dummy" , false);
}

private String tagLoop(ArrayList<String> tags, String suff, String fallback) {
if(tags == null || tags.isEmpty()) {
// no tags use fallback
return getString(fallback, "");
}
for(String tag : tags) {
String x = (tag.toLowerCase() + suff).replaceAll(" ", "_");
String res = getString(x, "");
if(StringUtils.isNotBlank(res)) {
// use first tag found
return res;
}
}
// down here no matching tag was found
// return fallback
return getString(fallback, "");
}

private boolean tagLoopBool(ArrayList<String> tags, String suff, String fallback, boolean def) {
String b = tagLoop(tags, suff, fallback);
if(StringUtils.isBlank(b)) {
return def;
}
return b.trim().equalsIgnoreCase("true");
}
}
10 changes: 9 additions & 1 deletion src/main/java/net/pms/configuration/RendererConfiguration.java
Expand Up @@ -262,7 +262,15 @@ public static void resetAllRenderers() {

public RootFolder getRootFolder() {
if (rootFolder == null) {
rootFolder = new RootFolder();
ArrayList<String> tags = new ArrayList();
tags.add(getRendererName());
for(InetAddress sa : addressAssociation.keySet()) {
if(addressAssociation.get(sa) == this) {
tags.add(sa.getHostAddress());
}
}

rootFolder = new RootFolder(tags);
if (pmsConfiguration.getUseCache()) {
rootFolder.discoverChildren();
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/net/pms/dlna/DLNAResource.java
Expand Up @@ -561,7 +561,7 @@ public void addChild(DLNAResource child) {
// is preferred.
String name = getName();

if (!configuration.isHideRecentlyPlayedFolder()) {
if (!configuration.isHideRecentlyPlayedFolder(null)) {
player = child.getPlayer();
} else {
for (Player p : PlayerFactory.getPlayers()) {
Expand Down
63 changes: 50 additions & 13 deletions src/main/java/net/pms/dlna/RootFolder.java
Expand Up @@ -58,11 +58,18 @@ public class RootFolder extends DLNAResource {
private FolderLimit lim;
private MediaMonitor mon;
private RecentlyPlayed last;
private ArrayList<String> tags;

public RootFolder() {
public RootFolder(ArrayList<String> tags) {
setIndexId(0);
this.tags = tags;
}

public RootFolder() {
this(null);
}


@Override
public InputStream getInputStream() {
return null;
Expand Down Expand Up @@ -99,12 +106,12 @@ public void discoverChildren() {
return;
}

if (!configuration.isHideRecentlyPlayedFolder()) {
if (!configuration.isHideRecentlyPlayedFolder(tags)) {
last = new RecentlyPlayed();
addChild(last);
}

if (!configuration.isHideNewMediaFolder()) {
if (!configuration.isHideNewMediaFolder(tags)) {
String m = (String) configuration.getFoldersMonitored();
if (!StringUtils.isEmpty(m)) {
String[] tmp = m.split(",");
Expand All @@ -122,22 +129,22 @@ public void discoverChildren() {
addChild(lim);
}

for (DLNAResource r : getConfiguredFolders()) {
for (DLNAResource r : getConfiguredFolders(tags)) {
addChild(r);
}

for (DLNAResource r : getVirtualFolders()) {
for (DLNAResource r : getVirtualFolders(tags)) {
addChild(r);
}

if (configuration.getSearchFolder()) {
SearchFolder sf = new SearchFolder("Search disc folders", new FileSearch(getConfiguredFolders()));
SearchFolder sf = new SearchFolder("Search disc folders", new FileSearch(getConfiguredFolders(null)));
addChild(sf);
}

String webConfPath = configuration.getWebConfPath();
File webConf = new File(webConfPath);
if (webConf.exists() && configuration.getExternalNetwork()) {
if (webConf.exists() && configuration.getExternalNetwork() && !configuration.isHideWebFolder(tags)) {
addWebFolder(webConf);
}

Expand Down Expand Up @@ -173,7 +180,7 @@ public void discoverChildren() {
addChild(r);
}

if (!configuration.getHideVideoSettings()) {
if (!configuration.getHideVideoSettings(tags)) {
addAdminFolder();
}

Expand Down Expand Up @@ -253,9 +260,9 @@ private synchronized void scan(DLNAResource resource) {
}
}

private List<RealFile> getConfiguredFolders() {
private List<RealFile> getConfiguredFolders(ArrayList<String> tags) {
List<RealFile> res = new ArrayList<>();
File[] files = PMS.get().getSharedFoldersArray(false);
File[] files = PMS.get().getSharedFoldersArray(false, tags);

if (files == null || files.length == 0) {
files = File.listRoots();
Expand All @@ -268,9 +275,9 @@ private List<RealFile> getConfiguredFolders() {
return res;
}

private List<DLNAResource> getVirtualFolders() {
private List<DLNAResource> getVirtualFolders(ArrayList<String> tags) {
List<DLNAResource> res = new ArrayList<>();
List<MapFileConfiguration> mapFileConfs = MapFileConfiguration.parseVirtualFolders();
List<MapFileConfiguration> mapFileConfs = MapFileConfiguration.parseVirtualFolders(tags);

if (mapFileConfs != null) {
for (MapFileConfiguration f : mapFileConfs) {
Expand Down Expand Up @@ -1139,7 +1146,7 @@ public boolean enable() {
private DLNAResource getVideoSettingsFolder() {
DLNAResource res = null;

if (!configuration.getHideVideoSettings()) {
if (!configuration.getHideVideoSettings(tags)) {
res = new VirtualFolder(Messages.getString("PMS.37"), null);
VirtualFolder vfSub = new VirtualFolder(Messages.getString("PMS.8"), null);
res.addChild(vfSub);
Expand Down Expand Up @@ -1262,8 +1269,17 @@ public boolean enable() {
*/
private List<DLNAResource> getAdditionalFoldersAtRoot() {
List<DLNAResource> res = new ArrayList<>();
String[] legalPlugs = null;
String tmp = configuration.getPlugins(tags);
if(StringUtils.isNotBlank(tmp)) {
legalPlugs = tmp.split(",");
}

for (ExternalListener listener : ExternalFactory.getExternalListeners()) {
if(illegalPlugin(legalPlugs, listener.name())) {
LOGGER.debug("plugin " + listener.name() + " is not legal for render");
continue;
}
if (listener instanceof AdditionalFolderAtRoot) {
AdditionalFolderAtRoot afar = (AdditionalFolderAtRoot) listener;

Expand Down Expand Up @@ -1319,4 +1335,25 @@ public void stopPlaying(DLNAResource res) {
last.add(res);
}
}

private boolean illegalPlugin(String[] plugs, String name) {
if(StringUtils.isBlank(name)) {
if(plugs == null || plugs.length == 0) {
// only allowed without plugins filter
return false;
}
else {
return true;
}
}
if(plugs == null || plugs.length == 0) {
return false;
}
for(String p : plugs) {
if(name.equals(p)) {
return false;
}
}
return true;
}
}
2 changes: 1 addition & 1 deletion src/main/java/net/pms/newgui/DbgPacker.java
Expand Up @@ -113,7 +113,7 @@ private void poll() {
}
// add core items with debug.log last (LinkedHashMap preserves insertion order)
String profileDirectory = configuration.getProfileDirectory();
String vfolders = configuration.getVirtualFolders();
String vfolders = configuration.getVirtualFolders(null);
if (StringUtils.isNotEmpty(vfolders) && vfolders.startsWith("@")) {
add(new File(vfolders.substring(1)));
}
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/net/pms/newgui/NavigationShareTab.java
Expand Up @@ -354,7 +354,7 @@ public void actionPerformed(ActionEvent e) {
// Hide Server Settings folder
hidevideosettings = new JCheckBox(Messages.getString("FoldTab.38"));
hidevideosettings.setContentAreaFilled(false);
if (configuration.getHideVideoSettings()) {
if (configuration.getHideVideoSettings(null)) {
hidevideosettings.setSelected(true);
}
hidevideosettings.addItemListener(new ItemListener() {
Expand Down Expand Up @@ -622,7 +622,7 @@ public void itemStateChanged(ItemEvent e) {

newmediafolder = new JCheckBox(Messages.getString("FoldTab.54"));
newmediafolder.setContentAreaFilled(false);
if (configuration.isHideNewMediaFolder()) {
if (configuration.isHideNewMediaFolder(null)) {
newmediafolder.setSelected(true);
}
newmediafolder.addItemListener(new ItemListener() {
Expand All @@ -634,7 +634,7 @@ public void itemStateChanged(ItemEvent e) {

recentlyplayedfolder = new JCheckBox(Messages.getString("FoldTab.55"));
recentlyplayedfolder.setContentAreaFilled(false);
if (configuration.isHideRecentlyPlayedFolder()) {
if (configuration.isHideRecentlyPlayedFolder(null)) {
recentlyplayedfolder.setSelected(true);
}
recentlyplayedfolder.addItemListener(new ItemListener() {
Expand Down

0 comments on commit 564af02

Please sign in to comment.