forked from jens-maus/carddav2fb
-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathApi.php
140 lines (117 loc) · 3.37 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
<?php
namespace Andig\FritzBox;
use Andig\Http\ClientTrait;
define('EMPTY_SID', '0000000000000000');
/**
* Copyright (c) 2019 Andreas Götz
* @license MIT
*/
class Api
{
use ClientTrait;
/** @var string */
protected $url;
/** @var string */
protected $sid = EMPTY_SID;
/**
* Execute fb login
*
* @access public
*/
public function __construct(string $url = 'https://fritz.box')
{
$this->url = rtrim($url, '/');
}
/**
* Get session ID
*
* @return string SID
*/
public function getSID(): string
{
return $this->sid;
}
/**
* Multi-part file uploads
*
* @param array $formFields
* @param array $fileFields
* @return string POST result
* @throws \Exception
*/
public function postFile(array $formFields, array $fileFields): string
{
$multipart = [];
// sid must be first parameter
$formFields = array_merge(['sid' => $this->sid], $formFields);
foreach ($formFields as $key => $val) {
$multipart[] = [
'name' => $key,
'contents' => $val,
];
}
foreach ($fileFields as $name => $file) {
$multipart[] = [
'name' => $name,
'filename' => $file['filename'],
'contents' => $file['content'],
'headers' => [
'Content-Type' => $file['type'],
],
];
}
$url = $this->url . '/cgi-bin/firmwarecfg';
$resp = $this->getClient()->request('POST', $url, [
'multipart' => $multipart,
]);
return (string)$resp->getBody();
}
/**
* image upload to FRITZ!Box. Guzzle's multipart option does not work on
* this interface. If this changes, this function can be deleted
*
* @param string $body
* @return string POST result
* @throws \Exception
*/
public function postImage($body): string
{
$url = $this->url . '/cgi-bin/firmwarecfg';
$resp = $this->getClient()->request('POST', $url, [
'body' => $body,
]);
return (string)$resp->getBody();
}
/**
* Login, throws on failure
*
* @throws \Exception
*/
public function login()
{
$url = $this->url . '/login_sid.lua';
// read the current status
$resp = $this->getClient()->request('GET', $url);
$xml = simplexml_load_string((string)$resp->getBody());
if ($xml->SID != EMPTY_SID) {
$this->sid = (string)$xml->SID;
return;
}
// the challenge-response magic, pay attention to the mb_convert_encoding()
$response = $xml->Challenge . '-' . md5(mb_convert_encoding($xml->Challenge . '-' . $this->password, "UCS-2LE", "UTF-8"));
// login
$resp = $this->getClient()->request('GET', $url, [
'query' => [
'username' => $this->username,
'response' => $response,
]
]);
// retrieve SID from response
$xml = simplexml_load_string((string)$resp->getBody());
if ($xml->SID != EMPTY_SID) {
$this->sid = (string)$xml->SID;
return;
}
throw new \Exception('Login failed with an unknown response - please check credentials.');
}
}