Remove bottom margin from title
[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   } elseif (is_file($basename . '.msg')) {
55     if (is_file($basename . '.info'))
56       $info = read_info($dir,$fname);
57   }
58
59   return $info;
60 }
61
62 function call_cmp($a, $b)
63 {
64   if (!empty($a['read']) && empty($b['read']))
65     return 1;
66   elseif (!empty($b['read']) && empty($a['read']))
67     return -1;
68   elseif ($a['timestamp'] > $b['timestamp'])
69     return -1;
70   elseif ($a['timestamp'] < $b['timestamp'])
71     return 1;
72   return 0;
73 }
74
75 function read_directory($directory)
76 {
77   $result = array();
78
79   if ($dir = opendir(SPOOL_DIR . '/' . $directory)) {
80     while (($filename = readdir($dir)) !== false) {
81       if (($pos = strpos($filename, '.vmsg')) !== false || ($pos = strpos($filename, '.msg')) !== false) {
82         $fname = substr($filename,0,$pos);
83         $info = message_info($directory, $fname);
84         $info['fname'] = $fname;
85         $result[] = $info;
86       }
87     }
88     closedir($dir);
89   } else {
90     error_log('Cannot open directory ' . SPOOL_DIR . '/' . $directory);
91   }
92
93   usort($result, "call_cmp");
94   return $result;
95 }
96
97 function callinfo($dir, $call)
98 {
99   return read_info($dir, $call);
100 }
101
102 function send_call($dir, $call)
103 {
104   if ($_SERVER['HTTP_USER_AGENT'] == 'NSPlayer') return;
105
106   $dir = str_replace('/','x',$dir);
107   $call = str_replace('/','x',$call);
108
109   if ($dir == 'messages')
110     $ext = '.msg';
111   else
112     $ext = '.vmsg';
113
114   $fname = SPOOL_DIR . '/' . $dir . '/' . $call . $ext;
115   $aufile = SPOOL_DIR . '/' . $dir . '/' . $call . '.au';
116
117   if (!is_file($fname)) {
118     printf("<html><body><h3>Anruf %s in %s nicht gefunden!</h3></body></html>", $call, $dir);
119     return;
120   }
121
122   system('vboxtoau < ' . $fname . ' > ' . $aufile);
123
124   if (($f = fopen($aufile, 'r')) === false) {
125     printf("<html><body><h3>Anruf %s in %s konnte geoeffnet werden!</h3></body></html>", $call, $dir);
126     return;
127   }
128
129   $info = read_info($dir, $call);
130   if (empty($info['read'])) {
131     $info['read'] = date('U');
132     write_info($dir, $call, $info);
133   }
134
135   header(sprintf('Content-disposition: inline; filename="%s.au"', $call));
136   header('Content-Type: audio/basic');
137   header("Content-Length: " . filesize($aufile));
138   header("Cache-Control: no-cache" );
139   header("Pragma: no-cache" );
140   sleep(1);
141   fpassthru($f);
142   fclose($f);
143   unlink($aufile);
144 }
145
146 function archive_call($call)
147 {
148   $basename = SPOOL_DIR . '/' . 'incoming' . '/' . $call;
149   $basenew = SPOOL_DIR . '/' . 'archive' . '/' . $call;
150
151   rename($basename . '.vmsg', $basenew . '.vmsg');
152   rename($basename . '.info', $basenew . '.info');
153 }
154
155 function delete_call($dir, $call)
156 {
157   $basename = SPOOL_DIR . '/' . $dir . '/' . $call;
158
159   unlink($basename . '.vmsg');
160   unlink($basename . '.info');
161 }
162
163 function save_call()
164 {
165   $info = read_info($_POST['dir'], $_POST['call']);
166   $info['name'] = $_POST['name'];
167   $info['note'] = $_POST['note'];
168   write_info($_POST['dir'], $_POST['call'], $info);
169 }
170
171 ?>