Merge branch 'MDL-73874-311' of https://github.com/BruceGoodGuy/moodle into MOODLE_31...
[moodle.git] / lib / cookies.js
blobc9c2004baae5f3e119797b348d3b95aef5bbaf98
1 /*******************************************************
2 COOKIE FUNCTIONALITY
3 Based on "Night of the Living Cookie" by Bill Dortch
4 (c) 2003, Ryan Parman
5 http://www.skyzyx.com
6 Distributed according to SkyGPL 2.1, http://www.skyzyx.com/license/
7 *******************************************************/
8 function cookie(name, value, expires, path, domain, secure)
10         // Passed Values
11         this.name=name;
12         this.value=value;
13         this.expires=expires;
14         this.path=path;
15         this.domain=domain;
16         this.secure=secure;
18         // Read cookie
19         this.read=function()
20         {
21                 // To allow for faster parsing
22                 var ck=document.cookie;
24                 var arg = this.name + "=";
25                 var alen = arg.length;
26                 var clen = ck.length;
27                 var i = 0;
29                 while (i < clen)
30                 {
31                         var j = i + alen;
32                         if (ck.substring(i, j) == arg)
33                         {
34                                 var endstr = ck.indexOf (";", j);
35                                 if (endstr == -1) endstr = ck.length;
36                                 return unescape(ck.substring(j, endstr));
37                         }
38                         i = ck.indexOf(" ", i) + 1;
39                         if (i == 0) break;
40                 }
41                 return null;
42         };
44         // Set cookie
45         this.set=function()
46         {
47                 // Store initial value of "this.expires" for re-initialization.
48                 expStore=this.expires;
50                 // Set time to absolute zero.
51                 exp = new Date();
52                 base = new Date(0);
53                 skew = base.getTime();
54                 if (skew > 0)  exp.setTime (exp.getTime() - skew);
55                 exp.setTime(exp.getTime() + (this.expires*24*60*60*1000));
56                 this.expires=exp;
58                 document.cookie = this.name + "=" + escape (this.value) +
59                                 ((this.expires) ? "; expires=" + this.expires.toGMTString() : "") +
60                                 ((this.path) ? "; path=" + this.path : "") +
61                                 ((this.domain) ? "; domain=" + this.domain : "") +
62                                 ((this.secure) ? "; secure" : "");
64                 // Re-initialize
65                 this.expires=expStore;
66         };
68         // Kill cookie
69         this.kill=function()
70         {
71                 document.cookie = this.name + "=" +
72                                 ((this.path) ? "; path=" + this.path : "") +
73                                 ((this.domain) ? "; domain=" + this.domain : "") +
74                                 "; expires=Thu, 01-Jan-70 00:00:01 GMT";
75         };
77         // Change cookie settings.
78         this.changeName=function(chName) { this.kill(); this.name=chName; this.set(); };
79         this.changeVal=function(chVal) { this.kill(); this.value=chVal; this.set(); };
80         this.changeExp=function(chExp) { this.kill(); this.expires=chExp; this.set(); };
81         this.changePath=function(chPath) { this.kill(); this.path=chPath; this.set(); };
82         this.changeDomain=function(chDom) { this.kill(); this.domain=chDom; this.set(); };
83         this.changeSecurity=function(chSec) { this.kill(); this.secure=chSec; this.set(); }