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); } } } } }
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 execute($bindings = null, $write_statement = false) { $function = array('class_name' => __NAMESPACE__, 'method_name' => __METHOD__); try { $this->statement->execute($bindings); if ($write_statement == false) { return $this->statement->fetchAll(PDO::FETCH_ASSOC); } } catch (PDOException $exception) { $error = Tool::prepare('Unable to execute statement.', $exception->getMessage(), __LINE__, $this->route->get_return_type(), Constants::get('default_error_code')); Tool::error($function, $error, false); } }
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; } }
public static function error($info, $error, $loggable = false) { //preset $info('class_name', 'method_name', 'return_type') $class_name = set_default($info['class_name'], ''); $method_name = set_default($info['method_name'], ''); $return_type = set_default($error['return_type'], Constants::get('default_return_type')); //preset $error('message', 'details', 'line', 'error_code') $message = set_default($error['message'], 'Unknown Error Occurred.'); $details = set_default($error['details'], ''); $line = set_default($error['line'], ''); $http_status_code = set_default($error['http_status_code'], Constants::get('default_error_code')); $error['function'] = str_replace($class_name . '/', '', $method_name); Writer::write($http_status_code, $error, Constants::get('error_tag'), $return_type); $debuggable = Config::get('enable_debugging'); if ($debuggable == true) { } if ($loggable == true) { } //write to log exit; }
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(); } } }
function validate_hash($hash, $hash_type = 'md5') { $results = false; if (array_contains($hash_type, Constants::get('allowed_hash_types'), false)) { $results = preg_match(Regex::get('hash_' . strtolower($hash_type) . '_regex'), $hash); } else { $results = false; } return $results; }
function enforce_https($return_type = 'json') { $enable_https = Config::get('enable_https'); if ($enable_https == true) { if (validate_https() == false) { Writer::write(412, 'Request is not sent securely with https.', Constants::get('error_tag'), $return_type); } else { return true; } } }
$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 { //remove the return type from the request_path $item = end($request_path); $item = substr($item, 0, strlen($item) - (strlen($return_type) + 1)); $request_path[count($request_path) - 1] = $item; } //extract request_method, can be defined in both get and post reauest, post takes precedence $request_method = set_default($_REQUEST['request_method'], $_SERVER['REQUEST_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'); } $request_params = $_REQUEST; //form route and send through router $route = new Route($request_path, $request_params, $request_method, $return_type); $router = new Router($route); $router->route(); //TODO: Can handle redirection to api introduction if no specific request is made } 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 } } } }