-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupload.php
More file actions
133 lines (109 loc) · 5.52 KB
/
Copy pathupload.php
File metadata and controls
133 lines (109 loc) · 5.52 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
<?php
/*
Copyright (c) 2011 David Wolff
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
require 'includes/soss_common.php';
require 'soss_db.php';
require 'SOSSFilestore.class.php';
require 'soss_request.php';
require 'soss_json.php';
// Make sure we are authorized to view this data, this page is really
// for students and graders only.
if( ! ($_SESSION['auth'] >= AUTH_STUDENT) ) {
soss_send_json_response(SOSS_RESPONSE_PERMISSION_DENIED, "Access Denied");
}
////////////////////
function validate_post() {
//soss_send_json_response(SOSS_RESPONSE_ERROR,"".print_r($_FILES,true). "UPLOAD_ERR_NO_FILE: ".UPLOAD_ERR_NO_FILE);
if (empty($_FILES['userfile']['name'][0]) or empty($_FILES['userfile'])) {
soss_send_json_response(SOSS_RESPONSE_ERROR, "No files submitted.");
}
$results = array('assignment' => soss_get_request('assignment'));
if (empty($results['assignment']) or $results['assignment'] == "__none__") {
soss_send_json_response(SOSS_RESPONSE_ERROR, "Missing assignment.");
}
$validCount = 0;
// Check all file uploads for errors
for ($i = 0; $i < count($_FILES['userfile']['name']); $i++) {
if ($_FILES['userfile']['error'][$i] == UPLOAD_ERR_INI_SIZE or
$_FILES['userfile']['error'][$i] == UPLOAD_ERR_FORM_SIZE) {
soss_send_json_response(SOSS_RESPONSE_ERROR, "UPLOAD FAILED: The file " . $_FILES['userfile']['name'][$i] . " is too large.");
} elseif ($_FILES['userfile']['error'][$i] == UPLOAD_ERR_PARTIAL) {
soss_send_json_response(SOSS_RESPONSE_ERROR, "UPLOAD FAILED: The file " . $_FILES['userfile']['name'][$i] . " was only partially uploaded." .
"Please try again.");
} elseif ($_FILES['userfile']['error'][$i] == UPLOAD_ERR_NO_TMP_DIR) {
soss_send_json_response(SOSS_RESPONSE_ERROR, "UPLOAD FAILED: Server configuration error: no temporary directory for file uploads. Contact administrator.");
} elseif ($_FILES['userfile']['error'][$i] == UPLOAD_ERR_CANT_WRITE) {
soss_send_json_response(SOSS_RESPONSE_ERROR, "UPLOAD FAILED: Server configuration error: unable to write files to disk. Contact administrator.");
} elseif ($_FILES['userfile']['error'][$i] == UPLOAD_ERR_EXTENSION) {
soss_send_json_response(SOSS_RESPONSE_ERROR, "UPLOAD FAILED: Server configuration error: file upload stopped by extension. Contact administrator.");
} elseif ($_FILES['userfile']['error'][$i] != UPLOAD_ERR_NO_FILE) {
$validCount++;
}
}
if ($validCount == 0) {
soss_send_json_response(SOSS_RESPONSE_ERROR, "No files uploaded.");
} else {
$results['num_files'] = $validCount;
}
return $results;
}
$results = validate_post();
if ($results !== false) {
try {
$db = SOSS_DB::getInstance();
// Check to see if the user already submitted for this assignment
$sql = "SELECT id_num FROM %s WHERE ";
$sql .="student_username='%s' AND ";
$sql .="student_class_id='%s' AND ";
$sql .= "assignment_name='%s' AND ";
$sql .= "assignment_class_id='%s'";
$sql = sprintf($sql, SOSS_DB::$FILES_TABLE,
$db->dbclean($_SESSION['uname']),
$db->dbclean($_SESSION['classid']),
$db->dbclean($results['assignment']),
$db->dbclean($_SESSION['classid']));
$dbresult = $db->query($sql);
$data = array(
'overwrite' => (mysql_num_rows($dbresult) > 0)
);
if ($data['overwrite']) {
$row = $db->fetch_row($dbresult);
}
SOSSFilestore::storeUploads('userfile', $db,
$_SESSION['uname'],
$results['assignment'],
$_SESSION['classid']);
// Get the ID for the inserted row
$data['fileid'] = mysql_insert_id();
$data['nFiles'] = $results['num_files'];
if ($results['num_files'] > 1) {
$tmp = "files";
} else {
$tmp = "file";
}
soss_send_json_response(SOSS_RESPONSE_SUCCESS, $results['num_files'] .
" $tmp uploaded successfully.", $data);
} catch (SOSS_DB_Exception $e) {
soss_send_json_response(SOSS_RESPONSE_ERROR, "SQL Exception: " . $e->getMessage());
} catch (SOSSFilestoreException $e) {
soss_send_json_response(SOSS_RESPONSE_ERROR, "Filestore exception: " . $e->getMessage());
}
} else {
soss_send_json_response(SOSS_RESPONSE_ERROR, "Failed to validate POST data.");
}