127 lines
3.3 KiB
PHP
127 lines
3.3 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
namespace Hura8\System\Security;
|
||
|
|
|
||
|
|
|
||
|
|
class DataClean
|
||
|
|
{
|
||
|
|
|
||
|
|
/**
|
||
|
|
* @description limit max length. limitLength("toi ten nguyen", 10) => "toi ten ng"
|
||
|
|
* @param string $text
|
||
|
|
* @param int $max_length
|
||
|
|
* @return string
|
||
|
|
*/
|
||
|
|
public static function limitLength(string $text, int $max_length = 100 ) : string
|
||
|
|
{
|
||
|
|
if(strlen($text) <= $max_length) {
|
||
|
|
return $text;
|
||
|
|
}
|
||
|
|
|
||
|
|
return substr($text, 0, $max_length-1);
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
/**
|
||
|
|
* @description limit max length but keep full words. limitLengthFullWords("toi ten nguyen", 10) => "toi ten"
|
||
|
|
* @param string $text
|
||
|
|
* @param int $max_length
|
||
|
|
* @return string
|
||
|
|
*/
|
||
|
|
public static function limitLengthFullWords(string $text, int $max_length = 100 ) : string
|
||
|
|
{
|
||
|
|
|
||
|
|
if(strlen($text) <= $max_length) {
|
||
|
|
return $text;
|
||
|
|
}
|
||
|
|
|
||
|
|
$char_at_max_length = $text[$max_length-1];
|
||
|
|
if($char_at_max_length == ' ') {
|
||
|
|
return substr($text, 0, $max_length-1);
|
||
|
|
}
|
||
|
|
|
||
|
|
//else fall back to the last space
|
||
|
|
$words = explode(" ", substr($text, 0, $max_length-1));
|
||
|
|
array_pop($words);
|
||
|
|
|
||
|
|
return join(" ", $words);
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
/**
|
||
|
|
* @param $input_list array
|
||
|
|
* @param $type string
|
||
|
|
* @return array
|
||
|
|
*/
|
||
|
|
public static function makeListOfInputSafe(array $input_list, $type){
|
||
|
|
if(!sizeof($input_list)) return [];
|
||
|
|
|
||
|
|
$result = [];
|
||
|
|
foreach ($input_list as $key => $str) {
|
||
|
|
$result[$key] = self::makeInputSafe($str, $type);
|
||
|
|
}
|
||
|
|
|
||
|
|
return $result;
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
/**
|
||
|
|
* @param $input string data to process
|
||
|
|
* @param $type string, type of data to validate against, enum : int|double|email|id|string|date|plain
|
||
|
|
* @return mixed
|
||
|
|
*/
|
||
|
|
public static function makeInputSafe($input, $type){
|
||
|
|
|
||
|
|
if(is_array($input)) {
|
||
|
|
return $input;
|
||
|
|
}
|
||
|
|
|
||
|
|
if ( $type == DataType::ID ) {
|
||
|
|
//is $input the database item_id ?
|
||
|
|
return preg_replace('/[^a-z0-9_\-\.]/i', '', $input);
|
||
|
|
}
|
||
|
|
|
||
|
|
if ( $type == DataType::EMAIL ) {
|
||
|
|
return filter_var($input, FILTER_VALIDATE_EMAIL) ? $input : '';
|
||
|
|
}
|
||
|
|
|
||
|
|
if ( $type == DataType::INTEGER ) {
|
||
|
|
// support negative number
|
||
|
|
return (int) preg_replace('/[^0-9\-]/', '', $input);
|
||
|
|
}
|
||
|
|
|
||
|
|
if ( $type == DataType::DOUBLE ) {
|
||
|
|
// support negative number
|
||
|
|
$input = preg_replace('/[^0-9,\-]/', '', $input);
|
||
|
|
//convert vietnamese style , to . for percentage
|
||
|
|
$input = str_replace(",", ".", $input);
|
||
|
|
|
||
|
|
return (double) $input;
|
||
|
|
}
|
||
|
|
|
||
|
|
if ( $type == DataType::DATE ) {
|
||
|
|
// support pattern:
|
||
|
|
// date = d-m-Y
|
||
|
|
// datetime = d-m-Y H:i:a
|
||
|
|
$pattern = "/([0-9]{2})-([0-9]{2})-([0-9]{2,4})(\s)?([0-9]{1,2}:[0-9]{1,2}(:[0-9]{1,2})?)?/i";
|
||
|
|
return preg_replace($pattern, '', $input);
|
||
|
|
}
|
||
|
|
|
||
|
|
if ( $type == DataType::PLAIN_TEXT || $type == DataType::STRING ) {
|
||
|
|
return strip_tags($input);
|
||
|
|
}
|
||
|
|
|
||
|
|
if ( $type == DataType::NON_VIETNAMESE ) {
|
||
|
|
return preg_replace('/[^a-z0-9_\s\-]/i', '', $input);
|
||
|
|
}
|
||
|
|
|
||
|
|
if ( $type == DataType::RICH_TEXT ) {
|
||
|
|
return $input;
|
||
|
|
}
|
||
|
|
|
||
|
|
return null;
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
}
|