📄 vmail.cgi
字号:
#!/usr/bin/perl## Web based Voicemail for Asterisk## Copyright (C) 2002, Linux Support Services, Inc.## Distributed under the terms of the GNU General Public License## Written by Mark Spencer <markster@linux-support.net>## (icky, I know.... if you know better perl please help!)### Synchronization added by GDS Partners (www.gdspartners.com)# Stojan Sljivic (stojan.sljivic@gdspartners.com)#use CGI qw/:standard/;use Carp::Heavy;use CGI::Carp qw(fatalsToBrowser);use DBI;use Fcntl qw ( O_WRONLY O_CREAT O_EXCL );use Time::HiRes qw ( usleep );$context=""; # Define here your by default context (so you dont need to put voicemail@context in the login@validfolders = ( "INBOX", "Old", "Work", "Family", "Friends", "Cust1", "Cust2", "Cust3", "Cust4", "Cust5" );%formats = ( "wav" => { name => "Uncompressed WAV", mime => "audio/x-wav", pref => 1 }, "WAV" => { name => "GSM Compressed WAV", mime => "audio/x-wav", pref => 2 }, "gsm" => { name => "Raw GSM Audio", mime => "audio/x-gsm", pref => 3 });$astpath = "/_asterisk";$stdcontainerstart = "<table align=center width=600><tr><td>\n";$footer = "<hr><font size=-1><a href=\"http://www.asterisk.org\">The Asterisk Open Source PBX</a> Copyright 2004, <a href=\"http://www.digium.com\">Digium, Inc.</a></a>";$stdcontainerend = "</td></tr><tr><td align=right>$footer</td></tr></table>\n";sub lock_path() { my($path) = @_; my $rand; my $rfile; my $start; my $res; $rand = rand 99999999; $rfile = "$path/.lock-$rand"; sysopen(RFILE, $rfile, O_WRONLY | O_CREAT | O_EXCL, 0666) or return -1; close(RFILE); $res = link($rfile, "$path/.lock"); $start = time; if ($res == 0) { while (($res == 0) && (time - $start <= 5)) { $res = link($rfile, "$path/.lock"); usleep(1); } } unlink($rfile); if ($res == 0) { return -1; } else { return 0; }}sub unlock_path() { my($path) = @_; unlink("$path/.lock");}sub untaint() { my($data) = @_; if ($data =~ /^([-\@\w.]+)$/) { $data = $1; } else { die "Security violation."; } return $data;}sub login_screen() { print header; my ($message) = @_; print <<_EOH;<TITLE>Asterisk Web-Voicemail</TITLE><BODY BGCOLOR="white">$stdcontainerstart<FORM METHOD="post"><input type=hidden name="action" value="login"><table align=center><tr><td valign=top align=center rowspan=6><img align=center src="$astpath/animlogo.gif"></td></tr><tr><td align=center colspan=2><font size=+2>Comedian Mail Login</font></td></tr><tr><td align=center colspan=2><font size=+1>$message</font></td></tr><tr><td>Mailbox:</td><td><input type=text name="mailbox"></td></tr><tr><td>Password:</td><td><input type=password name="password"></td></tr><tr><td align=right colspan=2><input value="Login" type=submit></td></tr><input type=hidden name="context" value="$context"></table></FORM>$stdcontainerend</BODY>\n_EOH}sub check_login(){ local ($filename, $startcat) = @_; local ($mbox, $context) = split(/\@/, param('mailbox')); local $pass = param('password'); local $category = $startcat; local @fields; local $tmp; local (*VMAIL); if (!$category) { $category = "general"; } if (!$context) { $context = param('context'); } if (!$context) { $context = "default"; } if (!$filename) { $filename = "/etc/asterisk/voicemail.conf"; }# print header;# print "Including <h2>$filename</h2> while in <h2>$category</h2>...\n"; open(VMAIL, "<$filename") || die("Bleh, no $filename"); while(<VMAIL>) { chomp; if (/include\s\"([^\"]+)\"$/) { ($tmp, $category) = &check_login("/etc/asterisk/$1", $category); if (length($tmp)) {# print "Got '$tmp'\n"; return ($tmp, $category); } } elsif (/\[(.*)\]/) { $category = $1; } elsif ($category eq "general") { if (/([^\s]+)\s*\=\s*(.*)/) { if ($1 eq "dbname") { $dbname = $2; } elsif ($1 eq "dbpass") { $dbpass = $2; } elsif ($1 eq "dbhost") { $dbhost = $2; } elsif ($1 eq "dbuser") { $dbuser = $2; } } if ($dbname and $dbpass and $dbhost and $dbuser) { # db variables are present. Use db for authentication. my $dbh = DBI->connect("DBI:mysql:$dbname:$dbhost",$dbuser,$dbpass); my $sth = $dbh->prepare(qq{select fullname,context from voicemail where mailbox='$mbox' and password='$pass' and context='$context'}); $sth->execute(); if (($fullname, $category) = $sth->fetchrow_array()) {; return ($fullname ? $fullname : "Extension $mbox in $context",$category); } } } elsif (($category ne "general") && ($category ne "zonemessages")) { if (/([^\s]+)\s*\=\>?\s*(.*)/) { @fields = split(/\,\s*/, $2);# print "<p>Mailbox is $1\n"; if (($mbox eq $1) && (($pass eq $fields[0]) || ("-${pass}" eq $fields[0])) && ($context eq $category)) { return ($fields[1] ? $fields[1] : "Extension $mbox in $context", $category); } } } } close(VMAIL); return ("", $category);}sub validmailbox(){ local ($context, $mbox, $filename, $startcat) = @_; local $category = $startcat; local @fields; local (*VMAIL); if (!$context) { $context = param('context'); } if (!$context) { $context = "default"; } if (!$filename) { $filename = "/etc/asterisk/voicemail.conf"; } if (!$category) { $category = "general"; } open(VMAIL, "<$filename") || die("Bleh, no $filename"); while(<VMAIL>) { chomp; if (/include\s\"([^\"]+)\"$/) { ($tmp, $category) = &validmailbox($mbox, $context, "/etc/asterisk/$1"); if ($tmp) { return ($tmp, $category); } } elsif (/\[(.*)\]/) { $category = $1; } elsif ($category eq "general") { if (/([^\s]+)\s*\=\s*(.*)/) { if ($1 eq "dbname") { $dbname = $2; } elsif ($1 eq "dbpass") { $dbpass = $2; } elsif ($1 eq "dbhost") { $dbhost = $2; } elsif ($1 eq "dbuser") { $dbuser = $2; } } if ($dbname and $dbpass and $dbhost and $dbuser) { # db variables are present. Use db for authentication. my $dbh = DBI->connect("DBI:mysql:$dbname:$dbhost",$dbuser,$dbpass); my $sth = $dbh->prepare(qq{select fullname,context from voicemail where mailbox='$mbox' and password='$pass' and context='$context'}); $sth->execute(); if (($fullname, $category) = $sth->fetchrow_array()) {; return ($fullname ? $fullname : "unknown", $category); } } } elsif (($category ne "general") && ($category ne "zonemessages") && ($category eq $context)) { if (/([^\s]+)\s*\=\>?\s*(.*)/) { @fields = split(/\,\s*/, $2); if (($mbox eq $1) && ($context eq $category)) { return ($fields[2] ? $fields[2] : "unknown", $category); } } } } return ("", $category);}sub mailbox_options(){ local($context, $current, $filename, $category) = @_; local (*VMAIL); local $tmp2; local $tmp; if (!$filename) { $filename = "/etc/asterisk/voicemail.conf"; } if (!$category) { $category = "general"; }# print header;# print "Including <h2>$filename</h2> while in <h2>$category</h2>...\n"; open(VMAIL, "<$filename") || die("Bleh, no voicemail.conf"); while(<VMAIL>) { chomp; s/\;.*$//; if (/include\s\"([^\"]+)\"$/) { ($tmp2, $category) = &mailbox_options($context, $current, "/etc/asterisk/$1", $category);# print "Got '$tmp2'...\n"; $tmp .= $tmp2; } elsif (/\[(.*)\]/) { $category = $1; } elsif ($category eq "general") { if (/([^\s]+)\s*\=\s*(.*)/) { if ($1 eq "dbname") { $dbname = $2; } elsif ($1 eq "dbpass") { $dbpass = $2; } elsif ($1 eq "dbhost") { $dbhost = $2; } elsif ($1 eq "dbuser") { $dbuser = $2; } } if ($dbname and $dbpass and $dbhost and $dbuser) { # db variables are present. Use db for authentication. my $dbh = DBI->connect("DBI:mysql:$dbname:$dbhost",$dbuser,$dbpass); my $sth = $dbh->prepare(qq{select mailbox,fullname,context from voicemail where context='$context' order by mailbox}); $sth->execute(); while (($mailbox, $fullname, $category) = $sth->fetchrow_array()) { $text = $mailbox; if ($fullname) { $text .= " (".$fullname.")"; } if ($mailbox eq $current) { $tmp .= "<OPTION SELECTED>$text</OPTION>\n"; } else { $tmp .= "<OPTION>$text</OPTION>\n"; } } return ($tmp, $category); } } elsif (($category ne "general") && ($category ne "zonemessages")) { if (/([^\s]+)\s*\=\>?\s*(.*)/) { @fields = split(/\,\s*/, $2); $text = "$1"; if ($fields[1]) { $text .= " ($fields[1])"; } if ($1 eq $current) { $tmp .= "<OPTION SELECTED>$text</OPTION>\n"; } else { $tmp .= "<OPTION>$text</OPTION>\n"; } } } } close(VMAIL); return ($tmp, $category);}sub mailbox_list(){ local ($name, $context, $current) = @_; local $tmp; local $text; local $tmp; local $opts; if (!$context) { $context = "default"; } $tmp = "<SELECT name=\"$name\">\n"; ($opts) = &mailbox_options($context, $current); $tmp .= $opts; $tmp .= "</SELECT>\n"; }sub msgcount() { my ($context, $mailbox, $folder) = @_; my $path = "/var/spool/asterisk/voicemail/$context/$mailbox/$folder"; if (opendir(DIR, $path)) { my @msgs = grep(/^msg....\.txt$/, readdir(DIR)); closedir(DIR); return sprintf "%d", $#msgs + 1; } return "0";}sub msgcountstr(){ my ($context, $mailbox, $folder) = @_;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -