-
Notifications
You must be signed in to change notification settings - Fork 10
/
freebox.class.php
558 lines (492 loc) · 13.3 KB
/
freebox.class.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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
<?php
/**
* API Freebox OS
* https://github.com/DjMomo/ClassePhpFreebox
*
*/
class apifreebox
{
private $IP;
private $PORT;
private $BASE_URL;
private $URL;
private $APP_TOKEN;
private $TRACK_ID;
private $APP_ID;
private $APP_NAME;
private $APP_VERSION;
private $DEVICE_NAME;
private $SESSION_TOKEN;
//protected $API = array();
/**
* Constructeur
* @param array configuration
*/
public function __construct($config)
{
// On assigne les paramètres aux variables d'instance.
$this->IP = $config['url'];
$this->PORT = $config['port'];
$this->APP_ID = $config['app_id'];
$this->APP_NAME = $config['app_name'];
$this->APP_VERSION = $config['app_version'];
$this->DEVICE_NAME = $_SERVER["SERVER_NAME"];
$api_info = $this->_GetAPIInfo();
$api_version = explode(".",$api_info['api_version']);
$this->BASE_URL = $this->IP.":".$this->PORT.$api_info['api_base_url']."v".$api_version[0]."/";
$this->_NewSession();
}
/**
* Retourne les méthodes "Getxxxx" d'une classe
* @param string objet de la classe
*/
public function GetMethodsList($class)
{
$methods = get_class_methods($class);
$i = 0;
$j = 1; // ignore __construct
foreach ($methods as $method)
{
if ((strtolower(substr($method,0,3))) === "get")
{
$list[$i] = $method;
$i++;
}
$j++;
}
return $list;
}
/**
* Retourne la version de l'API Freebox
*/
public function _GetAPIInfo ()
{
$this->URL = $this->IP.':'.$this->PORT.'/api_version';
return $this->get();
}
/**
* Effectue l'authentification auprès de la Freebox
* @param bool Force le rafraichissement du token
*/
public function _RequestAuth($force_refresh = 0)
{
$appURL = "login/authorize/";
$TokenFile = "token";
$appParams = array (
"app_id" => $this->APP_ID,
"app_name" => $this->APP_NAME,
"app_version" => $this->APP_VERSION,
"device_name" => $this->DEVICE_NAME
);
if ((!file_exists($TokenFile)) || ($force_refresh == 1))
{
$result = $this->setURL($appURL)->post($appParams);
$csvdata = array(
$result['result']['app_token'],
$result['result']['track_id']
);
if ($result['success'] == true)
{
if ((is_writable($TokenFile)) || (!file_exists($TokenFile)))
{
if (!$fp = fopen($TokenFile, 'w'))
{
echo "Impossible d'ouvrir le token $TokenFile";
exit;
}
fputcsv($fp, $csvdata);
$this->APP_TOKEN = $result['result']['app_token'];
$this->TRACK_ID = $result['result']['track_id'];
fclose($fp);
}
}
else
echo "Accès au token $TokenFile interdit";
}
else
{
$data = file_get_contents($TokenFile);
if ( $data !== FALSE)
{
$data = explode(',',$data);
$this->APP_TOKEN = trim($data[0]);
$this->TRACK_ID = trim($data[1]);
}
}
}
/**
* Récupère le status d'accès
*/
public function _GetAuthStatus()
{
if (strcmp($this->TRACK_ID,"") != 0)
{
$appURL = "login/authorize/".$this->TRACK_ID;
$response = $this->setURL($appURL)->get();
}
else
{
$this->_RequestAuth();
if (strcmp($this->TRACK_ID,"") != 0)
{
$appURL = "login/authorize/".$this->TRACK_ID;
$response = $this->setURL($appURL)->get();
}
else
$response = array('success' => true, 'result' => array('status' => 'unknown'));
}
return $response;
}
/**
* Récupère le token de la session
*/
public function _NewSession()
{
$appURL = "login/session/";
$response = $this->_GetAuthStatus();
if (strcmp($response['result']['status'],'granted') != 0)
{
$this->_RequestAuth();
$response = $this->_GetAuthStatus();
while (strcmp($response['result']['status'],'pending') == 0)
{
sleep(1);
echo "<br />Attente d'autorisation pour l'application. Validez l'accès depuis l'afficheur de la Freebox Server.";
$response = $this->_GetAuthStatus();
}
if (strcmp($response['result']['status'],'granted') != 0)
{
echo "Request access ".$response['result']['status']." !";
exit;
}
}
$password = $this->hmac_sha1($this->APP_TOKEN, $response['result']['challenge']);
$appParams = array (
"app_id" => $this->APP_ID,
"app_version" => $this->APP_VERSION,
"password" => $password
);
$response = $this->setURL($appURL)->post($appParams);
$this->SESSION_TOKEN = $response['result']['session_token'];
}
/**
* Récupère les droits d'accès
*/
public function _GetRights()
{
$result = $this->_NewSession();
return $result['permissions'];
}
# allow magic acces : $freebox->API->method()
public function __get($name){
if(array_key_exists($name, $this->API))
return $this->API[$name];
else
throw new Exception ("API $name doesn't exists");
}
public function setURL ($pUrl)
{
$this->URL = $this->BASE_URL.$pUrl;
return $this;
}
public function get ($pParams = array())
{
return $this->_InterrogerAPI ($this->URL,'GET');
}
public function post ($pPostParams=array(), $pGetParams = array())
{
return $this->_InterrogerAPI ($this->URL,'POST',$pPostParams);
}
public function put ($pPutParams = array())
{
return $this->_InterrogerAPI ($this->URL,'PUT',$pPutParams);
}
public function delete ($pDelParams = array())
{
return $this->_InterrogerAPI ($this->URL,'DELETE',$pDelParams);
}
public function _createContext($pMethod, $pHeader = null, $pContent = null)
{
if (strcmp($this->SESSION_TOKEN, "") != 0)
$auth = "\r\nX-Fbx-App-Auth: ".$this->SESSION_TOKEN."\r\n";
else
$auth = null;
$opts = array(
'http'=>array(
'method'=>$pMethod,
'header'=>'Content-type: application/x-www-form-urlencoded' . $auth, //application/x-www-form-urlencoded
)
);
if ($pContent !== null)
{
if (is_array($pContent))
$pContent = http_build_query($pContent);
$opts['http']['content'] = $pContent;
}
return stream_context_create($opts);
}
public function _makeUrl($pParams)
{
return $this->URL
.(strpos($this->URL, '?') ? '' : '?')
.http_build_query($pParams);
}
public function _launch ($pUrl, $context)
{
if (($stream = fopen($pUrl, 'r', false, $context)) !== false)
{
$content = stream_get_contents($stream);
$header = stream_get_meta_data($stream);
fclose($stream);
return array('content'=>$content, 'header'=>$header);
}
else
return false;
}
public function _InterrogerAPI ($pUrl, $context, $Params = null)
{
$headers = array ('Content-type: application/x-www-form-urlencoded');
if (strcmp($this->SESSION_TOKEN,null)!=0)
$headers = array ('X-Fbx-App-Auth: '.$this->SESSION_TOKEN);
// Initialisation de la connexion avec CURL.
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $pUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POST, 0);
if ($context === "DELETE")
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE");
if ($context === "POST")
{
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($Params));
}
if ($context === "PUT")
{
/** use a max of 256KB of RAM before going to disk */
$fp = fopen('php://temp/maxmemory:256000', 'w');
if (!$fp)
die('could not open temp memory data');
fwrite($fp, json_encode($Params));
fseek($fp, 0);
curl_setopt($ch, CURLOPT_PUT, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
curl_setopt($ch, CURLOPT_INFILE, $fp);
curl_setopt($ch, CURLOPT_INFILESIZE, strlen(json_encode($Params)));
}
$retour_curl = curl_exec($ch);
curl_close($ch);
// On essaye de décoder le retour JSON.
$retour_json = json_decode( $retour_curl, true );
// Gestion minimale des erreurs.
if( $retour_json === false )
throw new Exception("Erreur dans le retour JSON !");
if( isset($retour_json['error']) )
throw new Exception( json_encode($retour_json) );
return $retour_json;
}
private function hmac_sha1($key, $data)
{
// Adjust key to exactly 64 bytes
if (strlen($key) > 64) {
$key = str_pad(sha1($key, true), 64, chr(0));
}
if (strlen($key) < 64) {
$key = str_pad($key, 64, chr(0));
}
// Outter and Inner pad
$opad = str_repeat(chr(0x5C), 64);
$ipad = str_repeat(chr(0x36), 64);
// Xor key with opad & ipad
for ($i = 0; $i < strlen($key); $i++)
{
$opad[$i] = $opad[$i] ^ $key[$i];
$ipad[$i] = $ipad[$i] ^ $key[$i];
}
return sha1($opad.sha1($ipad.$data, true));
}
public function GetListClasses()
{
$dirname = './API/';
$dir = opendir($dirname);
while($file = readdir($dir))
{
if($file != '.' && $file != '..' && !is_dir($dirname.$file) )
{
$classe = substr($file,0,strlen($file) - 4);
$array[] = $classe;
}
}
closedir($dir);
return $array;
}
public function write_ini_file($assoc_arr, $path, $has_sections=FALSE, $initial_content = "")
{
$content = $initial_content;
if ($has_sections)
{
foreach ($assoc_arr as $key=>$elem)
{
$content .= "\n[".$key."]\n";
if (isset($elem))
foreach ($elem as $key2=>$elem2)
{
if(is_array($elem2))
{
for($i=0;$i<count($elem2);$i++)
{
$content .= $key2."[] = \"".$elem2[$i]."\"\n";
}
}
else if($elem2=="") $content .= $key2." = \n";
else $content .= $key2." = \"".$elem2."\"\n";
}
}
}
else {
foreach ($assoc_arr as $key=>$elem)
{
if(is_array($elem))
{
for($i=0;$i<count($elem);$i++)
{
$content .= $key."[] = \"".$elem[$i]."\"\n"; //$key2
}
}
else if($elem=="") $content .= $key." = \n"; //$key2
else $content .= $key." = \"".$elem."\"\n"; //$key2
}
}
if (!$handle = fopen($path, 'w'))
{
return false;
}
if (!fwrite($handle, $content))
{
fclose($handle);
return false;
}
fclose($handle);
return true;
}
public function convert_truefalse_to_boolean($var)
{
if ($var === true) return 1;
if ($var === false) return 0;
return $var;
}
public function convert_truefalse_to_text($var)
{
if ($var === true) return "true";
if ($var === false) return "false";
return $var;
}
/* Retourne tous les paramètres de la freebox dans un fichier XML */
public function config_to_XML()
{
$array_classes = $this->GetListClasses();
// Création fichier XML avec les données
// Instance de la class DomDocument
$doc = new DOMDocument();
// Definition de la version et de l'encodage
$doc->version = '1.0';
$doc->encoding = 'UTF-8';
$doc->formatOutput = true;
// Ajout d'un commentaire a la racine
$comment_elt = $doc->createComment(utf8_encode('Données de la Freebox Révolution - Boitier Server'));
$doc->appendChild($comment_elt);
$racine = $doc->createElement('freeboxOS');
// Ajout la balise 'update' a la racine
$version_elt = $doc->createElement('update',date("Y-m-d H:i"));
$racine->appendChild($version_elt);
foreach ($array_classes as $class_name)
{
$Classe_Elt = $doc->createElement($class_name);
$class_methods = get_class_methods($class_name);
$class = new $class_name($this);
foreach ($class_methods as $method_name)
{
if (substr($method_name,0,3) === "Get")
{
$r = new ReflectionMethod($class_name, $method_name);
$params = $r->getNumberOfParameters();
if ($params == 0)
{
$datas = call_user_func(array($class, $method_name));
$Method_Elt = $doc->createElement($method_name);
DomArrayToXml($datas["result"], $doc, $Method_Elt);
$Classe_Elt->appendChild($Method_Elt);
}
}
$racine->appendChild($Classe_Elt);
}
}
$doc->appendChild($racine);
return $doc->saveXML();
}
public function DisplayResult($json,$action = "")
{
if ($json["success"] == true)
{
echo "$action OK !";
echo "<br />Données supplémentaires retournées par la Freebox : ";
$keys = array_keys($json["result"]);
$i = 0;
foreach ($json["result"] as $value)
{
$value = $this->convert_truefalse_to_text(utf8_decode($value));
echo "<br />".$keys[$i]."= $value";
$i++;
}
}
else
{
echo "Erreur lors de l'exécution de la commande $action.";
echo "<br />Le message d'erreur est le suivant : ".utf8_decode($json["msg"])." (".utf8_decode($json["error_code"]).")";
}
}
}
/**
* Inclut la classe à partir d'un fichier
* @param string Nom de la classe à inclure
*/
function __autoload($classname)
{
$filename = "./API/". $classname .".php";
include_once($filename);
}
/**
* Converti (récursivement) un array en XML (via DOM)
*/
function DomArrayToXml($array, $dom_doc, $node)
{
$array_special_char = array(true, false, "<", ">", "&", "'", "\"");
$array_replace_char = array(1,0,"<", ">", "&","'", """);
if (is_array($array))
{
foreach($array as $key => $item)
{
if (is_numeric($key))
$key = "id-".$key;
if(is_array($item))
{
$element = $dom_doc->createElement($key);
DomArrayToXml($item, $dom_doc, $element);
$node->appendChild($element);
}
else
{
//if ($item === true) $item = 1;
//if ($item === false) $item = 0;
$encoded_item = str_replace ($array_special_char, $array_replace_char,$item);
$element = $dom_doc->createElement($key,utf8_encode($encoded_item));
$node->appendChild($element);
}
}
}
else
{
$element = $dom_doc->createElement("Datas",utf8_encode("Nothing"));
$node->appendChild($element);
}
}