Request::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 3
ccs 3
cts 3
cp 1
rs 10
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
/**
4
 * @author Victor Dubiniuk <[email protected]>
5
 *
6
 * @copyright Copyright (c) 2015, ownCloud, Inc.
7
 * @license AGPL-3.0
8
 *
9
 * This code is free software: you can redistribute it and/or modify
10
 * it under the terms of the GNU Affero General Public License, version 3,
11
 * as published by the Free Software Foundation.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
 * GNU Affero General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU Affero General Public License, version 3,
19
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
20
 *
21
 */
22
23
namespace Owncloud\Updater\Http;
24
25
/**
26
 * Class Request
27
 *
28
 * @package Owncloud\Updater\Http
29
 */
30
class Request {
31
	protected $vars;
32
33
	/**
34
	 * Request constructor.
35
	 *
36
	 * @param array $vars
37
	 */
38 18
	public function __construct($vars = []){
39 18
		$this->vars = $vars;
40 18
	}
41
42
	/**
43
	 * Returns the request uri
44
	 * @return string
45
	 */
46
	public function getRequestUri() {
47
		return $this->server('REQUEST_URI');
48
	}
49
50
	/**
51
	 * @return string
52
	 */
53
	public function getServerProtocol() {
54
		$forwardedProto = $this->server('HTTP_X_FORWARDED_PROTO');
55
		if (!is_null($forwardedProto)) {
56
			$proto = strtolower($this->getPartBeforeComma($forwardedProto));
57
			// Verify that the protocol is always HTTP or HTTPS
58
			// default to http if an invalid value is provided
59
			return $proto === 'https' ? 'https' : 'http';
60
		}
61
62
		$isHttps = $this->server('HTTPS');
63
		if ($isHttps !== null
64
			&& $isHttps !== 'off'
65
			&& $isHttps !== ''
66
		) {
67
			return 'https';
68
		}
69
70
		return 'http';
71
	}
72
73
	/**
74
	 * @return mixed|string
75
	 */
76 5
	public function getHost(){
77 5
		$host = 'localhost';
78 5
		$forwardedHost = $this->server('HTTP_X_FORWARDED_HOST');
79 5
		if (!is_null($forwardedHost)) {
80 3
			$host = $this->getPartBeforeComma($forwardedHost);
81
		} else {
82 2
			$httpHost = $this->server('HTTP_HOST');
83 2
			if (is_null($httpHost)) {
84 1
				$serverName = $this->server('SERVER_NAME');
85 1
				if (!is_null($serverName)){
86 1
					$host = $serverName;
87
				}
88
			} else {
89 1
				$host = $httpHost;
90
			}
91
		}
92 5
		return $host;
93
	}
94
95
	/**
96
	 * @param string $name
97
	 * @return mixed
98
	 */
99 4
	public function postParameter($name){
100 4
		return isset($this->vars['post'][$name]) ? $this->vars['post'][$name] : null;
101
	}
102
103
	/**
104
	 * @param string $name
105
	 * @return mixed
106
	 */
107 5
	public function header($name) {
108 5
		$name = strtoupper($name);
109 5
		return $this->server('HTTP_'.$name);
110
	}
111
112
	/**
113
	 * @param string $name
114
	 * @return mixed
115
	 */
116 14
	public function server($name){
117 14
		return isset($this->vars['headers'][$name]) ? $this->vars['headers'][$name] : null;
118
	}
119
120
	/**
121
	 * Return first part before comma or the string itself if there is no comma
122
	 * @param string $str
123
	 * @return string
124
	 */
125 3
	private function getPartBeforeComma($str){
126 3
		if (strpos($str, ',') !== false) {
127 2
			$parts = explode(',', $str);
128 2
			$result = $parts[0];
129
		} else {
130 1
			$result = $str;
131
		}
132 3
		return trim($result);
133
	}
134
135
}
136
137