1+ <?php
2+
3+ /**
4+ * Class csrf
5+ *
6+ * @author Baha Şener
7+ * @mail baha.sener@hotmail.com
8+ * @date 8 December 2022
9+ */
10+
11+ class Csrf{
12+
13+ public $ config ;
14+
15+ public function __construct ($ config ){
16+
17+ $ this ->config = $ config ;
18+
19+ if (!in_array ('openssl ' , get_loaded_extensions ())){
20+ $ this ->showError ('OpenSSL extension must be installed in PHP. ' );
21+ die ();
22+ }
23+
24+ if (!isset ($ this ->config ['key ' ]) || empty ($ this ->config ['key ' ])){
25+ $ this ->showError ('You must specify a key value with the configuration. ' );
26+ die ();
27+ }
28+
29+ if (!isset ($ this ->config ['secret ' ]) || empty ($ this ->config ['secret ' ])){
30+ $ this ->showError ('You must specify a secret value with the configuration. ' );
31+ die ();
32+ }
33+
34+ if (empty ($ _SESSION ['_csrf ' ])){
35+ $ _SESSION ['_csrf ' ] = $ this ->EncryptToken (bin2hex (random_bytes (32 )));
36+ }
37+
38+ }
39+
40+ private function EncryptToken ($ data ){
41+
42+ if (isset ($ data )){
43+
44+ $ key = hash ('sha256 ' , $ this ->config ['key ' ]);
45+ $ iv = substr (hash ('sha256 ' , $ this ->config ['secret ' ]), 0 , 16 );
46+ $ encrypt = openssl_encrypt ($ data , 'AES-128-CBC ' , $ key , 0 , $ iv );
47+ return $ encrypt ;
48+
49+ }
50+
51+ }
52+
53+ public function Get (){
54+ return $ _SESSION ['_csrf ' ];
55+ }
56+
57+ public function Check ($ token ){
58+
59+ $ userToken = $ this ->EncryptToken ($ token );
60+ $ systemToken = $ this ->EncryptToken ($ _SESSION ['_csrf ' ]);
61+
62+ if (hash_equals ($ userToken , $ systemToken )){
63+ unset($ _SESSION ['_csrf ' ]);
64+ return true ;
65+ }else {
66+ return false ;
67+ }
68+
69+ }
70+
71+ public function Reset (){
72+
73+ if (isset ($ _SESSION ['_csrf ' ])){
74+
75+ unset($ _SESSION ['_csrf ' ]);
76+
77+ if (!isset ($ _SESSION ['_csrf ' ])){
78+
79+ $ _SESSION ['_csrf ' ] = $ this ->EncryptToken (bin2hex (random_bytes (32 )));
80+ if (isset ($ _SESSION ['_csrf ' ])){
81+ return true ;
82+ }else {
83+ return false ;
84+ }
85+
86+ }
87+
88+ }else {
89+
90+ if (empty ($ _SESSION ['_csrf ' ])){
91+
92+ $ _SESSION ['_csrf ' ] = $ this ->EncryptToken (bin2hex (random_bytes (32 )));
93+ if (isset ($ _SESSION ['_csrf ' ])){
94+ return true ;
95+ }else {
96+ return false ;
97+ }
98+
99+ }else {
100+ return false ;
101+ }
102+
103+ }
104+
105+ }
106+
107+ private function DecryptToken ($ data ){
108+
109+ if (isset ($ data )){
110+
111+ $ key = hash ('sha256 ' , $ this ->config ['key ' ]);
112+ $ iv = substr (hash ('sha256 ' , $ this ->config ['secret ' ]), 0 , 16 );
113+ return openssl_decrypt ($ data , 'AES-128-CBC ' , $ key , 0 , $ iv );
114+
115+ }
116+
117+ }
118+
119+ private function showError ($ error ){
120+ $ this ->errorTemplate ($ error );
121+ }
122+
123+ private function errorTemplate ($ errorMsg , $ title = null )
124+ {
125+ ?>
126+ <div class="php-encryption-error-msg-content">
127+ <div class="php-encryption-error-title">
128+ <?= $ title ? $ title : __CLASS__ . ' Error: ' ?>
129+ </div>
130+ <div class="php-encryption-error-msg"><?= $ errorMsg ?> </div>
131+ </div>
132+ <style>
133+ .php-encryption-error-msg-content {
134+ padding: 15px;
135+ border-left: 5px solid #c00000;
136+ background: rgba(192, 0, 0, 0.06);
137+ background: #f8f8f8;
138+ margin-bottom: 10px;
139+ }
140+
141+ .php-encryption-error-title {
142+ font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
143+ font-size: 16px;
144+ font-weight: 500;
145+ }
146+
147+ .php-encryption-error-msg {
148+ margin-top: 15px;
149+ font-size: 14px;
150+ font-family: Consolas, Monaco, Menlo, Lucida Console, Liberation Mono, DejaVu Sans Mono, Bitstream Vera Sans Mono, Courier New, monospace, sans-serif;
151+ color: #c00000;
152+ }
153+ </style>
154+ <?php
155+ }
156+
157+ }
0 commit comments