-
Notifications
You must be signed in to change notification settings - Fork 6
/
api.php
168 lines (141 loc) · 5.12 KB
/
api.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
<?php
require_once 'app/bootstrap.php';
use Ladecadanse\Evenement;
use Ladecadanse\Utils\Validateur;
use Ladecadanse\Utils\Logger;
if (empty($_SERVER['PHP_AUTH_USER']) || empty($_SERVER['PHP_AUTH_PW']) || !($_SERVER['PHP_AUTH_USER'] == LADECADANSE_API_USER && $_SERVER['PHP_AUTH_PW'] == LADECADANSE_API_KEY))
{
header('WWW-Authenticate: Basic realm="La décadanse"');
header($_SERVER['SERVER_PROTOCOL'] . ' 401 Unauthorized');
die('Not authorized');
}
$tab_entity = ['event'];
try
{
$get['entity'] = Validateur::validateUrlQueryValue($_GET['entity'], 'enum', 1, $tab_entity);
} catch (Exception $e)
{
header($_SERVER['SERVER_PROTOCOL'] . ' 400 Bad Request');
exit;
}
if (!array_key_exists(trim($_GET['region']), $glo_regions))
{
echo 'Unknown region';
header($_SERVER['SERVER_PROTOCOL'] . ' 400 Bad Request');
exit;
}
$get['region'] = trim($_GET['region']);
try
{
$get['date'] = Validateur::validateUrlQueryValue(trim($_GET['date']), 'date', 1);
} catch (Exception $e)
{
header($_SERVER['SERVER_PROTOCOL'] . ' 400 Bad Request');
exit;
}
if (!preg_match('/^[0-9]{2}:[0-9]{2}:[0-9]{2}$/', trim($_GET['endtime'])))
{
header($_SERVER['SERVER_PROTOCOL'] . ' 400 Bad Request');
exit;
}
$get['endtime'] = trim($_GET['endtime']);
$eventCategories = [
'fête',
'cinéma',
'théâtre',
'expos',
'divers',
];
try
{
$get['category'] = Validateur::validateUrlQueryValue($_GET['category'], 'enum', 1, $eventCategories);
} catch (Exception $e)
{
header($_SERVER['SERVER_PROTOCOL'] . ' 400 Bad Request');
exit;
}
$result = [];
if ($get['entity'] == 'event')
{
// if nighttime : get endtime is > 0h and < 6h
$datetimeMinEnd = new Datetime($get['date']);
$datetimeMinEnd->modify('+1 day');
$events = [];
$sql = "SELECT e.*, eloc.localite eLocalite, l.nom lNom, l.adresse lAdresse, l.quartier lQuartier, loc.localite lLocalite, l.URL lUrl
FROM evenement e
LEFT JOIN localite eloc ON e.localite_id = eloc.id
LEFT JOIN lieu l ON e.idLieu = l.idLieu
LEFT JOIN localite loc ON l.localite_id = loc.id
WHERE dateEvenement = '" . $connector->sanitize($get['date']) . "' AND genre = '" . $connector->sanitize($get['category']) . "' AND e.statut NOT IN ('propose') AND e.region IN ('" . $connector->sanitize($get['region']) . "')";
// noctambus : don't covers 'rf', 'hs'
if (!empty($get['endtime']))
{
$sql .= "AND horaire_fin >= '" . $datetimeMinEnd->format('Y-m-d') . ' ' . $connector->sanitize($get['endtime']) . "' AND horaire_fin < '" . $datetimeMinEnd->format('Y-m-d') . " 06:00:01'";
// noctambus : covers from 01h to...
}
$sql .= ' ORDER BY e.dateAjout DESC';
// echo $sql;
$req_even = $connector->query($sql);
while ($row = $connector->fetchAssoc($req_even))
{
// dump($row);
$event = [
'idevenement' => $row['idevenement'],
'statut' => $row['statut'],
'titre' => $row['titre'],
];
// image : if flyer empty, image; add path
if (!empty($row['image']))
{
$event['image'] = Evenement::getFileHref(Evenement::getFilePath($row['image']));
}
elseif (!empty($row['flyer']))
{
$event['image'] = Evenement::getFileHref(Evenement::getFilePath($row['flyer']));
}
$event['description'] = $row['description'];
$event['references'] = $row['ref'];
// TODO (organisateurs)
// horaire
$event['horaire'] = [
'debut' => $row['horaire_debut'],
'fin' => $row['horaire_fin'],
'complement' => $row['horaire_complement'],
];
$event['prix'] = $row['prix'];
$event['prelocations'] = $row['prelocations'];
// lieu directly from event table
$event['lieu'] = [
'nom' => $row['nomLieu'],
'adresse' => $row['adresse'],
'quartier' => $row['quartier'],
'localite' => $row['eLocalite'],
'url' => $row['urlLieu'],
];
// or from registered lieu
if (!empty($row['idLieu']))
{
$event['lieu'] = [
'nom' => $row['lNom'],
'adresse' => $row['lAdresse'],
'quartier' => $row['lQuartier'],
'localite' => $row['lLocalite'],
'url' => $row['lUrl'],
];
}
$event['created'] = $row['dateAjout'];
$event['updated'] = $row['date_derniere_modif'];
$events[] = $event;
} //end while
//$events[0]['horaire']['debut'] = "2023-05-21 00:30:00";
//$events[1]['lieu']['nom'] = "";
//$events[1]['titre'] = "";
$result = [
'date' => $get['date'],
'events' => $events,
];
}//end if
$logger->log('global', 'api', "GET ".count($events)." item(s) for ".$get['entity']." ".$get['category']." in ". $get['region']." the ".$get['date']." until ".$get['endtime']." by user ".LADECADANSE_API_USER, Logger::GRAN_YEAR);
header('Content-Disposition: inline; filename=' . $get['entity'] . '.json');
header('Content-Type: application/json; charset=UTF-8');
echo json_encode($result);