]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/javascript/util/Cookie.js
free Cookie library. persists cookies.
[Evergreen.git] / Open-ILS / src / javascript / util / Cookie.js
1 /*
2
3 DISCLAIMER: THESE JAVASCRIPT FUNCTIONS ARE SUPPLIED 'AS IS', WITH 
4 NO WARRANTY EXPRESSED OR IMPLIED. YOU USE THEM AT YOUR OWN RISK. 
5 PAUL STEPHENS DOES NOT ACCEPT ANY LIABILITY FOR 
6 ANY LOSS OR DAMAGE RESULTING FROM THEIR USE, HOWEVER CAUSED. 
7
8 Paul Stephens' cookie-handling object library
9
10 Version 2.1
11 2.0 - Introduces field names
12 2.1 - Fixes bug where undefined embedded fields[] elements weren't written to disk
13
14 www.paulspages.co.uk 
15
16 TO USE THIS LIBRARY, INSERT ITS CONTENTS IN THE <HEAD> SECTION 
17 OF YOUR WEB PAGE SOURCE, BEFORE ANY OTHER JAVASCRIPT ROUTINES.
18
19 (C) Paul Stephens, 2001-2003. Feel free to use this code, but please leave this comment block in. This code must not be sold, either alone or as part of an application, without the consent of the author.
20 */
21
22 function cookieObject(name, expires, accessPath) {
23 var i, j
24 this.name = name
25 this.fieldSeparator = "#"
26 this.found = false
27 this.expires = expires
28 this.accessPath = accessPath
29 this.rawValue = ""
30 this.fields = new Array()
31 this.fieldnames = new Array() 
32 if (arguments.length > 3) { // field name(s) specified
33   j = 0
34   for (i = 3; i < arguments.length; i++) {
35     this.fieldnames[j] = arguments[i]    
36     j++
37   }
38   this.fields.length = this.fieldnames.length 
39 }
40 this.read = ucRead
41
42 this.write = ucWrite
43
44 this.remove = ucDelete
45 this.get = ucFieldGet
46 this.put = ucFieldPut
47 this.namepos = ucNamePos
48 this.read()
49 }
50
51
52 function ucFieldGet(fieldname) {
53 var i = this.namepos(fieldname)
54 if (i >=0) {
55   return this.fields[i]
56 } else {
57   return "BadFieldName!"
58 }
59 }
60
61 function ucFieldPut (fieldname, fieldval) {
62 var i = this.namepos(fieldname)
63 if (i >=0) {
64   this.fields[i] = fieldval
65   return true
66 } else {
67   return false
68 }
69 }
70
71 function ucNamePos(fieldname) {
72 var i 
73 for (i = 0; i < this.fieldnames.length; i++) {
74   if (fieldname == this.fieldnames[i]) {
75     return i
76   }
77 }
78 return -1
79 }
80
81
82 function ucWrite() {      
83   var cookietext = this.name + "=" 
84
85 // concatenate array elements into cookie string
86
87 // Special case - single-field cookie, so write without # terminator
88 if (this.fields.length == 1) {
89   cookietext += escape(this.fields[0])
90   } else { // multi-field cookie
91     for (i= 0; i < this.fields.length; i++) {
92       cookietext += escape(this.fields[i]) + this.fieldSeparator }
93   }
94
95
96 // Set expiry parameter, if specified
97     if (this.expires != null) {  
98       if (typeof(this.expires) == "number") { // Expiry period in days specified  
99         var today=new Date()     
100         var expiredate = new Date()      
101         expiredate.setTime(today.getTime() + 1000*60*60*24*this.expires)
102         cookietext += "; expires=" + expiredate.toGMTString()
103       } else { // assume it's a date object
104         cookietext +=  "; expires=" + this.expires.toGMTString()
105       } // end of typeof(this.expires) if
106     } // end of this.expires != null if 
107    
108 // add path, if specified
109    if (this.accessPath != null) {
110    cookietext += "; PATH="+this.accessPath }
111
112 // write cookie
113    // alert("writing "+cookietext)
114    document.cookie = cookietext 
115    return null  
116 }
117
118
119 function ucRead() {
120   var search = this.name + "="                       
121   var CookieString = document.cookie            
122   this.rawValue = null
123   this.found = false     
124   if (CookieString.length > 0) {                
125     offset = CookieString.indexOf(search)       
126     if (offset != -1) {                         
127       offset += search.length                   
128       end = CookieString.indexOf(";", offset)   
129       if (end == -1) {  // cookie is last item in the string, so no terminator                        
130        end = CookieString.length }              
131       this.rawValue = CookieString.substring(offset, end)                                   
132       this.found = true 
133       } 
134     }
135    
136 if (this.rawValue != null) { // unpack into fields
137
138   var sl = this.rawValue.length
139   var startidx = 0
140   var endidx = 0
141   var i = 0
142
143 // Special case - single-field cookies written by other functions,
144 // so without a '#' terminator
145
146 if (this.rawValue.substr(sl-1, 1) != this.fieldSeparator) {
147   this.fields[0] = unescape(this.rawValue)
148   } else { // separate fields
149
150   do  
151   {
152    endidx = this.rawValue.indexOf(this.fieldSeparator, startidx)
153    if (endidx !=-1) {
154      this.fields[i] = unescape(this.rawValue.substring(startidx, endidx))
155      i++
156      startidx = endidx + 1}
157   }
158   while (endidx !=-1 & endidx != (this.rawValue.length -1));
159 }
160 } // end of unpack into fields if block
161   return this.found
162 } // end of function
163
164
165 function ucDelete() {
166   this.expires = -10
167   this.write()
168   return this.read()
169 }
170
171
172
173 /*
174 *********** IT'S OK TO REMOVE THE CODE BELOW HERE IF YOUR PAGE 
175 DOESN'T USE cookieList() OBJECTS OR THE findCookieObject() FUNCTION.
176 */
177
178
179
180
181 function findCookieObject(cName, cObjArray) {
182 /* 
183 This function finds a named cookie among the objects
184 pointed to by a cookieList array (see below).
185
186 Parameters are the cookie name to search for (a string), and an array created with 
187 the new cookieList() constructor (see below)
188
189 NOTE - if you're only dealing with a specific, named cookie, then it's
190 more efficient to ceate a single cookieObject directly with that name,
191 and check its .found property to see if it already exists on this client.
192
193 This function is for when you've created an all-cookies array anyway,
194 and now want to check whether a specific cookie is present.
195
196 It returns a pointer to the cookieObject if found, or null if not found.
197 */
198
199 var cpointer = null, i
200 for (i in cObjArray) {
201   if (cName == cObjArray[i].name) {
202     cpointer = cObjArray[i]
203   }
204 }
205 return cpointer
206 }
207
208
209 function cookieList() {
210 /* 
211 This constructor function creates a cookieObject object (see below) 
212 for each cookie in document.cookie,
213 and returns an array of pointers to the objects.
214
215 You can use it to load all the cookies available to a page, then walk through them.
216
217 Example usage:
218
219 cookList = new cookieList()
220 for (i in cookList) {
221  document.write(cookList[i].name + " " + cookList[i].fields[0] + "
222 ")
223 }
224
225 */
226
227 var i = 0, rawstring, offset = 0, start, newname
228 cpointers = new Array()
229 rawstring = document.cookie
230 if (rawstring.length > 0) {
231   do {
232    start = rawstring.indexOf("=", offset)
233    if (start != -1) { // another cookie found in string
234      // get cookie string up to end of current cookie name
235      newname = rawstring.substring(0, start) 
236      if (offset > 0) { 
237        // if not first cookie in string, remove previous cookie data from substring
238        // subsequent cookie names have a space before them (just a little browser foible!)
239        newname = newname.substring(newname.lastIndexOf(";")+2, start)
240      }     
241      cpointers[i] = new cookieObject(newname)
242      offset = start + 1
243      i++
244    }
245   } while (start != -1)
246 } // end rawstring.length > 0
247 return cpointers
248 } //end function
249