Skip to content

Commit 5204d01

Browse files
author
MrAnyx
committed
[feat] Added Exception classes and CSRF token class and other things
1 parent e36ea8f commit 5204d01

File tree

5 files changed

+114
-11
lines changed

5 files changed

+114
-11
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace TimePHP\Exception;
4+
5+
class RedirectionException extends \Exception {
6+
7+
public function __construct($message = null, $code = 2000) {
8+
if (!$message) {
9+
throw new $this('Unknown ' . get_class($this));
10+
}
11+
parent::__construct($message, $code);
12+
}
13+
14+
public function __toString(): string {
15+
return __CLASS__ . "[{$this->code}] : {$this->message} at line {$this->line}";
16+
}
17+
}

src/Exception/SessionException.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,16 @@
22

33
namespace TimePHP\Exception;
44

5-
class SessionException extends Exception {
5+
class SessionException extends \Exception {
66

7-
public function __construct($message, $code = "1-000") {
7+
public function __construct($message = null, $code = 1000) {
8+
if (!$message) {
9+
throw new $this('Unknown ' . get_class($this));
10+
}
811
parent::__construct($message, $code);
912
}
1013

11-
// chaîne personnalisée représentant l'objet
12-
public function __toString() {
13-
return __CLASS__ . ": [{$this->code}]: {$this->message}\n";
14+
public function __toString(): string {
15+
return __CLASS__ . "[{$this->code}] : {$this->message} at line {$this->line}";
1416
}
1517
}

src/Foundation/AbstractController.php

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010

1111
use Twig\Environment;
1212
use TimePHP\Foundation\Router;
13+
use TimePHP\Security\CsrfToken;
14+
use TimePHP\Exception\SessionException;
15+
use TimePHP\Exception\RedirectionException;
1316

1417
/**
1518
* @category Controller
@@ -51,4 +54,63 @@ public function generate(string $name, array $params = [], array $flags = []): s
5154
return Router::generate($name, $params, $flags);
5255
}
5356

57+
/**
58+
* Create a session based on parameters
59+
*
60+
* @param array $params
61+
* @return void
62+
*/
63+
public function createSession(array $params): void {
64+
if($params === null || empty($params) || count($params) === 0){
65+
throw new SessionException("createSession function requires an array, null given", 1001);
66+
} else if(count($_SESSION) > 0) {
67+
throw new SessionException('A session has already been started', 1002);
68+
} else {
69+
$_SESSION["csrf_token"] = CsrfToken::generate();
70+
foreach($params as $key => $value){
71+
$_SESSION[$key] = $value;
72+
}
73+
}
74+
}
75+
76+
/**
77+
* Close the current session
78+
*
79+
* @return void
80+
*/
81+
public function closeSession(): void {
82+
if(count($_SESSION) === 0){
83+
throw new SessionException('No session was created. $_SESSION is empty', 1003);
84+
} else {
85+
session_unset();
86+
session_destroy();
87+
}
88+
}
89+
90+
/**
91+
* Redirect to a url using a specific url
92+
*
93+
* @param string $url
94+
* @return void
95+
*/
96+
public function redirectUrl(string $url): void {
97+
header("Location: $url");
98+
}
99+
100+
/**
101+
* Redirect to a url using a route name
102+
*
103+
* @param string $routeName
104+
* @param array $params
105+
* @param array $flags
106+
* @return void
107+
*/
108+
public function redirectRouteName(string $routeName, array $params = [], array $flags = []): void {
109+
if(is_string($routeName)) {
110+
header("Location: {$this->generate($routeName, $params, $flags)}");
111+
} else {
112+
throw new RedirectionException("$routeName doesn't exists");
113+
}
114+
}
115+
54116
}

src/Foundation/Router.php

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,7 @@ public static function generate(string $name, array $params = [], array $flags =
5858
if(count($flags) === 0){
5959
return $url;
6060
} else {
61-
$index = 0;
62-
foreach($flags as $key => $value){
63-
$index === 0 ? $url.="?" : $url.="&";
64-
$url.=$key."=".$value;
65-
$index++;
66-
}
61+
$url .= "?".http_build_query($flags);
6762
return $url;
6863
}
6964
}

src/Security/CsrfToken.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace TimePHP\Security;
4+
5+
class CsrfToken {
6+
7+
/**
8+
* Generate a valid CSRF token
9+
*
10+
* @return string
11+
*/
12+
public static function generate(): string {
13+
return bin2hex(random_bytes(32));
14+
}
15+
16+
/**
17+
* Compare 2 hashes
18+
*
19+
* @param string $sessionToken
20+
* @param string $inputToken
21+
* @return boolean
22+
*/
23+
public static function compare(string $sessionToken, string $inputToken): bool {
24+
return hash_equals($sessionToken, $inputToken);
25+
}
26+
27+
}

0 commit comments

Comments
 (0)