1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Classes en rapport avec les formulaires |
4
|
|
|
* @author Vermeulen Maxime <[email protected]> |
5
|
|
|
* @version 1.0 |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace BFW; |
9
|
|
|
|
10
|
|
|
use \Exception; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Permet de gérer les formulaire (gestion des tokens) |
14
|
|
|
* @package bfw |
15
|
|
|
*/ |
16
|
|
|
class Form implements \BFWInterface\IForm |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var $_kernel L'instance du Kernel |
20
|
|
|
*/ |
21
|
|
|
protected $_kernel; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var $idForm L'id du formulaire |
25
|
|
|
*/ |
26
|
|
|
protected $idForm; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Constructeur |
30
|
|
|
* |
31
|
|
|
* @param string $idForm L'id du formulaire |
32
|
|
|
*/ |
33
|
|
|
public function __construct($idForm=null) |
34
|
|
|
{ |
35
|
1 |
|
$this->_kernel = getKernel(); |
36
|
|
|
|
37
|
1 |
|
$this->idForm = $idForm; |
38
|
1 |
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Accesseur set sur id_form |
42
|
|
|
* |
43
|
|
|
* @param string $idForm L'id du formulaire |
44
|
|
|
*/ |
45
|
|
|
public function setIdForm($idForm) |
46
|
|
|
{ |
47
|
1 |
|
$this->idForm = $idForm; |
48
|
1 |
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Permet de créer un token pour le formulaire |
52
|
|
|
* |
53
|
|
|
* @return string Le token à mettre dans un champ input de type hidden. |
54
|
|
|
* |
55
|
|
|
* @throws \Exception : Si le nom du formulaire n'est pas définie. |
56
|
|
|
*/ |
57
|
|
|
public function tokenCreate() |
58
|
|
|
{ |
59
|
1 |
|
if(is_null($this->idForm)) {throw new Exception('Form name is undefined.');} |
60
|
|
|
|
61
|
1 |
|
$Id = uniqid(rand(), true); |
62
|
1 |
|
$date = new Date(); |
63
|
|
|
|
64
|
1 |
|
global $_SESSION; |
65
|
1 |
|
$_SESSION['token'][$this->idForm] = array( |
66
|
1 |
|
'token' => $Id, |
67
|
1 |
|
'date' => $date->getDate() |
68
|
1 |
|
); |
69
|
|
|
|
70
|
1 |
|
return $Id; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Permet de vérifier si le token est correct |
75
|
|
|
* |
76
|
|
|
* @return bool True si le toke est bon, false sinon. |
77
|
|
|
*/ |
78
|
|
|
public function tokenVerif() |
79
|
|
|
{ |
80
|
1 |
|
global $_SESSION, $_POST; |
81
|
|
|
|
82
|
1 |
|
if(isset($_SESSION['token']) && is_array($_SESSION['token'])) |
83
|
1 |
|
{ |
84
|
1 |
|
if(isset($_SESSION['token'][$this->idForm]) && is_array($_SESSION['token'][$this->idForm])) |
85
|
1 |
|
{ |
86
|
1 |
|
$token = $_SESSION['token'][$this->idForm]['token']; |
87
|
1 |
|
$date_create = $_SESSION['token'][$this->idForm]['date']; |
88
|
1 |
|
$date_createDT = new Date($date_create); |
89
|
|
|
|
90
|
1 |
|
if(isset($_POST['token']) && $_POST['token'] == $token) |
91
|
1 |
|
{ |
92
|
1 |
|
$date_limit = new Date(); |
93
|
1 |
|
$date_limit->modify('-15 minute'); |
94
|
|
|
|
95
|
1 |
|
if($date_createDT >= $date_limit) |
96
|
1 |
|
{ |
97
|
1 |
|
unset($_SESSION['token'][$this->idForm]); |
98
|
1 |
|
return true; |
99
|
|
|
} |
100
|
1 |
|
} |
101
|
1 |
|
} |
102
|
1 |
|
} |
103
|
|
|
|
104
|
1 |
|
return false; |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|