view cs/cookies.js @ 78:f833a888c548

add cookie-based PIN system, and update laterlinks to use it
author paulo
date Thu, 02 Jun 2016 00:27:50 -0700
parents
children 4d3f845c4ef2
line source
1 /*\
2 |*|
3 |*| :: cookies.js ::
4 |*|
5 |*| A complete cookies reader/writer framework with full unicode support.
6 |*|
7 |*| Revision #1 - September 4, 2014
8 |*|
9 |*| https://developer.mozilla.org/en-US/docs/Web/API/document.cookie
10 |*| https://developer.mozilla.org/User:fusionchess
11 |*|
12 |*| This framework is released under the GNU Public License, version 3 or later.
13 |*| http://www.gnu.org/licenses/gpl-3.0-standalone.html
14 |*|
15 |*| Syntaxes:
16 |*|
17 |*| * docCookies.setItem(name, value[, end[, path[, domain[, secure]]]])
18 |*| * docCookies.getItem(name)
19 |*| * docCookies.removeItem(name[, path[, domain]])
20 |*| * docCookies.hasItem(name)
21 |*| * docCookies.keys()
22 |*|
23 \*/
25 var docCookies = {
26 getItem: function (sKey) {
27 if (!sKey) { return null; }
28 return decodeURIComponent(document.cookie.replace(new RegExp("(?:(?:^|.*;)\\s*" + encodeURIComponent(sKey).replace(/[\-\.\+\*]/g, "\\$&") + "\\s*\\=\\s*([^;]*).*$)|^.*$"), "$1")) || null;
29 },
30 setItem: function (sKey, sValue, vEnd, sPath, sDomain, bSecure) {
31 if (!sKey || /^(?:expires|max\-age|path|domain|secure)$/i.test(sKey)) { return false; }
32 var sExpires = "";
33 if (vEnd) {
34 switch (vEnd.constructor) {
35 case Number:
36 sExpires = vEnd === Infinity ? "; expires=Fri, 31 Dec 9999 23:59:59 GMT" : "; max-age=" + vEnd;
37 break;
38 case String:
39 sExpires = "; expires=" + vEnd;
40 break;
41 case Date:
42 sExpires = "; expires=" + vEnd.toUTCString();
43 break;
44 }
45 }
46 document.cookie = encodeURIComponent(sKey) + "=" + encodeURIComponent(sValue) + sExpires + (sDomain ? "; domain=" + sDomain : "") + (sPath ? "; path=" + sPath : "") + (bSecure ? "; secure" : "");
47 return true;
48 },
49 removeItem: function (sKey, sPath, sDomain) {
50 if (!this.hasItem(sKey)) { return false; }
51 document.cookie = encodeURIComponent(sKey) + "=; expires=Thu, 01 Jan 1970 00:00:00 GMT" + (sDomain ? "; domain=" + sDomain : "") + (sPath ? "; path=" + sPath : "");
52 return true;
53 },
54 hasItem: function (sKey) {
55 if (!sKey) { return false; }
56 return (new RegExp("(?:^|;\\s*)" + encodeURIComponent(sKey).replace(/[\-\.\+\*]/g, "\\$&") + "\\s*\\=")).test(document.cookie);
57 },
58 keys: function () {
59 var aKeys = document.cookie.replace(/((?:^|\s*;)[^\=]+)(?=;|$)|^\s*|\s*(?:\=[^;]*)?(?:\1|$)/g, "").split(/\s*(?:\=[^;]*)?;\s*/);
60 for (var nLen = aKeys.length, nIdx = 0; nIdx < nLen; nIdx++) { aKeys[nIdx] = decodeURIComponent(aKeys[nIdx]); }
61 return aKeys;
62 }
63 };