6873ae777aab21e5925bd49030289d3398d4674e
[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   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   }
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   $aufile = SPOOL_DIR . '/' . $dir . '/' . $call . '.au';
109
110   if (!is_file($fname)) {
111     printf("<html><body><h3>Anruf %s in %s nicht gefunden!</h3></body></html>", $call, $dir);
112     return;
113   }
114
115   system('vboxtoau < ' . $fname . ' > ' . $aufile);
116
117   if (($f = fopen($aufile, 'r')) === false) {
118     printf("<html><body><h3>Anruf %s in %s konnte geoeffnet werden!</h3></body></html>", $call, $dir);
119     return;
120   }
121
122   $info = read_info($dir, $call);
123   if (empty($info['read'])) {
124     $info['read'] = date('U');
125     write_info($dir, $call, $info);
126   }
127
128   header(sprintf('Content-disposition: inline; filename="%s.au"', $call));
129   header('Content-Type: audio/basic');
130   header("Content-Length: " . filesize($aufile));
131   header("Cache-Control: no-cache" );
132   header("Pragma: no-cache" );
133   sleep(1);
134   fpassthru($f);
135   fclose($f);
136   unlink($aufile);
137 }
138
139 function archive_call($call)
140 {
141   $basename = SPOOL_DIR . '/' . 'incoming' . '/' . $call;
142   $basenew = SPOOL_DIR . '/' . 'archive' . '/' . $call;
143
144   rename($basename . '.vmsg', $basenew . '.vmsg');
145   rename($basename . '.info', $basenew . '.info');
146 }
147
148 function delete_call($dir, $call)
149 {
150   $basename = SPOOL_DIR . '/' . $dir . '/' . $call;
151
152   unlink($basename . '.vmsg');
153   unlink($basename . '.info');
154 }
155
156 function save_call()
157 {
158   $info = read_info($_POST['dir'], $_POST['call']);
159   $info['name'] = $_POST['name'];
160   $info['note'] = $_POST['note'];
161   write_info($_POST['dir'], $_POST['call'], $info);
162 }
163
164 ?>