aboutsummaryrefslogtreecommitdiffstats
path: root/etherpad/src/static/js/pad_cookie.js
blob: 3cc31edf4eeefc0c5824b1881d60d93d16eec93f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/**
 * Copyright 2009 Google Inc.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS-IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */


var padcookie = (function(){
  function getRawCookie() {
    // returns null if can't get cookie text
    if (! document.cookie) {
      return null;
    }
    // look for (start of string OR semicolon) followed by whitespace followed by prefs=(something);
    var regexResult = document.cookie.match(/(?:^|;)\s*prefs=([^;]*)(?:;|$)/);
    if ((! regexResult) || (! regexResult[1])) {
      return null;
    }
    return regexResult[1];
  }
  function setRawCookie(safeText) {
    var expiresDate = new Date();
    expiresDate.setFullYear(3000);
    document.cookie = ('prefs='+safeText+';expires='+expiresDate.toGMTString());
  }
  function parseCookie(text) {
    // returns null if can't parse cookie.

    try {
      var cookieData = JSON.parse(unescape(text));
      return cookieData;
    }
    catch (e) {
      return null;
    }
  }
  function stringifyCookie(data) {
    return escape(JSON.stringify(data));
  }
  function saveCookie() {
    if (! inited) {
      return;
    }
    setRawCookie(stringifyCookie(cookieData));

    if (pad.getIsProPad() && (! getRawCookie()) && (! alreadyWarnedAboutNoCookies)) {
      alert("Warning: it appears that your browser does not have cookies enabled."+
	    " EtherPad uses cookies to keep track of unique users for the purpose"+
	    " of putting a quota on the number of active users.  Using EtherPad without "+
	    " cookies may fill up your server's user quota faster than expected.");
      alreadyWarnedAboutNoCookies = true;
    }
  }

  var wasNoCookie = true;
  var cookieData = {};
  var alreadyWarnedAboutNoCookies = false;
  var inited = false;

  var self = {
    init: function(prefsToSet) {
      var rawCookie = getRawCookie();
      if (rawCookie) {
        var cookieObj = parseCookie(rawCookie);
        if (cookieObj) {
          wasNoCookie = false; // there was a cookie
          delete cookieObj.userId;
          delete cookieObj.name;
          delete cookieObj.colorId;
          cookieData = cookieObj;
        }
      }

      for(var k in prefsToSet) {
        cookieData[k] = prefsToSet[k];
      }

      inited = true;
      saveCookie();
    },
    wasNoCookie: function() { return wasNoCookie; },
    getPref: function(prefName) {
      return cookieData[prefName];
    },
    setPref: function(prefName, value) {
      cookieData[prefName] = value;
      saveCookie();
    }
  };
  return self;
}());