Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
fusiondirectory
fusiondirectory
Commits
095e51d4
Commit
095e51d4
authored
Jan 25, 2017
by
Côme Chilliet
Browse files
Fixed complexity and length of templateHandling::applyModifier for sonar
parent
afcf7476
Changes
1
Hide whitespace changes
Inline
Side-by-side
include/class_templateHandling.inc
View file @
095e51d4
...
...
@@ -245,6 +245,56 @@ class templateHandling
return
$fields
;
}
private
static
function
randomStringModifier
(
array
$args
)
{
$length
=
8
;
$chars
=
'b'
;
if
(
count
(
$args
)
>=
2
)
{
$length
=
mt_rand
(
$args
[
0
],
$args
[
1
]);
if
(
count
(
$args
)
>=
3
)
{
$chars
=
$args
[
2
];
}
}
elseif
(
count
(
$args
)
>=
1
)
{
$length
=
$args
[
0
];
}
$res
=
''
;
for
(
$i
=
0
;
$i
<
$length
;
++
$i
)
{
switch
(
$chars
)
{
case
'd'
:
/* digits */
$res
.
=
(
string
)
rand
(
0
,
9
);
break
;
case
'l'
:
/* letters */
$nb
=
mt_rand
(
65
,
116
);
if
(
$nb
>
90
)
{
/* lowercase */
$nb
+=
6
;
}
$res
.
=
chr
(
$nb
);
break
;
case
'b'
:
/* both */
default
:
$nb
=
mt_rand
(
65
,
126
);
if
(
$nb
>
116
)
{
/* digit */
$nb
=
(
string
)(
$nb
-
117
);
}
else
{
if
(
$nb
>
90
)
{
/* lowercase */
$nb
+=
6
;
}
$nb
=
chr
(
$nb
);
}
$res
.
=
$nb
;
break
;
}
}
return
$res
;
}
/*! \brief Apply a modifier
*
* \param string $m the modifier
...
...
@@ -261,38 +311,47 @@ class templateHandling
/* $str is an array and $m is lowercase, so it's a string modifier */
$str
=
$str
[
0
];
}
$result
=
array
(
$str
);
switch
(
$m
)
{
case
'F'
:
// First
return
array
(
$str
[
0
]);
$result
=
array
(
$str
[
0
]);
break
;
case
'L'
:
// Last
return
array
(
end
(
$str
));
$result
=
array
(
end
(
$str
));
break
;
case
'J'
:
// Join
if
(
isset
(
$args
[
0
]))
{
re
turn
array
(
join
(
$args
[
0
],
$str
));
$
re
sult
=
array
(
join
(
$args
[
0
],
$str
));
}
else
{
re
turn
array
(
join
(
$str
));
$
re
sult
=
array
(
join
(
$str
));
}
break
;
case
'C'
:
// Count
return
array
(
count
(
$str
));
$result
=
array
(
count
(
$str
));
break
;
case
'c'
:
// comment
return
array
(
''
);
$result
=
array
(
''
);
break
;
case
'b'
:
// base64
if
(
isset
(
$args
[
0
])
&&
(
$args
[
0
]
==
'd'
))
{
re
turn
array
(
base64_decode
(
$str
));
$
re
sult
=
array
(
base64_decode
(
$str
));
}
return
array
(
base64_encode
(
$str
));
$result
=
array
(
base64_encode
(
$str
));
break
;
case
'u'
:
// uppercase
return
array
(
mb_strtoupper
(
$str
,
'UTF-8'
));
$result
=
array
(
mb_strtoupper
(
$str
,
'UTF-8'
));
break
;
case
'l'
:
// lowercase
return
array
(
mb_strtolower
(
$str
,
'UTF-8'
));
$result
=
array
(
mb_strtolower
(
$str
,
'UTF-8'
));
break
;
case
'a'
:
// remove accent
$str
=
htmlentities
(
$str
,
ENT_NOQUOTES
,
'UTF-8'
);
...
...
@@ -301,7 +360,8 @@ class templateHandling
// handle ligatures
$str
=
preg_replace
(
'#&([A-za-z]{2})(?:lig);#'
,
'\1'
,
$str
);
// delete unhandled characters
return
array
(
preg_replace
(
'#&[^;]+;#'
,
''
,
$str
));
$result
=
array
(
preg_replace
(
'#&[^;]+;#'
,
''
,
$str
));
break
;
case
't'
:
// translit
$localesaved
=
setlocale
(
LC_CTYPE
,
0
);
...
...
@@ -311,10 +371,12 @@ class templateHandling
$ret
[]
=
iconv
(
'UTF8'
,
'ASCII//TRANSLIT'
,
$str
);
}
setlocale
(
LC_CTYPE
,
$localesaved
);
return
array_unique
(
$ret
);
$result
=
array_unique
(
$ret
);
break
;
case
'p'
:
// spaces
return
array
(
preg_replace
(
'/\s/u'
,
''
,
$str
));
$result
=
array
(
preg_replace
(
'/\s/u'
,
''
,
$str
));
break
;
case
's'
:
// substring
if
(
count
(
$args
)
<
1
)
{
...
...
@@ -328,61 +390,21 @@ class templateHandling
for
(
$i
=
$m
[
1
];
$i
<
$m
[
2
];
++
$i
)
{
$res
[]
=
substr
(
$str
,
$args
[
0
],
$i
);
}
re
turn
array_unique
(
$res
);
$
re
sult
=
array_unique
(
$res
);
}
else
{
re
turn
array
(
substr
(
$str
,
$args
[
0
],
$args
[
1
]));
$
re
sult
=
array
(
substr
(
$str
,
$args
[
0
],
$args
[
1
]));
}
break
;
case
'r'
:
// random string
$length
=
8
;
$chars
=
'b'
;
if
(
count
(
$args
)
>=
2
)
{
$length
=
mt_rand
(
$args
[
0
],
$args
[
1
]);
if
(
count
(
$args
)
>=
3
)
{
$chars
=
$args
[
2
];
}
}
elseif
(
count
(
$args
)
>=
1
)
{
$length
=
$args
[
0
];
}
$res
=
''
;
for
(
$i
=
0
;
$i
<
$length
;
++
$i
)
{
switch
(
$chars
)
{
case
'd'
:
/* digits */
$res
.
=
(
string
)
rand
(
0
,
9
);
break
;
case
'l'
:
/* letters */
$nb
=
mt_rand
(
65
,
116
);
if
(
$nb
>
90
)
{
/* lowercase */
$nb
+=
6
;
}
$res
.
=
chr
(
$nb
);
break
;
case
'b'
:
/* both */
default
:
$nb
=
mt_rand
(
65
,
126
);
if
(
$nb
>
116
)
{
/* digit */
$nb
=
(
string
)(
$nb
-
117
);
}
else
{
if
(
$nb
>
90
)
{
/* lowercase */
$nb
+=
6
;
}
$nb
=
chr
(
$nb
);
}
$res
.
=
$nb
;
break
;
}
}
return
array
(
$res
);
$result
=
array
(
static
::
randomStringModifier
(
$args
));
break
;
default
:
trigger_error
(
"Unkown modifier '
$m
'"
);
return
array
(
$str
);
$result
=
array
(
$str
);
break
;
}
return
$result
;
}
/*! \brief Flattens dependencies (if a depends of b which depends of c then a depends of c) */
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment