/
api.php
executable file
·166 lines (156 loc) · 5.43 KB
/
api.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
<?php
//
// a simple PHP filebrowser API
//
// there is an hardcoded $BASE_PATH that should be the jail
// all commands are relative to this root
// the upload commands are located in upload.php
//
// commands :
//
// * GET
// * cmd=view&file=path/to/file.jpg : diplay/download arbitraty file
//
// * POST
// * cmd=get&path=a/nother/path : directory listing
// * cmd=newdir&dir=a/nother/path : creates a directory
// * cmd=rename&oldname=path/to/old.txt&newname=path/to/new.jpg : renames a file or directory
// * cmd=delete&file=a/nother/file.jpeg : deletes a file or directory
//
header("Content-Type:text/javascript");
require('utils.php');
require('config.php');
// set to GET for testing purposes, should be POST by default (security)
$DATAS=$_POST;
if (isset($DATAS['cmd'])) $xaction = $DATAS['cmd'];
if (isset($_GET['cmd'])) {
if ($_GET['cmd']=='view' || $_GET['cmd']=='upload' || $_GET['cmd']=='download') {
// special case for cmd=view which always uses GET (view/download)
// special case for upload (cannot send in POST along with HTML5 D'n'd
$xaction = $_GET['cmd'];
}
}
// the buildPath function is responsible of security
switch ($xaction) {
case 'get':
// get directory listing
$destfile = $DATAS['path'];
$target = buildPath($BASE_PATH, $destfile);
// print $target;
if (!is_dir( $target )) {
$json = array();
}
else {
$json = getDirListing( $target );
}
print json_encode($json);
break;
case 'newdir':
// create new dir
$destfile = $DATAS['path'];
checkVar( $destfile );
$target = buildPath($BASE_PATH, $destfile);
if (!@mkdir( $target )) {
print jsonResponse(false, 'cannot create directory');
break;
}
print jsonResponse();
break;
case 'rename':
// rename file/dir
$old = buildPath($BASE_PATH, $DATAS['oldname']);
$new = buildPath($BASE_PATH, $DATAS['newname']);
// we can only rename to a valid filetype
checkFileExtension( $new );
if (!@rename( $old, $new)) {
print jsonResponse(false, 'cannot rename');
break;
}
print jsonResponse();
break;
case 'delete':
// delete file/dir
$destfile = $DATAS['file'];
checkVar( $destfile );
$target = buildPath($BASE_PATH, $destfile);
if (!file_exists( $target )) die('Fichier inexistant');
if (is_dir( $target ))
if (!@rmdir( $target )) {
print jsonResponse(false, 'cannot delete');
break;
}
else if (file_exists( $target ))
if (!@unlink( $target )) {
print jsonResponse(false, 'cannot delete');
break;
}
print jsonResponse(file_exists( $target )?false:true);
break;
case 'view':
// download target file (using GET)
$destfile = $_GET['file'];
checkVar( $destfile );
$target = buildPath($BASE_PATH, $destfile);
if (!file_exists( $target )) {
print jsonResponse(false, 'file does not exists');
break;
}
$hd = fopen($target, "rb");
$ext = strtolower(substr(strrchr($destfile, '.'), 1));
header('Content-Disposition: inline; filename="'. $destfile . '"');
header ('Content-type: image/'.$ext);
header('Content-Length: ' . filesize($target));
while (!feof($hd)) {
print fread($hd, 4096);
flush();
}
fclose($hd);
break;
case 'download':
// download target file (using GET)
$destfile = $_GET['file'];
checkVar( $destfile );
$target = buildPath($BASE_PATH, $destfile);
if (!file_exists( $target )) {
print jsonResponse(false, 'file does not exists');
break;
}
$hd = fopen($target, "rb");
$ext = strtolower(substr(strrchr($destfile, '.'), 1));
header('Content-Type: application/force-download');
header('Content-Disposition: attachment; filename='.basename($destfile));
//header('Content-Disposition: inline; filename="'. $destfile . '"');
// header ('Content-type: image/'.$ext);
header('Content-Length: ' . filesize($target));
while (!feof($hd)) {
print fread($hd, 4096);
flush();
}
fclose($hd);
break;
case 'upload':
if (!strlen($_SERVER['HTTP_X_FILE_NAME'])) {
// classic upload
foreach($_FILES as $file) {
$destfile = $file['name'];
checkVar( $destfile );
$target = buildPath($BASE_PATH, $destfile);
checkFileExtension( $target );
if (move_uploaded_file($file['tmp_name'], $target)) $success = true;
}
} else {
// HTML5 single file upload
$destfile = $_SERVER['HTTP_X_FILE_NAME'];
checkVar( $destfile );
$target = buildPath($BASE_PATH, $destfile);
checkFileExtension( $target );
if (!@file_put_contents($target, file_get_contents("php://input"))) {
print jsonResponse(false, 'cannot create file');
break;
}
$success = true;
}
print jsonResponse($success);
break;
}
?>