From 37b7790412f425fa16f378f8457624066f98f539 Mon Sep 17 00:00:00 2001 From: dconco Date: Thu, 4 Jan 2024 00:16:01 +0100 Subject: [PATCH 1/3] Added .editorconfig --- .editorconfig | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 .editorconfig diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..a556955 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,13 @@ +# EditorConfig is awesome: https://EditorConfig.org + +# top-most EditorConfig file +root = true + +[*] +indent_style = tab +indent_size = 3 +end_of_line = crlf +charset = utf-8 +trim_trailing_whitespace = false +insert_final_newline =true +insert_final_newline = false \ No newline at end of file From b77f6ac14ef038b3c583ee204c5b04bbadbfad66 Mon Sep 17 00:00:00 2001 From: dconco Date: Sat, 2 Mar 2024 11:53:23 +0100 Subject: [PATCH 2/3] updated --- App/Controller/ApiController.php | 671 ++++++------ App/PhpSlides.php | 1747 ++++++++++++++++-------------- 2 files changed, 1264 insertions(+), 1154 deletions(-) diff --git a/App/Controller/ApiController.php b/App/Controller/ApiController.php index 076bb19..06350c5 100644 --- a/App/Controller/ApiController.php +++ b/App/Controller/ApiController.php @@ -9,7 +9,6 @@ use PhpSlides\Controller\Controller; - /** * --------------------------------------------------------------- * @@ -22,322 +21,356 @@ final class Api extends Controller { - /** - * ------------------------------------------------------------------------ - * - * | ANY REQUEST FROM API ENDPOINT - * - * | Accept all type of request or any other method - * - * | Cannot evaluate `{?} URL parameters` in api route if it's an array - * | - * - * @param array|string $route This describes the URL string to check if it matches the request URL, use array of URLs for multiple request - * @param mixed $class_method Can contain any types of data to return to the client side. - * - * ------------------------------------------------------------------------ - */ - final public static function any(array|string $route, int $class_method, string $method = "*") - { - try - { - $dir = Route::$root_dir; - - // will store all the parameters value in this array - $req = []; - $req_value = []; - - // will store all the parameters names in this array - $paramKey = []; - - - // Gets all constant int values wrapped in an array - ob_start(); - $bin_const_types = include $dir . "/App/bin/const_types.php"; - ob_end_clean(); - - $bin_const_types = unserialize(hex2bin(($bin_const_types))); - $const_types_method = $bin_const_types[$class_method][1]; - $const_types_class = $bin_const_types[$class_method][0]; - - // finding if there is any {?} parameter in $route - if (is_string($route)) - { - preg_match_all("/(?<={).+?(?=})/", $route, $paramMatches); - } - - // if the route does not contain any param call routing(); - if (empty($paramMatches[0]) || is_array($route)) - { - - /** - * ------------------------------------------------------ - * | Check if $class_method is a callable function - * | or array of controller, and if not, - * | it's a string of text or html document - * ------------------------------------------------------ - */ - - $class_method = self::routing($route, $class_method, $method); - - if ($class_method) - { - if ( - (preg_match("/(Controller)/", $const_types_class, $matches) && count($matches) > 1) && - (preg_match("/(Api)/", $const_types_class, $matches) && count($matches) > 0) - ) - { - http_response_code(200); - header("Content-Type: application/json"); - - print_r(self::controller($const_types_class, $const_types_method)); - } - else - { - throw new Exception("Invalid api controller class or not existing: " . $const_types_class); - } - - self::log(); - exit(); - } - else - { - return; - } - } - - // setting parameters names - foreach ($paramMatches[0] as $key) - { - $paramKey[] = $key; - } - - /** - * ---------------------------------------------- - * | Replacing first and last forward slashes - * | $_REQUEST['uri'] will be empty if req uri is / - * ---------------------------------------------- - */ - - if (!empty(Route::$request_uri)) - { - $route = preg_replace("/(^\/)|(\/$)/", "", $route); - $reqUri = preg_replace("/(^\/)|(\/$)/", "", Route::$request_uri); - } - else - { - $reqUri = "/"; - } - - // exploding route address - $uri = explode("/", $route); - - // will store index number where {?} parameter is required in the $route - $indexNum = []; - - // storing index number, where {?} parameter is required with the help of regex - foreach ($uri as $index => $param) - { - if (preg_match("/{.*}/", $param)) - { - $indexNum[] = $index; - } - } - - /** - * ---------------------------------------------------------------------------------- - * | Exploding request uri string to array to get the exact index number value of parameter from $_REQUEST['uri'] - * ---------------------------------------------------------------------------------- - */ - $reqUri = explode("/", $reqUri); - - /** - * ---------------------------------------------------------------------------------- - * | Running for each loop to set the exact index number with reg expression this will help in matching route - * ---------------------------------------------------------------------------------- - */ - foreach ($indexNum as $key => $index) - { - - /** - * -------------------------------------------------------------------------------- - * | In case if req uri with param index is empty then return because URL is not valid for this route - * -------------------------------------------------------------------------------- - */ - - if (empty($reqUri[$index])) - { - return; - } - - // setting params with params names - $req[$paramKey[$key]] = htmlspecialchars($reqUri[$index]); - $req_value[] = htmlspecialchars($reqUri[$index]); - - // this is to create a regex for comparing route address - $reqUri[$index] = "{.*}"; - } - - // converting array to string - $reqUri = implode("/", $reqUri); - - /** - * ----------------------------------- - * | replace all / with \/ for reg expression - * | regex to match route is ready! - * ----------------------------------- - */ - $reqUri = str_replace("/", "\\/", $reqUri); - - // now matching route with regex - if (preg_match("/$reqUri/", $route)) - { - // checks if the requested method is of the given route - if ( - strtoupper($_SERVER["REQUEST_METHOD"]) !== strtoupper($method) && - $method !== "*" - ) - { - http_response_code(405); - self::log(); - exit("Method Not Allowed"); - } - - if ( - (preg_match("/(Controller)/", $const_types_class, $matches) && count($matches) > 1) && - (preg_match("/(Api)/", $const_types_class, $matches) && count($matches) > 0) - ) - { - http_response_code(200); - header("Content-Type: application/json"); - - print_r( - self::controller($const_types_class, $const_types_method, [ - ...$req_value, - ]), - ); - } - else - { - throw new Exception("Invalid api controller class or not existing: " . $const_types_class); - } - - self::log(); - exit(); - } - } - catch ( Exception $e ) - { - http_response_code(500); - print_r($e->getMessage()); - exit(); - } - } - - - /** - * -------------------------------------------------------------- - * - * | GET API ROUTE METHOD - * - * | Cannot evaluate {?} URL parameters in API route if it's an array - * - * -------------------------------------------------------------- - */ - public static function get( - array|string $route, - int $class_method, - ) { - self::any($route, $class_method, "GET"); - } - - - - /** - * -------------------------------------------------------------- - * - * | POST API ROUTE METHOD - * - * | Cannot evaluate {?} URL parameters in API route if it's an array - * - * -------------------------------------------------------------- - */ - public static function post( - array|string $route, - int $class_method, - ) { - self::any($route, $class_method, "POST"); - } - - - - /** - * -------------------------------------------------------------- - * - * | PUT API ROUTE METHOD - * - * | Cannot evaluate {?} URL parameters in API route if it's an array - * - * -------------------------------------------------------------- - */ - public static function put( - array|string $route, - int $class_method, - ) { - self::any($route, $class_method, "PUT"); - } - - - - /** - * -------------------------------------------------------------- - * - * | UPDATE API ROUTE METHOD - * - * | Cannot evaluate {?} URL parameters in API route if it's an array - * - * -------------------------------------------------------------- - */ - public static function update( - array|string $route, - int $class_method, - ) { - self::any($route, $class_method, "UPDATE"); - } - - - - /** - * -------------------------------------------------------------- - * - * | PATCH API ROUTE METHOD - * - * | Cannot evaluate {?} URL parameters in API route if it's an array - * - * -------------------------------------------------------------- - */ - public static function patch( - array|string $route, - int $class_method, - ) { - self::any($route, $class_method, "PATCH"); - } - - - - /** - * -------------------------------------------------------------- - * - * | DELETE API ROUTE METHOD - * - * | Cannot evaluate {?} URL parameters in API route if it's an array - * - * -------------------------------------------------------------- - */ - public static function delete( - array|string $route, - int $class_method, - ) { - self::any($route, $class_method, "DELETE"); - } + /** + * ------------------------------------------------------------------------ + * + * | ANY REQUEST FROM API ENDPOINT + * + * | Accept all type of request or any other method + * + * | Cannot evaluate `{?} URL parameters` in api route if it's an array + * | + * + * @param array|string $route This describes the URL string to check if it matches the request URL, use array of URLs for multiple request + * @param mixed $class_method Can contain any types of data to return to the client side. + * + * ------------------------------------------------------------------------ + */ + final public static function any(array|string $route, int $class_method, string $method = "*") + { + try + { + /** + * Retrieves the charset from the configuration file. + * + * The above code is retrieving the value of the 'charset' key from the 'config_file' array. + * The 'config_file' array is obtained using the 'config_file' method of the current class. + * + * @return string The charset value. + */ + $config_file = self::config_file(); + $charset = $config_file['charset']; + + /** + * Throws an exception if the method is 'UPDATE'. + * + * @param string $method The HTTP method. + * @throws Exception If the method is 'UPDATE'. + */ + if (strtoupper($method) === 'UPDATE') + { + throw new Exception('Invalid use of deprecated method. Method `UPDATE` has been deprecated since version 1.2.1. Use `PUT` instead.'); + } + + + /** + * Sets the HTTP header "Allow" with the specified method. + * + * @param string $method The HTTP method to be set. + * @return void + */ + $method = strtoupper($method); + header("Allow: $method; charset=$charset"); + + $dir = Route::$root_dir; + + // will store all the parameters value in this array + $req = []; + $req_value = []; + + // will store all the parameters names in this array + $paramKey = []; + + + // Gets all constant int values wrapped in an array + ob_start(); + $bin_const_types = include $dir . "/App/bin/const_types.php"; + ob_end_clean(); + + $bin_const_types = unserialize(hex2bin(($bin_const_types))); + $const_types_method = $bin_const_types[$class_method][1]; + $const_types_class = $bin_const_types[$class_method][0]; + + // finding if there is any {?} parameter in $route + if (is_string($route)) + { + preg_match_all("/(?<={).+?(?=})/", $route, $paramMatches); + } + + // if the route does not contain any param call routing(); + if (empty($paramMatches[0]) || is_array($route)) + { + + /** + * ------------------------------------------------------ + * | Check if $class_method is a callable function + * | or array of controller, and if not, + * | it's a string of text or html document + * ------------------------------------------------------ + */ + + $class_method = self::routing($route, $class_method, $method); + + if ($class_method) + { + if ( + (preg_match("/(Controller)/", $const_types_class, $matches) && count($matches) > 1) && + (preg_match("/(Api)/", $const_types_class, $matches) && count($matches) > 0) + ) + { + http_response_code(200); + header("Content-Type: application/json; charset=$charset"); + + print_r(self::controller($const_types_class, $const_types_method)); + } + else + { + throw new Exception("Invalid api controller class or not existing: " . $const_types_class); + } + + self::log(); + exit(); + } + else + { + return; + } + } + + // setting parameters names + foreach ($paramMatches[0] as $key) + { + $paramKey[] = $key; + } + + /** + * ---------------------------------------------- + * | Replacing first and last forward slashes + * | $_REQUEST['uri'] will be empty if req uri is / + * ---------------------------------------------- + */ + + if (!empty(Route::$request_uri)) + { + $route = preg_replace("/(^\/)|(\/$)/", "", $route); + $reqUri = preg_replace("/(^\/)|(\/$)/", "", Route::$request_uri); + } + else + { + $reqUri = "/"; + } + + // exploding route address + $uri = explode("/", $route); + + // will store index number where {?} parameter is required in the $route + $indexNum = []; + + // storing index number, where {?} parameter is required with the help of regex + foreach ($uri as $index => $param) + { + if (preg_match("/{.*}/", $param)) + { + $indexNum[] = $index; + } + } + + /** + * ---------------------------------------------------------------------------------- + * | Exploding request uri string to array to get the exact index number value of parameter from $_REQUEST['uri'] + * ---------------------------------------------------------------------------------- + */ + $reqUri = explode("/", $reqUri); + + /** + * ---------------------------------------------------------------------------------- + * | Running for each loop to set the exact index number with reg expression this will help in matching route + * ---------------------------------------------------------------------------------- + */ + foreach ($indexNum as $key => $index) + { + + /** + * -------------------------------------------------------------------------------- + * | In case if req uri with param index is empty then return because URL is not valid for this route + * -------------------------------------------------------------------------------- + */ + + if (empty($reqUri[$index])) + { + return; + } + + // setting params with params names + $req[$paramKey[$key]] = htmlspecialchars($reqUri[$index]); + $req_value[] = htmlspecialchars($reqUri[$index]); + + // this is to create a regex for comparing route address + $reqUri[$index] = "{.*}"; + } + + // converting array to string + $reqUri = implode("/", $reqUri); + + /** + * ----------------------------------- + * | replace all / with \/ for reg expression + * | regex to match route is ready! + * ----------------------------------- + */ + $reqUri = str_replace("/", "\\/", $reqUri); + + // now matching route with regex + if (preg_match("/$reqUri/", $route)) + { + // checks if the requested method is of the given route + if ( + strtoupper($_SERVER["REQUEST_METHOD"]) !== $method && + $method !== "*" + ) + { + http_response_code(405); + self::log(); + exit("Method Not Allowed"); + } + + if ( + (preg_match("/(Controller)/", $const_types_class, $matches) && count($matches) > 1) && + (preg_match("/(Api)/", $const_types_class, $matches) && count($matches) > 0) + ) + { + http_response_code(200); + header("Content-Type: application/json; charset=$charset"); + + print_r( + self::controller($const_types_class, $const_types_method, [ + ...$req_value, + ]), + ); + } + else + { + throw new Exception("Invalid api controller class or not existing: " . $const_types_class); + } + + self::log(); + exit(); + } + } + catch ( Exception $e ) + { + http_response_code(500); + print_r($e->getMessage()); + exit(); + } + } + + + /** + * -------------------------------------------------------------- + * + * | GET API ROUTE METHOD + * + * | Cannot evaluate {?} URL parameters in API route if it's an array + * + * -------------------------------------------------------------- + */ + public static function get( + array|string $route, + int $class_method, + ) { + self::any($route, $class_method, "GET"); + } + + + + /** + * -------------------------------------------------------------- + * + * | POST API ROUTE METHOD + * + * | Cannot evaluate {?} URL parameters in API route if it's an array + * + * -------------------------------------------------------------- + */ + public static function post( + array|string $route, + int $class_method, + ) { + self::any($route, $class_method, "POST"); + } + + + + /** + * -------------------------------------------------------------- + * + * | PUT API ROUTE METHOD + * + * | Cannot evaluate {?} URL parameters in API route if it's an array + * + * -------------------------------------------------------------- + */ + public static function put( + array|string $route, + int $class_method, + ) { + self::any($route, $class_method, "PUT"); + } + + + + /** + * -------------------------------------------------------------- + * + * | UPDATE API ROUTE METHOD + * + * | Cannot evaluate {?} URL parameters in API route if it's an array + * + * @deprecated ^1.2.1 This method has been deprecated since version 1.2.1. Use `Api::put()` instead + * + * -------------------------------------------------------------- + */ + public static function update( + array|string $route, + int $class_method, + ) { + self::any($route, $class_method, "UPDATE"); + } + + + + /** + * -------------------------------------------------------------- + * + * | PATCH API ROUTE METHOD + * + * | Cannot evaluate {?} URL parameters in API route if it's an array + * + * -------------------------------------------------------------- + */ + public static function patch( + array|string $route, + int $class_method, + ) { + self::any($route, $class_method, "PATCH"); + } + + + + /** + * -------------------------------------------------------------- + * + * | DELETE API ROUTE METHOD + * + * | Cannot evaluate {?} URL parameters in API route if it's an array + * + * -------------------------------------------------------------- + */ + public static function delete( + array|string $route, + int $class_method, + ) { + self::any($route, $class_method, "DELETE"); + } } diff --git a/App/PhpSlides.php b/App/PhpSlides.php index d19292d..e022c64 100644 --- a/App/PhpSlides.php +++ b/App/PhpSlides.php @@ -1,5 +1,17 @@ + * @license MIT + */ declare(strict_types=1); namespace PhpSlides; @@ -31,795 +43,848 @@ final class Route extends Controller { - /** - * `$log` method prints logs in `.log` file in the root of the project each time any request has been received, when setted to true. - * It's been setted to true by default, can be changed anytime. - * - * @static $log - * @var bool $log - * @return bool - */ - public static bool $log; - - /** - * Gets the full location of project root directory - * - * @static $root_dir - * @var string $root_dir - * @return string Location directory of project with `__DIR__` - */ - public static string $root_dir; - - /** - * Get's all full request URL - * - * @static $root_dir - * @var string $root_dir - * @return string - */ - public static string $request_uri; - - - - /** - * ------------------------------------------------------ - * | - * | Get the file extension content-type with mime - * - * @param string $filename File path or file resources - * @return bool|string Returns the MIME content type for a file as determined by using information from the magic.mime file. - * | - * ------------------------------------------------------ - */ - public static function file_type(string $filename): bool|string - { - if (is_file($filename)) - { - if (!extension_loaded('fileinfo')) - { - throw new Exception( - 'Fileinfo extension is not enabled. Please enable it in your php.ini configuration.', - ); - } - - $file_info = finfo_open(FILEINFO_MIME_TYPE); - $file_type = finfo_file($file_info, $filename); - finfo_close($file_info); - - $file_ext = explode('.', $filename); - $file_ext = strtolower(end($file_ext)); - - if ($file_type === 'text/plain' || $file_type === 'application/octet-stream') - { - switch ($file_ext) - { - case 'css': - return 'text/css'; - case 'csv': - return 'text/csv'; - case 'htm': - return 'text/htm'; - case 'html': - return 'text/html'; - case 'js': - return 'application/javascript'; - case 'pdf': - return 'application/pdf'; - case 'doc': - return 'application/msword'; - case 'docx': - return 'application/vnd.openxmlformats-officedocument.wordprocessingml.document'; - case 'xls': - return 'application/vnd.ms-excel'; - case 'xlsx': - return 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'; - case 'json': - return 'application/json'; - case 'md': - return 'text/markdown'; - case 'ppt': - return 'application/mspowerpoint'; - case 'pptx': - return 'application/vnd.openxmlformats-officedocument.presentationml.presentation'; - case 'swf': - return 'application/x-shockwave-flash'; - case 'ai': - return 'application/postscript'; - case 'odt': - return 'application/vnd.oasis.opendocument.text'; - - default: - return $file_type; - } - } - else - { - return $file_type; - } - } - else - { - return false; - } - } - - - - - /** - * --------------------------------------------------------------------------------------------------------- - * - * | If `$request_log` is set to true, it prints logs in `.log` file in the root of the project each time any request has been received. - * | It's been setted to true by default - * - * - * | This function handles getting files request and describe the type of request to handle according to `phpslides.config.json` file in the root of the project, - * | for more security, it disallow users in navigating to wrong paths or files of the project. - * - * - * | This config method must be called before writing any other Route method or codes. - * | - * - * @param bool $request_log The parameter indicates request logger to prints out logs output on each received request - * - * --------------------------------------------------------------------------------------------------------- - */ - public static function config(bool $request_log = true) - { - try - { - self::$log = $request_log; - self::$root_dir = htmlspecialchars(dirname(getcwd())); - self::$request_uri = htmlspecialchars($_REQUEST['uri']); - - $dir = self::$root_dir; - $req = preg_replace("/(^\/)|(\/$)/", '', self::$request_uri); - $url = explode('/', $req); - - $req_ext = explode('.', end($url)); - $req_ext = strtolower(end($req_ext)); - - $file = self::slides_include($dir . '/public/' . $req); - $file_type = $file ? self::file_type($dir . '/public/' . $req) : null; - - $config_file = self::config_file(); - - $charset = $config_file['charset']; - - /** - * ---------------------------------------------- - * | Config File & Request Router configurations - * ---------------------------------------------- - */ - if (!empty($config_file) && $file_type != null) - { - $config = $config_file['public']; - $accept = true; - - // loop over the requested URL folders - foreach ($url as $index => $value) - { - - /** - * ----------------------------------------------- - * | Checks if array key from URL exists in the config file - * ----------------------------------------------- - */ - if (array_key_exists($value, $config)) - { - if (in_array($req_ext, $config[$value]) && $accept != false) - { - $accept = $req_ext; - - /** - * ----------------------------------------------- - * | Checks if the next array key from URL exists in the config file - * ----------------------------------------------- - */ - if (array_key_exists($url[$index + 1], $config)) - { - continue; - } - /** - * ----------------------------------------------- - * | Performs the logic for accepting current file - * ----------------------------------------------- - */ - else - { - http_response_code(200); - header("Content-Type: $file_type"); - - print_r($file); - self::log(); - - exit(); - } - } - /** - * ----------------------------------------------------------- - * | Checks if * or image exists in the config file of the $value - * | Then it accept all types of files or all types of image in the childrens folder - * ----------------------------------------------------------- - */ - elseif ( - in_array('*', $config[$value]) || - (in_array('image', $config[$value]) && - preg_match('/(image\/*)/', $file_type)) || - (in_array('video', $config[$value]) && - preg_match('/(video\/*)/', $file_type)) || - (in_array('audio', $config[$value]) && - preg_match('/(audio\/*)/', $file_type) && - $accept != false) - ) - { - $accept = '*'; - - if (array_key_exists($url[$index + 1], $config)) - { - continue; - } - /** - * ----------------------------------------------- - * | Performs the logic for accepting current file - * ----------------------------------------------- - */ - else - { - http_response_code(200); - header("Content-Type: $file_type"); - - print_r($file); - self::log(); - - exit(); - } - } - else - { - $accept = false; - } - } - } - - /** - * ------------------------------------------------------------------------ - * | If request URL is a file from / and is in the root directory of public folder - * ------------------------------------------------------------------------ - */ - if ( - array_key_exists('/', $config) && - count($url) === 1 && - is_file($dir . '/public/' . $url[0]) - ) - { - $req_ext = explode('.', $url[0]); - $req_ext = strtolower(end($req_ext)); - $root = $config['/']; - - /** - * --------------------------------------------------------------------------------------------- - * | checks if the requested file extension is available in the config files or * which signifies all types of extension - * --------------------------------------------------------------------------------------------- - */ - for ($i = 0; $i < count($root); $i++) - { - $root1 = strtolower($root[$i]); - - if ( - $root1 === $req_ext || - $root1 === '*' || - ($root1 === 'image' && preg_match('/(image\/*)/', $req_ext)) || - ($root1 === 'video' && preg_match('/(video\/*)/', $req_ext)) || - ($root1 === 'audio' && preg_match('/(audio\/*)/', $req_ext)) - ) - { - http_response_code(200); - header("Content-Type: $file_type"); - - print_r($file); - self::log(); - exit(); - } - } - } - } - - - /** - * ---------------------------------------------- - * | Generates Bin files - * ---------------------------------------------- - */ - $gen_codes1 = "getMessage(); - exit(); - } - } - - - /** - * ------------------------------------------------------------------------ - * - * | ANY REQUEST FROM ROUTE - * - * | Accept all type of request or any other method - * - * | Cannot evaluate `{?} URL parameters` in route if it's an array - * | - * - * @param array|string $route This describes the URL string to check if it matches the request URL, use array of URLs for multiple request - * @param mixed $callback Can contain any types of data to return to the client side/browser. - * - * ------------------------------------------------------------------------ - */ - public static function any(array|string $route, mixed $callback, string $method = '*') - { - - /** - * -------------------------------------------------------------- - * - * | Not Found Error - * - * | This * route serves as 404, which executes whenever there're no matching routes from the request url - * | which takes a callback parameter that is rendered to the webpage - * - * -------------------------------------------------------------- - */ - - if ((is_array($route) && in_array('*', $route)) || $route === '*') - { - header('HTTP/1.0 404 Not Found'); - header('Content-Type: */*'); - - print_r(is_callable($callback) ? $callback() : $callback); - self::log(); - exit(); - } - - // will store all the parameters value in this array - $req = []; - $req_value = []; - - // will store all the parameters names in this array - $paramKey = []; - - // finding if there is any {?} parameter in $route - if (is_string($route)) - { - preg_match_all('/(?<={).+?(?=})/', $route, $paramMatches); - } - - // if the route does not contain any param call routing(); - if (empty($paramMatches[0]) || is_array($route)) - { - - /** - * ------------------------------------------------------ - * | Check if $callback is a callable function - * | or array of controller, and if not, - * | it's a string of text or html document - * ------------------------------------------------------ - */ - $callback = self::routing($route, $callback, $method); - - if ($callback) - { - if ( - is_array($callback) && - (preg_match('/(Controller)/', $callback[0], $matches) && - count($matches) > 1) - ) - { - print_r( - self::controller( - $callback[0], - count($callback) > 1 ? $callback[1] : '', - ), - ); - } - else - { - print_r(is_callable($callback) ? $callback() : $callback); - } - - self::log(); - exit(); - } - else - { - return; - } - } - - // setting parameters names - foreach ($paramMatches[0] as $key) - { - $paramKey[] = $key; - } - - /** - * ---------------------------------------------- - * | Replacing first and last forward slashes - * | $_REQUEST['uri'] will be empty if req uri is / - * ---------------------------------------------- - */ - - if (!empty(self::$request_uri)) - { - $route = preg_replace("/(^\/)|(\/$)/", '', $route); - $reqUri = preg_replace("/(^\/)|(\/$)/", '', self::$request_uri); - } - else - { - $reqUri = '/'; - } - - // exploding route address - $uri = explode('/', $route); - - // will store index number where {?} parameter is required in the $route - $indexNum = []; - - // storing index number, where {?} parameter is required with the help of regex - foreach ($uri as $index => $param) - { - if (preg_match('/{.*}/', $param)) - { - $indexNum[] = $index; - } - } - - /** - * ---------------------------------------------------------------------------------- - * | Exploding request uri string to array to get the exact index number value of parameter from $_REQUEST['uri'] - * ---------------------------------------------------------------------------------- - */ - $reqUri = explode('/', $reqUri); - - /** - * ---------------------------------------------------------------------------------- - * | Running for each loop to set the exact index number with reg expression this will help in matching route - * ---------------------------------------------------------------------------------- - */ - foreach ($indexNum as $key => $index) - { - /** - * -------------------------------------------------------------------------------- - * | In case if req uri with param index is empty then return because URL is not valid for this route - * -------------------------------------------------------------------------------- - */ - - if (empty($reqUri[$index])) - { - return; - } - - // setting params with params names - $req[$paramKey[$key]] = htmlspecialchars($reqUri[$index]); - $req_value[] = htmlspecialchars($reqUri[$index]); - - // this is to create a regex for comparing route address - $reqUri[$index] = '{.*}'; - } - - // converting array to string - $reqUri = implode('/', $reqUri); - - /** - * ----------------------------------- - * | replace all / with \/ for reg expression - * | regex to match route is ready! - * ----------------------------------- - */ - $reqUri = str_replace('/', '\\/', $reqUri); - - // now matching route with regex - if (preg_match("/$reqUri/", $route)) - { - // checks if the requested method is of the given route - if ( - strtoupper($_SERVER['REQUEST_METHOD']) !== strtoupper($method) && - $method !== '*' - ) - { - http_response_code(405); - self::log(); - exit('Method Not Allowed'); - } - - http_response_code(200); - header('Content-Type: */*'); - - if ( - is_array($callback) && - (preg_match('/(Controller)/', $callback[0], $matches) && - count($matches) > 1) - ) - { - print_r( - self::controller( - $callback[0], - count($callback) > 1 ? $callback[1] : '', - $req_value, - ), - ); - } - else - { - print_r(is_callable($callback) ? $callback(...$req_value) : $callback); - } - - self::log(); - exit(); - } - } - - - - /** - * --------------------------------------------------------------------------- - * - * | VIEW ROUTE METHOD - * - * | Route only needs to return a view; you may provide an array for multiple request - * - * | View Route does not accept `{?} URL parameters` in route, use GET method instead - * - * @param array|string $route This describes the URL string to render, use array of strings for multiple request - * @param string $view It renders this param, it can be functions to render, view:: to render or strings of text or documents - * | - * - * --------------------------------------------------------------------------- - */ - public static function view(array|string $route, string $view) - { - - /** - * ---------------------------------------- - * | Replacing first and last forward slashes - * | $_REQUEST['uri'] will be empty if req uri is / - * ---------------------------------------- - */ - - $uri = []; - $str_route = ''; - $reqUri = preg_replace("/(^\/)|(\/$)/", '', self::$request_uri); - - if (is_array($route)) - { - for ($i = 0; $i < count($route); $i++) - { - $each_route = preg_replace("/(^\/)|(\/$)/", '', $route[$i]); - array_push($uri, $each_route); - } - } - else - { - $str_route = preg_replace("/(^\/)|(\/$)/", '', $route); - } - - if (in_array($reqUri, $uri) || $reqUri === $str_route) - { - if (strtoupper($_SERVER['REQUEST_METHOD']) !== 'GET') - { - http_response_code(405); - self::log(); - exit('Method Not Allowed'); - } - - // render view page to browser - $view = view::render($view) !== 'null' ? view::render($view) : $view; - - if ($view != 'null') - { - print_r($view); - self::log(); - exit(); - } - } - } - - - - /** - * -------------------------------------------------------------- - * - * | REDIRECT ROUTE METHOD - * - * | This method redirects the routes URL to the giving URL directly - * - * @param string $route The requested url to redirect - * @param string $new_url The new URL route to redirect to - * @param int $code The code for redirect method, 301 for permanent redirecting & 302 for temporarily redirect. - * - * --------------------------------------------------------------- - */ - public static function redirect(string $route, string $new_url, int $code = 302) - { - if (!empty(self::$request_uri)) - { - $route = preg_replace("/(^\/)|(\/$)/", '', $route); - $new_url = preg_replace("/(^\/)|(\/$)/", '', $new_url); - $reqUri = preg_replace("/(^\/)|(\/$)/", '', self::$request_uri); - } - else - { - $reqUri = '/'; - $new_url = preg_replace("/(^\/)|(\/$)/", '', $new_url); - } - - if ($reqUri === $route) - { - header("Location: $new_url", true, $code); - exit(); - } - } - - - - /** - * -------------------------------------------------------------- - * - * | GET ROUTE METHOD - * - * | Cannot evaluate {?} URL parameters in route if it's an array - * - * -------------------------------------------------------------- - */ - public static function get(array|string $route, $callback) - { - self::any($route, $callback, 'GET'); - } - - - - /** - * -------------------------------------------------------------- - * - * | POST ROUTE METHOD - * - * | Cannot evaluate {?} URL parameters in route if it's an array - * - * -------------------------------------------------------------- - */ - public static function post(array|string $route, $callback) - { - self::any($route, $callback, 'POST'); - } - - - - /** - * -------------------------------------------------------------- - * - * | PUT ROUTE METHOD - * - * | Cannot evaluate {?} URL parameters in route if it's an array - * - * -------------------------------------------------------------- - */ - public static function put(array|string $route, $callback) - { - self::any($route, $callback, 'PUT'); - } - - - - /** - * -------------------------------------------------------------- - * - * | UPDATE ROUTE METHOD - * - * | Cannot evaluate {?} URL parameters in route if it's an array - * - * -------------------------------------------------------------- - */ - public static function update(array|string $route, $callback) - { - self::any($route, $callback, 'UPDATE'); - } - - - - /** - * -------------------------------------------------------------- - * - * | PATCH ROUTE METHOD - * - * | Cannot evaluate {?} URL parameters in route if it's an array - * - * -------------------------------------------------------------- - */ - public static function patch(array|string $route, $callback) - { - self::any($route, $callback, 'PATCH'); - } - - - - /** - * -------------------------------------------------------------- - * - * | DELETE ROUTE METHOD - * - * | Cannot evaluate {?} URL parameters in route if it's an array - * - * -------------------------------------------------------------- - */ - public static function delete(array|string $route, $callback) - { - self::any($route, $callback, 'DELETE'); - } + /** + * `$log` method prints logs in `.log` file in the root of the project each time any request has been received, when setted to true. + * It's been setted to true by default, can be changed anytime. + * + * @static $log + * @var bool $log + * @return bool + */ + public static bool $log; + + /** + * Gets the full location of project root directory + * + * @static $root_dir + * @var string $root_dir + * @return string Location directory of project with `__DIR__` + */ + public static string $root_dir; + + /** + * Get's all full request URL + * + * @static $root_dir + * @var string $root_dir + * @return string + */ + public static string $request_uri; + + + + /** + * ------------------------------------------------------ + * | + * | Get the file extension content-type with mime + * + * @param string $filename File path or file resources + * @return bool|string Returns the MIME content type for a file as determined by using information from the magic.mime file. + * | + * ------------------------------------------------------ + */ + public static function file_type(string $filename): bool|string + { + if (is_file($filename)) + { + if (!extension_loaded('fileinfo')) + { + throw new Exception( + 'Fileinfo extension is not enabled. Please enable it in your php.ini configuration.', + ); + } + + $file_info = finfo_open(FILEINFO_MIME_TYPE); + $file_type = finfo_file($file_info, $filename); + finfo_close($file_info); + + $file_ext = explode('.', $filename); + $file_ext = strtolower(end($file_ext)); + + if ($file_type === 'text/plain' || $file_type === 'application/octet-stream') + { + switch ($file_ext) + { + case 'css': + return 'text/css'; + case 'csv': + return 'text/csv'; + case 'htm': + return 'text/htm'; + case 'html': + return 'text/html'; + case 'js': + return 'application/javascript'; + case 'pdf': + return 'application/pdf'; + case 'doc': + return 'application/msword'; + case 'docx': + return 'application/vnd.openxmlformats-officedocument.wordprocessingml.document'; + case 'xls': + return 'application/vnd.ms-excel'; + case 'xlsx': + return 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'; + case 'json': + return 'application/json'; + case 'md': + return 'text/markdown'; + case 'ppt': + return 'application/mspowerpoint'; + case 'pptx': + return 'application/vnd.openxmlformats-officedocument.presentationml.presentation'; + case 'swf': + return 'application/x-shockwave-flash'; + case 'ai': + return 'application/postscript'; + case 'odt': + return 'application/vnd.oasis.opendocument.text'; + + default: + return $file_type; + } + } + else + { + return $file_type; + } + } + else + { + return false; + } + } + + + + + /** + * --------------------------------------------------------------------------------------------------------- + * + * | If `$request_log` is set to true, it prints logs in `.log` file in the root of the project each time any request has been received. + * | It's been setted to true by default + * + * | This function handles getting files request and describe the type of request to handle according to `phpslides.config.json` file in the root of the project, + * | for more security, it disallow users in navigating to wrong paths or files of the project. + * + * | This config method must be called before writing any other Route method or codes. + * + * @param bool $request_log The parameter indicates request logger to prints out logs output on each received request + * + * --------------------------------------------------------------------------------------------------------- + */ + public static function config(bool $request_log = true) + { + try + { + self::$log = $request_log; + self::$root_dir = htmlspecialchars(dirname(getcwd())); + self::$request_uri = htmlspecialchars($_REQUEST['uri']); + + $dir = self::$root_dir; + $req = preg_replace("/(^\/)|(\/$)/", '', self::$request_uri); + $url = explode('/', $req); + + $req_ext = explode('.', end($url)); + $req_ext = strtolower(end($req_ext)); + + $file = self::slides_include($dir . '/public/' . $req); + $file_type = $file ? self::file_type($dir . '/public/' . $req) : null; + + /** + * Retrieves the charset from the configuration file. + * + * The above code is retrieving the value of the 'charset' key from the 'config_file' array. + * The 'config_file' array is obtained using the 'config_file' method of the current class. + * + * @return string The charset value. + */ + $config_file = self::config_file(); + $charset = $config_file['charset']; + + /** + * ---------------------------------------------- + * | Config File & Request Router configurations + * ---------------------------------------------- + */ + if (!empty($config_file) && $file_type != null) + { + $config = $config_file['public']; + $accept = true; + + // loop over the requested URL folders + foreach ($url as $index => $value) + { + + /** + * ----------------------------------------------- + * | Checks if array key from URL exists in the config file + * ----------------------------------------------- + */ + if (array_key_exists($value, $config)) + { + if (in_array($req_ext, $config[$value]) && $accept != false) + { + $accept = $req_ext; + + /** + * ----------------------------------------------- + * | Checks if the next array key from URL exists in the config file + * ----------------------------------------------- + */ + if (array_key_exists($url[$index + 1], $config)) + { + continue; + } + /** + * ----------------------------------------------- + * | Performs the logic for accepting current file + * ----------------------------------------------- + */ + else + { + http_response_code(200); + header("Content-Type: $file_type; charset=$charset"); + + print_r($file); + self::log(); + + exit(); + } + } + /** + * ----------------------------------------------------------- + * | Checks if * or image exists in the config file of the $value + * | Then it accept all types of files or all types of image in the childrens folder + * ----------------------------------------------------------- + */ + elseif ( + in_array('*', $config[$value]) || + (in_array('image', $config[$value]) && + preg_match('/(image\/*)/', $file_type)) || + (in_array('video', $config[$value]) && + preg_match('/(video\/*)/', $file_type)) || + (in_array('audio', $config[$value]) && + preg_match('/(audio\/*)/', $file_type) && + $accept != false) + ) + { + $accept = '*'; + + if (array_key_exists($url[$index + 1], $config)) + { + continue; + } + /** + * ----------------------------------------------- + * | Performs the logic for accepting current file + * ----------------------------------------------- + */ + else + { + http_response_code(200); + header("Content-Type: $file_type; charset=$charset"); + + print_r($file); + self::log(); + + exit(); + } + } + else + { + $accept = false; + } + } + } + + /** + * ------------------------------------------------------------------------ + * | If request URL is a file from / and is in the root directory of public folder + * ------------------------------------------------------------------------ + */ + if ( + array_key_exists('/', $config) && + count($url) === 1 && + is_file($dir . '/public/' . $url[0]) + ) + { + $req_ext = explode('.', $url[0]); + $req_ext = strtolower(end($req_ext)); + $root = $config['/']; + + /** + * --------------------------------------------------------------------------------------------- + * | checks if the requested file extension is available in the config files or * which signifies all types of extension + * --------------------------------------------------------------------------------------------- + */ + for ($i = 0; $i < count($root); $i++) + { + $root1 = strtolower($root[$i]); + + if ( + $root1 === $req_ext || + $root1 === '*' || + ($root1 === 'image' && preg_match('/(image\/*)/', $req_ext)) || + ($root1 === 'video' && preg_match('/(video\/*)/', $req_ext)) || + ($root1 === 'audio' && preg_match('/(audio\/*)/', $req_ext)) + ) + { + http_response_code(200); + header("Content-Type: $file_type; charset=$charset"); + + print_r($file); + self::log(); + exit(); + } + } + } + } + + + /** + * ---------------------------------------------- + * | Generates Bin files + * ---------------------------------------------- + */ + $gen_codes1 = "getMessage(); + exit(); + } + } + + + /** + * ------------------------------------------------------------------------ + * + * | ANY REQUEST FROM ROUTE + * + * | Accept all type of request or any other method + * + * | Cannot evaluate `{?} URL parameters` in route if it's an array + * | + * + * @param array|string $route This describes the URL string to check if it matches the request URL, use array of URLs for multiple request + * @param mixed $callback Can contain any types of data to return to the client side/browser. + * + * ------------------------------------------------------------------------ + */ + public static function any(array|string $route, mixed $callback, string $method = '*') + { + + /** + * Retrieves the charset from the configuration file. + * + * The above code is retrieving the value of the 'charset' key from the 'config_file' array. + * The 'config_file' array is obtained using the 'config_file' method of the current class. + * + * @return string The charset value. + */ + $config_file = self::config_file(); + $charset = $config_file['charset']; + + /** + * -------------------------------------------------------------- + * + * | Not Found Error + * + * | This * route serves as 404, which executes whenever there're no matching routes from the request url + * | which takes a callback parameter that is rendered to the webpage + * + * -------------------------------------------------------------- + */ + if ((is_array($route) && in_array('*', $route)) || $route === '*') + { + header('HTTP/1.0 404 Not Found'); + header("Content-Type: */*; charset=$charset", true, 404); + + print_r(is_callable($callback) ? $callback() : $callback); + self::log(); + exit(); + } + + /** + * Sets the HTTP header "Allow" with the specified method. + * + * @param string $method The HTTP method to be set. + * @return void + */ + $method = strtoupper($method); + header("Allow: $method; charset=$charset"); + + /** + * Throws an exception if the method is 'UPDATE'. + * + * @param string $method The HTTP method. + * @throws Exception If the method is 'UPDATE'. + */ + if ($method === 'UPDATE') + { + throw new Exception('Invalid use of deprecated method. Method `UPDATE` has been deprecated since version 1.2.1. Use `PUT` instead.'); + } + + // will store all the parameters value in this array + $req = []; + $req_value = []; + + // will store all the parameters names in this array + $paramKey = []; + + // finding if there is any {?} parameter in $route + if (is_string($route)) + { + preg_match_all('/(?<={).+?(?=})/', $route, $paramMatches); + } + + // if the route does not contain any param call routing(); + if (empty($paramMatches[0]) || is_array($route)) + { + + /** + * ------------------------------------------------------ + * | Check if $callback is a callable function + * | or array of controller, and if not, + * | it's a string of text or html document + * ------------------------------------------------------ + */ + $callback = self::routing($route, $callback, $method); + + if ($callback) + { + if ( + is_array($callback) && + (preg_match('/(Controller)/', $callback[0], $matches) && + count($matches) > 1) + ) + { + print_r( + self::controller( + $callback[0], + count($callback) > 1 ? $callback[1] : '', + ), + ); + } + else + { + print_r(is_callable($callback) ? $callback() : $callback); + } + + self::log(); + exit(); + } + else + { + return; + } + } + + // setting parameters names + foreach ($paramMatches[0] as $key) + { + $paramKey[] = $key; + } + + /** + * ---------------------------------------------- + * | Replacing first and last forward slashes + * | $_REQUEST['uri'] will be empty if req uri is / + * ---------------------------------------------- + */ + + if (!empty(self::$request_uri)) + { + $route = preg_replace("/(^\/)|(\/$)/", '', $route); + $reqUri = preg_replace("/(^\/)|(\/$)/", '', self::$request_uri); + } + else + { + $reqUri = '/'; + } + + // exploding route address + $uri = explode('/', $route); + + // will store index number where {?} parameter is required in the $route + $indexNum = []; + + // storing index number, where {?} parameter is required with the help of regex + foreach ($uri as $index => $param) + { + if (preg_match('/{.*}/', $param)) + { + $indexNum[] = $index; + } + } + + /** + * ---------------------------------------------------------------------------------- + * | Exploding request uri string to array to get the exact index number value of parameter from $_REQUEST['uri'] + * ---------------------------------------------------------------------------------- + */ + $reqUri = explode('/', $reqUri); + + /** + * ---------------------------------------------------------------------------------- + * | Running for each loop to set the exact index number with reg expression this will help in matching route + * ---------------------------------------------------------------------------------- + */ + foreach ($indexNum as $key => $index) + { + /** + * -------------------------------------------------------------------------------- + * | In case if req uri with param index is empty then return because URL is not valid for this route + * -------------------------------------------------------------------------------- + */ + + if (empty($reqUri[$index])) + { + return; + } + + // setting params with params names + $req[$paramKey[$key]] = htmlspecialchars($reqUri[$index]); + $req_value[] = htmlspecialchars($reqUri[$index]); + + // this is to create a regex for comparing route address + $reqUri[$index] = '{.*}'; + } + + // converting array to string + $reqUri = implode('/', $reqUri); + + /** + * ----------------------------------- + * | replace all / with \/ for reg expression + * | regex to match route is ready! + * ----------------------------------- + */ + $reqUri = str_replace('/', '\\/', $reqUri); + + // now matching route with regex + if (preg_match("/$reqUri/", $route)) + { + // checks if the requested method is of the given route + if ( + strtoupper($_SERVER['REQUEST_METHOD']) !== $method && + $method !== '*' + ) + { + http_response_code(405); + self::log(); + exit('Method Not Allowed'); + } + + http_response_code(200); + header('Content-Type: */*'); + + if ( + is_array($callback) && + (preg_match('/(Controller)/', $callback[0], $matches) && + count($matches) > 1) + ) + { + print_r( + self::controller( + $callback[0], + count($callback) > 1 ? $callback[1] : '', + $req_value, + ), + ); + } + else + { + print_r(is_callable($callback) ? $callback(...$req_value) : $callback); + } + + self::log(); + exit(); + } + } + + + + /** + * --------------------------------------------------------------------------- + * + * | VIEW ROUTE METHOD + * + * | Route only needs to return a view; you may provide an array for multiple request + * + * | View Route does not accept `{?} URL parameters` in route, use GET method instead + * + * @param array|string $route This describes the URL string to render, use array of strings for multiple request + * @param string $view It renders this param, it can be functions to render, view:: to render or strings of text or documents + * | + * + * --------------------------------------------------------------------------- + */ + public static function view(array|string $route, string $view) + { + + /** + * ---------------------------------------- + * | Replacing first and last forward slashes + * | $_REQUEST['uri'] will be empty if req uri is / + * ---------------------------------------- + */ + + $uri = []; + $str_route = ''; + $reqUri = preg_replace("/(^\/)|(\/$)/", '', self::$request_uri); + + if (is_array($route)) + { + for ($i = 0; $i < count($route); $i++) + { + $each_route = preg_replace("/(^\/)|(\/$)/", '', $route[$i]); + array_push($uri, $each_route); + } + } + else + { + $str_route = preg_replace("/(^\/)|(\/$)/", '', $route); + } + + if (in_array($reqUri, $uri) || $reqUri === $str_route) + { + if (strtoupper($_SERVER['REQUEST_METHOD']) !== 'GET') + { + http_response_code(405); + self::log(); + exit('Method Not Allowed'); + } + + // render view page to browser + $view = view::render($view) !== 'null' ? view::render($view) : $view; + + if ($view != 'null') + { + print_r($view); + self::log(); + exit(); + } + } + } + + + + /** + * -------------------------------------------------------------- + * + * | REDIRECT ROUTE METHOD + * + * | This method redirects the routes URL to the giving URL directly + * + * @param string $route The requested url to redirect + * @param string $new_url The new URL route to redirect to + * @param int $code The code for redirect method, 301 for permanent redirecting & 302 for temporarily redirect. + * + * --------------------------------------------------------------- + */ + public static function redirect(string $route, string $new_url, int $code = 302) + { + if (!empty(self::$request_uri)) + { + $route = preg_replace("/(^\/)|(\/$)/", '', $route); + $new_url = preg_replace("/(^\/)|(\/$)/", '', $new_url); + $reqUri = preg_replace("/(^\/)|(\/$)/", '', self::$request_uri); + } + else + { + $reqUri = '/'; + $new_url = preg_replace("/(^\/)|(\/$)/", '', $new_url); + } + + if ($reqUri === $route) + { + header("Location: $new_url", true, $code); + exit(); + } + } + + + + /** + * -------------------------------------------------------------- + * + * | GET ROUTE METHOD + * + * | Cannot evaluate {?} URL parameters in route if it's an array + * + * @param array|string $route This describes the URL string to check if it matches the request URL, use array of URLs for multiple request + * @param mixed $callback Can contain any types of data to return to the client side/browser. + * + * -------------------------------------------------------------- + */ + public static function get(array|string $route, $callback) + { + self::any($route, $callback, 'GET'); + } + + + + /** + * -------------------------------------------------------------- + * + * | POST ROUTE METHOD + * + * | Cannot evaluate {?} URL parameters in route if it's an array + * + * @param array|string $route This describes the URL string to check if it matches the request URL, use array of URLs for multiple request + * @param mixed $callback Can contain any types of data to return to the client side/browser. + * + * -------------------------------------------------------------- + */ + public static function post(array|string $route, $callback) + { + self::any($route, $callback, 'POST'); + } + + + + /** + * -------------------------------------------------------------- + * + * | PUT ROUTE METHOD + * + * | Cannot evaluate {?} URL parameters in route if it's an array + * + * @param array|string $route This describes the URL string to check if it matches the request URL, use array of URLs for multiple request + * @param mixed $callback Can contain any types of data to return to the client side/browser. + * + * -------------------------------------------------------------- + */ + public static function put(array|string $route, $callback) + { + self::any($route, $callback, 'PUT'); + } + + + + /** + * -------------------------------------------------------------- + * + * | UPDATE ROUTE METHOD + * + * | Cannot evaluate {?} URL parameters in route if it's an array + * + * @deprecated ^1.2.1 This method has been deprecated since version 1.2.1. Use `Route::put()` instead + * + * -------------------------------------------------------------- + */ + public static function update(array|string $route, $callback) + { + self::any($route, $callback, 'UPDATE'); + } + + + + /** + * -------------------------------------------------------------- + * + * | PATCH ROUTE METHOD + * + * | Cannot evaluate {?} URL parameters in route if it's an array + * + * @since 1.2.0 + * @version ^1.2.1 + * @param array|string $route This describes the URL string to check if it matches the request URL, use array of URLs for multiple request + * @param mixed $callback Can contain any types of data to return to the client side/browser. + * + * -------------------------------------------------------------- + */ + public static function patch(array|string $route, $callback) + { + self::any($route, $callback, 'PATCH'); + } + + + + /** + * -------------------------------------------------------------- + * + * | DELETE ROUTE METHOD + * + * | Cannot evaluate {?} URL parameters in route if it's an array + * + * @param array|string $route This describes the URL string to check if it matches the request URL, use array of URLs for multiple request + * @param mixed $callback Can contain any types of data to return to the client side/browser. + * + * -------------------------------------------------------------- + */ + public static function delete(array|string $route, $callback) + { + self::any($route, $callback, 'DELETE'); + } } @@ -829,56 +894,68 @@ public static function delete(array|string $route, $callback) * * | Router View * - * | which control the public URL and validating + * Which control the public URL and validating. + * This class is used in rendering views and parsing public URL in views. * * -------------------------------------------------------------- */ final class view extends Controller { - /** - * -------------------------------------------------------------- - * - * | Render views and parse public URL in views - * - * @param string $view - * @return string return the file gotten from the view parameters - * - * -------------------------------------------------------------- - */ - final public static function render(string $view): mixed - { - try - { - // split :: into array and extract the folder and files - $file = preg_replace('/(::)|::/', '/', $view); - $view_path = '/views/' . $file; - - $file_uri = Route::$root_dir . $view_path; - - if (is_file($file_uri . '.view.php') && !preg_match('/(..\/)/', $view)) - { - $file_type = Route::file_type($file_uri . '.view.php'); - header("Content-Type: $file_type"); - - return self::slides_include($file_uri . '.view.php'); - } - elseif (is_file($file_uri) && !preg_match('/(..\/)/', $view)) - { - $file_type = Route::file_type($file_uri); - header("Content-Type: $file_type"); - - return self::slides_include($file_uri); - } - else - { - throw new Exception("No view controller path found called `$view`"); - } - } - catch ( Exception $e ) - { - print $e->getMessage(); - return 'null'; - } - } + /** + * -------------------------------------------------------------- + * + * | This method is used in rendering views and parsing public URL in views. + * + * @param string $view + * @return string return the file gotten from the view parameters + * + * -------------------------------------------------------------- + */ + final public static function render(string $view): mixed + { + try + { + /** + * Retrieves the charset from the configuration file. + * + * The above code is retrieving the value of the 'charset' key from the 'config_file' array. + * The 'config_file' array is obtained using the 'config_file' method of the current class. + * + * @return string The charset value. + */ + $config_file = self::config_file(); + $charset = $config_file['charset']; + + // split :: into array and extract the folder and files + $file = preg_replace('/(::)|::/', '/', $view); + $view_path = '/views/' . $file; + + $file_uri = Route::$root_dir . $view_path; + + if (is_file($file_uri . '.view.php') && !preg_match('/(..\/)/', $view)) + { + $file_type = Route::file_type($file_uri . '.view.php'); + header("Content-Type: $file_type; charset=$charset"); + + return self::slides_include($file_uri . '.view.php'); + } + elseif (is_file($file_uri) && !preg_match('/(..\/)/', $view)) + { + $file_type = Route::file_type($file_uri); + header("Content-Type: $file_type; charset=$charset"); + + return self::slides_include($file_uri); + } + else + { + throw new Exception("No view controller path found called `$view`"); + } + } + catch ( Exception $e ) + { + print $e->getMessage(); + return 'null'; + } + } } \ No newline at end of file From 66549be1caec3ebf0491c04e50d24d172ce3374d Mon Sep 17 00:00:00 2001 From: Dave Conco <118613296+dconco@users.noreply.github.com> Date: Wed, 5 Jun 2024 16:28:29 +0100 Subject: [PATCH 3/3] Update PhpSlides.php --- App/PhpSlides.php | 102 +++++++++++++++++++++------------------------- 1 file changed, 46 insertions(+), 56 deletions(-) diff --git a/App/PhpSlides.php b/App/PhpSlides.php index e022c64..87016ac 100644 --- a/App/PhpSlides.php +++ b/App/PhpSlides.php @@ -903,59 +903,49 @@ final class view extends Controller { /** - * -------------------------------------------------------------- - * - * | This method is used in rendering views and parsing public URL in views. - * - * @param string $view - * @return string return the file gotten from the view parameters - * - * -------------------------------------------------------------- - */ - final public static function render(string $view): mixed - { - try - { - /** - * Retrieves the charset from the configuration file. - * - * The above code is retrieving the value of the 'charset' key from the 'config_file' array. - * The 'config_file' array is obtained using the 'config_file' method of the current class. - * - * @return string The charset value. - */ - $config_file = self::config_file(); - $charset = $config_file['charset']; - - // split :: into array and extract the folder and files - $file = preg_replace('/(::)|::/', '/', $view); - $view_path = '/views/' . $file; - - $file_uri = Route::$root_dir . $view_path; - - if (is_file($file_uri . '.view.php') && !preg_match('/(..\/)/', $view)) - { - $file_type = Route::file_type($file_uri . '.view.php'); - header("Content-Type: $file_type; charset=$charset"); - - return self::slides_include($file_uri . '.view.php'); - } - elseif (is_file($file_uri) && !preg_match('/(..\/)/', $view)) - { - $file_type = Route::file_type($file_uri); - header("Content-Type: $file_type; charset=$charset"); - - return self::slides_include($file_uri); - } - else - { - throw new Exception("No view controller path found called `$view`"); - } - } - catch ( Exception $e ) - { - print $e->getMessage(); - return 'null'; - } - } -} \ No newline at end of file + * -------------------------------------------------------------- + * + * | Render views and parse public URL in views + * + * @param string $view + * @return mixed return the file gotten from the view parameters + * + * -------------------------------------------------------------- + */ + final public static function render(string $view): mixed + { + try + { + // split :: into array and extract the folder and files + $file = preg_replace('/(::)|::/', '/', $view); + $file = strtolower(trim($file, '\/\/')); + $view_path = '/views/' . $file; + + $file_uri = Route::$root_dir . $view_path; + + if (is_file($file_uri . '.view.php') && !preg_match('/(..\/)/', $view)) + { + $file_type = Route::file_type($file_uri . '.view.php'); + header("Content-Type: $file_type"); + + return self::slides_include($file_uri . '.view.php'); + } + elseif (is_file($file_uri) && !preg_match('/(..\/)/', $view)) + { + $file_type = Route::file_type($file_uri); + header("Content-Type: $file_type"); + + return self::slides_include($file_uri); + } + else + { + throw new Exception("No view controller path found called `$view`"); + } + } + catch ( Exception $e ) + { + print $e->getMessage(); + return 'null'; + } + } +}