1
1
<?php
2
2
3
3
use webinterface \main ;
4
+ use webinterface \fileController ;
5
+ use webinterface \authorizeController ;
4
6
5
- session_start ();
6
-
7
- define ('DS ' , DIRECTORY_SEPARATOR , true );
8
- define ('BASE_PATH ' , __DIR__ . DS , TRUE );
9
-
10
-
11
- $ path_vendor = __DIR__ . '/../vendor/autoload.php ' ;
12
- $ path_config = BASE_PATH . '../config/config.php ' ;
13
- $ path_version = BASE_PATH . '../config/version.php ' ;
14
- $ path_message = BASE_PATH . '../config/messages.json ' ;
15
-
16
- if (file_exists ($ path_vendor )) {
17
- require $ path_vendor ;
18
- } else {
19
- echo '<h1><span style="color: #FF0000">Ein Fehler ist aufgetreten.</span></h1><h3>Die Datei "/vendor/autoload.php" konnte nicht gefunden werden.</h3><h3>Führe im Webseiten-Root "composer install" aus!</h3> ' ;
20
- die ();
21
- }
22
-
23
- if (file_exists ($ path_config )) {
24
- require $ path_config ;
25
- } else { die ('<h1><span style="color: #FF0000">Ein Fehler ist aufgetreten.</span></h1><h3>Die Datei "/config/config.php" konnte nicht gefunden werden.</h3><h3>Führe das Setup mit "wisetup" im Master erneut aus!</h3> ' );
26
- }
27
-
28
- if (file_exists ($ path_version )) {
29
- require $ path_version ;
30
- } else {
31
- die ('<h1><span style="color: #FF0000">Ein Fehler ist aufgetreten.</span></h1><h3>Die Datei "/config/version.php" konnte nicht gefunden werden</h3><h3>Führe das Setup mit "wisetup" im Master erneut aus!</h3> ' );
32
- }
33
-
34
- if (!file_exists ($ path_message )) {
35
- die ('<h1><span style="color: #FF0000">Ein Fehler ist aufgetreten.</span></h1><h3>Die Datei "/config/message.json" konnte nicht gefunden werden</h3><h3>Führe das Setup mit "wisetup" im Master erneut aus!</h3> ' );
36
- }
7
+ // require some files
8
+ require __DIR__ . '/../vendor/autoload.php ' ;
9
+ // ensure all necessary files are there
10
+ fileController::dieWhenFileMissing ();
37
11
38
- if (!isset ($ _SESSION ['cn3-wi-csrf ' ])) $ _SESSION ['cn3-wi-csrf ' ] = uniqid ('' , true );
12
+ // files
13
+ require fileController::getVersionFilePath ();
14
+ require fileController::getConfigurationPath ();
39
15
40
- $ main = new webinterface \main ($ config , $ version );
16
+ // create instance of main controller
17
+ $ main = new main ($ config , $ version );
41
18
19
+ // create app route controller
42
20
$ app = System \App::instance ();
43
21
$ app ->request = System \Request::instance ();
44
22
$ app ->route = System \Route::instance ($ app ->request );
45
23
46
- $ route = $ app ->route ;
24
+ // check if we already have a session
25
+ session_start ();
47
26
48
- if (isset ($ _SESSION ['cn3-wi-access_token ' ])){
49
- $ user = main::buildRequest ("auth " , $ _SESSION ['cn3-wi-access_token ' ], "POST " );
50
- if (!$ user ['success ' ]){
51
- unset($ _SESSION ['cn3-wi-access_token ' ]);
52
- header ('Location: ' .main::getUrl ());
27
+ $ app ->route ->any ('/ ' , function () use ($ main ) {
28
+ if (isset ($ _GET ['action ' ]) and $ _GET ['action ' ] == "logout " ) {
29
+ session_unset ();
30
+ header ('Location: ' . strtok ($ _SERVER ["REQUEST_URI " ], '? ' ));
53
31
die ();
54
- }
55
-
56
-
57
- $ route ->any ('/ ' , function () {
58
- if (isset ($ _POST ['action ' ])){
59
- if (isset ($ _POST ['csrf ' ])) {
60
- if ($ _POST ['csrf ' ] != $ _SESSION ['csrf ' ]) {
61
- header ('Location: ' . main::getUrl () . "/?action&success=false&message=csrfFailed " );
62
- die ();
63
- }
64
- } else {
65
- header ('Location: ' . main::getUrl () . "/?action&success=false&message=csrfFailed " );
66
- die ();
67
- }
68
-
69
- if ($ _POST ['action ' ] == "login " AND isset ($ _POST ['username ' ]) AND isset ($ _POST ['password ' ])){
70
- $ action = \webinterface \authorizeController::login ($ _POST ['username ' ], $ _POST ['password ' ]);
71
- if ($ action ['success ' ] == true ) {
72
- header ('Location: ' . main::getUrl ());
73
- } else {
74
- header ('Location: ' . main::getUrl () . "/?action&success=false&message=loginFailed " );
75
- }
76
- die ();
77
- }
32
+ } else if (isset ($ _SESSION ['cn3-wi-access_token ' ])) {
33
+ // try to refresh the session token
34
+ $ result = $ main ::buildDefaultRequest ("session/refresh " );
35
+ if ($ result ['success ' ] === true ) {
36
+ // success, use the updated token and redirect to the dashboard
37
+ $ _SESSION ['cn3-wi-access_token ' ] = $ result ['token ' ];
38
+ redirectToDashboard ();
39
+ return ;
40
+ } else {
41
+ // invalid token in session cache, just clear and run login
42
+ session_unset ();
78
43
}
79
-
80
- include "../pages/header.php " ;
81
- include "../pages/webinterface/index.php " ;
82
- include "../pages/footer.php " ;
83
-
84
- });
85
- } else {
86
- $ route ->any ('/ ' , function () {
87
- if (isset ($ _POST ['action ' ])){
88
- if (isset ($ _POST ['cn3-wi-csrf ' ])) {
89
- if ($ _POST ['cn3-wi-csrf ' ] != $ _SESSION ['cn3-wi-csrf ' ]) {
90
- header ('Location: ' . main::getUrl () . "/?action&success=false&message=csrfFailed " );
91
- die ();
92
- }
93
- } else {
94
- header ('Location: ' . main::getUrl () . "/?action&success=false&message=csrfFailed " );
95
- die ();
96
- }
97
-
98
- if ($ _POST ['action ' ] == "login " AND isset ($ _POST ['username ' ]) AND isset ($ _POST ['password ' ])){
99
- $ action = \webinterface \authorizeController::login ($ _POST ['username ' ], $ _POST ['password ' ]);
100
- if ($ action ['success ' ] == true ) {
101
- header ('Location: ' . main::getUrl ());
102
- } else {
103
- header ('Location: ' . main::getUrl () . "/?action&success=false&message=loginFailed " );
104
- }
105
- die ();
106
- }
44
+ } else if (isset ($ _POST ['action ' ]) and $ _POST ['action ' ] == "login " and isset ($ _POST ['username ' ]) and isset ($ _POST ['password ' ])) {
45
+ $ loginResult = authorizeController::login ($ _POST ['username ' ], $ _POST ['password ' ]);
46
+ if ($ loginResult == LOGIN_RESULT_SUCCESS ) {
47
+ redirectToDashboard ();
48
+ return ;
107
49
}
50
+ }
108
51
109
- include " ../pages/small-header.php " ;
110
- include " ../pages/webinterface/login.php " ;
111
- include " ../pages/footer.php " ;
52
+ displayLoginPage () ;
53
+ }) ;
54
+ $ app -> route -> end () ;
112
55
113
- });
56
+ function displayLoginPage ()
57
+ {
58
+ include "../pages/small-header.php " ;
59
+ include "../pages/webinterface/login.php " ;
60
+ include "../pages/footer.php " ;
114
61
}
115
62
116
-
117
- $ route ->end ();
63
+ function redirectToDashboard ()
64
+ {
65
+ include "../pages/header.php " ;
66
+ include "../pages/webinterface/index.php " ;
67
+ include "../pages/footer.php " ;
68
+ }
0 commit comments