-
Notifications
You must be signed in to change notification settings - Fork 0
/
writeAPI.php
227 lines (202 loc) · 6.38 KB
/
writeAPI.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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
<?php
require('dbHandler.php');
/*
Valid POST API Requests:
writeSingleMatchData:
Argument Format:
{'scout' : '', 'matchNumber' : '', 'teamNumber' : '',
'autoMobility' : '', 'autoConeLevel1' : '', 'autoConeLevel2' : '', 'autoConeLevel3' : '',
'autoCubeLevel1' : '', 'autoCubeLevel2' : '', 'autoCubeLevel3' : '', 'autoChargeStation' : '',
'teleopConeLevel1' : '', 'teleopConeLevel2' : '', 'teleopConeLevel3' : '', 'teleopCubeLevel1' : '',
'teleopCubeLevel2' : '', 'teleopCubeLevel3' : '', 'teleopChargeStation' : '', 'cannedComments' : '',
'textComments' : ''}
Response Format:
true OR false
*/
if (isset($_POST['writeSingleMatchData'])) {
$result = new stdClass();
$result->success = true;
$db = new dbHandler();
try {
$matchData = json_decode($_POST['writeSingleMatchData'], true);
$matchData['matchKey'] = $matchData['matchNumber'] . '-' . $matchData['teamNumber'];
$db->writeRowToTable('datatable', $matchData);
} catch (Exception $e) {
error_log($e);
$e = json_decode(json_encode($e));
$result->error = $e -> errorInfo;
$result->success = false;
}
echo (json_encode($result));
}
if (isset($_POST['writeDataList'])){
$result = new stdClass();
$result->success = true;
$db = new dbHandler();
$rawDataList = json_decode($_POST['writeDataList'], true);
foreach($rawDataList as &$matchData){
try {
$matchData['matchKey'] = $matchData['matchNumber'] . '-' . $matchData['teamNumber'];
$db->writeRowToTable('datatable', $matchData);
} catch (Exception $e) {
error_log($e);
$e = json_decode(json_encode($e));
$result->error = $e -> errorInfo;
$result->success = false;
}
}
echo (json_encode($result));
}
if (isset($_POST['writePitScoutData'])) {
$result = new stdClass();
$result -> success = true;
$db = new dbHandler();
//create pitScouttable if it doesn't exist
if (!$db->getTableExists("pitScouttable")) {
$db->createTable("pitScouttable");
}
$matchData = json_decode($_POST['writePitScoutData'], true);
$success = true;
try {
$db->writeRowToTable('pitScouttable', $matchData);
} catch (Exception $e) {
error_log($e);
$e = json_decode(json_encode($e));
$result->error = $e -> errorInfo;
$result -> success = false;
}
echo (json_encode($result));
}
if (isset($_POST['writeStrikeScoutData'])) {
$result = new stdClass();
$result -> success = true;
$db = new dbHandler();
//create strikeScouttable if it doesn't exist
if (!$db->getTableExists("strikeScouttable")) {
$db->createTable("strikeScouttable");
}
$matchData = json_decode($_POST['writeStrikeScoutData'], true);
$success = true;
try {
$db->replaceRowInTable('strikeScouttable', $matchData);
} catch (Exception $e) {
error_log($e);
$e = json_decode(json_encode($e));
$result->error = $e -> errorInfo;
$result -> success = false;
}
echo (json_encode($result));
}
if (isset($_GET['saveAllianceRank'])){
$db = new dbHandler();
$teamList = json_decode($_GET['saveAllianceRank'], true);
$match = $_GET['match'];
$result = array();
$result['success'] = true;
# Fill array.
if (count($teamList) < 6){
for($i = count($teamList); $i < 6; $i++){
array_push($teamList, 0);
}
}
$leadScoutData = array();
$leadScoutData['matchNum'] = $match;
$leadScoutData['team1'] = $teamList[0];
$leadScoutData['team2'] = $teamList[1];
$leadScoutData['team3'] = $teamList[2];
$leadScoutData['team4'] = $teamList[3];
$leadScoutData['team5'] = $teamList[4];
$leadScoutData['team6'] = $teamList[5];
$db->writeRowToTable('LSTable', $leadScoutData);
echo (json_encode($result));
}
if (isset($_POST["pitPictureUpload"])) {
$result = new stdClass();
$settings = new siteSettings();
$result -> error = "";
$result -> success = true;
$path = './uploads/';
$target_dir = $path;
$target_file = $target_dir . $_POST["teamNumber"];
$uploadOk = 1;
$imageFileType = strtolower(pathinfo(basename($_FILES["fileToUpload"]["name"]), PATHINFO_EXTENSION));
//check if $path is set
if (!$path) {
$result -> error = "pictureFolder is not set in config";
$uploadOk = 0;
$result -> success = false;
}
//check if directory exists;
if ($path && !is_dir($path)) {
$result -> error = "pictureFolder does not exist";
$uploadOk = 0;
$result -> success = false;
}
// Check if image file is a actual image or fake image
if (isset($_POST["teamNumber"])) {
$check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
if ($check !== false) {
//$result -> error = "File is an image - " . $check["mime"] . ".";
$uploadOk = 1;
} else {
$result -> error = "File is not an image.";
$uploadOk = 0;
}
}
// Append number monotonic number to end of image and check if it exists.
$i = 0;
$valid = false;
while($i < 20){ // Only allow 20 pics per team.
$new_file_name = strtolower($target_file . '-' . $i . '.' . $imageFileType);
if (file_exists($new_file_name)){
$i++;
}
else {
$valid = true;
break;
}
}
if ($valid){
$target_file = $new_file_name;
}
else {
$result -> error = "Sorry, too many files under that team.";
$uploadOk = 0;
}
// Check file size
if ($_FILES["fileToUpload"]["size"] > 100000000) {
$result -> error = "Sorry, your file is too large.";
$uploadOk = 0;
}
// Allow certain file formats
if (
$imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
&& $imageFileType != "gif"
) {
$result -> error = "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
$uploadOk = 0;
}
$message = "";
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
$result -> success = false;
// if everything is ok, try to upload file
} else {
try {
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
$temp = "The file " . htmlspecialchars($target_file) . " has been uploaded";
if (file_exists($target_file)) {
$result -> error = $temp;
} else {
$result -> error = "The file " . htmlspecialchars($target_file) . " could not be found on the server";
}
}
} catch (Exception $e) {
$result -> $error = $e;
}
}
$redirect = "pictureUpload.php";
$message = json_encode($result);
header("Location: " . $redirect . "?message=" . $message);
die();
}