Subversion Repositories wimsdev

Rev

Rev 10534 | Blame | Compare with Previous | Last modification | View Log | RSS feed

  1. /*
  2.    DynAPI Distribution
  3.    Cookie functions
  4.  
  5.    The DynAPI Distribution is distributed under the terms of the GNU LGPL license.
  6. */
  7.  
  8. /*
  9. This is not tested, should work like this:
  10.  
  11.                 var c = new Cookie('mycookieset');
  12.                 c.add('array',[1,2,3]); // re-saves cookie each time a value is added
  13.  
  14.                 var c = new Cookie('mycookieset');  // auto-retrieves saved cookie object
  15.                 var name = c.get('name');
  16.                 var array = c.get('array');
  17.                 array[array.length] = 4;
  18.                 c.add('name',name+' MyLastName');
  19.                 c.add('array',array);
  20. */
  21.  
  22.  
  23. function Cookie(name,pDType) {
  24.         this.DynObject = DynObject;
  25.         this.DynObject();
  26.  
  27.         this.data = {};
  28.         this.name = name;
  29.         this.exists = false;
  30.         this._pdt=pDType;
  31.         var c = dynapi.functions.getCookie(this.name);
  32.         if (c) {
  33.                 this.exists = true;
  34.                 var a = c.split(',');
  35.                 var x,n,v;
  36.                 for (var i=0;i<a.length;i++) {
  37.                         x = a[i].split('=');
  38.                         n = x[0];
  39.                         v = Cookie.decode(x[1]);
  40.                         if (n && v) this.data[n] = v;
  41.                 }
  42.                 //var i1 = c.indexOf('expires=');
  43.                 this._save(false);
  44.         }
  45.         else this._save();
  46. };
  47. // to-do: replace escape(),unescape() with better encoding functions
  48. Cookie.decode = function(t,_lvl){
  49.         var dt = (t+'').substring(0,2);
  50.         if(isNaN(_lvl)) _lvl=0; else _lvl++;
  51.         if(dt=='a[') {                  //array
  52.                 t=t.substring(2,t.length-1);
  53.                 t=t.split('\\'+_lvl);
  54.                 for(var i=0;i<t.length;i++) t[i]=Cookie.decode(t[i],_lvl);
  55.         }
  56.         else if(dt=='o[') {     //object
  57.                 var a,n,v;
  58.                 t=t.substring(2,t.length-1);
  59.                 a=t.split('\\'+_lvl);
  60.                 t={};
  61.                 for(var i=0;i<a.length;i++) {
  62.                         n=a[i].substring(0,a[i].indexOf(':'));
  63.                         if(n) v=a[i].substring(n.length+1);
  64.                         else v=null;
  65.                         t[n]=Cookie.decode(v,_lvl);
  66.                 }
  67.         }
  68.         else if(dt=='n[') {     //number:float, integer
  69.                 t=parseFloat(t.substring(2,t.length-1));
  70.         }
  71.         else if(dt=='d[') {     //date
  72.                 t=new Date(unescape(t.substring(2,t.length-1)));
  73.         }
  74.         else if(dt=='b[') {     //boolean
  75.                 t=(t.substring(2,t.length-1)=="1")? true:false;
  76.         }
  77.         else if(dt=='u[') {     //null
  78.                 t=null;
  79.         }
  80.         else{                           //string
  81.                 t=unescape(t);
  82.         }
  83.         return t;
  84. };
  85. // to-do: replace escape(),unescape() with better encoding functions
  86. Cookie.encode = function(t,pDType,_lvl){
  87.         if (!pDType) t=escape(t);
  88.         else if (t==null) t='u[]';
  89.         else if(typeof(t)=='number') t='n['+t+']';
  90.         else if(typeof(t)=='boolean') t='b['+((t)? 1:0)+']';
  91.         else if(typeof(t)!='object') t=escape(t);
  92.         else {
  93.                 if(isNaN(_lvl)) _lvl=0; else _lvl++;
  94.                 if(t.constructor==Date) t='d['+escape(t)+']';
  95.                 else if(t.constructor==Array){
  96.                         //encode array = a[n1\0n2...\0nN]
  97.                         var a=[];
  98.                         for(var i=0;i<t.length;i++) a[i]=Cookie.encode(t[i],pDType);
  99.                         t='a['+a.join('\\'+_lvl)+']';
  100.                 }
  101.                 else {
  102.                         //encode object = o[name1:value1\0name2:value2...\0nameN:valueN]
  103.                         var a=[];
  104.                         for(var i in t){
  105.                                 a[a.length]=(i+':'+Cookie.encode(t[i],pDType,_lvl));
  106.                         }
  107.                         t='o['+a.join('\\'+_lvl)+']';
  108.                 }
  109.         }
  110.         return t;
  111. };
  112. var p = dynapi.setPrototype('Cookie','DynObject');
  113. p.get = function(name) {
  114.         return this.data[name];
  115. };
  116. p.getAll = function() {
  117.         return data;
  118. };
  119. p.add = function(name,value) {
  120.         this.data[name] = value;
  121.         this._save();
  122. };
  123. p.remove = function(name) {
  124.         this.data[name] = null;
  125.         delete this.data[name];
  126.         this._save();
  127. };
  128. p.removeAll = function(){
  129.         this.data = {};
  130.         this._save();
  131. };
  132. p.setExpires = function(days) {
  133.         this.expires = days;
  134. };
  135. p.destroy = function() {
  136.         dynapi.functions.deleteCookie(this.name);
  137. };
  138. p._save = function(write) {
  139.         var s = '';
  140.         for (var i in this.data) {
  141.                 var v = this.data[i];
  142.                 if (v) s += i + '=' + Cookie.encode(v,this._pdt) + ',';
  143.         }
  144.         s = s.substring(0,s.length-1);
  145.         var f = 'Saved';
  146.         if (write!=false) dynapi.functions.setCookie(this.name,s,this.expires);
  147.         else f = 'Found';
  148.         dynapi.debug.print(f+' Cookie: name='+this.name+' data={'+s+'}');
  149. };
  150.  
  151. dynapi.functions.setCookie = function(name,value,days) {
  152.         if (days) {
  153.                 var date=new Date();
  154.                 date.setTime(date.getTime()+(days*24*60*60*1000));
  155.                 var expires="; expires="+date.toGMTString();
  156.         }
  157.         else expires = "";
  158.         dynapi.frame.document.cookie = name+"="+value+expires+"; path=/";
  159. };
  160. dynapi.functions.getCookie = function(name) {
  161.         var nameEQ = name+"=";
  162.         var c,ca = dynapi.frame.document.cookie.split(';');
  163.         for(var i=0;i<ca.length;i++) {
  164.                 c=ca[i];
  165.                 while (c.charAt(0)==' ') c=c.substring(1,c.length);
  166.                 if (c.indexOf(nameEQ)==0) return c.substring(nameEQ.length,c.length);
  167.         }
  168.         return null;
  169. };
  170. dynapi.functions.deleteCookie = function(name) {
  171.         dynapi.functions.setCookie(name,"",-1);
  172. };
  173.