-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcancel_booking.php
190 lines (174 loc) · 9.58 KB
/
cancel_booking.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
<?php
// TODO
// warn the bookers/pilots just before/after
// add pilot/booker's name in error messages...
/*
Copyright 2013-2024 Eric Vyncke
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
ob_start("ob_gzhandler");
require_once 'dbi.php' ;
$response = array() ;
$response['error'] = '' ; // Until now: no error :-)
$response['message'] = '' ; // Until now: no error :-)
// Parameter sanitization
$id = (isset($_REQUEST['id'])) ? trim($_REQUEST['id']) : '' ;
if ($id == '') die("Missing parameter: id") ;
if (!is_numeric($id)) die("Bien essaye... $id") ;
$auth = (isset($_REQUEST['auth'])) ? trim($_REQUEST['auth']) : '' ;
if ($auth && ($auth != md5($id . $shared_secret)))
die("Code ($auth) is not valid for booking $id") ;
$reason = mysqli_real_escape_string($mysqli_link, web2db(trim($_REQUEST['reason']))) ;
$response['id'] = $id ;
$result = mysqli_query($mysqli_link, "select * from $table_bookings where r_id = $id and r_cancel_date is null") ;
if ((!$result) || (mysqli_num_rows($result) == 0))
$response['error'] .= "Cette réservation, $id, n'existe pas, " . mysqli_error($mysqli_link) . '<br/>' ;
else {
$booking = mysqli_fetch_array($result) ;
$pilot_id = $booking['r_pilot'] ;
$instructor_id = $booking['r_instructor'] ;
$booking_type = $booking['r_type'] ;
$plane = $booking['r_plane'] ;
$booking_start = $booking['r_start'] ;
$booking_end = $booking['r_stop'] ;
$response['pilot_id'] = $pilot_id ;
$response['instructor_id'] = $instructor_id ;
$response['plane'] = $plane ;
$response['booking_start'] = $booking_start ;
$response['booking_end'] = $booking_end ;
if (!($userIsAdmin || $userIsMechanic || $userIsInstructor || ($auth != ''))) {
if (($pilot_id != $userId) && ($booking['r_who'] != $userId))
$response['error'] .= "Cette réservation, $id, ne peut pas être modifiée par vous ($userId), uniquement par le pilote $pilot_id ou l'instructeur/admin $booking[r_who]<br/>" ;
}
}
if ($response['error'] == '') {
$result = mysqli_query($mysqli_link, "update $table_bookings set r_cancel_date = sysdate(), r_cancel_reason = '$reason',
r_cancel_who = $userId, r_cancel_address = '" . getClientAddress() . "', r_sequence = r_sequence + 1 where r_id = $id") ;
if ($result && mysqli_affected_rows($mysqli_link) == 1) {
if ($booking_type == BOOKING_CUSTOMER) {
mysqli_query($mysqli_link, "UPDATE $table_flights SET f_booking = NULL, f_date_linked = NULL WHERE f_booking = $id")
or $response['error'] .= "Cannot delete customer booking: " . mysqli_error($mysqli_link) ;
}
$result = mysqli_query($mysqli_link, "select name, email from $table_users where id = $pilot_id") ;
$pilot = mysqli_fetch_array($result) ;
$pilot['name'] = db2web($pilot['name']) ; // SQL DB is latin1 and the rest is in UTF-8
if (($auth != '') or ($pilot_id == $userId))
$booker = $pilot ;
else {
$result = mysqli_query($mysqli_link, "select name, email from $table_users where id = $userId") ;
$booker = mysqli_fetch_array($result) ;
$booker['name'] = db2web($booker['name']) ; // SQL DB is latin1 and the rest is in UTF-8
}
if ($booking['r_instructor']) {
$result = mysqli_query($mysqli_link, "select name, email from $table_users where id = $booking[r_instructor]") ;
$instructor = mysqli_fetch_array($result) ;
$instructor['name'] = db2web($instructor['name']) ; // SQL DB is latin1 and the rest is in UTF-8
}
$mime_preferences = array(
"input-charset" => "UTF-8",
"output-charset" => "UTF-8",
"scheme" => "Q") ;
if ($booking_type == BOOKING_MAINTENANCE) {
$response['message'] = "La maintenance de $plane du $booking_start au $booking_end: est annulée" ;
$email_subject = iconv_mime_encode('Subject',
"Annulation de la mise en maintenance de $plane par $booker[name] [#$id]",
$mime_preferences) ;
$email_message = "<p>La maintenance du $booking_start au $booking_end sur le $plane " ;
$email_message .= "est annulée.<br/>" ;
} else {
$response['message'] = "La réservation de $plane du $booking_start au $booking_end: est annulée" ;
$email_subject = iconv_mime_encode('Subject',
"Annulation d'une réservation de $plane par $booker[name] pour $pilot[name] [#$id]",
$mime_preferences) ;
if ($email_subject === FALSE)
$email_subject = "Cannot iconv(pilot/$pilot[name])" ;
$email_message = "<p>La réservation du $booking_start au $booking_end sur le $plane " ;
$email_message .= "avec $pilot[name] en pilote est annulée.<br/>\n" ;
}
$email_message .= "Cette opération a été effectuée par $booker[name]. \n" ;
if ($reason) $email_message .= "La raison donnée est: <i>" . trim($_REQUEST['reason']) . "</i>.\n" ;
$email_message .= "</p>\n" ;
if ($test_mode) $email_message .= "<hr><p><font color=red><B>Ceci est une version de test</b></font></p>\n" ;
$email_header = "From: $managerName <$smtp_from>\r\n" ;
if (!$test_mode) {
$email_header .= "To: $pilot[name] <$pilot[email]>\r\n" ;
$email_recipients = $pilot['email'] ;
if ($pilot_id != $userId and $booker['email'] != '' and !$userIsInstructor) { // Don't send warning to FI
$email_header .= "Cc: $booker[name] <$booker[email]>\r\n" ;
$email_recipients .= ", $booker[email]" ;
}
if (!$userIsInstructor and $booking['r_instructor']) { // Only warns FI when cancelled by the student
$email_header .= "Cc: $instructor[name] <$instructor[email]>\r\n" ;
$email_recipients .= ", $instructor[email]" ;
}
if ($booking_type == BOOKING_MAINTENANCE) {
$email_header_recipients .= "Cc: $fleetName <$fleetEmail>\r\n" ;
$email_recipients .= ", $fleetEmail" ;
}
if ($bccTo != '') {
$email_recipients .= ", $bccTo" ;
}
}
$email_header .= "X-Comment: reservation is $id\r\n" ;
$email_header .= "References: <booking-$id@$smtp_localhost>\r\n" ;
$email_header .= "In-Reply-To: <booking-$id@$smtp_localhost>\r\n" ;
$email_header .= "Thread-Topic: Réservation RAPCS #$id\r\n" ;
$email_header .= "Content-Type: text/html; charset=UTF-8\r\n" ;
if ($test_mode)
smtp_mail("eric.vyncke@ulg.ac.be", substr($email_subject, 9), $email_message, $email_header) ;
else
@smtp_mail($email_recipients, substr($email_subject, 9), $email_message, $email_header) ;
if ($booking_type == BOOKING_MAINTENANCE)
journalise($userId, 'W', "Cancellation of maintenance #$id of $plane. $booking_start => $booking_end") ;
else
journalise($userId, 'W', "Cancellation of booking #$id of $plane done for $pilot[name] by $booker[name]. $booking_start => $booking_end. Reason: " . trim($_REQUEST['reason'])) ;
} else
$response['error'] .= "Un problème technique s'est produit, annulation non effectuée..." . mysqli_error($mysqli_link) . "<br/>" ;
}
// Let's send the data back
header('Content-type: application/json');
print(json_encode($response)) ;
if ($response['error'])
journalise($userId, 'E', "Error ($response[error]) while cancelling booking #$id of $plane done for $pilot[name] by $booker[name]. $booking_start => $booking_end") ;
else {
// Warn by email the previous and next bookings if any
$result = mysqli_query($mysqli_link, "select * from $table_bookings, $table_users
where r_plane = '$plane' and r_cancel_date is null and r_start = '$booking_end' and (id = r_pilot or id = r_instructor)")
or die("Cannot fetch previous booking: " . mysqli_error($mysqli_link)) ;
email_adjacent($result, $booking, $booker) ;
$result = mysqli_query($mysqli_link, "select * from $table_bookings, $table_users
where r_plane = '$plane' and r_cancel_date is null and r_stop = '$booking_start' and (id = r_pilot or id = r_instructor)")
or die("Cannot fetch previous booking: " . mysqli_error($mysqli_link)) ;
email_adjacent($result, $booking, $booker) ;
}
function email_adjacent($result, $booking, $booker) {
global $managerName, $managerEmail, $userId, $convertToUtf8, $smtp_localhost, $smtp_from ;
$row = mysqli_fetch_array($result) ;
if (!$row) return ;
// No need to warn if next is the same
if ($row['r_instructor'] == $booking['r_instructor']) return ;
if ($row['r_pilot'] == $booking['r_pilot']) return ;
$row['name'] = db2web($row['name']) ; // SQL DB is latin1 and the rest is in UTF-8
$id = $row['r_id'] ; // Get back the adjacent booking ID
$email_header = "From: $managerName <$smtp_from>\r\n" ;
$email_header .= "To: $row[name] <$row[email]>\r\n" ;
$email_header .= "X-Comment: reservation is $id\r\n" ;
$email_header .= "References: <booking-$id@$smtp_localhost>\r\n" ;
$email_header .= "In-Reply-To: <booking-$id@$smtp_localhost>\r\n" ;
$email_header .= "Thread-Topic: Réservation RAPCS #$id\r\n" ;
$email_header .= "Content-Type: text/html; charset=UTF-8\r\n" ;
$email_message = "<p>Bonjour,</p><p>Pour votre information, suite à une annulation d'une autre réservation par $booker[name], le $row[r_plane]
est maintenant disponible du $booking[r_start] au $booking[r_stop]. N'hésitez donc pas à étendre votre
réservation.</p>" ;
@smtp_mail($row['email'], "$row[r_plane] est disponible du $booking[r_start] au $booking[r_stop]", $email_message, $email_header) ;
journalise($userId, 'I', "Warning $row[name] by email that booking can be extended") ;
}
?>