Skip to content

Commit

Permalink
More function documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
svandragt committed Jan 17, 2024
1 parent 35d3b11 commit e5210e0
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 3 deletions.
28 changes: 26 additions & 2 deletions src/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,27 @@

use Svandragt\Lamb\Response;

/**
* Registers a new route.
*
* @param bool|string $action The action to register. It can be a boolean or a string.
* @param string $callback The callback function to execute when the route is accessed.
* @param mixed ...$args Additional arguments to pass to the callback function.
*
* @return void
*/
function register_route( bool|string $action, string $callback, mixed ...$args ) : void {
global $routes;
$routes[ $action ] = [ $callback, $args ];
}

/**
* Calls the callback function associated with the specified action and returns the result.
*
* @param bool|string $action The action to call the callback function for.
*
* @return array The result of the callback function.
*/
function call_route( bool|string $action ) : array {
global $routes;
[ $callback, $args ] = $routes[ $action ];
Expand All @@ -20,7 +36,15 @@ function call_route( bool|string $action ) : array {
return $callback( $args );
}

function is_reserved_route(string $name) : bool {
/**
* Checks if a given route is reserved.
*
* @param string $name The name of the route to check.
*
* @return bool True if the route is reserved, false otherwise.
*/
function is_reserved_route( string $name ) : bool {
global $routes;
return isset($routes[$name]);

return isset( $routes[ $name ] );
}
28 changes: 27 additions & 1 deletion src/security.php
Original file line number Diff line number Diff line change
@@ -1,17 +1,43 @@
<?php

/**
* Checks if a user is logged in. If not, it redirects to the login page
*
* @since 1.0.0
*/

namespace Svandragt\Lamb\Security;

use Svandragt\Lamb\Response;
/**
* Class Response
*
* Represents a response returned by a controller action.
*/

use Svandragt\Lamb\Response;

# Security
/**
* Checks if the user is logged in.
*
* @return void
*
* If the user is not logged in, a flash message "Please login" is added to the session and the user is redirected to the login page.
*/
function require_login() : void {
if ( ! isset( $_SESSION[ SESSION_LOGIN ] ) ) {
$_SESSION['flash'][] = "Please login";
Response\redirect_uri( "/login" );
}
}

/**
* Checks if the CSRF token in the POST request matches the token stored in the session.
* If the tokens don't match, sends a 405 Method Not Allowed response and terminates the script.
* After successful validation, removes the CSRF token from the session.
*
* @return void
*/
function require_csrf() : void {
$token = htmlspecialchars( $_POST[ HIDDEN_CSRF_NAME ] );
$csrf = $_SESSION[ HIDDEN_CSRF_NAME ] ?? null;
Expand Down

0 comments on commit e5210e0

Please sign in to comment.