aboutsummaryrefslogtreecommitdiff
path: root/src/callnotifier/Logger/CallDreambox.php
diff options
context:
space:
mode:
authorChristian Weiske <cweiske@cweiske.de>2019-03-13 22:30:46 +0100
committerChristian Weiske <cweiske@cweiske.de>2019-03-13 22:30:46 +0100
commit3cbb4d58e42c3e2371760a75523860e5a4df97a3 (patch)
treed2f65da435e92f123800f32b34fd896d56293a28 /src/callnotifier/Logger/CallDreambox.php
parent42ff6e2c7c3212c5582d6c9f5a50c983e0e1a578 (diff)
downloadauerswald-callnotifier-3cbb4d58e42c3e2371760a75523860e5a4df97a3.tar.gz
auerswald-callnotifier-3cbb4d58e42c3e2371760a75523860e5a4df97a3.zip
Fetch Dreambox session token before sending message to itv1.1.0
Diffstat (limited to 'src/callnotifier/Logger/CallDreambox.php')
-rw-r--r--src/callnotifier/Logger/CallDreambox.php90
1 files changed, 82 insertions, 8 deletions
diff --git a/src/callnotifier/Logger/CallDreambox.php b/src/callnotifier/Logger/CallDreambox.php
index 81dd63a..636d725 100644
--- a/src/callnotifier/Logger/CallDreambox.php
+++ b/src/callnotifier/Logger/CallDreambox.php
@@ -52,15 +52,89 @@ class Logger_CallDreambox extends Logger_CallBase
protected function notify($msg)
{
- $url = 'http://' . $this->host
- . '/web/message?type=2&timeout=10&text=' . urlencode($msg);
- $this->debug('Fetch: ' . $url);
-
- exec(
- 'curl'
- . ' ' . escapeshellarg($url)
- . ' > /dev/null 2>&1 &'
+ $dreamboxUrl = 'http://' . $this->host;
+ $token = $this->fetchSessionToken($dreamboxUrl);
+
+ if ($token === false) {
+ return false;
+ }
+
+ $url = $dreamboxUrl . '/web/message';
+ $this->debug('POSTing to: ' . $url);
+
+ $postMsg = 'type=2'
+ . '&timeout=10'
+ . '&text=' . urlencode($msg)
+ . '&sessionid=' . urlencode($token);
+ $ctx = stream_context_create(
+ [
+ 'http' => [
+ 'method' => 'POST',
+ 'header' => [
+ 'Content-type: application/x-www-form-urlencoded',
+ ],
+ 'content' => $postMsg
+ ]
+ ]
);
+ $xml = @file_get_contents($url, false, $ctx);
+ $sx = $this->handleError($xml);
+ }
+
+ protected function fetchSessionToken($dreamboxUrl)
+ {
+ $xml = @file_get_contents($dreamboxUrl . '/web/session');
+ $sx = $this->handleError($xml);
+ if ($sx === false) {
+ return false;
+ }
+
+ $token = (string) $sx;
+
+ return $token;
+ }
+
+ protected function handleError($xml)
+ {
+ if ($xml === false) {
+ if (!isset($http_response_header)) {
+ $this->warn(
+ 'Error talking with dreambox web interface: '
+ . error_get_last()['message']
+ );
+ return false;
+ }
+
+ list($http, $code, $message) = explode(
+ ' ', $http_response_header[0], 3
+ );
+ if ($code == 401) {
+ //dreambox web interface authentication has been enabled
+ $this->warn(
+ 'Error: Web interface authentication is required'
+ );
+ return false;
+ } else {
+ $this->warn(
+ 'Failed to fetch dreambox session token: '
+ . error_get_last()['message']
+ );
+ return false;
+ }
+ }
+
+ $sx = simplexml_load_string($xml);
+ if (isset($sx->e2state) && (string) $sx->e2state === 'False') {
+ $this->warn('Error: ' . $sx->e2statetext);
+ return false;
+ }
+
+ return $sx;
+ }
+
+ protected function warn($msg)
+ {
+ echo $msg . "\n";
}
}
?>