-
Notifications
You must be signed in to change notification settings - Fork 78
/
web-file-browser.php
208 lines (167 loc) · 5.39 KB
/
web-file-browser.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
<?php
// NOTE: You should be using Composer's global autoloader. But just so these examples
// work for people who don't have Composer, we'll use the library's "autoload.php".
require_once __DIR__.'/../lib/Dropbox/autoload.php';
use \Dropbox as dbx;
$appInfoFile = __DIR__."/web-file-browser.app";
session_start();
$req = $_SERVER['SCRIPT_NAME'];
if ($req == "/") {
$dbxClient = getClient();
if ($dbxClient === false) {
header("Location: /oauth-start");
exit;
}
$path = "/";
if (isset($_GET['path'])) $path = $_GET['path'];
if (isset($_GET['dl'])) {
passFileToBrowser($dbxClient, $path);
}
else {
$entry = $dbxClient->getMetadataWithChildren($path);
if ($entry['is_dir']) {
echo renderFolder($entry);
}
else {
echo renderFile($entry);
}
}
}
else if ($req == "/oauth-start") {
$dbxConfig = getAppConfig();
$webAuth = new dbx\WebAuth($dbxConfig);
$callbackUrl = getBaseUrl()."/oauth_callback";
list($requestToken, $authorizeUrl) = $webAuth->start($callbackUrl);
$_SESSION['requestToken'] = $requestToken->serialize();
header("Location: $authorizeUrl");
exit;
}
else if ($req == "/oauth_callback") {
$dbxConfig = getAppConfig();
$webAuth = new dbx\WebAuth($dbxConfig);
if (isset($_GET['not_approved']) && $_GET['not_approved'] == 'true') {
echo renderHtmlPage("Not Authorized", "This app was not given access to this folder");
exit;
}
if (!isset($_GET['oauth_token'])) {
echo renderHtmlPage("Error", "Dropbox didn't give us an 'oauth_token' parameter.");
exit;
}
$requestToken = dbx\RequestToken::deserialize($_SESSION['requestToken']);
if (!$requestToken->matchesKey($_GET['oauth_token'])) {
echo renderHtmlPage("Error", "Request token mismatch.");
exit;
}
list($accessToken, $dropboxUserId) = $webAuth->finish($requestToken);
$_SESSION['accessToken'] = $accessToken->serialize();
echo renderHtmlPage("Authorized!", "Auth complete, <a href='/'>click here</a> to browse");
exit;
}
else if ($req == "/upload") {
if (empty($_FILES['file']['name'])) {
echo renderHtmlPage("Error", "Please choose a file to upload");
exit;
}
if (!empty($_FILES['file']['error'])) {
echo renderHtmlPage("Error", "Error ".$_FILES['file']['error']." uploading file. See <a href='http://php.net/manual/en/features.file-upload.errors.php'>the docs</a> for details");
exit;
}
$dbxClient = getClient();
$remoteDir = "/";
if (isset($_POST['folder'])) $remoteDir = $_POST['folder'];
$remotePath = rtrim($remoteDir, "/")."/".$_FILES['file']['name'];
$result = $dbxClient->uploadFile($remotePath, dbx\WriteMode::add(), fopen($_FILES['file']['tmp_name'], "rb"));
$str = print_r($result, TRUE);
echo renderHtmlPage("Uploading File", "Result: <pre>$str</pre>");
}
else {
echo renderHtmlPage("Bad URL", "No handler for $req");
exit;
}
function renderFolder($entry)
{
// TODO: Add a token to counter CSRF attacks.
$form = <<<HTML
<form action='/upload' method='post' enctype='multipart/form-data'>
<label for='file'>Upload file:</label> <input name='file' type='file'/>
<input type='submit' value='Upload'/>
<input name='folder' type='hidden' value='$entry[path]'/>
</form>
HTML;
$listing = '';
foreach($entry['contents'] as $child) {
$cp = $child['path'];
$cn = basename($cp);
if ($child['is_dir']) $cn .= '/';
$cp = urlencode($cp);
$listing .= "<div><a style='text-decoration: none' href='/?path=$cp'>$cn</a></div>";
}
return renderHtmlPage("Folder: $entry[path]", $form.$listing);
}
function getAppConfig()
{
global $appInfoFile;
try {
$appInfo = dbx\AppInfo::loadFromJsonFile($appInfoFile);
}
catch (dbx\AppInfoLoadException $ex) {
fwrite(STDERR, "Unable to load \"$appInfoFile\": " . $ex->getMessage() . "\n");
die;
}
$userLocale = null;
$dbxConfig = new dbx\Config($appInfo, "examples-web-file-browser", $userLocale);
return $dbxConfig;
}
function getClient()
{
if(!isset($_SESSION['accessToken'])) {
return false;
}
$dbxConfig = getAppConfig();
try {
$accessToken = dbx\AccessToken::deserialize($_SESSION['accessToken']);
$dbxClient = new dbx\Client($dbxConfig, $accessToken);
}
catch (Exception $e) {
error_log("Error in getClient: ".$e->getMessage());
return false;
}
return $dbxClient;
}
function renderFile($entry)
{
$metadataStr = print_r($entry, TRUE);
$path = urlencode($entry['path']);
$body = <<<HTML
<pre>$metadataStr</pre>
<a href="/?path=$path&dl=true">Download this file</a>
HTML;
return renderHtmlPage( "File: $entry[path]", $body );
}
function passFileToBrowser(dbx\Client $dbxClient, $path)
{
$fd = tmpfile();
$metadata = $dbxClient->getFile($path, $fd);
header("Content-type: $metadata[mime_type]");
fseek($fd, 0);
fpassthru($fd);
fclose($fd);
}
function renderHtmlPage($title, $body)
{
return <<<HTML
<html>
<head>
<title>$title</title>
</head>
<body>
<h1>$title</h1>
$body
</body>
</html>
HTML;
}
function getBaseUrl()
{
return "http://$_SERVER[SERVER_NAME]:$_SERVER[SERVER_PORT]";
}