Initial implementation
[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 get_incoming()
58 {
59   $result = array();
60
61   if ($dir = opendir(SPOOL_DIR . '/incoming')) {
62     while (($filename = readdir($dir)) !== false) {
63       if (($pos = strpos($filename, '.vmsg')) !== false) {
64         $fname = substr($filename,0,$pos);
65         $info = message_info('incoming', $fname);
66         $info['fname'] = $fname;
67         $result[] = $info;
68       }
69     }
70     closedir($dir);
71   }
72   return $result;
73 }
74
75 function callinfo($dir, $call)
76 {
77   return read_info($dir, $call);
78 }
79
80 function send_call($dir, $call)
81 {
82   $dir = str_replace('/','x',$dir);
83   $call = str_replace('/','x',$call);
84   $fname = SPOOL_DIR . '/' . $dir . '/' . $call . '.vmsg';
85
86   if (!is_file($fname)) {
87     printf("<html><body><h3>Anruf %s in %s nicht gefunden!</h3></body></html>", $call, $dir);
88     return;
89   }
90
91   if (($p = popen('vboxtoau < ' . $fname, 'r')) === false) {
92     printf("<html><body><h3>Anruf %s in %s konnte geoeffnet werden!</h3></body></html>", $call, $dir);
93     return;
94   }
95
96   header(sprintf('Content-disposition: inline; filename="%s.au"', $call));
97   header('Content-Type: audio/basic');
98   header("Cache-Control: ");
99   header("Pragma: ");
100   sleep(1);
101   fpassthru($p);
102
103   pclose($p);
104 }
105
106 ?>