-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.php
More file actions
298 lines (254 loc) · 8.78 KB
/
util.php
File metadata and controls
298 lines (254 loc) · 8.78 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
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
<?php
include "Parsedown/Parsedown.php";
$Parsedown = new Parsedown();
function handle_upload($cid)
{
logmessage($cid, "Uploading assignment from " . $_SESSION["username"] . " " . $_SESSION["id"]);
logmessage($cid, "Upload " . $title . " for " . $_SESSION["username"]);
$project = mysqli_real_escape_string($cid, $_POST['project']);
if ($project < 3)
{
echo "<b>Submissions only on projects 3 and above right now!</b><br>";
return;
}
$dest = "submissions";
# echo "CID is $cid";
# echo "File is " . $_FILES['file1']['name'];
# echo "Type is " . $_FILES['file1']['type'];
$a = imageblob($_FILES['file1']['name'], $_FILES['file1']['tmp_name']);
$a = addslashes($a);
$b = imageblob($_FILES['file2']['name'], $_FILES['file2']['tmp_name']);
$b = addslashes($b);
$c = imageblob($_FILES['file3']['name'], $_FILES['file3']['tmp_name']);
$c = addslashes($c);
$title = mysqli_real_escape_string($cid,$_POST['title']);
$git = mysqli_real_escape_string($cid, $_POST['git']);
$project = mysqli_real_escape_string($cid, $_POST['project']);
$collab1 = mysqli_real_escape_string($cid, $_POST['collab1']);
$collab2 = mysqli_real_escape_string($cid, $_POST['collab2']);
if ($_FILES['gitlog']['tmp_name'] != NULL)
$gitlog = mysqli_real_escape_string($cid, file_get_contents($_FILES['gitlog']['tmp_name']));
else
$gitlog = "File not read";
$gitlog = preg_replace('/[\x00-\x05\x7F-\xFF]/', '', $gitlog);
if ($_FILES['markdown']['tmp_name'] != NULL)
$markdown = mysqli_real_escape_string($cid, file_get_contents($_FILES['markdown']['tmp_name']));
else
$gitlog = "File not read";
$markdown = preg_replace('/[\x00-\x05\x7F-\xFF]/', '', $markdown);
$classnumber = mysqli_real_escape_string($cid, $_POST['classnumber']);
$id = $_SESSION["id"];
$sql = "SELECT entry from submissions WHERE id='$id' AND project='$project'";
# echo "UPDATE QUERY:";
# echo $sql;
$rs = mysqli_query($cid, $sql);
if (mysqli_num_rows($rs) == 0)
{
$sql = "INSERT INTO submissions (title,id,project,classnumber,collab1,collab2,git,data1,data2,data3,markdown,gitlog) VALUES ('$title','$id','$project','$classnumber','$collab1','$collab2','$git','$a', '$b', '$c','$markdown','$gitlog')";
$rs = mysqli_query($cid, $sql);
echo "Uploaded a new project<br>\n";
logmessage($cid, "Upload " . $title . " for " . $_SESSION["username"]);
} else {
$row = mysqli_fetch_assoc($rs);
echo "Updated a prior submission " . $row["entry"] . "<br>\n";
sql_replace($cid, "title", $title, $row["entry"]);
sql_replace($cid, "classnumber", $classnumber, $row["entry"]);
sql_replace($cid, "collab1", $collab1, $row["entry"]);
sql_replace($cid, "collab2", $collab2, $row["entry"]);
sql_replace($cid, "git", $git, $row["entry"]);
sql_replace($cid, "data1", $a, $row["entry"]);
sql_replace($cid, "data2", $b, $row["entry"]);
sql_replace($cid, "data3", $c, $row["entry"]);
sql_replace($cid, "markdown", $markdown, $row["entry"]);
sql_replace($cid, "gitlog", $gitlog, $row["entry"]);
logmessage($cid, "Revise " . $title . " for " . $_SESSION["username"]);
}
# echo $sql;
}
function sql_replace($cid, $field, $newval, $entry)
{
# echo "Replace $field";
$sql = "UPDATE submissions SET $field = '$newval' WHERE entry=$entry";
$rs = mysqli_query($cid, $sql);
# echo $sql;
}
function imageblob($filename, $tmpname)
{
$ext = pathinfo($filename, PATHINFO_EXTENSION);
# echo "Filename is " . $filename . "<br>\n";
# echo "Temp name is " . $tmpname . "<br>\n";
# echo "Ext is " . $ext . "<br>\n";
switch ($ext)
{
case "png":
case "PNG":
$source = imagecreatefrompng($tmpname);
break;
case "jpg":
case "jpeg":
case "JPG":
$source = imagecreatefromjpeg($tmpname);
break;
case "gif":
case "GIF":
$source = imagecreatefromgif($tmpname);
break;
default:
}
$imsize = 800;
if ($source)
{
list($width, $height) = getimagesize($tmpname);
$dest = "profile_images";
$newwidth = $width;
$newheight = $height;
if ($width > $imsize)
{
$scale = $imsize/$width;
$newwidth = $width * $scale;
$newheight = $height * $scale;
}
if ($newheight > $imsize)
{
$scale = $imsize/$newheight;
$newwidth = $newwidth * $scale;
$newheight = $newheight * $scale;
}
# echo "Scale " . $width . " by " . $height . " to " . $newwidth . " by " . $newheight;
$thumb = imagecreatetruecolor($newwidth, $newheight);
imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
ob_start();
imagepng($thumb);
$content = ob_get_contents();
ob_end_clean();
return $content;
}
return 0;
}
function user_submissions()
{
$sql = "SELECT entry from submissions where id='$id'";
$rs = mysqli_query($cid, $sql);
}
function show_log($cid, $entry)
{
$entry = mysqli_real_escape_string($cid,$entry);
$sql = "SELECT * from submissions where entry='$entry'";
# echo $sql;
# echo "<br>";
$rs = mysqli_query($cid, $sql);
if (mysqli_num_rows($rs) == 1)
{
$row = mysqli_fetch_assoc($rs);
# echo "Here is the log file for $entry<br>";
echo "<pre>\n";
echo htmlentities($row["gitlog"]);
echo "</pre>\n";
# echo "Done<br>";
}
}
function show_submission($cid, $entry, $mode)
{
$entry = mysqli_real_escape_string($cid,$entry);
$sql = "SELECT * from submissions where entry='$entry'";
# echo $sql;
# echo "<br>";
$rs = mysqli_query($cid, $sql);
if (mysqli_num_rows($rs) == 1)
{
$row = mysqli_fetch_assoc($rs);
echo "<h1>Program " . $row["project"] . ": " . $row["title"] . "</h1><br>\n";
if ($mode == 0)
{
$sql = "SELECT * from review where entry=$entry";
$rs2 = mysqli_query($cid, $sql);
$count = 0;
$rated = 0;
while ($reviews = mysqli_fetch_array($rs2, MYSQLI_ASSOC))
{
# echo "Review of " . $reviews["score"] . "<br>\n";
$count = $count + $reviews["score"];
if ($reviews["score"] > 0)
$rated = $rated + 1;
}
if ($rated > 0)
$avg = $count / $rated;
else
$avg = "---";
echo "Total number of points: " . $count . " ($rated ratings, average $avg)<br>\n";
echo "<b>Points for in-class voting and presentations yet to be computed!</b><br>\n";
$sql = "SELECT * from points where entry=$entry";
$points = mysqli_query($cid, $sql);
$pt = mysqli_fetch_assoc($points);
echo "Readme (10 points):" . $pt["readme"] . "<br>\n";
echo "Screen shots (10 points):" . $pt["screenshots"] . "<br>\n";
echo "Git Log (10 points):" . $pt["gitlog"] . "<br>\n";
echo "Different-day checkin (40 points):" . $pt["commits"] . "<br>\n";
echo "Voted on other projects (10 points):" . $pt["voted"] . "<br>\n";
echo "Points from other voters (10 points):" . $pt["classpoints"] . "<br>\n";
echo "Presentation (10 points):" . $pt["presentation"] . "<br>\n";
echo "Project total (out of 100):" . $pt["total"] . "<br>\n";
}
$url = $row["git"];
if ($mode == 0)
echo "<a href=\"$url\">GIT Repository: $url</a></br>\n";
# echo "<pre>\n";
# echo htmlentities($row["markdown"]);
if (!$Parsedown)
$Parsedown = new Parsedown();
echo $Parsedown->text($row["markdown"]);
# echo "\n</pre>\n<br>\n";
echo "<img src=\"image.php?id=$entry\">\n";
echo "<img src=\"image.php?id=$entry&image=2\">\n";
echo "<img src=\"image.php?id=$entry&image=3\"><br>\n";
$count = substr_count($row["gitlog"], "Date:");
echo "$count check-ins<br>\n";
# Patterns like Date: Wed Jan 30
$gla = preg_split('/$\R?^/m', $row["gitlog"]);
$glines = count($gla);
# echo "GIT log has $glines lines<br>\n";
$i = 0;
$prevdate = "";
$uniq = 0;
foreach ($gla as $line)
{
$n = sscanf($line, "Date: %s %s %d", $day, $month, $date);
# echo "Line $line<br>\nDates $dates<br>\n";
if ($n == 3)
{
# echo "Saw date $day $month $date<br>\n";
if (strcmp($prevdate, $date) != 0)
{
$uniq = $uniq + 1;
# echo "Unique<br>\n";
}
$prevdate = $date;
}
}
echo "$uniq different days of coding.<br>\n";
# echo "<a href=\"index.php?action=viewlog&ln=$entry\">View the log</a><br>\n";
if ($mode == 0)
{
echo "\n<pre>\n";
echo htmlentities($row["gitlog"]);
echo "\n</pre>\n";
}
}
}
function show_existing($cid, $id)
{
echo "Submitted programs<br>";
$sql = "SELECT entry from submissions where id='$id' order by entry desc";
$rs = mysqli_query($cid, $sql);
while (list($entry) = mysqli_fetch_array($rs))
{
show_submission($cid, $entry, 0);
echo "\n<hr>\n";
}
}
function logmessage($cid, $message)
{
$message = mysqli_real_escape_string($cid, $message);
$sql = "INSERT INTO log (log) VALUES ('$message')";
mysqli_query($cid, $sql);
}