3
Cookie Functions -- "Night of the Living Cookie" Version (25-Jul-96)
4
Written by: Bill Dortch, hIdaho Design <bdortch@hidaho.com>
5
The following functions are released to the public domain.
7
Refactored for Foswiki by Arthur Clemens 2006.
11
The preferred way for reading and writing cookies is using getPref and setPref, otherwise the limit of 20 cookies per domain is reached soon. See http://foswiki.org/Support/DataStorageInUserCookie
13
var foswiki; if (foswiki == undefined) foswiki = {};
16
FOSWIKI_PREF_COOKIE_NAME:"FOSWIKIPREF",
18
Separates key-value pairs
20
COOKIE_PREF_SEPARATOR:"|",
22
Separates key from value
24
COOKIE_PREF_VALUE_SEPARATOR:"=",
26
By default expire one year from now.
28
COOKIE_EXPIRY_TIME:365 * 24 * 60 * 60 * 1000,
31
Writes data to a user cookie, using key-value notation. If the key already exists, the value is overwritten. If the key is new, a new key/value pair is created.
32
Characters '|' and '=' are reserved as separators.
33
@param inPrefName : (String) name of the preference to write, for instance 'SHOWATTACHMENTS'
34
@param inPrefValue : (String) stringified value to write, for instance '1'
36
setPref:function(inPrefName, inPrefValue) {
37
var prefName = foswiki.Pref._getSafeString(inPrefName);
38
var prefValue = (isNaN(inPrefValue)) ? foswiki.Pref._getSafeString(inPrefValue) : inPrefValue;
39
var cookieString = foswiki.Pref._getPrefCookie();
40
var prefs = cookieString.split(foswiki.Pref.COOKIE_PREF_SEPARATOR);
41
var index = foswiki.Pref._getKeyValueLoc(prefs, prefName);
43
// updating this entry is done by removing the existing entry from the array and then pushing the new key-value onto it
44
prefs.splice(index, 1);
46
// else not found, so don't remove an existing entry
47
var keyvalueString = prefName + foswiki.Pref.COOKIE_PREF_VALUE_SEPARATOR + prefValue;
48
prefs.push(keyvalueString);
49
foswiki.Pref._writePrefValues(prefs);
53
Reads the value of a preference.
54
Characters '|' and '=' are reserved as separators.
55
@param inPrefName (String): name of the preference to read, for instance 'SHOWATTACHMENTS'
56
@return The value of the preference; an empty string when no value is found.
58
getPref:function(inPrefName) {
59
var prefName = foswiki.Pref._getSafeString(inPrefName);
60
return foswiki.Pref.getPrefValueFromPrefList(prefName, foswiki.Pref.getPrefList());
64
Reads the value of a preference from an array of key-value pairs. Use in conjunction with getPrefList() when you want to store the key-value pairs for successive look-ups.
65
@param inPrefName (String): name of the preference to read, for instance 'SHOWATTACHMENTS'
66
@param inPrefList (Array): list of key-value pairs, retrieved with getPrefList()
67
@return The value of the preference; an empty string when no value is found.
69
getPrefValueFromPrefList:function(inPrefName, inPrefList) {
70
var keyvalue = foswiki.Pref._getKeyValue(inPrefList, inPrefName);
71
if (keyvalue != null) return keyvalue[1];
76
Gets the list of all values set with setPref.
77
@return An Array of key-value pair pref values; null if no value has been set before.
79
getPrefList:function() {
80
var cookieString = foswiki.Pref._getPrefCookie();
81
if (!cookieString) return null;
82
return cookieString.split(foswiki.Pref.COOKIE_PREF_SEPARATOR);
86
Retrieves the value of the cookie specified by "name".
87
@param inName : (String) identifier name of the cookie
88
@return (String) the cookie value; null if no cookie with name inName has been set.
90
getCookie:function(inName) {
91
var arg = inName + "=";
92
var alen = arg.length;
93
var clen = document.cookie.length;
97
if (document.cookie.substring(i, j) == arg) {
98
return foswiki.Pref._getCookieVal(j);
100
i = document.cookie.indexOf(" ", i) + 1;
107
Creates a new cookie or updates an existing cookie.
108
@param inName : (String) identifier name of the cookie
109
@param inValue : (String) stringified cookie value, for instance '1'
110
@param inExpires : (Date) (optional) the expiration data of the cookie; if omitted or null, expires the cookie at the end of the current session
111
@param inPath : (String) (optional) the path for which the cookie is valid; if omitted or null, uses the path of the current document
112
@param inDomain : (String) (optional) the domain for which the cookie is valid; if omitted or null, uses the domain of the current document
113
@param inUsesSecure : (Boolean) (optional) whether cookie transmission requires a secure channel (https)
115
To call setCookie using name, value and path, write:
117
foswiki.Pref.setCookie ("myCookieName", "myCookieValue", null, "/");
119
To set a secure cookie for path "/myPath", that expires after the current session, write:
121
foswiki.Pref.setCookie ("myCookieName", "myCookieValue", null, "/myPath", null, true);
124
setCookie:function(inName, inValue, inExpires, inPath, inDomain, inUsesSecure) {
125
var cookieString = inName + "=" + escape (inValue) +
126
((inExpires) ? "; expires=" + inExpires.toGMTString() : "") +
127
((inPath) ? "; path=" + inPath : "") +
128
((inDomain) ? "; domain=" + inDomain : "") +
129
((inUsesSecure) ? "; secure" : "");
130
document.cookie = cookieString;
134
Function to delete a cookie. (Sets expiration date to start of epoch)
135
@param inName : (String) identifier name of the cookie
136
@param inPath : (String) The path for which the cookie is valid. This MUST be the same as the path used to create the cookie, or null/omitted if no path was specified when creating the cookie.
137
@param inDomain : (String) The domain for which the cookie is valid. This MUST be the same as the domain used to create the cookie, or null/omitted if no domain was specified when creating the cookie.
139
deleteCookie:function(inName, inPath, inDomain) {
140
if (foswiki.Pref.getCookie(inName)) {
141
document.cookie = inName + "=" + ((inPath) ? "; path=" + inPath : "") + ((inDomain) ? "; domain=" + inDomain : "") + "; expires=Thu, 01-Jan-70 00:00:01 GMT";
145
/* PRIVILIGED METHODS */
148
Finds a key-value pair in an array.
149
@param inKeyValues: (Array) the array to iterate
150
@param inKey: (String) the key to find in the array
151
@return The first occurrence of a key-value pair, where key == inKey; null if none is found.
153
_getKeyValue:function(inKeyValues, inKey) {
154
if (!inKeyValues) return null;
155
var i = inKeyValues.length;
157
var keyvalue = inKeyValues[i].split(foswiki.Pref.COOKIE_PREF_VALUE_SEPARATOR);
158
if (keyvalue[0] == inKey) return keyvalue;
164
Finds the location of a key-value pair in an array.
165
@param inKeyValues: (Array) the array to iterate
166
@param inKey: (String) the key to find in the array
167
@return The location of the first occurrence of a key-value tuple, where key == inKey; -1 if none is found.
169
_getKeyValueLoc:function(inKeyValues, inKey) {
170
if (!inKeyValues) return null;
171
var i = inKeyValues.length;
173
var keyvalue = inKeyValues[i].split(foswiki.Pref.COOKIE_PREF_VALUE_SEPARATOR);
174
if (keyvalue[0] == inKey) return i;
180
Writes a cookie with the stringified array values of inValues.
181
@param inValues: (Array) an array with key-value tuples
183
_writePrefValues:function(inValues) {
184
var cookieString = (inValues != null) ? inValues.join(foswiki.Pref.COOKIE_PREF_SEPARATOR) : '';
185
var expiryDate = new Date ();
186
foswiki.Pref._fixCookieDate (expiryDate); // Correct for Mac date bug - call only once for given Date object!
187
expiryDate.setTime (expiryDate.getTime() + foswiki.Pref.COOKIE_EXPIRY_TIME);
188
foswiki.Pref.setCookie(foswiki.Pref.FOSWIKI_PREF_COOKIE_NAME, cookieString, expiryDate, '/');
192
Gets the FOSWIKI_PREF_COOKIE_NAME cookie; creates a new cookie if it does not exist.
193
@return The pref cookie.
195
_getPrefCookie:function() {
196
var cookieString = foswiki.Pref.getCookie(foswiki.Pref.FOSWIKI_PREF_COOKIE_NAME);
197
if (cookieString == undefined) {
204
Strips reserved characters '|' and '=' from the input string.
205
@return The stripped string.
207
_getSafeString:function(inString) {
208
var regex = new RegExp(/[|=]/);
209
return inString.replace(regex, "");
213
Retrieves the decoded value of a cookie.
214
@param inOffset : (Number) location of value in full cookie string.
216
_getCookieVal:function(inOffset) {
217
var endstr = document.cookie.indexOf (";", inOffset);
219
endstr = document.cookie.length;
221
return unescape(document.cookie.substring(inOffset, endstr));
225
Function to correct for 2.x Mac date bug. Call this function to
226
fix a date object prior to passing it to setCookie.
227
IMPORTANT: This function should only be called *once* for
228
any given date object! See example at the end of this document.
230
_fixCookieDate:function(inDate) {
231
var base = new Date(0);
232
var skew = base.getTime(); // dawn of (Unix) time - should be 0
233
if (skew > 0) { // Except on the Mac - ahead of its time
234
inDate.setTime(inDate.getTime() - skew);
238
// Set to true to suppress mandatory field validation on save
239
// (see foswiki_edit.js)
240
validateSuppressed : false