@@ -201,13 +201,8 @@ public static function authorizePost()
201
201
//set request type to POST
202
202
self ::$ requestType = "POST " ;
203
203
204
- // look for token in header, then in payload
205
- $ token = false ;
206
- if (isset (apache_request_headers ()[self ::$ config ['CSRFP_TOKEN ' ]])) {
207
- $ token = apache_request_headers ()[self ::$ config ['CSRFP_TOKEN ' ]];
208
- } else if (isset ($ _POST [self ::$ config ['CSRFP_TOKEN ' ]])) {
209
- $ token = $ _POST [self ::$ config ['CSRFP_TOKEN ' ]];
210
- }
204
+ // look for token in payload else from header
205
+ $ token = self ::getTokenFromRequest ();
211
206
212
207
//currently for same origin only
213
208
if (!($ token && isset ($ _SESSION [self ::$ config ['CSRFP_TOKEN ' ]])
@@ -234,6 +229,37 @@ public static function authorizePost()
234
229
}
235
230
}
236
231
232
+ /*
233
+ * Fucntion: getTokenFromRequest
234
+ * function to get token in case of POST request
235
+ *
236
+ * Parameters:
237
+ * void
238
+ *
239
+ * Returns:
240
+ * any (string / bool) - token retrieved from header or form payload
241
+ */
242
+ private static function getTokenFromRequest () {
243
+ // look for token in header, then in payload
244
+ if (isset ($ _POST [self ::$ config ['CSRFP_TOKEN ' ]])) {
245
+ return $ _POST [self ::$ config ['CSRFP_TOKEN ' ]];
246
+ }
247
+
248
+ if (function_exists ('apache_request_headers ' )) {
249
+ if (isset (apache_request_headers ()[self ::$ config ['CSRFP_TOKEN ' ]])) {
250
+ return apache_request_headers ()[self ::$ config ['CSRFP_TOKEN ' ]];
251
+ }
252
+ } else {
253
+ $ serverKey = 'HTTP_ ' .strtoupper (self ::$ config ['CSRFP_TOKEN ' ]);
254
+ $ serverKey = str_replace ('- ' , '_ ' , $ serverKey );
255
+ if (isset ($ _SERVER [$ serverKey ])) {
256
+ return $ _SERVER [$ serverKey ];
257
+ }
258
+ }
259
+
260
+ return false ;
261
+ }
262
+
237
263
/*
238
264
* Function: isValidToken
239
265
* function to check the validity of token in session array
0 commit comments