]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/web/opac/common/js/Cookies.js
forcing array-ness on non-existant params
[Evergreen.git] / Open-ILS / web / opac / common / js / Cookies.js
1 // HTTP.Cookies - Burak Gürsoy <burak[at]cpan[dot]org>
2 if (!HTTP) var HTTP = {}; // create base class if undefined
3
4 HTTP.Cookies = function () { // HTTP.Cookies constructor
5    this.JAR = ''; // data cache
6 }
7
8 HTTP.Cookies.VERSION = '1.01';
9
10 HTTP.Cookies.Date = function () { // expire time calculation class
11    this.format = {
12    's' : 1,
13    'm' : 60,
14    'h' : 60 * 60,
15    'd' : 60 * 60 * 24,
16    'M' : 60 * 60 * 24 * 30,
17    'y' : 60 * 60 * 24 * 365
18    };
19 }
20
21 HTTP.Cookies.Date.prototype.parse = function (x) {
22    if(!x || x == 'now') return 0;
23    var date = x.match(/^(.+?)(\w)$/i);
24    var of = 0;
25    return (this.is_num(date[1]) && (of = this.is_date(date[1],date[2]))) ? of : 0;
26 }
27
28 HTTP.Cookies.Date.prototype.is_date = function (num, x) {
29    if (!x || x.length != 1) return 0;
30    var ar = [];
31    return (ar = x.match(/^(s|m|h|d|w|M|y)$/) ) ? num * 1000 * this.format[ar[0]] : 0;
32 }
33
34 HTTP.Cookies.Date.prototype.is_num = function (x) {
35    if (x.length == 0) return;
36    var ok = 1;
37    for (var i = 0; i < x.length; i++) {
38       if ("0123456789.-+".indexOf(x.charAt(i)) == -1) {
39          ok--;
40          break;
41       }
42    }
43    return ok;
44 }
45
46 HTTP.Cookies.prototype.date = new HTTP.Cookies.Date; // date object instance
47
48 // Get the value of the named cookie. Usage: password = cookie.read('password');
49 HTTP.Cookies.prototype.read = function (name) {
50    var value  = '';
51    if(!this.JAR) {
52                 this.JAR = {};
53       var array  = document.cookie.split(';');
54       for (var x = 0; x < array.length; x++) {
55          var pair = array[x].split('=');
56          if(pair[0].substring (0,1) == ' ') pair[0] = pair[0].substring(1, pair[0].length);
57          if(pair[0] == name) {
58             value = pair[1];
59          }
60          this.JAR[pair[0]] = pair[1];
61       }
62    } else {
63       for(var cookie in this.JAR) {
64          if(cookie == name) {
65             value = this.JAR[cookie];
66          }
67            }
68    }
69    return value ? unescape(value) : '';
70 }
71
72 // Create a new cookie or overwrite existing. Usage: cookie.write('password', 'secret', '1m');
73 HTTP.Cookies.prototype.write = function (name, value, expires, path, domain, secure) {
74    var extra = '';
75    if (!expires) expires = '';
76    if (expires == '_epoch') {
77       expires = new Date(0);
78    } else if (expires != -1) {
79       var Now  = new Date;
80       Now.setTime(Now.getTime() + this.date.parse(expires));
81       expires = Now.toGMTString();
82    }
83    if(expires) extra += "; expires=" + expires;
84    if(path   ) extra += "; path="    + path;
85    if(domain ) extra += "; domain="  + domain;
86    if(secure ) extra += "; secure="  + secure;
87    document.cookie = name + "=" + escape(value) + extra;
88 }
89
90 // Delete the named cookie. Usage: cookie.remove('password');
91 HTTP.Cookies.prototype.remove = function (name, path, domain, secure) {
92    this.write(name, '', '_epoch', path, domain, secure);
93 }
94
95 /*
96
97 =head1 NAME
98
99 HTTP.Cookies - JavaScript class for reading, writing and deleting cookies
100
101 =head1 SYNOPSIS
102
103    var cookie    = new HTTP.Cookies;
104    var password  = cookie.read('password');
105    var lastvisit = cookie.read('lastvisit');
106    cookie.write('lastvisit',1079383075,'+1y');
107    cookie.remove('password');
108
109 =head1 DESCRIPTION
110
111 HTTP.Cookies is a class for http cookies manipulation. It defines
112 three object methods to read, write and remove cookies. Implementation
113 is somehow similar to the Perl module CGI.pm' s C<cookie()> method.
114
115 =head1 METHODS
116
117 =head2 read NAME
118
119 Reads the cookie named C<name> and returns it's value or an empty
120 string upon failure.
121
122 =head2 write NAME, VALUE [, EXPIRES, PATH, DOMAIN, SECURE]
123
124 Creates a new cookie with C<NAME> and C<VALUE>. Optional C<EXPIRES>
125 value sets the cookie lifetime.
126
127 Expire date format: you can use negative or positive numbers combined
128 with 's', 'm', 'h', 'd', 'w', 'M', 'y' or you can use 'now' to
129 expire as soon as possible. Meanings:
130
131     s   = second
132     m   = minute
133     h   = hour
134     d   = day
135     w   = week
136     M   = month
137     y   = year
138     now = immediately
139
140 for a session cookie; pass "-1" as the expires value.
141
142 Optional parameter C<DOMAIN> can be used to define the domain
143 for which the HTTP cookie is valid.
144
145 Optional parameter C<EXPIRES> can be used to make it a secure cookie
146 (secure cookies can only be used with HTTPS protocol).
147
148 =head2 remove NAME [, PATH, DOMAIN, SECURE]
149
150 Deletes/removes the named cookie from the client.
151
152 =head1 SEE ALSO
153
154 =head1 BUGS
155
156 Contact the author if you find any.
157
158 This library is tested with: Opera 8.01, MSIE 6.0,
159 Netscape Communicator 4.77, Mozilla 1.7.8
160 and Mozilla FireFox 1.0.4 under Windows XP Professional SP2.
161
162 =head1 AUTHOR
163
164 Burak Gürsoy, E<lt>burakE<64>cpan.orgE<gt>
165
166 =head1 COPYRIGHT
167
168 Copyright 2005 Burak Gürsoy. All rights reserved.
169
170 =head1 LICENSE
171
172 This library is free software; you can redistribute it and/or modify
173 it under the terms of the "Artistic License":
174 L<http://dev.perl.org/licenses/artistic.html>.
175
176 =cut
177
178 */