]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/web/opac/common/js/Cookies.js
did some debugging.. made buildOPACLink safer WRT array params
[Evergreen.git] / Open-ILS / web / opac / common / js / Cookies.js
1 // HTTP.Cookies - Burak Gürsoy <burak[at]cpan[dot]org>
2
3 /*
4 I removed all the docs (except author and license info) to reduce download size
5 -bill erickson <billserickson@gmail.com>
6 */
7
8 if (!HTTP) var HTTP = {}; // create base class if undefined
9
10 HTTP.Cookies = function () { // HTTP.Cookies constructor
11    this.JAR = ''; // data cache
12 }
13
14 HTTP.Cookies.VERSION = '1.01';
15
16 HTTP.Cookies.Date = function () { // expire time calculation class
17    this.format = {
18    's' : 1,
19    'm' : 60,
20    'h' : 60 * 60,
21    'd' : 60 * 60 * 24,
22    'M' : 60 * 60 * 24 * 30,
23    'y' : 60 * 60 * 24 * 365
24    };
25 }
26
27 HTTP.Cookies.Date.prototype.parse = function (x) {
28    if(!x || x == 'now') return 0;
29    var date = x.match(/^(.+?)(\w)$/i);
30    var of = 0;
31    return (this.is_num(date[1]) && (of = this.is_date(date[1],date[2]))) ? of : 0;
32 }
33
34 HTTP.Cookies.Date.prototype.is_date = function (num, x) {
35    if (!x || x.length != 1) return 0;
36    var ar = [];
37    return (ar = x.match(/^(s|m|h|d|w|M|y)$/) ) ? num * 1000 * this.format[ar[0]] : 0;
38 }
39
40 HTTP.Cookies.Date.prototype.is_num = function (x) {
41    if (x.length == 0) return;
42    var ok = 1;
43    for (var i = 0; i < x.length; i++) {
44       if ("0123456789.-+".indexOf(x.charAt(i)) == -1) {
45          ok--;
46          break;
47       }
48    }
49    return ok;
50 }
51
52 HTTP.Cookies.prototype.date = new HTTP.Cookies.Date; // date object instance
53
54 // Get the value of the named cookie. Usage: password = cookie.read('password');
55 HTTP.Cookies.prototype.read = function (name) {
56    var value  = '';
57    if(!this.JAR) {
58                 this.JAR = {};
59       var array  = document.cookie.split(';');
60       for (var x = 0; x < array.length; x++) {
61          var pair = array[x].split('=');
62          if(pair[0].substring (0,1) == ' ') pair[0] = pair[0].substring(1, pair[0].length);
63          if(pair[0] == name) {
64             value = pair[1];
65          }
66          this.JAR[pair[0]] = pair[1];
67       }
68    } else {
69       for(var cookie in this.JAR) {
70          if(cookie == name) {
71             value = this.JAR[cookie];
72          }
73            }
74    }
75    return value ? unescape(value) : '';
76 }
77
78 // Create a new cookie or overwrite existing. Usage: cookie.write('password', 'secret', '1m');
79 HTTP.Cookies.prototype.write = function (name, value, expires, path, domain, secure) {
80    var extra = '';
81    if (!expires) expires = '';
82    if (expires == '_epoch') {
83       expires = new Date(0);
84    } else if (expires != -1) {
85       var Now  = new Date;
86       Now.setTime(Now.getTime() + this.date.parse(expires));
87       expires = Now.toGMTString();
88    }
89    if(expires) extra += "; expires=" + expires;
90    if(path   ) extra += "; path="    + path;
91    if(domain ) extra += "; domain="  + domain;
92    if(secure ) extra += "; secure="  + secure;
93    document.cookie = name + "=" + escape(value) + extra;
94 }
95
96 // Delete the named cookie. Usage: cookie.remove('password');
97 HTTP.Cookies.prototype.remove = function (name, path, domain, secure) {
98    this.write(name, '', '_epoch', path, domain, secure);
99 }
100
101 /*
102
103 =head1 NAME
104
105 HTTP.Cookies - JavaScript class for reading, writing and deleting cookies
106
107 =head1 AUTHOR
108
109 Burak Gürsoy, E<lt>burakE<64>cpan.orgE<gt>
110
111 =head1 COPYRIGHT
112
113 Copyright 2005 Burak Gürsoy. All rights reserved.
114
115 =head1 LICENSE
116
117 This library is free software; you can redistribute it and/or modify
118 it under the terms of the "Artistic License":
119 L<http://dev.perl.org/licenses/artistic.html>.
120
121 =cut
122
123 */