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.plugins.contact.web;
35
36 import fr.paris.lutece.plugins.contact.business.Contact;
37 import fr.paris.lutece.plugins.contact.business.ContactHome;
38 import fr.paris.lutece.plugins.contact.business.ContactList;
39 import fr.paris.lutece.plugins.contact.business.ContactListHome;
40 import fr.paris.lutece.plugins.contact.service.ContactPlugin;
41 import fr.paris.lutece.portal.service.captcha.CaptchaSecurityService;
42 import fr.paris.lutece.portal.service.content.XPageAppService;
43 import fr.paris.lutece.portal.service.i18n.I18nService;
44 import fr.paris.lutece.portal.service.mail.MailService;
45 import fr.paris.lutece.portal.service.message.SiteMessage;
46 import fr.paris.lutece.portal.service.message.SiteMessageException;
47 import fr.paris.lutece.portal.service.message.SiteMessageService;
48 import fr.paris.lutece.portal.service.plugin.Plugin;
49 import fr.paris.lutece.portal.service.plugin.PluginService;
50 import fr.paris.lutece.portal.service.security.LuteceUser;
51 import fr.paris.lutece.portal.service.security.SecurityService;
52 import fr.paris.lutece.portal.service.security.UserNotSignedException;
53 import fr.paris.lutece.portal.service.template.AppTemplateService;
54 import fr.paris.lutece.portal.util.mvc.commons.annotations.Action;
55 import fr.paris.lutece.portal.util.mvc.commons.annotations.View;
56 import fr.paris.lutece.portal.util.mvc.utils.MVCUtils;
57 import fr.paris.lutece.portal.util.mvc.xpage.MVCApplication;
58 import fr.paris.lutece.portal.util.mvc.xpage.annotations.Controller;
59 import fr.paris.lutece.portal.web.xpages.XPage;
60 import fr.paris.lutece.util.ReferenceList;
61 import fr.paris.lutece.util.date.DateUtil;
62 import fr.paris.lutece.util.html.HtmlTemplate;
63 import fr.paris.lutece.util.string.StringUtil;
64 import fr.paris.lutece.util.url.UrlItem;
65
66 import java.util.ArrayList;
67 import java.util.Collection;
68 import java.util.HashMap;
69 import java.util.Map;
70
71 import javax.servlet.http.HttpServletRequest;
72
73 import org.apache.commons.lang3.StringUtils;
74
75
76
77
78 @Controller( xpageName = "contact", pageTitleI18nKey = "contact.pagePathLabel", pagePathI18nKey = "subscribe.pageTitle" )
79 public class ContactApp extends MVCApplication
80 {
81
82 private static final long serialVersionUID = 6553298772139973292L;
83
84
85
86 private static final String TEMPLATE_XPAGE_CONTACT = "skin/plugins/contact/page_contact.html";
87 private static final String TEMPLATE_XPAGE_LISTS = "skin/plugins/contact/page_lists.html";
88 private static final String TEMPLATE_MESSAGE_CONTACT = "skin/plugins/contact/message_contact.html";
89 private static final String MARK_CONTACTS_LIST = "contacts_list";
90 private static final String MARK_DEFAULT_CONTACT = "default_contact";
91 private static final String MARK_CONTACT_ALERT_MSG = "alert_msg";
92 private static final String MARK_VISITOR_LASTNAME = "visitor_last_name";
93 private static final String MARK_VISITOR_FIRSTNAME = "visitor_first_name";
94 private static final String MARK_VISITOR_ADDRESS = "visitor_address";
95 private static final String MARK_VISITOR_EMAIL = "visitor_email";
96 private static final String MARK_OBJECT = "message_object";
97 private static final String MARK_MESSAGE = "message";
98 private static final String MARK_STYLE_LAST_NAME = "style_last_name";
99 private static final String MARK_STYLE_FIRST_NAME = "style_first_name";
100 private static final String MARK_STYLE_EMAIL = "style_email";
101 private static final String MARK_STYLE_OBJECT = "style_object";
102 private static final String MARK_STYLE_MESSAGE = "style_message";
103 private static final String MARK_STYLE_CONTACT = "style_contact";
104 private static final String MARK_PORTAL_URL = "portal_url";
105 private static final String MARK_CONTACT_NAME = "contact_name";
106 private static final String MARK_CURRENT_DATE = "current_date";
107 private static final String MARK_CAPTCHA = "captcha";
108 private static final String MARK_IS_ACTIVE_CAPTCHA = "is_active_captcha";
109 private static final String MARK_IS_TOS_REQUIRED = "is_tos_required";
110 private static final String MARK_TOS_MESSAGE = "tos_message";
111 private static final String MARK_TOS_ACCEPTED = "accept_tos";
112
113 private static final String MARK_LIST_OF_LISTS = "list_of_lists";
114 private static final String MARK_ID_CONTACT_LIST = "id_contact_list";
115 private static final String MARK_MYLUTECE_USER = "mylutece_user";
116 private static final String PARAMETER_PAGE = "page";
117 private static final String PARAMETER_CONTACT = "contact";
118 private static final String PARAMETER_VISITOR_LASTNAME = "visitor_last_name";
119 private static final String PARAMETER_VISITOR_FIRSTNAME = "visitor_first_name";
120 private static final String PARAMETER_VISITOR_ADDRESS = "visitor_address";
121 private static final String PARAMETER_VISITOR_EMAIL = "visitor_email";
122 private static final String PARAMETER_MESSAGE_OBJECT = "message_object";
123 private static final String PARAMETER_MESSAGE = "message";
124 private static final String PARAMETER_SEND = "send";
125 private static final String PARAMETER_ID_CONTACT_LIST = "id_contact_list";
126 private static final String PARAMETER_TOS_ACCEPTED = "accept_tos";
127 private static final String PROPERTY_SENDING_OK = "contact.message_contact.sending.ok";
128 private static final String PROPERTY_MANDATORY_FIELD_MISSING = "contact.message_contact.mandatory.field";
129 private static final String PROPERTY_SENDING_NOK = "contact.message_contact.sending.nok";
130 private static final String PROPERTY_RECIPIENT_MISSING = "contact.message_contact.recipient.missing";
131 private static final String PROPERTY_ERROR_EMAIL = "contact.message_contact.error.email";
132 private static final String PROPERTY_COMBO_CHOOSE = "contact.message_contact.comboChoose";
133 private static final String PROPERTY_CAPTCHA_ERROR = "contact.message_contact.captchaError";
134 private static final String PROPERTY_TOS_ERROR = "contact.message_contact.tosRequired";
135
136 private static final String PROPERTY_LIST_NOT_EXISTS = "contact.message_contact.listNotExists";
137 private static final String PROPERTY_NOT_AUTHORIZED = "contact.message_contact.notauthorized";
138 private static final String PROPERTY_NO_LIST_VISIBLE = "contact.message_contact.noListVisible";
139 private static final String JCAPTCHA_PLUGIN = "jcaptcha";
140 private static final String EMPTY_STRING = "";
141 private static final String ACTION_SEND_MESSAGE = "actionSendMessage";
142 private static final String VIEW_CONTACT_LISTS = "viewContactLists";
143 private static final String VIEW_CONTACT_PAGE = "viewContactPage";
144
145
146 private CaptchaSecurityService _captchaService;
147
148
149 private Plugin _plugin;
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166 @Override
167 public XPage getPage( HttpServletRequest request, int nMode, Plugin plugin ) throws SiteMessageException, UserNotSignedException
168 {
169 String strPluginName = request.getParameter( PARAMETER_PAGE );
170 _plugin = PluginService.getPlugin( strPluginName );
171
172 return super.getPage( request, nMode, plugin );
173 }
174
175
176
177
178
179
180
181
182
183
184
185 private boolean isVisible( HttpServletRequest request, String strRole )
186 {
187 if ( ( strRole == null ) || ( strRole.trim( ).equals( EMPTY_STRING ) ) )
188 {
189 return true;
190 }
191
192 if ( !strRole.equals( ContactList.ROLE_NONE ) && SecurityService.isAuthenticationEnable( ) )
193 {
194 return SecurityService.getInstance( ).isUserInRole( request, strRole );
195 }
196
197 return true;
198 }
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213 @View( VIEW_CONTACT_PAGE )
214 public XPage getForm( HttpServletRequest request ) throws SiteMessageException
215 {
216 String strPortalUrl = request.getRequestURI( );
217 Map<String, Object> model = new HashMap<String, Object>( );
218
219 model.put( MARK_PORTAL_URL, strPortalUrl );
220
221 boolean bIsCaptchaEnabled = PluginService.isPluginEnable( JCAPTCHA_PLUGIN );
222 model.put( MARK_IS_ACTIVE_CAPTCHA, bIsCaptchaEnabled );
223
224 if ( bIsCaptchaEnabled )
225 {
226 _captchaService = new CaptchaSecurityService( );
227 model.put( MARK_CAPTCHA, _captchaService.getHtmlCode( ) );
228 }
229
230 String strIdContactList = request.getParameter( PARAMETER_ID_CONTACT_LIST );
231 String strSendMessage = request.getParameter( PARAMETER_SEND );
232 String strVisitorLastName = ( request.getParameter( PARAMETER_VISITOR_LASTNAME ) != null ) ? request.getParameter( PARAMETER_VISITOR_LASTNAME ) : "";
233 String strVisitorFirstName = ( request.getParameter( PARAMETER_VISITOR_FIRSTNAME ) != null ) ? request.getParameter( PARAMETER_VISITOR_FIRSTNAME ) : "";
234 String strVisitorEmail = ( request.getParameter( PARAMETER_VISITOR_EMAIL ) != null ) ? request.getParameter( PARAMETER_VISITOR_EMAIL ) : "";
235 String strVisitorAddress = ( request.getParameter( PARAMETER_VISITOR_ADDRESS ) != null ) ? request.getParameter( PARAMETER_VISITOR_ADDRESS ) : "";
236 String strObject = ( request.getParameter( PARAMETER_MESSAGE_OBJECT ) != null ) ? request.getParameter( PARAMETER_MESSAGE_OBJECT ) : "";
237 String strMessage = ( request.getParameter( PARAMETER_MESSAGE ) != null ) ? request.getParameter( PARAMETER_MESSAGE ) : "";
238 String strContact = ( request.getParameter( PARAMETER_CONTACT ) != null ) ? request.getParameter( PARAMETER_CONTACT ) : "";
239
240 if ( strSendMessage != null )
241 {
242 String strStyleLastName = strVisitorLastName.equals( "" ) ? "error" : "";
243 String strStyleFirstName = strVisitorFirstName.equals( "" ) ? "error" : "";
244 String strStyleEmail = ( strVisitorEmail.equals( "" ) || ( StringUtil.checkEmail( strVisitorEmail ) != true ) ) ? "error" : "";
245 String strStyleObject = strObject.equals( "" ) ? "error" : "";
246 String strStyleMessage = strMessage.equals( "" ) ? "error" : "";
247 String strStyleContact = strContact.equals( "0" ) ? "error" : "";
248 String strAlertMsg = "";
249
250 if ( strSendMessage.equals( "done" ) )
251 {
252 UrlItem url = new UrlItem( strPortalUrl );
253 url.addParameter( XPageAppService.PARAM_XPAGE_APP, ContactPlugin.PLUGIN_NAME );
254 url.addParameter( MVCUtils.PARAMETER_VIEW, VIEW_CONTACT_PAGE );
255 url.addParameter( PARAMETER_ID_CONTACT_LIST, strIdContactList );
256 SiteMessageService.setMessage( request, PROPERTY_SENDING_OK, SiteMessage.TYPE_INFO, url.getUrl( ) );
257 }
258
259 else
260 if ( strSendMessage.equals( "error_exception" ) )
261 {
262 strAlertMsg = I18nService.getLocalizedString( PROPERTY_SENDING_NOK, request.getLocale( ) );
263 }
264
265 else
266 if ( strSendMessage.equals( "error_captcha" ) )
267 {
268 strAlertMsg = I18nService.getLocalizedString( PROPERTY_CAPTCHA_ERROR, request.getLocale( ) );
269 }
270
271 else
272 if ( strSendMessage.equals( "error_tos" ) )
273 {
274 strAlertMsg = I18nService.getLocalizedString( PROPERTY_TOS_ERROR, request.getLocale( ) );
275 }
276
277 else
278 if ( strSendMessage.equals( "error_field" ) )
279 {
280 strAlertMsg = I18nService.getLocalizedString( PROPERTY_MANDATORY_FIELD_MISSING, request.getLocale( ) );
281 }
282
283 else
284 if ( strSendMessage.equals( "error_recipient" ) )
285 {
286 strAlertMsg = I18nService.getLocalizedString( PROPERTY_RECIPIENT_MISSING, request.getLocale( ) );
287 }
288 else
289 if ( strSendMessage.equals( "error_email" ) )
290 {
291 strAlertMsg = I18nService.getLocalizedString( PROPERTY_ERROR_EMAIL, request.getLocale( ) );
292 }
293
294 model.put( MARK_CONTACT_ALERT_MSG, strAlertMsg );
295 model.put( MARK_STYLE_LAST_NAME, strStyleLastName );
296 model.put( MARK_STYLE_FIRST_NAME, strStyleFirstName );
297 model.put( MARK_STYLE_OBJECT, strStyleObject );
298 model.put( MARK_STYLE_EMAIL, strStyleEmail );
299 model.put( MARK_STYLE_MESSAGE, strStyleMessage );
300 model.put( MARK_STYLE_CONTACT, strStyleContact );
301 }
302
303 int nIdContactList = Integer.parseInt( strIdContactList );
304 ContactList contactList = ContactListHome.findByPrimaryKey( nIdContactList, _plugin );
305
306 if ( !ContactListHome.listExists( contactList.getId( ), _plugin ) )
307 {
308 SiteMessageService.setMessage( request, PROPERTY_LIST_NOT_EXISTS, SiteMessage.TYPE_ERROR );
309 }
310
311 if ( !isVisible( request, contactList.getRole( ) ) )
312 {
313 SiteMessageService.setMessage( request, PROPERTY_NOT_AUTHORIZED, SiteMessage.TYPE_STOP );
314 }
315
316 String strComboItem = I18nService.getLocalizedString( PROPERTY_COMBO_CHOOSE, request.getLocale( ) );
317
318
319 ReferenceList listContact = ContactHome.getContactsByListWithString( contactList.getId( ), strComboItem, _plugin );
320
321 if ( SecurityService.isAuthenticationEnable( ) )
322 {
323 LuteceUser user = SecurityService.getInstance( ).getRegisteredUser( request );
324
325 if ( user != null )
326 {
327 model.put( MARK_MYLUTECE_USER, user );
328 }
329 }
330
331 model.put( MARK_CONTACTS_LIST, listContact );
332 model.put( MARK_VISITOR_LASTNAME, strVisitorLastName );
333 model.put( MARK_VISITOR_FIRSTNAME, strVisitorFirstName );
334 model.put( MARK_VISITOR_EMAIL, strVisitorEmail );
335 model.put( MARK_VISITOR_ADDRESS, strVisitorAddress );
336 model.put( MARK_OBJECT, strObject );
337 model.put( MARK_MESSAGE, strMessage );
338 model.put( MARK_ID_CONTACT_LIST, nIdContactList );
339
340 boolean bIsTosRequired = contactList.getTos( );
341 model.put( MARK_IS_TOS_REQUIRED, bIsTosRequired );
342
343 if ( bIsTosRequired )
344 {
345 String strTosMessage = contactList.getTosMessage( );
346 model.put( MARK_TOS_MESSAGE, strTosMessage );
347
348 boolean bTosAccepted = request.getParameter( PARAMETER_TOS_ACCEPTED ) != null;
349 model.put( MARK_TOS_ACCEPTED, bTosAccepted );
350 }
351
352 model.put( MARK_DEFAULT_CONTACT, ( ( strContact == null ) || ( strContact.equals( "" ) ) ) ? "0" : strContact );
353
354 return getXPage( TEMPLATE_XPAGE_CONTACT, request.getLocale( ), model );
355 }
356
357
358
359
360
361
362
363
364
365
366 @View( value = VIEW_CONTACT_LISTS, defaultView = true )
367 public XPage getLists( HttpServletRequest request ) throws SiteMessageException
368 {
369 Map<String, Object> model = new HashMap<String, Object>( );
370 Collection<ContactList> listOfLists = ContactListHome.findAll( _plugin );
371
372 Collection<ContactList> visibleList = new ArrayList<ContactList>( );
373
374 for ( ContactList currentList : listOfLists )
375 {
376 if ( isVisible( request, currentList.getRole( ) ) )
377 {
378 visibleList.add( currentList );
379 }
380 }
381
382 if ( visibleList.size( ) == 0 )
383 {
384 SiteMessageService.setMessage( request, PROPERTY_NO_LIST_VISIBLE, SiteMessage.TYPE_WARNING );
385 }
386 else
387 if ( visibleList.size( ) == 1 )
388 {
389 String strContactListId = StringUtils.EMPTY;
390
391 for ( ContactList onlyList : visibleList )
392 {
393 strContactListId = Integer.toString( onlyList.getId( ) );
394 }
395
396 Map<String, String> mapParameters = new HashMap<String, String>( );
397 mapParameters.put( PARAMETER_ID_CONTACT_LIST, strContactListId );
398
399 return redirect( request, VIEW_CONTACT_PAGE, mapParameters );
400 }
401
402 model.put( MARK_LIST_OF_LISTS, visibleList );
403
404 return getXPage( TEMPLATE_XPAGE_LISTS, request.getLocale( ), model );
405 }
406
407
408
409
410
411
412
413
414
415
416 @Action( ACTION_SEND_MESSAGE )
417 public XPage doSendMessage( HttpServletRequest request ) throws SiteMessageException
418 {
419 String strIdContactList = request.getParameter( PARAMETER_ID_CONTACT_LIST );
420 String strVisitorLastName = ( request.getParameter( PARAMETER_VISITOR_LASTNAME ) == null ) ? "" : request.getParameter( PARAMETER_VISITOR_LASTNAME );
421 String strVisitorFirstName = ( request.getParameter( PARAMETER_VISITOR_FIRSTNAME ) == null ) ? "" : request.getParameter( PARAMETER_VISITOR_FIRSTNAME );
422 String strVisitorAddress = ( request.getParameter( PARAMETER_VISITOR_ADDRESS ) == null ) ? "" : request.getParameter( PARAMETER_VISITOR_ADDRESS );
423 String strVisitorEmail = ( request.getParameter( PARAMETER_VISITOR_EMAIL ) == null ) ? "" : request.getParameter( PARAMETER_VISITOR_EMAIL );
424 String strObject = ( request.getParameter( PARAMETER_MESSAGE_OBJECT ) == null ) ? "" : request.getParameter( PARAMETER_MESSAGE_OBJECT );
425 String strMessage = ( request.getParameter( PARAMETER_MESSAGE ) == null ) ? "" : request.getParameter( PARAMETER_MESSAGE );
426 String strDateOfDay = DateUtil.getCurrentDateString( request.getLocale( ) );
427 String strContact = request.getParameter( PARAMETER_CONTACT );
428 int nContact = ( strContact == null ) ? 0 : Integer.parseInt( strContact );
429 int nIdContactList = Integer.parseInt( strIdContactList );
430 boolean bTosAccepted = request.getParameter( PARAMETER_TOS_ACCEPTED ) != null;
431
432 Map<String, String> mapParamError = new HashMap<String, String>( );
433 mapParamError.put( PARAMETER_ID_CONTACT_LIST, strIdContactList );
434 mapParamError.put( PARAMETER_VISITOR_LASTNAME, strVisitorLastName );
435 mapParamError.put( PARAMETER_VISITOR_FIRSTNAME, strVisitorFirstName );
436 mapParamError.put( PARAMETER_VISITOR_ADDRESS, strVisitorAddress );
437 mapParamError.put( PARAMETER_VISITOR_EMAIL, strVisitorEmail );
438 mapParamError.put( PARAMETER_CONTACT, strContact );
439 mapParamError.put( PARAMETER_MESSAGE_OBJECT, strObject );
440 mapParamError.put( PARAMETER_MESSAGE, strMessage );
441 if ( bTosAccepted )
442 {
443 mapParamError.put( PARAMETER_TOS_ACCEPTED, "1" );
444 }
445
446
447 if ( PluginService.isPluginEnable( JCAPTCHA_PLUGIN ) )
448 {
449 _captchaService = new CaptchaSecurityService( );
450
451 if ( !_captchaService.validate( request ) )
452 {
453 mapParamError.put( PARAMETER_SEND, "error_captcha" );
454
455 return redirect( request, VIEW_CONTACT_PAGE, mapParamError );
456 }
457 }
458
459
460 if ( nContact == 0 )
461 {
462 mapParamError.put( PARAMETER_SEND, "error_recipient" );
463
464 return redirect( request, VIEW_CONTACT_PAGE, mapParamError );
465 }
466
467 Contact contact = ContactHome.findByPrimaryKey( nContact, _plugin );
468 String strEmailContact = contact.getEmail( );
469 String strContactName = contact.getName( );
470
471
472 if ( strMessage.length( ) > 1000 )
473 {
474 strMessage = strMessage.substring( 0, 1000 );
475 }
476
477
478 if ( strVisitorLastName.equals( "" ) || strVisitorFirstName.equals( "" ) || strVisitorEmail.equals( "" ) || strContact.equals( "" )
479 || strObject.equals( "" ) || strMessage.equals( "" ) )
480 {
481 mapParamError.put( PARAMETER_SEND, "error_field" );
482
483 return redirect( request, VIEW_CONTACT_PAGE, mapParamError );
484 }
485
486
487
488 if ( StringUtil.checkEmail( strVisitorEmail ) != true )
489 {
490 mapParamError.put( PARAMETER_SEND, "error_email" );
491
492 return redirect( request, VIEW_CONTACT_PAGE, mapParamError );
493 }
494
495 ContactList contactlist = ContactListHome.findByPrimaryKey( nIdContactList, _plugin );
496 boolean bIsTosRequired = contactlist.getTos( );
497
498
499 if ( bIsTosRequired )
500 {
501 if ( !bTosAccepted )
502 {
503 mapParamError.put( PARAMETER_SEND, "error_tos" );
504
505 return redirect( request, VIEW_CONTACT_PAGE, mapParamError );
506 }
507 }
508
509 Map<String, String> model = new HashMap<String, String>( );
510 model.put( MARK_VISITOR_LASTNAME, strVisitorLastName );
511 model.put( MARK_VISITOR_FIRSTNAME, strVisitorFirstName );
512 model.put( MARK_VISITOR_ADDRESS, strVisitorAddress );
513 model.put( MARK_VISITOR_EMAIL, strVisitorEmail );
514 model.put( MARK_CONTACT_NAME, strContactName );
515 model.put( MARK_MESSAGE, strMessage );
516 model.put( MARK_CURRENT_DATE, strDateOfDay );
517
518 HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_MESSAGE_CONTACT, request.getLocale( ), model );
519
520 String strMessageText = template.getHtml( );
521
522 MailService.sendMailHtml( strEmailContact, strVisitorLastName, strVisitorEmail, strObject, strMessageText );
523 ContactHome.updateHits( nContact, nIdContactList, _plugin );
524
525 Map<String, String> mapParamSuccess = new HashMap<String, String>( );
526 mapParamSuccess.put( PARAMETER_ID_CONTACT_LIST, strIdContactList );
527 mapParamSuccess.put( PARAMETER_SEND, "done" );
528
529 return redirect( request, VIEW_CONTACT_PAGE, mapParamSuccess );
530 }
531 }