Example #1
0
 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);
     }
 }
Example #2
0
 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);
     }
 }
Example #3
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;
     }
 }
Example #4
0
 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();
         }
     }
 }
Example #5
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);
    }
}
Example #6
0
<?php

$script = 'tools/object/tool.php';
echo 'Current Test Script: ' . $script . '<br/>';
/***** BEGIN FILE TESTING HERE *****/
include_once $script;
echo '<h3>Tool</h3>';
include_once $script;
use tools\object\Tool;
/* prepares and prints the error, writes to log if indicated 
 * requires two array of data to be passed in $info(class_name, method_name, return_type) - return_type default at json
 * $error(message, details, line, error_code) - error_code default at 400
 * $loggable default at false (if true, writes to log)
 */
echo '<hr/>Signature: public static function error($info, $error, $loggable = false) <br/>';
echo '// $info(class_name, method_name, return_type) <br/>';
echo '// $error(message, details, line, error_code) - error code default at 400. <br/><br/>';
echo 'Data input: <br/>';
echo '$info("class_name"=> __NAMESPACE__, "$method_name"=>__METHOD__, "$return_type"=>"json")<br/>';
echo '$error("message"=>"message","details"=>"details", "error_code"=>404) <br/>';
echo 'Tool::error($info, $error, false): ';
$info = array('class_name' => __NAMESPACE__, 'method_name' => __METHOD__, 'return_type' => 'json');
$error = array('message' => 'message', 'details' => 'details', 'error_code' => 404);
Tool::error($info, $error, false);
?>