-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.php
194 lines (162 loc) · 4.59 KB
/
functions.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
<?php
function initdb() {
include 'config.php';
$db = new PDO( 'sqlite:' . $DB_NAME );
$dbcmds = ['CREATE TABLE IF NOT EXISTS files(
key TEXT NOT NULL,
name TEXT NOT NULL
)',
];
foreach ( $dbcmds as $cmd ) {
$db->exec( $cmd );
}
$clean = 1;
foreach ( $db->errorInfo() as $error ) {
if ($error != 0) {
$clean = $error;
}
}
return $clean;
}
function randomName( $length ) {
$VALID_CHARS = array_merge( range( 0, 9 ), range( 'a', 'z' ), range( 'A', 'Z' ));
$buffer = '';
for( $i = 0; $i < $length; $i++ ) {
$buffer .= $VALID_CHARS[mt_rand( 0, count( $VALID_CHARS ) - 1 )];
}
$name = $buffer;
return $name;
}
function cleanString( $input ) {
$output = preg_replace("/[^A-Za-z0-9.]/", '_', $input);
return $output;
}
function passHash( $input ) {
$output = password_hash( $input, PASSWORD_DEFAULT );
return $output;
}
function getFiles() {
include 'config.php';
$db = new PDO('sqlite:' . $DB_NAME);
$query = $db->prepare( 'SELECT name FROM files' );
$query->execute();
$result = $query->fetchAll(PDO::FETCH_COLUMN, 0);
return $result;
}
//takes file name as input and returns owner
function getFileOwner( $file ) {
include 'config.php';
$db = new PDO('sqlite:' . $DB_NAME);
$query = $db->prepare( 'SELECT key FROM files WHERE name=:file' );
$query->bindValue(':file', $file, PDO::PARAM_STR);
$query->execute();
$result = $query->fetch(PDO::FETCH_ASSOC);
return $result;
}
//owner is secret key
//file is _FILE object
function addFile( $owner, $file ) {
if( $file['error'] === 1 ) {
echo("File too large");
exit(1);
}
include 'config.php';
$files = getFiles();
$fileLocation = $file['tmp_name'];
$fileExtension = pathinfo( $file['name'], PATHINFO_EXTENSION );
$fileExtension = strtolower( $fileExtension );
$fileType = $file['type'];
$destFile = '';
//Check if file is allowed type
if( !in_array( $fileExtension, $ALLOWED_FILE_TYPES )) {
echo("Disallowed file type");
exit(1);
}
//Check if file name should be randomized
foreach( $RANDOMIZE_FILE_TYPES as $type ) {
if( strpos( $fileType, $type ) !== false ) {
$randomize = true;
}
}
if( $randomize ) {
//Ensure file by that name doesn't already exist
do {
$destName = randomName( $FILE_NAME_LENGTH );
$destFile = $destName . '.' . $fileExtension;
}
while( in_array( $destFile, $files ));
} else {
$destFile = cleanString( $file['name'] );
if( in_array( $destFile, $files )) {
echo("A file by that name already exists");
exit(1);
}
}
//Check if file is protected
if( in_array( $destFile, $PROTECTED_FILES )) {
echo("File blacklisted");
exit(1);
}
//exit if unable to move file
if( !move_uploaded_file( $fileLocation, $destFile )) {
echo("Unable to move file, please check permissions");
exit(1);
}
//add file to database
$hashedOwner = passHash( $owner );
$db = new PDO( 'sqlite:' . $DB_NAME );
$query = $db->prepare( 'INSERT INTO files (key, name) VALUES (:owner, :file)' );
$query->bindValue( ':owner', $hashedOwner, PDO::PARAM_STR );
$query->bindValue( ':file', $destFile, PDO::PARAM_STR );
$query->execute();
return $destFile;
}
//owner is secret key
//file is file name
function deleteFile( $owner, $file ) {
include 'config.php';
//Check if file is protected
if( in_array( $file, $PROTECTED_FILES )) {
echo("File blacklisted");
exit(1);
}
$files = getFiles();
//check that the file is in the database
if( !in_array( $file, $files )) {
echo("File does not exist in db");
exit(1);
}
//checks that the correct owner is attempting to delete the file
$fileOwner = getFileOwner( $file );
$fileOwner = $fileOwner['key'];
if( !password_verify( $owner, $fileOwner )) {
echo("You cannot delete files you did not upload");
exit(1);
}
//delete file
if( !unlink( $file )) {
echo("Unable to delete file");
exit(1);
}
//remove file from database
$db = new PDO( 'sqlite:' . $DB_NAME );
$query = $db->prepare( 'DELETE FROM files WHERE name=:file' );
$query->bindValue( ':file', $file, PDO::PARAM_STR );
$query->execute();
echo("Deleted file $file from database");
}
//owner is secret key
//fileName is the name of the new file
function craftResponse( $owner, $fileName ) {
include 'config.php';
$link = $FILE_LOCATION . $fileName;
$deleteLink = $FILE_LOCATION . $SCRIPT_NAME . '?' . $KEY_ARG . '=' . $owner . '&' . $DELETE_ARG . '=' . $fileName;
$response = array(
'data' => array(
'link' => $link,
'delete' => $deleteLink
)
);
$response = json_encode( $response );
return $response;
}