link to my blog post
[auerswald-callnotifier.git] / src / callnotifier / Logger / CallDreambox.php
index 4720c4c920bf4f3dba081171db314ed89d879853..636d725f93ddf42d5dc3c70255ea9b2d317ce280 100644 (file)
@@ -5,18 +5,26 @@ class Logger_CallDreambox extends Logger_CallBase
 {
     protected $host;
 
-    public function __construct($host)
+    public function __construct($host, $callTypes = 'io', $msns = array())
     {
+        parent::__construct($callTypes, $msns);
         $this->host = $host;
     }
 
     public function log($type, $arData)
     {
-        switch ($type) {
-        case 'startingCall':
-            $this->displayStart($arData['call']);
-            break;
+        if ($type != 'startingCall') {
+            return;
+        }
+
+        $call = $arData['call'];
+        if (!$this->hasValidType($call)) {
+            return;
         }
+        if (!$this->hasValidMsn($call)) {
+            return;
+        }
+        $this->displayStart($call);
     }
 
 
@@ -44,13 +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);
-        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";
     }
 }
 ?>