-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathproxyRequest.php
143 lines (120 loc) · 4.07 KB
/
proxyRequest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
<?php
require_once './config.php'; //library credentials
require_once './build/libZoteroSingle.php';
$library = new Zotero_Library($libraryType, $libraryID, $librarySlug, $apiKey);
$requestUrl = isset($_GET["requestUrl"]) ? $_GET["requestUrl"] : false;
$requestQueryParams = isset($_GET['requestQueryParams']) ? $_GET['requestQueryParams'] : false;
//limit requestUrl to zotero.org
if(strpos($requestUrl, 'zotero.org') === false){
die;
}
//act as transparent proxy until JS lib can make requests directly to api
$requestMethod = $_SERVER['REQUEST_METHOD'];
//raw body of the request
$rawbody = @file_get_contents('php://input');
$passedIfMatch = getHeader('If-Match');
$headers = array();
if($passedIfMatch){
$headers['If-Match'] = $passedIfMatch;
}
$response = $library->proxyHttpRequest($requestUrl, strtoupper($requestMethod), $rawbody, $headers);
//take the http response we got and send all of it along
$httpHeaders = $response->getHeaders();
$statusSent = false;
foreach($httpHeaders as $key=>$val){
$headerText = $key . ': ' . $val;
if(!$statusSent){
header($headerText, true, $response->getStatus());
$statusSent = true;
}
else{
header($headerText);
}
}
echo $response->getBody();
//getHeader function from Zend_Controller_Request_Http
function getHeader($header){
// Try to get it from the $_SERVER array first
$temp = 'HTTP_' . strtoupper(str_replace('-', '_', $header));
if (isset($_SERVER[$temp])) {
return $_SERVER[$temp];
}
// This seems to be the only way to get the Authorization header on
// Apache
if (function_exists('apache_request_headers')) {
$headers = apache_request_headers();
if (isset($headers[$header])) {
return $headers[$header];
}
$header = strtolower($header);
foreach ($headers as $key => $value) {
if (strtolower($key) == $header) {
return $value;
}
}
}
return false;
}
function parsePathVars($basePath, $pathname = null) {
//parse variables out of library urls
//:userslug/items/:itemKey/*
//:userslug/items/collection/:collectionKey
//groups/:groupidentifier/items/:itemKey/*
//groups/:groupidentifier/items/collection/:collectionKey/*
if(!$pathname){
$pathname = $this->getRequest()->getRequestUri();
}
$replaced = trim(str_replace($basePath, '', $pathname), '/');
$split_replaced = explode('/', $replaced);
$pathVars = array();
if(count($split_replaced) == 1) return $pathVars;
for($i = 0; $i < count($split_replaced); $i = $i + 2){
$pathVar = isset($pathVars[$split_replaced[$i]]) ? $pathVars[$split_replaced[$i]] : null;
//if var already present change to array and/or push
if($pathVar){
if(is_array($pathVar)){
array_push($pathVar, $split_replaced[$i+1]);
}
else{
$ar = array($pathVar);
array_push($ar, $split_replaced[$i+1]);
$pathVar = $ar;
}
}
//otherwise just set the value in the object
else{
$pathVar = $split_replaced[$i+1];
}
$pathVars[$split_replaced[$i]] = $pathVar;
}
return $pathVars;
}
function proper_parse_str($str) {
# result array
$arr = array();
# split on outer delimiter
$pairs = explode('&', $str);
# loop through each pair
foreach ($pairs as $i) {
# split into name and value
list($name,$value) = explode('=', $i, 2);
$name = urldecode($name);
$value = urldecode($value);
# if name already exists
if( isset($arr[$name]) ) {
# stick multiple values into an array
if( is_array($arr[$name]) ) {
$arr[$name][] = $value;
}
else {
$arr[$name] = array($arr[$name], $value);
}
}
# otherwise, simply stick it in a scalar
else {
$arr[$name] = $value;
}
}
# return result array
return $arr;
}