Skip to content

JSPWIKI-1117 #44

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,18 @@ public static void reloadPreferences( final PageContext pageContext ) {
prefs.put("livepreview", TextUtil.getStringProperty( props, "jspwiki.defaultprefs.template.livepreview", "true" ) );
prefs.put("previewcolumn", TextUtil.getStringProperty( props, "jspwiki.defaultprefs.template.previewcolumn", "true" ) );

prefs.put("cookies", TextUtil.getStringProperty( props, "jspwiki.defaultprefs.template.cookies", "false" ) );

// FIXME: editormanager reads jspwiki.editor -- which of both properties should continue
prefs.put("editor", TextUtil.getStringProperty( props, "jspwiki.defaultprefs.template.editor", "plain" ) );
parseJSONPreferences( (HttpServletRequest) pageContext.getRequest(), prefs );

final String prefVal = TextUtil.urlDecodeUTF8( HttpUtil.retrieveCookieValue(
(HttpServletRequest) pageContext.getRequest(), "JSPWikiUserPrefs" ) );
final String prefCookieVal = TextUtil.urlDecodeUTF8( HttpUtil.retrieveCookieValue(
(HttpServletRequest) pageContext.getRequest(), "CookiePrefs" ) );

parseJSONPreferences(prefs, prefVal);
parseJSONPreferences(prefs, prefCookieVal);
pageContext.getSession().setAttribute( SESSIONPREFS, prefs );
}

Expand All @@ -125,19 +133,21 @@ public static void reloadPreferences( final PageContext pageContext ) {
* @param request
* @param prefs The default hashmap of preferences
*/
private static void parseJSONPreferences( final HttpServletRequest request, final Preferences prefs ) {
final String prefVal = TextUtil.urlDecodeUTF8( HttpUtil.retrieveCookieValue( request, "JSPWikiUserPrefs" ) );
if( prefVal != null ) {
static void parseJSONPreferences( final Preferences prefs, final String prefVal ) {
if( prefVal != null ) {
String key = null;
// Convert prefVal JSON to a generic hashmap
@SuppressWarnings( "unchecked" ) final Map< String, String > map = new Gson().fromJson( prefVal, Map.class );
for( String key : map.keySet() ) {
key = TextUtil.replaceEntities( key );
for( Entry<String, String> keyVals : map.entrySet() ) {
key = TextUtil.replaceEntities( keyVals.getKey() );
if(key != null){
// Sometimes this is not a String as it comes from the Cookie set by Javascript
final Object value = map.get( key );
final Object value = keyVals.getValue();
if( value != null ) {
prefs.put( key, value.toString() );
}
}
}
}
}

Expand Down
5 changes: 5 additions & 0 deletions jspwiki-main/src/main/resources/templates/default.properties
Original file line number Diff line number Diff line change
Expand Up @@ -692,3 +692,8 @@ javascript.tip.default.title=More...

javascript.prefs.areyousure=Without clicking the Save User Preferences button, \
your changes will be lost. Are you sure you want to exit this page?

javascript.prefs.cookiepolicy.message=This application uses cookies to work properly. \
By using the application you understand and accept the use of necessary cookies. Please confirm to proceed.
javascript.prefs.cookiepolicy.confirm.button=I understand and accept

Original file line number Diff line number Diff line change
Expand Up @@ -637,3 +637,6 @@ javascript.sort.click = Klikkaa j
javascript.sort.descending = Laskeva j�rjestys. Klikkaa vaihtaaksesi.
javascript.tip.default.title = Lis��...
javascript.sectionediting.label = Osion�kym�
javascript.prefs.cookiepolicy.message=T�m� sovellus vaatii ev�steiden k�yt�n toimiakseen oikein. \
K�ytt�m�ll� sovellusta ymm�rr�t ja hyv�ksyt ev�steiden k�yt�n. Ole hyv� ja vahvista jatkaaksesi.
javascript.prefs.cookiepolicy.confirm.button= Ymm�rr�n ja hyv�ksyn vaaditut ev�steet
95 changes: 94 additions & 1 deletion jspwiki-war/src/main/webapp/templates/default/commonheader.jsp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
<%@ page import="org.apache.wiki.api.core.*" %>
<%@ page import="org.apache.wiki.ui.*" %>
<%@ page import="org.apache.wiki.util.*" %>
<%@ page import="org.apache.wiki.api.spi.Wiki" %>
<%@ page import="org.apache.wiki.preferences.Preferences" %>
<%@ page import="java.util.*" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
Expand Down Expand Up @@ -57,12 +58,31 @@ BOOTSTRAP, IE compatibility / http://getbootstrap.com/getting-started/#support-i
</c:if>
</wiki:PageExists>


<%-- COOKIE read client preferences --%>
<%
Preferences.setupPreferences(pageContext);

Engine wiki = Wiki.engine().find( getServletConfig() );
// Create wiki context and check for authorization
Context wikiContext = Wiki.context().create( wiki, request, ContextEnum.PAGE_VIEW.getRequestContext() );

String cookiesOn = (String)request.getParameter("cookies");
if(cookiesOn != null)
wikiContext.setVariable("cookies", cookiesOn);

// Redirect if request was for a special page
String redirect = wikiContext.getRedirectURL( );
if( redirect != null && cookiesOn == null)
{
response.sendRedirect( redirect );
return;
}
%>


<%-- SKINS : extra stylesheets, extra javascript --%>


<%-- Localized JS; must come before any css, to avoid blocking immediate execution --%>
<%-- var LocalizedStrings= { "javascript.<xx>":"...", etc. } --%>
<script type="text/javascript">//<![CDATA[
Expand All @@ -86,6 +106,7 @@ String.I18N.PREFIX = "javascript.";

<script src="<wiki:Link format='url' jsp='scripts/haddock.js'/>"></script>

<script src="<wiki:Link format='url' jsp='scripts/haddock-edit.js'/>"></script>
<wiki:IncludeResources type="script"/>


Expand Down Expand Up @@ -156,4 +177,76 @@ String.I18N.PREFIX = "javascript.";
src="<wiki:Link format='url' templatefile='skins/' /><c:out value='${prefs.SkinName}/skin.js' />" ></script>
</c:if>



<%-- Support for cookie acceptance --%>
<c:if test='${!(prefs.cookies)}'>
<script type="text/javascript">
document.addEventListener("DOMContentLoaded", readyForCookieTest);
var appBaseName = document.querySelector('meta[name="wikiApplicationName"]').content;

function sendAjaxCookieConfirmation(dialogElem){
var request = new XMLHttpRequest();
var appBaseUrl = "/"+appBaseName+"/";
var cookieAcceptParam = "?cookies=true";
request.open('GET', appBaseUrl+cookieAcceptParam, true);

request.onload = function() {
if (this.status >= 200 && this.status < 400) {
// Success!
var resp = this.response;
console.log("Cookies accepted!");
dialogElem.toggle();
dialogElem.hide();
dialogElem.destroy();
} else {
// We reached our target server, but it returned an error
}
};

request.onerror = function() {
// There was a connection error of some sort
};
request.send();
}
var cookiesAllow = ${prefs.cookies};

function setCookie(cname, cvalue, exdays) {
var d = new Date();
// Cookie setting lasts for 1 day before deleted from browser.
// TODO: Setup in server preference for client's preferred cookie lifetime.
d.setTime(d.getTime() + (exdays*24*60*60*1000));
var expires = "expires="+ d.toUTCString();
document.cookie ="CookiePrefs={"+ cname + ":'" + cvalue + "'}";
}

function readyForCookieTest(){
if(!cookiesAllow){
var cookieDialogHtml = "<div class='dialog-message'>"+LocalizedStrings["javascript.prefs.cookiepolicy.message"]+"</div>";
var cookieOkBtnHtml = "<div class='btn-row' style='padding: 5px;width: fit-content;margin-left: auto;'><button class='btn-accept-cookies btn btn-success'>"+LocalizedStrings["javascript.prefs.cookiepolicy.confirm.button"]+"</button></div>";

var dialog = new Dialog({
caption:self.ApplicationName || appBaseName,
showNow:true,
body: (cookieDialogHtml+cookieOkBtnHtml),
relativeTo:$(".context-view")
});

//adopt one or more DOM elements as body of the DIALOG
dialog.show();

var cookiesOkEvent = function cookiesConfirm(){
setCookie("cookies", true, 1);
sendAjaxCookieConfirmation(dialog);
}

var cookieOkBtn = document.getElementsByClassName("btn-accept-cookies");
cookieOkBtn[0].addEventListener("click", cookiesOkEvent, false);
}else{
console.log("Cookie policy: accepted");
}
}
</script>
</c:if>

<wiki:Include page="localheader.jsp"/>