Test Failed
Push — master ( 9a8fb8...31dcf5 )
by Justin
09:33 queued 05:15
created

Plugin_HTTPAuth_HTTPAuth::headerEvent()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 22
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 22
rs 8.9197
cc 4
eloc 11
nc 4
nop 0
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;
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
64
	}
65
66
}
67
68
?>
0 ignored issues
show
Best Practice introduced by
It is not recommended to use PHP's closing tag ?> in files other than templates.

Using a closing tag in PHP files that only contain PHP code is not recommended as you might accidentally add whitespace after the closing tag which would then be output by PHP. This can cause severe problems, for example headers cannot be sent anymore.

A simple precaution is to leave off the closing tag as it is not required, and it also has no negative effects whatsoever.

Loading history...
69