-
Notifications
You must be signed in to change notification settings - Fork 0
/
cron-monthly.php
370 lines (315 loc) · 18.5 KB
/
cron-monthly.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
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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
<?php
/*
Copyright 2014-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.
*/
// TODO: every month, if no connection in the last month & if pilot/student/instructor => send reminder about the userid
// TODO: every month, send email about maintenance of the plane + inactive planes
// TODO: every month, statistics on WE ?
require_once 'dbi.php' ;
ob_start() ; // To allow the ob_flush()
// SMTP email debuging & optimization
//$smtp_info['debug'] = True;
$smtp_info['persist'] = True;
$managerEmail = $smtp_from ; // Allow more debugging
$test_mode = false ; // Only send to [email protected] when test_mode is true
$debug = true ;
$bccTo = "[email protected]" ;
$mime_preferences = array(
"input-charset" => "UTF-8",
"output-charset" => "UTF-8",
"scheme" => "Q") ;
$max_profile_count = 7 ;
$actions = (isset($_REQUEST['actions']) and $_REQUEST['actions'] != '') ? trim(strtolower($_REQUEST['actions'])) : 'pblme' ;
// Values for $actions
// p = profile of members
// b = booking
// l = log book
// m = maintenance
// e = email lists
// t = mode test
if (strpos($actions, 't') !== FALSE) $test_mode = true ;
print(date('Y-m-d H:i:s').": starting for actions = {$actions}.\n") ;
journalise(0, 'I', "Cron-monthly: starting for actions = {$actions}") ;
print(date('Y-m-d H:i:s').": preparing lists of plane bookings & logbook entries.\n") ;
$email_body = "<p>Voici la liste mensuelle des diverses réservations des avions du RAPCS.</p>" ;
function print_plane_table($title, $sql, $columns) {
global $email_body, $mysqli_link, $convertToUtf8 ;
$email_body .= "<h2>$title</h2>\n<table border='1'><tr>" ;
foreach ($columns as $column)
$email_body .= "<th>$column</th>" ;
$email_body .= "</tr>\n" ;
print(date('Y-m-d H:i:s') . ": ($title) executing: $sql\n") ;
$result = mysqli_query($mysqli_link, $sql) or die(date('Y-m-d H:i:s') . ": Erreur systeme lors de la lecture des profils: " . mysqli_error($mysqli_link)) ;
$n = 0 ;
while ($row = mysqli_fetch_array($result)) {
$email_body .= "<tr>" ;
for ($i = 0; $i < count($columns) ; $i++) {
$style = (is_numeric($row[$i])) ? ' style="text-align:right"' : '' ;
$email_body .= "<td$style>" . db2web($row[$i]) . "</td>\n" ;
}
$email_body .= "</tr>\n" ;
$n ++ ;
}
$email_body .= "</tr>\n" ;
mysqli_free_result($result) ;
$email_body .= "</table>\n$n ligne(s).<br/>\n" ;
print(date('Y-m-d H:i:s') . ": ($title) $n lines\n") ;
ob_flush() ;
}
if (strpos($actions, 'b') !== FALSE) {
$sql = "select r_plane, count(*), sum(r_duration)
from $table_planes p left join $table_bookings on p.id = r_plane
where p.actif != 0 and p.ressource = 0 and r_cancel_date is null and r_start > date_sub(sysdate(), interval 1 month) and r_type != " . BOOKING_MAINTENANCE . "
group by r_plane" ;
print_plane_table("Réservations du dernier mois", $sql, ['Avion', 'Nbr réservations', 'Durée prévue<br/>en heures']) ;
$sql = "select name, count(*), sum(r_duration) as total_duration
from $table_users p left join $table_bookings on p.id = r_pilot
where r_plane = 'PH-AML' and r_cancel_date is null and r_start > date_sub(sysdate(), interval 1 month) and r_type != " . BOOKING_MAINTENANCE . "
group by r_pilot
order by total_duration desc
limit 0,10" ;
print_plane_table("Réservations PH-AML top-10 du dernier mois", $sql, ['Pilote', 'Nbr réservations', 'Durée prévue<br/>en heures']) ;
$sql = "select name, count(*), sum(r_duration) as total_duration
from $table_users p left join $table_bookings on p.id = r_pilot
where r_plane = 'OO-SPQ' and r_cancel_date is null and r_start > date_sub(sysdate(), interval 1 month) and r_type != " . BOOKING_MAINTENANCE . "
group by r_pilot
order by total_duration desc
limit 0,10" ;
print_plane_table("Réservations OO-SPQ top-10 du dernier mois", $sql, ['Pilote', 'Nbr réservations', 'Durée prévue<br/>en heures']) ;
}
if (strpos($actions, 'l') !== FALSE) {
$sql = "SELECT r_plane, count(l_id), min(l_start_hour), max(l_end_hour), max(l_end_hour * 60 + l_end_minute) - min(l_start_hour * 60 + l_start_minute)
FROM $table_planes p LEFT JOIN $table_bookings ON p.id = r_plane JOIN $table_logbook ON r_id = l_booking
WHERE p.actif != 0 and p.ressource = 0 AND r_cancel_date is null
AND r_start > date_sub(sysdate(), interval 1 month) AND r_start < sysdate()
AND r_type != " . BOOKING_MAINTENANCE . "
GROUP BY l_plane" ;
print_plane_table("Entrées dans les carnets de routes informatiques du dernier mois", $sql, ['Avion', 'Nbr de vols', 'Début', 'Fin', 'Minutes moteur']) ;
}
if (strpos($actions, 'm') !== FALSE) {
$sql = "select r_plane, r_start, r_stop, r_comment
from $table_planes p left join $table_bookings on p.id = r_plane
where p.ressource = 0 and r_cancel_date is null and r_start > date_sub(sysdate(), interval 1 month) and r_type = " . BOOKING_MAINTENANCE . "
order by r_plane, r_start" ;
print_plane_table("Avions en maintenance", $sql, ['Avion', 'Début', 'Fin', 'Commentaire']) ;
}
$email_header = "From: $managerName <$smtp_from>\r\n" ;
$email_header .= "Return-Path: <[email protected]>\r\n" ; // Will set the MAIL FROM enveloppe by the Pear Mail send()
$email_header .= "To: [email protected], [email protected]\r\n" ;
$email_header .= "Cc: [email protected]\r\n" ;
if ($bccTo != '') {
$email_header .= "Bcc: $bccTo\r\n" ;
$email_recipients .= ", $bccTo" ;
}
// Only send if actions b l m are selected
if (strpos($actions, 'b') !== FALSE or strpos($actions, 'l') !== FALSE or strpos($actions, 'm') !== FALSE) {
if ($test_mode) {
$smtp_info['debug'] = True ;
smtp_mail("[email protected]", "Statistiques utilisations des avions (test)", $email_body) ;
} else
@smtp_mail($email_recipients, "Statistiques sur l'utilisation des avions", $email_body, $email_header) ;
ob_flush() ;
mysqli_close($mysqli_link) ; // Sometimes OVH times out ...
$mysqli_link = mysqli_connect($db_host, $db_user, $db_password) ;
if (! $mysqli_link) die("Impossible de se connecter a MySQL:" . mysqli_connect_error()) ;
if (! mysqli_select_db($mysqli_link, $db_name)) die("Impossible d'ouvrir la base de donnees:" . mysqli_error($mysqli_link)) ;
journalise(0, 'I', "Cron-monthly: statistics email sent") ;
}
if (strpos($actions, 'p') !== FALSE) {
journalise(0, 'I', "Cron-monthly: starting checks on users' profiles") ;
// Reminder of incomplete profile
//$joomla_admin_group = 7 ;
//$joomla_pilot_group = 13 ;
//$joomla_student_group = 16 ;
//$joomla_instructor_group = 14 ;
//$joomla_mechanic_group = 17 ;
$sql = "select *,u.name as full_name
from $table_users u left join $table_person p on u.id = p.jom_id
where block = 0 and exists (select * from $table_user_usergroup_map m
where u.id = m.user_id and m.group_id in ($joomla_admin_group, $joomla_pilot_group, $joomla_student_group, $joomla_instructor_group))" ;
print(date('Y-m-d H:i:s') . ": executing: $sql\n") ; ob_flush() ;
$result = mysqli_query($mysqli_link, $sql) or die(date('Y-m-d H:i:s') . ": Erreur systeme lors de la lecture des profils: " . mysqli_error($mysqli_link)) ;
$all_rows = mysqli_fetch_all($result, MYSQLI_ASSOC) ;
foreach ($all_rows as $row) {
$profile_count = 0 ;
$missing_items = array() ;
$full_name = db2web($row['full_name']) ; // SQL DB is latin1 and the rest is in UTF-8
$first_name = db2web($row['first_name']) ; // SQL DB is latin1 and the rest is in UTF-8
if ($row['email'] != '') $profile_count ++ ;
if ($row['first_name'] != '') $profile_count ++ ; else $missing_items[] = '<b>prénom</b>' ;
if ($row['last_name'] != '') $profile_count ++ ; else $missing_items[] = '<b>nom de famille</b>' ;
if ($row['home_phone'] == '') $missing_items[] = 'téléphone privé' ;
if ($row['work_phone'] == '') $missing_items[] = 'téléphone travail' ;
if ($row['cell_phone'] != '') $profile_count ++ ; else $missing_items[] = '<b>téléphone mobile</b>' ;
if ($row['city'] != '') $profile_count ++ ; else $missing_items[] = 'ville' ;
if ($row['country'] == '') $missing_items[] = 'pays' ;
if ($row['sex'] != '' and $row['sex'] != 0) $profile_count ++ ; else $missing_items[] = 'genre' ;
if ($row['birthdate'] != '' and $row['birthdate'] != '0000-00-00 00:00:00') $profile_count ++ ; else $missing_items[] = 'date de naissance' ;
$missing_items_string = implode(', ', $missing_items) ;
if ($debug) print(date('Y-m-d H:i:s').": processing user#$row[jom_id] $row[name]/$row[username]/$full_name: profile items count $profile_count ($missing_items_string).\n") ;
if ($profile_count + 2 >= $max_profile_count) continue ;
if ($profile_count < $max_profile_count/2)
journalise($row['jom_id'], 'W', db2web("Incomplete profile for $row[name]/$row[username]/$row[full_name]: profile items count $profile_count ($missing_items_string)")) ;
if ($row['email'] == '') {
print(date('Y-m-d H:i:s').": no email address... skipping !!!!!\n") ; ob_flush() ;
continue ;
}
// Need to warn the user...
$email_subject = iconv_mime_encode('Subject',
"Votre profil sur www.spa-aviation.be est incomplet", $mime_preferences) ;
$email_message = '' ;
if ($first_name != '')
$email_message .= "<p>Bonjour $first_name,</p>" ;
else
$email_message .= "<p>Bonjour,</p>" ;
$email_message .= "<p>À titre informatif, votre profil sur le site de notre club est incomplet: seulement $profile_count informations sur $max_profile_count...<br/>
Ces informations ne sont visibles que pour les autres membres RAPCS (+ le SPW et notre atelier).
<b>Seules certaines données sont obligatoires pour effectuer une réservation: nom, prénom, email et numéro
de téléphone mobile</b> (ceci afin de vous contacter si nécessaire); les autres informations sont simplement
pour permettre de nous connaître au sein de notre club.</p>
<p>Veuillez visiter le lien ci-dessous et compléter les données manquantes ($missing_items_string):\n" ;
$email_message .= "<a href=https://www.spa-aviation.be/resa/mobile_profile.php>profil réservation</a>.</p>\n" ;
$email_message .= "<p>Pour rappel, votre identifiant est <b>$row[username]</b> et vous devez être connecté(e) pour changer votre profil.</p>\r\n" ;
$email_message .= "<hr>Ceci est un message automatique envoyé tous les mois tant que votre profil n'est pas complet." ;
if ($test_mode) $email_message .= "<hr><font color=red><B>Ceci est une version de test</b></font>" ;
$email_header = "From: $managerName <$smtp_from>\r\n" ;
$email_header .= "Return-Path: <[email protected]>\r\n" ; // Will set the MAIL FROM enveloppe by the Pear Mail send()
$email_header .= "To: $full_name <$row[email]>\r\n" ;
$email_recipients = $row['email'] ;
if ($bccTo != '') {
$email_header .= "Bcc: $bccTo\r\n" ;
$email_recipients .= ", $bccTo" ;
}
$email_header .= "X-Comment: joomla user ID is $row[jom_id]\r\n" ;
if ($test_mode)
smtp_mail("[email protected]", substr($email_subject, 9), $email_message, "Content-Type: text/html; charset=\"UTF-8\"\r\n") ;
else
@smtp_mail($email_recipients, substr($email_subject, 9), $email_message, $email_header) ;
}
mysqli_free_result($result) ;
print(date('Y-m-d H:i:s').": End of profile checks.\n") ; ob_flush() ;
mysqli_close($mysqli_link) ; // Sometimes OVH times out ...
$mysqli_link = mysqli_connect($db_host, $db_user, $db_password) ;
if (! $mysqli_link) die("Impossible de se connecter a MySQL:" . mysqli_connect_error()) ;
if (! mysqli_select_db($mysqli_link, $db_name)) die("Impossible d'ouvrir la base de donnees:" . mysqli_error($mysqli_link)) ;
journalise(0, 'I', "Cron-monthly: email reminders for missing profiles sent") ;
}
if (strpos($actions, 'e') !== FALSE) {
mysqli_close($mysqli_link) ; // Sometimes OVH times out ...
$mysqli_link = mysqli_connect($db_host, $db_user, $db_password) ;
if (! $mysqli_link) die("Impossible de se connecter a MySQL:" . mysqli_connect_error()) ;
if (! mysqli_select_db($mysqli_link, $db_name)) die("Impossible d'ouvrir la base de donnees:" . mysqli_error($mysqli_link)) ;
print(date('Y-m-d H:i:s').": preparing lists of pilots/students/members.\n") ; ob_flush() ;
$email_body = "<p>Voici la liste mensuelle des divers utilisateurs du site RAPCS.</p>" ;
function print_table($title, $sql) {
global $email_body, $mysqli_link, $convertToUtf8 ;
$email_body .= "<h2>$title</h2>\n<table border='1'><tr><th>Username</th><th>Nom</th><th>Email</th></tr>\n" ;
print(date('Y-m-d H:i:s') . ": ($title) executing: $sql\n") ; ob_flush() ;
$result = mysqli_query($mysqli_link, $sql) or die(date('Y-m-d H:i:s') . ": Erreur systeme lors de la lecture des profils: " . mysqli_error($mysqli_link)) ;
$n = 0 ;
while ($row = mysqli_fetch_array($result)) {
$row['full_name'] = db2web($row['full_name']) ; // SQL DB is latin1 and the rest is in UTF-8
$row['username'] = db2web($row['username']) ; // SQL DB is latin1 and the rest is in UTF-8
$email_body .= "<tr><td>$row[username]</td><td>$row[full_name]</td><td>$row[email]</td></tr>\n" ;
$n ++ ;
}
mysqli_free_result($result) ;
$email_body .= "</table>\n$n ligne(s).<br/>\n" ;
print(date('Y-m-d H:i:s') . ": ($title) $n lines\n") ; ob_flush() ;
}
$sql = "select *,concat(u.name, ' - ', count(*), ' vol(s)') as full_name
from $table_users u join $table_bookings b on b.r_pilot = u.id
where u.block = 0 and b.r_start > date_sub(sysdate(), interval 1 month) and b.r_stop < sysdate() and b.r_cancel_date is null and b.r_type != " . BOOKING_MAINTENANCE . "
and not exists (select * from $table_logbook l
where l.l_booking = b.r_id)
group by username
order by full_name" ;
print_table("<span style=\"color: red;\">Pilotes/élèves sans aucune entrée dans les carnets de routes des avions pour des réservations de ce dernier mois</span>", $sql) ;
$sql = "select *,u.name as full_name
from $table_users u join $table_person p on u.id = p.jom_id
where u.block = 0 and (p.cell_phone is null or p.cell_phone = '') and exists (select * from $table_user_usergroup_map m
where u.id = m.user_id and m.group_id in ($joomla_admin_group, $joomla_pilot_group, $joomla_student_group, $joomla_instructor_group))
order by full_name" ;
print_table("<span style=\"color: red;\">Utilisateurs sans numéro de mobile</span>", $sql) ;
$sql = "select *,u.name as full_name
from $table_users u
where block = 0 and not exists (select * from $table_user_usergroup_map m
where u.id = m.user_id and m.group_id in ($joomla_admin_group, $joomla_pilot_group, $joomla_student_group, $joomla_instructor_group))
order by name" ;
print_table("Utilisateurs qui ne sont ni pilotes ni élèves", $sql) ;
$sql = "select *,u.name as full_name
from $table_users u
where block = 0 and exists (select * from $table_user_usergroup_map m
where u.id = m.user_id and m.group_id in ($joomla_student_group))
order by name" ;
print_table("Utilisateurs élèves", $sql) ;
$sql = "select *,u.name as full_name
from $table_users u
where block = 0 and exists (select * from $table_user_usergroup_map m
where u.id = m.user_id and m.group_id in ($joomla_pilot_group))
order by name" ;
print_table("Utilisateurs pilotes ([email protected])", $sql) ;
$sql = "select *,u.name as full_name
from $table_users u
where block = 0 and not exists (select * from $table_bookings b
where b.r_start > date_sub(sysdate(), interval 1 year) and b.r_pilot = u.id and b.r_cancel_date is null
)
order by name" ;
print_table("Utilisateurs sans aucune réservation dans les 12 derniers mois", $sql) ;
$sql = "select *,u.name as full_name
from $table_users u
where block = 0 and exists (select * from $table_user_usergroup_map m
where u.id = m.user_id and m.group_id in ($joomla_board_group))
order by name" ;
print_table("Membres du Conseil d'Administration ([email protected])", $sql) ;
$sql = "select *,u.name as full_name
from $table_users u
where block = 0 and u.username != 'admin' and exists (select * from $table_user_usergroup_map m
where u.id = m.user_id and m.group_id in ($joomla_sysadmin_group, $joomla_superuser_group, $joomla_admin_group))
order by name" ;
print_table("Administrateurs système du site ([email protected])", $sql) ;
$sql = "select *,u.name as full_name
from $table_users u
where block = 0 and exists (select * from $table_user_usergroup_map m
where u.id = m.user_id and m.group_id in ($joomla_flight_pilot_group))
order by name" ;
print_table("Pilotes pour les vols découverte", $sql) ;
$sql = "select *,u.name as full_name
from $table_users u
where block = 0 and exists (select * from $table_user_usergroup_map m
where u.id = m.user_id and m.group_id in ($joomla_flight_manager_group))
order by name" ;
print_table("Gestionnaires des vols découverte", $sql) ;
$email_header = "From: $managerName <$smtp_from>\r\n" ;
$email_header .= "Return-Path: <[email protected]>\r\n" ; // Will set the MAIL FROM enveloppe by the Pear Mail send()
$email_header .= "To: [email protected], [email protected]\r\n" ;
$email_header .= "Cc: [email protected], [email protected]\r\n" ;
if ($bccTo != '') {
$email_header .= "Bcc: $bccTo\r\n" ;
$email_recipients .= ", $bccTo" ;
}
if ($test_mode) {
$smtp_info['debug'] = True;
smtp_mail("[email protected]", "Listes diverses (test)", $email_body, "Content-Type: text/html; charset=\"UTF-8\"\r\n") ;
} else
smtp_mail($email_recipients, "Listes diverses", $email_body, $email_header) ;
mysqli_close($mysqli_link) ; // Sometimes OVH times out ...
$mysqli_link = mysqli_connect($db_host, $db_user, $db_password) ;
if (! $mysqli_link) die("Impossible de se connecter a MySQL:" . mysqli_connect_error()) ;
if (! mysqli_select_db($mysqli_link, $db_name)) die("Impossible d'ouvrir la base de donnees:" . mysqli_error($mysqli_link)) ;
journalise(0, "I", "Cron-monthly: misc lists sent") ;
}
print(date('Y-m-d H:i:s').": end of job.\n") ; ob_flush() ;
journalise(0, "I", "End of monthly cron job (actions=$actions)") ;
?>