-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathadmin.php
More file actions
302 lines (281 loc) · 11.3 KB
/
admin.php
File metadata and controls
302 lines (281 loc) · 11.3 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
299
300
301
302
<!DOCTYPE html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="UBC Airline Booking Service">
<title>UBC Air</title>
<link rel="stylesheet" href="http://yui.yahooapis.com/pure/0.5.0/pure-min.css">
<link rel="stylesheet" href="css/mainpage.css"
</head>
<body>
<div class="header">
<div class="home-menu pure-menu pure-menu-open pure-menu-horizontal pure-menu-fixed">
<a class="pure-menu-heading" href="">UBC Air</a>
<ul>
<li><a href='index.php'>Home</a></li>
<?php
if(!array_key_exists('loggedin', $_COOKIE) ) {
echo "<li><a href='login.php'>Login</a></li>";
echo "<li><a href='register.php'>Sign Up</a></li>";
header('location: login.php');
}
else {
echo "<li><a href='logout.php'>Logout</a></li>";
echo "<li><a href='support.php'>My Orders</a></li>";
}
?>
<li><a href='flights.php'>Find flights</a></li>
</ul>
</div>
</div>
<body>
<div class="content-customer-area">
<!-- Drop down menu for selecting specific table -->
<p>Select working data table</p>
<form method="POST" action="admin.php">
<select id="tabchoice" name="tabchoice" onchange="this.form.submit()">
<option selected value = "default">(Choose a data table)</option>
<option value = "CUSTOMER">Customers</option>
<option value = "AIRPORT">Airports</option>
<option value = "PLANE">Airplanes</option>
<option value = "FLIGHT">Flights</option>
<option value = "MAKE_RES">Reservations</option>
<option value = "RES_INCLUDES">Reserv-Flights*</option>
<option value = "HAS_B">Baggages</option>
<option value = "LAST_LOCATION">Baggage Locations</option>
<option value = "DETER_PAY">Reserv-Payments*</option>
<option value = "PAYMENT">Payments</option>
</select>
<input type="number" max="5" value ="0" name="test">
<noscript><input type="submit" value="Submit"></noscript></p>
</form>
<script>
<!-- Check inputs in insert form -->
function validateInsert() {
var elements = document.forms["insertform"].elements;
for (e in elements) {
if (elements[e].name == "tablename") break;
var validity = isValid(elements[e].name, elements[e].value);
if (!validity) return false;
}
return true;
}
<!-- Check inputs in update form-->
function validateUpdate() {
var elements = document.forms["updateform"].elements;
var validity = isValid(elements["field2change"].value, elements["newvalue"].value);
var validity2 = isValid(elements["updatesearchby"].value, elements["searchedvalue"].value);
if (!validity || !validity2) return false;
else return true;
}
<!-- Check input in delete form -->
function validateDelete() {
var elements = document.forms["deleteform"].elements;
return isValid(elements["deletesearchby"].value, elements["searchedvalue"].value);
}
<!-- Helper, actually checks given input vs. attribute name, using regular expressions -->
function isValid(name, value) {
var pattern;
switch (name) {
case "RESID":
case "RESORDER":
case "BID":
case "STATUS":
case "CID":
case "PCLASS":
case "TICKET_NUM":
case "FID":
case "PAYID":
case "PHONE":
case "COST":
case "TOTAL_COST":
case "PASSPORT_NUM":
case "IS_ADMIN":
case "CID": pattern = /(^\d\d*\d$)|(^\d$)/; break;
case "CREDITCARD": pattern = /^\d\d{14}\d$/; break;
case "WEIGHT_KG": pattern = /(^(\d*)\.{0,1}(\d*)$)|(^\d$)/; break;
case "EMAIL": pattern = /^\w\w*(\.\w+)*@\w+\.\w+$/; break;
case "DEPARTAP":
case "ARRIVALAP":
case "CODE": pattern = /^[a-zA-Z][a-zA-Z][a-zA-Z]$/; break;
case "COUNTRY":
case "PASSPORT_COUNTRY": pattern = /^[a-zA-Z][a-zA-Z]$/; break;
case "DEPARTTIME":
case "LAST_UPDATE":
case "ARRIVALTIME": pattern = /.+/; break;
default: pattern = /^\w\w*\w$/;
}
if (!pattern.test(value)) {
alert("Invalid input, " + name + " has regex " + pattern);
return false;
}
else return true;
}
</script>
<?php
// These stuff are needed (for now) to connect Oracle, will figure out how to import from
// main php file
$success = True; //keep track of errors so it redirects the page only if there are no errors
//$db_conn = OCILogon("ora_b4s8", "a16894123", "ug");
include "oci_functions.php";
// Same source as above, modified for generic table printing
// Prints everything after table is selected
function printResults($name, $cols, $data) {
$attributes = array();
while ($row = OCI_Fetch_Array($cols, OCI_NUM)) {
$attributes[] = $row[0];
}
echo "<br>INSERT NEW<br>(enter value for each attribute)";
printInsertFields($name, $attributes);
echo "<br>UPDATE<br>(search for the row to change, then choose attribute to change and enter new value)<br>";
printUpdateFields($name, $attributes);
echo "<br>DELETE<br>(search for the row to delete)<br>";
printDeleteFields($name, $attributes);
echo "<br>DATA FROM TABLE " . $name . "<br>";
printTable($attributes, $data);
}
// Prints the table attributes and data
function printTable($attributes, $data) {
echo "<div class="."pure-table pure-table-bordered pure-table-striped"."><table>";
// print the top row (attribute labels)
$label = "<tr>";
foreach ($attributes as $value) {
$label = $label . "<th>" . $value . "</th>";
}
echo $label . "</tr>";
// print the data rows (tuples)
while ($tuple = OCI_Fetch_Array($data, OCI_NUM)) {
$output = "<tr>";
foreach($tuple as $value) {
$output = $output . "<td>" . $value . "</td>";
}
echo $output . "</tr>";
}
echo "</table></div>";
}
// Prints the form to insert new tuple into chosen table
function printInsertFields($table, $attributes) {
$form = "<form method='POST' name='insertform' action='admin.php' onsubmit='return validateInsert()'><table>";
for ($it=0; $it < count($attributes); $it++) {
if (strcmp($attributes[$it],"DEPARTTIME") == 0 || strcmp($attributes[$it],"ARRIVALTIME") == 0 || strcmp($attributes[$it],"LAST_UPDATE") == 0) $type = "datetime-local";
else $type = "text";
$form = $form . "<tr><td>" . $attributes[$it] . ":</td><td><input type='$type' name = '$attributes[$it]' required></td></tr>";
}
echo $form . "</table>
<p><input type='hidden' value='$table' name='tablename'></p>
<p><input type='submit' value='Insert' name='insertsubmit'></p>
</form>";
}
// Prints the form to update table data
function printUpdateFields($table, $attributes) {
$form = "<form method='POST' name ='updateform' action='admin.php' onsubmit='return validateUpdate()'><table>"
. "<tr><td>Search row by: </td><td><select id='updatesearchby' name='updatesearchby'>"
. "<option selected value='default'>(Select Column)</option>";
for ($it=0; $it < count($attributes); $it++) {
$form = $form . "<option value ='$attributes[$it]'>$attributes[$it]</option>";
}
$form = $form . "</select></td>"
. "<td><input type='text' name='searchedvalue' placeholder='Value to search'></td>"
. "</tr>"
. "<tr><td>Attribute to update: </td><td><select id='field2change' name='field2change'>"
. "<option selected value='default'>(Select Column)</option>";
for ($it=0; $it < count($attributes); $it++) {
$form = $form . "<option value ='$attributes[$it]'>$attributes[$it]</option>";
}
$form = $form . "</select></td>"
. "<td><input type='text' name='newvalue' placeholder='Enter new value'></td>"
. "</tr></table>"
. "<p><input type='hidden' value='$table' name='tablename'></p>"
. "<p><input type='submit' value='Update' name='updatesubmit'></p></form>";
echo $form;
}
// Prints the form to delete table data
function printDeleteFields($table, $attributes) {
$form = "<form method='POST' name='deleteform' action='admin.php' onsubmit='return validateDelete()'><table>"
. "<tr><td>Search row by: </td><td><select id='deletesearchby' name='deletesearchby'>"
. "<option selected value='default'>(Select Column)</option>";
for ($it=0; $it < count($attributes); $it++) {
$form = $form . "<option value ='$attributes[$it]'>$attributes[$it]</option>";
}
$form = $form . "</select></td>"
. "<td><input type='text' name='searchedvalue' placeholder='Value to search'></td>"
. "</tr></table>"
. "<p><input type='hidden' value='$table' name='tablename'></p>"
. "<p><input type='submit' value='Delete' name='deletesubmit'></p></form>";
echo $form;
}
// Only starts retrieving above forms when connection to Oracle is established
if ($db_conn) {
// Get the drop down table selection and call function to deal with selected table
if (array_key_exists('tabchoice', $_POST)) {
// save selection for future load
setcookie("tabchoice",$_POST['tabchoice']);
}
// Handle tuple insert form submission
if (array_key_exists('insertsubmit', $_POST)) {
reset($_POST);
list($key, $value) = each($_POST);
$tuple = "'$value'";
while (list($key, $value) = each($_POST)) {
if (strcmp($key,"DEPARTTIME") == 0 || strcmp($key,"ARRIVALTIME") == 0) {
$value = parseDate($value,3);
}
if (strcmp($key,"insertsubmit") == 0 || strcmp($key,"tablename") == 0) break;
$tuple = "$tuple,'$value'";
}
$tablename = $_POST['tablename'];
executePlainSQL("insert into $tablename values ($tuple)");
OCICommit($db_conn);
}
// Handle tuple update form submission
if (array_key_exists('updatesubmit', $_POST)) {
$tablename = $_POST['tablename'];
$field2change = $_POST['field2change'];
$newvalue = $_POST['newvalue'];
$updatesearchby = $_POST['updatesearchby'];
$searchedvalue = $_POST['searchedvalue'];
executePlainSQL("update $tablename"
. " set $field2change ='$newvalue'"
. " where $updatesearchby = '$searchedvalue'");
OCICommit($db_conn);
// save selection for next load
setcookie("updatesearchby",$updatesearchby);
}
// Handle delete from submission
if (array_key_exists('deletesubmit', $_POST)) {
$tablename = $_POST['tablename'];
$deletesearchby = $_POST['deletesearchby'];
$searchedvalue = $_POST['searchedvalue'];
executePlainSQL("delete from $tablename"
. " where $deletesearchby = '$searchedvalue'");
OCICommit($db_conn);
// save selection for next load
setcookie("deletesearchby",$deletesearchby);
}
if ($_POST && $success) {
//POST-REDIRECT-GET -- See http://en.wikipedia.org/wiki/Post/Redirect/Get
header("location: admin.php");
// default will check if there is any table already selected and output that
} else if ((strcmp($_COOKIE['tabchoice'], "") !== 0) && (strcmp($_COOKIE['tabchoice'], "default") !== 0)) {
$tabchoice = $_COOKIE['tabchoice'];
$cols = executePlainSQL("select column_name from user_tab_columns where table_name = '$tabchoice'");
$data = executePlainSQL("select * from " . $tabchoice);
printResults($tabchoice, $cols, $data);
// retrieve past delete/update selection from cookie for this load
$updatesearchby = $_COOKIE['updatesearchby'];
$deletesearchby = $_COOKIE['deletesearchby'];
// delete them as only needed for one load
setcookie("updatesearchby", "", time()-3600);
setcookie("deletesearchby", "", time()-3600);
// set the drop down lists according to the cookie (last delete/update selections)
echo "<script>document.getElementById('tabchoice').value='$tabchoice'</script>";
if (strcmp($_COOKIE['updatesearchby'], "") !== 0) {
echo "<script>document.getElementById('updatesearchby').value='$updatesearchby'</script>";
}
if (strcmp($_COOKIE['deletesearchby'], "") !== 0) {
echo "<script>document.getElementById('deletesearchby').value='$deletesearchby'</script>";
}
}
OCILogoff($db_conn);
}
?>