Diffstat (limited to 'frontend/beta/js/Clipperz/Base.js') (more/less context) (ignore whitespace changes)
-rw-r--r-- | frontend/beta/js/Clipperz/Base.js | 308 |
1 files changed, 308 insertions, 0 deletions
diff --git a/frontend/beta/js/Clipperz/Base.js b/frontend/beta/js/Clipperz/Base.js new file mode 100644 index 0000000..5bd972b --- a/dev/null +++ b/frontend/beta/js/Clipperz/Base.js | |||
@@ -0,0 +1,308 @@ | |||
1 | /* | ||
2 | |||
3 | Copyright 2008-2011 Clipperz Srl | ||
4 | |||
5 | This file is part of Clipperz's Javascript Crypto Library. | ||
6 | Javascript Crypto Library provides web developers with an extensive | ||
7 | and efficient set of cryptographic functions. The library aims to | ||
8 | obtain maximum execution speed while preserving modularity and | ||
9 | reusability. | ||
10 | For further information about its features and functionalities please | ||
11 | refer to http://www.clipperz.com | ||
12 | |||
13 | * Javascript Crypto Library is free software: you can redistribute | ||
14 | it and/or modify it under the terms of the GNU Affero General Public | ||
15 | License as published by the Free Software Foundation, either version | ||
16 | 3 of the License, or (at your option) any later version. | ||
17 | |||
18 | * Javascript Crypto Library is distributed in the hope that it will | ||
19 | be useful, but WITHOUT ANY WARRANTY; without even the implied | ||
20 | warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | ||
21 | See the GNU Affero General Public License for more details. | ||
22 | |||
23 | * You should have received a copy of the GNU Affero General Public | ||
24 | License along with Javascript Crypto Library. If not, see | ||
25 | <http://www.gnu.org/licenses/>. | ||
26 | |||
27 | */ | ||
28 | |||
29 | if (typeof(Clipperz) == 'undefined') { Clipperz = {}; } | ||
30 | if (typeof(Clipperz.Base) == 'undefined') { Clipperz.Base = {}; } | ||
31 | |||
32 | Clipperz.Base.VERSION = "0.1"; | ||
33 | Clipperz.Base.NAME = "Clipperz.Base"; | ||
34 | |||
35 | MochiKit.Base.update(Clipperz.Base, { | ||
36 | |||
37 | //------------------------------------------------------------------------- | ||
38 | |||
39 | '__repr__': function () { | ||
40 | return "[" + this.NAME + " " + this.VERSION + "]"; | ||
41 | }, | ||
42 | |||
43 | //------------------------------------------------------------------------- | ||
44 | |||
45 | 'toString': function () { | ||
46 | return this.__repr__(); | ||
47 | }, | ||
48 | |||
49 | //------------------------------------------------------------------------- | ||
50 | |||
51 | 'trim': function (aValue) { | ||
52 | return aValue.replace(/^\s+|\s+$/g, ""); | ||
53 | }, | ||
54 | |||
55 | //------------------------------------------------------------------------- | ||
56 | |||
57 | 'stringToByteArray': function (aValue) { | ||
58 | varresult; | ||
59 | var i, c; | ||
60 | |||
61 | result = []; | ||
62 | |||
63 | c = aValue.length; | ||
64 | for (i=0; i<c; i++) { | ||
65 | result[i] = aValue.charCodeAt(i); | ||
66 | } | ||
67 | |||
68 | return result; | ||
69 | }, | ||
70 | |||
71 | //......................................................................... | ||
72 | |||
73 | 'byteArrayToString': function (anArrayOfBytes) { | ||
74 | varresult; | ||
75 | var i, c; | ||
76 | |||
77 | result = ""; | ||
78 | |||
79 | c = anArrayOfBytes.length; | ||
80 | for (i=0; i<c; i++) { | ||
81 | result += String.fromCharCode(anArrayOfBytes[i]); | ||
82 | } | ||
83 | |||
84 | return result; | ||
85 | }, | ||
86 | |||
87 | //------------------------------------------------------------------------- | ||
88 | |||
89 | 'getValueForKeyInFormContent': function (aFormContent, aKey) { | ||
90 | return aFormContent[1][MochiKit.Base.find(aFormContent[0], aKey)]; | ||
91 | }, | ||
92 | |||
93 | //------------------------------------------------------------------------- | ||
94 | |||
95 | 'indexOfObjectInArray': function(anObject, anArray) { | ||
96 | varresult; | ||
97 | vari, c; | ||
98 | |||
99 | result = -1; | ||
100 | |||
101 | c = anArray.length; | ||
102 | for (i=0; ((i<c) && (result < 0)); i++) { | ||
103 | if (anArray[i] === anObject) { | ||
104 | result = i; | ||
105 | } | ||
106 | } | ||
107 | |||
108 | return result; | ||
109 | }, | ||
110 | |||
111 | 'removeObjectAtIndexFromArray': function(anIndex, anArray) { | ||
112 | anArray.splice(anIndex, 1); | ||
113 | }, | ||
114 | |||
115 | 'removeObjectFromArray': function(anObject, anArray) { | ||
116 | varobjectIndex; | ||
117 | |||
118 | objectIndex = Clipperz.Base.indexOfObjectInArray(anObject, anArray); | ||
119 | if (objectIndex > -1) { | ||
120 | Clipperz.Base.removeObjectAtIndexFromArray(objectIndex, anArray); | ||
121 | } else { | ||
122 | // jslog.error("Trying to remove an object not present in the array"); | ||
123 | //TODO: raise an exception | ||
124 | } | ||
125 | }, | ||
126 | |||
127 | 'removeFromArray': function(anArray, anObject) { | ||
128 | return Clipperz.Base.removeObjectFromArray(anObject, anArray); | ||
129 | }, | ||
130 | |||
131 | //------------------------------------------------------------------------- | ||
132 | |||
133 | 'splitStringAtFixedTokenSize': function(aString, aTokenSize) { | ||
134 | var result; | ||
135 | varstringToProcess; | ||
136 | |||
137 | stringToProcess = aString; | ||
138 | result = []; | ||
139 | if (stringToProcess != null) { | ||
140 | while (stringToProcess.length > aTokenSize) { | ||
141 | result.push(stringToProcess.substring(0, aTokenSize)); | ||
142 | stringToProcess = stringToProcess.substring(aTokenSize); | ||
143 | } | ||
144 | |||
145 | result.push(stringToProcess); | ||
146 | } | ||
147 | |||
148 | return result; | ||
149 | }, | ||
150 | |||
151 | //------------------------------------------------------------------------- | ||
152 | |||
153 | 'objectType': function(anObject) { | ||
154 | var result; | ||
155 | |||
156 | if (anObject == null) { | ||
157 | result = null; | ||
158 | } else { | ||
159 | result = typeof(anObject); | ||
160 | |||
161 | if (result == "object") { | ||
162 | if (anObject instanceof Array) { | ||
163 | result = 'array' | ||
164 | } else if (anObject.constructor == Boolean) { | ||
165 | result = 'boolean' | ||
166 | } else if (anObject instanceof Date) { | ||
167 | result = 'date' | ||
168 | } else if (anObject instanceof Error) { | ||
169 | result = 'error' | ||
170 | } else if (anObject instanceof Function) { | ||
171 | result = 'function' | ||
172 | } else if (anObject.constructor == Number) { | ||
173 | result = 'number' | ||
174 | } else if (anObject.constructor == String) { | ||
175 | result = 'string' | ||
176 | } else if (anObject instanceof Object) { | ||
177 | result = 'object' | ||
178 | } else { | ||
179 | throw Clipperz.Base.exception.UnknownType; | ||
180 | } | ||
181 | } | ||
182 | } | ||
183 | |||
184 | return result; | ||
185 | }, | ||
186 | |||
187 | //------------------------------------------------------------------------- | ||
188 | |||
189 | 'escapeHTML': function(aValue) { | ||
190 | var result; | ||
191 | |||
192 | result = aValue; | ||
193 | result = result.replace(/</g, "<"); | ||
194 | result = result.replace(/>/g, ">"); | ||
195 | |||
196 | return result; | ||
197 | }, | ||
198 | |||
199 | //------------------------------------------------------------------------- | ||
200 | |||
201 | 'deepClone': function(anObject) { | ||
202 | var result; | ||
203 | |||
204 | result = Clipperz.Base.evalJSON(Clipperz.Base.serializeJSON(anObject)); | ||
205 | |||
206 | return result; | ||
207 | }, | ||
208 | |||
209 | //------------------------------------------------------------------------- | ||
210 | |||
211 | 'evalJSON': function(aString) { | ||
212 | /* | ||
213 | var result; | ||
214 | |||
215 | //check for XSS injection | ||
216 | if (/<script>/.test(aString)) { | ||
217 | throw "error"; | ||
218 | } | ||
219 | |||
220 | if (/<iframe>/.test(aString)) { | ||
221 | throw "error"; | ||
222 | } | ||
223 | |||
224 | result = MochiKit.Base.evalJSON(aString); | ||
225 | |||
226 | return result; | ||
227 | */ | ||
228 | |||
229 | // return MochiKit.Base.evalJSON(aString); | ||
230 | return JSON2.parse(aString); | ||
231 | }, | ||
232 | |||
233 | 'serializeJSON': function(anObject) { | ||
234 | // return MochiKit.Base.serializeJSON(anObject); | ||
235 | return JSON2.stringify(anObject); | ||
236 | }, | ||
237 | |||
238 | //------------------------------------------------------------------------- | ||
239 | |||
240 | 'sanitizeString': function(aValue) { | ||
241 | var result; | ||
242 | |||
243 | if (Clipperz.Base.objectType(aValue) == 'string') { | ||
244 | result = aValue; | ||
245 | result = result.replace(/</img,"<"); | ||
246 | result = result.replace(/>/img,">"); | ||
247 | } else { | ||
248 | result = aValue; | ||
249 | } | ||
250 | |||
251 | return result; | ||
252 | }, | ||
253 | |||
254 | //------------------------------------------------------------------------- | ||
255 | |||
256 | 'exception': { | ||
257 | 'AbstractMethod': new MochiKit.Base.NamedError("Clipperz.Base.exception.AbstractMethod"), | ||
258 | 'UnknownType': new MochiKit.Base.NamedError("Clipperz.Base.exception.UnknownType"), | ||
259 | 'VulnerabilityIssue':new MochiKit.Base.NamedError("Clipperz.Base.exception.VulnerabilityIssue") | ||
260 | }, | ||
261 | |||
262 | //------------------------------------------------------------------------- | ||
263 | __syntaxFix__: "syntax fix" | ||
264 | |||
265 | }); | ||
266 | |||
267 | |||
268 | |||
269 | MochiKit.Base.registerComparator('Object dummy comparator', | ||
270 | function(a, b) { | ||
271 | return ((a.constructor == Object) && (b.constructor == Object)); | ||
272 | }, | ||
273 | function(a, b) { | ||
274 | var result; | ||
275 | var aKeys; | ||
276 | var bKeys; | ||
277 | |||
278 | //MochiKit.Logging.logDebug(">>> comparator"); | ||
279 | //MochiKit.Logging.logDebug("- a: " + Clipperz.Base.serializeJSON(a)); | ||
280 | //MochiKit.Logging.logDebug("- b: " + Clipperz.Base.serializeJSON(a)); | ||
281 | aKeys = MochiKit.Base.keys(a).sort(); | ||
282 | bKeys = MochiKit.Base.keys(b).sort(); | ||
283 | |||
284 | result = MochiKit.Base.compare(aKeys, bKeys); | ||
285 | //if (result != 0) { | ||
286 | //MochiKit.Logging.logDebug("- comparator 'keys':"); | ||
287 | //MochiKit.Logging.logDebug("- comparator aKeys: " + Clipperz.Base.serializeJSON(aKeys)); | ||
288 | //MochiKit.Logging.logDebug("- comparator bKeys: " + Clipperz.Base.serializeJSON(bKeys)); | ||
289 | //} | ||
290 | if (result == 0) { | ||
291 | vari, c; | ||
292 | |||
293 | c = aKeys.length; | ||
294 | for (i=0; (i<c) && (result == 0); i++) { | ||
295 | result = MochiKit.Base.compare(a[aKeys[i]], b[bKeys[i]]); | ||
296 | //if (result != 0) { | ||
297 | //MochiKit.Logging.logDebug("- comparator 'values':"); | ||
298 | //MochiKit.Logging.logDebug("- comparator a[aKeys[i]]: " + Clipperz.Base.serializeJSON(a[aKeys[i]])); | ||
299 | //MochiKit.Logging.logDebug("- comparator b[bKeys[i]]: " + Clipperz.Base.serializeJSON(b[bKeys[i]])); | ||
300 | //} | ||
301 | } | ||
302 | } | ||
303 | |||
304 | //MochiKit.Logging.logDebug("<<< comparator - result: " + result); | ||
305 | return result; | ||
306 | }, | ||
307 | true | ||
308 | ); | ||