From: Christian Weiske Date: Wed, 13 Mar 2019 21:30:46 +0000 (+0100) Subject: Fetch Dreambox session token before sending message to it X-Git-Tag: v1.1.0 X-Git-Url: https://git.cweiske.de/auerswald-callnotifier.git/commitdiff_plain/3cbb4d58e42c3e2371760a75523860e5a4df97a3 Fetch Dreambox session token before sending message to it --- 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"; } } ?>