Completed
Push — master ( 75018b...9f7060 )
by Radu
01:54
created

RequestServerTrait::getRemoteAddress()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 3
c 2
b 0
f 0
dl 0
loc 7
rs 10
cc 2
nc 2
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace WebServCo\Framework\Traits;
6
7
use WebServCo\Framework\Helpers\PhpHelper;
8
9
trait RequestServerTrait
10
{
11
12
    /**
13
    * @return array<int|string,string>
14
    */
15
    public function getAcceptContentTypes(): array
16
    {
17
        if (!isset($this->server['HTTP_ACCEPT'])) {
18
            return [];
19
        }
20
        $acceptTypes = [];
21
        $httpAccept = \strtolower(\str_replace(' ', '', $this->server['HTTP_ACCEPT']));
22
        $parts = \explode(',', $httpAccept);
23
24
        foreach ($parts as $item) {
25
            $q = 1; // the default quality is 1.
26
            if (\strpos($item, ';q=')) { // check if there is a different quality
27
                // divide "mime/type;q=X" into two parts: "mime/type" i "X"
28
                [$item, $q] = \explode(';q=', $item);
29
            }
30
            // WARNING: $q == 0 means, that mime-type isn’t supported!
31
            $acceptTypes[$q] = $item;
32
        }
33
        \asort($acceptTypes);
34
        return $acceptTypes;
35
    }
36
37
    public function getAcceptLanguage(): string
38
    {
39
        if (!isset($this->server['HTTP_ACCEPT_LANGUAGE'])) {
40
            return '';
41
        }
42
        return \substr($this->server['HTTP_ACCEPT_LANGUAGE'], 0, 2);
43
    }
44
45
    public function getContentType(): string
46
    {
47
        if (!isset($this->server['CONTENT_TYPE'])) {
48
            return '';
49
        }
50
        return $this->server['CONTENT_TYPE'];
51
    }
52
53
    public function getHost(): string
54
    {
55
        if (!empty($this->server['HTTP_HOST'])) {
56
            return $this->server['HTTP_HOST'];
57
        }
58
59
        if (!empty($this->server['SERVER_NAME'])) {
60
            return $this->server['SERVER_NAME'];
61
        }
62
63
        if (!empty($this->server['HOSTNAME'])) {
64
            return $this->server['HOSTNAME']; //CLI
65
        }
66
        return '';
67
    }
68
69
    public function getHostExtension(): string
70
    {
71
        $host = $this->getHost();
72
        if (empty($host)) {
73
            return '';
74
        }
75
76
        $parts = \explode('.', $host);
77
        return \end($parts);
78
    }
79
80
    public function getReferer(): string
81
    {
82
        return $this->server['HTTP_REFERER'] ?? '';
83
    }
84
85
    /**
86
    * Get remote address from current Request object.
87
    *
88
    * For a general helper method see \WebServCo\Framework\Helpers\RequestHelper.getRemoteAddress
89
    */
90
    public function getRemoteAddress(): string
91
    {
92
        if (PhpHelper::isCli()) {
93
            return \gethostbyname(\php_uname('n'));
94
        }
95
96
        return $this->server['REMOTE_ADDR'] ?? '';
97
    }
98
99
    public function getRefererHost(): string
100
    {
101
        return (string) \parse_url($this->getReferer(), \PHP_URL_HOST);
102
    }
103
104
    public function getSchema(): string
105
    {
106
        if (PhpHelper::isCli()) {
107
            return '';
108
        }
109
110
        if (isset($this->server['HTTPS']) && 'off' !== $this->server['HTTPS']) {
111
            return 'https';
112
        }
113
114
        if (isset($this->server['HTTP_X_FORWARDED_PROTO']) && 'https' === $this->server['HTTP_X_FORWARDED_PROTO']) {
115
            return 'https';
116
        }
117
118
        if (isset($this->server['HTTP_X_FORWARDED_SSL']) && 'on' === $this->server['HTTP_X_FORWARDED_SSL']) {
119
            return 'https';
120
        }
121
        return 'http';
122
    }
123
124
    public function getServerProtocol(): string
125
    {
126
        if (!isset($this->server['SERVER_PROTOCOL'])) {
127
            return '';
128
        }
129
        return $this->server['SERVER_PROTOCOL'];
130
    }
131
132
    public function getServerVariable(string $index): ?string
133
    {
134
        if (!\array_key_exists($index, $this->server)) {
135
            throw new \OutOfBoundsException('Requested key does not exist.');
136
        }
137
        return $this->server[$index];
138
    }
139
140
    public function getUserAgent(): string
141
    {
142
        if (PhpHelper::isCli()) {
143
            return '';
144
        }
145
        return $this->server['HTTP_USER_AGENT'] ?? '';
146
    }
147
}
148