001: package org.claros.intouch.webdisk.controllers;
002:
003: import java.io.File;
004: import java.io.FileInputStream;
005: import java.io.FileOutputStream;
006: import java.util.TreeSet;
007:
008: import org.apache.commons.logging.Log;
009: import org.apache.commons.logging.LogFactory;
010: import org.claros.commons.configuration.Paths;
011: import org.claros.commons.configuration.PropertyFile;
012: import org.claros.commons.exception.NoPermissionException;
013: import org.claros.commons.utility.Utility;
014: import org.claros.intouch.webdisk.models.ClarosWebDskFile;
015: import org.claros.intouch.webdisk.models.ClarosWebDskFolder;
016:
017: public class WebdiskController {
018: private static String homeBase;
019: private static Log log = LogFactory.getLog(WebdiskController.class);
020:
021: /**
022: *
023: * @param username
024: * @return
025: * @throws Exception
026: */
027: public static File getUserHome(String username) throws Exception {
028: String domain = "localhost";
029: if (username.indexOf("@") > 0) {
030: domain = username.substring(username.indexOf("@") + 1);
031: username = username.substring(0, username.indexOf("@"));
032: }
033:
034: // make sure domain dir exists at the file system
035: File fDomain = new File(homeBase + "/" + domain);
036: if (!fDomain.exists()) {
037: fDomain.mkdir();
038: }
039:
040: // access the directory.
041: String firstLetter = username.substring(0, 1);
042:
043: File f = new File(homeBase + "/" + domain + "/" + firstLetter);
044: if (!f.exists()) {
045: f.mkdir();
046: }
047: f = new File(homeBase + "/" + domain + "/" + firstLetter + "/"
048: + username);
049: if (!f.exists()) {
050: f.mkdir();
051: }
052:
053: String uploadStr = PropertyFile.getConfiguration(
054: "/config/config.xml").getString(
055: "webdisk.upload-dir-name");
056: File uploadDir = new File(homeBase + "/" + domain + "/"
057: + firstLetter + "/" + username + "/" + uploadStr);
058: if (!uploadDir.exists()) {
059: uploadDir.mkdir();
060:
061: String tmp = Paths.getCfgFolder() + "upload-readme.txt";
062: FileOutputStream fo = new FileOutputStream(uploadDir
063: + "/readme.txt");
064: FileInputStream fis = new FileInputStream(tmp);
065: int i = -1;
066: while ((i = fis.read()) != -1) {
067: fo.write(i);
068: }
069: fo.close();
070: fis.close();
071: }
072:
073: return f;
074: }
075:
076: /**
077: *
078: * @param username
079: * @return
080: * @throws Exception
081: */
082: public static TreeSet getUserFiles(String username)
083: throws Exception {
084: File home = getUserHome(username);
085:
086: TreeSet set = new TreeSet();
087: getFolderContents(username, home, set);
088: return set;
089: }
090:
091: public static File getUploadDir(String username) throws Exception {
092: String domain = "localhost";
093: if (username.indexOf("@") > 0) {
094: domain = username.substring(username.indexOf("@") + 1);
095: username = username.substring(0, username.indexOf("@"));
096: }
097:
098: // make sure domain dir exists at the file system
099: File fDomain = new File(homeBase + "/" + domain);
100: if (!fDomain.exists()) {
101: fDomain.mkdir();
102: }
103:
104: // access the directory.
105: String firstLetter = username.substring(0, 1);
106:
107: File f = new File(homeBase + "/" + domain + "/" + firstLetter);
108: if (!f.exists()) {
109: f.mkdir();
110: }
111: f = new File(homeBase + "/" + domain + "/" + firstLetter + "/"
112: + username);
113: if (!f.exists()) {
114: f.mkdir();
115: }
116:
117: String uploadStr = PropertyFile.getConfiguration(
118: "/config/config.xml").getString(
119: "webdisk.upload-dir-name");
120: File uploadDir = new File(homeBase + "/" + domain + "/"
121: + firstLetter + "/" + username + "/" + uploadStr);
122: if (!uploadDir.exists()) {
123: uploadDir.mkdir();
124: }
125: return uploadDir;
126: }
127:
128: /**
129: *
130: * @param username
131: * @param path
132: * @return
133: * @throws NoPermissionException
134: * @throws Exception
135: */
136: public static void getFolderContents(String username, File obj,
137: TreeSet out) throws NoPermissionException, Exception {
138: checkPathPermissions(username, obj.getAbsolutePath());
139:
140: if (obj.isDirectory()) {
141: File allFiles[] = obj.listFiles();
142:
143: ClarosWebDskFolder fold = new ClarosWebDskFolder(obj);
144:
145: TreeSet contents = fold.getContents();
146: for (File aFile : allFiles) {
147: getFolderContents(username, aFile, contents);
148: }
149: fold.setContents(contents);
150: out.add(fold);
151: } else if (obj.isFile()) {
152: out.add(new ClarosWebDskFile(obj));
153: }
154: }
155:
156: /**
157: *
158: * @param username
159: * @param path
160: * @throws NoPermissionException
161: * @throws Exception
162: */
163: public static void checkPathPermissions(String username, String path)
164: throws NoPermissionException, Exception {
165: if (path.indexOf("../") >= 0) {
166: throw new NoPermissionException();
167: }
168: if (!correctPath(path).startsWith(homeBase)) {
169: throw new NoPermissionException();
170: }
171:
172: File home = getUserHome(username);
173: String homePath = home.getAbsolutePath();
174: if (!correctPath(path).startsWith(correctPath(homePath))) {
175: throw new NoPermissionException();
176: }
177: }
178:
179: static {
180: try {
181: homeBase = PropertyFile.getConfiguration(
182: "/config/config.xml")
183: .getString("webdisk.home-base");
184: homeBase = correctPath(homeBase);
185:
186: if (homeBase != null) {
187: if (homeBase.endsWith("/") || homeBase.endsWith("\\")) {
188: homeBase = homeBase.substring(0,
189: homeBase.length() - 1);
190: }
191: File f = new File(homeBase);
192: if (!f.exists()) {
193: log
194: .fatal("home-base does not exist. Webdisk will not be functional");
195: }
196: if (!f.isDirectory()) {
197: log
198: .fatal("home-base is not a directory. Webdisk will not be functional");
199: }
200: if (!f.canRead() || !f.canWrite()) {
201: log
202: .fatal("home-base is not readable/writable. Make sure file system permissions are right. Webdisk will not be functional");
203: }
204: } else {
205: log
206: .fatal("home-base parameter at config.xml is null. Webdisk will not be functional");
207: }
208: } catch (Exception e) {
209: log
210: .fatal("home-base can not be determined. Webdisk will not be functional");
211: }
212: }
213:
214: public static String correctPath(String path) {
215: path = Utility.replaceAllOccurances(path, "\\\\\\\\\\", "/");
216: path = Utility.replaceAllOccurances(path, "\\\\\\\\", "/");
217: path = Utility.replaceAllOccurances(path, "\\\\\\", "/");
218: path = Utility.replaceAllOccurances(path, "\\\\", "/");
219: path = Utility.replaceAllOccurances(path, "\\", "/");
220: return path;
221: }
222:
223: /**
224: *
225: * @param username
226: * @param path
227: * @return
228: * @throws Exception
229: */
230: public static File getUserFile(String username, String path)
231: throws Exception {
232: if (path.indexOf("../") >= 0) {
233: throw new NoPermissionException();
234: }
235:
236: File userHome = getUserHome(username);
237: String homeDir = correctPath(userHome.getAbsolutePath());
238: if (!path.startsWith("/")) {
239: path = "/" + path;
240: }
241: String pt = homeDir + path;
242:
243: File f = new File(pt);
244: String corrPath = correctPath(f.getAbsolutePath());
245:
246: checkPathPermissions(username, corrPath);
247:
248: return f;
249: }
250: }
|