This repository has been archived by the owner on Oct 10, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaction.php
executable file
·406 lines (308 loc) · 9.35 KB
/
action.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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
<?php
require_once('header.php');
switch($_['action']){
//Gestion du login
case 'LOGIN':
if($_['email']==EMAIL && $_['password']==PASSWORD){
$_SESSION['myUser'] = serialize(array('login'=>EMAIL));
}else{
exit('Bad code');
}
header('location: index.php');
break;
//Gestion de la deconnexion
case 'DISCONNECT':
$_SESSION = array();
session_unset();
session_destroy();
header('location: index.php');
break;
/*
Gestion des prises
*/
//
//Ajouter une prise
//
case 'ADD_ENGINE':
$db = (file_exists(PATH_BDD)?Functions::unstore():array());
//Copie de l'image sur le serveur
$fichier = basename($_FILES['picture']['name']);
move_uploaded_file($_FILES['picture']['tmp_name'], PICTURE_FOLDER .'/'. $fichier);
//Copie des donnees
$newEngine['picture'] = PICTURE_FOLDER .'/'.$fichier;
$newEngine['name'] = $_['name'];
$newEngine['code'] = $_['code'];
$newEngine['state'] = 'off';
$newEngine['description'] = $_['description'];
$newEngine['place'] = $_['idPlace'];
$newEngine['typeEngine'] = $_['typeEngine'];
//Si Aucune cle n'existe alors cree la cle 1 sinon creer la cle suivante
$db['keys']['engines']=(isset($db['keys']['engines'])?$db['keys']['engines']+1:1);
$db['engines']['id-'.$db['keys']['engines']] = $newEngine;
//Enregistrement dans la base JSON
Functions::store($db);
header('location: settings.php');
break;
//
//Modifier une prise
//
case 'MODIFY_ENGINE':
$db = (file_exists(PATH_BDD)?Functions::unstore():array());
//Verifie si la commande n'a pas ete lance a la main
if (!empty($_POST))
{
//Si une image est defini alors la sauvegarder
if ($_FILES['picture']['error'] == 0)
{
$fichier = basename($_FILES['picture']['name']);
move_uploaded_file($_FILES['picture']['tmp_name'], PICTURE_FOLDER .'/'. $fichier);
$db['engines'][$_['idengine']]['picture'] = PICTURE_FOLDER .'/'.$fichier;
}
$db['engines'][$_['idengine']]['name'] = $_['name'];
$db['engines'][$_['idengine']]['code'] = $_['code'];
$db['engines'][$_['idengine']]['description'] = $_['description'];
$db['engines'][$_['idengine']]['typeEngine'] = $_['typeEngine'];
//Si une piece est defini alors la sauvegarder
if (!empty($_['idPlace']))
{
$db['engines'][$_['idengine']]['place'] = $_['idPlace'];
}
//Enregistrement dans la base JSON
Functions::store($db);
//Retour a la configuration de la prise
header('Location: ' . $_SERVER['HTTP_REFERER']);
}
else
{
header('location: index.php');
}
break;
case 'SHOW_ENGINE':
$db = (file_exists(PATH_BDD)?Functions::unstore():array());
break;
//
//Modifier l'etat d'une prise
//
case 'CHANGE_STATE':
//Ouvrir le fichier JSON
$db = (file_exists(PATH_BDD)?Functions::unstore():array());
//Vérifie qu'une ID et un état a été précisé
if (isset($_['engine']) && isset($_['state']))
{
//Récupération des informations de la prise à partir de l'ID
foreach($db['engines'] as $id=>$engine){
if ($_['engine'] == $id)
{
$code = $engine['code'];
$typeengine = $engine['typeEngine'];
}
}
//Si c'est une prise chacon
if( $typeengine == 0)
{
system(' ./chacon '.PIN.' '.SENDER.' '.$code.' '.$_['state']); //Activation
}
//Si prise SCS
if($typeengine == 1){
//Execution de la commande
system(' ./scs '.$code.' '.$_['state']);
}
//Changement de l'état dans la base de données
$db['engines'][$_['engine']]['state'] = $_['state'];
//Sauvegarde dans la BD JSON
Functions::store($db);
//Renvoi sur la page (ou message A vos ordres si YURI est utilisée)
if(!isset($_['provider'])){
header('location: index.php?place='.$db['engines'][$_['engine']]['place']);
}else{
echo 'A vos ordres';
}
}
break;
//
//Effacer une prise
//
case 'DELETE_ENGINE':
$db = (file_exists(PATH_BDD)?Functions::unstore():array());
unset($db['engines'][$_['engine']]);
Functions::store($db);
header('location: settings.php');
break;
//
//Effacer une prise
//
case 'DELETE_ENGINE':
$db = (file_exists(PATH_BDD)?Functions::unstore():array());
unset($db['engines'][$_['engine']]);
Functions::store($db);
header('location: settings.php');
break;
/*
Gestion des piece
*/
//
//Ajouter une piece
//
case 'ADD_PLACE':
$db = (file_exists(PATH_BDD)?Functions::unstore():array());
$newPlace['name'] = $_['place'];
$db['keys']['places']=(isset($db['keys']['places'])?$db['keys']['places']+1:1);
$db['places']['id-'.$db['keys']['places']] = $newPlace;
Functions::store($db);
header('location: settings.php?menu=1');
break;
//
//Effacer une piece
//
case 'DELETE_PLACE':
$db = (file_exists(PATH_BDD)?Functions::unstore():array());
unset($db['places'][$_['place']]);
Functions::store($db);
header('location: settings.php?menu=1');
break;
case 'MODIFY_PLACE':
$db = (file_exists(PATH_BDD)?Functions::unstore():array());
//Verifie si la commande n'a pas ete lance a la main
if (!empty($_POST))
{
$db['places'][$_['idplace']]['name'] = $_['place'];
//Enregistrement dans la base JSON
Functions::store($db);
//Retour a la configuration de la prise
header('Location: ' . $_SERVER['HTTP_REFERER']);
}
else
{
header('location: index.php');
}
break;
/*
Creation d'une programmation
*/
//
//Ajout d'une programmation
//
case 'ADD_CRON':
//Verifie si la commande n'a pas ete lance a la main ou si aucune prise n'a ete selectionnee
if (!empty($_POST))
{
if ($_['idCronedEngine'] != "noid")
{
$db = (file_exists(PATH_BDD)?Functions::unstore():array());
//Engistrement de la programmation dans la base JSON
$newCron['state'] = $_['state'];
$newCron['idCronedEngine'] = $_['idCronedEngine'];
$newCron['time'] = $_['time'];
$newCron['date'] = $_['date'];
$db['keys']['cron']=(isset($db['keys']['cron'])?$db['keys']['cron']+1:1);
$db['cron']['id-'.$db['keys']['cron']] = $newCron;
Functions::store($db);
//Extraction de l'heure et des minutes
$hour = substr($newCron['time'],0,2);
$minutes = substr($newCron['time'],3,4);
//Extraction de l'etat
if ($newCron['state'] == 0){$state="off";}
if ($newCron['state'] == 1){$state="on";}
//Extraction de la date
$date = $newCron['date'] ;
//Extraction du dossier courant
$dir = getcwd();
//Extraction du type de prise
$typeEngine = $db['engines'][$newCron['idCronedEngine']]['typeEngine'];
if ($typeEngine == 0){$program=$dir ."/chacon ".PIN." ".SENDER;}
if ($typeEngine == 1){$program=$dir ."/scs";}
//Extraction du code de la prise
$code = $db['engines'][$newCron['idCronedEngine']]['code'];
//Ajout de la programmation dans la table CRON
system('./cron_add '.$minutes.' '.$hour.' '.$date.' "'.$program.' '.$code.' '.$state.'"');
header('location: settings.php?menu=2');
}
else
{
header('location: settings.php?menu=2&error=1');
}
}
else
{
header('location: index.php');
}
break;
//
//Effacer une programmation
//
case 'DELETE_CRON':
$db = (file_exists(PATH_BDD)?Functions::unstore():array());
//Recuperation de la programmation dans la base JSON
$newCron['state'] = $db['cron'][$_['cron']]['state'];
$newCron['idCronedEngine'] = $db['cron'][$_['cron']]['idCronedEngine'];
$newCron['time'] = $db['cron'][$_['cron']]['time'];
$newCron['date'] = $db['cron'][$_['cron']]['date'];
//Extraction de l'heure et des minutes
$hour = substr($newCron['time'],0,2);
$minutes = substr($newCron['time'],3,4);
//Extraction de l'etat
if ($newCron['state'] == 0){$state="off";}
if ($newCron['state'] == 1){$state="on";}
//Extraction de la date
$date = $newCron['date'] ;
//Extraction du dossier courant
$dir = getcwd();
//Extraction du type de prise
$typeEngine = $db['engines'][$newCron['idCronedEngine']]['typeEngine'];
if ($typeEngine == 0){$program=$dir ."/chacon ".PIN." ".SENDER;}
if ($typeEngine == 1){$program=$dir ."/scs";}
//Extraction du code de la prise
$code = $db['engines'][$newCron['idCronedEngine']]['code'];
//Supression de la programmation dans la table CRON
system('./cron_del '.$minutes.' '.$hour.' '.$date.' "'.$program.'" '.$code.' '.$state);
//Supression de la programmation dans la base JSON
unset($db['cron'][$_['cron']]);
Functions::store($db);
header('location: settings.php?menu=2');
break;
/*
Gestion du controle vocale
*/
case 'GET_YURI_XML':
$db = (file_exists(PATH_BDD)?Functions::unstore():array());
$hccPath = substr($_SERVER['HTTP_REFERER'],0,strrpos($_SERVER['HTTP_REFERER'], '/')).'/action.php';
$template = '<grammar version="1.0" xml:lang="fr-FR" mode="voice" root="ruleEedomus" xmlns="http://www.w3.org/2001/06/grammar" tag-format="semantics/1.0">
<rule id="ruleEedomus" scope="public">
<example>Yuri allume le salon</example>
<tag>out.action=new Object(); </tag>
<item>Yuri</item>
<one-of>
<item>allume<tag>out.action.state="on"</tag></item>
<item>eteint<tag>out.action.state="off"</tag></item>
</one-of>
<one-of>';
foreach($db['engines'] as $id=>$engine){
$template .= '<item>'.$engine['name'].'
<tag>out.action.engine=\''.$id.'\'</tag>
</item>';
}
$template .= '
</one-of>
<tag>out.action.action=\'CHANGE_STATE\'</tag>
<tag>out.action.provider=\'yuri\'</tag>
<tag>out.action._attributes.threashold="0.80";</tag>
<tag>out.action._attributes.uri="'.$hccPath.'";</tag>
</rule>
</grammar>';
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename=hcc_yuri_xml.xml');
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . strlen($template));
ob_clean();
flush();
echo $template;
break;
default:
echo 'Aucune action correcte n\'est spécifiée';
break;
}
?>