-
Notifications
You must be signed in to change notification settings - Fork 0
/
cookie.js
284 lines (268 loc) · 9.79 KB
/
cookie.js
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
import * as Storage from "@storage";
import * as Env from "@env";
window.storage = Storage.open(Env.path("USER_DOCUMENTS") + "/data.db");
var root = storage.root || initDb(storage);
function initDb(storage) {
storage.root = {
version: 1,
meta: {},
localStorage: {},
sessionStorage: {},
cookie: {}
};
storage.commit();
return storage.root;
}
window.localStorage = {
setItem: function (Key, Val) {
root["localStorage"][Key] = Val;
storage.commit();
},
getItem: function (Key) {
if (!root["localStorage"][Key]) return "";
return root["localStorage"][Key];
},
removeItem: function (Key) {
this.setItem(Key, undefined);
},
clear: function () {
root["localStorage"] = {};
storage.commit();
}
};
window.sessionStorage = {
setItem: function (Key, Val) {
root["sessionStorage"][Key] = Val;
},
getItem: function (Key) {
if (!root["sessionStorage"][Key]) return "";
return root["sessionStorage"][Key];
},
removeItem: function (Key) {
this.setItem(Key, undefined);
},
clear: function () {
root["sessionStorage"] = {};
}
};
String.prototype.toBoolean = function () {
var regexTrue = /^\s*(true|1|on|yes)\s*$/i;
var regexFalse = /^\s*(false|0|off|no)\s*$/i;
if (regexTrue.test(this)) return true;
if (regexFalse.test(this)) return false;
return Boolean(this);
}
Object.defineProperty(document, "cookie", {
get: function () {
var setDefault = new Map([
[".", ""],
["", ""],
["none", ""],
["None", ""],
["undefined", ""],
["Undefined", ""],
["null", ""],
["Null", ""],
[undefined, ""],
[null, ""],
]);
var aDomain = location.host;
var aPath = location.pathname;
if (setDefault.has(aDomain)) var aDomain = "localhost";
var domainPart = aDomain.split(".");
var pathPart = aPath.split("/");
var domainPart = removeEmpty(domainPart);
var pathPart = removeEmpty(pathPart);
if (!location.host) pathPart.shift();
var domainStr = "";
var dot = ".";
var forCounter = 0;
var returnObj = {};
var aLength = domainPart.length;
for (var i = aLength - 1; i > -1; i--) {
forCounter++;
if (forCounter <= 1) var isLocked = 1;
if (aLength == 1) var isLocked = 0;
var aPart = domainPart[i].replace(/\s*/g,"");
if (!aPart) continue;
var domainStr = aPart + domainStr;
if (!isLocked) getCookies(domainStr);
if (forCounter == aLength) var dot = "";
var domainStr = dot + domainStr;
var isLocked = 0;
}
function getCookies(aDomain) {
var storRoot = storage.root["cookie"][aDomain];
var storChild = storRoot;
var aLength = pathPart.length;
for (var j = 0; j <= aLength; j++) {
if (j == aLength && j) break;
if (j == aLength && !j) var aLocked = 1;
if (!storChild) break;
storRoot = storChild;
for (var k in storRoot) {
if (!storRoot[k]["key"]) continue;
var timeNow = new Date();
var timeStmp = storRoot[k]["expires"].getTime();
var expr = (timeNow > timeStmp);
if (expr) delete storRoot[k];
if (!expr) returnObj[storRoot[k]["key"]] = storRoot[k]["value"];
if (!expr && !returnObj[storRoot[k]["key"]]) returnObj[storRoot[k]["key"]] = "";
}
if (!aLocked) storChild = storRoot["@" + pathPart[j]];
}
}
function removeEmpty(arr) {
for (var i = 0; i < arr.length; i++) {
var aPart = arr[i];
if (aPart) var aPart = aPart.replace(/\s*/g, "");
if (!aPart) arr.splice(i, 1);
if (!aPart) i--;
}
return arr;
}
var returnStr = "";
var addtion = "; ";
var forCounter = 0;
var objLength = Object.keys(returnObj).length;
for (var i in returnObj) {
forCounter++;
if (forCounter == objLength) var addtion = "";
returnStr += i + "=" + returnObj[i] + addtion;
}
return returnStr;
},
set: function (val) {
var setDefault = new Map([
[".", ""],
["", ""],
["none", ""],
["None", ""],
["undefined", ""],
["Undefined", ""],
["null", ""],
["Null", ""],
[undefined, ""],
[null, ""],
]);
var cookieParts = val.split(";");
var predefined = ["key", "value"];
var forCounter = 0;
var cookieMap = {};
for (var i = 0; i < cookieParts.length; i++) {
if (forCounter <= 1) var isLocked = 1;
var aPart = cookieParts[i].trim();
if (!aPart) forCounter++;
if (!aPart) continue;
var aMap = aPart.split("=");
if (isLocked) aMap[1] = aMap[forCounter];
if (isLocked) aMap[0] = predefined[forCounter];
if (aMap[0] == "expires") aMap[1] = new Date(aMap[1]);
if (aMap[0] == "secure" && aMap[1] == undefined) aMap[1] = "1";
if (aMap[0] == "secure") aMap[1] = aMap[1].toBoolean();
cookieMap[aMap[0]] = aMap[1];
var isLocked = 0;
if (forCounter <= 0) i--;
forCounter++;
}
if (!cookieMap["value"]) cookieMap["value"] = "";
if (!cookieMap["expires"]) var timeNow = new Date();
if (!cookieMap["expires"]) var timeFtr = timeNow.getDate() + 7;
if (!cookieMap["expires"]) var timeCrcl = new Date();
if (!cookieMap["expires"]) timeCrcl.setDate(timeFtr);
if (!cookieMap["expires"]) cookieMap["expires"] = timeCrcl;
if (setDefault.has(cookieMap["domain"]))
cookieMap["domain"] = "localhost";
var domainPart = cookieMap["domain"].split(".");
var aDomain = "";
var dot = ".";
var forCounter = 0;
var storRoot = storage.root["cookie"];
var domainPart = removeEmpty(domainPart);
for (var i = domainPart.length - 1; i > -1; i--) {
forCounter++;
if (forCounter <= 1) var isLocked = 1;
var aPart = domainPart[i].replace(/\s*/g,"");
if (domainPart.length == 1) var isLocked = 0;
if (!aPart) continue;
var aDomain = aPart + aDomain;
if (!isLocked && !storRoot[aDomain]) storRoot[aDomain] = {};
if (forCounter == domainPart.length) var dot = "";
var aDomain = dot + aDomain;
var isLocked = 0;
}
if (setDefault.has(cookieMap["path"]))
cookieMap["path"] = "/";
var pathPart = cookieMap["path"].split("/");
var aPath = "";
var slash = "/";
var forCounter = 0;
var storRoot = storRoot[aDomain];
var pathPart = removeEmpty(pathPart);
for (var i = 0; i < pathPart.length; i++) {
forCounter++;
var aPart = pathPart[i].trim();
if (!aPart) continue;
if (!storRoot[aPart]) storRoot["@"+aPart] = {};
storRoot = storRoot["@"+aPart];
aPath += slash + aPart;
}
if (!aPath) aPath = "/";
function removeEmpty(arr) {
for (var i = 0; i < arr.length; i++) {
var aPart = arr[i];
if (aPart) var aPart = aPart.replace(/\s*/g, "");
if (!aPart) arr.splice(i, 1);
if (!aPart) i--;
}
return arr;
}
cookieMap["domain"] = aDomain;
cookieMap["path"] = aPath;
storRoot[cookieMap["key"]] = cookieMap;
storage.commit();
}
});
window.Cookies = {
set: function (cname, cvalue, settings) {
if (!cname) return "";
if (!cvalue) var cvalue = "";
if (!settings) var settings = {};
if (!settings.path) settings.path = "/";
if (!settings.expires) settings.expires = 7;
var d = new Date();
d.setTime(d.getTime() + (settings.expires * 24 * 60 * 60 * 1000));
var expires = d.toUTCString();
var cs = cname + "=" + cvalue + "; ";
var addtion = "; ";
var cl = Object.keys(settings).length;
var forCounter = 0;
for (var i in settings) {
forCounter++;
if (forCounter == cl) var addtion = "";
if (i != "expires") settings[i] = settings[i].toString();
if (i == "expires") settings[i] = expires;
cs += i + "=" + settings[i] + addtion;
}
document.cookie = cs;
return cs;
},
get: function (cname) {
var name = cname + "=";
var ca = document.cookie.split(';');
var returnObj = {};
for (var i = 0; i < ca.length; i++) {
var c = ca[i].trim();
if (!cname) var kv = c.split("=");
if (!cname) returnObj[kv[0]] = kv[1];
if (c.indexOf(name) == 0) { return c.substring(name.length, c.length); }
}
if (!cname) return returnObj;
return "";
},
remove: function (cname, settings) {
if (!settings) var settings = {};
settings.expires = 0;
return this.set(cname, "", settings);
}
}