Harmonizer::harmonizeHttpAuth()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 20
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 0
loc 20
rs 9.2
cc 4
eloc 15
nc 4
nop 0
1
<?php
2
3
namespace Schnittstabil\Harmonizer;
4
5
/**
6
 * Harmonizer.
7
 */
8
class Harmonizer
9
{
10
    public $server;
11
12
    /**
13
     * Create a new Harmonizer instance.
14
     *
15
     * @param array $server array to infer
16
     */
17
    public function __construct(array &$server)
18
    {
19
        $this->server = &$server;
20
    }
21
22
    /**
23
     * Add entry to an array iff it doesn't exist an entry with the same key.
24
     *
25
     * @param array $array array of entries
26
     * @param mixed $key   key of new entry
27
     * @param mixed $value value of new entry
28
     */
29
    private function arrayAdd(array &$array, $key, $value)
30
    {
31
        if (!isset($array[$key])) {
32
            $array[$key] = $value;
33
        }
34
    }
35
36
    /**
37
     * Infering (in-place) missing REDIRECT_ variables in `$server`.
38
     *
39
     * @return static
40
     */
41
    public function harmonizeRedirectVariables()
42
    {
43
        foreach (array_keys($this->server) as $redirectKey) {
44
            $key = $redirectKey;
45
            while (substr($key, 0, 9) === 'REDIRECT_') {
46
                $key = substr($key, 9);
47
                if (isset($this->server[$key])) {
48
                    $redirectKey = $key;
49
                    continue;
50
                }
51
                $this->server[$key] = $this->server[$redirectKey];
52
            }
53
        }
54
55
        return $this;
56
    }
57
58
    /**
59
     * Infering (in-place) missing user variables in `$server`.
60
     *
61
     * @return static
62
     */
63
    public function harmonizeUserVariables()
64
    {
65
        if (isset($this->server['PHP_AUTH_USER'])) {
66
            $this->arrayAdd($this->server, 'REMOTE_USER', $this->server['PHP_AUTH_USER']);
67
        }
68
        if (isset($this->server['REMOTE_USER'])) {
69
            $this->arrayAdd($this->server, 'PHP_AUTH_USER', $this->server['REMOTE_USER']);
70
        }
71
72
        return $this;
73
    }
74
75
    /**
76
     * Infering (in-place) missing authorization variables in `$server`.
77
     *
78
     * @return static
79
     */
80
    public function harmonizeHttpAuth()
81
    {
82
        if (isset($this->server['HTTP_AUTHORIZATION'])) {
83
            $auth = $this->server['HTTP_AUTHORIZATION'];
84
            if ('Basic ' === substr($auth, 0, 6)) {
85
                list($username, $password) = explode(':', base64_decode(substr($auth, 6)));
86
                $this->arrayAdd($this->server, 'AUTH_TYPE', 'Basic');
87
                $this->arrayAdd($this->server, 'REMOTE_USER', $username);
88
                $this->arrayAdd($this->server, 'PHP_AUTH_USER', $username);
89
                $this->arrayAdd($this->server, 'PHP_AUTH_PW', $password);
90
            } elseif (preg_match('/^Digest .*username="(?P<username>[^"]*)".*$/', $auth, $matches)) {
91
                $this->arrayAdd($this->server, 'AUTH_TYPE', 'Digest');
92
                $this->arrayAdd($this->server, 'PHP_AUTH_DIGEST', substr($auth, 7));
93
                $this->arrayAdd($this->server, 'REMOTE_USER', $matches['username']);
94
                $this->arrayAdd($this->server, 'PHP_AUTH_USER', $matches['username']);
95
            }
96
        }
97
98
        return $this;
99
    }
100
101
    /**
102
     * Infering (in-place) missing variables in `$server`.
103
     *
104
     * @param array $server array to infer
105
     *
106
     * @return array $server
107
     */
108
    public static function harmonize(array &$server)
109
    {
110
        return (new self($server))
111
        ->harmonizeRedirectVariables()
112
        ->harmonizeUserVariables()
113
        ->harmonizeHttpAuth();
114
    }
115
}
116