Exemplo n.º 1
0
echo 'validate_range($variable_range("xyz", 0, 3): ' . (validate_range('xyz', 0, 3) ? 'true' : 'false') . '<br/>';
echo 'validate_range($variable_range("", 1, 3): ' . (validate_range('', 1, 3) ? 'true' : 'false') . '<br/>';
echo 'validate_range($variable_range("xyz", 0, 2): ' . (validate_range('xyz', 0, 2) ? 'true' : 'false') . '<br/>';
echo 'validate_range($variable_range(-1, 0, 2): ' . (validate_range(-1, 0, 2) ? 'true' : 'false') . '<br/>';
echo 'validate_range($variable_range(3, 0, 2): ' . (validate_range(3, 0, 2) ? 'true' : 'false') . '<br/>';
echo 'validate_range($variable_range("201", 0, 2): ' . (validate_range("201", 0, 200) ? 'true' : 'false') . '//numeric in string will be converted in numeric <br/>';
echo '<br/>Passed<br/><hr/>';
/* retrieves the variable name of the passed in variable as a string 
   however function does not return the correct variable is value is same. */
echo 'Signature: variable_name($variable)<br/>';
echo '//returns the variable name that was passed in';
$selected_item = 'item';
$arraylist_item = array('lalala');
echo 'variable_name($selected_item): ' . variable_name($selected_item) . '<br/>';
echo 'variable_name("test_item"): "' . variable_name("test_item") . '" >br/> //returns empty if variable is not an item<br/>';
echo 'variable_name($arraylist_item): ' . variable_name($arraylist_item) . '<br/>';
echo '<br/>Passed<br/><hr/>';
/* validates if input is a valid md5 string, checks for corresponding regex in /tools/contants/regex when defined in /tools/constants/constants $allowed_hash_types */
echo 'Signature: validate_hash($hash, $hash_type = "md5")<br/>';
echo '#checks if hash type is valid based on list in constants.php, checks if hash given matches the relevant regex<br/><br/>';
echo 'validate_hash("123asd", "md5): ' . (validate_hash('123asd', 'md5') ? 'true' : 'false') . '<br/>';
echo 'validate_hash("123asd", "lalalhash"): ' . (validate_hash('123asd', 'lalahash') ? 'true' : 'false') . '//if hash type not valid false <br/>';
echo 'validate_hash("5f4dcc3b5aa765d61d8327deb882cf99"): ' . (validate_hash('5f4dcc3b5aa765d61d8327deb882cf99') ? 'true' : 'false') . '<br/>';
echo '<br/>Passed<br/><hr/>';
/* validates the email address formatting and ensure that the domain exist */
echo 'Signature: validate_email($email)<br/><br/>';
echo 'validate_email("*****@*****.**"): ' . (validate_email("*****@*****.**") ? 'true' : 'false') . '<br/>';
echo 'validate_email("*****@*****.**"): ' . (validate_email("*****@*****.**") ? 'true' : 'false') . '<br/>';
echo 'validate_email("*****@*****.**"): ' . (validate_email("*****@*****.**") ? 'true' : 'false') . '<br/>';
echo 'validate_email("*****@*****.**"): ' . (validate_email("*****@*****.**") ? 'true' : 'false') . '<br/>';
echo 'validate_email("googogo@123.123"): ' . (validate_email("googogo@123.123") ? 'true' : 'false') . ' //must be valid tld<br/>';
Exemplo n.º 2
0
function enforce_inputs()
{
    $function = array('class_name' => __NAMESPACE__, 'method_name' => __METHOD__);
    $message = '';
    $variable = '';
    $line = '';
    //retrieve last item as return type if available
    $array = func_get_args();
    $return_type = end($array);
    if (is_string($return_type) == false) {
        $return_type = Constants::get('default_return_type');
    } else {
        $allowed_return_types = Constants::get('allowed_return_types');
        if (array_contains($return_type, $allowed_return_types) == false) {
            $return_type = Constants::get('default_return_type');
        }
    }
    //loop through every item to validate, sets a message to throw the error at the end of the method
    foreach ($array as $list) {
        if (is_ready($list)) {
            //every list must be ready
            if (is_array($list)) {
                //ensure that item is an array
                if (count($list) == 5) {
                    //every list contains the required items
                    $variable = set_default($list[0], null);
                    $type = set_default($list[1], ':');
                    $min = set_default($list[2], null);
                    $max = set_default($list[3], null);
                    $nullable = set_default($list[4], false);
                    $validation_list = null;
                    if (isset($variable)) {
                        //ensure that variable is set
                        //ensures that valid variable list type is request
                        if (list_contains($type, Constants::get('variable_list')) == false) {
                            $message = 'Invalid variable validation requirement - type (' . $type . ') unrecognized.';
                            $line = __LINE__;
                        }
                        //if either one is not a numeric, check for array
                        if (validate_type($min, 'numeric') == false || validate_type($max, 'numeric') == false) {
                            if (validate_type($min, 'array') == true || validate_type($max, 'array') == true) {
                                $validation_list = (is_null($min) == true || isset($min) == false) && validate_type($max, 'array') ? $max : $min;
                                if (validate_type($validation_list, 'array') == false) {
                                    $message = 'Invalid variable validation requirement - an array to validate is required.';
                                    $line = __LINE__;
                                }
                            } else {
                                if (is_null($min) == false || is_null($max) == false) {
                                    $message = 'Invalid variable validation requirement - min/max must be numeric or an array of list in either one or both nulls.';
                                    $line = __LINE__;
                                }
                            }
                            //ensure than max is less than min
                        } else {
                            if ($max < $min) {
                                $message = 'Invalid variable validation requirement - min is more than max.';
                                $line = __LINE__;
                            }
                        }
                        //ensure that nullable is boolean
                        if (validate_type($nullable, 'bool') == false) {
                            $message = 'Invalid variable validation requirement - nullable must be boolean.';
                            $line = __LINE__;
                        }
                        //verify variable if not null
                        if (!is_null($variable)) {
                            if (validate_type($variable, $type) == false) {
                                $message = 'Variable is not a ' . $type . '.';
                                $line = __LINE__;
                            }
                            if (is_null($validation_list) == false && isset($validation_list) == true) {
                                if (array_contains($variable, $validation_list) == false) {
                                    $message = 'Variable is not found in the list provided.';
                                    $line = __LINE__;
                                }
                            } else {
                                if (validate_type($variable, 'string:numeric') == true) {
                                    if (is_null($min) == false || is_null($max) == false) {
                                        if (validate_range($variable, $min, $max) == false) {
                                            $message = 'Variable does not meet the min/max requirement.';
                                            $line = __LINE__;
                                        }
                                    }
                                }
                            }
                        }
                    } else {
                        if ($nullable == false) {
                            $message = 'Variable is not set, unable to validate variable';
                            $line = __LINE__;
                        }
                    }
                } else {
                    $message = 'Incomplete variable validation list. [variable, type, min, max, nullable*]';
                    $line = __LINE__;
                }
            }
        } else {
            $message = 'Invalid variable validation list, an array is required. [variable, type, min, max, nullable*]';
            $line = __LINE__;
        }
    }
    if (compare_string($message, '') == false) {
        $variable_name = variable_name($variable);
        if (isset($variable_name) == true && $variable_name != '') {
            $variable_name = '$' . $variable_name;
        } else {
            $variable_name = $type != 'password' ? $variable : '*password*';
        }
        if ($variable_name != '') {
            $variable_name = ' [' . $variable_name . ']';
        }
        $error = Tool::prepare($message . $variable_name, '', $line, $return_type, Constants::get('default_error_code'));
        Tool::error($function, $error, false);
    }
}