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.portal.web.system;
35
36 import java.io.File;
37 import java.io.FileInputStream;
38 import java.io.FileNotFoundException;
39 import java.io.IOException;
40 import java.io.Serializable;
41 import java.util.ArrayList;
42 import java.util.Collection;
43 import java.util.Collections;
44 import java.util.Date;
45 import java.util.HashMap;
46 import java.util.List;
47 import java.util.Map;
48
49 import javax.servlet.ServletContext;
50 import javax.servlet.http.HttpServletRequest;
51
52 import fr.paris.lutece.portal.service.admin.AccessDeniedException;
53 import fr.paris.lutece.portal.service.admin.AdminUserService;
54 import fr.paris.lutece.portal.service.datastore.DatastoreService;
55 import fr.paris.lutece.portal.service.datastore.LocalizedData;
56 import fr.paris.lutece.portal.service.datastore.LocalizedDataGroup;
57 import fr.paris.lutece.portal.service.i18n.I18nService;
58 import fr.paris.lutece.portal.service.security.SecurityTokenService;
59 import fr.paris.lutece.portal.service.site.properties.SitePropertiesService;
60 import fr.paris.lutece.portal.service.template.AppTemplateService;
61 import fr.paris.lutece.portal.service.util.AppPathService;
62 import fr.paris.lutece.portal.service.util.AppPropertiesService;
63 import fr.paris.lutece.portal.web.admin.AdminFeaturesPageJspBean;
64 import fr.paris.lutece.util.html.HtmlTemplate;
65 import fr.paris.lutece.util.http.SecurityUtil;
66
67
68
69
70 public class SystemJspBean extends AdminFeaturesPageJspBean
71 {
72
73 public static final String RIGHT_PROPERTIES_MANAGEMENT = "CORE_PROPERTIES_MANAGEMENT";
74 public static final String RIGHT_LOGS_VISUALISATION = "CORE_LOGS_VISUALISATION";
75
76
77 public static final String JSP_MANAGE_PROPERTIES = "ManageProperties.jsp";
78
79
80 private static final long serialVersionUID = 3770485521087669430L;
81
82
83 private static final String MARK_FILES_LIST = "files_list";
84 private static final String MARK_FILES_SYSTEM_DIRECTORY = "files_system_directory";
85 private static final String MARK_FILES_SYSTEM_NAME = "file_system_name";
86 private static final String MARK_FILE_SYSTEM_DATA = "file_system_data";
87 private static final String MARK_PROPERTIES_GROUPS_LIST = "groups_list";
88
89
90 private static final String TEMPLATE_MANAGE_FILES_SYSTEM = "admin/system/manage_files_system.html";
91 private static final String TEMPLATE_VIEW_FILES_SYSTEM = "admin/system/view_files_system.html";
92 private static final String TEMPLATE_VIEW_FILE = "admin/system/view_file.html";
93
94 private static final String TEMPLATE_MODIFY_PROPERTIES = "admin/system/modify_properties.html";
95
96
97 private static final String PARAMETER_FILE = "file";
98 private static final String PARAMETER_DIRECTORY = "directory";
99 private static final String PARAMETER_DIR = "dir";
100
101
102 private static final String PROPERTY_FILES_SYSTEM_LIST = "system.list";
103 private static final String PROPERTY_TITLE_MANAGE_FILES_SYSTEM = "portal.system.manage_files_system.pageTitle";
104 private static final String PROPERTY_FILE_DESCRIPTION = "portal.system.manage_files_system.description.";
105 private static final String PROPERTY_FILE_NAME = "portal.system.manage_files_system.name.";
106 private static final String PROPERTY_TITLE_VIEW_FILES_SYSTEM = "portal.system.view_files_system.pageTitle";
107 private static final String PROPERTY_TITLE_VIEW_FILE = "portal.system.view_file.pageTitle";
108 private static final String MARK_WEBAPP_URL = "webapp_url";
109 private static final String MARK_LOCALE = "locale";
110
111
112
113
114
115
116
117
118 public String getManageFilesSystem( HttpServletRequest request )
119 {
120 setPageTitleProperty( PROPERTY_TITLE_MANAGE_FILES_SYSTEM );
121 ArrayList<SystemFile> list = new ArrayList<>( );
122
123 for ( String strDirectory : getDirectories( ) )
124 {
125 SystemFileeb/system/SystemFile.html#SystemFile">SystemFile file = new SystemFile( );
126 file.setName( I18nService.getLocalizedString( PROPERTY_FILE_NAME + strDirectory, request.getLocale( ) ) );
127 file.setDescription( I18nService.getLocalizedString( PROPERTY_FILE_DESCRIPTION + strDirectory, request.getLocale( ) ) );
128 file.setDirectory( AppPropertiesService.getProperty( "system." + strDirectory + ".directory" ) );
129 list.add( file );
130 }
131
132 Map<String, Collection<SystemFile>> model = new HashMap<>( );
133 model.put( MARK_FILES_LIST, list );
134
135 HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_MANAGE_FILES_SYSTEM, getLocale( ), model );
136
137 return getAdminPage( template.getHtml( ) );
138 }
139
140
141
142
143
144
145
146
147 public String getManageFilesSystemDir( HttpServletRequest request )
148 {
149 setPageTitleProperty( PROPERTY_TITLE_VIEW_FILES_SYSTEM );
150 String strDir = request.getParameter( PARAMETER_DIR );
151
152 if ( !isValidDirectoryPath( strDir ) )
153 {
154 return getManageFilesSystem( request );
155 }
156
157 String strDirectory = AppPathService.getWebAppPath( ) + strDir;
158 File directory = new File( strDirectory );
159 ArrayList<SystemFile> listFiles = new ArrayList<>( );
160 for ( File file : directory.listFiles( ) )
161 {
162 SystemFileb/system/SystemFile.html#SystemFile">SystemFile sFile = new SystemFile( );
163 sFile.setName( file.getName( ) );
164 sFile.setDirectory( strDir );
165 sFile.setSize( (int) ( file.length( ) / 1000 ) + 1 );
166 sFile.setDate( new Date( file.lastModified( ) ) );
167 listFiles.add( sFile );
168 }
169 Collections.sort( listFiles );
170
171 Map<String, Serializable> model = new HashMap<>( );
172 model.put( MARK_FILES_LIST, listFiles );
173 model.put( MARK_FILES_SYSTEM_DIRECTORY, strDir );
174
175 HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_VIEW_FILES_SYSTEM, getLocale( ), model );
176
177 return getAdminPage( template.getHtml( ) );
178 }
179
180
181
182
183
184
185
186
187
188
189 public String getFileView( HttpServletRequest request )
190 {
191 Map<String, Object> model = new HashMap<>( );
192 setPageTitleProperty( PROPERTY_TITLE_VIEW_FILE );
193
194 String strFileData;
195 String strFile = request.getParameter( PARAMETER_FILE );
196 String strDirectory = request.getParameter( PARAMETER_DIRECTORY );
197
198 if ( strFile == null )
199 {
200 strFileData = "ERROR : No file selected !";
201 }
202 else
203 if ( !isValidDirectoryPath( strDirectory ) )
204 {
205 strFileData = "ERROR : Invalid directory !";
206 }
207 else
208 {
209 String strFilePath = AppPathService.getWebAppPath( );
210
211 if ( strFilePath != null && !SecurityUtil.containsPathManipulationChars( request, strFile ) )
212 {
213 strFileData = getFileData( strFilePath + strDirectory + strFile );
214 }
215 else
216 {
217 strFileData = "ERROR : " + strFile + " not found !";
218 }
219 }
220
221 model.put( MARK_FILES_SYSTEM_NAME, strDirectory + strFile );
222 model.put( MARK_FILE_SYSTEM_DATA, strFileData );
223 model.put( MARK_FILES_SYSTEM_DIRECTORY, strDirectory );
224
225 HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_VIEW_FILE, getLocale( ), model );
226
227 return getAdminPage( template.getHtml( ) );
228 }
229
230
231
232
233
234
235
236
237 public String getManageProperties( HttpServletRequest request )
238 {
239 Map<String, Object> model = new HashMap<>( );
240 model.put( MARK_PROPERTIES_GROUPS_LIST, SitePropertiesService.getGroups( getLocale( ) ) );
241 model.put( MARK_WEBAPP_URL, AppPathService.getBaseUrl( request ) );
242 model.put( MARK_LOCALE, getLocale( ).getLanguage( ) );
243 model.put( SecurityTokenService.MARK_TOKEN, SecurityTokenService.getInstance( ).getToken( request, TEMPLATE_MODIFY_PROPERTIES ) );
244
245 HtmlTemplate templateList = AppTemplateService.getTemplate( TEMPLATE_MODIFY_PROPERTIES, getLocale( ), model );
246
247 return getAdminPage( templateList.getHtml( ) );
248 }
249
250
251
252
253
254
255
256
257
258
259
260
261 public static String doModifyProperties( HttpServletRequest request, ServletContext context ) throws AccessDeniedException
262 {
263 if ( !SecurityTokenService.getInstance( ).validate( request, TEMPLATE_MODIFY_PROPERTIES ) )
264 {
265 throw new AccessDeniedException( ERROR_INVALID_TOKEN );
266 }
267 List<LocalizedDataGroup> groups = SitePropertiesService.getGroups( AdminUserService.getAdminUser( request ).getLocale( ) );
268
269 for ( LocalizedDataGroup group : groups )
270 {
271 List<LocalizedData> datas = group.getLocalizedDataList( );
272
273 for ( LocalizedData data : datas )
274 {
275 String strValue = request.getParameter( data.getKey( ) );
276
277 if ( ( strValue != null ) && !data.getValue( ).equals( strValue ) )
278 {
279 DatastoreService.setDataValue( data.getKey( ), strValue );
280 }
281 }
282 }
283
284
285 return JSP_MANAGE_PROPERTIES;
286 }
287
288
289
290
291
292
293
294
295
296
297 private static String getFileData( String strFilePath )
298 {
299 StringBuilder sbData = new StringBuilder( );
300
301 try ( FileInputStream fis = new FileInputStream( strFilePath ) )
302 {
303 int chr = 0;
304
305 while ( chr != -1 )
306 {
307 chr = fis.read( );
308 sbData.append( (char) chr );
309 }
310
311
312 sbData.setLength( sbData.length( ) - 1 );
313 }
314 catch( FileNotFoundException e )
315 {
316 sbData.append( "ERROR : File " ).append( strFilePath ).append( " not found" );
317 }
318 catch( IOException e )
319 {
320 sbData.append( "ERROR : Error reading the file : " ).append( strFilePath );
321 }
322 return sbData.toString( );
323 }
324
325
326
327
328
329
330 private String [ ] getDirectories( )
331 {
332 String strDirectories = AppPropertiesService.getProperty( PROPERTY_FILES_SYSTEM_LIST );
333 return strDirectories.split( "," );
334 }
335
336
337
338
339
340
341
342
343 private boolean isValidDirectoryPath( String strPath )
344 {
345 for ( String strFileSystemName : getDirectories( ) )
346 {
347 String strDirectoryPath = AppPropertiesService.getProperty( "system." + strFileSystemName + ".directory" );
348
349 if ( strDirectoryPath.equals( strPath ) )
350 {
351 return true;
352 }
353 }
354 return false;
355 }
356
357 }