cookie_jar.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. // Licensed Materials - Property of IBM
  2. //
  3. // IBM Cognos Products: ps
  4. //
  5. // (C) Copyright IBM Corp. 2005, 2019
  6. //
  7. // US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
  8. // Define the limit for max No. of characters that we want to store in a browser cookie. Cognos customers can "potentially" modify this in this file.
  9. // Use ~ 80% of actual browser limit of 4094 bytes. This limit should be checked when setting cookies that could potentially exceed the limit.
  10. var g_iMaxAllowableCookieSize = 3275;
  11. var g_cc_state = "cc_state";
  12. function setCookie(name, value, expires, path, domain, secure) {
  13. // We could be very strict and prevent the cookie size from going over the limit right here but that would penalize small cookies too.
  14. // if ((document.cookie.length + value.length) > g_iMaxAllowableCookieSize) {
  15. // return;
  16. // }
  17. if (!path) {
  18. path = getPath();
  19. }
  20. if (!domain) {
  21. domain = getDomain();
  22. }
  23. if (!secure) {
  24. secure = getSecure();
  25. }
  26. document.cookie = name + "=" + value +
  27. ((expires) ? "; expires=" + expires.toGMTString() : "") +
  28. ((path) ? "; path=" + path : "") +
  29. ((domain) ? "; domain=" + domain : "") +
  30. ((secure) ? "; secure" : "");
  31. }
  32. function delCookie(name, path, domain) {
  33. if (path == null) {
  34. path = getPath();
  35. }
  36. if (!domain) {
  37. domain = getDomain();
  38. }
  39. var expires = new Date();
  40. if (getCookie(name)) {
  41. document.cookie = name + "=" +
  42. ("; expires=" + expires.toGMTString()) +
  43. ((path) ? "; path=" + path : "") +
  44. ((domain) ? "; domain=" + domain : "");
  45. }
  46. }
  47. function getCookie(name) {
  48. var arg = name + "=";
  49. var alen = arg.length;
  50. var clen = document.cookie.length;
  51. var i = 0;
  52. while (i < clen) {
  53. var j = i + alen;
  54. if (document.cookie.substring(i, j) == arg) {
  55. return getCookieVal(j);
  56. }
  57. i = document.cookie.indexOf(" ", i) + 1;
  58. if (i == 0) {
  59. break;
  60. }
  61. }
  62. return null;
  63. }
  64. function getCookieVal(offset) {
  65. var endstr = document.cookie.indexOf(";", offset);
  66. if (endstr == -1) {
  67. endstr = document.cookie.length;
  68. }
  69. return document.cookie.substring(offset, endstr);
  70. }
  71. function setExpireDate(nOffset) {
  72. if (!nOffset) {
  73. nOffset = 0;
  74. }
  75. var expdate = new Date();
  76. expdate.setTime(expdate.getTime() + nOffset);
  77. return expdate;
  78. }
  79. function getPath() {
  80. if (typeof sCookiePath != 'undefined' && sCookiePath != "") {
  81. return sCookiePath;
  82. } else {
  83. return location.pathname.substring(0, location.pathname.lastIndexOf("/"));
  84. }
  85. }
  86. function getDomain() {
  87. if (typeof sCookieDomain != 'undefined') {
  88. return sCookieDomain;
  89. }
  90. return null;
  91. }
  92. function getSecure() {
  93. if ((typeof sCookieSecure != 'undefined') && (sCookieSecure === 'true')) {
  94. return true;
  95. }
  96. return null;
  97. }
  98. function getSessionString() {
  99. var sSession = getCookie(g_cc_state);
  100. if (sSession != null) {
  101. var indexOfFirstQuote = sSession.indexOf('"');
  102. if (indexOfFirstQuote == 0) {
  103. var newSession = sSession.substring(1, sSession.length - 1);
  104. sSession = newSession;
  105. }
  106. }
  107. return sSession;
  108. }
  109. function setStateValue(sName, sValue) {
  110. var sSession = getSessionString();
  111. //-- If we have no session or it is empty or the name is invalid then return.
  112. if (!(sName == "" || sName == null)) {
  113. var sNewSession = "";
  114. if (sSession == "" || sSession == "null" || sSession == null) {
  115. sNewSession = sName + sNameValueDelimiter + sValue;
  116. } else {
  117. var aSession = [];
  118. var bParameterExists = false;
  119. // unpack the session
  120. aSession = sSession.split(sParamDelimiter);
  121. var n = aSession.length;
  122. // update the specified parameter
  123. for (var i = 0; i < n; i++) {
  124. if (aSession[i].indexOf(sName) == 0) {
  125. aSession[i] = sName + sNameValueDelimiter + sValue;
  126. bParameterExists = true;
  127. break;
  128. }
  129. }
  130. // if a value was updated in the existing session then repack the session, otherwise just append the new param at the end.
  131. if (!bParameterExists) {
  132. sNewSession = sSession + sParamDelimiter + sName + sNameValueDelimiter + sValue;
  133. } else {
  134. // pack the updated session
  135. sNewSession = aSession.join(sParamDelimiter);
  136. }
  137. }
  138. // Save the new session ...(since cc_state always has a : we must quote it
  139. setCookie(g_cc_state, "\"" + sNewSession + "\"", 0, getPath());
  140. }
  141. return;
  142. }
  143. function clearSessionEntry(sNames) {
  144. var sSession = getSessionString();
  145. if (sSession != "" && sSession != "null" && sSession != null) {
  146. var newSession = sSession;
  147. var aName = [];
  148. // Unpack the elements passed in to remove from the cc_state.
  149. // The format of sNames is the same as what is used for the cc_state cookie <name> [[separator <name>]...])
  150. aName = sNames.split(sParamDelimiter);
  151. var n = aName.length;
  152. for (var i = 0; i < n; i++) {
  153. var sName = aName[i];
  154. // index in the session of the named entry.
  155. var iStart = newSession.indexOf(sName);
  156. if (iStart != -1) {
  157. var frontEnd = null;
  158. var backEnd = null;
  159. // index in the session after the named entry.
  160. var iEnd = iStart + sName.length + sNameValueDelimiter.length;
  161. // Get the info from the session before the named entry.
  162. if (--iStart > 0) {
  163. frontEnd = newSession.substring(0, iStart);
  164. }
  165. if (iEnd < newSession.length) {
  166. // Get the info from the session after the named entry - including the parameter delimiter.
  167. backEnd = newSession.substring(iEnd);
  168. iEnd = backEnd.indexOf(sParamDelimiter);
  169. if (iEnd != -1) {
  170. backEnd = backEnd.substring(++iEnd);
  171. } else {
  172. backEnd = null;
  173. }
  174. }
  175. // Put the info together again - without the named entry
  176. if (backEnd != null && frontEnd != null) {
  177. newSession = frontEnd + sParamDelimiter + backEnd;
  178. } else {
  179. if (backEnd == null) {
  180. newSession = frontEnd;
  181. } else {
  182. if (frontEnd == null) {
  183. newSession = backEnd;
  184. }
  185. }
  186. }
  187. }
  188. }
  189. // Save the new session ...(since cc_state always has a : we must quote it
  190. setCookie(g_cc_state, "\"" + newSession + "\"", 0, getPath());
  191. }
  192. }
  193. function getSessionValue(sName) {
  194. // Get the current session
  195. var sSession = getSessionString();
  196. var sRetValue = null;
  197. // If we have no session or it is empty then return null.
  198. if (sSession != "" && sSession != "null" && sSession != null) {
  199. var aSession = [];
  200. aSession = sSession.split(sParamDelimiter);
  201. var n = aSession.length;
  202. // unpack the session and update the specified parameter
  203. for (var i = 0; i < n; i++) {
  204. if (aSession[i].indexOf(sName) == 0) {
  205. sRetValue = aSession[i].substring(aSession[i].indexOf(sNameValueDelimiter) + sNameValueDelimiter.length, aSession[i].length);
  206. break;
  207. }
  208. }
  209. }
  210. return sRetValue;
  211. }
  212. function resetStateParam(sName) {
  213. // If we have no session or it is empty then return null.
  214. var sCurrentValue = getSessionValue(sName);
  215. if (sCurrentValue) {
  216. setStateValue(sName, "");
  217. }
  218. }
  219. function updateSession() {
  220. // Updates the browser session to control caching.
  221. var currentValue = parseInt(getSessionValue("us"), 10);
  222. var newValue = isNaN(currentValue) ? 0 : (currentValue + 1);
  223. setStateValue("us", newValue);
  224. }