⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 a2billing.php

📁 a2billing的agi脚本,有一些中文的注释
💻 PHP
📖 第 1 页 / 共 3 页
字号:
#!/usr/bin/php -q
<?php   
/***************************************************************************
 *
 * a2billing.php : PHP A2Billing Core
 * Written for PHP 4.x & PHP 5.X versions.
 *
 * A2Billing -- Asterisk billing solution.
 * Copyright (C) 2004, 2007 Belaid Arezqui <areski _atl_ gmail com>
 *
 * See http://www.asterisk2billing.org for more information about
 * the A2Billing project. 
 * Please submit bug reports, patches, etc to <areski _atl_ gmail com>
 *
 * This software is released under the terms of the GNU Lesser General Public License v2.1
 * A copy of which is available from http://www.gnu.org/copyleft/lesser.html
 *
 ****************************************************************************/

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");

$charge_callback=0;
$G_startime = time();
$agi_date = "Release : July 2007";
$agi_version = "Asterisk2Billing - Version 1.3.0 (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] == 'did')			$mode = 'did';
elseif ($argc > 2 && strlen($argv[2]) > 0 && $argv[2] == 'callback')		$mode = 'callback';
elseif ($argc > 2 && strlen($argv[2]) > 0 && $argv[2] == 'cid-callback')	$mode = 'cid-callback';	
elseif ($argc > 2 && strlen($argv[2]) > 0 && $argv[2] == 'all-callback')	$mode = 'all-callback';
elseif ($argc > 2 && strlen($argv[2]) > 0 && $argv[2] == 'predictivedialer')	$mode = 'predictivedialer';
elseif ($argc > 2 && strlen($argv[2]) > 0 && $argv[2] == 'voucher')		$mode = 'voucher';
else $mode = 'standard';

// get the area code for the cid-callback & all-callback
if ($argc > 3 && strlen($argv[3]) > 0) $caller_areacode = $argv[3];


$A2B = new A2Billing();
$A2B -> load_conf($agi, NULL, 0, $idconfig);



$A2B -> debug( VERBOSE | WRITELOG, $agi, __FILE__, __LINE__, "IDCONFIG : $idconfig");
$A2B -> debug( VERBOSE | WRITELOG, $agi, __FILE__, __LINE__, "MODE : $mode");


$A2B -> CC_TESTING = isset($A2B->agiconfig['debugshell']) && $A2B->agiconfig['debugshell'];	
//$A2B -> CC_TESTING = true;

define ("DB_TYPE", isset($A2B->config["database"]['dbtype'])?$A2B->config["database"]['dbtype']:null); 	
	
// TEST DID
// if ($A2B -> CC_TESTING) $mode = 'did';

//-- 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);

//$A2B -> accountcode = '2222222222';

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);

/*
	$currencies_list['ADF'][1]="Andorran Franc";
	$currencies_list['ADF'][2]="0.1339";
	[ADF] => Array ( [1] => Andorran Franc (ADF), [2] => 0.1339 )
*/

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]);
	}
}

//if ($A2B -> CC_TESTING) $agi->verbose (print_r($currencies_list,true));

$RateEngine = new RateEngine();

if ($A2B -> CC_TESTING) { 
	$RateEngine->debug_st=1;
	$accountcode = '2222222222';
}

// ??? $A2B->callingcard_auto_setcallerid($agi); for other modes	
	
if ($mode == 'standard'){

	if ($A2B->agiconfig['answer_call']==1){
		$A2B -> debug( VERBOSE | WRITELOG, $agi, __FILE__, __LINE__, '[ANSWER CALL]');
		$agi->answer();
		$status_channel=6;
	}else{
		$A2B -> debug( VERBOSE | WRITELOG, $agi, __FILE__, __LINE__, '[NO ANSWER CALL]');
		$status_channel=4;
	}
	
	$A2B -> play_menulanguage ($agi);
	
	/*************************   PLAY INTRO MESSAGE   ************************/
	if (strlen($A2B->agiconfig['intro_prompt'])>0){
		$agi-> stream_file($A2B->agiconfig['intro_prompt'], '#');
	}
	
	/* WE START ;) */	
	$cia_res = $A2B -> callingcard_ivr_authenticate($agi);
	$A2B -> debug( WRITELOG, $agi, __FILE__, __LINE__, "[TRY : callingcard_ivr_authenticate]");
	// GOOD AUTHENTICATION AND WE HAVE CREDIT TO GO AHEAD
	if ($cia_res==0){
		
		// RE-SET THE CALLERID
		$A2B->callingcard_auto_setcallerid($agi);
		//$A2B->callingcard_acct_start_inuse($agi,1);
		for ($i=0;$i< $A2B->agiconfig['number_try'] ;$i++){
			
			$RateEngine->Reinit();
			$A2B-> Reinit();
			
			// RETRIEVE THE CHANNEL STATUS AND LOG : STATUS - CREIT - MIN_CREDIT_2CALL 
			$stat_channel = $agi->channel_status($A2B-> channel);
			$A2B -> debug( VERBOSE | WRITELOG, $agi, __FILE__, __LINE__, '[CHANNEL STATUS : '.$stat_channel["result"].' = '.$stat_channel["data"].']');
			$A2B -> debug( VERBOSE | WRITELOG, $agi, __FILE__, __LINE__, "[CREDIT : ".$A2B-> credit."][CREDIT MIN_CREDIT_2CALL : ".$A2B->agiconfig['min_credit_2call']."]");
			
			// CHECK IF THE CHANNEL IS UP
			//if ($stat_channel["status"]!= "6" && $stat_channel["status"]!= "1"){	
			if (($A2B->agiconfig['answer_call']==1) && ($stat_channel["result"]!=$status_channel) && ($A2B -> CC_TESTING!=1)){
				if ($A2B->set_inuse==1) $A2B->callingcard_acct_start_inuse($agi,0);
				$A2B -> write_log("[STOP - EXIT]", 0);
				exit();
			}
			
			// CREATE A DIFFERENT UNIQUEID FOR EACH TRY
			if ($i>0){
				$A2B-> uniqueid=$A2B-> uniqueid+ 1000000000 ;
			}
			
			
			if( $A2B->credit < $A2B->agiconfig['min_credit_2call'] && $A2B -> typepaid==0 && $A2B->agiconfig['jump_voucher_if_min_credit']==1) {
				
				$A2B -> debug( WRITELOG, $agi, __FILE__, __LINE__, "[NOTENOUGHCREDIT - Refill with vouchert]");
				$vou_res = $A2B -> refill_card_with_voucher($agi,2);
				if ($vou_res==1){
					$A2B -> debug( WRITELOG, $agi, __FILE__, __LINE__, "[ADDED CREDIT - refill_card_withvoucher Success] ");
				} else {
					$A2B -> debug( WRITELOG, $agi, __FILE__, __LINE__, "[NOTENOUGHCREDIT - refill_card_withvoucher fail] ");
				}
			}
			
			if( $A2B->credit < $A2B->agiconfig['min_credit_2call'] && $A2B -> typepaid==0) {
				
				// SAY TO THE CALLER THAT IT DEOSNT HAVE ENOUGH CREDIT TO MAKE A CALL							
				$prompt = "prepaid-no-enough-credit-stop";
				$agi-> stream_file($prompt, '#');
				$A2B -> debug( VERBOSE | WRITELOG, $agi, __FILE__, __LINE__, "[STOP STREAM FILE $prompt]");
				
				
				if (($A2B->agiconfig['notenoughcredit_cardnumber']==1) && (($i+1)< $A2B->agiconfig['number_try'])){
					
					if ($A2B->set_inuse==1) $A2B->callingcard_acct_start_inuse($agi,0);
					
					$A2B->agiconfig['cid_enable']=0;
					$A2B->agiconfig['use_dnid']=0;
//					$A2B->agiconfig['say_balance_after_auth']=0;
					$A2B->agiconfig['cid_auto_assign_card_to_cid']=0;
					$A2B->accountcode='';
					$A2B->username='';
					$A2B-> ask_other_cardnumber	= 1;
					
					$cia_res = $A2B -> callingcard_ivr_authenticate($agi);
					$A2B -> debug( WRITELOG, $agi, __FILE__, __LINE__, "[NOTENOUGHCREDIT_CARDNUMBER - TRY : callingcard_ivr_authenticate]");
					if ($cia_res!=0) break;
					
					$A2B -> debug( WRITELOG, $agi, __FILE__, __LINE__, "[NOTENOUGHCREDIT_CARDNUMBER - callingcard_acct_start_inuse]");
					$A2B->callingcard_acct_start_inuse($agi,1);
					continue;
					
				}else{
					
					$send_reminder = 1;
					$A2B -> debug( VERBOSE | WRITELOG, $agi, __FILE__, __LINE__, "[SET MAIL REMINDER - NOT ENOUGH CREDIT]");
					break;
				}
			}
			
			if ($agi->request['agi_extension']=='s'){
				$A2B->dnid = $agi->request['agi_dnid'];
			}else{
				$A2B->dnid = $agi->request['agi_extension'];
			}
			
			if ($A2B->agiconfig['ivr_voucher']==1){
				$res_dtmf = $agi->get_data('prepaid-refill_card_with_voucher', 5000, 1);
				$A2B -> debug( VERBOSE | WRITELOG, $agi, __FILE__, __LINE__, "RES REFILL CARD VOUCHER DTMF : ".$res_dtmf ["result"]);
				$A2B-> ivr_voucher = $res_dtmf ["result"];
				if ((isset($A2B-> ivr_voucher)) && ($A2B-> ivr_voucher == $A2B->agiconfig['ivr_voucher_prefixe']))
				{	
					$vou_res = $A2B->refill_card_with_voucher($agi, $i);
				//	if ($vou_res==1)$A2B->fct_say_balance ($agi, $A2B->add_credit, 1);
				}
			}

			if ($A2B->agiconfig['sip_iax_friends']==1){
			
				if ($A2B->agiconfig['sip_iax_pstn_direct_call']==1){	

					if ($A2B->agiconfig['use_dnid']==1 && !in_array ($A2B->dnid, $A2B->agiconfig['no_auth_dnid']) && strlen($A2B->dnid)>2 && $i==0 ){
			
						$A2B -> destination = $A2B->dnid;
						
					}else{
	
						$prompt_enter_dest = $A2B->agiconfig['file_conf_enter_destination'];
						$res_dtmf = $agi->get_data($prompt_enter_dest, 4000, 20);
						$A2B -> debug( VERBOSE | WRITELOG, $agi, __FILE__, __LINE__, "RES sip_iax_pstndirect_call DTMF : ".$res_dtmf ["result"]);
						$A2B-> destination = $res_dtmf ["result"];
					}
					
					if ( (strlen($A2B-> destination)>0) && (strlen($A2B->agiconfig['sip_iax_pstn_direct_call_prefix'])>0) && (strncmp($A2B->agiconfig['sip_iax_pstn_direct_call_prefix'], $A2B-> destination,strlen($A2B->agiconfig['sip_iax_pstn_direct_call_prefix']))==0) ){
						$A2B-> dnid = $A2B-> destination;
						$A2B-> sip_iax_buddy = $A2B->agiconfig['sip_iax_pstn_direct_call_prefix'];
						$A2B-> agiconfig['use_dnid'] = 1;
						$A2B -> debug( VERBOSE | WRITELOG, $agi, __FILE__, __LINE__, "SIP 1. IAX - dnid : ".$A2B->dnid." - ".strlen($A2B->agiconfig['sip_iax_pstn_direct_call_prefix']));
						$A2B->dnid = substr($A2B->dnid,strlen($A2B->agiconfig['sip_iax_pstn_direct_call_prefix']));
						$A2B -> debug( VERBOSE | WRITELOG, $agi, __FILE__, __LINE__, "SIP 2. IAX - dnid : ".$A2B->dnid);
					}elseif (strlen($A2B->destination)>0){
						$A2B->dnid = $A2B->destination;
						$A2B->agiconfig['use_dnid'] = 1;
						$A2B -> debug( VERBOSE | WRITELOG, $agi, __FILE__, __LINE__, "TRUNK - dnid : ".$A2B->dnid." (".$A2B->agiconfig['use_dnid'].")");
					}
				}else{
			
					//$res_dtmf = $agi->agi_exec("GET DATA prepaid-sipiax-press9 2000 1");
					$res_dtmf = $agi->get_data('prepaid-sipiax-press9', 2000, 1);
					$A2B -> debug( VERBOSE | WRITELOG, $agi, __FILE__, __LINE__, "RES SIP_IAX_FRIEND DTMF : ".$res_dtmf ["result"]);
					
					$A2B-> sip_iax_buddy = $res_dtmf ["result"];
				}
			}
			
			
			if ( isset($A2B-> sip_iax_buddy) && ($A2B-> sip_iax_buddy == $A2B->agiconfig['sip_iax_pstn_direct_call_prefix'])) {
				
				$A2B -> debug( VERBOSE | WRITELOG, $agi, __FILE__, __LINE__, 'CALL SIP_IAX_BUDDY');
				$cia_res = $A2B-> call_sip_iax_buddy($agi, $RateEngine,$i);
				
			}else{
				if ($A2B-> callingcard_ivr_authorize($agi, $RateEngine, $i)==1){
					// PERFORM THE CALL	
					$result_callperf = $RateEngine->rate_engine_performcall ($agi, $A2B-> destination, $A2B);
					
					if (!$result_callperf) {
						$prompt="prepaid-dest-unreachable";
						//$agi->agi_exec("STREAM FILE $prompt #");
						$agi-> stream_file($prompt, '#');
					}
					// INSERT CDR  & UPDATE SYSTEM
					$RateEngine->rate_engine_updatesystem($A2B, $agi, $A2B-> destination);
					
					if ($A2B->agiconfig['say_balance_after_call']==1){		
						$A2B-> fct_say_balance ($agi, $A2B-> credit);
					}
					$A2B -> debug( VERBOSE | WRITELOG, $agi, __FILE__, __LINE__, '[a2billing account stop]');
				}
			}
			$A2B->agiconfig['use_dnid']=0;
		}//END FOR
		
	}else{
		$A2B -> debug( VERBOSE | WRITELOG, $agi, __FILE__, __LINE__, "[AUTHENTICATION FAILED (cia_res:".$cia_res.")]");
	}
	
	/****************  SAY GOODBYE   ***************/
	if ($A2B->agiconfig['say_goodbye']==1) $agi-> stream_file('prepaid-final', '#');


// MODE DID
}elseif ($mode == 'did'){
	
	
	if ($A2B->agiconfig['answer_call']==1){
		$A2B -> debug( VERBOSE | WRITELOG, $agi, __FILE__, __LINE__, '[ANSWER CALL]');
		$agi->answer();
	}else{
		$A2B -> debug( VERBOSE | WRITELOG, $agi, __FILE__, __LINE__, '[NO ANSWER CALL]');
	}
	// TODO
	// CRONT TO CHARGE MONTLY
	
	$RateEngine -> Reinit();
	$A2B -> Reinit();
	
	$mydnid = $agi->request['agi_extension'];
	if ($A2B -> CC_TESTING) $mydnid = '11111111';
	
	if (strlen($mydnid) > 0){
		$A2B -> debug( VERBOSE | WRITELOG, $agi, __FILE__, __LINE__, "[DID CALL - [CallerID=".$A2B->CallerID."]:[DID=".$mydnid."]");
		
		$QUERY =  "SELECT cc_did.id, cc_did_destination.id, billingtype, tariff, destination,  voip_call, username".
			" FROM cc_did, cc_did_destination,  cc_card ".
			" WHERE id_cc_did=cc_did.id and cc_card.id=id_cc_card and cc_did_destination.activated=1  and cc_did.activated=1 and did='$mydnid' ".
			" AND cc_did.startingdate<= CURRENT_TIMESTAMP AND (cc_did.expirationdate > CURRENT_TIMESTAMP OR cc_did.expirationdate IS NULL OR ".
			" cc_did.expirationdate = '0000-00-00 00:00:00') ORDER BY priority ASC";
		
		$A2B -> debug( VERBOSE | WRITELOG, $agi, __FILE__, __LINE__, $QUERY);
		$result = $A2B -> instance_table -> SQLExec ($A2B->DBHandle, $QUERY);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -