author | Giulio Cesare Solaroli <giulio.cesare@clipperz.com> | 2011-10-02 23:56:18 (UTC) |
---|---|---|
committer | Giulio Cesare Solaroli <giulio.cesare@clipperz.com> | 2011-10-02 23:56:18 (UTC) |
commit | ef68436ac04da078ffdcacd7e1f785473a303d45 (patch) (unidiff) | |
tree | c403752d66a2c4775f00affd4fa8431b29c5b68c /frontend/gamma/js/Clipperz/Base.js | |
parent | 597ecfbc0249d83e1b856cbd558340c01237a360 (diff) | |
download | clipperz-ef68436ac04da078ffdcacd7e1f785473a303d45.zip clipperz-ef68436ac04da078ffdcacd7e1f785473a303d45.tar.gz clipperz-ef68436ac04da078ffdcacd7e1f785473a303d45.tar.bz2 |
First version of the newly restructured repository
Diffstat (limited to 'frontend/gamma/js/Clipperz/Base.js') (more/less context) (show whitespace changes)
-rw-r--r-- | frontend/gamma/js/Clipperz/Base.js | 531 |
1 files changed, 531 insertions, 0 deletions
diff --git a/frontend/gamma/js/Clipperz/Base.js b/frontend/gamma/js/Clipperz/Base.js new file mode 100644 index 0000000..7f321ef --- a/dev/null +++ b/frontend/gamma/js/Clipperz/Base.js | |||
@@ -0,0 +1,531 @@ | |||
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.2"; | ||
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 | 'itemgetter': function (aKeyPath) { | ||
52 | // return MochiKit.Base.compose.apply(null, [MochiKit.Base.itemgetter('key3')]); | ||
53 | return MochiKit.Base.compose.apply(null, | ||
54 | MochiKit.Base.map( | ||
55 | MochiKit.Base.itemgetter, | ||
56 | MochiKit.Iter.reversed( | ||
57 | aKeyPath.split('.') | ||
58 | ) | ||
59 | ) | ||
60 | ); | ||
61 | }, | ||
62 | |||
63 | //------------------------------------------------------------------------- | ||
64 | |||
65 | 'isUrl': function (aValue) { | ||
66 | return (MochiKit.Base.urlRegExp.test(aValue)); | ||
67 | }, | ||
68 | |||
69 | 'isEmail': function (aValue) { | ||
70 | return (MochiKit.Base.emailRegExp.test(aValue)); | ||
71 | }, | ||
72 | |||
73 | //------------------------------------------------------------------------- | ||
74 | |||
75 | 'caseInsensitiveCompare': function (a, b) { | ||
76 | return MochiKit.Base.compare(a.toLowerCase(), b.toLowerCase()); | ||
77 | }, | ||
78 | |||
79 | 'reverseComparator': function (aComparator) { | ||
80 | return MochiKit.Base.compose(function(aResult) { return -aResult; }, aComparator); | ||
81 | }, | ||
82 | |||
83 | //------------------------------------------------------------------------- | ||
84 | /* | ||
85 | 'dependsOn': function(module, deps) { | ||
86 | if (!(module in Clipperz)) { | ||
87 | MochiKit[module] = {}; | ||
88 | } | ||
89 | |||
90 | if (typeof(dojo) != 'undefined') { | ||
91 | dojo.provide('Clipperz.' + module); | ||
92 | } | ||
93 | for (var i = 0; i < deps.length; i++) { | ||
94 | if (typeof(dojo) != 'undefined') { | ||
95 | dojo.require('Clipperz.' + deps[i]); | ||
96 | } | ||
97 | if (typeof(JSAN) != 'undefined') { | ||
98 | JSAN.use('Clipperz.' + deps[i], []); | ||
99 | } | ||
100 | if (!(deps[i] in Clipperz)) { | ||
101 | throw 'Clipperz.' + module + ' depends on Clipperz.' + deps[i] + '!' | ||
102 | } | ||
103 | } | ||
104 | }, | ||
105 | */ | ||
106 | //------------------------------------------------------------------------- | ||
107 | |||
108 | 'trim': function (aValue) { | ||
109 | return aValue.replace(/^\s+|\s+$/g, ""); | ||
110 | }, | ||
111 | |||
112 | //------------------------------------------------------------------------- | ||
113 | |||
114 | 'stringToByteArray': function (aValue) { | ||
115 | varresult; | ||
116 | var i, c; | ||
117 | |||
118 | result = []; | ||
119 | |||
120 | c = aValue.length; | ||
121 | for (i=0; i<c; i++) { | ||
122 | result[i] = aValue.charCodeAt(i); | ||
123 | } | ||
124 | |||
125 | return result; | ||
126 | }, | ||
127 | |||
128 | //......................................................................... | ||
129 | |||
130 | 'byteArrayToString': function (anArrayOfBytes) { | ||
131 | varresult; | ||
132 | var i, c; | ||
133 | |||
134 | result = ""; | ||
135 | |||
136 | c = anArrayOfBytes.length; | ||
137 | for (i=0; i<c; i++) { | ||
138 | result += String.fromCharCode(anArrayOfBytes[i]); | ||
139 | } | ||
140 | |||
141 | return result; | ||
142 | }, | ||
143 | |||
144 | //------------------------------------------------------------------------- | ||
145 | |||
146 | 'getValueForKeyInFormContent': function (aFormContent, aKey) { | ||
147 | return aFormContent[1][MochiKit.Base.find(aFormContent[0], aKey)]; | ||
148 | }, | ||
149 | |||
150 | //------------------------------------------------------------------------- | ||
151 | |||
152 | 'indexOfObjectInArray': function(anObject, anArray) { | ||
153 | varresult; | ||
154 | vari, c; | ||
155 | |||
156 | result = -1; | ||
157 | |||
158 | c = anArray.length; | ||
159 | for (i=0; ((i<c) && (result < 0)); i++) { | ||
160 | if (anArray[i] === anObject) { | ||
161 | result = i; | ||
162 | } | ||
163 | } | ||
164 | |||
165 | return result; | ||
166 | }, | ||
167 | |||
168 | //------------------------------------------------------------------------- | ||
169 | |||
170 | 'removeObjectAtIndexFromArray': function(anIndex, anArray) { | ||
171 | anArray.splice(anIndex, 1); | ||
172 | }, | ||
173 | |||
174 | //------------------------------------------------------------------------- | ||
175 | |||
176 | 'removeObjectFromArray': function(anObject, anArray) { | ||
177 | varobjectIndex; | ||
178 | |||
179 | objectIndex = Clipperz.Base.indexOfObjectInArray(anObject, anArray); | ||
180 | if (objectIndex > -1) { | ||
181 | Clipperz.Base.removeObjectAtIndexFromArray(objectIndex, anArray); | ||
182 | } else { | ||
183 | Clipperz.log("Trying to remove an object not present in the array"); | ||
184 | throw Clipperz.Base.exception.ObjectNotFound; | ||
185 | } | ||
186 | }, | ||
187 | |||
188 | 'removeFromArray': function(anArray, anObject) { | ||
189 | return Clipperz.Base.removeObjectFromArray(anObject, anArray); | ||
190 | }, | ||
191 | |||
192 | //------------------------------------------------------------------------- | ||
193 | |||
194 | 'splitStringAtFixedTokenSize': function(aString, aTokenSize) { | ||
195 | var result; | ||
196 | varstringToProcess; | ||
197 | |||
198 | stringToProcess = aString; | ||
199 | result = []; | ||
200 | if (stringToProcess != null) { | ||
201 | while (stringToProcess.length > aTokenSize) { | ||
202 | result.push(stringToProcess.substring(0, aTokenSize)); | ||
203 | stringToProcess = stringToProcess.substring(aTokenSize); | ||
204 | } | ||
205 | |||
206 | result.push(stringToProcess); | ||
207 | } | ||
208 | |||
209 | return result; | ||
210 | }, | ||
211 | |||
212 | //------------------------------------------------------------------------- | ||
213 | |||
214 | 'objectType': function(anObject) { | ||
215 | var result; | ||
216 | |||
217 | if (anObject == null) { | ||
218 | result = null; | ||
219 | } else { | ||
220 | result = typeof(anObject); | ||
221 | |||
222 | if (result == "object") { | ||
223 | if (anObject instanceof Array) { | ||
224 | result = 'array' | ||
225 | } else if (anObject.constructor == Boolean) { | ||
226 | result = 'boolean' | ||
227 | } else if (anObject instanceof Date) { | ||
228 | result = 'date' | ||
229 | } else if (anObject instanceof Error) { | ||
230 | result = 'error' | ||
231 | } else if (anObject instanceof Function) { | ||
232 | result = 'function' | ||
233 | } else if (anObject.constructor == Number) { | ||
234 | result = 'number' | ||
235 | } else if (anObject.constructor == String) { | ||
236 | result = 'string' | ||
237 | } else if (anObject instanceof Object) { | ||
238 | result = 'object' | ||
239 | } else { | ||
240 | throw Clipperz.Base.exception.UnknownType; | ||
241 | } | ||
242 | } | ||
243 | } | ||
244 | |||
245 | return result; | ||
246 | }, | ||
247 | |||
248 | //------------------------------------------------------------------------- | ||
249 | |||
250 | 'escapeHTML': function(aValue) { | ||
251 | var result; | ||
252 | |||
253 | result = aValue; | ||
254 | result = result.replace(/</g, "<"); | ||
255 | result = result.replace(/>/g, ">"); | ||
256 | |||
257 | return result; | ||
258 | }, | ||
259 | |||
260 | //------------------------------------------------------------------------- | ||
261 | |||
262 | 'deepClone': function(anObject) { | ||
263 | var result; | ||
264 | |||
265 | result = Clipperz.Base.evalJSON(Clipperz.Base.serializeJSON(anObject)); | ||
266 | |||
267 | return result; | ||
268 | }, | ||
269 | |||
270 | //------------------------------------------------------------------------- | ||
271 | |||
272 | //'deepCompare': function (aObject, bObject) { | ||
273 | // return (Clipperz.Base.serializeJSON(aObject) == Clipperz.Base.serializeJSON(bObject)); | ||
274 | //}, | ||
275 | |||
276 | //------------------------------------------------------------------------- | ||
277 | |||
278 | 'evalJSON': function(aString) { | ||
279 | return JSON.parse(aString); | ||
280 | }, | ||
281 | |||
282 | 'serializeJSON': function(anObject) { | ||
283 | return JSON.stringify(anObject); | ||
284 | }, | ||
285 | |||
286 | 'formatJSON': function (anObject, sIndent) { | ||
287 | var realTypeOf = function (v) { | ||
288 | if (typeof(v) == "object") { | ||
289 | if (v === null) return "null"; | ||
290 | if (v.constructor == (new Array).constructor) return "array"; | ||
291 | if (v.constructor == (new Date).constructor) return "date"; | ||
292 | if (v.constructor == (new RegExp).constructor) return "regex"; | ||
293 | return "object"; | ||
294 | } | ||
295 | return typeof(v); | ||
296 | }; | ||
297 | |||
298 | //function FormatJSON(oData, sIndent) { | ||
299 | if (arguments.length < 2) { | ||
300 | var sIndent = ""; | ||
301 | } | ||
302 | // var sIndentStyle = " "; | ||
303 | var sIndentStyle = " "; | ||
304 | var sDataType = realTypeOf(anObject); | ||
305 | |||
306 | // open object | ||
307 | if (sDataType == "array") { | ||
308 | if (anObject.length == 0) { | ||
309 | return "[]"; | ||
310 | } | ||
311 | var sHTML = "["; | ||
312 | } else if (sDataType == "object") { | ||
313 | var sHTML = "{"; | ||
314 | } else { | ||
315 | return "{}"; | ||
316 | } | ||
317 | // } else { | ||
318 | // var iCount = 0; | ||
319 | // $.each(anObject, function() { | ||
320 | // iCount++; | ||
321 | // return; | ||
322 | // }); | ||
323 | // if (iCount == 0) { // object is empty | ||
324 | // return "{}"; | ||
325 | // } | ||
326 | // var sHTML = "{"; | ||
327 | // } | ||
328 | |||
329 | // loop through items | ||
330 | var iCount = 0; | ||
331 | // $.each(anObject, function(sKey, vValue) { | ||
332 | MochiKit.Iter.forEach(MochiKit.Base.keys(anObject), function(sKey) { | ||
333 | var vValue = anObject[sKey]; | ||
334 | |||
335 | if (iCount > 0) { | ||
336 | sHTML += ","; | ||
337 | } | ||
338 | if (sDataType == "array") { | ||
339 | sHTML += ("\n" + sIndent + sIndentStyle); | ||
340 | } else { | ||
341 | sHTML += ("\n" + sIndent + sIndentStyle + "\"" + sKey + "\"" + ": "); | ||
342 | } | ||
343 | |||
344 | // display relevant data type | ||
345 | switch (realTypeOf(vValue)) { | ||
346 | case "array": | ||
347 | case "object": | ||
348 | sHTML += Clipperz.Base.formatJSON(vValue, (sIndent + sIndentStyle)); | ||
349 | break; | ||
350 | case "boolean": | ||
351 | case "number": | ||
352 | sHTML += vValue.toString(); | ||
353 | break; | ||
354 | case "null": | ||
355 | sHTML += "null"; | ||
356 | break; | ||
357 | case "string": | ||
358 | sHTML += ("\"" + vValue + "\""); | ||
359 | break; | ||
360 | default: | ||
361 | sHTML += ("TYPEOF: " + typeof(vValue)); | ||
362 | } | ||
363 | |||
364 | // loop | ||
365 | iCount++; | ||
366 | }); | ||
367 | |||
368 | // close object | ||
369 | if (sDataType == "array") { | ||
370 | sHTML += ("\n" + sIndent + "]"); | ||
371 | } else { | ||
372 | sHTML += ("\n" + sIndent + "}"); | ||
373 | } | ||
374 | |||
375 | // return | ||
376 | return sHTML; | ||
377 | }, | ||
378 | |||
379 | //------------------------------------------------------------------------- | ||
380 | |||
381 | 'mergeItems': function (anArrayOfValues) { | ||
382 | var result; | ||
383 | var i, c; | ||
384 | |||
385 | result = {}; | ||
386 | |||
387 | c = anArrayOfValues.length; | ||
388 | for (i=0; i<c; i++) { | ||
389 | result[anArrayOfValues[i][0]] = anArrayOfValues[i][1]; | ||
390 | } | ||
391 | |||
392 | return result; | ||
393 | }, | ||
394 | |||
395 | //------------------------------------------------------------------------- | ||
396 | |||
397 | 'map': function (fn, lstObj/*, lst... */) { | ||
398 | var result; | ||
399 | |||
400 | if (MochiKit.Base.isArrayLike(lstObj)) { | ||
401 | result = MochiKit.Base.map.apply(this, arguments); | ||
402 | } else { | ||
403 | varkeys; | ||
404 | var values; | ||
405 | var computedValues; | ||
406 | |||
407 | keys = MochiKit.Base.keys(lstObj); | ||
408 | values = MochiKit.Base.values(lstObj); | ||
409 | computedValues = MochiKit.Base.map(fn, values); | ||
410 | |||
411 | result = Clipperz.Base.mergeItems(MochiKit.Base.zip(keys, computedValues)); | ||
412 | } | ||
413 | |||
414 | return result; | ||
415 | }, | ||
416 | |||
417 | //------------------------------------------------------------------------- | ||
418 | |||
419 | 'sanitizeString': function(aValue) { | ||
420 | var result; | ||
421 | |||
422 | if (Clipperz.Base.objectType(aValue) == 'string') { | ||
423 | result = aValue; | ||
424 | result = result.replace(/</img,"<"); | ||
425 | result = result.replace(/>/img,">"); | ||
426 | } else { | ||
427 | result = aValue; | ||
428 | } | ||
429 | |||
430 | return result; | ||
431 | }, | ||
432 | |||
433 | //------------------------------------------------------------------------- | ||
434 | |||
435 | 'module': function(aValue) { | ||
436 | // aValue = 'Clipperz.PM.Compact' | ||
437 | // | ||
438 | // if (typeof(Clipperz) == 'undefined') { Clipperz = {}; } | ||
439 | // if (typeof(Clipperz.PM) == 'undefined') { Clipperz.PM = {}; } | ||
440 | // if (typeof(Clipperz.PM.UI.Common.Components) == 'undefined') { Clipperz.PM.UI.Common.Components = {}; } | ||
441 | |||
442 | //console.log(">>> module: " + aValue); | ||
443 | var currentScope; | ||
444 | var pathElements; | ||
445 | var i,c; | ||
446 | |||
447 | currentScope = window; | ||
448 | pathElements = aValue.split('.'); | ||
449 | c = pathElements.length; | ||
450 | for (i=0; i<c; i++) { | ||
451 | //console.log("--- current path element: " + pathElements[i]); | ||
452 | //console.log("--- current scope", currentScope); | ||
453 | if (typeof(currentScope[pathElements[i]]) == 'undefined') { | ||
454 | currentScope[pathElements[i]] = {}; | ||
455 | } | ||
456 | |||
457 | currentScope = currentScope[pathElements[i]]; | ||
458 | } | ||
459 | }, | ||
460 | |||
461 | //------------------------------------------------------------------------- | ||
462 | |||
463 | 'exception': { | ||
464 | 'AbstractMethod': new MochiKit.Base.NamedError("Clipperz.Base.exception.AbstractMethod"), | ||
465 | 'UnknownType': new MochiKit.Base.NamedError("Clipperz.Base.exception.UnknownType"), | ||
466 | 'VulnerabilityIssue':new MochiKit.Base.NamedError("Clipperz.Base.exception.VulnerabilityIssue"), | ||
467 | 'MandatoryParameter':new MochiKit.Base.NamedError("Clipperz.Base.exception.MandatoryParameter"), | ||
468 | 'ObjectNotFound': new MochiKit.Base.NamedError("Clipperz.Base.exception.ObjectNotFound"), | ||
469 | 'raise': function (aName) { | ||
470 | throw Clipperz.Base.exception[aName]; | ||
471 | } | ||
472 | }, | ||
473 | |||
474 | //------------------------------------------------------------------------- | ||
475 | |||
476 | 'extend': YAHOO.extendX, | ||
477 | |||
478 | //------------------------------------------------------------------------- | ||
479 | __syntaxFix__: "syntax fix" | ||
480 | |||
481 | }); | ||
482 | |||
483 | //Original regExp courtesy of John Gruber: http://daringfireball.net/2009/11/liberal_regex_for_matching_urls | ||
484 | //Updated to match Clipperz usage pattern. | ||
485 | //MochiKit.Base.urlRegExp = new RegExp(/\b(([\w-]+:\/\/?|www[.])[^\s()<>]+(?:\([\w\d]+\)|([^[:punct:]\s]|\/)))/); | ||
486 | MochiKit.Base.urlRegExp = new RegExp(/^((([\w-]+:\/\/?)|(www\.))[^\s()<>]+((?:\([\w\d]+\)|([^[:punct:]\s]|\/)))?)/); | ||
487 | |||
488 | //RegExp found here: http://www.tipsntracks.com/117/validate-an-email-address-using-regular-expressions.html | ||
489 | MochiKit.Base.emailRegExp = new RegExp(/^([a-zA-Z0-9_\-\.]+)@(([a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3}))|(([01]?\d\d?|2[0-4]\d|25[0-5])\.){3}([01]?\d\d?|25[0-5]|2[0-4]\d))$/); | ||
490 | |||
491 | |||
492 | MochiKit.Base.registerComparator('Object dummy comparator', | ||
493 | function(a, b) { | ||
494 | return ((a.constructor == Object) && (b.constructor == Object)); | ||
495 | }, | ||
496 | function(a, b) { | ||
497 | var result; | ||
498 | var aKeys; | ||
499 | var bKeys; | ||
500 | |||
501 | //MochiKit.Logging.logDebug(">>> comparator"); | ||
502 | //MochiKit.Logging.logDebug("- a: " + Clipperz.Base.serializeJSON(a)); | ||
503 | //MochiKit.Logging.logDebug("- b: " + Clipperz.Base.serializeJSON(a)); | ||
504 | aKeys = MochiKit.Base.keys(a).sort(); | ||
505 | bKeys = MochiKit.Base.keys(b).sort(); | ||
506 | |||
507 | result = MochiKit.Base.compare(aKeys, bKeys); | ||
508 | //if (result != 0) { | ||
509 | //MochiKit.Logging.logDebug("- comparator 'keys':"); | ||
510 | //MochiKit.Logging.logDebug("- comparator aKeys: " + Clipperz.Base.serializeJSON(aKeys)); | ||
511 | //MochiKit.Logging.logDebug("- comparator bKeys: " + Clipperz.Base.serializeJSON(bKeys)); | ||
512 | //} | ||
513 | if (result == 0) { | ||
514 | vari, c; | ||
515 | |||
516 | c = aKeys.length; | ||
517 | for (i=0; (i<c) && (result == 0); i++) { | ||
518 | result = MochiKit.Base.compare(a[aKeys[i]], b[bKeys[i]]); | ||
519 | //if (result != 0) { | ||
520 | //MochiKit.Logging.logDebug("- comparator 'values':"); | ||
521 | //MochiKit.Logging.logDebug("- comparator a[aKeys[i]]: " + Clipperz.Base.serializeJSON(a[aKeys[i]])); | ||
522 | //MochiKit.Logging.logDebug("- comparator b[bKeys[i]]: " + Clipperz.Base.serializeJSON(b[bKeys[i]])); | ||
523 | //} | ||
524 | } | ||
525 | } | ||
526 | |||
527 | //MochiKit.Logging.logDebug("<<< comparator - result: " + result); | ||
528 | return result; | ||
529 | }, | ||
530 | true | ||
531 | ); | ||