PageRenderTime 31ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/public/scripts/media_files/js/helper.js

https://gitlab.com/devtoannh/cafe
JavaScript | 411 lines | 350 code | 50 blank | 11 comment | 52 complexity | a5615fa105bc7df78c958b5949b2e75d MD5 | raw file
  1. /** This file is part of KCFinder project
  2. *
  3. * @desc Helper object
  4. * @package KCFinder
  5. * @version 2.51
  6. * @author Pavel Tzonkov <pavelc@users.sourceforge.net>
  7. * @copyright 2010, 2011 KCFinder Project
  8. * @license http://www.opensource.org/licenses/gpl-2.0.php GPLv2
  9. * @license http://www.opensource.org/licenses/lgpl-2.1.php LGPLv2
  10. * @link http://kcfinder.sunhater.com
  11. */
  12. var _ = function(id) {
  13. return document.getElementById(id);
  14. };
  15. _.nopx = function(val) {
  16. return parseInt(val.replace(/^(\d+)px$/, "$1"));
  17. };
  18. _.unselect = function() {
  19. if (document.selection && document.selection.empty)
  20. document.selection.empty() ;
  21. else if (window.getSelection) {
  22. var sel = window.getSelection();
  23. if (sel && sel.removeAllRanges)
  24. sel.removeAllRanges();
  25. }
  26. };
  27. _.selection = function(field, start, end) {
  28. if (field.createTextRange) {
  29. var selRange = field.createTextRange();
  30. selRange.collapse(true);
  31. selRange.moveStart('character', start);
  32. selRange.moveEnd('character', end-start);
  33. selRange.select();
  34. } else if (field.setSelectionRange) {
  35. field.setSelectionRange(start, end);
  36. } else if (field.selectionStart) {
  37. field.selectionStart = start;
  38. field.selectionEnd = end;
  39. }
  40. field.focus();
  41. };
  42. _.htmlValue = function(value) {
  43. return value
  44. .replace(/\&/g, "&amp;")
  45. .replace(/\"/g, "&quot;")
  46. .replace(/\'/g, "&#39;");
  47. };
  48. _.htmlData = function(value) {
  49. return value
  50. .replace(/\&/g, "&amp;")
  51. .replace(/\</g, "&lt;")
  52. .replace(/\>/g, "&gt;")
  53. .replace(/\ /g, "&nbsp;");
  54. }
  55. _.jsValue = function(value) {
  56. return value
  57. .replace(/\\/g, "\\\\")
  58. .replace(/\r?\n/, "\\\n")
  59. .replace(/\"/g, "\\\"")
  60. .replace(/\'/g, "\\'");
  61. };
  62. _.basename = function(path) {
  63. var expr = /^.*\/([^\/]+)\/?$/g;
  64. return expr.test(path)
  65. ? path.replace(expr, "$1")
  66. : path;
  67. };
  68. _.dirname = function(path) {
  69. var expr = /^(.*)\/[^\/]+\/?$/g;
  70. return expr.test(path)
  71. ? path.replace(expr, "$1")
  72. : '';
  73. };
  74. _.inArray = function(needle, arr) {
  75. if ((typeof arr == 'undefined') || !arr.length || !arr.push)
  76. return false;
  77. for (var i = 0; i < arr.length; i++)
  78. if (arr[i] == needle)
  79. return true;
  80. return false;
  81. };
  82. _.getFileExtension = function(filename, toLower) {
  83. if (typeof(toLower) == 'undefined') toLower = true;
  84. if (/^.*\.[^\.]*$/.test(filename)) {
  85. var ext = filename.replace(/^.*\.([^\.]*)$/, "$1");
  86. return toLower ? ext.toLowerCase(ext) : ext;
  87. } else
  88. return "";
  89. };
  90. _.escapeDirs = function(path) {
  91. var fullDirExpr = /^([a-z]+)\:\/\/([^\/^\:]+)(\:(\d+))?\/(.+)$/,
  92. prefix = "";
  93. if (fullDirExpr.test(path)) {
  94. var port = path.replace(fullDirExpr, "$4");
  95. prefix = path.replace(fullDirExpr, "$1://$2")
  96. if (port.length)
  97. prefix += ":" + port;
  98. prefix += "/";
  99. path = path.replace(fullDirExpr, "$5");
  100. }
  101. var dirs = path.split('/');
  102. var escapePath = '';
  103. for (var i = 0; i < dirs.length; i++)
  104. escapePath += encodeURIComponent(dirs[i]) + '/';
  105. return prefix + escapePath.substr(0, escapePath.length - 1);
  106. };
  107. _.outerSpace = function(selector, type, mbp) {
  108. if (!mbp) mbp = "mbp";
  109. var r = 0;
  110. if (/m/i.test(mbp)) {
  111. var m = _.nopx($(selector).css('margin-' + type));
  112. if (m) r += m;
  113. }
  114. if (/b/i.test(mbp)) {
  115. var b = _.nopx($(selector).css('border-' + type + '-width'));
  116. if (b) r += b;
  117. }
  118. if (/p/i.test(mbp)) {
  119. var p = _.nopx($(selector).css('padding-' + type));
  120. if (p) r += p;
  121. }
  122. return r;
  123. };
  124. _.outerLeftSpace = function(selector, mbp) {
  125. return _.outerSpace(selector, 'left', mbp);
  126. };
  127. _.outerTopSpace = function(selector, mbp) {
  128. return _.outerSpace(selector, 'top', mbp);
  129. };
  130. _.outerRightSpace = function(selector, mbp) {
  131. return _.outerSpace(selector, 'right', mbp);
  132. };
  133. _.outerBottomSpace = function(selector, mbp) {
  134. return _.outerSpace(selector, 'bottom', mbp);
  135. };
  136. _.outerHSpace = function(selector, mbp) {
  137. return (_.outerLeftSpace(selector, mbp) + _.outerRightSpace(selector, mbp));
  138. };
  139. _.outerVSpace = function(selector, mbp) {
  140. return (_.outerTopSpace(selector, mbp) + _.outerBottomSpace(selector, mbp));
  141. };
  142. _.kuki = {
  143. prefix: '',
  144. duration: 356,
  145. domain: '',
  146. path: '',
  147. secure: false,
  148. set: function(name, value, duration, domain, path, secure) {
  149. name = this.prefix + name;
  150. if (duration == null) duration = this.duration;
  151. if (secure == null) secure = this.secure;
  152. if ((domain == null) && this.domain) domain = this.domain;
  153. if ((path == null) && this.path) path = this.path;
  154. secure = secure ? true : false;
  155. var date = new Date();
  156. date.setTime(date.getTime() + (duration * 86400000));
  157. var expires = date.toGMTString();
  158. var str = name + '=' + value + '; expires=' + expires;
  159. if (domain != null) str += '; domain=' + domain;
  160. if (path != null) str += '; path=' + path;
  161. if (secure) str += '; secure';
  162. return (document.cookie = str) ? true : false;
  163. },
  164. get: function(name) {
  165. name = this.prefix + name;
  166. var nameEQ = name + '=';
  167. var kukis = document.cookie.split(';');
  168. var kuki;
  169. for (var i = 0; i < kukis.length; i++) {
  170. kuki = kukis[i];
  171. while (kuki.charAt(0) == ' ')
  172. kuki = kuki.substring(1, kuki.length);
  173. if (kuki.indexOf(nameEQ) == 0)
  174. return kuki.substring(nameEQ.length, kuki.length);
  175. }
  176. return null;
  177. },
  178. del: function(name) {
  179. return this.set(name, '', -1);
  180. },
  181. isSet: function(name) {
  182. return (this.get(name) != null);
  183. }
  184. };
  185. _.md5 = function(string) {
  186. var RotateLeft = function(lValue, iShiftBits) {
  187. return (lValue<<iShiftBits) | (lValue>>>(32-iShiftBits));
  188. };
  189. var AddUnsigned = function(lX,lY) {
  190. var lX4, lY4, lX8, lY8, lResult;
  191. lX8 = (lX & 0x80000000);
  192. lY8 = (lY & 0x80000000);
  193. lX4 = (lX & 0x40000000);
  194. lY4 = (lY & 0x40000000);
  195. lResult = (lX & 0x3FFFFFFF) + (lY & 0x3FFFFFFF);
  196. if (lX4 & lY4)
  197. return (lResult ^ 0x80000000 ^ lX8 ^ lY8);
  198. if (lX4 | lY4)
  199. return (lResult & 0x40000000)
  200. ? (lResult ^ 0xC0000000 ^ lX8 ^ lY8)
  201. : (lResult ^ 0x40000000 ^ lX8 ^ lY8);
  202. else
  203. return (lResult ^ lX8 ^ lY8);
  204. };
  205. var F = function(x, y, z) { return (x & y) | ((~x) & z); };
  206. var G = function(x, y, z) { return (x & z) | (y & (~z)); };
  207. var H = function(x, y, z) { return (x ^ y ^ z); };
  208. var I = function(x, y, z) { return (y ^ (x | (~z))); };
  209. var FF = function(a, b, c, d, x, s, ac) {
  210. a = AddUnsigned(a, AddUnsigned(AddUnsigned(F(b, c, d), x), ac));
  211. return AddUnsigned(RotateLeft(a, s), b);
  212. };
  213. var GG = function(a, b, c, d, x, s, ac) {
  214. a = AddUnsigned(a, AddUnsigned(AddUnsigned(G(b, c, d), x), ac));
  215. return AddUnsigned(RotateLeft(a, s), b);
  216. };
  217. var HH = function(a, b, c, d, x, s, ac) {
  218. a = AddUnsigned(a, AddUnsigned(AddUnsigned(H(b, c, d), x), ac));
  219. return AddUnsigned(RotateLeft(a, s), b);
  220. };
  221. var II = function(a, b, c, d, x, s, ac) {
  222. a = AddUnsigned(a, AddUnsigned(AddUnsigned(I(b, c, d), x), ac));
  223. return AddUnsigned(RotateLeft(a, s), b);
  224. };
  225. var ConvertToWordArray = function(string) {
  226. var lWordCount;
  227. var lMessageLength = string.length;
  228. var lNumberOfWords_temp1 = lMessageLength + 8;
  229. var lNumberOfWords_temp2 = (lNumberOfWords_temp1 - (lNumberOfWords_temp1 % 64)) / 64;
  230. var lNumberOfWords = (lNumberOfWords_temp2 + 1) * 16;
  231. var lWordArray = [lNumberOfWords - 1];
  232. var lBytePosition = 0;
  233. var lByteCount = 0;
  234. while (lByteCount < lMessageLength) {
  235. lWordCount = (lByteCount - (lByteCount % 4)) / 4;
  236. lBytePosition = (lByteCount % 4) * 8;
  237. lWordArray[lWordCount] = (lWordArray[lWordCount] | (string.charCodeAt(lByteCount) << lBytePosition));
  238. lByteCount++;
  239. }
  240. lWordCount = (lByteCount - (lByteCount % 4)) / 4;
  241. lBytePosition = (lByteCount % 4) * 8;
  242. lWordArray[lWordCount] = lWordArray[lWordCount] | (0x80 << lBytePosition);
  243. lWordArray[lNumberOfWords - 2] = lMessageLength << 3;
  244. lWordArray[lNumberOfWords - 1] = lMessageLength >>> 29;
  245. return lWordArray;
  246. };
  247. var WordToHex = function(lValue) {
  248. var WordToHexValue = "", WordToHexValue_temp = "", lByte, lCount;
  249. for (lCount = 0; lCount <= 3; lCount++) {
  250. lByte = (lValue >>> (lCount * 8)) & 255;
  251. WordToHexValue_temp = "0" + lByte.toString(16);
  252. WordToHexValue = WordToHexValue + WordToHexValue_temp.substr(WordToHexValue_temp.length - 2,2);
  253. }
  254. return WordToHexValue;
  255. };
  256. var x = [];
  257. var k, AA, BB, CC, DD, a, b, c, d;
  258. var S11 = 7, S12 = 12, S13 = 17, S14 = 22;
  259. var S21 = 5, S22 = 9, S23 = 14, S24 = 20;
  260. var S31 = 4, S32 = 11, S33 = 16, S34 = 23;
  261. var S41 = 6, S42 = 10, S43 = 15, S44 = 21;
  262. string = _.utf8encode(string);
  263. x = ConvertToWordArray(string);
  264. a = 0x67452301; b = 0xEFCDAB89; c = 0x98BADCFE; d = 0x10325476;
  265. for (k = 0; k < x.length; k += 16) {
  266. AA = a; BB = b; CC = c; DD = d;
  267. a = FF(a, b, c, d, x[k + 0], S11, 0xD76AA478);
  268. d = FF(d, a, b, c, x[k + 1], S12, 0xE8C7B756);
  269. c = FF(c, d, a, b, x[k + 2], S13, 0x242070DB);
  270. b = FF(b, c, d, a, x[k + 3], S14, 0xC1BDCEEE);
  271. a = FF(a, b, c, d, x[k + 4], S11, 0xF57C0FAF);
  272. d = FF(d, a, b, c, x[k + 5], S12, 0x4787C62A);
  273. c = FF(c, d, a, b, x[k + 6], S13, 0xA8304613);
  274. b = FF(b, c, d, a, x[k + 7], S14, 0xFD469501);
  275. a = FF(a, b, c, d, x[k + 8], S11, 0x698098D8);
  276. d = FF(d, a, b, c, x[k + 9], S12, 0x8B44F7AF);
  277. c = FF(c, d, a, b, x[k + 10], S13, 0xFFFF5BB1);
  278. b = FF(b, c, d, a, x[k + 11], S14, 0x895CD7BE);
  279. a = FF(a, b, c, d, x[k + 12], S11, 0x6B901122);
  280. d = FF(d, a, b, c, x[k + 13], S12, 0xFD987193);
  281. c = FF(c, d, a, b, x[k + 14], S13, 0xA679438E);
  282. b = FF(b, c, d, a, x[k + 15], S14, 0x49B40821);
  283. a = GG(a, b, c, d, x[k + 1], S21, 0xF61E2562);
  284. d = GG(d, a, b, c, x[k + 6], S22, 0xC040B340);
  285. c = GG(c, d, a, b, x[k + 11], S23, 0x265E5A51);
  286. b = GG(b, c, d, a, x[k + 0], S24, 0xE9B6C7AA);
  287. a = GG(a, b, c, d, x[k + 5], S21, 0xD62F105D);
  288. d = GG(d, a, b, c, x[k + 10], S22, 0x2441453);
  289. c = GG(c, d, a, b, x[k + 15], S23, 0xD8A1E681);
  290. b = GG(b, c, d, a, x[k + 4], S24, 0xE7D3FBC8);
  291. a = GG(a, b, c, d, x[k + 9], S21, 0x21E1CDE6);
  292. d = GG(d, a, b, c, x[k + 14], S22, 0xC33707D6);
  293. c = GG(c, d, a, b, x[k + 3], S23, 0xF4D50D87);
  294. b = GG(b, c, d, a, x[k + 8], S24, 0x455A14ED);
  295. a = GG(a, b, c, d, x[k + 13], S21, 0xA9E3E905);
  296. d = GG(d, a, b, c, x[k + 2], S22, 0xFCEFA3F8);
  297. c = GG(c, d, a, b, x[k + 7], S23, 0x676F02D9);
  298. b = GG(b, c, d, a, x[k + 12], S24, 0x8D2A4C8A);
  299. a = HH(a, b, c, d, x[k + 5], S31, 0xFFFA3942);
  300. d = HH(d, a, b, c, x[k + 8], S32, 0x8771F681);
  301. c = HH(c, d, a, b, x[k + 11], S33, 0x6D9D6122);
  302. b = HH(b, c, d, a, x[k + 14], S34, 0xFDE5380C);
  303. a = HH(a, b, c, d, x[k + 1], S31, 0xA4BEEA44);
  304. d = HH(d, a, b, c, x[k + 4], S32, 0x4BDECFA9);
  305. c = HH(c, d, a, b, x[k + 7], S33, 0xF6BB4B60);
  306. b = HH(b, c, d, a, x[k + 10], S34, 0xBEBFBC70);
  307. a = HH(a, b, c, d, x[k + 13], S31, 0x289B7EC6);
  308. d = HH(d, a, b, c, x[k + 0], S32, 0xEAA127FA);
  309. c = HH(c, d, a, b, x[k + 3], S33, 0xD4EF3085);
  310. b = HH(b, c, d, a, x[k + 6], S34, 0x4881D05);
  311. a = HH(a, b, c, d, x[k + 9], S31, 0xD9D4D039);
  312. d = HH(d, a, b, c, x[k + 12], S32, 0xE6DB99E5);
  313. c = HH(c, d, a, b, x[k + 15], S33, 0x1FA27CF8);
  314. b = HH(b, c, d, a, x[k + 2], S34, 0xC4AC5665);
  315. a = II(a, b, c, d, x[k + 0], S41, 0xF4292244);
  316. d = II(d, a, b, c, x[k + 7], S42, 0x432AFF97);
  317. c = II(c, d, a, b, x[k + 14], S43, 0xAB9423A7);
  318. b = II(b, c, d, a, x[k + 5], S44, 0xFC93A039);
  319. a = II(a, b, c, d, x[k + 12], S41, 0x655B59C3);
  320. d = II(d, a, b, c, x[k + 3], S42, 0x8F0CCC92);
  321. c = II(c, d, a, b, x[k + 10], S43, 0xFFEFF47D);
  322. b = II(b, c, d, a, x[k + 1], S44, 0x85845DD1);
  323. a = II(a, b, c, d, x[k + 8], S41, 0x6FA87E4F);
  324. d = II(d, a, b, c, x[k + 15], S42, 0xFE2CE6E0);
  325. c = II(c, d, a, b, x[k + 6], S43, 0xA3014314);
  326. b = II(b, c, d, a, x[k + 13], S44, 0x4E0811A1);
  327. a = II(a, b, c, d, x[k + 4], S41, 0xF7537E82);
  328. d = II(d, a, b, c, x[k + 11], S42, 0xBD3AF235);
  329. c = II(c, d, a, b, x[k + 2], S43, 0x2AD7D2BB);
  330. b = II(b, c, d, a, x[k + 9], S44, 0xEB86D391);
  331. a = AddUnsigned(a, AA);
  332. b = AddUnsigned(b, BB);
  333. c = AddUnsigned(c, CC);
  334. d = AddUnsigned(d, DD);
  335. }
  336. var temp = WordToHex(a) + WordToHex(b) + WordToHex(c) + WordToHex(d);
  337. return temp.toLowerCase();
  338. };
  339. _.utf8encode = function(string) {
  340. string = string.replace(/\r\n/g,"\n");
  341. var utftext = "";
  342. for (var n = 0; n < string.length; n++) {
  343. var c = string.charCodeAt(n);
  344. if (c < 128) {
  345. utftext += String.fromCharCode(c);
  346. } else if((c > 127) && (c < 2048)) {
  347. utftext += String.fromCharCode((c >> 6) | 192);
  348. utftext += String.fromCharCode((c & 63) | 128);
  349. } else {
  350. utftext += String.fromCharCode((c >> 12) | 224);
  351. utftext += String.fromCharCode(((c >> 6) & 63) | 128);
  352. utftext += String.fromCharCode((c & 63) | 128);
  353. }
  354. }
  355. return utftext;
  356. };