A2Billing
来源:互联网 发布:多重网络怎么解决方法 编辑:程序博客网 时间:2024/05/18 02:35
#!/usr/bin/php -q
<?php
declare(ticks = 1);
if (function_exists('pcntl_signal'))
{
pcntl_signal(SIGHUP, SIG_IGN);
}
error_reporting(E_ALL ^ (E_NOTICE | E_WARNING));
include_once (dirname(__FILE__)."/libs_a2billing/db_php_lib/Class.Table.php");
include (dirname(__FILE__)."/libs_a2billing/Class.A2Billing.php");
include (dirname(__FILE__)."/libs_a2billing/Class.RateEngine.php");
include (dirname(__FILE__)."/libs_a2billing/phpagi_2_14/phpagi.php");
include (dirname(__FILE__)."/libs_a2billing/phpagi_2_14/phpagi-asmanager.php");
include (dirname(__FILE__)."/libs_a2billing/Misc.php");
include (dirname(__FILE__)."/outgoing.php");
$charge_callback=0;
$G_startime = time();
$agi_date = "Release : 22 January 2008";
$agi_version = "Asterisk2Billing - Version 1.3.2 (Yellowjacket)";
if ($argc > 1 && ($argv[1] == '--version' || $argv[1] == '-v'))
{
echo "A2Billing - Version $agi_version - $agi_date/n";
exit;
}
/********** CREATE THE AGI INSTANCE + ANSWER THE CALL **********/
$agi = new AGI();
if ($argc > 1 && is_numeric($argv[1]) && $argv[1] >= 0){
$idconfig = $argv[1];
}else{
$idconfig = 1;
}
if ($argc > 2 && strlen($argv[2]) > 0 && $argv[2] == 'menu') $mode = 'menu';
elseif ($argc > 2 && strlen($argv[2]) > 0 && $argv[2] == 'callback') $mode = 'callback';
elseif ($argc > 2 && strlen($argv[2]) > 0 && $argv[2] == 'voip-callback') $mode = 'voip-callback';
elseif ($argc > 2 && strlen($argv[2]) > 0 && $argv[2] == 'precallback') $mode = 'precallback';
elseif ($argc > 2 && strlen($argv[2]) > 0 && $argv[2] == 'balance') $mode = 'balance';
elseif ($argc > 2 && strlen($argv[2]) > 0 && $argv[2] == 'voucher') $mode = 'voucher';
elseif ($argc > 2 && strlen($argv[2]) > 0 && $argv[2] == 'halfvoucher') $mode = 'halfvoucher';
elseif ($argc > 2 && strlen($argv[2]) > 0 && $argv[2] == 'answervoucher') $mode = 'answervoucher';
elseif ($argc > 2 && strlen($argv[2]) > 0 && $argv[2] == 'answercall') $mode = 'answercall';
else $mode = 'standard';
// get the area code for the cid-callback & all-callback
if ($argc > 3 && strlen($argv[3]) > 0)
$publicphone = $argv[3];
else
$publicphone = false;
$A2B = new A2Billing();
$A2B -> load_conf($agi, NULL, 0, $idconfig);
$A2B -> mode = $mode;
//$A2B -> debug( VERBOSE | WRITELOG, $agi, __FILE__, __LINE__, "IDCONFIG : $idconfig");
//$A2B -> debug( VERBOSE | WRITELOG, $agi, __FILE__, __LINE__, "MODE : $mode");
define ("DB_TYPE", isset($A2B->config["database"]['dbtype'])?$A2B->config["database"]['dbtype']:null);
//-- Print header
//$A2B -> debug( VERBOSE , $agi, __FILE__, __LINE__, 'AGI Request:');
//$A2B -> debug( VERBOSE , $agi, __FILE__, __LINE__, print_r($agi->request, true));
/* GET THE AGI PARAMETER(参数) */
$A2B -> get_agi_request_parameter ($agi);
if (!$A2B -> DbConnect())
{
$agi-> stream_file('prepaid-final', '#');
exit;
}
$instance_table = new Table();
$A2B -> set_instance_table ($instance_table);
//GET CURRENCIES(货币) FROM DATABASE
$QUERY = "SELECT id,currency,name,value from cc_currencies order by id";
$result = $A2B -> instance_table -> SQLExec ($A2B->DBHandle, $QUERY);//
if (is_array($result))
{
$num_cur = count($result);
for ($i=0;$i<$num_cur;$i++)
{
$currencies_list[$result[$i][1]] = array (1 => $result[$i][2], 2 => $result[$i][3]);
}
}
$RateEngine = new RateEngine();
if($mode == 'menu') //辩证半直拨预约和半直拨充值
{
$agi->hangup();
if(substr($A2B->dnid,0,3)=='11#')
{
$A2B->dnid=substr($A2B->dnid,3); //半直拨充值
$mode='halfvoucher';
}
elseif(substr($A2B->dnid,0,3)=='12#')
{
$called=substr($A2B->dnid,3);
$called=str_replace('#','',$called); //半直拨预约
$mode='halfdid';
}
else
{
$agi->verbose("this is mode is error!");
$agi->hangup();
exit;
}
}
if($mode=='halfvoucher') //半直拨充值
{
$A2B->dnid=str_replace('#','',$A2B->dnid);
if(strstr($A2B->dnid,'*')==false)
{
//为本机充值 result_vouchernumber充值卡
$result_vouchernumber=$A2B->dnid;
if(substr($A2B->CallerID,0,1)==0)
{
$A2B->CallerID=substr($A2B->CallerID,1);
}
//result_voucherphone用户手机号码通过$A2B->CallerID获取得到
$result_voucherphone=$A2B->CallerID;
}
else
{
//为其它手机充值
$temp_dnid=explode('*',$A2B->dnid);
$result_vouchernumber=$temp_dnid[0]; //充值卡
$result_voucherphone=$temp_dnid[1]; //充值的手机号码
}
/*
*
*/
//判断是否手机号码
if(strlen($result_voucherphone)==11 && (ereg("^13[0-9][0-9]{4,8}$", $result_voucherphone)||ereg("^15[0-9][0-9]{4,8}$", $result_voucherphone)||ereg("^18[0-9][0-9]{4,8}$", $result_voucherphone)) )
{
//判断充值卡位数是否正确
if(isset($result_vouchernumber)&& strlen($result_vouchernumber)==$A2B -> config["global"]['len_voucher'])
{
$QUERY="select voucher,credit,tariff,agent,expireday from cc_voucher where expirationdate >= CURRENT_TIMESTAMP and activated='t' and voucher = '".$result_vouchernumber."'";
$result_voucher=$A2B -> instance_table -> SQLExec ($A2B->DBHandle, $QUERY);
$result_count = $A2B -> instance_table -> SQLExec ($A2B->DBHandle, $QUERY);
if(is_array($result_voucher))//充值卡通过初步验证
{
$expire_day=$result_voucher[0][4];
$halfvoucher='ok';
}
else
{
$agi->exec("playback","you_input_error");
$agi->hangup();
exit;
}
}
else
{
$agi->exec("playback","you_input_error");
$agi->hangup();
exit;
}
}
else
{
$agi->exec("playback","you_input_error");
$agi->hangup();
exit;
}
if($halfvoucher=='ok')
{
$QUERY= "SELECT username , tariff from cc_card where username='".$result_voucherphone."' ";//查看是不是新用户
$result_cardtariff=$A2B -> instance_table -> SQLExec ($A2B->DBHandle, $QUERY);
if(!is_array($result_cardtariff))//是新用户 那么就开始创建
{
$upass=substr($result_voucherphone,-6);
$QUERY="insert into cc_card (creationdate,enableexpire,username,useralias ,expiredays,userpass ,uipass,credit,tariff,activated,simultaccess,agent) VALUES(now(),'3','$result_voucherphone','$result_voucherphone','$expire_day','$upass','$upass','".$result_voucher[0][1]."','".$result_voucher[0][2]."','t','1',".$result_voucher[0][3].")";//插入新纪录
$result = $A2B ->instance_table -> SQLExec ($A2B->DBHandle, $QUERY);
$QUERY = "UPDATE cc_voucher SET activated='u', usedcardnumber='".$result_voucherphone."', usedate=now() WHERE voucher='".$result_vouchernumber."'";//修改卡的状态
$result = $A2B -> instance_table -> SQLExec ($A2B->DBHandle, $QUERY);
$QUERY= "SELECT id from cc_card where username='".$result_voucherphone."'";
$tmp=$A2B -> instance_table -> SQLExec ($A2B->DBHandle, $QUERY);
$context=$A2B->config['callback']['context_voip_callback'];
$QUERY="insert into cc_iax_buddies (id_cc_card,name,accountcode,callerid ,context,host,mailbox,port,secret,username) VALUES('".$tmp[0][0]."','".$result_voucherphone."','".$result_voucherphone."','".$result_voucherphone."','".$context."','dynamic','".$result_voucherphone."','0','".$upass."','".$result_voucherphone."')";
$result_sip = $A2B -> instance_table -> SQLExec ($A2B->DBHandle, $QUERY);
$QUERY="insert into cc_sip_buddies (id_cc_card,name,accountcode,callerid ,context,mailbox,port,secret,username) VALUES('".$tmp[0][0]."','".$result_voucherphone."','".$result_voucherphone."','".$result_voucherphone."','".$context."','".$result_voucherphone."','0','".$upass."','".$result_voucherphone."')";
$result_iax = $A2B -> instance_table -> SQLExec ($A2B->DBHandle, $QUERY);
$QUERY="select commender,commended,credit from cc_commend where status='0' and commended='".$result_voucherphone."'";
$result_commend = $A2B -> instance_table -> SQLExec ($A2B->DBHandle, $QUERY);
if(is_array($result_commend))//的确被人推荐过(推荐用户才可以注册)
{
$QUERY="update cc_card set credit = credit + ".$result_commend[0][2]." where username = '".$result_commend[0][0]."'";
$updete_credit = $A2B -> instance_table -> SQLExec ($A2B->DBHandle, $QUERY);
$QUERY="update cc_commend set status = 1 where commender = '".$result_commend[0][0]."' and commended= '".$result_voucherphone."'";
$updete_commend = $A2B -> instance_table -> SQLExec ($A2B->DBHandle, $QUERY);
}
$agi->exec("playback","voucher_success");
$agi->hangup();
}
else//不是新用户
{
if($result_cardtariff[0][1]==$result_voucher[0][2])
{
$QUERY="SELECT id,expiredays, UNIX_TIMESTAMP(creationdate),credit FROM cc_card WHERE UNIX_TIMESTAMP( creationdate ) +60 *60 *24 * expiredays < UNIX_TIMESTAMP( now( ) ) and username = '".$result_voucherphone."'";//查看用户是否过期
$result_expire = $A2B -> instance_table -> SQLExec ($A2B->DBHandle, $QUERY);
if(is_array($result_expire))// 已经过期
{
$QUERY = "insert into cc_expire_bak(card_id,expiredays,creationdate,credit) values('".$result_expire[0][0]."','".$result_expire[0][1]."','".date("Y-m-d H:d:s",$result_expire[0][2])."','".$result_expire[0][3]."')";//备份
$result = $A2B -> instance_table -> SQLExec ($A2B->DBHandle, $QUERY);
$QUERY = "UPDATE cc_voucher SET activated='u', usedcardnumber='".$result_voucherphone ."', usedate=now() WHERE voucher='".$result_vouchernumber."'";
$result = $A2B -> instance_table -> SQLExec ($A2B->DBHandle, $QUERY);
$QUERY = "UPDATE cc_card SET activated = 't',credit='".$result_voucher[0][1]."',creationdate=CURRENT_TIMESTAMP,expiredays= '$expire_day' WHERE username='".$result_voucherphone."'";
$result = $A2B-> instance_table -> SQLExec ($A2B->DBHandle, $QUERY);
}
else//没有过期
{
$QUERY = "UPDATE cc_voucher SET activated='u', usedcardnumber='".$result_voucherphone ."', usedate=now() WHERE voucher='".$result_vouchernumber."'";
$result = $A2B -> instance_table -> SQLExec ($A2B->DBHandle, $QUERY);
//$agi->verbose($QUERY);
$QUERY ="UPDATE cc_card SET creationdate=now() , activated = 't',credit=credit + '".$result_voucher[0][1]."',expiredays='$expire_day' WHERE username='".$result_voucherphone."'";
$result = $A2B -> instance_table -> SQLExec ($A2B->DBHandle, $QUERY);
}
$agi->exec("playback","voucher_success");
$agi->hangup();
}
else
{
//如果手机和充值卡的tariff不同
$agi->exec("playback","voucher_failed");
$agi->hangup();
}
}
}
}
if($mode=='answervoucher')//接通充值
{
$agi->answer();
if(substr($A2B->CallerID,0,1)==0)
{
$A2B->CallerID=substr($A2B->CallerID,1);
}
$called_party=$A2B->CallerID;
for($vocherphone=0;$vocherphone<=3;$vocherphone++)
{
if($vocherphone==3)
{
$agi->hangup();
break 2;
}
//请输入你要充值手机号码 并以井号结束 本机直接按井号键结束 返回请按11井
$res_dtmf = $agi->get_data('please_input_your_voucherphone',10000,12);
$result_voucherphone = $res_dtmf ["result"];
//本机
if(isset($result_voucherphone) && strlen($result_voucherphone)==11 && (ereg("^13[0-9][0-9]{4,8}$", $result_voucherphone)||ereg("^15[0-9][0-9]{4,8}$", $result_voucherphone)||ereg("^18[0-9][0-9]{4,8}$", $result_voucherphone)))
{
$the_username=1;
break;
}
elseif($res_dtmf ["data"]=="timeout")
{
continue;
}
//其它手机
elseif(strlen($result_voucherphone)==0 && (ereg("^13[0-9][0-9]{4,8}$", $called_party)||ereg("^15[0-9][0-9]{4,8}$", $called_party)||ereg("^18[0-9][0-9]{4,8}$", $called_party)) )
{
$result_voucherphone=$called_party;
$the_username=1;
break;
}
else
{
$agi->exec("playback","you_input_error");
}
}
if($the_username==1)
{
for($vocherpwd=0;$vocherpwd<=3;$vocherpwd++)
{
if($vocherpwd==3)
{
$agi->hangup();
break 2;
}
$res_dtmf = $agi->get_data('please_input_your_pwd',10000,11);//请输入你要充值的卡号 并以井号结束 返回请按*#键
$result_vouchernumber = $res_dtmf ["result"];//获取卡号
if(isset($result_vouchernumber)&& strlen($result_vouchernumber)==$A2B -> config["global"]['len_voucher'])//判断充值卡**如果正确
{
$QUERY="select voucher,credit,tariff,agent,expireday from cc_voucher where expirationdate >= CURRENT_TIMESTAMP and activated='t' and voucher = '".$result_vouchernumber."'";
$result_voucher=$A2B -> instance_table -> SQLExec ($A2B->DBHandle, $QUERY);
if(is_array($result_voucher))//充值卡通过初步验证
{
$expire_day=$result_voucher[0][4];
$the_voucher=1;
break;
}
else
{
$agi->exec("playback","you_input_error");
}
}
else
{
$agi->exec("playback","you_input_error");
}
}
}
if($the_voucher==1 && $the_username==1)//有卡$result_vouchernumber 有账号了$result_voucherphone
{
$QUERY= "SELECT username , tariff from cc_card where username='".$result_voucherphone."' ";//查看是不是新用户
$result_cardtariff=$A2B -> instance_table -> SQLExec ($A2B->DBHandle, $QUERY);
if(!is_array($result_cardtariff))//是新用户 那么就开始创建
{
$upass=substr($result_voucherphone,-6);
$QUERY="insert into cc_card (creationdate,enableexpire,username,useralias ,expiredays,userpass ,uipass,credit,tariff,activated,simultaccess,agent) VALUES(now(),'3','$result_voucherphone','$result_voucherphone','$expire_day','$upass','$upass','".$result_voucher[0][1]."','".$result_voucher[0][2]."','t','1',".$result_voucher[0][3].")";//插入新纪录
$result = $A2B ->instance_table -> SQLExec ($A2B->DBHandle, $QUERY);
$QUERY = "UPDATE cc_voucher SET activated='u', usedcardnumber='".$result_voucherphone."', usedate=now() WHERE voucher='".$result_vouchernumber."'";//修改卡的状态
$result = $A2B -> instance_table -> SQLExec ($A2B->DBHandle, $QUERY);
$QUERY= "SELECT id from cc_card where username='".$result_voucherphone."'";
$tmp=$A2B -> instance_table -> SQLExec ($A2B->DBHandle, $QUERY);
$context=$A2B->config['callback']['context_voip_callback'];
$QUERY="insert into cc_iax_buddies (id_cc_card,name,accountcode,callerid ,context,host,mailbox,port,secret,username) VALUES('".$tmp[0][0]."','".$result_voucherphone."','".$result_voucherphone."','".$result_voucherphone."','".$context."','dynamic','".$result_voucherphone."','0','".$upass."','".$result_voucherphone."')";
$result_sip = $A2B -> instance_table -> SQLExec ($A2B->DBHandle, $QUERY);
$QUERY="insert into cc_sip_buddies (id_cc_card,name,accountcode,callerid ,context,mailbox,port,secret,username) VALUES('".$tmp[0][0]."','".$result_voucherphone."','".$result_voucherphone."','".$result_voucherphone."','".$context."','".$result_voucherphone."','0','".$upass."','".$result_voucherphone."')";
$result_iax = $A2B -> instance_table -> SQLExec ($A2B->DBHandle, $QUERY);
$QUERY="select commender,commended,credit from cc_commend where status='0' and commended='".$result_voucherphone."'";
$result_commend = $A2B -> instance_table -> SQLExec ($A2B->DBHandle, $QUERY);
if(is_array($result_commend))//的确被人推荐过
{
$QUERY="update cc_card set credit = credit + ".$result_commend[0][2]." where username = '".$result_commend[0][0]."'";
$updete_credit = $A2B -> instance_table -> SQLExec ($A2B->DBHandle, $QUERY);
$QUERY="update cc_commend set status = 1 where commender = '".$result_commend[0][0]."' and commended= '".$result_voucherphone."'";
$updete_commend = $A2B -> instance_table -> SQLExec ($A2B->DBHandle, $QUERY);
}
$agi->exec("playback","voucher_success");
}
else//不是新用户
{
if($result_cardtariff[0][1]==$result_voucher[0][2])
{
$QUERY="SELECT id,expiredays, UNIX_TIMESTAMP(creationdate),credit FROM cc_card WHERE UNIX_TIMESTAMP( creationdate ) +60 *60 *24 * expiredays < UNIX_TIMESTAMP( now( ) ) and username = '".$result_voucherphone."'";//查看用户是否过期
$result_expire = $A2B -> instance_table -> SQLExec ($A2B->DBHandle, $QUERY);
if(is_array($result_expire))// 已经过期
{
$QUERY = "insert into cc_expire_bak(card_id,expiredays,creationdate,credit) values('".$result_expire[0][0]."','".$result_expire[0][1]."','".date("Y-m-d H:d:s",$result_expire[0][2])."','".$result_expire[0][3]."')";//备份
$result = $A2B -> instance_table -> SQLExec ($A2B->DBHandle, $QUERY);
$QUERY = "UPDATE cc_voucher SET activated='u', usedcardnumber='".$result_voucherphone ."', usedate=now() WHERE voucher='".$result_vouchernumber."'";
$result = $A2B -> instance_table -> SQLExec ($A2B->DBHandle, $QUERY);
$QUERY = "UPDATE cc_card SET activated = 't',credit='".$result_voucher[0][1]."',creationdate=CURRENT_TIMESTAMP,expiredays= '$expire_day' WHERE username='".$result_voucherphone."'";
$result = $A2B-> instance_table -> SQLExec ($A2B->DBHandle, $QUERY);
}
else//没有过期
{
$QUERY = "UPDATE cc_voucher SET activated='u', usedcardnumber='".$result_voucherphone ."', usedate=now() WHERE voucher='".$result_vouchernumber."'";
$result = $A2B -> instance_table -> SQLExec ($A2B->DBHandle, $QUERY);
//$agi->verbose($QUERY);
$QUERY ="UPDATE cc_card SET creationdate=now() , activated = 't',credit=credit + '".$result_voucher[0][1]."',expiredays='$expire_day' WHERE username='".$result_voucherphone."'";
$result = $A2B -> instance_table -> SQLExec ($A2B->DBHandle, $QUERY);
}
$agi->exec("playback","voucher_success");
}
else
{
//如果手机和充值卡的tariff不同
$agi->exec("playback","voucher_failed");
}
}
}
}
if($mode == 'precallback' || $mode=='halfdid' || $mode=='answercall') //依次为正常预约 半直拨预约 接通预约 因为这里很多代码重复 所以写到一起 通过不同的mode值辨别
{
if(substr($A2B->CallerID,0,1)==0)
{
$A2B->CallerID=substr($A2B->CallerID,1);
}
if(strlen($A2B->CallerID)>=11 && (ereg("^13[0-9][0-9]{4,8}$",$A2B->CallerID) || ereg("^15[0-9][0-9]{4,8}$",$A2B->CallerID) || ereg("^18[0-9][0-9]{4,8}$",$A2B->CallerID)) )//判断是否手机号码
{
$check_vip=$RateEngine->rate_check_vip($A2B,$A2B->CallerID,1); //找到黑名单,返回0 反之返回1
if($check_vip==0)
{
$agi->verbose("rate_check_vip===========".$A2B->CallerID."====so.... hangup!");
$agi->hangup();
exit;
}
$minutes= $A2B -> config["callback"]['minutes'];
$times = $A2B -> config["callback"]['times'];
if($minutes!=0 && $times!=0) //检测恶意用户
{
$t=time(); //获得当前时间
$t1=date("Y-m-d H:i:s",$t);
$t=$t-$minutes; //向前推多少时间
$t2=date("Y-m-d H:i:s",$t);
$cc_account=substr($A2B->CallerID,-11); //主叫的后11位
$QUERY="select * from cc_callback_spool where entry_time>'$t2' and entry_time<'$t1' and account like '%".$cc_account."'";
$agi->verbose($QUERY);
$result_count = $A2B -> instance_table -> SQLExec ($A2B->DBHandle, $QUERY);
$count=(int)count($result_count);
$agi->verbose($count);
if($count > $times )
{
$QUERY="insert into cc_vip(telphone,grade) values ('$A2B->CallerID','1')";
$result_count = $A2B -> instance_table -> SQLExec ($A2B->DBHandle, $QUERY);
exit;
}
}
$QUERY="select * from cc_card where username= '".$A2B->CallerID."'";
$temp_username = $A2B -> instance_table -> SQLExec ($A2B->DBHandle, $QUERY);
if(is_array($temp_username)) //是我们的用户 那么我们考虑是否报余额 提醒他充值 提醒他有效期
{
$QUERY="select username,tariff,credit,activated ,id from cc_card where username= '".$A2B->CallerID."'";
$result_username = $A2B -> instance_table -> SQLExec ($A2B->DBHandle, $QUERY);
if($result_username[0][2] <= $A2B -> config["callback"]['min_credit']) //余额小于多少提醒他充值
{
$prompt="your_credit_no_enough";
$agi-> stream_file($prompt, '#');
}
if ($A2B->agiconfig['say_balance_after_call']==1 && $result_username[0][3]=='t') //报余额
{
$A2B-> fct_say_balance ($agi, $result_username[0][2]);
}
$QUERY="SELECT creationdate, activated, FROM_UNIXTIME( UNIX_TIMESTAMP( creationdate ) +60 *60 *24 * (expiredays-5) ) ,FROM_UNIXTIME( UNIX_TIMESTAMP( creationdate ) +60 *60 *24 * expiredays ) FROM cc_card where username='$A2B->CallerID'";
$result_expiretime= $A2B->instance_table -> SQLExec ($A2B->DBHandle, $QUERY, 1);
if($result_expiretime[0][2]<=date("Y-m-d H:i:s") && $result_expiretime[0][3]>=date("Y-m-d H:i:s") && $result_expiretime[0][1] =='t' )
{
//你的有效期不足5天,请注意充值!
$agi-> stream_file("you_will_expire_voucher", '#');
}
$callfile=1;
$A2B ->tariff=$result_username[0][1];
}
else
{
//新用户 但是是手机用户,那么我们要考虑回铃!
$callfile=1;
$A2B ->tariff=$A2B->config['callback']['callback_tariff']; //回拨时候走的哪个tariff 请配置a2billing.conf
$agi->verbose("I am new user tariff========".$A2B->config['callback']['callback_tariff']);
}
}
else
{
//不是手机用户
$agi->hangup();
exit;
}
if($callfile==1) //准备工作就绪 我们开始写.call文件
{
if($mode=='halfdid')
{
if (strlen($called)==1 && is_numeric($called) && $called>=0)//快速拨号
{
$QUERY = "SELECT phone FROM cc_speeddial WHERE id_cc_card='".$result_username[0][4]."' AND speeddial='".$called."'";
$result = $A2B -> instance_table -> SQLExec ($A2B->DBHandle, $QUERY);
if( is_array($result))
{
$called= $result[0][0];
}
else
{
$agi->exec("playback","there_is_not_likeman|noanswer");
$agi->hangup();
$A2B->DbDisconnect();
exit;
}
}
else //查找缩位拨号
{
$QUERY = "SELECT destination FROM cc_superlinkman WHERE card_username='".$A2B->CallerID."' AND destination like '%".$called."'";
$result = $A2B -> instance_table -> SQLExec ($A2B->DBHandle, $QUERY);
if( is_array($result))
{
$called= $result[0][0];
}
else
{
$agi->exec("playback","there_is_not_likeman|noanswer");
$agi->hangup();
$A2B->DbDisconnect();
exit;
}
}
}
if($mode=='answercall')
{
$agi->answer();
for($answercall=0;$answercall<=3;$answercall++)
{
if($answercall==3)
{
$agi->hangup();
exit;
}
$result = $agi->get_data("please_input_numberphone", 10000, 15);//请输入被叫并以井号键结束
$result_dtmf = $result["result"];
if(is_numeric($result_dtmf) && strlen($result_dtmf)>5)
{
$called= $result_dtmf;
break;
}
elseif(strlen($result_dtmf)==1 && is_numeric($result_dtmf))
{
$QUERY = "SELECT phone FROM cc_speeddial WHERE id_cc_card='".$result_username[0][4]."' AND speeddial='".$result_dtmf."'";
$result = $A2B -> instance_table -> SQLExec ($A2B->DBHandle, $QUERY);
if( is_array($result))
{
$called= $result[0][0];
break;
}
else
{
$agi->exec("playback","there_is_not_likeman|noanswer");
}
}
elseif(strlen($result_dtmf)==4 && is_numeric($result_dtmf))
{
$QUERY = "SELECT destination FROM cc_superlinkman WHERE card_username='".$A2B->CallerID."' AND destination like '%".$result_dtmf."'";
$result = $A2B -> instance_table -> SQLExec ($A2B->DBHandle, $QUERY);
if( is_array($result))
{
$called= $result[0][0];
break;
}
else
{
$agi->exec("playback","there_is_not_likeman|noanswer");
}
}
else
{
$agi->exec("playback","you_input_error");
}
}
}
$A2B -> destination=$A2B->CallerID;
$called_party=$A2B->destination;
$check_hcode=$RateEngine->rate_check_dialprefix($A2B,$A2B->destination , $A2B->tariff); //找到H码表 将返回前插码 否则返回0
if($check_hcode!=0)
{
$A2B->destination=$check_hcode.$A2B->destination; //能找到H码
$agi->verbose("Hcode is ok".$check_hcode);
}
$h_called_party=$A2B->destination;
$agi->verbose("tariff========".$A2B->tariff);
$agi->verbose("destination========".$A2B->destination);
$agi->exec("playback","please_hangup_wait|noanswer");
$resfindrate = $RateEngine->rate_engine_findrates($A2B, $A2B ->destination, $A2B ->tariff,1); //1 代表回拨属性 本次是回拨 肯定要写上'1' 默认是外呼
if ($resfindrate!=0)
{
$res_all_calcultimeout = $RateEngine->rate_engine_all_calcultimeout($A2B, $A2B->credit);
$agi->verbose ("RES_ALL_CALCULTIMEOUT ::> $res_all_calcultimeout");
if ($res_all_calcultimeout)
{
// MAKE THE CALL
if ($RateEngine -> ratecard_obj[0][34]!='-1')
{
$usetrunk = 34;
$usetrunk_failover = 1;
$RateEngine -> usedtrunk = $RateEngine -> ratecard_obj[0][34];
} else {
$usetrunk = 29;
$RateEngine -> usedtrunk = $RateEngine -> ratecard_obj[0][29];
$usetrunk_failover = 0;
}
$prefix = $RateEngine -> ratecard_obj[0][$usetrunk+1];
$tech = $RateEngine -> ratecard_obj[0][$usetrunk+2];
$ipaddress = $RateEngine -> ratecard_obj[0][$usetrunk+3];
$removeprefix = $RateEngine -> ratecard_obj[0][$usetrunk+4];
$timeout = $RateEngine -> ratecard_obj[0]['timeout'];
$callbackrate = $RateEngine -> ratecard_obj[0]['callbackrate'];
$failover_trunk = $RateEngine -> ratecard_obj[0][40+$usetrunk_failover];
$addparameter = $RateEngine -> ratecard_obj[0][42+$usetrunk_failover];
$idtrunk=$RateEngine -> ratecard_obj[0][56];
$iddevice=$RateEngine -> ratecard_obj[0][57];
$destination = $A2B ->destination;
if (strncmp($destination, $removeprefix, strlen($removeprefix)) == 0) $destination= substr($destination, strlen($removeprefix));//?μ?o
$pos_dialingnumber = strpos($ipaddress, '%dialingnumber%' );
$ipaddress = str_replace("%cardnumber%", $A2B->cardnumber, $ipaddress);
$ipaddress = str_replace("%dialingnumber%", $prefix.$destination, $ipaddress);
if ($pos_dialingnumber !== false){
$dialstr = "$tech/$ipaddress".$dialparams;
}else{
if ($A2B->agiconfig['switchdialcommand'] == 1){
$dialstr = "$tech/$prefix$destination@$ipaddress".$dialparams;
}else{
$dialstr = "$tech/$ipaddress/$prefix$destination".$dialparams;
}
}
//ADDITIONAL PARAMETER %dialingnumber%, %cardnumber%
if (strlen($addparameter)>0){
$addparameter = str_replace("%cardnumber%", $A2B->cardnumber, $addparameter);
$addparameter = str_replace("%dialingnumber%", $prefix.$destination, $addparameter);
$dialstr .= $addparameter;
}
$channel= $dialstr;
$exten = $A2B -> config["callback"]['extension'];
if ($argc > 4 && strlen($argv[4]) > 0) $exten = $argv[4];
$context = $A2B -> config["callback"]['context_callback'];
//$context='zb-callback';
$id_server_group = $A2B -> config["callback"]['id_server_group'];
$priority = 1;
$timeout = $A2B -> config["callback"]['timeout']*1000;
$cidgroupid=$RateEngine -> ratecard_obj[0][44];//this is the callernumber group,this is find by cc_outbound_cid_group table
$agi->verbose("channel===".$channel);
if ($A2B->config["database"]['dbtype'] == "postgres"){
$QUERY = "SELECT cid FROM cc_outbound_cid_list WHERE activated = 1 AND outbound_cid_group = $cidgroupid ORDER BY RANDOM() LIMIT 1";
}
else
{
$QUERY = "SELECT cid FROM cc_outbound_cid_list WHERE activated = 1 AND outbound_cid_group = $cidgroupid ORDER BY RAND() LIMIT 1";
}
$cidresult = $A2B->instance_table -> SQLExec ($A2B -> DBHandle, $QUERY);
$outcid = 1;
if (is_array($cidresult) && count($cidresult)>0){
$outcid = $cidresult[0][0];
//$A2B -> CallerID = $outcid;
$callerid = $outcid;
//$agi -> set_callerid($outcid);
}
$application='';
//$callerid=$A2B->CallerID;
$account = $A2B -> accountcode=$A2B->CallerID;
$uniqueid = date("Y_M_D_H_i_s").MDP_NUMERIC(5).'-'.MDP_STRING(7);
$variable = "IDCONF=$idconfig|CALLED=".$called."|MODE=CID|IDTRUNK=$idtrunk|DESTINATION=$h_called_party|IDDEVICE=$iddevice|TARIFF=$A2B->tariff|LEG=$called_party|CBID=$uniqueid|account=";
foreach($callbackrate as $key => $value){
$variable .= '|'.strtoupper($key).'='.$value;
}
$status = 'PENDING';
$server_ip = 'localhost';
$num_attempt =1;
if (is_numeric($A2B -> config["callback"]['sec_wait_before_callback']) && $A2B -> config["callback"]['sec_wait_before_callback']>=1)
{
$sec_wait_before_callback = $A2B -> config["callback"]['sec_wait_before_callback'];
}else{
$sec_wait_before_callback = 5;
}
$agi->verbose("sec_wait_before_callback=".$sec_wait_before_callback);
if ($A2B->config["database"]['dbtype'] != "postgres"){
// MYSQL
$QUERY = " INSERT INTO cc_callback_spool (uniqueid, status, server_ip, num_attempt, channel, exten, context, priority, variable, id_server_group, callback_time, account, callerid, timeout ) VALUES ('$uniqueid', '$status', '$server_ip', '$num_attempt', '$channel', '$exten', '$context', '$priority', '$variable', '$id_server_group', ADDDATE( CURRENT_TIMESTAMP, INTERVAL $sec_wait_before_callback SECOND ), '$account', '$callerid', '$timeout')";
}else{
// POSTGRESQL
$QUERY = " INSERT INTO cc_callback_spool (uniqueid, status, server_ip, num_attempt, channel, exten, context, priority, variable, id_server_group, callback_time, account, callerid, timeout ) VALUES ('$uniqueid', '$status', '$server_ip', '$num_attempt', '$channel', '$exten', '$context', '$priority', '$variable', '$id_server_group', (CURRENT_TIMESTAMP + INTERVAL '$sec_wait_before_callback SECOND'), '$account', '$callerid', '$timeout')";
}
$res = $A2B -> DBHandle -> Execute($QUERY);
$A2B -> debug( WRITELOG, $agi, __FILE__, __LINE__, "[CALLBACK-ALL : INSERT CALLBACK REQUEST IN SPOOL : QUERY=$QUERY]");
$retrytime=20;
$variable="CBID=$uniqueid"; // add bu yu,set a lot of variables failure.
$time=null;
if (!is_null($time))
{
cw_outgoing(NULL,$channel,$callerid,$num_attempt,$retrytime,$timeout,$context,$exten,$priority,$variable,NULL,NULL,$sec_wait_before_callback);
}
else
{
cw_outgoing($time,$channel,$callerid,$num_attempt,$retrytime,$timeout,$context,$exten,$priority,$variable,NULL,NULL,$sec_wait_before_callback);
}
}
else
{
$error_msg = 'Error : You don t have enough credit to call you back !!!';
$A2B -> debug( WRITELOG, $agi, __FILE__, __LINE__, "[CALLBACK-CALLERID : CALLED=".$A2B ->destination." | $error_msg]");
}
}
else
{
$error_msg = 'Error : There is no route to call back your phonenumber !!!';
$A2B -> debug( WRITELOG, $agi, __FILE__, __LINE__, "[CALLBACK-CALLERID : CALLED=".$A2B ->destination." | $error_msg]");
}
}
}
if($mode=='callback') //外呼
{
$callback_uniqueid=$agi->get_variable("CBID",true);
$QUERY="select variable from cc_callback_spool where uniqueid= '".$callback_uniqueid."'";
$result = $A2B -> instance_table -> SQLExec ($A2B->DBHandle, $QUERY);
$agi->exec("Set ",array($result[0][0]));
$called_party = $agi->get_variable("LEG", true); //用户号码
$callback_leg = $agi->get_variable("CALLED", true); //被叫号码
$callback_tariff = $agi->get_variable("TARIFF", true); //回拨的品牌
$callback_mode = $agi->get_variable("MODE", true);
$callback_idtrunk=$agi->get_variable("IDTRUNK", true); //回拨的中继
$id_device = $agi->get_variable("IDDEVICE", true); //回拨的设备
$QUERY="update cc_device set inuse=inuse+1 where id_device='$id_device'";
$update_device=$A2B -> instance_table -> SQLExec ($A2B->DBHandle, $QUERY);
if ($callback_mode=='CID')
{
$charge_callback = 1;
$A2B->agiconfig['use_dnid'] = 0;
$A2B->agiconfig['number_try'] =1;
$A2B->CallerID = $called_party;
}elseif ($callback_mode=='ALL')
{
$A2B->agiconfig['use_dnid'] = 0;
$A2B->agiconfig['number_try'] =1;
$A2B->agiconfig['cid_enable'] =0;
}else
{
$charge_callback = 1;
// FOR THE WEB-CALLBACK
$A2B->agiconfig['number_try'] =1;
$A2B->agiconfig['use_dnid'] =1;
$A2B->agiconfig['say_balance_after_auth']=0;
$A2B->agiconfig['cid_enable'] =0;
$A2B->agiconfig['say_timetocall']=0;
}
for($retry=1;$retry<=3;$retry++)
{
$mode="callback";
$QUERY="select username,credit ,tariff , nbused ,id from cc_card where username = '".$called_party."'";
$result_user=$A2B->instance_table -> SQLExec ($A2B->DBHandle,$QUERY);
if(is_array($result_user))
{
$A2B->username=$result_user[0][0];
$A2B->credit=$result_user[0][1];
$A2B->tariff=$result_user[0][2];
$A2B->id_card=$result_user[0][4];
$cc_card_id=$A2B->id_card;
if($A2B->credit<= $A2B -> config["callback"]['min_credit']) //余额小于多少提醒他充值
{
$prompt="your_credit_no_enough";
$agi-> stream_file($prompt, '#');
}
$QUERY="SELECT id,expiredays, UNIX_TIMESTAMP(creationdate),credit FROM cc_card WHERE UNIX_TIMESTAMP( creationdate ) +60 *60 *24 * expiredays < UNIX_TIMESTAMP( now( ) ) and username = '".$called_party."'";
$result_expire = $A2B -> instance_table -> SQLExec ($A2B->DBHandle, $QUERY);
if(is_array($result_expire))//我们首先判断他是否过期
{
$agi-> stream_file( "your_card_expire", '#');
$mode='voucher';
}
$QUERY=" SELECT UNIX_TIMESTAMP( `firstusedate` ) , username FROM cc_card where username = '".$called_party."'";
$result_firstusedate = $A2B -> instance_table -> SQLExec ($A2B->DBHandle, $QUERY);
if($result_firstusedate[0][0]==0)
{
$A2B->nbused=0;
}
else
{
$A2B->nbused=1;
}
$A2B->callingcard_acct_start_inuse($agi,1);
$QUERY = "UPDATE cc_callback_spool SET agi_result='AGI PROCESSING' WHERE uniqueid='$callback_uniqueid'";
$res = $A2B -> DBHandle -> Execute($QUERY);
$A2B->accountcode=$A2B->username;
$charge_callback = 1;
$A2B -> callingcard_auto_setcallerid($agi);
$RateEngine->Reinit();
$A2B-> Reinit();
$orig_credit = $A2B -> credit;
$stat_channel = $agi->channel_status($A2B-> channel);
if(strlen($callback_leg)>1 && $retry==1)
{
$A2B->destination=$callback_leg;
}
else
{
$A2B->destination='';
}
$sum_trunk=0;
$ccounttrunk=array();
if ($A2B-> callingcard_ivr_authorize($agi, $RateEngine, $i)==1)
{
$id_device_inuse=$RateEngine -> ratecard_obj[0][57];
$QUERY="update cc_device set inuse=inuse+1 where id_device=$id_device_inuse";
$agi->verbose($QUERY);
$result_trunk = $A2B -> instance_table -> SQLExec ($A2B->DBHandle, $QUERY);
$agi->stream_file("callback_wait");
$result_callperf = $RateEngine->rate_engine_performcall ($agi, $A2B-> destination, $A2B);
$QUERY="update cc_device set inuse=inuse-1 where id_device=$id_device_inuse";
$agi->verbose($QUERY);
$result_trunk = $A2B -> instance_table -> SQLExec ($A2B->DBHandle, $QUERY);
if(($RateEngine->dialstatus == "CHANUNAVAIL") || ($RateEngine->dialstatus == "CONGESTION"))
{
for($nex_trunk=0;$nex_trunk<$sum_trunk-1;$nex_trunk++)
{
$resfindrate = $RateEngine->rate_engine_findrates_bak($A2B, $A2B ->destination, $A2B ->tariff,$ccounttrunk[$nex_trunk]);
$res_all_calcultimeout = $RateEngine->rate_engine_all_calcultimeout($A2B, $A2B->credit);
$A2B->timeout = $RateEngine-> ratecard_obj[0]['timeout'];
$minutes = intval($A2B->timeout / 60);
$seconds = $A2B->timeout % 60;
if (!($minutes>0) || !$res_all_calcultimeout)
{
$prompt="prepaid-no-enough-credit";
$agi-> stream_file($prompt, '#');
break;
}
if($resfindrate)
{
$id_device_inuse=$RateEngine -> ratecard_obj[0][57];
$QUERY="update cc_device set inuse=inuse+1 where id_device=$id_device_inuse";
$agi->verbose($QUERY);
$result_trunk = $A2B -> instance_table -> SQLExec ($A2B->DBHandle, $QUERY);
$result_callperf = $RateEngine->rate_engine_performcall ($agi, $A2B-> destination, $A2B);
$QUERY="update cc_device set inuse=inuse-1 where id_device=$id_device_inuse";
$agi->verbose($QUERY);
$result_trunk = $A2B -> instance_table -> SQLExec ($A2B->DBHandle, $QUERY);
if(($RateEngine->dialstatus == "CHANUNAVAIL") || ($RateEngine->dialstatus == "CONGESTION"))
{
continue;
}
else
{
break;
}
}
}
}
if (!$result_callperf)
{
$prompt="prepaid-dest-unreachable";
$agi-> stream_file($prompt, '#');
}
$RateEngine->rate_engine_updatesystem($A2B, $agi, $A2B->reallydestination);
$A2B->destination='';
$charge_callback = 1;
if ($A2B->agiconfig['say_balance_after_call']==1)
{
$A2B-> fct_say_balance ($agi, $A2B-> credit);
}
}
if ($A2B->set_inuse==1)
{
$A2B->callingcard_acct_start_inuse($agi,0);
}
if($A2B->destination=='11')
{
//自助管理按1注册充值,按2余额查询,按3查询预约号码,按4进入转绑,按5设置缩位拨号
for($manger=1;$manger<=3;$manger++)
{
if($manger==3)
{
$agi->hangup();
break 2;
}
$res_dtmf = $agi->get_data('please_input_your_manger',10000,1);
$res_manger= $res_dtmf ["result"];
if( $res_dtmf ["data"]=='timeout')
{
continue;
}
elseif($res_manger==1)
{
$mode='voucher';
break;
}
elseif($res_manger==2)
{
$mode='balance';
break;
}
elseif($res_manger==3)
{
$mode='commend';
break;
}
}
}
}
else
{
//新用户 到充值流程
$agi-> stream_file( "you_are_new_user", '#');
$mode='voucher';
}
if($mode=='voucher')
{
for($vocherphone=0;$vocherphone<=3;$vocherphone++)
{
if($vocherphone==3)
{
$agi->hangup();
break 2;
}
$res_dtmf = $agi->get_data('please_input_your_voucherphone',10000,12);//请输入你要充值手机号码 并以井号结束 本机直接按井号键结束 返回请按11井
$result_voucherphone = $res_dtmf ["result"];
if(isset($result_voucherphone) && strlen($result_voucherphone)==11 && (ereg("^13[0-9][0-9]{4,8}$", $result_voucherphone)||ereg("^15[0-9][0-9]{4,8}$", $result_voucherphone)||ereg("^18[0-9][0-9]{4,8}$", $result_voucherphone)))
{
$the_username=1;
break;
}
elseif($res_dtmf ["data"]=="timeout")
{
continue;
}
elseif(strlen($result_voucherphone)==0 && (ereg("^13[0-9][0-9]{4,8}$", $called_party)||ereg("^15[0-9][0-9]{4,8}$", $called_party)||ereg("^18[0-9][0-9]{4,8}$", $called_party)) )
{
$result_voucherphone=$called_party;
$the_username=1;
break;
}
else
{
$agi->exec("playback","you_input_error");
}
}
if($the_username==1)
{
for($vocherpwd=0;$vocherpwd<=3;$vocherpwd++)
{
if($vocherpwd==3)
{
$agi->hangup();
break 2;
}
$res_dtmf = $agi->get_data('please_input_your_pwd',10000,11);//请输入你要充值的卡号 并以井号结束 返回请按*#键
$result_vouchernumber = $res_dtmf ["result"];//获取卡号
if(isset($result_vouchernumber)&& strlen($result_vouchernumber)==$A2B -> config["global"]['len_voucher'])//判断充值卡如果正确
{
$QUERY="select voucher,credit,tariff,agent,expireday from cc_voucher where expirationdate >= CURRENT_TIMESTAMP and activated='t' and voucher = '".$result_vouchernumber."'";
$result_voucher=$A2B -> instance_table -> SQLExec ($A2B->DBHandle, $QUERY);
if(is_array($result_voucher))//充值卡通过初步验证
{
$expire_day=$result_voucher[0][4];
$the_voucher=1;
break;
}
else
{
$agi->exec("playback","you_input_error");
}
}
else
{
$agi->exec("playback","you_input_error");
}
}
}
if($the_voucher==1 && $the_username==1)//有卡$result_vouchernumber 有账号了$result_voucherphone
{
$QUERY= "SELECT username , tariff from cc_card where username='".$result_voucherphone."' ";//查看是不是新用户
$result_cardtariff=$A2B -> instance_table -> SQLExec ($A2B->DBHandle, $QUERY);
if(!is_array($result_cardtariff))//是新用户 那么就开始创建
{
$upass=substr($result_voucherphone,-6);
$QUERY="insert into cc_card (creationdate,enableexpire,username,useralias ,expiredays,userpass ,uipass,credit,tariff,activated,simultaccess,agent) VALUES(now(),'3','$result_voucherphone','$result_voucherphone','$expire_day','$upass','$upass','".$result_voucher[0][1]."','".$result_voucher[0][2]."','t','1',".$result_voucher[0][3].")";//插入新纪录
$result = $A2B ->instance_table -> SQLExec ($A2B->DBHandle, $QUERY);
$QUERY = "UPDATE cc_voucher SET activated='u', usedcardnumber='".$result_voucherphone."', usedate=now() WHERE voucher='".$result_vouchernumber."'";//修改卡的状态
$result = $A2B -> instance_table -> SQLExec ($A2B->DBHandle, $QUERY);
$QUERY= "SELECT id from cc_card where username='".$result_voucherphone."'";
$tmp=$A2B -> instance_table -> SQLExec ($A2B->DBHandle, $QUERY);
$context=$A2B->config['callback']['context_voip_callback'];
$QUERY="insert into cc_iax_buddies (id_cc_card,name,accountcode,callerid ,context,host,mailbox,port,secret,username) VALUES('".$tmp[0][0]."','".$result_voucherphone."','".$result_voucherphone."','".$result_voucherphone."','".$context."','dynamic','".$result_voucherphone."','0','".$upass."','".$result_voucherphone."')";
$result_sip = $A2B -> instance_table -> SQLExec ($A2B->DBHandle, $QUERY);
$QUERY="insert into cc_sip_buddies (id_cc_card,name,accountcode,callerid ,context,mailbox,port,secret,username) VALUES('".$tmp[0][0]."','".$result_voucherphone."','".$result_voucherphone."','".$result_voucherphone."','".$context."','".$result_voucherphone."','0','".$upass."','".$result_voucherphone."')";
$result_iax = $A2B -> instance_table -> SQLExec ($A2B->DBHandle, $QUERY);
$QUERY="select commender,commended,credit from cc_commend where status='0' and commended='".$result_voucherphone."'";
$result_commend = $A2B -> instance_table -> SQLExec ($A2B->DBHandle, $QUERY);
if(is_array($result_commend))//的确被人推荐过
{
$QUERY="update cc_card set credit = credit + ".$result_commend[0][2]." where username = '".$result_commend[0][0]."'";
$updete_credit = $A2B -> instance_table -> SQLExec ($A2B->DBHandle, $QUERY);
$QUERY="update cc_commend set status = 1 where commender = '".$result_commend[0][0]."' and commended= '".$result_voucherphone."'";
$updete_commend = $A2B -> instance_table -> SQLExec ($A2B->DBHandle, $QUERY);
}
$agi->exec("playback","voucher_success");
}
else//不是新用户
{
if($result_cardtariff[0][1]==$result_voucher[0][2])
{
$QUERY="SELECT id,expiredays, UNIX_TIMESTAMP(creationdate),credit FROM cc_card WHERE UNIX_TIMESTAMP( creationdate ) +60 *60 *24 * expiredays < UNIX_TIMESTAMP( now( ) ) and username = '".$result_voucherphone."'";//查看用户是否过期
$result_expire = $A2B -> instance_table -> SQLExec ($A2B->DBHandle, $QUERY);
if(is_array($result_expire))// 已经过期
{
$QUERY = "insert into cc_expire_bak(card_id,expiredays,creationdate,credit) values('".$result_expire[0][0]."','".$result_expire[0][1]."','".date("Y-m-d H:d:s",$result_expire[0][2])."','".$result_expire[0][3]."')";//备份
$result = $A2B -> instance_table -> SQLExec ($A2B->DBHandle, $QUERY);
$QUERY = "UPDATE cc_voucher SET activated='u', usedcardnumber='".$result_voucherphone ."', usedate=now() WHERE voucher='".$result_vouchernumber."'";
$result = $A2B -> instance_table -> SQLExec ($A2B->DBHandle, $QUERY);
$QUERY = "UPDATE cc_card SET activated = 't',credit='".$result_voucher[0][1]."',creationdate=CURRENT_TIMESTAMP,expiredays= '$expire_day' WHERE username='".$result_voucherphone."'";
$result = $A2B-> instance_table -> SQLExec ($A2B->DBHandle, $QUERY);
}
else//没有过期
{
$QUERY = "UPDATE cc_voucher SET activated='u', usedcardnumber='".$result_voucherphone ."', usedate=now() WHERE voucher='".$result_vouchernumber."'";
$result = $A2B -> instance_table -> SQLExec ($A2B->DBHandle, $QUERY);
//$agi->verbose($QUERY);
$QUERY ="UPDATE cc_card SET creationdate=now() , activated = 't',credit=credit + '".$result_voucher[0][1]."',expiredays='$expire_day' WHERE username='".$result_voucherphone."'";
$result = $A2B -> instance_table -> SQLExec ($A2B->DBHandle, $QUERY);
}
$agi->exec("playback","voucher_success");
}
else
{
//如果手机和充值卡的tariff不同
$agi->exec("playback","voucher_failed");
}
}
}
}
if($mode=='balance')
{
for($balance=1;$balance<=3;$balance++)
{
if($balance==3)
{
$agi->hangup();
break 2;;
}
$result_dtmf = $agi->get_data("select_balance_phone", 10000, 12);//请输入你要查询的手机号码 # 本机 11 返回
$result_balance = $result_dtmf ["result"];
if(strlen($result_balance)==11 && is_numeric($result_balance))
{
$QUERY="select credit from cc_card where username = '".$result_balance."'";
$result_credit = $A2B -> instance_table -> SQLExec ($A2B->DBHandle, $QUERY);
if(is_array($result_credit))
{
$A2B->fct_say_balance($agi , $result_credit[0][0]);
break;
}
else
{
//您的输入有误
$agi->exec("playback","you_input_error");
continue;
}
}
elseif($result_dtmf ["data"]=='timeout')
{
continue;
}
elseif(isset($result_balance) && strlen($result_balance)==0 ) //本机
{
$QUERY="select credit from cc_card where username='".$called_party."'";
$result_credit= $A2B -> instance_table -> SQLExec ($A2B->DBHandle, $QUERY);
$A2B->fct_say_balance($agi , $result_credit[0][0]);
break;
}
else
{
$agi->exec("playback","you_input_error");
}
}
}
if($mode=='queryphone')
{
$QUERY="select systemphone from cc_precallback";
$query_phone = $A2B -> instance_table -> SQLExec ($A2B->DBHandle, $QUERY);
for($queryphone=0;$queryphone<count($query_phone);$queryphone++)
{
$agi->exec("playback","the_query_phone_is");
$agi->say_digits($query_phone[$queryphone][0]);
}
}
if($mode=='commend')
{
for($commend=1;$commend<=3;$commend++)
{
if($commend==3)
{
$agi->hangup();
break 2;
}
$res_dtmf = $agi->get_data("input_your_commend", 20000, 12);
$mobile_phone=$res_dtmf["result"];
if((ereg("^13[0-9][0-9]{4,8}$",$mobile_phone) || ereg("^15[0-9][0-9]{4,8}$", $mobile_phone) ||ereg("^18[0-9][0-9]{4,8}$", $mobile_phone))&& strlen( $mobile_phone)==11)
{
$QUERY = "SELECT * from cc_card where username = "."'".$mobile_phone."'";
$result_commend=$A2B -> instance_table -> SQLExec ($A2B->DBHandle, $QUERY);
if(is_array($result_commend))
{
$agi->exec("playback","the_commend_failed");
continue;
}
else
{
$QUERY="select * from cc_commend where commended = '".$mobile_phone."'";
$temp_commend=$A2B -> instance_table -> SQLExec ($A2B->DBHandle, $QUERY);
//看看是否已经推荐
if(is_array($temp_commend))
{
$agi->exec("playback","your_input_phone_commended");
continue;
}
else
{
$commend_credit=$A2B -> config["callback"]['commend_credit'];
$QUERY="insert into cc_commend (commender ,commended ,creationdate,status,credit ) values ('$called_party','$mobile_phone',now(),'0',$commend_credit)";
$agi->verbose( $QUERY);
$result=$A2B -> instance_table -> SQLExec ($A2B->DBHandle, $QUERY);
$agi->exec("playback","commend_is_success");
break;
}
}
}
else
{
$agi->exec("playback","you_input_error");
continue;
}
}
}
if($mode=='speedlinkman')
{
for($speeddial=1;$speeddial<=3;$speeddial++)
{
if($speeddial==3)
{
$agi->hangup();
break 2;;
}
$result_dtmf = $agi->get_data("input_speed_linkman_phone", 10000, 15);//请输入要绑定的号码 按#结束
$result_phone = $result_dtmf ["result"];
if(isset($result_phone) && strlen($result_phone)>5 && strstr($result_phone,'*')==false)
{
$phone=$result_phone;
for($speedkey=1;$speedkey<=3;$speedkey++)
{
if($speeddial==3)
{
$agi->exec("playback","you_input_error");
break 2;
}
$result_dtmf = $agi->get_data("input_your_speeddial", 10000, 5);//请输入4位缩位拨号键 按#结束
$result_speeddial = $result_dtmf ["result"];
if(isset($result_speeddial) && strlen($result_speeddial)==4 && strstr($result_phone,'*')==false)
{
$QUERY="select * from cc_superlinkman where username ='$called_party' and destination=$result_phone and speedkey='$result_speeddial'";
$temp_linkman = $A2B -> instance_table -> SQLExec ($A2B->DBHandle, $QUERY);
if(is_array($temp_linkman))
{
$agi->exec("playback","the_linkman_is_exist");
}
else
{
$QUERY="insert into cc_cc_superlinkman(card_id,card_username,destination,speedkey) values ('$cc_card_id','$called_party','$result_phone','$result_speeddial')";
$A2B -> instance_table -> SQLExec ($A2B->DBHandle, $QUERY);
$agi->exec("playback","the_linkman_is_success");
break;
}
}
else
{
$agi->exec("playback","you_input_error");
}
}
}
else
{
$agi->exec("playback","you_input_error");
}
}
}
if($mode=='changbind')
{
for($changbind=1;$changbind<=3;$changbind++)
{
if($changbind==3)
{
$agi->hangup();
break;
}
$result_dtmf = $agi->get_data("input_bind_new_phone", 10000, 12);//请输入新的绑定号码 按#结束
$temp_phone = $result_dtmf ["result"];
$QUERY="select * from cc_card where username = '".$temp_phone."'";
$result_user=$A2B->instance_table -> SQLExec ($A2B->DBHandle,$QUERY);
if(!is_array($result_user) && isset($temp_phone) && strlen($temp_phone)==11 && (ereg("^13[0-9][0-9]{4,8}$", $temp_phone)||ereg("^15[0-9][0-9]{4,8}$", $temp_phone)||ereg("^18[0-9][0-9]{4,8}$", $temp_phone)))
{
$customer=$temp_phone;
$QUERY="UPDATE `cc_card` SET `username` = '$customer', `useralias` = '$customer' WHERE `username` ='$called_party' ";
$temp=$A2B->instance_table -> SQLExec ($A2B->DBHandle,$QUERY);
$QUERY="UPDATE `cc_iax_buddies` SET `name` = '$customer', `accountcode` = '$customer', `regexten` = '$customer', `callerid` = '$customer', `username` = '$customer' WHERE `name` ='$called_party'";
$temp=$A2B->instance_table -> SQLExec ($A2B->DBHandle,$QUERY);
$QUERY="UPDATE `cc_sip_buddies` SET `name` = '$customer',`accountcode` = '$customer',`callerid` = '$customer',`mailbox` = '$customer',`username` = '$customer' WHERE `name` ='$called_party'";
$temp=$A2B->instance_table -> SQLExec ($A2B->DBHandle,$QUERY);
$QUERY="UPDATE cc_speeddial set phone='$customer' where phone='$called_party'";
$temp=$A2B->instance_table -> SQLExec ($A2B->DBHandle,$QUERY);
$QUERY="UPDATE cc_superlinkman set card_username='$customer' where card_username='$called_party'";
$temp=$A2B->instance_table -> SQLExec ($A2B->DBHandle,$QUERY);
//记录修改绑定过程
$QUERY = "insert into cc_change_bind(change_username,old_username,new_username,mothod) values "."(".$called_party.",".$called_party.",".$customer.","."'".a2b."'".")";
$temp=$A2B->instance_table -> SQLExec ($A2B->DBHandle,$QUERY);
$agi->exec("playback","chang_bind_is_success");
$agi->hangup();
break 2;
}
else
{
$agi->exec("playback","you_input_error");
}
}
}
}
}
//A段计费模块!
if($mode!='precallback')
{
$QUERY="update cc_device set inuse=inuse-1 where id_device=$id_device";
$result_trunk = $A2B -> instance_table -> SQLExec ($A2B->DBHandle, $QUERY);
$A2B->username='10066';
$A2B -> tariff=$callback_tariff;
$A2B -> debug( VERBOSE | WRITELOG, $agi, __FILE__, __LINE__, "[CALLBACK 1ST LEG]:[MAKE BILLING FOR THE 1ST LEG - TARIFF:".$A2B -> tariff.";CALLED=$called_party]");
$A2B->agiconfig['use_dnid'] =1;
$A2B ->dnid = $A2B ->destination = $called_party;
$A2B->reallydestination=$A2B->destination;
$check_hcode=$RateEngine->rate_check_dialprefix($A2B,$A2B->destination , $A2B->tariff); //找到H码表 讲返回前插码 否则返回0
if($check_hcode!=0)
{
$A2B->destination=$check_hcode.$A2B->destination; //能找到H码
$agi->verbose("Hcode is ok".$check_hcode);
}
$resfindrate = $RateEngine->rate_engine_findrates_a($A2B, $A2B->destination,$A2B -> tariff,$callback_idtrunk);
$RateEngine-> usedratecard = 0;
// IF FIND RATE
if ($resfindrate!=0 && is_numeric($RateEngine->usedratecard)){
$res_all_calcultimeout = $RateEngine->rate_engine_all_calcultimeout($A2B, $A2B->credit);
if ($res_all_calcultimeout){
// SET CORRECTLY THE CALLTIME FOR THE 1st LEG
$RateEngine -> answeredtime = time() - $G_startime;
$RateEngine -> dialstatus = 'ANSWERED';
$A2B -> debug( VERBOSE | WRITELOG, $agi, __FILE__, __LINE__, "[CALLBACK]:[RateEngine -> answeredtime=".$RateEngine -> answeredtime."]");
// INSERT CDR & UPDATE SYSTEM
$RateEngine->rate_engine_updatesystem_a($A2B, $agi, $A2B->reallydestination, 1, 0, 1,$callback_idtrunk);
}else{
$A2B -> debug( VERBOSE | WRITELOG, $agi, __FILE__, __LINE__, "[CALLBACK 1ST LEG]:[ERROR - BILLING FOR THE 1ST LEG - rate_engine_all_calcultimeout: CALLED=$called_party]");
}
}else{
$A2B -> debug( VERBOSE | WRITELOG, $agi, __FILE__, __LINE__, "[CALLBACK 1ST LEG]:[ERROR - BILLING FOR THE 1ST LEG - rate_engine_findrates: CALLED=$called_party - RateEngine->usedratecard=".$RateEngine->usedratecard."]");
}
}
if ($A2B->set_inuse==1)
{
$A2B->callingcard_acct_start_inuse($agi,0);
}
$A2B->DbDisconnect();
/************** END OF THE APPLICATION ****************/
$A2B -> write_log("[exit]", 0);
?>
- A2Billing
- Asterisk + FreePBX + A2Billing汉化
- A2Billing 代码分析
- A2Billing源码分析笔记(一)
- Asterisk+freePbx+A2Billing搭建企业VOIP通讯平台
- Asterisk+freePbx+A2Billing搭建企业VOIP通讯平台
- 多数据库系统分析
- TCP/IP详解学习笔记(1)-基本概念
- Java在Web开发语言上败给了PHP
- HMM学习最佳范例五:前向算法1
- VS2005 增加命名空间
- A2Billing
- 转:协作的力量 Visual Studio Team System大局观
- ssh配置详解
- Visual Leak Detector
- 写书评,得《观止》,程序员自我修炼
- 图像术语知识
- 视频编码之YCbCr、YUV
- dbgrideh快速导出excel
- [CSS]详解display:inline | block |inline-block的区别