Bernd Wurst commited on 2023-08-05 11:33:04
Zeige 2 geänderte Dateien mit 24 Einfügungen und 8 Löschungen.
... | ... |
@@ -39,7 +39,7 @@ if (isset($_SESSION['kundennr']) and $_SESSION['kundennr']) { |
39 | 39 |
$content .= ' |
40 | 40 |
<form class="form" action="save.php" method="post"> |
41 | 41 |
<input type="hidden" name="form" value="address">'; |
42 |
- $content .= '<input name="fname" type="hidden" value="'.$_SESSION['fname'].'"> |
|
42 |
+ $content .= '<input name="fname" type="hidden" value="'.(isset($_SESSION['fname']) ? $_SESSION['fname'] : '').'"> |
|
43 | 43 |
<input name="lname" type="hidden" value="'.$_SESSION['lname'].'"> |
44 | 44 |
<div class="form-group form-group-lg"> |
45 | 45 |
<div><input class="btn btn-primary btn-lg" type="submit" value="Weiter >"></div> |
... | ... |
@@ -1,12 +1,22 @@ |
1 | 1 |
<?php |
2 | 2 |
|
3 | 3 |
require_once("config.php"); |
4 |
+$base_url = $config['api_url']; |
|
5 |
+ |
|
6 |
+$TOKEN_FILE = '../jwt-token.json'; |
|
7 |
+$jwt = [ |
|
8 |
+ "token" => null, |
|
9 |
+ "refresh_token" => null, |
|
10 |
+ "exp" => time() |
|
11 |
+]; |
|
4 | 12 |
|
5 |
-$jwt_token = null; |
|
6 |
-$jwt_refresh_token = null; |
|
13 |
+if (file_exists($TOKEN_FILE)) { |
|
14 |
+ $jwt = json_decode(file_get_contents($TOKEN_FILE), true); |
|
15 |
+} |
|
7 | 16 |
|
17 |
+if (!$jwt['token'] || $jwt['exp'] < time()+5) { |
|
18 |
+ // get new token |
|
8 | 19 |
# init API |
9 |
-$base_url = $config['api_url']; |
|
10 | 20 |
$url = $base_url . 'login'; |
11 | 21 |
$data = [ |
12 | 22 |
"username" => $config['api_username'], |
... | ... |
@@ -25,12 +35,18 @@ $context = stream_context_create( $options ); |
25 | 35 |
$result = file_get_contents( $url, false, $context ); |
26 | 36 |
$response = json_decode( $result, true); |
27 | 37 |
|
28 |
-$jwt_token = $response["token"]; |
|
29 |
-$jwt_refresh_token = $response["refresh_token"]; |
|
38 |
+ $jwt['token'] = $response["token"]; |
|
39 |
+ $jwt['refresh_token'] = $response["refresh_token"]; |
|
40 |
+ $fields = explode('.', $jwt['token']); |
|
41 |
+ $data = json_decode(base64_decode($fields[1]), true); |
|
42 |
+ $jwt['exp'] = $data['exp']; |
|
43 |
+} |
|
44 |
+ |
|
45 |
+file_put_contents($TOKEN_FILE, json_encode($jwt)); |
|
30 | 46 |
|
31 | 47 |
|
32 | 48 |
function api_call($method, $url, $content = []) { |
33 |
- global $jwt_token; |
|
49 |
+ global $jwt; |
|
34 | 50 |
global $base_url; |
35 | 51 |
$url = $base_url . $url; |
36 | 52 |
$options = array( |
... | ... |
@@ -39,7 +55,7 @@ function api_call($method, $url, $content = []) { |
39 | 55 |
'content' => json_encode( $content ), |
40 | 56 |
'header'=> "Content-Type: application/json\r\n" . |
41 | 57 |
"Accept: application/json\r\n" . |
42 |
- "Authorization: Bearer ".$jwt_token."\r\n" |
|
58 |
+ "Authorization: Bearer ".$jwt['token']."\r\n" |
|
43 | 59 |
) |
44 | 60 |
); |
45 | 61 |
|
46 | 62 |