|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Copyright (c) 2018 Justin Kuenzel (jukusoft.com) |
|
5
|
|
|
* |
|
6
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
7
|
|
|
* you may not use this file except in compliance with the License. |
|
8
|
|
|
* You may obtain a copy of the License at |
|
9
|
|
|
* |
|
10
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
|
11
|
|
|
* |
|
12
|
|
|
* Unless required by applicable law or agreed to in writing, software |
|
13
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
|
14
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
15
|
|
|
* See the License for the specific language governing permissions and |
|
16
|
|
|
* limitations under the License. |
|
17
|
|
|
*/ |
|
18
|
|
|
|
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Project: JuKuCMS |
|
22
|
|
|
* License: Apache 2.0 license |
|
23
|
|
|
* User: Justin |
|
24
|
|
|
* Date: 17.04.2018 |
|
25
|
|
|
* Time: 14:28 |
|
26
|
|
|
*/ |
|
27
|
|
|
|
|
28
|
|
|
class Plugin_HTTPAuth_HTTPAuth { |
|
29
|
|
|
|
|
30
|
|
|
public static function headerEvent () { |
|
31
|
|
|
//get preferences first |
|
32
|
|
|
$prefs = new Preferences("plugin_httpauth"); |
|
33
|
|
|
|
|
34
|
|
|
$activated = $prefs->get("activated", false); |
|
35
|
|
|
|
|
36
|
|
|
if (!$activated) { |
|
37
|
|
|
return; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
//check, if user is logged in |
|
41
|
|
|
if (User::current()->isLoggedIn()) { |
|
42
|
|
|
//http auth is not required, because user is already logged in |
|
43
|
|
|
return; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
//check, if credentials was already send |
|
47
|
|
|
if (!isset($_SERVER['PHP_AUTH_USER'])) { |
|
48
|
|
|
self::sendHeader($prefs); |
|
49
|
|
|
} else { |
|
50
|
|
|
echo "<p>Hallo {$_SERVER['PHP_AUTH_USER']}.</p>"; |
|
51
|
|
|
echo "<p>Sie gaben {$_SERVER['PHP_AUTH_PW']} als Passwort ein.</p>"; |
|
52
|
|
|
} |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
protected static function sendHeader (Preferences $prefs) { |
|
56
|
|
|
header('WWW-Authenticate: Basic realm="My Realm"'); |
|
57
|
|
|
header('HTTP/1.0 401 Unauthorized'); |
|
58
|
|
|
|
|
59
|
|
|
//text which will be sended, if user clicks on abort |
|
60
|
|
|
echo $prefs->get("abort_text", "<h1>401 Authorization Required</h1>"); |
|
61
|
|
|
|
|
62
|
|
|
ob_end_flush(); |
|
63
|
|
|
exit; |
|
|
|
|
|
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
?> |
|
|
|
|
|
|
69
|
|
|
|
In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.