Add context menu to list of calls, support archiving and deletion of messages
[infodrom/phone] / phone.php
1 <?
2 define('SPOOL_DIR', '/var/spool/vbox/ttyI6');
3
4 function read_info($dir, $call)
5 {
6   $info = array();
7   if (($f = fopen(SPOOL_DIR . '/' . $dir . '/' . $call . '.info', 'r')) !== false) {
8     $line = fgets($f, 1024);
9     fclose($f);
10     $info = unserialize($line);
11     if (empty($info['note'])) $info['note'] = '';
12   }
13   return $info;
14 }
15
16 function write_info($dir,$fname,$info)
17 {
18   if (($f = fopen(SPOOL_DIR . '/' . $dir . '/' . $fname . '.info', 'w')) !== false) {
19     fwrite($f, serialize($info)."\n");
20     fclose($f);
21   }
22 }
23
24 function message_info($dir,$fname)
25 {
26   $info = array();
27   $basename = SPOOL_DIR . '/' . $dir . '/' . $fname;
28
29   if (is_file($basename . '.vmsg')) {
30     if (!is_file($basename . '.info')) {
31       if (($p = popen('vboxmode ' . $basename . '.vmsg', 'r')) !== false) {
32         while (!feof($p)) {
33           $line = chop(fgets($p, 1024));
34           $pos = strpos($line, ':')+2;
35           if (strpos($line, 'Speaker name............: ') !== false) {
36             $info['name'] = utf8_encode(substr($line, $pos));
37           } elseif (strpos($line, 'Length..................: ') !== false) {
38             $info['length'] = substr($line, $pos);
39           } elseif (strpos($line, 'Speaker caller number...: ') !== false) {
40             $info['number'] = substr($line, $pos);
41           } elseif (strpos($line, 'Creation time...........: ') !== false) {
42             $info['date'] = substr($line, $pos);
43           }
44         }
45         pclose($p);
46         $s = stat($basename . '.vmsg');
47         $info['timestamp'] = $s['mtime'];
48         write_info($dir,$fname,$info);
49       }
50     }
51     $info = read_info($dir,$fname);
52   }
53
54   return $info;
55 }
56
57 function call_cmp($a, $b)
58 {
59   if (!empty($a['read']) && empty($b['read']))
60     return 1;
61   elseif (!empty($b['read']) && empty($a['read']))
62     return -1;
63   elseif ($a['timestamp'] > $b['timestamp'])
64     return -1;
65   else
66     return 1;
67   return 0;
68 }
69
70 function read_directory($directory)
71 {
72   $result = array();
73
74   if ($dir = opendir(SPOOL_DIR . '/' . $directory)) {
75     while (($filename = readdir($dir)) !== false) {
76       if (($pos = strpos($filename, '.vmsg')) !== false) {
77         $fname = substr($filename,0,$pos);
78         $info = message_info($directory, $fname);
79         $info['fname'] = $fname;
80         $result[] = $info;
81       }
82     }
83     closedir($dir);
84   }
85
86   usort($result, "call_cmp");
87   return $result;
88 }
89
90 function callinfo($dir, $call)
91 {
92   return read_info($dir, $call);
93 }
94
95 function send_call($dir, $call)
96 {
97   $dir = str_replace('/','x',$dir);
98   $call = str_replace('/','x',$call);
99   $fname = SPOOL_DIR . '/' . $dir . '/' . $call . '.vmsg';
100
101   if (!is_file($fname)) {
102     printf("<html><body><h3>Anruf %s in %s nicht gefunden!</h3></body></html>", $call, $dir);
103     return;
104   }
105
106   if (($p = popen('vboxtoau < ' . $fname, 'r')) === false) {
107     printf("<html><body><h3>Anruf %s in %s konnte geoeffnet werden!</h3></body></html>", $call, $dir);
108     return;
109   }
110
111   $info = read_info($dir, $call);
112   if (empty($info['read'])) {
113     $info['read'] = date('U');
114     write_info($dir, $call, $info);
115   }
116
117   header(sprintf('Content-disposition: inline; filename="%s.au"', $call));
118   header('Content-Type: audio/basic');
119   header("Cache-Control: ");
120   header("Pragma: ");
121   sleep(1);
122   fpassthru($p);
123
124   pclose($p);
125 }
126
127 function archive_call($call)
128 {
129   $basename = SPOOL_DIR . '/' . 'incoming' . '/' . $call;
130   $basenew = SPOOL_DIR . '/' . 'archive' . '/' . $call;
131
132   rename($basename . '.vmsg', $basenew . '.vmsg');
133   rename($basename . '.info', $basenew . '.info');
134 }
135
136 function delete_call($dir, $call)
137 {
138   $basename = SPOOL_DIR . '/' . $dir . '/' . $call;
139
140   unlink($basename . '.vmsg');
141   unlink($basename . '.info');
142 }
143
144 ?>