Passed
Push — master ( 392768...c278a9 )
by Radu
01:29
created

RequestServerTrait::getServerVariable()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
namespace WebServCo\Framework\Traits;
3
4
use WebServCo\Framework\Framework;
5
6
trait RequestServerTrait
7
{
8
    public function getAcceptContentTypes()
9
    {
10
        if (!isset($this->server['HTTP_ACCEPT'])) {
11
            return false;
12
        }
13
        $acceptTypes = [];
14
        $httpAccept = strtolower(str_replace(' ', '', $this->server['HTTP_ACCEPT']));
15
        $parts = explode(',', $httpAccept);
16
17
        foreach ($parts as $item) {
18
            $q = 1; // the default quality is 1.
19
            if (strpos($item, ';q=')) { // check if there is a different quality
20
                // divide "mime/type;q=X" into two parts: "mime/type" i "X"
21
                list($item, $q) = explode(';q=', $item);
22
            }
23
            // WARNING: $q == 0 means, that mime-type isn’t supported!
24
            $acceptTypes[$q] = $item;
25
        }
26
        asort($acceptTypes);
27
        return $acceptTypes;
28
    }
29
30
    public function getAcceptLanguage()
31
    {
32
        if (!isset($this->server['HTTP_ACCEPT_LANGUAGE'])) {
33
            return false;
34
        }
35
        return substr($this->server['HTTP_ACCEPT_LANGUAGE'], 0, 2);
36
    }
37
38
    public function getHost()
39
    {
40
        if (!empty($this->server['HTTP_HOST'])) {
41
            return $this->server['HTTP_HOST'];
42
        } elseif (!empty($this->server['SERVER_NAME'])) {
43
            return $this->server['SERVER_NAME'];
44
        } elseif (!empty($this->server['HOSTNAME'])) {
45
            return $this->server['HOSTNAME']; //CLI
46
        }
47
        return '';
48
    }
49
50
    public function getHostExtension()
51
    {
52
        $host = $this->getHost();
53
        if (empty($host)) {
54
            return false;
55
        }
56
57
        $parts = explode('.', $host);
58
        return end($parts);
59
    }
60
61
    public function getReferer()
62
    {
63
        return isset($this->server['HTTP_REFERER']) ? $this->server['HTTP_REFERER'] : null;
64
    }
65
66
    public function getSchema()
67
    {
68
        if (Framework::isCLI()) {
69
            return null;
70
        }
71
72
        if (isset($this->server['HTTPS']) && 'off' != $this->server['HTTPS']) {
73
            return 'https';
74
        } elseif (isset($this->server['HTTP_X_FORWARDED_PROTO']) &&
75
        'https' == $this->server['HTTP_X_FORWARDED_PROTO']) {
76
            return 'https';
77
        } elseif (isset($this->server['HTTP_X_FORWARDED_SSL']) &&
78
        'on' == $this->server['HTTP_X_FORWARDED_SSL']) {
79
            return 'https';
80
        }
81
        return 'http';
82
    }
83
84
    public function getServerProtocol()
85
    {
86
        if (!isset($this->server['SERVER_PROTOCOL'])) {
87
            return false;
88
        }
89
        return $this->server['SERVER_PROTOCOL'];
90
    }
91
92
    public function getRemoteAddress()
93
    {
94
        if (Framework::isCLI()) {
95
            return gethostbyname(php_uname('n'));
96
        }
97
        return isset($this->server['REMOTE_ADDR']) ? $this->server['REMOTE_ADDR'] : false;
98
    }
99
100
    public function getServerVariable($index)
101
    {
102
        if (!array_key_exists($index, $this->server)) {
103
            throw new \OutOfBoundsException('Requested key does not exist');
104
        }
105
        return $this->server[$index];
106
    }
107
}
108