741 |
10 Oct 06 |
olle |
1 |
/* |
1652 |
22 May 07 |
gregory |
$Id$ |
741 |
10 Oct 06 |
olle |
3 |
|
1916 |
31 Aug 07 |
jari |
Copyright (C) 2006 Olle Mansson |
1916 |
31 Aug 07 |
jari |
Copyright (C) 2007 Gregory Vincic |
741 |
10 Oct 06 |
olle |
6 |
|
1652 |
22 May 07 |
gregory |
This file is part of Proteios. |
1652 |
22 May 07 |
gregory |
Available at http://www.proteios.org/ |
741 |
10 Oct 06 |
olle |
9 |
|
1652 |
22 May 07 |
gregory |
Proteios is free software; you can redistribute it and/or modify it |
1652 |
22 May 07 |
gregory |
under the terms of the GNU General Public License as published by |
1652 |
22 May 07 |
gregory |
the Free Software Foundation; either version 2 of the License, or |
1652 |
22 May 07 |
gregory |
(at your option) any later version. |
741 |
10 Oct 06 |
olle |
14 |
|
1652 |
22 May 07 |
gregory |
Proteios is distributed in the hope that it will be useful, but |
1652 |
22 May 07 |
gregory |
WITHOUT ANY WARRANTY; without even the implied warranty of |
1652 |
22 May 07 |
gregory |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
1652 |
22 May 07 |
gregory |
General Public License for more details. |
741 |
10 Oct 06 |
olle |
19 |
|
1652 |
22 May 07 |
gregory |
You should have received a copy of the GNU General Public License |
1652 |
22 May 07 |
gregory |
along with this program; if not, write to the Free Software |
1652 |
22 May 07 |
gregory |
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA |
1652 |
22 May 07 |
gregory |
02111-1307, USA. |
1652 |
22 May 07 |
gregory |
24 |
*/ |
741 |
10 Oct 06 |
olle |
25 |
|
741 |
10 Oct 06 |
olle |
// Xerver Free Web Server |
741 |
10 Oct 06 |
olle |
// Copyright (C) 2002-2005 Omid Rouhani |
741 |
10 Oct 06 |
olle |
28 |
// |
741 |
10 Oct 06 |
olle |
29 |
// |
741 |
10 Oct 06 |
olle |
// This program is free software; you can redistribute it and/or |
741 |
10 Oct 06 |
olle |
// modify it under the terms of the GNU General Public License |
741 |
10 Oct 06 |
olle |
// as published by the Free Software Foundation; either version 2 |
741 |
10 Oct 06 |
olle |
// of the License, or (at your option) any later version. |
741 |
10 Oct 06 |
olle |
34 |
// |
741 |
10 Oct 06 |
olle |
// This program is distributed in the hope that it will be useful, |
741 |
10 Oct 06 |
olle |
// but WITHOUT ANY WARRANTY; without even the implied warranty of |
741 |
10 Oct 06 |
olle |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
741 |
10 Oct 06 |
olle |
// GNU General Public License for more details. |
741 |
10 Oct 06 |
olle |
39 |
// |
741 |
10 Oct 06 |
olle |
// You should have received a copy of the GNU General Public License |
741 |
10 Oct 06 |
olle |
// along with this program; if not, write to the Free Software |
741 |
10 Oct 06 |
olle |
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
741 |
10 Oct 06 |
olle |
43 |
// |
741 |
10 Oct 06 |
olle |
44 |
// |
741 |
10 Oct 06 |
olle |
45 |
// ############################################################# |
741 |
10 Oct 06 |
olle |
// ## YOU CAN CONTACT THE AUTHOR (OMID ROUHANI) AT: ## |
741 |
10 Oct 06 |
olle |
// ## HTTP://WWW.JAVASCRIPT.NU/XERVER/ ## |
741 |
10 Oct 06 |
olle |
48 |
// ## ## |
741 |
10 Oct 06 |
olle |
// ## IF YOUR SOFTWARE IS NOT RELEASED UNDER THE ## |
741 |
10 Oct 06 |
olle |
// ## GNU GENERAL PUBLIC LICENSE (GPL), ## |
741 |
10 Oct 06 |
olle |
// ## PLEASE DO NOT COPY ANYTHING FROM THIS SOURCE CODE!!! ## |
741 |
10 Oct 06 |
olle |
52 |
// ## ## |
741 |
10 Oct 06 |
olle |
// ## FOR FULL LICENSE, PLEASE READ "XERVER LICENSE". ## |
741 |
10 Oct 06 |
olle |
54 |
// ############################################################# |
741 |
10 Oct 06 |
olle |
55 |
|
1652 |
22 May 07 |
gregory |
56 |
package se.lu.thep.coreftpd.ftp_server; |
741 |
10 Oct 06 |
olle |
57 |
|
741 |
10 Oct 06 |
olle |
58 |
import java.io.File; |
741 |
10 Oct 06 |
olle |
59 |
|
741 |
10 Oct 06 |
olle |
60 |
/** |
1652 |
22 May 07 |
gregory |
61 |
* |
1652 |
22 May 07 |
gregory |
* <B>About this class:</B> <BR> |
1652 |
22 May 07 |
gregory |
* This class contains only static methods used to validate the data sent to |
1652 |
22 May 07 |
gregory |
* Xerver via the web based setup. |
1652 |
22 May 07 |
gregory |
65 |
* |
741 |
10 Oct 06 |
olle |
* @author <a href="http://www.JavaScript.nu/xerver/" TARGET="_top">Omid Rouhani</a> |
741 |
10 Oct 06 |
olle |
* @version 1.0 |
741 |
10 Oct 06 |
olle |
68 |
*/ |
741 |
10 Oct 06 |
olle |
69 |
|
1652 |
22 May 07 |
gregory |
70 |
final public class ValidateInput { |
741 |
10 Oct 06 |
olle |
71 |
/** |
1652 |
22 May 07 |
gregory |
* Returns true iff username is not null, not empty and only contains a-z, |
1652 |
22 May 07 |
gregory |
* 0-9 and underscore (_). |
1652 |
22 May 07 |
gregory |
74 |
*/ |
1652 |
22 May 07 |
gregory |
75 |
public static boolean isValidUserName(String username) { |
1652 |
22 May 07 |
gregory |
76 |
if (username == null) |
741 |
10 Oct 06 |
olle |
77 |
return false; |
741 |
10 Oct 06 |
olle |
78 |
|
741 |
10 Oct 06 |
olle |
79 |
if (username.equals("")) |
741 |
10 Oct 06 |
olle |
80 |
return false; |
741 |
10 Oct 06 |
olle |
81 |
|
1652 |
22 May 07 |
gregory |
82 |
byte[] buffer = username.getBytes(); |
1652 |
22 May 07 |
gregory |
83 |
for (int i = 0; i < buffer.length; i++) { |
1652 |
22 May 07 |
gregory |
84 |
char ch = (char) buffer[i]; |
1652 |
22 May 07 |
gregory |
85 |
if (!Character.isLetterOrDigit(ch) && ch != '_') { |
741 |
10 Oct 06 |
olle |
86 |
return false; |
741 |
10 Oct 06 |
olle |
87 |
} |
741 |
10 Oct 06 |
olle |
88 |
} |
741 |
10 Oct 06 |
olle |
89 |
return true; |
741 |
10 Oct 06 |
olle |
90 |
} |
741 |
10 Oct 06 |
olle |
91 |
|
741 |
10 Oct 06 |
olle |
92 |
/** |
1652 |
22 May 07 |
gregory |
* Returns true iff alias is not null, not empty and only contains a-z, 0-9 |
1652 |
22 May 07 |
gregory |
* and underscore (_). |
1652 |
22 May 07 |
gregory |
95 |
*/ |
1652 |
22 May 07 |
gregory |
96 |
public static boolean isValidAliasName(String alias) { |
741 |
10 Oct 06 |
olle |
97 |
return isValidUserName(alias); |
741 |
10 Oct 06 |
olle |
98 |
} |
741 |
10 Oct 06 |
olle |
99 |
|
741 |
10 Oct 06 |
olle |
100 |
/** |
1652 |
22 May 07 |
gregory |
* Returns true iff path is not null. |
1652 |
22 May 07 |
gregory |
102 |
*/ |
1652 |
22 May 07 |
gregory |
103 |
public static boolean isValidPath(String path) { |
1652 |
22 May 07 |
gregory |
104 |
if (path == null) |
741 |
10 Oct 06 |
olle |
105 |
return false; |
741 |
10 Oct 06 |
olle |
106 |
|
1652 |
22 May 07 |
gregory |
// NOTE: If we have this, we can't share a path that is temporary |
1652 |
22 May 07 |
gregory |
// unavailable, for example an empty CD drive. |
1652 |
22 May 07 |
gregory |
// File file=new File(path); |
1652 |
22 May 07 |
gregory |
// return file.exists(); |
1652 |
22 May 07 |
gregory |
111 |
return true; // So far we accept all paths... |
741 |
10 Oct 06 |
olle |
112 |
} |
741 |
10 Oct 06 |
olle |
113 |
|
741 |
10 Oct 06 |
olle |
114 |
/** |
1652 |
22 May 07 |
gregory |
* Returns null if path is not a valid directory, otherwise returns path |
1652 |
22 May 07 |
gregory |
* (with a slash in the end if necessary). |
1652 |
22 May 07 |
gregory |
117 |
*/ |
1652 |
22 May 07 |
gregory |
118 |
public static String makeValidPath(String path) { |
1652 |
22 May 07 |
gregory |
119 |
if (path == null) |
741 |
10 Oct 06 |
olle |
120 |
return null; |
741 |
10 Oct 06 |
olle |
121 |
|
1652 |
22 May 07 |
gregory |
122 |
path = path.trim(); |
741 |
10 Oct 06 |
olle |
123 |
|
1652 |
22 May 07 |
gregory |
124 |
File file = new File(path); |
741 |
10 Oct 06 |
olle |
125 |
|
1652 |
22 May 07 |
gregory |
126 |
if (!file.isAbsolute()) // The path is not an absolute path... (if we |
1652 |
22 May 07 |
gregory |
// remove this, the user can enter "data" and we |
1652 |
22 May 07 |
gregory |
// change it to "c:\path\to\Xerver\data\", but I |
1652 |
22 May 07 |
gregory |
// think we better not do this as if he enter |
1652 |
22 May 07 |
gregory |
// "dummyFolder" we don't the script to present |
1652 |
22 May 07 |
gregory |
// "c:\path\to\Xerver\dummyFolder\" to the user, |
1652 |
22 May 07 |
gregory |
// as we for now make no check whatever the |
1652 |
22 May 07 |
gregory |
// folder really exists or not). |
741 |
10 Oct 06 |
olle |
134 |
{ |
741 |
10 Oct 06 |
olle |
135 |
return null; |
741 |
10 Oct 06 |
olle |
136 |
} |
741 |
10 Oct 06 |
olle |
137 |
|
1652 |
22 May 07 |
gregory |
138 |
path = file.getAbsolutePath(); |
741 |
10 Oct 06 |
olle |
139 |
|
1652 |
22 May 07 |
gregory |
140 |
if (!path.endsWith(File.separator)) { |
1652 |
22 May 07 |
gregory |
141 |
path = path + File.separator; |
741 |
10 Oct 06 |
olle |
142 |
} |
741 |
10 Oct 06 |
olle |
143 |
|
1652 |
22 May 07 |
gregory |
// NOTE: If we have this, we can't share a path that is temporary |
1652 |
22 May 07 |
gregory |
// unavailable, for example an empty CD drive. |
1652 |
22 May 07 |
gregory |
// if (file.isDirectory()) |
1652 |
22 May 07 |
gregory |
// return path; |
1652 |
22 May 07 |
gregory |
// else |
1652 |
22 May 07 |
gregory |
// return null; |
1652 |
22 May 07 |
gregory |
150 |
return path; // So far we make no check at all that the directory |
1652 |
22 May 07 |
gregory |
// really exists. |
741 |
10 Oct 06 |
olle |
152 |
} |
741 |
10 Oct 06 |
olle |
153 |
} |