public function __construct($request_path, $request_params, $request_method, $return_type) { $function = array('class_name' => __NAMESPACE__, 'method_name' => __METHOD__); //validating the return type and set default if not found if (array_contains($return_type, Constants::get('allowed_return_types') == false)) { $return_type = Constants::get('default_return_type'); } else { $this->return_type = set_default($return_type, Constants::get('allowed_return_types')); } //validate if request path is valid else throw error if (is_ready($request_path) == false) { $error = Tool::prepare('Request path is invalid, unable to process routing request.', 'Request path is null, verify that index router has parsed the information correctly.', __LINE__, $this->return_type, Constants::get('default_error_code')); Tool::error($function, $error, false); } else { enforce_inputs(array($request_path, 'array', null, null, false), $this->return_type); $this->original_path = $request_path; $this->request_path = $request_path; } //validate if request method is valid, else set as default (post takes precendence if both are used) $this->request_method = set_default($request_method, Constants::get('default_http_method')); $allowed_http_methods = Constants::get('allowed_http_methods'); if (array_contains($request_method, $allowed_http_methods, false) == false) { $request_method = Constants::get('default_http_method'); } $this->request_method = strtolower($request_method); //add post params to class if exist $this->request_params = $request_params; //normalise request_path if (is_ready(end($this->request_path)) == false) { array_pop($this->request_path); } }
public function __construct($route) { $function = array('class_name' => __NAMESPACE__, 'method_name' => __METHOD__); //validate if route is valid else throw error if (is_ready($route)) { $this->route = $route; } else { //route is invalid, throw error $error = Tool::prepare('Route is invalid, unable to process routing request.', 'Route is null, verify that index router has parsed the information correctly.', __LINE__, $this->route->get_return_type(), Constants::get('default_error_code')); Tool::error($function, $error, false); } }
public function __construct($route) { $function = array('class_name' => __NAMESPACE__, 'method_name' => __METHOD__); //ensure that route is valid else send error if (is_ready($route)) { $this->route = $route; } else { $error = Tool::prepare('Route is invalid, unable to process routing request.', 'Route is null, verify that index router has parsed the information correctly.', __LINE__, $this->route->get_return_type(), Constants::get('default_error_code')); Tool::error($function, $error, false); } try { $this->connection = new PDO('mysql:host=' . DatabaseConfig::get('host') . ';dbname=' . DatabaseConfig::get('database'), DatabaseConfig::get('username'), DatabaseConfig::get('password')); $exception_mode = Config::get('enable_debugging') == true ? PDO::ERRMODE_EXCEPTION : PDO::ERRMODE_SILENT; $this->connection->setAttribute(PDO::ATTR_ERRMODE, $exception_mode); } catch (\PDOException $exception) { $error = Tool::prepare('Unable to connect to database.', $exception->getMessage(), __LINE__, $this->route->get_return_type(), Constants::get('default_error_code')); Tool::error($function, $error, false); } }
private function handle_include($current_location) { $controller = end($current_location); if (is_ready($controller) == false) { return; } //form the string to parse location $current_location = implode('/', $this->route->get_current_location()); $function = array('class_name' => __NAMESPACE__, 'method_name' => __METHOD__); //prepare location for directory and file from current location $include_path = getcwd() . Constants::get('controller_location') . '/' . $current_location; $directory_location = $include_path . '/index.php'; //for directory, redirect to index router (controller) $file_location = $include_path . '.php'; //for file, check if action exists (action) //check if include_path is a directory location, send to index router if (is_dir($include_path)) { //check if router is attatched to controller if indicated if (file_exists($directory_location)) { include_once $directory_location; //send the router the corresponding route $class_name = Constants::get('controller_reference') . '\\' . $controller; $router = new $class_name($this->route); } else { $error = Tool::prepare('Router is not installed. (controller/index.php) - ' . $directory_location, 'Ensure the .index.php router is provided for the controller indicated.', __LINE__, $this->route->get_return_type(), Constants::get('default_error_code')); Tool::error($function, $error, false); } //check if file location is a file, send to action } else { if (file_exists($file_location)) { include_once $file_location; //send the router the corresponding route $class_name = Constants::get('controller_reference') . '\\' . $controller; $router = new $class_name($this->route); //add variables back into the list if directory matches } else { $this->route->push_request_path(); } } }
$script = 'tools/validation/validator.php'; echo 'Current Test Script: ' . $script . '<br/>'; /***** BEGIN FILE TESTING HERE *****/ include_once $script; echo '<h3>General Variable Manipulation</h3>'; /* function is_ready($variable = null) * takes in an input by reference and check if it isset, is_null, or is an empty string * if variable isset, not null and is not an empty string, returns true, otherwise returns false */ echo '<hr/>Signature: is_ready($variable = null)<br/>'; echo '//variable must not be null and empty<br/><br/>'; echo 'is_ready("x"): ' . (is_ready('x') ? 'true' : 'false') . '<br/>'; echo 'is_ready(null): ' . (is_ready(null) ? 'true' : 'false') . '<br/>'; echo 'is_ready(""): ' . (is_ready('') ? 'true' : 'false') . '<br/>'; echo 'is_ready(" "): ' . (is_ready(" ") ? 'true' : 'false') . '<br/>'; echo '<br/>Passed<br/><hr/>'; /* function sanitize_input($string_input) * takes in the user input, returns the trimmed and escaped user input. * @$string_input will process strings or an array of strings * original value of the variable will be returned if it is unable to trim or/and escape the input. */ echo 'Signature: sanitize_input($string_input)<br/>'; echo '//removes all trailing spaces for individual input and array input<br/><br/>'; echo 'sanitize_input("x"): "' . sanitize_input('x') . '"<br/>'; echo 'sanitize_input("x "): "' . sanitize_input('x ') . '"<br/>'; echo 'sanitize_input(array()):'; print_r(sanitize_input(array())); echo '<br/>'; echo 'sanitize_input(array("sample one", "sample two ", "three ")): --result:'; print_r(sanitize_input(array("sample one", "sample two ", "three ")));
function variable_name($variable) { $results = 'undefined'; if (is_ready($variable) == true) { foreach ($GLOBALS as $var_name => $value) { if ($value === $variable) { $results = $var_name; } } } return $results; }
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); } }
require_once 'tools/constants/constants.php'; require_once 'tools/router/router.php'; require_once 'tools/router/route.php'; require_once 'tools/validation/validator.php'; use tools\constants\Tool; use tools\constants\Constants; use tools\router\Router; use tools\router\Route; /** index.php * default interception page for all request made to the web service * handles all redirection according to the link request */ //verify for maintenance //verify for interception $request = $_SERVER['QUERY_STRING']; if (is_ready($request)) { //normalize the request (remove path= and variables after &) $request = str_replace('path=', '', $request); //extract the required path only $count = strpos($request, '&') == false ? strlen($request) : strpos($request, '&'); $request = substr($request, 0, $count); $request_path = explode("/", $request); //extract return type, check if return type is in the allowed list $return_type = end($request_path); $return_type = set_default($return_type, Constants::get('default_return_type')); $return_type = strrchr($return_type, "."); $return_type = substr($return_type, 1); $allowed_return_types = Constants::get('allowed_return_types'); if (array_contains($return_type, $allowed_return_types, false) == false) { $return_type = Constants::get('default_return_type'); } else {
public function delete_accessor($param, $post_params) { //define tags for parent tags and child tag $parent_tag = 'results'; $child_tag = 'result'; //handle authentication //initalizing and set default for params $statement = ''; $param[0] = set_default($param[0], ''); //handle $param parsing if (is_ready($param[0]) == false) { //delete all records Writer::write(501, 'Method not implemented.', Constants::get('error_tag'), parent::get_route()->get_return_type()); } else { if (count($param) == 1) { if (is_numeric($param[0])) { //delete record based on the id $statement = 'DELETE FROM accessor WHERE _id = ' . $param[0]; } else { if (is_string($param[0])) { //delete record based on the search term on all columns } } } else { if (count($param) == 2) { //delete record based on the search term on a row column } } } }