Skip to content

Commit b7de5f7

Browse files
author
François Kooman
committed
include http/rest classes as well in the project
1 parent 8dbe783 commit b7de5f7

16 files changed

+1222
-92
lines changed

.phan/config.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
'src',
66
'web',
77
'bin',
8-
'vendor/fkooman/php-lib-json',
9-
'vendor/fkooman/php-lib-rest',
108
],
119
'exclude_analysis_directory_list' => [
1210
'vendor/',

composer.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
"name": "fkooman/php-voot-provider",
1717
"require": {
1818
"ext-ldap": "*",
19-
"fkooman/php-lib-rest": "0.3.*",
2019
"php": ">=5.4"
2120
}
2221
}

composer.lock

Lines changed: 2 additions & 84 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Http/IncomingRequest.php

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
<?php
2+
3+
/**
4+
* Copyright 2013 François Kooman <[email protected]>.
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+
namespace fkooman\VootProvider\Http;
20+
21+
class IncomingRequest
22+
{
23+
public function __construct()
24+
{
25+
$required_keys = ['SERVER_NAME', 'SERVER_PORT', 'REQUEST_URI', 'REQUEST_METHOD'];
26+
foreach ($required_keys as $r) {
27+
if (!array_key_exists($r, $_SERVER) || empty($_SERVER[$r])) {
28+
throw new IncomingRequestException('missing (one or more) required environment variables');
29+
}
30+
}
31+
}
32+
33+
public function getRequestMethod()
34+
{
35+
return $_SERVER['REQUEST_METHOD'];
36+
}
37+
38+
public function getPathInfo()
39+
{
40+
return array_key_exists('PATH_INFO', $_SERVER) ? $_SERVER['PATH_INFO'] : null;
41+
}
42+
43+
public function getRequestUri()
44+
{
45+
// scheme
46+
$proxy = false;
47+
if (array_key_exists('HTTPS', $_SERVER) && !empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') {
48+
$scheme = 'https';
49+
} elseif (array_key_exists('HTTP_X_FORWARDED_PROTO', $_SERVER) && 'https' === $_SERVER['HTTP_X_FORWARDED_PROTO']) {
50+
// HTTPS to HTTP proxy is present
51+
$scheme = 'https';
52+
$proxy = true;
53+
} else {
54+
$scheme = 'http';
55+
}
56+
57+
// server name
58+
if (filter_var($_SERVER['SERVER_NAME'], FILTER_VALIDATE_IP, FILTER_FLAG_IPV6) === false) {
59+
$name = $_SERVER['SERVER_NAME'];
60+
} else {
61+
$name = '['.$_SERVER['SERVER_NAME'].']';
62+
}
63+
64+
// server port
65+
if (($_SERVER['SERVER_PORT'] === '80' && ($scheme === 'http' || $proxy)) || ($_SERVER['SERVER_PORT'] === '443' && $scheme === 'https')) {
66+
$port = '';
67+
} else {
68+
$port = ':'.$_SERVER['SERVER_PORT'];
69+
}
70+
71+
return $scheme.'://'.$name.$port.$_SERVER['REQUEST_URI'];
72+
}
73+
74+
public function getContent()
75+
{
76+
if ($_SERVER['REQUEST_METHOD'] !== 'POST' && $_SERVER['REQUEST_METHOD'] !== 'PUT') {
77+
return null;
78+
}
79+
if (array_key_exists('CONTENT_LENGTH', $_SERVER) && $_SERVER['CONTENT_LENGTH'] > 0) {
80+
return $this->getRawContent();
81+
}
82+
83+
return null;
84+
}
85+
86+
public function getRawContent()
87+
{
88+
return file_get_contents('php://input');
89+
}
90+
91+
public function getBasicAuthUser()
92+
{
93+
if (array_key_exists('PHP_AUTH_USER', $_SERVER)) {
94+
return $_SERVER['PHP_AUTH_USER'];
95+
}
96+
97+
return null;
98+
}
99+
100+
public function getBasicAuthPass()
101+
{
102+
if (array_key_exists('PHP_AUTH_PW', $_SERVER)) {
103+
return $_SERVER['PHP_AUTH_PW'];
104+
}
105+
106+
return null;
107+
}
108+
109+
public function getRequestHeaders()
110+
{
111+
$requestHeaders = [];
112+
113+
// normalize headers from $_SERVER
114+
foreach ($_SERVER as $k => $v) {
115+
$key = Request::normalizeHeaderKey($k);
116+
$requestHeaders[$key] = $v;
117+
}
118+
119+
// also normalize Apache headers (if available), but do not override
120+
// headers from $_SERVER
121+
if (function_exists('apache_request_headers')) {
122+
$apacheHeaders = apache_request_headers();
123+
foreach ($apacheHeaders as $k => $v) {
124+
$key = Request::normalizeHeaderKey($k);
125+
if (!array_key_exists($key, $requestHeaders)) {
126+
$requestHeaders[$key] = $v;
127+
}
128+
}
129+
}
130+
131+
return $requestHeaders;
132+
}
133+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
/**
4+
* Copyright 2013 François Kooman <[email protected]>.
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+
namespace fkooman\VootProvider\Http;
20+
21+
class IncomingRequestException extends \Exception
22+
{
23+
}

src/Http/JsonResponse.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
/**
4+
* Copyright 2013 François Kooman <[email protected]>.
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+
namespace fkooman\VootProvider\Http;
20+
21+
class JsonResponse extends Response
22+
{
23+
public function __construct($statusCode = 200)
24+
{
25+
parent::__construct($statusCode, 'application/json');
26+
}
27+
28+
/**
29+
* @param mixed $content
30+
*/
31+
public function setContent($content)
32+
{
33+
parent::setContent(json_encode($content));
34+
}
35+
36+
public function getContent()
37+
{
38+
return json_decode(parent::getContent(), true);
39+
}
40+
}

0 commit comments

Comments
 (0)