2008-07-28 18:30:40 +02:00
|
|
|
var LSdebug_active = 0;
|
|
|
|
|
2008-11-14 16:30:58 +01:00
|
|
|
function LSdebug(arguments) {
|
2008-07-28 18:30:40 +02:00
|
|
|
if (LSdebug_active != 1) return;
|
2008-11-14 17:55:09 +01:00
|
|
|
if (typeof console != 'undefined') {
|
2008-11-14 16:30:58 +01:00
|
|
|
console.log(arguments);
|
2008-11-14 17:55:09 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (typeof opera != 'undefined') {
|
2008-11-14 16:30:58 +01:00
|
|
|
opera.postError(arguments);
|
2008-11-14 17:55:09 +01:00
|
|
|
return true;
|
|
|
|
}
|
2009-10-29 22:59:55 +01:00
|
|
|
if (typeof varLSdefault != 'undefined') {
|
|
|
|
varLSdefault.log(arguments);
|
|
|
|
return true;
|
|
|
|
}
|
2008-11-14 17:55:09 +01:00
|
|
|
alert(arguments);
|
2008-07-28 18:30:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2009-02-17 14:46:41 +01:00
|
|
|
* Construction of formatted string
|
2008-07-28 18:30:40 +02:00
|
|
|
*
|
2009-02-17 14:46:41 +01:00
|
|
|
* This function returns a formatted string according to given data & format parameters
|
2008-07-28 18:30:40 +02:00
|
|
|
*
|
|
|
|
* @author Benjamin Renard <brenard@easter-eggs.com>
|
|
|
|
*
|
2009-02-17 14:46:41 +01:00
|
|
|
* @param[in] $format string String Format
|
|
|
|
* @param[in] $data mixed Data used to compose the string.
|
|
|
|
* It can be strings array or object.
|
|
|
|
* @param[in] $meth string Object method name to call to get the new value for the formatted string.
|
2020-04-29 15:54:21 +02:00
|
|
|
*
|
2009-02-17 14:46:41 +01:00
|
|
|
* Invocation example :
|
2008-07-28 18:30:40 +02:00
|
|
|
* getFData('%{test1} je %{test2}',{
|
|
|
|
* getValue: function(val) {
|
|
|
|
* var data = {
|
|
|
|
* test1: 'val_test1',
|
|
|
|
* test2: 'val_test2'
|
|
|
|
* };
|
|
|
|
* return data[val];
|
|
|
|
* }
|
|
|
|
* },'getValue');
|
2020-04-29 15:54:21 +02:00
|
|
|
*
|
2009-02-17 14:46:41 +01:00
|
|
|
* @retval string The formatted string
|
2008-07-28 18:30:40 +02:00
|
|
|
*/
|
|
|
|
function getFData(format,data,meth) {
|
2014-07-04 12:41:16 +02:00
|
|
|
/*
|
2018-09-20 19:28:26 +02:00
|
|
|
* Format : %{[key name][:A][:B][! ou _][~][%]}
|
2014-07-04 12:41:16 +02:00
|
|
|
*
|
|
|
|
* Extracted fields
|
|
|
|
* - 1 : full string in %{}
|
|
|
|
* - 2 : key name
|
|
|
|
* - 3 : :A
|
|
|
|
* - 4 : A
|
|
|
|
* - 5 : :B
|
|
|
|
* - 6 : B
|
2018-09-20 19:28:26 +02:00
|
|
|
* - 7 : "!" / "_" / "~" / "%"
|
2014-07-04 12:41:16 +02:00
|
|
|
*/
|
2018-09-20 19:28:26 +02:00
|
|
|
var getMotif = new RegExp('%\{(([A-Za-z0-9]+)(\:(-?[0-9])+)?(\:(-?[0-9])+)?)([\!\_\~\%]*)?\}');
|
2020-04-29 15:54:21 +02:00
|
|
|
var find=1;
|
2008-10-27 15:06:49 +01:00
|
|
|
var val="";
|
2008-07-28 18:30:40 +02:00
|
|
|
if(($type(data)=='object') || ($type(data)=='array')) {
|
|
|
|
if ($type(data[meth])!='function') {
|
|
|
|
while (find) {
|
|
|
|
var ch = getMotif.exec(format);
|
|
|
|
if ($type(ch)) {
|
2014-07-04 12:41:16 +02:00
|
|
|
val=_getFData_extractAndModify(data[ch[2]],ch);
|
2008-10-27 15:06:49 +01:00
|
|
|
format=format.replace(new RegExp('%\{'+ch[1]+'\}'),val);
|
2008-07-28 18:30:40 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
find=0;
|
2020-04-29 15:54:21 +02:00
|
|
|
}
|
2008-07-28 18:30:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
while (find) {
|
|
|
|
var ch = getMotif.exec(format);
|
|
|
|
if ($type(ch)) {
|
|
|
|
try {
|
2008-10-27 15:06:49 +01:00
|
|
|
val=data[meth](ch[2]);
|
2014-06-20 13:38:25 +02:00
|
|
|
if ($type(val)=='array') {
|
|
|
|
if (val.length==0) {
|
|
|
|
val=''
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
val=val[0];
|
|
|
|
}
|
|
|
|
}
|
2008-07-28 18:30:40 +02:00
|
|
|
}
|
|
|
|
catch(e) {
|
2008-11-14 17:55:09 +01:00
|
|
|
LSdebug('getFData() : '+meth+'() -> rater');
|
2008-07-28 18:30:40 +02:00
|
|
|
return;
|
|
|
|
}
|
2020-04-29 15:54:21 +02:00
|
|
|
|
2014-07-04 12:41:16 +02:00
|
|
|
val=_getFData_extractAndModify(val,ch);
|
2020-04-29 15:54:21 +02:00
|
|
|
|
2018-09-20 19:28:26 +02:00
|
|
|
format=format.replace(new RegExp('%\{'+ch[1]+'[\:0-9\!\_\%\~]*\}'),val);
|
2008-07-28 18:30:40 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
find=0;
|
2020-04-29 15:54:21 +02:00
|
|
|
}
|
2008-07-28 18:30:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-07-04 12:41:16 +02:00
|
|
|
else if(($type(data)=='string')) {
|
|
|
|
while (find) {
|
|
|
|
var ch = getMotif.exec(format);
|
|
|
|
if ($type(ch)) {
|
|
|
|
val=_getFData_extractAndModify(data,ch)
|
2018-09-20 19:28:26 +02:00
|
|
|
format=format.replace(new RegExp('%\{'+ch[1]+'[\:0-9\!\_\%\~]*\}'),val);
|
2014-07-04 12:41:16 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
find=0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2008-07-28 18:30:40 +02:00
|
|
|
return format;
|
|
|
|
}
|
2008-10-06 15:11:14 +02:00
|
|
|
|
2014-07-04 12:41:16 +02:00
|
|
|
function _getFData_extractAndModify(data,ch) {
|
2018-09-20 19:28:26 +02:00
|
|
|
/*
|
|
|
|
* Extracted fields
|
|
|
|
* - 1 : full string in %{}
|
|
|
|
* - 2 : key name
|
|
|
|
* - 3 : :A
|
|
|
|
* - 4 : A
|
|
|
|
* - 5 : :B
|
|
|
|
* - 6 : B
|
|
|
|
* - 7 : "!" / "_" / "~" / "%"
|
|
|
|
*/
|
|
|
|
var val=(' ' + data).slice(1);
|
2014-07-04 12:41:16 +02:00
|
|
|
// If A
|
|
|
|
if($type(ch[4])) {
|
|
|
|
ch[4]=parseInt(ch[4]);
|
2018-09-20 19:28:26 +02:00
|
|
|
// If A and B
|
2014-07-04 12:41:16 +02:00
|
|
|
if ($type(ch[6])) {
|
|
|
|
ch[6]=parseInt(ch[6]);
|
2018-09-20 19:28:26 +02:00
|
|
|
// If A and B=0
|
2014-07-04 12:41:16 +02:00
|
|
|
if (ch[6]==0) {
|
2018-09-20 19:28:26 +02:00
|
|
|
// If A<0 and B=0
|
|
|
|
if (ch[4]<0) {
|
|
|
|
s=val.length-(-1*ch[4]);
|
|
|
|
l=val.length;
|
|
|
|
}
|
|
|
|
// If A >= 0 and B
|
|
|
|
else {
|
|
|
|
s=ch[4];
|
|
|
|
l=val.length;
|
|
|
|
}
|
2014-07-04 12:41:16 +02:00
|
|
|
}
|
2018-09-20 19:28:26 +02:00
|
|
|
// If A and B > 0
|
|
|
|
else if (ch[6]>0) {
|
|
|
|
// If A < 0 and B > 0 or A >= 0 and B > 0
|
2014-07-04 12:41:16 +02:00
|
|
|
s=ch[4];
|
|
|
|
l=ch[6];
|
|
|
|
}
|
2018-09-20 19:28:26 +02:00
|
|
|
// If A and B < 0
|
2014-07-04 12:41:16 +02:00
|
|
|
else {
|
2018-09-20 19:28:26 +02:00
|
|
|
// If A < 0 and B < 0
|
|
|
|
if (ch[4]<0) {
|
|
|
|
s=ch[6];
|
|
|
|
l=false;
|
2014-07-04 12:41:16 +02:00
|
|
|
}
|
2018-09-20 19:28:26 +02:00
|
|
|
// If A >= 0 and B < 0
|
2014-07-04 12:41:16 +02:00
|
|
|
else {
|
2018-09-20 19:28:26 +02:00
|
|
|
s=ch[4]+ch[6];
|
|
|
|
l=Math.abs(ch[6]);
|
2014-07-04 12:41:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-09-20 19:28:26 +02:00
|
|
|
// If only A
|
2014-07-04 12:41:16 +02:00
|
|
|
else {
|
2018-09-20 19:28:26 +02:00
|
|
|
if (ch[4]<0) {
|
|
|
|
s=ch[4];
|
|
|
|
l=false;
|
2014-07-04 12:41:16 +02:00
|
|
|
}
|
|
|
|
else {
|
2018-09-20 19:28:26 +02:00
|
|
|
s=0;
|
|
|
|
l=ch[4];
|
2014-07-04 12:41:16 +02:00
|
|
|
}
|
|
|
|
}
|
2018-09-20 19:28:26 +02:00
|
|
|
|
|
|
|
if (l==false) {
|
|
|
|
val=val.substr(s);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
val=val.substr(s, Math.abs(l));
|
|
|
|
}
|
2014-07-04 12:41:16 +02:00
|
|
|
}
|
2018-09-20 19:28:26 +02:00
|
|
|
|
|
|
|
if (ch[7] != undefined) {
|
|
|
|
// Upper or Lower case
|
|
|
|
if (ch[7].indexOf('!')>=0) {
|
|
|
|
val=val.toUpperCase();
|
|
|
|
}
|
|
|
|
else if (ch[7].indexOf('_')>=0) {
|
|
|
|
val=val.toLowerCase();
|
|
|
|
}
|
|
|
|
// Strip accents
|
|
|
|
if (ch[7].indexOf('~')>=0) {
|
|
|
|
val=new String(replaceAccents(val));
|
|
|
|
}
|
|
|
|
// Escape HTML entities
|
|
|
|
if (ch[7].indexOf('%')>=0) {
|
|
|
|
val=val.replace(/[\u00A0-\u9999<>\&]/gim, function(i) {
|
|
|
|
return '&#'+i.charCodeAt(0)+';';
|
|
|
|
});
|
|
|
|
}
|
2014-07-04 12:41:16 +02:00
|
|
|
}
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
2008-10-06 15:11:14 +02:00
|
|
|
/**
|
2009-02-17 14:46:41 +01:00
|
|
|
* Delete accentuated characters in a string
|
2020-04-29 15:54:21 +02:00
|
|
|
*
|
2009-02-17 14:46:41 +01:00
|
|
|
* @param[in] $string Original string
|
2020-04-29 15:54:21 +02:00
|
|
|
*
|
2009-02-17 14:46:41 +01:00
|
|
|
* @retval string de-accentuated string
|
2008-10-06 15:11:14 +02:00
|
|
|
*/
|
|
|
|
function replaceAccents(str) {
|
2018-09-20 19:28:26 +02:00
|
|
|
let accent = "àáâãäçèéêëìíîïñòóôõöùúûüýÿÀÁÂÃÄÇÈÉÊËÌÍÎÏÑÒÓÔÕÖÙÚÛÜÝ";
|
|
|
|
let sans_accent ="aaaaaceeeeiiiinooooouuuuyyAAAAACEEEEIIIINOOOOOUUUUY";
|
|
|
|
|
|
|
|
str = str.split('');
|
|
|
|
str.forEach((letter, index) => {
|
|
|
|
let i = accent.indexOf(letter);
|
|
|
|
if (i != -1) {
|
|
|
|
str[index] = sans_accent[i];
|
2008-10-06 15:11:14 +02:00
|
|
|
}
|
2018-09-20 19:28:26 +02:00
|
|
|
})
|
|
|
|
return str.join('');
|
2008-10-06 15:11:14 +02:00
|
|
|
}
|
2008-10-07 18:10:44 +02:00
|
|
|
|
|
|
|
/**
|
2009-02-17 14:46:41 +01:00
|
|
|
* Replace spaces or tabs of a string by an argument
|
2020-04-29 15:54:21 +02:00
|
|
|
*
|
2009-02-17 14:46:41 +01:00
|
|
|
* @param[in] $string The original string
|
|
|
|
* @param[in] $string The character to set instead of spaces or tabs
|
2020-04-29 15:54:21 +02:00
|
|
|
*
|
2009-02-17 14:46:41 +01:00
|
|
|
* @retval string The modified outspaced string
|
2008-10-07 18:10:44 +02:00
|
|
|
*/
|
|
|
|
function replaceSpaces(str,to) {
|
|
|
|
if (!$type(to)) {
|
|
|
|
to = '';
|
|
|
|
}
|
|
|
|
var new_str = String(str);
|
|
|
|
if (str && str!= "") {
|
|
|
|
var reg_exp= RegExp('[ \t]', "gi");
|
|
|
|
new_str = new_str.replace (reg_exp, to);
|
|
|
|
}
|
|
|
|
return new_str;
|
|
|
|
}
|
2009-10-22 10:31:54 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Add one variable with value in URL
|
|
|
|
* @param[in] url string The original URL
|
|
|
|
* @param[in] name string The variable name
|
|
|
|
* @param[in] value string The value of the variable
|
2020-04-29 15:54:21 +02:00
|
|
|
*
|
2009-10-22 10:31:54 +02:00
|
|
|
* @retval string The URL with the value
|
|
|
|
*/
|
|
|
|
function urlAddVar(url,name,value) {
|
|
|
|
if ($type(url)) {
|
|
|
|
var isExtended = RegExp('[?]');
|
|
|
|
if (isExtended.test(url)) {
|
|
|
|
url=url+'&';
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
url=url+'?';
|
|
|
|
}
|
|
|
|
return url + name + '=' + value;
|
|
|
|
}
|
|
|
|
return url;
|
|
|
|
}
|
2018-04-26 11:06:10 +02:00
|
|
|
|
2018-04-26 11:23:00 +02:00
|
|
|
/*
|
|
|
|
* Generate UUID
|
|
|
|
*/
|
|
|
|
function generate_uuid() {
|
|
|
|
function s4() {
|
|
|
|
return Math.floor((1 + Math.random()) * 0x10000)
|
|
|
|
.toString(16)
|
|
|
|
.substring(1);
|
|
|
|
}
|
|
|
|
return s4() + s4() + '-' + s4() + '-' + s4() + '-' + s4() + '-' + s4() + s4() + s4();
|
|
|
|
}
|
|
|
|
|
2018-04-26 11:06:10 +02:00
|
|
|
/*
|
|
|
|
* Base64 compatibility
|
|
|
|
*
|
|
|
|
* Source : http://ntt.cc/2008/01/19/base64-encoder-decoder-with-javascript.html
|
|
|
|
*/
|
|
|
|
if ($type(atob) != 'function') {
|
|
|
|
B64keyStr = "ABCDEFGHIJKLMNOP" +
|
|
|
|
"QRSTUVWXYZabcdef" +
|
|
|
|
"ghijklmnopqrstuv" +
|
|
|
|
"wxyz0123456789+/" +
|
|
|
|
"=";
|
|
|
|
|
|
|
|
function btoa(input) {
|
|
|
|
input = escape(input);
|
|
|
|
var output = "";
|
|
|
|
var chr1, chr2, chr3 = "";
|
|
|
|
var enc1, enc2, enc3, enc4 = "";
|
|
|
|
var i = 0;
|
|
|
|
|
|
|
|
do {
|
|
|
|
chr1 = input.charCodeAt(i++);
|
|
|
|
chr2 = input.charCodeAt(i++);
|
|
|
|
chr3 = input.charCodeAt(i++);
|
|
|
|
|
|
|
|
enc1 = chr1 >> 2;
|
|
|
|
enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
|
|
|
|
enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
|
|
|
|
enc4 = chr3 & 63;
|
|
|
|
|
|
|
|
if (isNaN(chr2)) {
|
|
|
|
enc3 = enc4 = 64;
|
|
|
|
} else if (isNaN(chr3)) {
|
|
|
|
enc4 = 64;
|
|
|
|
}
|
|
|
|
|
|
|
|
output = output +
|
|
|
|
B64keyStr.charAt(enc1) +
|
|
|
|
B64keyStr.charAt(enc2) +
|
|
|
|
B64keyStr.charAt(enc3) +
|
|
|
|
B64keyStr.charAt(enc4);
|
|
|
|
chr1 = chr2 = chr3 = "";
|
|
|
|
enc1 = enc2 = enc3 = enc4 = "";
|
|
|
|
} while (i < input.length);
|
|
|
|
|
|
|
|
return output;
|
|
|
|
}
|
|
|
|
|
|
|
|
function atob(input) {
|
|
|
|
var output = "";
|
|
|
|
var chr1, chr2, chr3 = "";
|
|
|
|
var enc1, enc2, enc3, enc4 = "";
|
|
|
|
var i = 0;
|
|
|
|
|
|
|
|
// remove all characters that are not A-Z, a-z, 0-9, +, /, or =
|
|
|
|
var base64test = /[^A-Za-z0-9\+\/\=]/g;
|
|
|
|
if (base64test.exec(input)) {
|
|
|
|
alert("There were invalid base64 characters in the input text.\n" +
|
|
|
|
"Valid base64 characters are A-Z, a-z, 0-9, '+', '/',and '='\n" +
|
|
|
|
"Expect errors in decoding.");
|
|
|
|
}
|
|
|
|
input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
|
|
|
|
|
|
|
|
do {
|
|
|
|
enc1 = B64keyStr.indexOf(input.charAt(i++));
|
|
|
|
enc2 = B64keyStr.indexOf(input.charAt(i++));
|
|
|
|
enc3 = B64keyStr.indexOf(input.charAt(i++));
|
|
|
|
enc4 = B64keyStr.indexOf(input.charAt(i++));
|
|
|
|
|
|
|
|
chr1 = (enc1 << 2) | (enc2 >> 4);
|
|
|
|
chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
|
|
|
|
chr3 = ((enc3 & 3) << 6) | enc4;
|
|
|
|
|
|
|
|
output = output + String.fromCharCode(chr1);
|
|
|
|
|
|
|
|
if (enc3 != 64) {
|
|
|
|
output = output + String.fromCharCode(chr2);
|
|
|
|
}
|
|
|
|
if (enc4 != 64) {
|
|
|
|
output = output + String.fromCharCode(chr3);
|
|
|
|
}
|
|
|
|
|
|
|
|
chr1 = chr2 = chr3 = "";
|
|
|
|
enc1 = enc2 = enc3 = enc4 = "";
|
|
|
|
|
|
|
|
} while (i < input.length);
|
|
|
|
|
|
|
|
return unescape(output);
|
|
|
|
}
|
|
|
|
}
|