link to my blog post
[auerswald-callnotifier.git] / src / callnotifier / Logger / CallDreambox.php
1 <?php
2 namespace callnotifier;
3
4 class Logger_CallDreambox extends Logger_CallBase
5 {
6     protected $host;
7
8     public function __construct($host, $callTypes = 'io', $msns = array())
9     {
10         parent::__construct($callTypes, $msns);
11         $this->host = $host;
12     }
13
14     public function log($type, $arData)
15     {
16         if ($type != 'startingCall') {
17             return;
18         }
19
20         $call = $arData['call'];
21         if (!$this->hasValidType($call)) {
22             return;
23         }
24         if (!$this->hasValidMsn($call)) {
25             return;
26         }
27         $this->displayStart($call);
28     }
29
30
31     protected function displayStart(CallMonitor_Call $call)
32     {
33         if ($call->type != CallMonitor_Call::INCOMING) {
34             return;
35         }
36
37         $this->addUnsetVars($call);
38
39         $msg = 'Anruf von ';
40         if ($call->fromName !== null) {
41             $msg .= $call->fromName
42                 . "\nNummer: " . $call->from;
43         } else {
44             $msg .= $call->from;
45         }
46         if ($call->fromLocation !== null) {
47             $msg .= "\nOrt: " . $call->fromLocation;
48         }
49
50         $this->notify($msg);
51     }
52
53     protected function notify($msg)
54     {
55         $dreamboxUrl = 'http://' . $this->host;
56         $token = $this->fetchSessionToken($dreamboxUrl);
57
58         if ($token === false) {
59             return false;
60         }
61
62         $url = $dreamboxUrl . '/web/message';
63         $this->debug('POSTing to: ' . $url);
64
65         $postMsg = 'type=2'
66             . '&timeout=10'
67             . '&text=' . urlencode($msg)
68             . '&sessionid=' . urlencode($token);
69         $ctx = stream_context_create(
70             [
71                 'http' => [
72                     'method' => 'POST',
73                     'header' => [
74                         'Content-type: application/x-www-form-urlencoded',
75                     ],
76                     'content' => $postMsg
77                 ]
78             ]
79         );
80         $xml = @file_get_contents($url, false, $ctx);
81         $sx = $this->handleError($xml);
82     }
83
84     protected function fetchSessionToken($dreamboxUrl)
85     {
86         $xml = @file_get_contents($dreamboxUrl . '/web/session');
87         $sx = $this->handleError($xml);
88         if ($sx === false) {
89             return false;
90         }
91
92         $token = (string) $sx;
93
94         return $token;
95     }
96
97     protected function handleError($xml)
98     {
99         if ($xml === false) {
100             if (!isset($http_response_header)) {
101                 $this->warn(
102                     'Error talking with dreambox web interface: '
103                     . error_get_last()['message']
104                 );
105                 return false;
106             }
107
108             list($http, $code, $message) = explode(
109                 ' ', $http_response_header[0], 3
110             );
111             if ($code == 401) {
112                 //dreambox web interface authentication has been enabled
113                 $this->warn(
114                     'Error: Web interface authentication is required'
115                 );
116                 return false;
117             } else {
118                 $this->warn(
119                     'Failed to fetch dreambox session token: '
120                     . error_get_last()['message']
121                 );
122                 return false;
123             }
124         }
125
126         $sx = simplexml_load_string($xml);
127         if (isset($sx->e2state) && (string) $sx->e2state === 'False') {
128             $this->warn('Error: ' . $sx->e2statetext);
129             return false;
130         }
131
132         return $sx;
133     }
134
135     protected function warn($msg)
136     {
137         echo $msg . "\n";
138     }
139 }
140 ?>