login | register
Wed 08 of Oct, 2008 [07:01 UTC]

voip-info.org

Discuss [1] History

Asterisk GUI phpMyEdit

Created by: flavour,Last modification on Thu 10 of Jun, 2004 [13:48 UTC]

Asterisk GUI phpMyEdit


I have developed a Web-based GUI for Asterisk using phpMyEdit.
This quickly provides a powerful interface to View/Add/Modify/Delete records.

All users are stored in a MySQL table:

CREATE TABLE `users` (
 `mailbox` varchar(4) NOT NULL default '',
 `shortname` varchar(20) NOT NULL default '',
 `fullname` varchar(40) NOT NULL default '',
 `email` varchar(40) NOT NULL default '',
 `password` varchar(20) NOT NULL default '',
 `AccountCode` varchar(20) NOT NULL default '',
 `location` varchar(6) NOT NULL default '',
 `pickupgroup` char(2) NOT NULL default '',
 `Language` char(2) NOT NULL default 'en',
 `CallDiversion` varchar(4) NOT NULL default '',
 `DivertUnavailable` varchar(4) NOT NULL default '',
 `DnD` char(1) NOT NULL default '0',
 `template` varchar(20) NOT NULL default '',
 `options` varchar(20) NOT NULL default '',
 `pager` varchar(20) NOT NULL default '',
 `context` varchar(20) NOT NULL default 'default',
 `type` varchar(5) NOT NULL default 'user',
 PRIMARY KEY  (`mailbox`)
) TYPE=MyISAM COMMENT='Users of the Asterisk PBX';

Users can roam between SIP & IAX clients
(Note that I use the templates functionality of chan_sip2, so user-agent settings are pulled from the 'template' field)


I have modified the PHP script to call an external script whenever any data is modified.
Changes need to be hooked into these 3 functions:
do_add_record()
do_change_record()
do_delete_record()
// Reload Asterisk to activate new settings
exec("sudo /usr/local/bin/update-asterisk.pl & >/dev/null");

This script writes out text-files which get #included into the main configuration files.
Then it restarts Asterisk by using the Manager interface.
- this works better than simply calling "asterisk -r -x reload" as it returns quicker.

update-asterisk.pl:

use DBI;
use Net::Telnet ();

################### BEGIN OF CONFIGURATION #################### 
# MySQL configuration
$dbuser = "asteriskro";
$dbpass = "password";
$dbhost = "localhost";
$dbname = "asterisk";
$dbtable_users = "users";

# Asterisk Manager configuration
$mgruser = "reload";
$mgrpass = "password";
$mgrhost = "127.0.0.1";
$mgrport = "5038";

$conf_path = "/etc/asterisk/";
$conf_file_users_sip = "users-sip.conf"; # User accounts accessible via SIP
$conf_file_users_iax = "users-iax.conf"; # User accounts accessible via IAX
$conf_file_voicemail = "users-voicemail.conf"; # Voicemail
$conf_file_mapping = "mapping.conf"; # Map incoming <sip:username@domain> requests to extensions
################### END OF CONFIGURATION #######################

$conf_users_sip = "$conf_path" . "$conf_file_users_sip";
$conf_users_iax = "$conf_path" . "$conf_file_users_iax";
$conf_voicemail = "$conf_path" . "$conf_file_voicemail";
$conf_mapping = "$conf_path" . "$conf_file_mapping";

$dbh = DBI->connect("dbi:mysql:dbname=$dbname;host=$dbhost", "$dbuser", "$dbpass");
$statement = "SELECT mailbox,template,password,fullname,pickupgroup,AccountCode,language,shortname,email,location from $dbtable_users";
my $result = $dbh->selectall_arrayref($statement);
unless ($result) {
 # check for errors after every single database call
 print "dbh->selectall_arrayref($statement) failed!\n";
 print "DBI::err=$DBI::err\n";
 print "DBI::errstr=$DBI::errstr\n";
 exit;
}

my @resultSet = @{$result};

if ( $#resultSet > -1 ) {

 open CONF, ">$conf_users_sip" || die "Cannot create/overwrite file: $conf_users_sip\n";
 open CONF2, ">$conf_users_iax" || die "Cannot create/overwrite file: $conf_users_iax\n";
 foreach $row (@{ $result }) {
  my @result = @{ $row };
  $mailbox=$result0;
  $template=$result1;
  $password=$result2;
  $fullname=$result3;
  $pickupgroup=$result4;
  $accountcode=$result5;
  $language=$result6;
  $shortname=$result7;
  $email=$result8;
  $location=$result9;
  if ($location eq "gboxf") {
   if ($template eq "firefly" | $template eq "iaxcomm") {
    print CONF2 "$mailbox\n";
    print CONF2 "type=friend\n";
    print CONF2 "host=dynamic\n";
    print CONF2 "context=lan-phones\n";
    print CONF2 "permit=0.0.0.0/0.0.0.0\n";
    print CONF2 "secret=$password\n";
    print CONF2 "callerid=\"$fullname\" <$mailbox>\n";
    print CONF2 "accountcode=$accountcode\n";
    print CONF2 "language=$language\n";
    print CONF2 "mailbox=$mailbox\@lan\n";
    print CONF2 "\n";
   } else {
    print CONF "$mailbox\n";
    print CONF "template=$template\n";
    print CONF "type=friend\n";
    print CONF "username=$mailbox\n";
    print CONF "secret=$password\n";
    print CONF "callerid=\"$fullname\" <$mailbox>\n";
    if ($pickupgroup eq"") {
    } else {
     print CONF "callgroup=$pickupgroup\n";
     print CONF "pickupgroup=$pickupgroup\n";
    }
    print CONF "accountcode=$accountcode\n";
    print CONF "language=$language\n";
    print CONF "mailbox=$mailbox\@lan\n";
    print CONF "\n";
   }
  }
 }
 close CONF;
 close CONF2;

 open CONF, ">$conf_voicemail" || die "Cannot create/overwrite file: $conf_voicemail\n";
 foreach $row (@{ $result }) {
  my @result = @{ $row };
  $mailbox=$result0;
  $password=$result2;
  $fullname=$result3;
  $language=$result6;
  $email=$result8;
  print CONF "$mailbox => $password,$fullname,$email,,attach=no|review=yes|language=$language\n";
 }
 close CONF;

 open CONF, ">$conf_mapping" || die "Cannot create/overwrite file: $conf_mapping\n";
 foreach $row (@{ $result }) {
  my @result = @{ $row };
  $mailbox=$result0;
  $shortname=$result7;
  print CONF "exten => $shortname,1,Goto(lan,$mailbox,1)\n";
 }
 close CONF;

}

# Reload Asterisk
$tn = new Net::Telnet (Port => $mgrport,
 Prompt => '/.*$%#> $/',
 Output_record_separator => '',
 # Errmode    => Return,
 );

#$fh = $tn->dump_log("./telnet.log");
$tn->open("$mgrhost");
$tn->waitfor('/0\n$/');
$tn->print("Action: Login\nUsername: $mgruser\nSecret: $mgrpass\n\n");
$tn->waitfor('/Authentication.*/');              # print auth accepted
$command="Action: command\nCommand: reload\n\n";
$tn->cmd(String => "$command", Prompt => '/--END COMMAND--/');

exit 0;


Note that sudo is used to allow the web interface to write to the Asterisk config files.
/etc/sudoers:

Defaults:apache !lecture
apache localhost = NOPASSWD: /usr/local/bin/update-asterisk.pl


I have further extended the functionality by using app_dbodbc.
Again, the same 3 functions in the PHP script need to have additional lines added
do_add_record():

// Update app_dbodbc's astdb
$extension = $newvalsmailbox;
if ($newvalstemplate == "firefly"){
 $protocol = "IAX2";
} else {
 $protocol = "SIP";
}
$dnd = $newvalsDnD;
$cd = $newvalsCallDiversion;
$cdu = $newvalsDivertUnavailable;
$location = $newvalslocation;

$query_dbodbc = "INSERT INTO astdb VALUES ('Protocol','$extension','$protocol')";
$res = $this->myquery($query_dbodbc, LINE);
$query_dbodbc = "INSERT INTO astdb VALUES ('dnd','$extension','$dnd')";
$res = $this->myquery($query_dbodbc, LINE);
$query_dbodbc = "INSERT INTO astdb VALUES ('cd','$extension','$cd')";
$res = $this->myquery($query_dbodbc, LINE);
$query_dbodbc = "INSERT INTO astdb VALUES ('cdu','$extension','$cdu')";
$res = $this->myquery($query_dbodbc, LINE);
$query_dbodbc = "INSERT INTO astdb VALUES ('location','$extension','$location')";
$res = $this->myquery($query_dbodbc, LINE);


I have a similar UI which users can use to amend their own settings.
They just see their own details (of course) & login using their shortname & password (authentication is controlled using mod_auth_mysql).


See also:


back to Asterisk GUI

Comments

Comments Filter
222

333It don't work

by , Thursday 28 of October, 2004 [07:58:44 UTC]
I need to make following changes to get the script working.

$mailbox=$result0;
$template=$result1;
$password=$result2;
$fullname=$result3;
$pickupgroup=$result4;
....
....