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