5a6de9946d3aabf9348d0b89325dfbdac541a83a
[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'] = str_replace(' seconds', 's', 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   elseif ($a['timestamp'] < $b['timestamp'])
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   } else {
87     error_log('Cannot open directory ' . SPOOL_DIR . '/' . $directory);
88   }
89
90   usort($result, "call_cmp");
91   return $result;
92 }
93
94 function callinfo($dir, $call)
95 {
96   return read_info($dir, $call);
97 }
98
99 function send_call($dir, $call)
100 {
101   if ($_SERVER['HTTP_USER_AGENT'] == 'NSPlayer') return;
102
103   $dir = str_replace('/','x',$dir);
104   $call = str_replace('/','x',$call);
105
106   if ($dir == 'messages')
107     $ext = '.msg';
108   else
109     $ext = '.vmsg';
110
111   $fname = SPOOL_DIR . '/' . $dir . '/' . $call . $ext;
112   $aufile = SPOOL_DIR . '/' . $dir . '/' . $call . '.au';
113
114   if (!is_file($fname)) {
115     printf("<html><body><h3>Anruf %s in %s nicht gefunden!</h3></body></html>", $call, $dir);
116     return;
117   }
118
119   system('vboxtoau < ' . $fname . ' > ' . $aufile);
120
121   if (($f = fopen($aufile, 'r')) === false) {
122     printf("<html><body><h3>Anruf %s in %s konnte geoeffnet werden!</h3></body></html>", $call, $dir);
123     return;
124   }
125
126   $info = read_info($dir, $call);
127   if (empty($info['read'])) {
128     $info['read'] = date('U');
129     write_info($dir, $call, $info);
130   }
131
132   header(sprintf('Content-disposition: inline; filename="%s.au"', $call));
133   header('Content-Type: audio/basic');
134   header("Content-Length: " . filesize($aufile));
135   header("Cache-Control: no-cache" );
136   header("Pragma: no-cache" );
137   sleep(1);
138   fpassthru($f);
139   fclose($f);
140   unlink($aufile);
141 }
142
143 function archive_call($call)
144 {
145   $basename = SPOOL_DIR . '/' . 'incoming' . '/' . $call;
146   $basenew = SPOOL_DIR . '/' . 'archive' . '/' . $call;
147
148   rename($basename . '.vmsg', $basenew . '.vmsg');
149   rename($basename . '.info', $basenew . '.info');
150 }
151
152 function delete_call($dir, $call)
153 {
154   $basename = SPOOL_DIR . '/' . $dir . '/' . $call;
155
156   unlink($basename . '.vmsg');
157   unlink($basename . '.info');
158 }
159
160 function save_call()
161 {
162   $info = read_info($_POST['dir'], $_POST['call']);
163   $info['name'] = $_POST['name'];
164   $info['note'] = $_POST['note'];
165   write_info($_POST['dir'], $_POST['call'], $info);
166 }
167
168 ?>