-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdin.php
More file actions
233 lines (197 loc) · 8.08 KB
/
din.php
File metadata and controls
233 lines (197 loc) · 8.08 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
<?php
/*
This script parses JSON data posted to this page and enters it into darsfordummies_dars.report
If $_POST['force'] is given a value, the script skips redundancy and
login checks for debugging purposes.
*/
if (empty($_POST['msg']))
die;
$json = json_decode($_POST['msg']);
require_once('parts/sqlCredentials.php');
require_once('parts/fbconnect.php');
// if not logged in
if (!empty($loginUrl) && empty($_POST['force']))
{
$loginUrl = $facebook->getLoginUrl(array(
'scope' => 'email',
'redirect_uri' => 'http://darsfordummies.web.engr.illinois.edu?red='.urlencode($_POST['loc']),));
echo '<a href="' . $loginUrl . '">Log in via Facebook</a>';
}
else
{
?>
Parsed data received<br>
<?php
$connection = mysqli_connect($sqlHost, $sqlUser, $sqlPass, $sqlDB) or die(mysqli_error($connection));
$uin = mysqli_real_escape_string($connection, $json->UIN);
$datetime = $json->date;
$datetime = substr($datetime, -2) . '/' . substr($datetime, 0, 5);
$datetime = $datetime . ' ' . $json->time;
$datetimeComparison = substr($json->date, 3, 2) . '/' . substr($json->date, 0, 2) . '/' . substr($json->date, 6);
$datetimeComparison = '20' . substr($json->date, 6) . '-' . str_replace('/', '-', substr($json->date, 0, 5));
$datetimeComparison .= ' ' . $json->time . ':00';
$datetimeComparison = strtotime($datetimeComparison);
if (!mysqli_real_query($connection, "select * from report where uin=$uin"))
die(mysqli_error($connection));
$result = mysqli_use_result($connection);
$time = mysqli_fetch_array($result);
$time = strtotime($time['timestamp']);
$result->free();
// if this is a newer dars report, then insert the data
if ($time < $datetimeComparison || !empty($_POST['force']))
{
// delete the previous requirements for this UIN
mysqli_real_query($connection, "delete from report where uin=$uin;");
mysqli_real_query($connection, "delete from courseopts where uin=$uin;");
mysqli_real_query($connection, "delete from taken where uin=$uin;");
$base_fields = "uin, timestamp, major, primaryGroup";
$base_values = "'$uin', '"
. mysqli_real_escape_string($connection, $datetime) . "', '"
. mysqli_real_escape_string($connection, $json->major) . "', ";
foreach($json->results as $result => $resultData) // result is one index of the results
{
// reset tuple
$fields = $base_fields;
$values = $base_values . "'" . mysqli_real_escape_string($connection, $resultData->primary->Group) ."'";
if (!empty($resultData->primary->Hrs))
{
$fields .= ", primaryHrs";
$values .= ", " . mysqli_real_escape_string($connection, $resultData->primary->Hrs);
}
if (!empty($resultData->primary->Course))
{
$fields .= ", primaryCourse";
$values .= ", " . mysqli_real_escape_string($connection, $resultData->primary->Course);
// try to use the course options if this insert has a Course requirement
if (!empty($resultData->courseOpts))
{
addCourseOpts($resultData->courseOpts,
mysqli_real_escape_string($connection, $resultData->primary->Group),
NULL);
}
}
if (!empty($resultData->secondary))
{
foreach($resultData->secondary as $secondaryResult => $secondaryResultObject) // $secondaryResult is index within secondary
{
$fieldsExtended = $fields;
$valuesExtended = $values;
foreach($secondaryResultObject as $secondaryResultAttribute => $secondaryResultData)
{
$fieldsExtended .= ", secondary" . mysqli_real_escape_string($connection, $secondaryResultAttribute);
$valuesExtended .= ", '" . mysqli_real_escape_string($connection, $secondaryResultData) . "'";
}
// try to use the course options if this insert has a Course requirement
if (!empty($secondaryResultObject->Course) && !empty($resultData->courseOpts))
{
addCourseOpts($resultData->courseOpts,
mysqli_real_escape_string($connection, $resultData->primary->Group),
mysqli_real_escape_string($connection, $secondaryResultObject->Group));
}
$fieldsExtended = mysqli_real_escape_string($connection, $fieldsExtended);
// insert into database
//echo "<br>insert into report ($fieldsExtended) values ($valuesExtended);";
if (!mysqli_real_query($connection, "insert into report ($fieldsExtended) values ($valuesExtended);"))
{
die(mysqli_error($connection));
}
}
}
else
{
// insert into database
//echo "<br>insert into report ($fields) values ($values);";
if (!mysqli_real_query($connection, "insert into report ($fields) values ($values);"))
{
die(mysqli_error($connection));
}
}
}
// add the Taken courses to the database
foreach ($json->taken as $key => $value)
{
if (substr($value, 0, 2) == 'FA')
$date = '20' . substr($value, 2) . '-9-1'; // 20__-September 1
else if (substr($value, 0, 2) == 'SP')
$date = '20' . substr($value, 2) . '-2-1'; // 20__-February 1
else if (substr($value, 0, 2) == 'SU')
$date = '20' . substr($value, 2) . '-6-1'; // 20__-June 1
if (!$connection->real_query("insert into taken values($uin, '$key', '$date');"))
die(mysqli_error($connection));
}
}
// tie this UIN to an FBID
mysqli_real_query($connection, "select uin from user where fbid='".$_SESSION['id']."';");
$result = mysqli_use_result($connection);
$result = mysqli_fetch_array($result);
$result = $result[0];
if ($result == 0)
{
$query = "update user set uin=".$uin." where fbid='".$_SESSION['id']."';";
mysqli_real_query($connection, $query);
}
elseif ($result != $uin) { ?>
<div id="confirmUIN">
We have your UIN previously recorded as <b><?php echo $result; ?></b>. Is this correct?<br>
<i>(No action results in keeping the previously recorded UIN.)</i><br>
<form method="GET" action="http://darsfordummies.web.engr.illinois.edu/parts/updateFb.php">
<input type="hidden" name="auditUrl" value="" id="return">
<input type="hidden" name="uin" value="<?php echo $uin; ?>">
<input type="button" value="Yes" onclick="document.getElementById('confirmUIN').innerHTML=''"> my UIN is <?php echo $result; ?>.<br>
<input type="submit" value="No" onclick="document.getElementById('return').value=window.location.href"> my UIN is <?php echo $uin; ?>.
</form>
</div>
<?php
}
mysqli_close($connection);
}
function addCourseOpts($fullString, $primaryGroup, $secondaryGroup=NULL)
{
global $connection;
global $uin;
$preg = '(\w{2,5} )?\d{3}(.*?\(.+?\))?';
$courseOpts = substr($fullString, 13);
$courseOpts = preg_split('/,/i', $courseOpts);
foreach ($courseOpts as $key => $value)
{
$courseOpts[$key] = trim($value);
// make sure that every entry has a department, not just a number
if (preg_match('/^(\w){2,5} (\d){3}/', $courseOpts[$key], $matches) == 0) // if this doesn't have a department,
$courseOpts[$key] = $dept . $courseOpts[$key]; // prepend it
else
{
preg_match('/\w{2,5} /', $matches[0], $dept); // else, set the last department to this department
$dept = $dept[0];
}
// if this array entry has multiple choices for the same credit...
if (preg_match_all("/$preg( OR $preg)+/i", $courseOpts[$key], $matches) != 0)
{
$temp = $courseOpts[$key];
unset($courseOpts[$key]);
foreach ($matches[0] as $matchKey => $matchVal)
{
$courseOpts[] = $matches[0][$matchKey];
// if these OR'd choices are not the only choices in this array entry...
if (strlen($matches[0][$matchKey]) < strlen($value))
{
$temp = substr($temp, strlen($matches[0][$matchKey]) + 1);// + strpos($temp, $matches[0][$matchKey]));
}
}
if ($temp !== $value && preg_match_all("/$preg/i", $temp, $temp2) > 0)
{
foreach ($temp2[0] as $key2 => $value2)
$courseOpts[] = $value2;
}
}
}
// new for loop; array indices dynamically added via the previous foreach loop are not iterated through by the loop
foreach ($courseOpts as $key => $value)
{
$query = "insert into courseopts(uin, primaryGroup, secondaryGroup, courseOpts)
values('$uin', '$primaryGroup', "
. (is_null($secondaryGroup) ? 'NULL' : "'$secondaryGroup'")
. ", '" .$connection->escape_string($courseOpts[$key]). "')";
mysqli_real_query($connection, $query);
}
}
?>