This repository has been archived by the owner on Jul 1, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsilo.php
215 lines (175 loc) · 5.19 KB
/
silo.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
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
<?php
/*
Silo -- a simple, general purpose file system for LSL via HTTP
version 2006-07-09-beta
by Zero Linden
Copyright (c) 2006 Linden Lab
Licensed under the "MIT" open source license.
This file is only part of the whole distribution.
silo.php -- the main impelmentation
*/
header('Content-Type: text/plain;charset=utf-8');
$hexPat = "[0-9a-f]";
$keyPat = "$hexPat{8}-$hexPat{4}-$hexPat{4}-$hexPat{4}-$hexPat{12}";
$partPat = "[-+_0-9a-z%]+";
$firstPat = $partPat;
//$firstPat = $keyPat;
// set to $keyPat to allow only paths starting with keys
$pathPattern = <<<END
{^
/
($firstPat)
((?:
/
$partPat
){0,10})
(/?)
$}ix
END;
$filePattern = "{^($partPat)(\\.data|\\.meta)?$}i";
function parsePath($path)
{
global $pathPattern, $hexPat;
if (!preg_match($pathPattern, $path, $parts)) {
httpError(400, "Bad Request", "pattern failure in: $path");
}
$first = strtolower($parts[1]);
$middle = strtolower($parts[2]);
$isDir = $parts[3] == "/";
$first = preg_replace('/(..)(?=.)/', '$1/', $first, 2);
// spread the first path part across two directory levels
return array(
'isDir' => $isDir,
'translatedPath' => "data/$first$middle"
);
}
function httpError($status, $message, $detail = "while processing")
{
global $pathInfo;
header("HTTP/1.0 $status $message");
echo $detail, ": ", $pathInfo, "\n";
exit;
}
function mkdirAsNeeded($path, $mode = 0777)
{
$oldMask = umask(0);
$pathSoFar = '';
foreach (explode('/', $path) as $part) {
if (!$part) continue;
$pathSoFar .= $part;
@mkdir($pathSoFar, $mode);
$pathSoFar .= '/';
}
umask($oldMask);
}
function rmdirAndContents($dir)
{
global $filePattern;
$h = @opendir($dir);
if (!$h) { return; }
while (($name = readdir($h)) !== false) {
if (preg_match($filePattern, $name)) {
$item = $dir . "/" . $name;
if (is_dir($item)) rmdirAndContents($item);
else @unlink($item);
}
}
closedir($h);
@rmdir($dir);
}
function getFile($dataFile, $metaFile)
{
if (!is_readable($dataFile)) {
httpError(404, "Not Found", "no data for");
}
$replayedHeaderLines = "{^(Content-Type)\\s*:}i";
foreach (file($metaFile) as $line) {
if (preg_match($replayedHeaderLines, $line)) {
header($line);
}
}
copy($dataFile, "php://output");
}
function putFile($dataFile, $metaFile)
{
mkdirAsNeeded(dirname($dataFile));
$preexists = file_exists($dataFile);
copy("php://input", $dataFile);
if (!is_writable($dataFile)) {
httpError(403, "Forbidden", "can't modify");
}
$storedHeaders = "{^(Content-Type|X-SecondLife-\\S*)\\s*$}i";
$h = fopen($metaFile, "w");
foreach (apache_request_headers() as $header => $value) {
if (preg_match($storedHeaders, $header, $matches)) {
fwrite($h, strtolower($matches[1]) . ": $value\n");
}
}
fclose($h);
if (!$preexists) {
header("HTTP/1.0 201 Created");
}
}
function delFile($dataFile, $metaFile)
{
if (!unlink($dataFile) || !unlink($metaFile)) {
if (is_file($dataFile) || is_file($metaFile)) {
httpError(403, "Forbidden", "can't modify");
}
}
}
function getDir($dirPath)
{
global $filePattern;
if (!is_dir($dirPath)) {
httpError(404, "Not Found", "no data at");
}
$h = opendir($dirPath);
if (!$h) {
httpError(403, "Forbidden", "can't read");
}
$files = array();
while (($f = readdir($h)) !== false) {
if (preg_match($filePattern, $f, $m)) {
$files[] = $m[1];
}
}
$files = array_unique($files);
sort($files); // some versions of PHP's array_unique don't always sort!
foreach ($files as $f) {
echo $f, "\n";
}
}
function delDir($dirPath)
{
rmdirAndContents($dirPath);
if (is_dir($dirPath)) {
httpError(403, "Forbidden", "can't modify");
}
}
$requestURI = $_SERVER['REQUEST_URI'];
$scriptName = $_SERVER['SCRIPT_NAME'];
$pathInfo = strtolower(substr($requestURI, strlen($scriptName)));
// Note: not using $_SERVER['PATH_INFO'] as it is URL decoded
$method = $_SERVER['REQUEST_METHOD'];
if (version_compare(PHP_VERSION, "4.3.0", "<")) {
header("HTTP/1.0 500 Internal Server Error");
echo "PHP version incompatible: ", PHP_VERSION, "\n";
exit;
}
$parse = parsePath($pathInfo);
if (! $parse['isDir']) {
$dataFile = $parse['translatedPath'] . ".data";
$metaFile = $parse['translatedPath'] . ".meta";
if ($method == 'GET') getFile($dataFile, $metaFile);
elseif ($method == 'PUT') putFile($dataFile, $metaFile);
elseif ($method == 'DELETE') delFile($dataFile, $metaFile);
else httpError(405, "Method Not Allowed");
}
else {
$dirPath = $parse['translatedPath'];
if ($method == 'GET') getDir($dirPath);
elseif ($method == 'DELETE') delDir($dirPath);
else httpError(405, "Method Not Allowed");
}
?>