forked from jpatokal/openflights
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalsearch.php
262 lines (236 loc) · 7.08 KB
/
alsearch.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
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
<?php
require_once "../php/locale.php";
require_once "../php/db_pdo.php";
include_once 'helper.php';
$name = $_POST["name"];
$alias = $_POST["alias"];
$mode = $_POST["mode"];
if (!$mode || $mode == 'F') {
$iata = $_POST["iata"];
$icao = $_POST["icao"];
$callsign = $_POST["callsign"];
$mode = "F";
} else {
$iata = "";
$icao = "";
$callsign = "";
}
$country = $_POST["country"];
$offset = $_POST["offset"];
$active = $_POST["active"];
$iatafilter = $_POST["iatafilter"];
$action = $_POST["action"];
$alid = $_POST["alid"];
$uid = $_SESSION["uid"] ?? null;
if ($action == "RECORD") {
if (!$uid || empty($uid)) {
printf("0;" . _("Your session has timed out, please log in again."));
exit;
}
// Check for duplicates
$sql = "SELECT * FROM airlines WHERE mode = ? AND (name LIKE ? OR alias LIKE ?)";
$params = [$mode, $name, $name];
// Editing an existing entry, so make sure we're not overwriting something else
if ($alid && $alid != "") {
$sql .= " AND alid != ?";
$params[] = $alid;
}
$sth = $dbh->prepare($sql);
if (!$sth->execute($params)) {
die('0;' . _('Duplicate check failed.'));
}
$row = $sth->fetch();
if ($row) {
printf("0;" .
sprintf(
_("A %s using the name or alias %s exists already."),
MODES_OPERATOR[$mode],
htmlspecialchars($name)
));
exit;
}
if ($alias != "") {
$sql = "SELECT * FROM airlines WHERE mode = ? AND (name LIKE ? OR alias LIKE ?)";
$params = [$mode, $name, $alias];
// Editing an existing entry, so make sure we're not overwriting something else
if ($alid && $alid != "") {
$sql .= " AND alid != ?";
$params[] = $alid;
}
$sth = $dbh->prepare($sql);
if (!$sth->execute($params)) {
die('0;' . _('Duplicate check failed.'));
}
$row = $sth->fetch();
if ($row) {
printf("0;" .
sprintf(
_("A %s using the name or alias %s exists already."),
MODES_OPERATOR[$mode],
htmlspecialchars($alias)
));
exit;
}
}
// IATA duplicates allowed only for non-active airlines
if ($iata != "") {
$sql = "SELECT * FROM airlines WHERE iata = ? AND active = 'Y'";
$params = [$iata];
// Editing an existing entry, so make sure we're not overwriting something else
if ($alid && $alid != "") {
$sql .= " AND alid != ?";
$params[] = $alid;
}
$sth = $dbh->prepare($sql);
if (!$sth->execute($params)) {
die('0;' . _('Duplicate check failed.'));
}
$row = $sth->fetch();
if ($row) {
printf('0;' . sprintf(_('An airline using the IATA code %s exists already.'), htmlspecialchars($iata)));
exit;
}
}
// ICAO duplicates are not
if ($icao != "") {
$sql = "SELECT * FROM airlines WHERE icao = ?";
$params = [$icao];
// Editing an existing entry, so make sure we're not overwriting something else
if ($alid && $alid != "") {
$sql .= " AND alid != ?";
$params[] = $alid;
}
$sth = $dbh->prepare($sql);
if (!$sth->execute($params)) {
die('0;' . _('Duplicate check failed.'));
}
$row = $sth->fetch();
if ($row) {
printf('0;' . sprintf(_('An airline using the ICAO code %s exists already.'), htmlspecialchars($icao)));
exit;
}
}
if (!$alid || $alid == "") {
// Adding new airline
$sql = <<<SQL
INSERT INTO airlines(name, alias, country, iata, icao, callsign, mode, active, uid)
VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?)
SQL;
$params = [
$name,
$alias,
$country,
$iata == "" ? null : $iata,
$icao == "" ? null : $icao,
$callsign,
$mode,
$active,
$uid
];
} else {
// Editing an existing airline
$sql = <<<SQL
UPDATE airlines
SET name = ?, alias = ?, country = ?, iata = ?, icao = ?, callsign = ?, mode = ?, active = ?
WHERE alid = ? AND (uid = ? OR ? IN (?))
SQL;
$params = [
$name,
$alias,
$country,
$iata == "" ? null : $iata,
$icao == "" ? null : $icao,
$callsign,
$mode,
$active,
$alid,
$uid,
$uid,
implode(', ', (array)$OF_ADMIN_UID)
];
}
$sth = $dbh->prepare($sql);
if (!$sth->execute($params)) {
die('0;' . sprintf(_('Adding new %s failed.'), MODES_OPERATOR[$mode]));
}
if (!$alid || $alid == "") {
printf('1;' . $dbh->lastInsertId() . ';' .
sprintf(_('New %s successfully added.'), MODES_OPERATOR[$mode])
);
} elseif ($sth->rowCount() === 1) {
printf('1;' . $alid . ';' . _("Airline successfully edited."));
} else {
printf('0;' . _("Editing airline failed:") . ' ' . $sql);
}
exit;
}
$filters = [];
$filterParams = [];
// Build filter
if ($name) {
$filters[] = "(name LIKE ? OR alias LIKE ?)";
$filterParams[] = $name . '%';
$filterParams[] = $name . '%';
}
if ($alias) {
$filters[] = "(name LIKE ? OR alias LIKE ?)";
$filterParams[] = $alias . '%';
$filterParams[] = $alias . '%';
}
if ($callsign) {
$filters[] = "callsign LIKE ?";
$filterParams[] = $callsign . '%';
}
if ($iata) {
$filters[] = "iata = ?";
$filterParams[] = $iata;
}
if ($icao) {
$filters[] = "icao = ?";
$filterParams[] = $icao;
}
if ($country != "ALL" && $country) {
$filters[] = "country = ?";
$filterParams[] = $country;
}
if ($mode) {
$filters[] = "mode = ?";
$filterParams[] = $mode;
}
if ($active != "") {
$filters[] = "active = ?";
$filterParams[] = $active;
}
if ($mode == "F" && $iatafilter != "false") {
$filters[] = "iata NOT IN ('', 'N/A')";
}
if (!$offset || !is_int($offset)) {
$offset = 0;
}
$sql = "SELECT * FROM airlines WHERE " . implode(" AND ", $filters) . " ORDER BY name";
$sth = $dbh->prepare($sql . " LIMIT 10 OFFSET " . $offset);
if (!$sth->execute($filterParams)) {
die('0;' . sprintf(_('Operation %s failed.'), htmlspecialchars($action)));
}
$sth2 = $dbh->prepare(str_replace("*", "COUNT(*)", $sql));
$sth2->execute($filterParams);
$row = $sth2->fetch();
if ($row) {
$max = $row[0];
}
printf("%s;%s", $offset, $max);
$isAdmin = in_array($uid, (array)$OF_ADMIN_UID);
foreach ($sth->fetchAll(PDO::FETCH_ASSOC) as $row) {
if ($row["uid"] || $isAdmin) {
if ($row["uid"] == $uid || $isAdmin) {
$row["al_uid"] = "own"; // editable
} else {
$row["al_uid"] = "user"; // added by another user
}
} else {
$row["al_uid"] = null; // in DB
}
unset($row["uid"]);
$row["al_name"] = format_airline($row);
print "\n" . json_encode($row);
}