001: /*
002: * This file is part of DrFTPD, Distributed FTP Daemon.
003: *
004: * DrFTPD is free software; you can redistribute it and/or modify
005: * it under the terms of the GNU General Public License as published by
006: * the Free Software Foundation; either version 2 of the License, or
007: * (at your option) any later version.
008: *
009: * DrFTPD is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
012: * GNU General Public License for more details.
013: *
014: * You should have received a copy of the GNU General Public License
015: * along with DrFTPD; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: */
018: package net.sf.drftpd.master.config;
019:
020: import java.io.FileInputStream;
021: import java.io.IOException;
022: import java.util.Properties;
023:
024: import net.sf.drftpd.master.config.FtpConfig;
025:
026: import org.apache.log4j.Logger;
027: import org.apache.oro.text.GlobCompiler;
028: import org.apache.oro.text.regex.MalformedPatternException;
029: import org.drftpd.GlobalContext;
030: import org.drftpd.permissions.GlobPathPermission;
031: import org.drftpd.remotefile.LinkedRemoteFileInterface;
032: import org.drftpd.usermanager.User;
033:
034: import com.Ostermiller.util.StringTokenizer;
035:
036: /**
037: * @author Teflon
038: * @version $Id$
039: */
040: public class ZipscriptConfig {
041: private static final Logger logger = Logger
042: .getLogger(ZipscriptConfig.class);
043:
044: protected GlobalContext _gctx;
045:
046: private String zsConf = "conf/zipscript.conf";
047:
048: private boolean _statusBarEnabled;
049:
050: private boolean _offlineFilesEnabled;
051:
052: private boolean _missingFilesEnabled;
053:
054: private boolean _id3Enabled;
055:
056: private boolean _dizEnabled;
057:
058: private boolean _raceStatsEnabled;
059:
060: private boolean _restrictSfvEnabled;
061:
062: private boolean _multiSfvAllowed;
063:
064: private boolean _SfvFirstRequired;
065:
066: private boolean _SfvFirstAllowNoExt;
067:
068: private String _AllowedExts;
069:
070: private String _SfvFirstUsers;
071:
072: public ZipscriptConfig(GlobalContext gctx) throws IOException {
073: _gctx = gctx;
074: Properties cfg = new Properties();
075: FileInputStream stream = null;
076: try {
077: stream = new FileInputStream(zsConf);
078: cfg.load(stream);
079: loadConfig(cfg);
080: } finally {
081: if (stream != null) {
082: stream.close();
083: }
084: }
085: }
086:
087: public void loadConfig(Properties cfg) throws IOException {
088: _statusBarEnabled = cfg.getProperty("statusbar.enabled") == null ? true
089: : cfg.getProperty("statusbar.enabled")
090: .equalsIgnoreCase("true");
091: _offlineFilesEnabled = cfg.getProperty("files.offline.enabled") == null ? true
092: : cfg.getProperty("files.offline.enabled")
093: .equalsIgnoreCase("true");
094: _missingFilesEnabled = cfg.getProperty("files.missing.enabled") == null ? true
095: : cfg.getProperty("files.missing.enabled")
096: .equalsIgnoreCase("true");
097: _id3Enabled = cfg.getProperty("cwd.id3info.enabled") == null ? true
098: : cfg.getProperty("cwd.id3info.enabled")
099: .equalsIgnoreCase("true");
100: _dizEnabled = cfg.getProperty("cwd.dizinfo.enabled") == null ? true
101: : cfg.getProperty("cwd.dizinfo.enabled")
102: .equalsIgnoreCase("true");
103: _raceStatsEnabled = cfg.getProperty("cwd.racestats.enabled") == null ? true
104: : cfg.getProperty("cwd.racestats.enabled")
105: .equalsIgnoreCase("true");
106: _restrictSfvEnabled = cfg.getProperty("sfv.restrict.files") == null ? false
107: : cfg.getProperty("sfv.restrict.files")
108: .equalsIgnoreCase("true");
109: _multiSfvAllowed = cfg.getProperty("allow.multi.sfv") == null ? true
110: : cfg.getProperty("allow.multi.sfv").equalsIgnoreCase(
111: "true");
112: _SfvFirstRequired = cfg.getProperty("sfvfirst.required") == null ? true
113: : cfg.getProperty("sfvfirst.required")
114: .equalsIgnoreCase("true");
115: _SfvFirstAllowNoExt = cfg.getProperty("sfvfirst.allownoext") == null ? true
116: : cfg.getProperty("sfvfirst.allownoext")
117: .equalsIgnoreCase("true");
118: _AllowedExts = cfg.getProperty("allowedexts") == null ? "sfv"
119: : cfg.getProperty("allowedexts").toLowerCase().trim()
120: + " sfv";
121: _SfvFirstUsers = cfg.getProperty("sfvfirst.users") == null ? "*"
122: : cfg.getProperty("sfvfirst.users");
123:
124: // Locals
125: String SfvFirstPathIgnore = cfg
126: .getProperty("sfvfirst.pathignore") == null ? "*" : cfg
127: .getProperty("sfvfirst.pathignore");
128: String SfvFirstPathCheck = cfg
129: .getProperty("sfvfirst.pathcheck") == null ? "*" : cfg
130: .getProperty("sfvfirst.pathcheck");
131:
132: // SFV First PathPermissions
133: if (_SfvFirstRequired) {
134: try {
135: // this one gets perms defined in sfvfirst.users
136: StringTokenizer st = new StringTokenizer(
137: SfvFirstPathCheck, " ");
138: while (st.hasMoreTokens()) {
139: _gctx.getConfig().addPathPermission(
140: "sfvfirst.pathcheck",
141: new GlobPathPermission(new GlobCompiler()
142: .compile(st.nextToken()), FtpConfig
143: .makeUsers(new StringTokenizer(
144: _SfvFirstUsers, " "))));
145: }
146: st = new StringTokenizer(SfvFirstPathIgnore, " ");
147: while (st.hasMoreTokens()) {
148: _gctx.getConfig().addPathPermission(
149: "sfvfirst.pathignore",
150: new GlobPathPermission(new GlobCompiler()
151: .compile(st.nextToken()), FtpConfig
152: .makeUsers(new StringTokenizer("*",
153: " "))));
154: }
155: } catch (MalformedPatternException e) {
156: logger.warn("Exception when reading " + zsConf, e);
157: }
158: }
159: }
160:
161: public GlobalContext getGlobalContext() {
162: return _gctx;
163: }
164:
165: public boolean id3Enabled() {
166: return _id3Enabled;
167: }
168:
169: public boolean dizEnabled() {
170: return _dizEnabled;
171: }
172:
173: public boolean missingFilesEnabled() {
174: return _missingFilesEnabled;
175: }
176:
177: public boolean multiSfvAllowed() {
178: return _multiSfvAllowed;
179: }
180:
181: public boolean offlineFilesEnabled() {
182: return _offlineFilesEnabled;
183: }
184:
185: public boolean raceStatsEnabled() {
186: return _raceStatsEnabled;
187: }
188:
189: public boolean restrictSfvEnabled() {
190: return _restrictSfvEnabled;
191: }
192:
193: public boolean statusBarEnabled() {
194: return _statusBarEnabled;
195: }
196:
197: public boolean checkAllowedExtension(String file) {
198: if (_SfvFirstAllowNoExt && !file.contains(".")) {
199: return true;
200: }
201: StringTokenizer st = new StringTokenizer(_AllowedExts, " ");
202: while (st.hasMoreElements()) {
203: String ext = "."
204: + st.nextElement().toString().toLowerCase();
205: if (file.toLowerCase().endsWith(ext)) {
206: return true;
207: }
208: }
209: return false;
210: }
211:
212: public boolean checkSfvFirstEnforcedPath(
213: LinkedRemoteFileInterface dir, User user) {
214: if (_SfvFirstRequired
215: && _gctx.getConfig().checkPathPermission(
216: "sfvfirst.pathcheck", user, dir)
217: && !_gctx.getConfig().checkPathPermission(
218: "sfvfirst.pathignore", user, dir)) {
219: return true;
220: }
221: return false;
222: }
223: }
|