1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 package fr.paris.lutece.util.string;
35
36 import fr.paris.lutece.portal.service.util.AppLogService;
37 import fr.paris.lutece.portal.service.util.AppPropertiesService;
38
39 import java.text.Normalizer;
40
41
42
43
44
45 public final class StringUtil
46 {
47 private static final String PROPERTY_XSS_CHARACTERS = "input.xss.characters";
48 private static final String EMAIL_PATTERN = "^[\\w_.\\-]+@[\\w_.\\-]+\\.[\\w]+$";
49 private static final String STRING_CODE_PATTERN = "^[\\w]+$";
50 private static final String CONSTANT_AT = "@";
51
52
53 private static char[] _aXssCharacters;
54 private static String _xssCharactersAsString;
55
56
57
58
59 private StringUtil( )
60 {
61 }
62
63
64
65
66
67
68
69
70
71 public static String substitute( String strSource, String strValue, String strBookmark )
72 {
73 StringBuilder strResult = new StringBuilder( );
74 int nPos = strSource.indexOf( strBookmark );
75 String strModifySource = strSource;
76
77 while ( nPos != -1 )
78 {
79 strResult.append( strModifySource.substring( 0, nPos ) );
80 strResult.append( strValue );
81 strModifySource = strModifySource.substring( nPos + strBookmark.length( ) );
82 nPos = strModifySource.indexOf( strBookmark );
83 }
84
85 strResult.append( strModifySource );
86
87 return strResult.toString( );
88 }
89
90
91
92
93
94
95
96 public static String replaceAccent( String strSource )
97 {
98 String strNormalized = Normalizer.normalize( strSource, Normalizer.Form.NFKD );
99 strNormalized = strNormalized.replaceAll( "\\p{M}", "" );
100
101 return strNormalized;
102 }
103
104
105
106
107
108
109
110 public static boolean containsHtmlSpecialCharacters( String strValue )
111 {
112 return ( ( strValue.indexOf( '"' ) > -1 ) || ( strValue.indexOf( '&' ) > -1 ) ||
113 ( strValue.indexOf( '<' ) > -1 ) || ( strValue.indexOf( '>' ) > -1 ) );
114 }
115
116
117
118
119
120
121
122
123 public static synchronized boolean containsXssCharacters( String strValue )
124 {
125
126 if ( _aXssCharacters == null )
127 {
128 _aXssCharacters = AppPropertiesService.getProperty( PROPERTY_XSS_CHARACTERS ).toCharArray( );
129 }
130
131 return containsXssCharacters( strValue, _aXssCharacters );
132 }
133
134
135
136
137
138
139
140
141
142 public static synchronized boolean containsXssCharacters( String strValue, char[] aXssCharacters )
143 {
144
145 boolean bContains = false;
146
147 if ( aXssCharacters != null )
148 {
149 for ( int nIndex = 0; !bContains && ( nIndex < aXssCharacters.length ); nIndex++ )
150 {
151 bContains = strValue.lastIndexOf( aXssCharacters[nIndex] ) >= 0;
152 }
153 }
154
155 return bContains;
156 }
157
158
159
160
161
162
163
164
165
166 public static synchronized boolean containsXssCharacters( String strValue, String strXssCharacters )
167 {
168
169 if ( strXssCharacters != null )
170 {
171 return containsXssCharacters( strValue, strXssCharacters.toCharArray( ) );
172 }
173
174 return false;
175 }
176
177
178
179
180
181
182
183 public static synchronized String getXssCharactersAsString( )
184 {
185
186 if ( _aXssCharacters == null )
187 {
188 _aXssCharacters = AppPropertiesService.getProperty( PROPERTY_XSS_CHARACTERS ).toCharArray( );
189 }
190
191 if ( _xssCharactersAsString == null )
192 {
193 StringBuilder sbfCharList = new StringBuilder( );
194
195 int iIndex;
196
197 for ( iIndex = 0; iIndex < ( _aXssCharacters.length - 1 ); iIndex++ )
198 {
199 sbfCharList.append( _aXssCharacters[iIndex] );
200 sbfCharList.append( ", " );
201 }
202
203
204 sbfCharList.append( _aXssCharacters[iIndex] );
205 _xssCharactersAsString = sbfCharList.toString( );
206 }
207
208 return _xssCharactersAsString;
209 }
210
211
212
213
214
215
216
217
218 public static synchronized boolean checkEmail( String strEmail )
219 {
220 return strEmail.matches( EMAIL_PATTERN );
221 }
222
223
224
225
226
227
228
229
230 public static synchronized boolean checkEmailAndDomainName( String strEmail, String[] strBannedDomainNames )
231 {
232 boolean bIsValid = strEmail.matches( EMAIL_PATTERN );
233
234 return bIsValid && checkEmailDomainName( strEmail, strBannedDomainNames );
235 }
236
237
238
239
240
241
242
243 public static synchronized boolean checkEmailDomainName( String strEmail, String[] strBannedDomainNames )
244 {
245 if ( ( strBannedDomainNames != null ) && ( strBannedDomainNames.length > 0 ) )
246 {
247 int nOffset;
248
249 if ( strBannedDomainNames[0].contains( CONSTANT_AT ) )
250 {
251 nOffset = 0;
252 }
253 else
254 {
255 nOffset = 1;
256 }
257
258 int nIndex = strEmail.indexOf( CONSTANT_AT );
259
260 if ( ( nIndex >= 0 ) && ( ( nIndex + nOffset ) < strEmail.length( ) ) )
261 {
262 String strDomainName = strEmail.substring( nIndex + nOffset );
263
264 for ( String strDomain : strBannedDomainNames )
265 {
266 if ( strDomainName.equals( strDomain ) )
267 {
268 return false;
269 }
270 }
271 }
272 }
273
274 return true;
275 }
276
277
278
279
280
281
282
283
284
285
286
287
288
289 public static synchronized boolean checkCodeKey( String strCodeKey )
290 {
291 return ( strCodeKey == null ) ? false : strCodeKey.matches( STRING_CODE_PATTERN );
292 }
293
294
295
296
297
298
299
300 public static int getIntValue( String strValue, int nDefaultValue )
301 {
302 try
303 {
304 return Integer.parseInt( strValue );
305 }
306 catch ( NumberFormatException nfe )
307 {
308 AppLogService.error( nfe.getMessage( ), nfe );
309 }
310
311 return nDefaultValue;
312 }
313 }