Exemplo n.º 1
0
 public static function generate_statement($statement_type, $list, $list_type, $tables, $conditions, $return_type = 'json')
 {
     //sets list to select all if not provided
     $list = set_default($list, '*');
     $conditions = set_default($conditions, '');
     //retrieve limits for shorthand
     $zero_minimium = Limits::get('zero_minimium');
     $text_limit = Limits::get('text_limit');
     //validate statement type
     $statement_types = Constants::get('statement_types');
     enforce_inputs(array($statement_type, 'string', $statement_types, null, false), array($list, 'array', null, null, true), array($list_type, 'string', Constants::get('statement_list_types'), null, false), array($tables, 'string', $text_limit['min'], $text_limit['max'], false), array($conditions, 'string', $zero_minimium, $text_limit['max'], true), $return_type);
     //conditions are optional
     $statement = '';
     if (compare_string($statement, 'SELECT', false)) {
         $statement = format_select($list, $list_type, $tables, $conditions);
     } else {
         if (compare_string($statement, 'INSERT', false)) {
             $statement = format_insert($list, $list_type, $tables, $conditions);
         } else {
             if (compare_string($statement, 'UPDATE', false)) {
                 $statement = format_update($list, $tables, $list_type, $conditions);
             } else {
                 if (compare_string($statement, 'DELETE', false)) {
                     $statement = format_delete($list, $tables, $list_type, $conditions);
                 }
             }
         }
     }
 }
Exemplo n.º 2
0
 public static function write($http_status_code, $data, $parent_tag, $return_type = 'json')
 {
     $function = array('class_name' => __NAMESPACE__, 'method_name' => __METHOD__);
     //setting default values to the input parameters
     $http_status_code = set_default($http_status_code, 200);
     $data = set_default($data, array());
     $parent_tag = set_default($parent_tag, 'datas');
     $return_type = set_default($return_type, Constants::get('default_return_type'));
     //retrieve required limits from tools/constants/limits
     $text_limit = Limits::get('text_limit');
     //ensuring that inputs are validated against the array list of requirements $enforcement contains the results of the validation and message of the error if any.
     $enforcement = enforce_inputs(array($data, 'string:array', null, null, false), array($parent_tag, 'string', $text_limit['min'], $text_limit['max'], false), array($return_type, 'string', Constants::get('allowed_return_types'), null, false));
     $http_status_codes = Constants::get('http_status_codes');
     if (isset($http_status_codes[$http_status_code])) {
         //validates if the $http_status_code provided is in the list
         //check if data type is an array
         if (!is_array($data)) {
             $error = Tool::prepare('Data provided must be an array.', '', __LINE__, $return_type, Constants::get('default_error_code'));
             Tool::error($function, $error, false);
         }
         //formats the data return with the appropriate http status code.
         header('HTTP/1.0 ' . $http_status_code . ' ' . $http_status_codes[$http_status_code], true, $http_status_code);
         if (compare_string($return_type, Constants::get('xml'))) {
             //checks if the user requested data to be returned in xml (by default json)
             header('Content-type: text/xml');
             echo '<' . $parent_tag . '>';
             foreach ($data as $index => $post) {
                 if (is_array($post)) {
                     foreach ($post as $key => $value) {
                         echo '<', $key, '>';
                         if (is_array($value)) {
                             foreach ($value as $tag => $val) {
                                 echo '<', $tag, '>', htmlentities($val), '</', $tag, '>';
                             }
                         }
                         echo '</', $key, '>';
                     }
                 } else {
                     echo '<' . $index . '>' . $post . '</' . $index . '>';
                 }
             }
             echo '</' . $parent_tag . '>';
         } else {
             //header('Content-type: application/json; charset=UTF-8');
             echo json_encode(array($parent_tag => $data));
         }
     } else {
         Writer::write(400, 'Invalid HTTP status code.', Constants::get('error_tag'), $return_type);
         return;
     }
 }
Exemplo n.º 3
0
echo '<br/>Passed<br/><hr/>';
/* Rewrite method of in_array to check if the array contains the item. However, it sanitizes the input before checking */
echo 'Signature: array_contains($item, $allowed_array, $case_sensitive = false)<br/>';
echo '//check if items is in array, but sanitize all input before checking, default not case sensitive<br/><br/>';
echo 'array_contains("x ", array("x", "y", "z")): ' . (array_contains("x ", array("x", "y", "z")) ? 'true' : 'false') . '<br/>';
echo 'array_contains("x ", array("x ", "y", "z")): ' . (array_contains("x ", array("x", "y", "z")) ? 'true' : 'false') . ' - both inputs and list are sanitized<br/>';
echo 'array_contains("X ", array("x ", "y", "z")): ' . (array_contains("X ", array("x", "y", "z")) ? 'true' : 'false') . '<br/>';
echo 'array_contains("X ", array("x ", "y", "z"), true): ' . (array_contains("X ", array("x", "y", "z"), true) ? 'true' : 'false') . '<br/>';
echo '<h3>String Manipulation</h3>';
/* rewrite method of strcmp which sanitizes and trim inputs, including case sensitive comparision */
echo '<hr/>Signature: compare_string($input_one = "", $input_two = "", $case_sensitive = false)<br/>';
echo '//default comparision is not case sensitive, passing variables in works accordingly.<br/><br/>';
echo 'compare_string("", ""): ' . (compare_string('', '') ? 'true' : 'false') . '<br/>';
echo 'compare_string("xyz", "xyz"): ' . (compare_string('xyz', 'xyz') ? 'true' : 'false') . '<br/>';
echo 'compare_string("xyz", "XYZ"): ' . (compare_string('xyz', 'XYZ') ? 'true' : 'false') . '<br/>';
echo 'compare_string("xyz", "XYZ", true): ' . (compare_string('xyz', 'XYZ', true) ? 'true' : 'false') . '<br/>';
echo '<br/>Passed<br/><hr/>';
/* takes in a string list of items separated by the ':', it matches it against an array to check if the item exist in the array
   function is similar to array_contains/in_array however it allows multiple items to be validated at the same time
   @$greedy is a boolean value, when set to true, all items set to validate against the list must match
   if @$greedy is set to false, as long as any item matches, true is returned
*/
echo 'Signature: list_contains($items, $list, $greedy = true)<br/>';
echo '//greedy is used to compare two list, if true, all items in both list must match, if false, as long one match it is true <br/><br/>';
echo 'list_contains("xyz", array("abc", "def", "ghi")): ' . (list_contains("xyz", array("abc", "def", "ghi")) ? 'true' : 'false') . '<br/>';
echo 'list_contains("xyz", array("abc", "def", "xyz")): ' . (list_contains("xyz", array("abc", "def", "xyz")) ? 'true' : 'false') . '<br/>';
echo 'list_contains("xyz", array("abc", "def", "xyz"), true): ' . (list_contains("xyz", array("abc", "def", "xyz"), true) ? 'true' : 'false') . ' - no effect <br/>';
echo 'list_contains("xyz", array("abc", "def", "xyz"), false): ' . (list_contains("xyz", array("abc", "def", "xyz"), false) ? 'true' : 'false') . ' - no effect <br/>';
echo 'list_contains("abc:def:khl", array("abc", "def", "xyz"), true): ' . (list_contains("abc:def:khl", array("abc", "def", "xyz"), true) ? 'true' : 'false') . '<br/>';
echo 'list_contains("abc:def:khl", array("abc", "def", "xyz"), false): ' . (list_contains("abc:def:khl", array("abc", "def", "xyz"), false) ? 'true' : 'false') . '<br/>';
echo '<br/>Passed<br/><hr/>';
Exemplo n.º 4
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);
    }
}
Exemplo n.º 5
0
                $cind += 2;
            } else {
                array_push($arr_cont, substr($tempaddtext, $cind, 3));
                $cind += 3;
            }
        }
    }
    return $arr_cont;
}
/**
 * 计算编辑距离占比 
 * 编辑距离/最大字符串长度
 * @param string 对比字符串1
 * @param string 对比字符串2
 * @return float 
 */
function compare_string($strA, $strB)
{
    $lenA = mb_strlen($strA, 'UTF-8');
    $lenB = mb_strlen($strB, 'UTF-8');
    $len = max($lenA, $lenB);
    $similar = levenshtein_cn($strA, $strB, 2);
    return $similar / $len;
}
/*=========================结束最小编辑距离============================*/
// is_str_equal($strA, $FileName);//字符串是否相等
$strA = 'php是最好的编程语言';
$strB = 'PHP不是最好的编程语言';
$source = compare_string($strA, $strB);
print_r($source);
//一般数值 < 0.3 则判定为字符串一致,可规定对比双方字符串长度再使用