-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfritz.php
545 lines (485 loc) · 16.5 KB
/
fritz.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
<?php
//
// Simple class to automate some FritzBox stuff
// Base Idea: https://www.symcon.de/forum/threads/25745-FritzBox-mit-SOAP-auslesen-und-steuern
// Build as PHP class and extend for send requests via web to FritzBox
//
class Fritz {
protected $fb_ip = '';
protected $fb_user = '';
protected $fb_pass = '';
protected $client = null;
protected $connected = null;
//
// Base functions
//
/**
* Constructor
* @param $fb_ip (string) ip address or url for the FritzBox
* @param $fb_pass (string) password for the FritzBox
* @param $fb_user (string) username for the FritzBox (default = fritz-api)
*/
public function __construct( $fb_ip = null, $fb_pass = null, $fb_user = null ) {
if( $fb_ip === null || $fb_pass == null ) return false;
if( $fb_user === null ) $this->fb_user = "fritz-api";
$this->fb_ip = $fb_ip;
$this->fb_pass = $fb_pass;
$this->setCon( true ); // TODO, validate connection to FritzBox
}
/**
* Client Helper function
*/
public function client( $upnp = null, $param = null, $uri = null ) {
if( $upnp === null || $upnp === "" ) return;
if( $param === null || $param === "" ) return;
if( $uri === null || $uri === "" ) return;
try {
$this->client = new SoapClient(
null,
array(
'location' => "http://" . $this->fb_ip . ":49000/" . $upnp . "/control/" . $param,
'uri' => $uri,
'noroot' => True,
'login' => $this->fb_user,
'password' => $this->fb_pass,
'trace' => True,
'exceptions' => false
)
);
} catch ( SoapFault $e ) {
// echo $e->getMessage();
echo $e->faultstring;
}
}
/**
* Set connection state
* @return (boolean)
*/
public function setCon( $state = null ) {
if( $state === null ) return false;
$this->connected = $state;
}
/**
* Get connection state
* @return (boolean)
*/
public function getCon() {
return $this->connected;
}
//
// Services
//
public function getServices() {
$fburl = "http://" . $this->fb_ip . ":49000/";
$services = array(
'tr64desc.xml', // TR64 Info
// 'any.xml', // Dummy
// 'deviceinfoSCPD.xml', // Device Info
// 'deviceconfigSCPD.xml', // Device Config
// 'layer3forwardingSCPD.xml', // Layer 3 Forwarding
// 'lanconfigsecuritySCPD.xml',// LAN config security
// 'mgmsrvSCPD.xml', // Management Server
// 'userifSCPD.xml', // User Interface
// 'x_voipSCPD.xml', // X VoIP
// 'x_storageSCPD.xml', // X AVM DE Storage
// 'x_contactSCPD.xml', // X AVM DE OnTel
// 'x_webdavSCPD.xml', // X AVM DE WebDav Client
// 'x_upnpSCPD.xml', // X AVM DE UPnP
// 'x_speedtestSCPD.xml', // X AVM DE Speedtest
// 'x_remoteSCPD.xml', // X AVM DE Remote Access
// 'x_myfritzSCPD.xml', // X AVM DE MyFritz
// 'x_tamSCPD.xml', // X AVM DE TAM
// 'x_appsetupSCPD.xml', // X AVM DE App Setup
// 'x_homeautoSCPD.xml', // X AVM DE Homeauto
// 'x_homeplugSCPD.xml', // X AVM DE Homeplug
// 'x_dectSCPD.xml', // X AVM DE DECT
// 'x_filelinksSCPD.xml', // X AVM DE Filelinks
// 'wlanconfigSCPD.xml', // X AVM Wlan configuration (urn 1,2)
// 'hostsSCPD.xml', // Hosts
// 'ethifconfigSCPD.xml', // LAN Ethernet Interface config
// 'lanhostconfigmgmSCPD.xml', // LAN Host config Management
// 'wancommonifconfigSCPD.xml',// WAN Common Interface config
// 'wandslifconfigSCPD.xml', // WAN DSL Interface config
// 'wandsllinkconfigSCPD.xml', // WAN DSL Link config
// 'wanethlinkconfigSCPD.xml', // WAN Ethernet Link config
// 'wanpppconnSCPD.xml', // WAN PPP connection
// 'wanipconnSCPD.xml', // WAN IP connection
// 'igddesc.xml', // Base
// 'igdicfgSCPD.xml' // WAN Common Interface config
// 'igddslSCPD.xml' // WAN DSL Link config
// 'igdconnSCPD.xml', // WAN IP Connection
// 'igd2ipv6fwcSCPD.xml', // Firewall IPv6 Control
// 'MediaServerDevDesc.xml', // Mediaserver
// 'MediaServerContentDirectory.xml', // Content Directory
// 'MediaServerConnectionManager.xml', // Connection Manager
// 'MediaReceiverRegistrar.xml', // X MS Media Receiver Registrar
// 'ServerStatus.xml', // AVM ServerStatus
// 'onlinestoredesc.xml', // ?
// 'fboxdesc.xml', // ?
// 'fboxSCPD.xml', // ?
// 'usbdesc.xml', // USB Device
// 'usbSCPD.xml', // USB Info
// 'timeSCPD.xml' // Time
);
$result = array();
foreach( $services as $service ) {
$url = $fburl . $service;
if( $this->getHeaderCode( $url ) == 200 ) {
$result[] = @simplexml_load_file( $url );
}
}
return $result;
}
public function getFirewallInfo() {
$this->client( 'igd2upnp', 'WANIPv6Firewall1', 'urn:schemas-upnp-org:service:WANIPv6FirewallControl:1' );
return $this->client->GetOutboundPinholeTimeout();
}
public function getDialUp() {
$this->client( 'upnp', 'x_voip', 'urn:dslforum-org:service:X_VoIP:1' );
return $client->{"X_AVM-DE_DialHangup"}();
}
/**
* GetInfo
* @param $value (string) model, serial, version, uptime, log
* @return (string)
*/
public function getInfo( $value = null, $lines = null ) {
if( $lines === null ) $lines = 5;
$this->client( 'upnp', 'deviceinfo', 'urn:dslforum-org:service:DeviceInfo:1' );
$data = $this->client->GetInfo();
if( $value === 'model' ) {
return $data['NewModelName'];
} else if( $value === 'serial' ) {
return $data['NewSerialNumber'];
} else if( $value === 'version' ) {
return $data['NewSoftwareVersion'];
} else if( $value === 'uptime' ) {
return gmdate( "H:i:s", $data['NewUpTime'] );
} else if( $value === 'log' ) {
// $logentries = utf8_encode( $data['NewDeviceLog'] );
$logentries = $data['NewDeviceLog'];
$lograw = explode( "\n", $logentries );
$log = "";
foreach( $lograw as $key => $value ) {
if( ( $key +1 ) > $lines ) break;
$log .= $value . "\n";
}
return $log;
} else {
return $data;
}
}
//
// Website access functions
//
/**
* Get SID
* @return (string)
*/
public function getSID() {
$this->client( 'upnp', 'deviceconfig', 'urn:dslforum-org:service:DeviceConfig:1' );
return substr( $this->client->{"X_AVM-DE_CreateUrlSID"}(), 4 );
}
/**
* Get fritzbox url for webaccess
* @param $urlpath (string) set path to use (e.g.: "/fon_num/sperre.lua" )
* @param $urlparams (string) set url params (e.g.: "&key=value&key1=value2" )
* @return $url (string)
*/
public function buildUrl( $urlpath = null, $urlparams = null ) {
if( $urlpath === null ) $urlpath = "";
if( $urlparams === null ) $urlparams = "";
$url = "http://" . $this->fb_ip . "/" . $urlpath . "?sid=" . $this->getSID() . $urlparams;
return $url;
}
/**
* Send data to a form in fritzbox
* @param $formurl (string) set form url (destination)
* @param $data (array) all form elements in an assosiated array
*/
public function setFormData( $formurl = null, $data = null ) {
if( $formurl === null ) return false;
if( $data === null || ! is_array( $data ) ) return false;
$formurl = $this->buildUrl( $formurl );
$options = array(
'http' => array(
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
'method' => 'POST',
'content' => http_build_query($data),
),
);
$context = stream_context_create($options);
$result = file_get_contents($formurl, false, $context);
if ( $result === false ) {
return false;
} else {
return true;
}
}
//
// DSL functions
//
/**
* Get last Connection data
* @return (string)
*/
public function getConnectionInfo() {
$this->client( 'upnp', 'time', 'urn:dslforum-org:service:Time:1' );
$result = $this->client->GetInfo();
$this->client( 'igdupnp', 'WANIPConn1', 'urn:schemas-upnp-org:service:WANIPConnection:1' );
$status = $this->client->GetStatusInfo();
return date('d.m.Y, H:i:s', strtotime($result['NewCurrentLocalTime']) - $status['NewUptime']);
}
/**
* Get last ReConnection data
* @return (string)
*/
public function getLastConnection() {
$this->client( 'upnp', 'time', 'urn:dslforum-org:service:Time:1' );
$result = $this->client->GetInfo();
$this->client( 'igdupnp', 'WANIPConn1', 'urn:schemas-upnp-org:service:WANIPConnection:1' );
$status = $this->client->GetStatusInfo();
return date('d.m.Y, H:i', strtotime($result['NewCurrentLocalTime']) - $status['NewUptime']);
}
/**
* Get DOWN link speed
* @return (string)
*/
public function getDownSpeed() {
$this->client( 'igdupnp', 'WANCommonIFC1', 'urn:schemas-upnp-org:service:WANCommonInterfaceConfig:1' );
$result = $this->client->GetCommonLinkProperties();
return round( $result['NewLayer1DownstreamMaxBitRate'] / 1000000, 1 );
}
/**
* Get UP link speed
* @return (string)
*/
public function getUpSpeed() {
$this->client( 'igdupnp', 'WANCommonIFC1', 'urn:schemas-upnp-org:service:WANCommonInterfaceConfig:1' );
$result = $this->client->GetCommonLinkProperties();
return isset( $result ) ? round( $result['NewLayer1UpstreamMaxBitRate'] / 1000000, 1 ) : "0";
}
/**
* Get WAN Status
* @return (array)
*/
public function statusWAN() {
$this->client( 'igdupnp', 'WANIPConn1', 'urn:schemas-upnp-org:service:WANIPConnection:1' );
return $this->client->GetStatusInfo();
}
//
// Telephone functions
//
/**
* Get Callerlist
* @return (array)
*/
public function getCallerlist() {
$this->client( 'upnp', 'x_contact', 'urn:dslforum-org:service:X_AVM-DE_OnTel:1' );
$callerurl = $this->client->GetCallList();
return @simplexml_load_file( $callerurl );
}
/**
* Get Phonebook data
* @return (array)
*/
public function getPhonebook() {
$this->client( 'upnp', 'x_contact', 'urn:dslforum-org:service:X_AVM-DE_OnTel:1' );
$phonebookurl = $this->client->GetPhonebook(new SoapParam( 0,"NewPhonebookID" ) );
return $this->parsePhonebook( @simplexml_load_file( $phonebookurl['NewPhonebookURL'] ) );
}
/**
* Phonebook helper
* @return (array)
*/
private function parsePhonebook( $raw ) {
$intern = array();
$extern = array();
$voip = array();
foreach ( $raw->phonebook->contact as $id => $data ) {
$name = $data->person->realName->__toString();
$uuid = $data->uniqueid->__toString();
$numbers = (array) $data->telephony->number;
unset( $numbers['@attributes'] ); // Remove number metadata
if( strpos( $numbers[0], '**' ) !== false ) {
$intern[ $uuid ] = array( 'name' => $name,
'numbers' => $numbers );
} else if( strpos( $numbers[0], '@' ) !== false ) {
$voip[ $uuid ] = array( 'name' => $name,
'numbers' => $numbers );
} else {
$extern[ $uuid ] = array( 'name' => $name,
'numbers' => $numbers );
}
}
return array( 'ext' => $extern,
'int' => $intern,
'voip' => $voip );
}
//
// Answering Mashine (TAM) functions
//
/**
* Set Answering Mashine state
* @param $state (boolean|string) true|false|on|off
* @return (array)
*/
public function setTam( $state = null ) {
if( $state === null ) return;
if( $state || $state == 'on' ) {
$value = 1;
} else {
$value = 0;
}
$this->client( 'upnp', 'x_tam', 'urn:dslforum-org:service:X_AVM-DE_TAM:1' );
$this->client->SetEnable(new SoapParam(0, 'NewIndex'),
new SoapParam( $value, 'NewEnable' )
);
}
/**
* Get Answering Mashine data
* @return (array)
*/
public function getTamList() {
$this->client( 'upnp', 'x_tam', 'urn:dslforum-org:service:X_AVM-DE_TAM:1' );
$result = $this->client->GetMessageList( new SoapParam( 0, 'NewIndex') );
return @simplexml_load_file($result);
}
//
// Wifi functions
//
/**
* GetWifiInfo
* @param $ghz null = 2.4GHz, not null = 5GHz
* enabled, status, channel, ssid, macfilter, bssid
* @return (string|array)
*/
public function getWifiInfo( $wifi = null, $value = null ) {
if( $wifi === null ) $wifi = 1; // 1=2.4GHz, 2=5GHz, 3=gust2.4GHz, 4=guest5GHz
else if( ! is_number( $wifi ) )
$this->client( 'upnp', 'wlanconfig' . $wifi, 'urn:dslforum-org:service:WLANConfiguration:' . $wifi );
$data = $this->client->GetInfo();
if( $value === 'enabled' ) {
return $data['NewEnable'];
} else if( $value === 'status' ) {
return $data['NewStatus'];
} else if( $value === 'channel' ) {
return $data['NewChannel'];
} else if( $value === 'ssid' ) {
return $data['NewSSID'];
} else if( $value === 'macfilter' ) {
return $data['NewMACAddressControlEnabled'];
} if( $value === 'bssid' ) {
return $data['NewBSSID'];
} else {
return $data;
}
}
/**
* Wlan 2.4 and 5 GHz
* @param $state (boolean|string) true|false|on|off
* @param $ghz (int) 5
*/
public function setWifi( $wifi = null, $state = null ) {
if( $state === null ) return;
if( $wifi === null || $wifi > 4 || $wifi < 1 ) $wifi = 1; // 1=2.4GHz, 2=gust2.4GHz, 3=5GHz, 4=guest5GHz
if( $state || $state == 'on' ) {
$value = 1;
} else {
$value = 0;
}
$this->client( 'upnp', 'wlanconfig' . $wifi, 'urn:dslforum-org:service:WLANConfiguration:' . $wifi );
$this->client->SetEnable( new SoapParam( $value, 'NewEnable' ) );
if( $this->getWifiInfo( $wifi, 'enabled' ) ) {
return true;
} else {
return false;
}
}
/**
* get active Wifi clients
* @param (int) unset=all, 1=2.4GHz, 2=guest2.4GHz, 3=5GHz, 4=guest5GHz
*
*/
public function getActiveWifiClients( $wifi = null ) {
if( $wifi !== null && $wifi >= 1 && $wifi <= 4 ) {
// Get from special frequency
$this->client( 'upnp', 'wlanconfig' . $wifi, 'urn:dslforum-org:service:WLANConfiguration:' . $wifi );
$hosts_amount = (int) $this->client->GetTotalAssociations();
$hosts = array();
for( $i = 0; $i < $hosts_amount; $i++ ) {
$hosts[] = $this->client->GetGenericAssociatedDeviceInfo(new SoapParam($i,'NewAssociatedDeviceIndex'));
}
} else {
// get from all wifi frequencies
$hosts = array();
for( $w = 1; $w < 4; $w++ ) {
$this->client( 'upnp', 'wlanconfig' . $w, 'urn:dslforum-org:service:WLANConfiguration:' . $w );
$hosts_amount = (int) $this->client->GetTotalAssociations();
for( $i = 0; $i < $hosts_amount; $i++ ) {
$hosts[] = $this->client->GetGenericAssociatedDeviceInfo(new SoapParam($i,'NewAssociatedDeviceIndex'));
}
$hosts_amount = 0;
}
}
return $hosts;
}
//
// IPTV Optimization
//
public function getTvOptimized( $wifi = null ) {
if( $wifi === null ) {
$this->client( 'upnp', 'wlanconfig1', 'urn:dslforum-org:service:WLANConfiguration:1' );
} else {
$this->client( 'upnp', 'wlanconfig' . $wifi, 'urn:dslforum-org:service:WLANConfiguration:' . $wifi );
}
return $this->client->{'X_AVM-DE_GetIPTVOptimized'}();
}
public function setTvOptimized( $state = null, $wifi = null ) {
if( $wifi === null ) {
$this->client( 'upnp', 'wlanconfig1', 'urn:dslforum-org:service:WLANConfiguration:1' );
} else {
$this->client( 'upnp', 'wlanconfig' . $wifi, 'urn:dslforum-org:service:WLANConfiguration:' . $wifi );
}
if( $state || $state == 'on' ) {
$value = 1;
} else {
$value = 0;
}
return $this->client->{'X_AVM-DE_SetIPTVOptimized'}( new SoapParam( $value, 'NewX_AVM-DE_IPTVoptimize') );
}
//
// UPNP
//
public function getUPNPInfo() {
$this->client( 'upnp', 'X_UPnP', 'urn:dslforum-org:service:X_AVM-DE_UPnP:1' );
return $this->client->GetInfo();
}
//
// LAN
//
public function getLanClients() {
$this->client( 'upnp', 'hosts', 'urn:dslforum-org:service:Hosts:1' );
$hosts = $this->client->GetHostNumberOfEntries();
$erg = array();
for ( $i = 0; $i < $hosts; $i++ ) {
$erg[] = $this->client->GetGenericHostEntry( new SoapParam( $i,'NewIndex' ) );
}
return $erg;
}
/**
* Reconnect DSL
*/
public function reconnectDSL() {
$this->client( 'igdupnp', 'WANIPConn1', 'urn:schemas-upnp-org:service:WANIPConnection:1' );
$this->client->ForceTermination();
}
/**
* Reboot FritzBox
*/
public function reboot() {
$this->client( 'upnp', 'deviceconfig', 'urn:dslforum-org:service:DeviceConfig:1' );
$this->client->Reboot();
}
}
?>