package AGI;
sub new {
my ($class, %args) = @_;
my $self = {};
$self->{'callback'} = undef;
$self->{'status'} = undef;
$self->{'lastresponse'} = undef;
bless $self, ref $class || $class;
return $self;
}
sub DESTROY(){
}
sub ReadParse {
my ($self, $fh) = @_;
my %input = ();
$fh = \*STDIN if (!$fh);
while (<$fh>) {
chomp;
last unless length($_);
if (/^agi_(\w+)\:\s+(.*)$/) {
$input{$1} = $2;
}
}
if (defined($DEBUG)&&($DEBUG>0)) {
print STDERR "AGI Environment Dump:\n";
foreach $i (sort keys %input) {
print STDERR " -- $i = $input{$i}\n";
}
}
return %input;
}
sub setcallback {
my ($self, $function) = @_;
if (defined($function) && ref($function) eq 'CODE') {
$self->{'callback'} = $function;
}
}
sub callback {
my ($self, $result) = @_;
if (defined($self->{'callback'}) && ref($self->{'callback'}) eq 'CODE') {
&{$self->{'callback'}}($result);
}
}
sub execute {
my ($self, $command) = @_;
$self->_execcommand($command);
my $res = $self->_readresponse();
return $self->_checkresult($res);
}
sub _execcommand {
my ($self, $command, $fh) = @_;
$fh = \*STDOUT if (!$fh);
select ((select ($fh), $| = 1)[0]);
return -1 if (!defined($command));
return print $fh "$command\n";
}
sub _readresponse {
my ($self, $fh) = @_;
my $response = undef;
$fh = \*STDIN if (!$fh);
$response = <$fh> || return '200 result=-1 (noresponse)';
chomp($response);
return $response;
}
sub _checkresult {
my ($self, $response) = @_;
return undef if (!defined($response));
my $result = undef;
$self->_lastresponse($response);
if ($response =~ /^200/) {
if ($response =~ /result=(-?[\d*#]+)/) {
$result = $1;
}
} elsif ($response =~ /\(noresponse\)/) {
$self->_status('noresponse');
} else {
print STDERR "Unexpected result '$response'\n" if (defined($DEBUG) && $DEBUG);
}
print STDERR "_checkresult($response) = $result\n" if (defined($DEBUG) && $DEBUG>3);
return $result;
}
sub _status {
my ($self, $status) = @_;
if (defined($status)) {
$self->{'status'} = $status;
} else {
return $self->{'status'};
}
}
sub _lastresponse {
my ($self, $response) = @_;
if (defined($response)) {
$self->{'lastresponse'} = $response;
} else {
return $self->{'lastresponse'};
}
}
sub stream_file {
my ($self, $filename, $digits) = @_;
my $ret = 0;
$digits = '""' if (!defined($digits));
return -1 if (!defined($filename));
$ret = $self->execute("STREAM FILE $filename $digits");
$self->callback($ret) if ($ret == -1);
return $ret;
}
sub send_text {
my ($self, $text) = @_;
my $ret = 0;
return $ret if (!defined($text));
$ret = $self->execute("SEND TEXT \"$text\"");
$self->callback($ret) if ($ret == -1);
return $ret;
}
sub send_image {
my ($self, $image) = @_;
my $ret = 0;
return -1 if (!defined($image));
$ret = $self->execute("SEND IMAGE $image");
$self->callback($ret) if ($ret == -1);
return $ret;
}
sub say_number {
my ($self, $number, $digits) = @_;
my $ret = 0;
$digits = '""' if (!defined($digits));
return -1 if (!defined($number));
$number =~ s/\D//g;
$ret = $self->execute("SAY NUMBER $number $digits");
$self->callback($ret) if ($ret == -1);
return $ret;
}
sub say_digits {
my ($self, $number, $digits) = @_;
my $ret = 0;
$digits = '""' if (!defined($digits));
return -1 if (!defined($number));
$number =~ s/\D//g;
$ret = $self->execute("SAY DIGITS $number $digits");
$self->callback($ret) if ($ret == -1);
return $ret;
}
sub answer {
my ($self) = @_;
my $ret = 0;
$ret = $self->execute('ANSWER');
$self->callback($ret) if ($ret == -1);
return $ret;
}
sub get_data {
my ($self, $filename, $timeout, $maxdigits) = @_;
my $ret = undef;
return -1 if (!defined($filename));
$ret = $self->execute("GET DATA $filename $timeout $maxdigits");
$self->callback($ret) if ($ret == -1);
return $ret;
}
sub set_callerid {
my ($self, $number) = @_;
return if (!defined($number));
return $self->execute("SET CALLERID $number");
}
sub set_context {
my ($self, $context) = @_;
return -1 if (!defined($context));
return $self->execute("SET CONTEXT $context");
}
sub set_extension {
my ($self, $extension) = @_;
return -1 if (!defined($extension));
return $self->execute("SET EXTENSION $extension");
}
sub set_priority {
my ($self, $priority) = @_;
return -1 if (!defined($priority));
return $self->execute("SET PRIORITY $priority");
}
sub receive_char {
my ($self, $timeout) = @_;
my $ret = 0;
#wait forever if timeout is not set. is this the prefered default?
$timeout = 0 if (!defined($timeout));
$ret = $self->execute("RECEIVE CHAR $timeout");
$self->callback($ret) if ($ret == -1);
return $ret;
}
sub tdd_mode {
my ($self, $mode) = @_;
return 0 if (!defined($mode));
return $self->execute("TDD MODE $mode");
}
sub wait_for_digit {
my ($self, $timeout) = @_;
my $ret = 0;
$timeout = -1 if (!defined($timeout));
$ret = $self->execute("WAIT FOR DIGIT $timeout");
$self->callback($ret) if ($ret == -1);
return $ret;
}
sub record_file {
my ($self, $filename, $format, $digits, $timeout, $beep) = @_;
my $ret = 0;
return -1 if (!defined($filename));
$digits = '""' if (!defined($digits));
$ret = $self->execute("RECORD FILE $filename $format $digits $timeout");
$self->callback($ret) if ($ret == -1);
return $ret;
}
sub set_autohangup {
my ($self, $time) = @_;
$time = 0 if (!defined($time));
return $self->execute("SET AUTOHANGUP $time");
}
sub hangup {
my ($self, $channel) = @_;
if ($channel) {
return $self->execute("HANGUP $channel");
} else {
return $self->execute("HANGUP");
}
}
sub exec {
my ($self, $app, $options) = @_;
return -1 if (!defined($app));
$options = '""' if (!defined($options));
return $self->execute("EXEC $app $options");
}
sub channel_status {
my ($self, $channel) = @_;
return $self->execute("CHANNEL STATUS $channel");
}
sub set_variable {
my ($self, $variable, $value) = @_;
return $self->execute("SET VARIABLE $variable $value");
}
sub get_variable {
my ($self, $variable) = @_;
my $result = undef;
if ($self->execute("GET VARIABLE $variable")) {
my $tempresult = $self->_lastresponse();
if ($tempresult =~ /\((.*)\)/) {
$result = $1;
}
}
return $result;
}
sub verbose {
my ($self, $message, $level) = @_;
return $self->execute("VERBOSE \"$message\" $level");
}
sub database_get {
my ($self, $family, $key) = @_;
my $result = undef;
if ($self->execute("DATABASE GET $family $key")) {
my $tempresult = $self->_lastresponse();
if ($tempresult =~ /\((.*)\)/) {
$result = $1;
}
}
return $result;
}
sub database_put {
my ($self, $family, $key, $value) = @_;
return $self->execute("DATABASE PUT $family $key $value");
}
sub database_del {
my ($self, $family, $key) = @_;
return $self->execute("DATABASE DEL $family $key");
}
sub database_deltree {
my ($self, $family, $key) = @_;
return $self->execute("DATABASE DELTREE $family $key");
}
sub noop {
my ($self) = @_;
return $self->execute("NOOP");
}
sub set_music {
my ($self, $mode, $class) = @_;
return $self->execute("SET MUSIC $mode $class");
}
package clsAANode;
# action definition
$ACT_NODE = 1;
$ACT_PLAY = 2;
$ACT_CODE = 3;
$ACT_SIGR = 4;
$ACT_MESG = 5;
$ACT_BACK = 6;
$ACT_MEM = 7;
$ACT_TOP_LISTEN = 8;
$ACT_TOP_PRESENT = 9;
#Key_init
# class constructor
sub new() {
my ($class,$agi,$conn,$callerid,$debug,$node_id, $p_node_id,$description, $prompt, $prompt_text,$autoexec,$dtmfs) = @_;
my $self = {};
$self->{agi} = $agi;
$self->{dbconn} = $conn;
$self->{cid} = $callerid;
$self->{debug} = $debug;
$self->{node_id} = $node_id;
$self->{p_node_id} = $p_node_id;
$self->{description} = $description;
$self->{prompt} = $prompt;
$self->{prompt_text} = $prompt_text;
$self->{autoexec} = $autoexec;
$self->{dtmfs} = $dtmfs;
$self->{node_actions} = undef;
$self->{sound_dir} = "/var/lib/asterisk/sounds/";
$self->{music_dir} = "/var/lib/asterisk/sounds/1900/music/";
$self->{record_dir} = "/var/lib/asterisk/sounds/1900/record/";
$self->{sound_dir_1900} = "/var/lib/asterisk/sounds/1900/sounds/";
$self->{child_dir} = "/var/lib/asterisk/sounds/1900/child_record/";
$self->{sound_dir_man} = "/var/lib/asterisk/sounds/1900/Nam/";
$self->{sound_dir_woman} = "/var/lib/asterisk/sounds/1900/Nu/";
$self->{nums_of_aciton} = 0;
bless($self, $class);
}
# class destructor
sub DESTROY() {
}
#
# Play node's prompt and return user input
#
sub play_prompt() {
my $self= shift;
my $dtmf=$self->{dtmfs};
$dtmf="#" if ($dtmf=="");
&log_to_file("dtmf=$dtmf\n") if ($debug == 1);
my $sound_file = $self->{sound_dir_1900} . $self->{prompt};
$sound_file =~ s/.gsm$//g;
&log_to_file("File sound=$sound_file") if ($self->{debug}==1);
# my $sql = "SELECT node_id, p_node_id,description, prompt, prompt_text,autoexec,dtmfs"
# . " FROM tbl_node WHERE node_id=" . $self->{p_node_id};
# $self->{dbconn}->query($sql);
# my ($nid, $pid,$desc, $pmt, $pmtt,$auto,$dtmfs) = $self->{dbconn}->{sth}->fetchrow_array();
my $resp;
# Hung: Change here if (($self->{node_id}==14)) to now
if (($self->{node_id}==14) or ($self->{node_id}==34) or ($self->{node_id}==44) or ($self->{node_id}==51) or ($self->{node_id}==52)or ($self->{node_id}==53) or ($self->{node_id}==55)){
$resp=$self->my_get_data($sound_file,,5000,2);
}else{
$resp = $self->my_stream_file($sound_file,$dtmf);
}
return $resp;
}
#
# Run node: play node's prompt, get user input,
# compare user input with node's actions,
# execute and if necessary, return next node
#
sub run_node() {
my $self=shift;
if($self->{nums_of_action} == 0) {
$self->load_node_actions();
return undef if($self->{nums_of_action} == 0);
}
# play node's prompt & get user input
my $input = -1;
$input = $self->play_prompt();
#log_to_file("User enter $input");
return $self if($input == -1);
my $action = undef;
my $found = 0;
&log_to_file("input=$input") if ($self->{debug}==1);
my $i = 0;
&log_to_file("nums_of_action=$self->{nums_of_action}") if ($self->{debug}==1);
# compare action dtmf and user input
my $dtmf=-1;
if ($input > -1){
for($i = 0; $i < $self->{nums_of_action}; $i++) {
&log_to_file("dtmf=$self->{node_actions}[$i]->{dtmf}\n") if ($self->{debug}==1);
# check press exist
if($self->{node_actions}[$i]->{dtmf} == $input) {
$found = 1;
$dtmf=$self->{node_actions}[$i]->{dtmf};
last;
}
}
} else {
&log_to_file("input=$input autoexec=$self->{autoexec}") if ($self->{debug}==1);
# Tu dong chay khong bam phim gi
if ($self->{autoexec}==1){
$found=1 ;
$i=0;
$dtmf='#';
}
&log_to_file("Is Found=$found") if ($self->{debug}==1);
}
# run action if exist
if($found == 1) {
return $self->run_action($self->{node_actions}[$i],$dtmf);
} else {
# check default action
if($input eq '9') { # default of '9' is Back
# create parent node
my $new_node = $self->get_parent_node();
# this node does not have parent (root node)
return undef if(!defined($new_node));
return $new_node;
} elsif($input =='*'){
# do nothing, fall through
&log_to_file("p_node=$self->{p_node_id}\n") if ($self->{debug}==1);
$self->{p_node_id}=1;
$new_node = $self->get_parent_node();
# this node does not have parent (root node)
return undef if(!defined($new_node));
return $new_node;
}
}
# if user enter an invalid value, return this node
#log_to_file("user enter $input, invalid value");
return $self;
}
#
# execute node action
#
sub run_action() {
my ($self, $act,$dtmf) = @_;
&log_to_file("action_value= $act->{action}") if ($self->{debug}==1);
if($act->{action} == $ACT_NODE) {
&log_to_file("action_value= $act->{action_value}") if ($self->{debug}==1);
my $new_node = create_node($self->{agi},$self->{dbconn},$self->{cid},$self->{debug},$act->{action_value});
return $new_node;
} elsif($act->{action} == $ACT_BACK) {
$new_node = $self->get_parent_node();
if(!defined($new_node)) {
return undef;
}
return $new_node;
}elsif($act->{action} == $ACT_PLAY) {
my $get_key=$self->music_play($dtmf);
&log_to_file("get_key = $get_key") if ($self->{debug}==1);
if ($get_key eq '9'){
&log_to_file("action_value= $act->{action_value}") if ($self->{debug}==1);
my $new_node = create_node($self->{agi},$self->{dbconn},$self->{cid},$self->{debug},$self->{node_id});
return $new_node;
}elsif ($get_key eq '*'){
&log_to_file("action_value= $act->{action_value}") if ($self->{debug}==1);
my $new_node = create_node($self->{agi},$self->{dbconn},$self->{cid},$self->{debug},1);
return $new_node;
}
} elsif($act->{action} == $ACT_CODE) {
my $get_key=$self->music_play_code($dtmf);
if ($get_key eq '9'){
&log_to_file("action_value= $act->{action_value}") if ($self->{debug}==1);
my $new_node = create_node($self->{agi},$self->{dbconn},$self->{cid},$self->{debug},$self->{node_id});
return $new_node;
}elsif ($get_key eq '*'){
&log_to_file("action_value= $act->{action_value}") if ($self->{debug}==1);
my $new_node = create_node($self->{agi},$self->{dbconn},$self->{cid},$self->{debug},1);
return $new_node;
}
}elsif($act->{action} == $ACT_MEM) {
my $get_key=$self->commend_member_id();
if ($get_key eq '9'){
&log_to_file("action_value= $act->{action_value}") if ($self->{debug}==1);
my $new_node = create_node($self->{agi},$self->{dbconn},$self->{cid},$self->{debug},$self->{node_id});
return $new_node;
}elsif ($get_key eq '*'){
&log_to_file("action_value= $act->{action_value}") if ($self->{debug}==1);
my $new_node = create_node($self->{agi},$self->{dbconn},$self->{cid},$self->{debug},0);
return $new_node;
}
}elsif($act->{action} == $ACT_TOP_LISTEN) {
my $get_key=$self->music_play1($dtmf,1);
&log_to_file("get_key = $get_key") if ($self->{debug}==1);
if ($get_key eq '9'){
&log_to_file("action_value= $act->{action_value}") if ($self->{debug}==1);
my $new_node = create_node($self->{agi},$self->{dbconn},$self->{cid},$self->{debug},$self->{node_id});
return $new_node;
}elsif ($get_key eq '*'){
&log_to_file("action_value= $act->{action_value}") if ($self->{debug}==1);
my $new_node = create_node($self->{agi},$self->{dbconn},$self->{cid},$self->{debug},8);
return $new_node;
}
}elsif($act->{action} == $ACT_TOP_PRESENT) {
my $get_key=$self->music_play1($dtmf,2);
&log_to_file("get_key = $get_key") if ($self->{debug}==1);
if ($get_key eq '9'){
&log_to_file("action_value= $act->{action_value}") if ($self->{debug}==1);
my $new_node = create_node($self->{agi},$self->{dbconn},$self->{cid},$self->{debug},$self->{node_id});
return $new_node;
}elsif ($get_key eq '*'){
&log_to_file("action_value= $act->{action_value}") if ($self->{debug}==1);
my $new_node = create_node($self->{agi},$self->{dbconn},$self->{cid},$self->{debug},8);
return $new_node;
}
}elsif($act->{action} == $ACT_SIGR) {
&log_to_file("action_value= $act->{action_value}") if ($self->{debug}==1);
my $new_node = create_node($self->{agi},$self->{dbconn},$self->{cid},$self->{debug},$act->{action_value});
return $new_node;
}elsif($act->{action} == $ACT_MESG) {
my $get_node=$self->listen_message($dtmf);
} else {
# something wrong here???
# return ifself;
return $self;
}
}
#
# return parent of this node
#
sub get_parent_node() {
my $self=shift;
if($self->{p_node_id} == -1) {
return undef;
}
# select parent node
my $sql = "SELECT node_id, p_node_id,description, prompt, prompt_text,autoexec,dtmfs"
. " FROM tbl_node WHERE node_id=" . $self->{p_node_id};
$self->{dbconn}->query($sql);
my ($nid, $pid,$desc, $pmt, $pmtt,$auto,$dtmfs) = $self->{dbconn}->{sth}->fetchrow_array();
# create new node
my $pnode = clsAANode->new($self->{agi},$self->{dbconn},$self->{cid},$self->{debug},$nid, $pid,$desc, $pmt, $pmtt,$auto,$dtmfs);
# return node
return $pnode;
}
sub listen_message(){
my ($self,$dtmf) = @_;
&log_to_file("start listen_message") if ($self->{debug}==1);
my $password_message;
my $phone_number;
my $file_music;
my $password;
my $song;
my $file_record;
my $i = 0;
my @my_messages = undef;
######### Hung Change Here ###########################
my $sql;
# get node con
my $sql1 = "SELECT nodeaction_value"
. " FROM tbl_nodeaction WHERE dtmf_value='$dtmf' and node_id=" . $self->{node_id};
&log_to_file("sql=$sql1") if ($self->{debug}==1);
$self->{dbconn}->query($sql1);
my $node_id_child;
$node_id_child = $self->{dbconn}->{sth}->fetchrow_array ;
&log_to_file("Node_id_child=$node_id_child") if ($self->{debug}==1);
$self->{dbconn}->{sth}->finish;
#get file sound
$sql = "SELECT prompt"
. " FROM tbl_node WHERE node_id=" . $node_id_child;
&log_to_file("sql=$sql") if ($self->{debug}==1);
$self->{dbconn}->query($sql);
my $file_prompt=$self->{dbconn}->{sth}->fetchrow_array;
$self->{dbconn}->{sth}->finish;
$file_prompt=$self->{sound_dir_1900}.$file_prompt;
&log_to_file("file_prompt=$file_prompt") if ($self->{debug}==1);
########################################################
# fetching data from statement handle
#my $sql="select phone_number,password,song_id,file_record from tbl_voicemail where phone_number='$self->{cid}'";
#$self->{dbconn}->query($sql);
#while(($phone_number,$password,$song,$file_record) = $self->{dbconn}->{sth}->fetchrow_array) {
# $my_messages[$i] = clsMessage->new($phone_number,$password,$song,$file_record);
# &log_to_file("phone=$phone_number,password=$password,song=$song ,file_record=$file_record\n") if ($self->{debug}==1);
# $i++;
#}
#my $n=$i;
my $check_password=0;
my $count=0;
$self->{dbconn}->{sth}->finish;
do {
&log_to_file("file_record=$my_messages[$i]->{file}") if ($self->{debug}==1);
$self->{agi}->stream_file('wrong_input') if($count > 0);
#Hung : change here $password_message=$self->{agi}->get_data('message_and_song',20000,10); --> $password_message=$self->{agi}->get_data('message_and_song',20000,7);
$password_message=$self->{agi}->get_data($file_prompt,20000,8);
# $count++;
$check_password=0;
# Hung: Change any phonenumber can listen message if type password correct
################################################################################################
my $sql="select count(*) from tbl_voicemail where password = '$password_message'";
$self->{dbconn}->query($sql);
$check_password = $self->{dbconn}->{sth}->fetchrow_array;
###############################################################################################
# for ($i=0;$i<$n;$i++){
# my $real_password=$my_messages[$i]->{password};
# &log_to_file("$password_message == $real_password\n") if ($self->{debug}==1);
# if ($password_message eq $real_password){
# $check_password=1 ;
# &log_to_file("OK\n") if ($self->{debug}==1) if ($debug == 1);
# last;
# }
# }
$count=$count+1;
&log_to_file("check_password=$check_password") if ($self->{debug}==1);
}while (($check_password==0) && ($count < 4));
my $reserve = 0;
&log_to_file("check_password=$check_password") if ($self->{debug}==1);
&log_to_file("i=$i") if ($self->{debug}==1);
if ($check_password==1){
# Hung: change here
#############################################################################################################################
my $sql="select phone_number,password,song_id,file_record,reserve_2 from tbl_voicemail where password = '$password_message'";
$self->{dbconn}->query($sql);
($phone_number,$password,$song,$file_record,$reserve) = $self->{dbconn}->{sth}->fetchrow_array;
$my_messages[$i] = clsMessage->new($phone_number,$password,$song,$file_record);
&log_to_file("phone=$phone_number,password=$password,song=$song ,file_record=$file_record\n") if ($self->{debug}==1);
#############################################################################################################################
$file_record=$my_messages[$i]->{file};
my $file_record1=($self->{record_dir}).($file_record);
$file_music=$my_messages[$i]->{song};
if($file_music == '0001'){
$file_record1 = "/var/lib/asterisk/sounds/1900/QuaTang/GhiAm/".$file_record;
&log_to_file("file_record=$file_record1") if ($self->{debug}==1);
$self->{agi}->stream_file($file_record1,"9");
} elsif($file_music == '0002') {
$file_record1 = "/var/lib/asterisk/sounds/1900/ThanTuong/GhiAm/".$file_record;
&log_to_file("file_record=$file_record1") if ($self->{debug}==1);
$self->{agi}->stream_file($file_record1,"9");
} else{
&log_to_file("file_record=$file_record1") if ($self->{debug}==1);
$self->{agi}->stream_file($file_record1,"9");
$file_music=($self->{music_dir}).($file_music);
&log_to_file("file_music=$file_music") if ($self->{debug}==1);
# if($reserve == 9) {
# $file_music=($self->{child_dir}).($file_music);
# &log_to_file("file_music=$file_music") if ($self->{debug}==1);
# }
my $input=0;
$input=$self->{agi}->stream_file($file_music,"9#*");
return $input if ($input > 0);}
}
return 57;
}
sub music_play() {
my ($self,$dtmf) = @_;
&log_to_file("Start music_play") if ($self->{debug}==1);
my $sql;
# get node con
my $sql1 = "SELECT nodeaction_value"
. " FROM tbl_nodeaction WHERE dtmf_value='$dtmf' and node_id=" . $self->{node_id};
&log_to_file("sql=$sql1") if ($self->{debug}==1);
$self->{dbconn}->query($sql1);
my $node_id_child;
$node_id_child = $self->{dbconn}->{sth}->fetchrow_array ;
&log_to_file("Node_id_child=$node_id_child") if ($self->{debug}==1);
$self->{dbconn}->{sth}->finish;
#get file sound
$sql = "SELECT prompt"
. " FROM tbl_node WHERE node_id=" . $node_id_child;
&log_to_file("sql=$sql") if ($self->{debug}==1);
$self->{dbconn}->query($sql);
my $file_prompt=$self->{dbconn}->{sth}->fetchrow_array;
$self->{dbconn}->{sth}->finish;
$file_prompt=$self->{sound_dir_1900}.$file_prompt;
&log_to_file("file_prompt=$file_prompt") if ($self->{debug}==1);
$sql = "SELECT option_id,song_id FROM tbl_current_song WHERE node_id=$self->{node_id} order by code";
&log_to_file("sql=$sql") if ($self->{debug}==1);
$self->{dbconn}->query($sql);
my $musics=undef;
my $i=0;
my $option;
my $song;
my $input1=0;
while(($option, $song) = $self->{dbconn}->{sth}->fetchrow_array) {
$musics[$i] = clsMusics->new($option, $song);
&log_to_file("opton=$option,song=$song\n" ) if ($self->{debug}==1);
$i++;
}
my $n=$i;
my $input;
$i=0;
my $file_music;
my $resp=-1;
my $count=0;
$resp=$self->my_stream_file($file_prompt,'9#*');
if (($resp eq '*') or ($resp eq '9')){
&log_to_file("resp=$resp") if ($self->{debug}==1);
return $resp;
}
while($i<$n){
PLAY_AGAIN:
$file_music=$self->{music_dir}.$musics[$i]->{song};
&log_to_file("file_music=$file_music\n") if ($self->{debug}==1);
$input=0;
$input=$self->my_stream_file_sql($file_music,"12349*");
&log_to_file("Key press =$input\n") if ($self->{debug}==1);
if($input eq '2'){
goto PLAY_AGAIN;
} elsif ($input eq '3'){
$i=$i-1 if ($i>0);
goto PLAY_AGAIN;
} elsif($input eq '4'){
my $file_record=$self->{cid}.time();
my $file_record_dir=$self->{record_dir}.$file_record;
&log_to_file("file_record_dir=$file_record_dir") if ($self->{debug}==1);
RECORD_AGAIN:
$self->{agi}->stream_file("help_record","#");
$self->{agi}->stream_file("beep");
# $self->{agi}->record_file('file_record1','gsm','#','50000');
$self->{agi}->record_file($file_record_dir,'gsm','#','50000');
&log_to_file("Message=OK") if ($self->{debug}==1);
SEND_FRIEND:
# Hung: add loop 3 times
my $dem = 0 ;
$input1 = -1 ;
while(($input1 == -1) && ($dem < 3)){
$input1=$self->my_stream_file("record_again","123#");
&log_to_file("Chon 1 ,2 ,3 # =$input1\n") if ($self->{debug}==1);
$dem = $dem + 1;
}
if($input1 eq '1'){
system("/bin/mv $file_record_dir");
goto RECORD_AGAIN;
}elsif($input1 eq '2'){
&log_to_file(" press 2OK") if ($self->{debug}==1);
$self->{agi}->stream_file($file_record_dir,"#");
&log_to_file(" end 2OK") if ($self->{debug}==1);
goto SEND_FRIEND;
} elsif ($input1 eq '3'){
$self->get_phone_number($musics[$i]->{song},$file_record);
}
}elsif(($input eq '*') or ($input eq '9')){
&log_to_file("input=$input") if ($self->{debug}==1);
return $input;
}
$i=$i+1;
}
# finish fetching data
$self->{dbconn}->{sth}->finish;
# create new node
# return node
}
sub music_play1() {
my ($self,$dtmf,$num) = @_;
&log_to_file("Start music_play") if ($self->{debug}==1);
my $sql;
# get node con
my $sql1 = "SELECT nodeaction_value"
. " FROM tbl_nodeaction WHERE dtmf_value='$dtmf' and node_id=" . $self->{node_id};
&log_to_file("sql=$sql1") if ($self->{debug}==1);
$self->{dbconn}->query($sql1);
my $node_id_child;
$node_id_child = $self->{dbconn}->{sth}->fetchrow_array ;
&log_to_file("Node_id_child=$node_id_child") if ($self->{debug}==1);
$self->{dbconn}->{sth}->finish;
#get file sound
$sql = "SELECT prompt"
. " FROM tbl_node WHERE node_id=" . $node_id_child;
&log_to_file("sql=$sql") if ($self->{debug}==1);
$self->{dbconn}->query($sql);
my $file_prompt=$self->{dbconn}->{sth}->fetchrow_array;
$self->{dbconn}->{sth}->finish;
$file_prompt=$self->{sound_dir_1900}.$file_prompt;
&log_to_file("file_prompt=$file_prompt") if ($self->{debug}==1);
if($num == 1){
$sql = "SELECT singer_id,song_id FROM view_toplisten";
&log_to_file("sql=$sql") if ($self->{debug}==1);
}
else{
$sql = "SELECT singer_id,song_id FROM view_toppresent";
&log_to_file("sql=$sql") if ($self->{debug}==1);
}
$self->{dbconn}->query($sql);
my $musics=undef;
my $i=0;
my $option;
my $song;
my $input1=0;
while(($option, $song) = $self->{dbconn}->{sth}->fetchrow_array) {
$musics[$i] = clsMusics->new($option, $song);
&log_to_file("opton=$option,song=$song\n" ) if ($self->{debug}==1);
$i++;
}
my $n=$i;
my $input;
$i=0;
my $file_music;
my $resp=-1;
my $count=0;
$resp=$self->my_stream_file($file_prompt,'9#*');
if (($resp eq '*') or ($resp eq '9')){
&log_to_file("resp=$resp") if ($self->{debug}==1);
return $resp;
}
while($i<$n){
PLAY_AGAIN:
$file_music=$self->{music_dir}.$musics[$i]->{song};
&log_to_file("file_music=$file_music\n") if ($self->{debug}==1);
$input=0;
$input=$self->my_stream_file_sql($file_music,"12349*");
&log_to_file("Key press =$input\n") if ($self->{debug}==1);
if($input eq '2'){
goto PLAY_AGAIN;
} elsif ($input eq '3'){
$i=$i-1 if ($i>0);
goto PLAY_AGAIN;
} elsif($input eq '4'){
my $file_record=$self->{cid}.time();
my $file_record_dir=$self->{record_dir}.$file_record;
&log_to_file("file_record_dir=$file_record_dir") if ($self->{debug}==1);
RECORD_AGAIN:
$self->{agi}->stream_file("help_record","#");
$self->{agi}->stream_file("beep");
# $self->{agi}->record_file('file_record1','gsm','#','50000');
$self->{agi}->record_file($file_record_dir,'gsm','#','50000');
&log_to_file("Message=OK") if ($self->{debug}==1);
SEND_FRIEND:
# Hung: add loop 3 times
my $dem = 0 ;
$input1 = -1 ;
while(($input1 == -1) && ($dem < 3)){
$input1=$self->my_stream_file("record_again","123#");
&log_to_file("Chon 1 ,2 ,3 # =$input1\n") if ($self->{debug}==1);
$dem = $dem + 1;
}
if($input1 eq '1'){
system("/bin/mv $file_record_dir");
goto RECORD_AGAIN;
}elsif($input1 eq '2'){
&log_to_file(" press 2OK") if ($self->{debug}==1);
$self->{agi}->stream_file($file_record_dir,"#");
&log_to_file(" end 2OK") if ($self->{debug}==1);
goto SEND_FRIEND;
} elsif ($input1 eq '3'){
$self->get_phone_number($musics[$i]->{song},$file_record);
}
}elsif(($input eq '*') or ($input eq '9')){
&log_to_file("input=$input") if ($self->{debug}==1);
return $input;
}
$i=$i+1;
}
# finish fetching data
$self->{dbconn}->{sth}->finish;
# create new node
# return node
}
sub music_play_code(){
my ($self,$dtmf) = @_;
&log_to_file("start music_play_code") if ($self->{debug}==1);
my $node_current;
# tim node con
my $sql1 = "SELECT nodeaction_value"
. " FROM tbl_nodeaction WHERE dtmf_value='$dtmf' and node_id=" . $self->{node_id};
&log_to_file("sql=$sql1") if ($self->{debug}==1);
$self->{dbconn}->query($sql1);
my $node_id_child;
$node_id_child = $self->{dbconn}->{sth}->fetchrow_array ;
&log_to_file("Node_id_child=$node_id_child") if ($self->{debug}==1);
$self->{dbconn}->{sth}->finish;
# Hung: Change here if (($self->{node_id}==14)) to now
if (($self->{node_id}==14) or ($self->{node_id}==34) or ($self->{node_id}==44) or ($self->{node_id}==51) or ($self->{node_id}==52)or ($self->{node_id}==53) or ($self->{node_id}==55)){
$node_current=$node_id_child;
}elsif($self->{node_id}==1){
$node_current=17;
}else{
$node_current=$self->{node_id};
}
my $sql = "SELECT option_id,song_id,code"
. " FROM tbl_current_song WHERE node_id=" . $node_current . " order by code";
&log_to_file("sql=$sql")if ($self->{debug}==1);
my $musics=undef;
my $i=0;
$self->{dbconn}->query($sql);
my $option;
my $song;
my $input1=0;
while(($option, $song,$code) = $self->{dbconn}->{sth}->fetchrow_array) {
$musics[$i] = clsMusics->new($option, $song,$code);
&log_to_file("Code=$code\n,song=$song\n") if ($self->{debug}==1);
$i++;
}
$self->{dbconn}->{sth}->finish;
my $n=$i;
$sql = "SELECT prompt,autoexec"
. " FROM tbl_node WHERE node_id=" . $node_id_child;
&log_to_file("sql=$sql") if ($self->{debug}==1);
$self->{dbconn}->query($sql);
my ($file_prompt,$autoexec)=$self->{dbconn}->{sth}->fetchrow_array;
&log_to_file("file_prompt=$file_prompt,autoexec=$autoexec") if ($self->{debug}==1);
$self->{dbconn}->{sth}->finish;
my $input;
$i=0;
my $file_music;
my $found=0;
my $song_code;
$file_prompt=$self->{sound_dir_1900}.$file_prompt;
&log_to_file("Get_code_file=$file_prompt")if ($self->{debug}==1);
PLAY_AGAIN:
$song_code=$self->my_get_data($file_prompt,5000,2);
&log_to_file("song_code=$song_code") if ($self->{debug}==1);
&log_to_file("song_code=$song_code") if ($self->{debug}==1);
$found=0;
for($i = 0; $i <$n; $i++) {
&log_to_file("code=$song_code ,music_code=$musics[$i]->{code}") if ($self->{debug}==1);
if($musics[$i]->{code} eq $song_code) {
$found = 1;
last;
}
}
if (($song_code==-1) and ($autoexec==1)) {
$found=2;
}
&log_to_file("song_code=$song_code") if ($self->{debug}==1);
&log_to_file("Tim duoc bai hat=$found") if ($self->{debug}==1);
if ($found==1){
PLAY_AGAIN1:
$file_music=$self->{music_dir}.$musics[$i]->{song};
&log_to_file("file_music=$file_music\n") if ($self->{debug}==1);
$input=0;
my $song_code1=$self->my_stream_file_sql($file_music,'12349*');
&log_to_file("Key code=$song_code\n") if ($self->{debug}==1);
if($song_code1 eq '2'){
goto PLAY_AGAIN1;
} elsif ($song_code1 eq '3'){
$i=$i-1 if ($i>0);
goto PLAY_AGAIN1;
}elsif ($song_code1 eq '1'){
$i=$i+1 if ($i<$n);
goto PLAY_AGAIN1;
}elsif ($song_code1 eq '4'){
my $file_record=$self->{cid}.time();
my $file_record_dir=$self->{record_dir}.$file_record;
&log_to_file("file_record_dir=$file_record_dir") if ($self->{debug}==1);
RECORD_AGAIN:
$self->{agi}->stream_file("help_record","#");
$self->{agi}->stream_file("beep");
$self->{agi}->record_file($file_record_dir,'gsm','#','50000');
# $self->{agi}->record_file('message','gsm','#','50000');
&log_to_file("Message=OK") if ($self->{debug}==1);
SEND_FRIEND:
# Hung: add loop 3 times
my $dem = 0 ;
$input1 = -1 ;
while(($input1 == -1) && ($dem < 3)){
$input1=$self->my_stream_file("record_again","123#");
&log_to_file("Chon 1 ,2 ,3 # =$input1\n") if ($self->{debug}==1);
$dem = $dem + 1;
}
# $input1=$self->my_stream_file("record_again","123#");
# &log_to_file("Chon 1 ,2 ,3 # =$input1\n") if ($self->{debug}==1);
if($input1 eq '1'){
system("/bin/mv $file_record_dir");
goto RECORD_AGAIN;
}elsif($input1 eq '2'){
$self->{agi}->stream_file($file_record_dir,"#");
#$self->my_stream_file('message',"#");
goto SEND_FRIEND;
} elsif ($input1 eq '3'){
$self->get_phone_number($musics[$i]->{song},$file_record);
}
}elsif($song_code1 eq '*') {
&log_to_file("Key press=$song_code1") if ($self->{debug}==1);
return $song_code1;
}elsif($song_code1 eq '9') {
&log_to_file("Key press=$song_code1") if ($self->{debug}==1);
goto PLAY_AGAIN;
} else {
&log_to_file("Play again") if ($self->{debug}==1);
goto PLAY_AGAIN;
}
}elsif(($song_code eq '*') or ($song_code eq '9')){
&log_to_file("Key press=$song_code") if ($self->{debug}==1);
return $song_code;
}
if ($found==2){
$i=0;
while($i<$n){
PLAY_AGAIN_1:
$file_music=$self->{music_dir}.$musics[$i]->{song};
&log_to_file("file_music=$file_music\n") if ($self->{debug}==1);
$input=0;
$input=$self->my_stream_file_sql($file_music,"12349*");
&log_to_file("Key press =$input\n") if ($self->{debug}==1);
if($input eq '2'){
goto PLAY_AGAIN_1;
} elsif ($input eq '3'){
$i=$i-1 if ($i>0);
goto PLAY_AGAIN_1;
} elsif($input eq '4'){
my $file_record=$self->{cid}.time();
my $file_record_dir=$self->{record_dir}.$file_record;
&log_to_file("file_record_dir=$file_record_dir") if ($self->{debug}==1);
RECORD_AGAIN_1:
$self->{agi}->stream_file("help_record","#");
$self->{agi}->stream_file("beep");
# $self->{agi}->record_file('file_record1','gsm','#','50000');
$self->{agi}->record_file($file_record_dir,'gsm','#','50000');
&log_to_file("Message=OK") if ($self->{debug}==1);
SEND_FRIEND_1:
# Hung: add loop 3 times
my $dem = 0 ;
$input1 = -1 ;
while(($input1 == -1) && ($dem < 3)){
$input1=$self->my_stream_file("record_again","123#");
&log_to_file("Chon 1 ,2 ,3 # =$input1\n") if ($self->{debug}==1);
$dem = $dem + 1;
}
# $input1=$self->my_stream_file("record_again","123#");
# &log_to_file("Chon 1 ,2 ,3 # =$input1\n") if ($self->{debug}==1);
if($input1 eq '1'){
system("/bin/mv $file_record_dir");
goto RECORD_AGAIN_1;
}elsif($input1 eq '2'){
&log_to_file(" press 2OK") if ($self->{debug}==1);
$self->{agi}->stream_file($file_record_dir,"#");
&log_to_file(" end 2OK") if ($self->{debug}==1);
goto SEND_FRIEND_1;
} elsif ($input1 eq '3'){
$self->get_phone_number($musics[$i]->{song},$file_record);
}
}elsif($input eq '*') {
&log_to_file("input=$input") if ($self->{debug}==1);
return $input;
}elsif ($input eq '9'){
goto PLAY_AGAIN;
}
$i=$i+1;
}
}
goto PLAY_AGAIN;
}
#
# Load all actions of this node
#
########## ADD 3 Function here ########################################################
sub check_member_id() {
my ($self,$member_id) = @_;
my $ret=0;
$sql = "SELECT sex_id FROM view_memberok where member_id=$member_id";
&log_to_file("sql=$sql") if ($self->{debug}==1);
$self->{dbconn}->query($sql);
$ret = $self->{dbconn}->{sth}->fetchrow_array or $ret = 0;
# finish fetching data
$self->{dbconn}->{sth}->finish;
if ($ret eq '01'){
return 1;
}elsif ($ret eq '02'){
return 2;
}
# create new node
return $ret;
}
sub get_prompt(){
my ($self,$member)=@_;
my $sql="select prompt from view_memberok where member_id=$member";
$self->{dbconn}->query($sql);
$ret = $self->{dbconn}->{sth}->fetchrow_array or $ret = 0;
# finish fetching data
$self->{dbconn}->{sth}->finish;
# create new node
return $ret;
}
sub commend_member_id(){
my ($self) = @_;
my $count=0;
my $member_exist=0;
my $member;
# run this node
PLAY_AGIAN:
$count=0;
do {
$self->my_stream_file('pass_bad','9*') if ($count>0) ;
$member=$self->my_get_data('member_id',50000,6);
$member_exist=$self->check_member_id($member);
&log_to_file("member_exist=$member_exist") if ($self->{debug}==1);
$count=$count+1;
}while (( $member_exist==0) && ($count < 3));
my $commend_dir;
if ($member_exist == 1){
$commend_dir=$self->{sound_dir_man};
}elsif ($member_exist == 2){
$commend_dir=$self->{sound_dir_woman};
} else
{
return '9';
}
my $file_music=$commend_dir.$self->get_prompt($member);;
&log_to_file("file_music=$file_music\n") if ($self->{debug}==1);
my $input=0;
$input=$self->my_stream_file($file_music,"9*");
&log_to_file("Key press =$input\n") if ($self->{debug}==1);
if($input eq '*') {
&log_to_file("input=$input") if ($self->{debug}==1);
return $input;
}elsif ($input eq '9'){
goto PLAY_AGIAN;
}
return '9';
}
#######################################################################################
sub get_phone_number(){
my ($self,$song_id,$file_record) = @_;
my $count = 0;
my $check_phone_number;
my $phone_number;
my $check=0;
do {
# run this node
# Hung change get_data('input_phone') --> get_data('input_phone',10000,11);
INSERT_AGAIN:
$check_phone_number = -1;
$phone_number=$self->{agi}->get_data('input_phone',10000,11);
&log_to_file("Number_phone=$phone_number\n") if ($self->{debug}==1);
&log_to_file("File_record=$file_record\n") if ($self->{debug}==1);
my $input=$self->{agi}->stream_file('message_send') if ($self->{debug}==1);
goto INSERT_AGAIN if ($phone_number == -1);
$self->{agi}->set_variable('LANGUAGE()','vn');
$self->{agi}->say_digits($phone_number);
# Hung : replay 3 times
my $dem1 = 0;
# $check_phone_number = -1;
while(($check_phone_number == -1) && ($dem1 < 4)){
$check_phone_number=$self->my_stream_file('phone_again','#*');
$dem1 = $dem1 + 1;
}
$check=1 if ($check_phone_number eq '#');
goto INSERT_AGAIN if ($check_phone_number eq '*');
$count++;
}while (( $check==0) && ($count < 5));
return 1 if ($check==0);
my $passwd=1000000+int(rand(9000000));
&log_to_file("passwd=$passwd") if ($self->{debug}==1);
my $sql = "insert into tbl_voicemail(callerid,phone_number,password,song_id,voicemail_date,file_record) values('$self->{cid}','$phone_number',$passwd,'$song_id',now(),'$file_record')";
&log_to_file("sql=$sql") if ($self->{debug}==1);
$self->{dbconn}->query($sql);
$self->{dbconn}->{sth}->finish;
&log_to_file("Chuyen bai hat va loi nha cho nguoi than") if ($self->{debug}==1);
$sql = "update tbl_song set present = present + 1 WHERE song_id = ".$song_id;
&log_to_file("sql=$sql") if ($self->{debug}==1);
$self->{dbconn}->query($sql);
$self->{dbconn}->{sth}->finish;
return 1;
}
sub load_node_actions() {
my $self = shift;
#Them
&log_to_file("Start load_node_action $self->{node_id}\n") if ($self->{debug}==1);
my $sql = "SELECT action_id, nodeaction_value, dtmf_value "
. "FROM tbl_nodeaction "
. "WHERE node_id=" . $self->{node_id};
# execute query
$self->{dbconn}->query($sql);
my $i = 0;
@actions = undef;
# fetching data from statement handle
while(($act, $value, $dtmf) = $self->{dbconn}->{sth}->fetchrow_array) {
$actions[$i] = clsAANodeAction->new($act, $value, $dtmf);
#them
&log_to_file("act=$act\n,value=$value\n,dtmf=$dtmf\n") if ($self->{debug}==1);
$i++;
}
# finish fetching data
$self->{dbconn}->{sth}->finish;
# save the result
$self->{node_actions} = \@actions;
$self->{nums_of_action} = $i;
&log_to_file("nums_of_action=$self->{nums_of_action}" ) if ($self->{debug}==1);
}
# create new node with node_id
sub create_node() {
my ($agi,$dbconn,$cid,$debug,$id) = @_;
&log_to_file("id=$id") if ($self->{debug}==1);
# select node
my $sql = "SELECT node_id, p_node_id,description, prompt, prompt_text,autoexec,dtmfs"
. " FROM tbl_node WHERE node_id=" . $id;
$dbconn->query($sql);
my ($nid, $pid,$desc, $pmt, $pmtt,$auto,$dtmfs) =$dbconn->{sth}->fetchrow_array();
&log_to_file("$nid, $pid,$desc, $pmt, $pmtt,$auto,$dtmfs") if ($self->{debug}==1);
# create new node
my $nnode = clsAANode->new($agi,$dbconn,$cid,$debug,$nid, $pid,$desc, $pmt, $pmtt,$auto,$dtmfs);
# return node
return $nnode;
}
sub my_get_data() {
my ($self,$file, $timeout, $maxdigit) = @_;
return -1 unless defined($file);
$timeout = 10 unless $timeout;
$maxdigit = 20 unless $maxdigit;
my $data = $self->{agi}->get_data($file, $timeout, $maxdigit);
$data=-1 unless defined($data) ;
chomp($data);
return $data =~ /\*(\d+)$/ ? $1 :
$data =~ /(\d+)\*$/ ? $1 : $data;
}
sub my_get_data_sql() {
my ($self,$file, $timeout, $maxdigit) = @_;
return -1 unless defined($file);
my $duration=0;
$timeout = 10 unless $timeout;
$maxdigit = 20 unless $maxdigit;
my ($giay, $phut, $gio, $ngay, $thang, $nam) = localtime();
$thang++;
$nam+=1900;
my $now = sprintf("%04i-%02i-%02i %02i:%02i:%02i", $nam, $thang, $ngay, $gio, $phut, $giay);
my $my_date_start=time();
my $data = $self->{agi}->get_data($file, $timeout, $maxdigit);
my $my_date_end=time();
$duration=$my_date_end-$my_date_start;
$data=-1 unless defined($data);
if ($duration > 2){
$file =~ /(.*)\/(.*)$/;
my $filename=$2;
my $sql = "insert into tbl_song_history(callerid,song_id,history_date,duration) values('$self->{cid}','$filename','$now',$duration)";
&log_to_file("sql=$sql") if ($self->{debug}==1);
$self->{dbconn}->query($sql);
$self->{dbconn}->{sth}->finish;
$sql = "update tbl_song set listen = listen + 1 WHERE song_id = ".$filename;
&log_to_file("sql=$sql") if ($self->{debug}==1);
$self->{dbconn}->query($sql);
$self->{dbconn}->{sth}->finish;
}
chomp($data);
return $data =~ /\*(\d+)$/ ? $1 :
$data =~ /(\d+)\*$/ ? $1 : $data;
}
sub my_stream_file() {
my ($self,$file, $digits) = @_;
return -1 if(!defined($file));
if(defined($digits)) {
my $option = $self->{agi}->stream_file($file, $digits);
if($option == -1 || $option == 0) { #stream_file returns without a digit being pressed
return -1;
}
return chr($option);
} else {
my $option= $self->{agi}->stream_file($file);
$option=-1 unless defined($option);
return $option;
}
}
sub my_stream_file_sql() {
my ($self,$file, $digits) = @_;
return -1 if(!defined($file));
my ($giay, $phut, $gio, $ngay, $thang, $nam) = localtime();
$thang++;
$nam+=1900;
my $now = sprintf("%04i-%02i-%02i %02i:%02i:%02i", $nam, $thang, $ngay, $gio, $phut, $giay);
my $my_date_start=time();
if(defined($digits)) {
&log_to_file("digits=$digits") if ($self->{debug}==1);
my $option = $self->{agi}->stream_file($file, $digits);
&log_to_file("option=$option") if ($self->{debug}==1);
my $my_date_end=time();
my $duration=$my_date_end-$my_date_start;
if ($duration > 2){
$file =~ /(.*)\/(.*)$/;
my $filename=$2;
my $sql = "insert into tbl_song_history(callerid,song_id,history_date,duration) values('$self->{cid}','$filename','$now',$duration)";
&log_to_file("sql=$sql") if ($self->{debug}==1);
$self->{dbconn}->query($sql);
$self->{dbconn}->{sth}->finish;
$sql = "update tbl_song set listen = listen + 1 WHERE song_id = ".$filename;
&log_to_file("sql=$sql") if ($self->{debug}==1);
$self->{dbconn}->query($sql);
$self->{dbconn}->{sth}->finish;
}
if($option == -1 || $option == 0) { #stream_file returns without a digit being pressed
return -1;
}
return chr($option);
} else {
my $option= $self->{agi}->stream_file($file);
my $my_date_end=time();
my $duration=$my_date_end-$my_date_start;
if ($duration > 2){
$file =~ /(.*)\/(.*)$/;
my $filename=$2;
my $sql = "insert into tbl_song_history(callerid,song_id,date_time,duration) values('$self->{cid},'$filename','$now',$duration)";
&log_to_file("sql=$sql") if ($self->{debug}==1);
$self->{dbconn}->query($sql);
$self->{dbconn}->{sth}->finish;
$sql = "update tbl_song set listen = listen + 1 WHERE song_id = ".$filename;
&log_to_file("sql=$sql") if ($self->{debug}==1);
$self->{dbconn}->query($sql);
$self->{dbconn}->{sth}->finish;
}
if($option == -1 || $option == 0) { #stream_file returns without a digit being pressed
return -1;
}
}
}
sub log_to_file() {
my $data = shift;
my $current = gmtime();
open LOG, ">>/tmp/aa.log";
print LOG "$current -- $data\n";
close LOG;
}
# End class clsAANode
package clsAANodeAction;
# class constructor
sub new() {
my ($class, $act, $act_value, $dtmf) = @_;
my $self = {};
$self->{action} = $act;
$self->{action_value} = $act_value;
$self->{dtmf} = $dtmf;
bless($self, $class);
}
# class destructor
sub DESTROY() {
}
#
# return aciton value
#
sub get_action_value() {
my $self = shift;
return $self->{action_value};
}
#
# return action id
#
sub get_action_id() {
my $self = shift;
return $self->{action};
}
#
# return dtmf value
#
sub get_dtmf() {
my $self = shift;
return $self->{dtmf};
}
# End class clsAANodeAction
package clsMusics;
# class constructor
sub new() {
my ($class, $option,$song,$code) = @_;
my $self = {};
$self->{option} = $option;
$self->{song} = $song;
$self->{code} = $code if(defined($code)) ;
bless($self, $class);
}
# class destructor
sub DESTROY() {
}
sub get_option() {
my $self = shift;
return $self->{option};
}
sub get_song() {
my $self = shift;
return $self->{song};
}
package clsMessage;
# class constructor
sub new() {
my ($class, $phone_number,$password,$song,$file_record) = @_;
my $self = {};
$self->{phone} = $phone_number;
$self->{password} = $password;
$self->{song} = $song;
$self->{file} = $file_record;
bless($self, $class);
}
# class destructor
sub DESTROY() {
}
sub get_phone() {
my $self = shift;
return $self->{phone};
}
sub get_password(){
my $self=shift;
return $self->{password};
}
sub get_song() {
my $self = shift;
return $self->{song};
}
package clsDBConnection;
use DBI;
use vars qw/@ISA/;
# use Connectbox::Classes;
@ISA = qw/DBI clsErrors/;
# use Connectbox::Common;
sub new {
my $class = shift;
my $self = {};
bless $self, $class;
$self->{LastSQL} = "";
$self->{RecordsCount} = 0;
$self->{SQL} = "";
$self->{Where} = "";
$self->{Order} = "";
$self->{Parameters} = "";
$self->{wp} = "";
$self->{AbsolutePage} = 0;
$self->{PageSize} = 0;
$self->{DB} = "MySQL";
$self->{DataSource} = 'dbi:mysql:asterisk:localhost';
$self->{UserName} = "username"; #### user mysql
$self->{Password} = "password";
$self->{PrintError} = 1;
$self->{RaiseError} = 0;
$self->{RecordNumber} = 0;
$self->{DateFormat} = ["yyyy", "-", "mm", "-", "dd", " ", "HH", ":", "nn", ":", "ss"];
$self->{BooleanFormat} = [1, 0, ""];
$self->{Uppercase} = 0;
$self->{Errors} = clsErrors->new();
$self->{Parameters} = undef;
$self->{dbh} = undef;
$self->{dbh} = DBI->connect_cached( $self->{DataSource}, $self->{UserName}, $self->{Password}, { PrintError => $self->{PrintError}, RaiseError => $self->{RaiseError} } )
or die $DBI::errstr;
$self->{dbh}->{LongReadLen} = 2000;
$self->{dbh}->{LongTruncOk} = 1;
$self->{sth} = undef;
$self->{RecordHashRef} = "";
return $self;
}
sub MoveToPage {
my ($self, $Page) = @_;
if ( $self->{RecordNumber} == 0 && $self->{PageSize} != 0 && $Page != 0 ) {
while ( $self->{RecordNumber} < ($Page - 1) * $self->{PageSize} && $self->next_record() && $Page <= $self->{RecordsCount}) {
$self->{RecordNumber}++;
}
}
}
sub PageCount {
my $self = shift;
if ( $self->{PageSize} ) {
my $initial = $self->{RecordsCount} / $self->{PageSize};
my $rounded = sprintf("%.0f", $self->{RecordsCount} / $self->{PageSize});
return $rounded < $initial ? $rounded + 1 : $rounded;
} else {
return 1
}
}
sub ToSQL {
my ($self, $Value, $ValueType) = @_;
if ( length($Value) ) {
if ( $ValueType == $ccsInteger || $ValueType == $ccsFloat ) {
return ( 0 + replace($Value, ",", ".") );
} elsif ( $ValueType == $ccsDate ) {
if (ref($Value) eq "ARRAY") {
$Value = CCFormatDate($Value, @{$self->{DateFormat}});
}
return $self->{dbh}->quote($Value);
} elsif ( $ValueType == $ccsBoolean ) {
$Value = CCFormatBoolean($Value, @{$self->{BooleanFormat}});
return $Value;
} else {
return $self->{dbh}->quote($Value);
}
} else {
return "NULL";
}
}
sub SQLValue {
my ($self, $Value, $ValueType) = @_;
if ( length($Value) ) {
if ( $ValueType == $ccsInteger || $ValueType == $ccsFloat ) {
return ( 0 + replace($Value, ",", ".") );
} elsif ( $ValueType == $ccsDate ) {
if (ref($Value) eq "ARRAY") {
$Value = CCFormatDate($Value, @{$self->{DateFormat}});
}
return $Value;
} elsif ( $ValueType == $ccsBoolean ) {
$Value = CCFormatBoolean($Value, @{$self->{BooleanFormat}});
return $Value;
} else {
$Value =~ s/'/''/g;
return $Value;
}
} else {
return "";
}
}
sub query {
my ($self, $sql) = @_;
return 0 if (!$sql);
$self->{sth} = undef;
$self->{sth} = $self->{dbh}->prepare( $sql );
if ($DBI::errstr) {
$self->{Errors}->addError($DBI::errstr);
return 0
}
$self->{sth}->execute();
if ($DBI::errstr) {
$self->{Errors}->addError($DBI::errstr);
return 0
}
}
sub next_record {
my $self = shift;
$self->{RecordHashRef} = $self->{sth}->fetchrow_hashref();
return $self->{RecordHashRef} ? 1 : 0;
}
sub f {
my ($self, $field_name) = @_;
if ($field_name =~ /^\d+$/) {
$field_name = $self->{sth}->{NAME}->[$field_name];
return $self->{RecordHashRef}->{$field_name};
} else {
$field_name = $self->{Uppercase} ? uc($field_name) : $field_name;
return $self->{RecordHashRef}->{$field_name};
}
}
sub n {
my ($self, $field_number) = @_;
return $self->f($self->{sth}->{NAME}->[$field_number]);
}
sub num_rows {
my ($self, $sql) = @_;
my $rec_count = 0;
if ($self->query($sql) != 0) {
$rec_count+=1 while ($self->{sth}->fetchrow_arrayref());
$self->{sth} = undef;
}
return $rec_count;
}
sub DESTROY {
my $self = shift;
undef $self->{sth};
$self->{dbh}->disconnect();
undef $self->{dbh};
}
# End class clsDBConnection
#clsErrors Class @0-347B837F
package clsErrors;
sub new {
my $self = {};
$self->{ErrorDelimiter} = "
";
$self->{ErrorsCount} = 0;
$self->{Errors} = [];
return bless $self;
}
sub addError {
my ($self, $Description) = @_;
if ($Description) {
$self->{Errors}->[$self->{ErrorsCount}] = $Description;
$self->{ErrorsCount}++;
}
}
sub AddErrors {
my ($self, $Errors) = @_;
for ( my $i = 0; $i < ( $#{$self->{Errors}} + 1 ); $i++) {
$self->addError($self->{Errors}->[$i])
}
}
sub Clear {
my $self = shift;
$self->{ErrorsCount} = 0;
@{$self->{Errors}} = ();
}
sub Count {
my $self = shift;
return $self->{ErrorsCount};
}
sub ToString {
my $self = shift;
if ( ( $#{$self->{Errors}} + 1 ) > 0) {
return join( $self->{ErrorDelimiter}, @{$self->{Errors}} ) . $self->{ErrorDelimiter};
} else {
return "";
}
}
Không có nhận xét nào:
Đăng nhận xét