-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproxy.php
More file actions
252 lines (204 loc) · 6.72 KB
/
Copy pathproxy.php
File metadata and controls
252 lines (204 loc) · 6.72 KB
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
<?php
/*
* adapted from http://www.snip2code.com/Snippet/30795/Simple-PHP-Proxy-Script/
*/
// todo this is added to every file. it need to be added to a custom php.ini file
ini_set('include_path', './' . PATH_SEPARATOR . '../' . PATH_SEPARATOR . ini_get('include_path'));
require_once('Template.php');
require_once('include/config.php');
if ($_GET && $_GET['relUrl']) {
$url = getFullUrlFromParams($_GET['relUrl']);
$res = doCurlRequest($url);
if (strpos($_GET['relUrl'], '.htm') !== false) {
parseLatestHtmlResults($res);
} else {
parseIndexHtmlResults($res);
}
}
function getFullUrlFromParams($val) {
$fullUrlArray = array(
constant('BASEURL'),
urldecode($val)
);
return implode("/", array_filter($fullUrlArray));
}
function parseLatestHtmlResults($res) {
$debug = '';
$error = '';
if (constant('DEBUG') == 1) {
$debug = "<code>\n";
$debug .= nl2br(htmlspecialchars($res)) . "\n\n";
$debug .= "</code><BR><BR>\n\n\n";
}
// find the image
preg_match('/src=(.*?)>/', $res, $imgFilename);
if (strlen($imgFilename[1]) > 0) {
$urlArray = array(
constant('BASEURL'),
getRelativeFolderPath(),
$imgFilename[1]
);
$imageUrl = implode("/", array_filter($urlArray));
} else {
$urlArray = array(
constant('BASEURL'),
getRelativeFolderPath(),
$imgFilename[1]
);
$error = "Graph not found at: " . implode("/", array_filter($urlArray));
}
array_pop($urlArray);
$baseFolderUrl = implode("/", array_filter($urlArray)) . "/";
$resArray = explode("\n", $res);
// find the links to the nearest files
foreach ($resArray as $line) {
if (preg_match('/PREV/', $line)) {
preg_match('/href=(.*?)>/', $line, $previous);
}
if (preg_match('/NEXT/', $line)) {
preg_match('/href=(.*?)>/', $line, $next);
}
}
$pathArray = getPathArray();
$guessedImageHtmlPageName = $pathArray[count($pathArray)-1];
$data = array(
'debug' => $debug,
'relativeFolderPath' => getRelativeFolderPath(),
'regionalMap' => isRegionalMap(),
'imageUrl' => $imageUrl,
'previous' => $previous[1],
'current' => $guessedImageHtmlPageName,
'next' => $next[1],
'imgNameDate' => formatFilenameAsDate($imgFilename[1]),
'error' => $error,
'popup' => isset($_GET['popup']) ? 'true' : 'false',
'baseFolderUrl' => $baseFolderUrl,
'datePickerUrl' => getRelativeFolderPath()
);
$dataAsQueryString = http_build_query($data);
$data['permlink'] = $dataAsQueryString;
$tmpl = new Template('views/proxyMapPagesTpl.php', $data);
echo $tmpl->render();
}
function parseIndexHtmlResults($res) {
$debug = '';
if (constant('DEBUG') == 1) {
$debug = "<code>\n";
$debug .= nl2br(htmlspecialchars($res)) . "\n\n";
$debug .= "</code><BR><BR>\n\n\n";
}
// fix this apparent closing tag EG: <a href=2012/>[2012]</a>
// as they break strip_tags
$res = str_replace("/>", ">", $res);
// limit content to anchors and bold tags to clear crud
$res = strip_tags($res, "<a><b>");
// make bold tags into H3's
$res = str_replace("b>", "h3>", $res);
// now remove lines that ...
$endOfLine = "\n";
$lines = explode($endOfLine, $res);
$array[] = null;
foreach ($lines as $string) {
// have a reference to index.htm
if (preg_match('/index/i', $string)) {
if (!preg_match('/oceancurrent/i', $string)) {
$todaysYear = date("Y");
$string = "<a href=\"../\" >[$todaysYear]</a>";
}
else {
continue;
}
}
// have a reference to oceancurrent
if (preg_match('/oceancurrent/i', $string)) {
continue;
}
// have an unwanted title
if (preg_match('/----/i', $string)) {
continue;
}
// not nested in a tag. (strip_tags leaves content orphaned)
if (!preg_match("%<.*?>%", $string)) {
continue;
}
array_push($array, $string);
}
$res = implode($endOfLine, $array);
$data = array(
'debug' => $debug,
'relativeFolderPath' => getRelativeFolderPath(),
'html' => $res
);
$tmpl = new Template('views/proxyIndexHtmlTpl.php', $data);
echo $tmpl->render();
}
function getRelativeFolderPath() {
$pathArray = getPathArray();
//if the last item is a file strip it off
$filename = $pathArray[count($pathArray)-1];
if (pathinfo($filename, PATHINFO_EXTENSION)) {
array_pop($pathArray);
}
return implode("/", $pathArray);
}
function getPathArray() {
return explode("/", urldecode($_GET['relUrl']));
}
function isRegionalMap() {
return (isset($_GET['regionalMap']));
}
function formatFilenameAsDate($filename) {
$possibleDate = stripExtension($filename);
$inputDateFormats = array(
"YmdG",
"Ymd"
);
$outputDateFormats = array(
"%s/%s/%s %s:00 Z",
"%s/%s/%s"
); // formats used by David
$len = count($inputDateFormats);
for ($i = 0; $i < $len; $i++) {
$dateArray = date_parse_from_format($inputDateFormats[$i], $possibleDate);
$outputDateFormat = $outputDateFormats[$i];
if ($dateArray['error_count'] == 0) {
$i = $len;
}
}
if ($dateArray['error_count'] == 0) {
$res = sprintf($outputDateFormat, $dateArray['year'], $dateArray['month'], $dateArray['day'], $dateArray['hour']);
return $res;
}
}
function doCurlRequest($url) {
$headers = getallheaders();
$headers_str = array();
$method = $_SERVER['REQUEST_METHOD'];
foreach ($headers as $key => $value) {
if ($key == 'Host') continue;
$headers_str[] = $key . ":" . $value;
}
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_URL, $url);
if ($method == "POST") {
$data_str = file_get_contents('php://input');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_str);
}
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
header('Content-Type: text/html');
$result = curl_exec($ch);
// Check if any error occurred
if (curl_errno($ch)) {
//$info = curl_getinfo($ch);
} else {
return $result;
}
curl_close($ch);
}
function stripExtension($filename) {
//http://stackoverflow.com/questions/2395882/how-to-remove-extension-from-string-only-real-extension
return preg_replace('/\\.[^.\\s]{2,3}$/', '', $filename);
}
?>