2012-06-04 16:58:40 +02:00
< ? php
/*
FusionPBX
Version : MPL 1.1
The contents of this file are subject to the Mozilla Public License Version
1.1 ( the " License " ); you may not use this file except in compliance with
the License . You may obtain a copy of the License at
http :// www . mozilla . org / MPL /
Software distributed under the License is distributed on an " AS IS " basis ,
WITHOUT WARRANTY OF ANY KIND , either express or implied . See the License
for the specific language governing rights and limitations under the
License .
The Original Code is FusionPBX
The Initial Developer of the Original Code is
Mark J Crane < markjcrane @ fusionpbx . com >
2015-02-12 17:42:58 +01:00
Portions created by the Initial Developer are Copyright ( C ) 2008 - 2015
2012-06-04 16:58:40 +02:00
the Initial Developer . All Rights Reserved .
Contributor ( s ) :
Mark J Crane < markjcrane @ fusionpbx . com >
2014-06-22 00:08:11 +02:00
Luis Daniel Lucio Quiroz < dlucio @ okay . com . mx >
2012-06-04 16:58:40 +02:00
*/
2014-06-22 00:08:11 +02:00
//check the permission
2012-06-04 16:58:40 +02:00
if ( defined ( 'STDIN' )) {
$document_root = str_replace ( " \\ " , " / " , $_SERVER [ " PHP_SELF " ]);
2013-04-30 12:05:34 +02:00
preg_match ( " /^(.*) \ /app \ /.* $ / " , $document_root , $matches );
2012-06-04 16:58:40 +02:00
$document_root = $matches [ 1 ];
set_include_path ( $document_root );
$_SERVER [ " DOCUMENT_ROOT " ] = $document_root ;
2013-07-06 08:03:27 +02:00
require_once " resources/require.php " ;
2012-06-04 16:58:40 +02:00
$display_type = 'text' ; //html, text
}
else {
include " root.php " ;
2013-07-06 08:03:27 +02:00
require_once " resources/require.php " ;
2015-03-06 18:15:09 +01:00
require_once " resources/pdo.php " ;
2012-06-04 16:58:40 +02:00
}
//set debug
2015-03-07 11:59:00 +01:00
$debug = false ; //true //false
2012-06-04 16:58:40 +02:00
if ( $debug ){
$time5 = microtime ( true );
$insert_time = $insert_count = 0 ;
}
2015-07-02 17:08:48 +02:00
function xml_cdr_log ( $msg ) {
global $debug ;
if ( ! $debug ) {
return ;
}
$fp = fopen ( $_SESSION [ 'server' ][ 'temp' ][ 'dir' ] . '/xml_cdr.log' , 'a+' );
if ( ! $fp ) {
return ;
}
fwrite ( $fp , $msg );
fclose ( $fp );
}
2012-06-04 16:58:40 +02:00
//increase limits
set_time_limit ( 3600 );
ini_set ( 'memory_limit' , '256M' );
2014-06-22 00:08:11 +02:00
ini_set ( " precision " , 6 );
2012-06-04 16:58:40 +02:00
//set pdo attribute that enables exception handling
$db -> setAttribute ( PDO :: ATTR_ERRMODE , PDO :: ERRMODE_EXCEPTION );
2015-02-12 17:42:58 +01:00
//add rating functions if the billing is installed
2016-01-17 08:01:13 +01:00
if ( file_exists ( $_SERVER [ " PROJECT_ROOT " ] . " /app/billing/app_config.php " )){
2014-07-18 01:46:04 +02:00
require_once " app/billing/resources/functions/rating.php " ;
2014-06-26 01:58:47 +02:00
}
2015-02-12 17:42:58 +01:00
2013-04-30 17:50:46 +02:00
//define the process_xml_cdr function
function process_xml_cdr ( $db , $leg , $xml_string ) {
//set global variable
global $debug ;
2012-06-04 16:58:40 +02:00
2015-07-02 17:08:48 +02:00
//fix the xml by escaping the contents of <sip_full_XXX>
2016-03-11 12:54:18 +01:00
$xml_string = preg_replace_callback ( " /<([^><]+)>(.*?[><].*?)< \ / \ g1>/ " ,
2015-07-02 17:08:48 +02:00
function ( $matches ) {
return '<' . $matches [ 1 ] . '>' .
2016-03-11 12:54:18 +01:00
str_replace ( " > " , " > " ,
2015-07-02 17:08:48 +02:00
str_replace ( " < " , " < " , $matches [ 2 ])
) .
'</' . $matches [ 1 ] . '>' ;
},
$xml_string
);
2015-05-15 11:26:13 +02:00
2013-04-30 17:50:46 +02:00
//parse the xml to get the call detail record info
try {
2015-07-02 17:08:48 +02:00
xml_cdr_log ( $xml_string );
2013-04-30 17:50:46 +02:00
$xml = simplexml_load_string ( $xml_string );
2015-07-02 17:08:48 +02:00
xml_cdr_log ( " \n xml load done \n " );
2013-04-30 17:50:46 +02:00
}
catch ( Exception $e ) {
echo $e -> getMessage ();
2015-07-02 17:08:48 +02:00
xml_cdr_log ( " \n fail loadxml: " . $e -> getMessage () . " \n " );
2013-04-30 17:50:46 +02:00
}
2012-06-04 16:58:40 +02:00
2013-04-30 17:50:46 +02:00
//prepare the database object
2013-07-06 09:13:36 +02:00
require_once " resources/classes/database.php " ;
2013-04-30 17:50:46 +02:00
$database = new database ;
$database -> table = " v_xml_cdr " ;
2014-06-14 06:53:04 +02:00
//misc
$uuid = check_str ( urldecode ( $xml -> variables -> uuid ));
$database -> fields [ 'uuid' ] = $uuid ;
$database -> fields [ 'accountcode' ] = check_str ( urldecode ( $xml -> variables -> accountcode ));
$database -> fields [ 'default_language' ] = check_str ( urldecode ( $xml -> variables -> default_language ));
$database -> fields [ 'bridge_uuid' ] = check_str ( urldecode ( $xml -> variables -> bridge_uuid ));
//$database->fields['digits_dialed'] = check_str(urldecode($xml->variables->digits_dialed));
$database -> fields [ 'sip_hangup_disposition' ] = check_str ( urldecode ( $xml -> variables -> sip_hangup_disposition ));
$database -> fields [ 'pin_number' ] = check_str ( urldecode ( $xml -> variables -> pin_number ));
//time
$database -> fields [ 'start_epoch' ] = check_str ( urldecode ( $xml -> variables -> start_epoch ));
$start_stamp = check_str ( urldecode ( $xml -> variables -> start_stamp ));
$database -> fields [ 'start_stamp' ] = $start_stamp ;
$database -> fields [ 'answer_stamp' ] = check_str ( urldecode ( $xml -> variables -> answer_stamp ));
$database -> fields [ 'answer_epoch' ] = check_str ( urldecode ( $xml -> variables -> answer_epoch ));
$database -> fields [ 'end_epoch' ] = check_str ( urldecode ( $xml -> variables -> end_epoch ));
$database -> fields [ 'end_stamp' ] = check_str ( urldecode ( $xml -> variables -> end_stamp ));
$database -> fields [ 'duration' ] = check_str ( urldecode ( $xml -> variables -> duration ));
$database -> fields [ 'mduration' ] = check_str ( urldecode ( $xml -> variables -> mduration ));
$database -> fields [ 'billsec' ] = check_str ( urldecode ( $xml -> variables -> billsec ));
$database -> fields [ 'billmsec' ] = check_str ( urldecode ( $xml -> variables -> billmsec ));
//codecs
$database -> fields [ 'read_codec' ] = check_str ( urldecode ( $xml -> variables -> read_codec ));
$database -> fields [ 'read_rate' ] = check_str ( urldecode ( $xml -> variables -> read_rate ));
$database -> fields [ 'write_codec' ] = check_str ( urldecode ( $xml -> variables -> write_codec ));
$database -> fields [ 'write_rate' ] = check_str ( urldecode ( $xml -> variables -> write_rate ));
$database -> fields [ 'remote_media_ip' ] = check_str ( urldecode ( $xml -> variables -> remote_media_ip ));
$database -> fields [ 'hangup_cause' ] = check_str ( urldecode ( $xml -> variables -> hangup_cause ));
$database -> fields [ 'hangup_cause_q850' ] = check_str ( urldecode ( $xml -> variables -> hangup_cause_q850 ));
//call center
$database -> fields [ 'cc_side' ] = check_str ( urldecode ( $xml -> variables -> cc_side ));
$database -> fields [ 'cc_member_uuid' ] = check_str ( urldecode ( $xml -> variables -> cc_member_uuid ));
$database -> fields [ 'cc_queue_joined_epoch' ] = check_str ( urldecode ( $xml -> variables -> cc_queue_joined_epoch ));
$database -> fields [ 'cc_queue' ] = check_str ( urldecode ( $xml -> variables -> cc_queue ));
$database -> fields [ 'cc_member_session_uuid' ] = check_str ( urldecode ( $xml -> variables -> cc_member_session_uuid ));
$database -> fields [ 'cc_agent' ] = check_str ( urldecode ( $xml -> variables -> cc_agent ));
$database -> fields [ 'cc_agent_type' ] = check_str ( urldecode ( $xml -> variables -> cc_agent_type ));
$database -> fields [ 'waitsec' ] = check_str ( urldecode ( $xml -> variables -> waitsec ));
//app info
$database -> fields [ 'last_app' ] = check_str ( urldecode ( $xml -> variables -> last_app ));
$database -> fields [ 'last_arg' ] = check_str ( urldecode ( $xml -> variables -> last_arg ));
//conference
$database -> fields [ 'conference_name' ] = check_str ( urldecode ( $xml -> variables -> conference_name ));
$database -> fields [ 'conference_uuid' ] = check_str ( urldecode ( $xml -> variables -> conference_uuid ));
$database -> fields [ 'conference_member_id' ] = check_str ( urldecode ( $xml -> variables -> conference_member_id ));
//call quality
$rtp_audio_in_mos = check_str ( urldecode ( $xml -> variables -> rtp_audio_in_mos ));
if ( strlen ( $rtp_audio_in_mos ) > 0 ) {
$database -> fields [ 'rtp_audio_in_mos' ] = $rtp_audio_in_mos ;
}
2013-04-30 17:50:46 +02:00
//get the values from the callflow.
$x = 0 ;
foreach ( $xml -> callflow as $row ) {
if ( $x == 0 ) {
$context = check_str ( urldecode ( $row -> caller_profile -> context ));
$database -> fields [ 'destination_number' ] = check_str ( urldecode ( $row -> caller_profile -> destination_number ));
$database -> fields [ 'context' ] = $context ;
$database -> fields [ 'network_addr' ] = check_str ( urldecode ( $row -> caller_profile -> network_addr ));
}
$database -> fields [ 'caller_id_name' ] = check_str ( urldecode ( $row -> caller_profile -> caller_id_name ));
$database -> fields [ 'caller_id_number' ] = check_str ( urldecode ( $row -> caller_profile -> caller_id_number ));
$x ++ ;
2012-06-04 16:58:40 +02:00
}
2013-04-30 17:50:46 +02:00
unset ( $x );
2012-06-04 16:58:40 +02:00
2016-06-02 00:45:50 +02:00
//if last_sent_callee_id_number is set use it for the destination_number
if ( strlen ( $xml -> variables -> last_sent_callee_id_number ) > 0 ) {
$database -> fields [ 'destination_number' ] = urldecode ( $xml -> variables -> last_sent_callee_id_number );
}
2013-04-30 17:50:46 +02:00
//store the call leg
$database -> fields [ 'leg' ] = $leg ;
2012-06-04 16:58:40 +02:00
2013-04-30 17:50:46 +02:00
//store the call direction
$database -> fields [ 'direction' ] = check_str ( urldecode ( $xml -> variables -> call_direction ));
2012-06-04 16:58:40 +02:00
2013-04-30 17:50:46 +02:00
//store post dial delay, in milliseconds
$database -> fields [ 'pdd_ms' ] = check_str ( urldecode ( $xml -> variables -> progress_mediamsec ) + urldecode ( $xml -> variables -> progressmsec ));
2012-06-04 16:58:40 +02:00
2013-04-30 17:50:46 +02:00
//get break down the date to year, month and day
$tmp_time = strtotime ( $start_stamp );
$tmp_year = date ( " Y " , $tmp_time );
$tmp_month = date ( " M " , $tmp_time );
$tmp_day = date ( " d " , $tmp_time );
2012-06-04 16:58:40 +02:00
2013-04-30 17:50:46 +02:00
//get the domain values from the xml
$domain_name = check_str ( urldecode ( $xml -> variables -> domain_name ));
2014-07-20 18:39:16 +02:00
$domain_uuid = check_str ( urldecode ( $xml -> variables -> domain_uuid ));
2012-07-07 20:40:15 +02:00
2016-02-10 21:54:15 +01:00
//get the domain name from sip_req_host
2016-02-11 01:15:31 +01:00
if ( strlen ( $domain_name ) == 0 ) {
2016-02-10 21:54:15 +01:00
$domain_name = check_str ( urldecode ( $xml -> variables -> sip_req_host ));
}
//send the domain name to the cdr log
2015-07-02 17:08:48 +02:00
xml_cdr_log ( " \n domain_name is ` $domain_name `; domain_uuid is ' $domain_uuid ' \n " );
2013-04-30 17:50:46 +02:00
//get the domain_uuid with the domain_name
2012-07-07 20:40:15 +02:00
if ( strlen ( $domain_uuid ) == 0 ) {
2013-04-30 17:50:46 +02:00
$sql = " select domain_uuid from v_domains " ;
if ( strlen ( $domain_name ) == 0 && $context != 'public' && $context != 'default' ) {
$sql .= " where domain_name = ' " . $context . " ' " ;
}
else {
$sql .= " where domain_name = ' " . $domain_name . " ' " ;
}
2012-07-07 20:40:15 +02:00
$row = $db -> query ( $sql ) -> fetch ();
$domain_uuid = $row [ 'domain_uuid' ];
}
2012-06-04 16:58:40 +02:00
2013-04-30 17:50:46 +02:00
//set values in the database
2016-02-10 21:54:15 +01:00
if ( strlen ( $domain_uuid ) > 0 ) {
$database -> domain_uuid = $domain_uuid ;
$database -> fields [ 'domain_uuid' ] = $domain_uuid ;
}
if ( strlen ( $domain_name ) > 0 ) {
$database -> fields [ 'domain_name' ] = $domain_name ;
}
2012-06-04 16:58:40 +02:00
2013-04-30 17:50:46 +02:00
//check whether a recording exists
2015-12-06 21:37:35 +01:00
$recording_relative_path = '/' . $_SESSION [ 'domain_name' ] . '/archive/' . $tmp_year . '/' . $tmp_month . '/' . $tmp_day ;
2013-04-30 17:50:46 +02:00
if ( file_exists ( $_SESSION [ 'switch' ][ 'recordings' ][ 'dir' ] . $recording_relative_path . '/' . $uuid . '.wav' )) {
$recording_file = $recording_relative_path . '/' . $uuid . '.wav' ;
}
elseif ( file_exists ( $_SESSION [ 'switch' ][ 'recordings' ][ 'dir' ] . $recording_relative_path . '/' . $uuid . '.mp3' )) {
$recording_file = $recording_relative_path . '/' . $uuid . '.mp3' ;
}
2014-07-20 18:39:16 +02:00
if ( isset ( $recording_file ) && ! empty ( $recording_file )) {
2013-04-30 17:50:46 +02:00
$database -> fields [ 'recording_file' ] = $recording_file ;
}
2013-03-10 06:39:45 +01:00
2014-06-14 09:07:04 +02:00
//save to the database in xml format
if ( $_SESSION [ 'cdr' ][ 'format' ][ 'text' ] == " xml " && $_SESSION [ 'cdr' ][ 'storage' ][ 'text' ] == " db " ) {
2014-06-14 06:53:04 +02:00
$database -> fields [ 'xml' ] = check_str ( $xml_string );
}
2014-06-14 09:07:04 +02:00
//save to the database in json format
if ( $_SESSION [ 'cdr' ][ 'format' ][ 'text' ] == " json " && $_SESSION [ 'cdr' ][ 'storage' ][ 'text' ] == " db " ) {
2014-07-20 18:41:36 +02:00
$database -> fields [ 'json' ] = check_str ( json_encode ( $xml ));
2012-06-04 16:58:40 +02:00
}
2013-04-30 17:50:46 +02:00
//insert the check_str($extension_uuid)
if ( strlen ( $xml -> variables -> extension_uuid ) > 0 ) {
$database -> fields [ 'extension_uuid' ] = check_str ( urldecode ( $xml -> variables -> extension_uuid ));
2012-06-04 16:58:40 +02:00
}
2013-04-30 17:50:46 +02:00
2014-06-19 22:52:41 +02:00
//billing information
2016-01-17 08:01:13 +01:00
if ( file_exists ( $_SERVER [ " PROJECT_ROOT " ] . " /app/billing/app_config.php " )){
2014-06-26 03:54:35 +02:00
$db2 = new database ;
2014-11-25 23:22:28 +01:00
$lcr_currency = ( strlen ( $_SESSION [ 'billing' ][ 'currency' ][ 'text' ]) ? $_SESSION [ 'billing' ][ 'currency' ][ 'text' ] : 'USD' );
2015-05-01 20:35:40 +02:00
$accountcode = ( strlen ( urldecode ( $xml -> variables -> accountcode ))) ? check_str ( urldecode ( $xml -> variables -> accountcode )) : $domain_name ;
2014-11-25 23:22:28 +01:00
2014-06-24 13:51:37 +02:00
switch ( check_str ( urldecode ( $xml -> variables -> call_direction ))){
case " outbound " :
2014-11-25 23:22:28 +01:00
$destination_number = check_str ( urldecode ( $xml -> variables -> lcr_query_digits ));
$destination_number_serie = number_series ( $destination_number );
2014-06-24 13:51:37 +02:00
$database -> fields [ 'carrier_name' ] = check_str ( urldecode ( $xml -> variables -> lcr_carrier ));
2014-09-04 22:58:20 +02:00
$sql_rate = " SELECT v_lcr.connect_increment, v_lcr.talk_increment, v_lcr.currency FROM v_lcr, v_carriers WHERE v_carriers.carrier_name = ' " . $xml -> variables -> lcr_carrier . " ' AND v_lcr.rate= " . $xml -> variables -> lcr_rate . " AND v_lcr.lcr_direction = ' " . check_str ( urldecode ( $xml -> variables -> call_direction )) . " ' AND digits IN ( $destination_number_serie ) AND v_lcr.carrier_uuid = v_carriers.carrier_uuid ORDER BY digits DESC, rate ASC limit 1 " ;
2015-05-01 20:35:40 +02:00
$sql_user_rate = " SELECT v_lcr.currency, connect_increment, talk_increment FROM v_lcr JOIN v_billings ON v_billings.type_value=' $accountcode ' WHERE v_lcr.carrier_uuid IS NULL AND v_lcr.lcr_direction = ' " . check_str ( urldecode ( $xml -> variables -> call_direction )) . " ' AND v_lcr.lcr_profile=v_billings.lcr_profile AND NOW() >= v_lcr.date_start AND NOW() < v_lcr.date_end AND digits IN ( $destination_number_serie ) ORDER BY digits DESC, rate ASC, date_start DESC LIMIT 1 " ;
2014-06-24 13:51:37 +02:00
if ( $debug ) {
echo " sql_rate: $sql_rate\n " ;
echo " sql_user_rate: $sql_user_rate\n " ;
}
$db2 -> sql = $sql_rate ;
$db2 -> result = $db2 -> execute ();
2016-06-02 00:45:50 +02:00
//print_r($db2->result);
2014-11-25 23:22:28 +01:00
$lcr_currency = ( strlen ( $db2 -> result [ 0 ][ 'currency' ]) ? check_str ( $db2 -> result [ 0 ][ 'currency' ]) :
( strlen ( $_SESSION [ 'billing' ][ 'currency' ][ 'text' ]) ? $_SESSION [ 'billing' ][ 'currency' ][ 'text' ] : 'USD' )
);
2014-06-24 13:51:37 +02:00
$lcr_rate = ( strlen ( $xml -> variables -> lcr_rate ) ? $xml -> variables -> lcr_rate : 0 );
$lcr_first_increment = ( strlen ( $db2 -> result [ 0 ][ 'connect_increment' ]) ? check_str ( $db2 -> result [ 0 ][ 'connect_increment' ]) : 60 );
$lcr_second_increment = ( strlen ( $db2 -> result [ 0 ][ 'talk_increment' ]) ? check_str ( $db2 -> result [ 0 ][ 'talk_increment' ]) : 60 );
unset ( $db2 -> sql );
unset ( $db2 -> result );
2014-07-20 18:39:16 +02:00
2014-06-24 13:51:37 +02:00
$db2 -> sql = $sql_user_rate ;
$db2 -> result = $db2 -> execute ();
$lcr_user_rate = ( strlen ( $xml -> variables -> lcr_user_rate ) ? $xml -> variables -> lcr_user_rate : 0.01 );
$lcr_user_first_increment = ( strlen ( $db2 -> result [ 0 ][ 'connect_increment' ]) ? check_str ( $db2 -> result [ 0 ][ 'connect_increment' ]) : 60 );
$lcr_user_second_increment = ( strlen ( $db2 -> result [ 0 ][ 'talk_increment' ]) ? check_str ( $db2 -> result [ 0 ][ 'talk_increment' ]) : 60 );
2014-11-25 23:22:28 +01:00
$lcr_user_currency = ( strlen ( $db2 -> result [ 0 ][ 'currency' ]) ? check_str ( $db2 -> result [ 0 ][ 'currency' ]) :
( strlen ( $_SESSION [ 'billing' ][ 'currency' ][ 'text' ]) ? $_SESSION [ 'billing' ][ 'currency' ][ 'text' ] : 'USD' )
);
2014-06-24 13:51:37 +02:00
unset ( $db2 -> sql );
unset ( $db2 -> result );
break ;
case " inbound " :
2015-01-22 16:29:43 +01:00
$callee_number = check_str ( urldecode ( $row -> caller_profile -> destination_number ));
$callee_number_serie = number_series ( $callee_number );
2016-01-09 05:44:06 +01:00
$sql_user_rate = " SELECT v_lcr.currency, v_lcr.rate, v_lcr.connect_increment, v_lcr.talk_increment FROM v_lcr JOIN v_billings ON v_billings.type_value=' $accountcode ' WHERE v_lcr.carrier_uuid IS NULL AND v_lcr.lcr_direction = ' " . check_str ( urldecode ( $xml -> variables -> call_direction )) . " ' AND v_lcr.lcr_profile=v_billings.lcr_profile AND NOW() >= v_lcr.date_start AND NOW() < v_lcr.date_end AND digits IN ( $callee_number_serie ) ORDER BY digits DESC, rate ASC, date_start DESC LIMIT 1 " ;
2015-05-01 20:35:40 +02:00
2014-07-12 09:49:41 +02:00
if ( $debug ) {
echo " sql_user_rate: $sql_user_rate\n " ;
}
$db2 -> sql = $sql_user_rate ;
$db2 -> result = $db2 -> execute ();
2014-06-24 13:51:37 +02:00
2014-07-12 09:49:41 +02:00
// If selling rate is found, then we fill with data, otherwise rate will be 0
2014-11-25 23:22:28 +01:00
$lcr_currency = ( strlen ( $db2 -> result [ 0 ][ 'currency' ]) ? check_str ( $db2 -> result [ 0 ][ 'currency' ]) :
( strlen ( $_SESSION [ 'billing' ][ 'currency' ][ 'text' ]) ? $_SESSION [ 'billing' ][ 'currency' ][ 'text' ] : 'USD' )
);
2014-12-29 06:35:08 +01:00
$lcr_user_rate = ( strlen ( $db2 -> result [ 0 ][ 'rate' ])) ? ( $db2 -> result [ 0 ][ 'rate' ]) : 0 ;
$lcr_user_first_increment = ( strlen ( $db2 -> result [ 0 ][ 'connect_increment' ])) ? ( $db2 -> result [ 0 ][ 'connect_increment' ]) : 60 ;
$lcr_user_second_increment = ( strlen ( $db2 -> result [ 0 ][ 'talk_increment' ])) ? ( $db2 -> result [ 0 ][ 'talk_increment' ]) : 60 ;
2014-11-25 23:22:28 +01:00
$lcr_user_currency = ( strlen ( $db2 -> result [ 0 ][ 'currency' ]) ? check_str ( $db2 -> result [ 0 ][ 'currency' ]) :
( strlen ( $_SESSION [ 'billing' ][ 'currency' ][ 'text' ]) ? $_SESSION [ 'billing' ][ 'currency' ][ 'text' ] : 'USD' )
);
2014-07-12 09:49:41 +02:00
// Actually, there is no way to detect what carrier is the calling comming from using current information
$lcr_rate = 0 ; $lcr_first_increment = 0 ; $lcr_second_increment = 0 ;
unset ( $db2 -> sql );
unset ( $db2 -> result );
2014-06-24 13:51:37 +02:00
break ;
case " local " :
2014-11-25 23:22:28 +01:00
$destination_number = check_str ( urldecode ( $xml -> variables -> lcr_query_digits ));
$destination_number_serie = number_series ( $destination_number );
2015-05-01 20:35:40 +02:00
$sql_user_rate = " SELECT v_lcr.currency, connect_increment, talk_increment FROM v_lcr JOIN v_billings ON v_billings.type_value=' $accountcode ' WHERE v_lcr.carrier_uuid IS NULL AND v_lcr.lcr_direction = ' " . check_str ( urldecode ( $xml -> variables -> call_direction )) . " ' AND v_lcr.lcr_profile=v_billings.lcr_profile AND NOW() >= v_lcr.date_start AND NOW() < v_lcr.date_end AND digits IN ( $destination_number_serie ) ORDER BY digits DESC, rate ASC, date_start DESC LIMIT 1 " ;
2014-07-12 09:49:41 +02:00
if ( $debug ) {
echo " sql_user_rate: $sql_user_rate\n " ;
}
$db2 -> sql = $sql_user_rate ;
$db2 -> result = $db2 -> execute ();
// If selling rate is found, then we fill with data, otherwise rate will be 0
2014-11-25 23:22:28 +01:00
$lcr_currency = ( strlen ( $db2 -> result [ 0 ][ 'currency' ]) ? check_str ( $db2 -> result [ 0 ][ 'currency' ]) :
( strlen ( $_SESSION [ 'billing' ][ 'currency' ][ 'text' ]) ? $_SESSION [ 'billing' ][ 'currency' ][ 'text' ] : 'USD' )
);
2014-12-29 06:35:08 +01:00
$lcr_user_rate = ( strlen ( $db2 -> result [ 0 ][ 'rate' ])) ? ( $$db2 -> result [ 0 ][ 'rate' ]) : 0 ;
$lcr_user_first_increment = ( strlen ( $db2 -> result [ 0 ][ 'connect_increment' ])) ? ( $db2 -> result [ 0 ][ 'connect_increment' ]) : 60 ;
$lcr_user_second_increment = ( strlen ( $db2 -> result [ 0 ][ 'talk_increment' ])) ? ( $db2 -> result [ 0 ][ 'talk_increment' ]) : 60 ;
2014-11-25 23:22:28 +01:00
$lcr_user_currency = ( strlen ( $db2 -> result [ 0 ][ 'currency' ]) ? check_str ( $db2 -> result [ 0 ][ 'currency' ]) :
( strlen ( $_SESSION [ 'billing' ][ 'currency' ][ 'text' ]) ? $_SESSION [ 'billing' ][ 'currency' ][ 'text' ] : 'USD' )
);
2014-07-12 09:49:41 +02:00
// Actually, internal calls have 0 cost
2014-06-24 13:51:37 +02:00
$lcr_rate = 0 ; $lcr_first_increment = 0 ; $lcr_second_increment = 0 ;
2014-07-12 09:49:41 +02:00
unset ( $db2 -> sql );
unset ( $db2 -> result );
2014-06-24 13:51:37 +02:00
break ;
2014-06-22 00:08:11 +02:00
}
2014-07-12 09:49:41 +02:00
// Please note that we save values using LCR currency, but we discount balance in billing currency
2014-06-22 00:08:11 +02:00
$time = check_str ( urldecode ( $xml -> variables -> billsec ));
$call_buy = call_cost ( $lcr_rate , $lcr_first_increment , $lcr_second_increment , $time );
2014-07-12 09:49:41 +02:00
$call_sell = call_cost ( $lcr_user_rate , $lcr_user_first_increment , $lcr_user_second_increment , $time );
2014-11-25 23:22:28 +01:00
// Costs/Sell call are in original LCR currency, they need to be converted
2014-07-12 09:49:41 +02:00
2014-06-22 00:08:11 +02:00
$database -> fields [ 'call_buy' ] = check_str ( $call_buy );
$database -> fields [ 'call_sell' ] = check_str ( $call_sell );
2014-07-18 01:46:04 +02:00
$db2 -> table = " v_xml_cdr " ;
2014-08-28 18:11:24 +02:00
$db2 -> sql = " SELECT currency FROM v_billings WHERE type_value=' $accountcode ' LIMIT 1 " ;
2014-07-18 02:13:44 +02:00
$db2 -> result = $db2 -> execute ();
2014-08-28 18:11:24 +02:00
$actual_currency = ( strlen ( $lcr_currency ) ?
$lcr_currency :
( strlen ( $_SESSION [ 'billing' ][ 'currency' ][ 'text' ]) ? $_SESSION [ 'billing' ][ 'currency' ][ 'text' ] : 'USD' )
);
2014-07-24 02:41:32 +02:00
$billing_currency = ( strlen ( $db2 -> result [ 0 ][ 'currency' ]) ? $db2 -> result [ 0 ][ 'currency' ] : $default_currency );
2014-07-18 01:46:04 +02:00
2014-06-22 00:08:11 +02:00
if ( $debug ) {
2014-07-20 18:39:16 +02:00
echo " sql: " . $db2 -> sql . " \n " ;
2014-06-22 20:52:14 +02:00
echo " c " . $database -> fields [ 'carrier_name' ] . " \n " ;
2014-06-22 00:08:11 +02:00
echo " t $time\n " ;
echo " b r: $lcr_rate - $lcr_first_increment - $lcr_first_increment = $call_buy\n " ;
echo " s r: $lcr_user_rate - $lcr_user_first_increment - $lcr_user_second_increment = $call_sell\n " ;
2014-11-25 23:22:28 +01:00
echo " lcr currency $lcr_currency\n " ;
echo " actual currency $actual_currency\n " ;
echo " user currency $lcr_user_currency\n " ;
echo " billing currency $billing_currency\n " ;
2014-07-12 09:49:41 +02:00
}
2014-07-18 01:46:04 +02:00
2014-07-18 02:13:44 +02:00
unset ( $database -> sql );
unset ( $database -> result );
2014-07-12 09:49:41 +02:00
$sql_balance = " SELECT balance, old_balance FROM v_billings WHERE type_value=' " . check_str ( urldecode ( $xml -> variables -> accountcode )) . " ' " ;
2014-06-24 03:09:41 +02:00
$db2 -> sql = $sql_balance ;
$db2 -> result = $db2 -> execute ();
$balance = $db2 -> result [ 0 ][ 'balance' ];
$old_balance = $db2 -> result [ 0 ][ 'old_balance' ];
if ( $debug ) {
echo " sql_balance: $sql_balance\n " ;
echo " bal: $balance\n " ;
echo " old bal: $old_balance\n " ;
}
2014-07-12 09:49:41 +02:00
// Lets convert rate from lcr_currency to billing_currency
2014-11-25 23:22:28 +01:00
$billing_call_sell = currency_convert ( $call_sell , $billing_currency , $lcr_user_currency );
2014-07-12 09:49:41 +02:00
2014-07-18 01:46:04 +02:00
if ( $debug ) {
echo " bcs: $billing_call_sell $billing_currency\n " ;
}
2014-07-12 09:49:41 +02:00
// Remember that old_balance is using billing_currency
$updated_balance = ( double ) $old_balance - ( double ) $billing_call_sell ;
2014-06-24 03:09:41 +02:00
unset ( $db2 -> sql );
unset ( $db2 -> result );
$sql_update_balance = " UPDATE v_billings SET balance= $updated_balance , old_balance= $updated_balance WHERE type_value=' " . check_str ( urldecode ( $xml -> variables -> accountcode )) . " ' " ;
if ( $debug ) {
echo " sql_update_balance: $sql_update_balance\n " ;
}
$db2 -> sql = $sql_update_balance ;
$db2 -> result = $db2 -> execute ();
unset ( $db2 -> sql );
unset ( $db2 -> result );
2014-07-20 18:39:16 +02:00
2014-06-19 22:52:41 +02:00
}
2013-04-30 17:50:46 +02:00
//insert xml_cdr into the db
if ( strlen ( $start_stamp ) > 0 ) {
$database -> add ();
2012-06-04 16:58:40 +02:00
if ( $debug ) {
2013-04-30 17:50:46 +02:00
echo $database -> sql . " \n " ;
2012-06-04 16:58:40 +02:00
}
}
2013-04-30 17:50:46 +02:00
//insert the values
if ( strlen ( $uuid ) > 0 ) {
if ( $debug ) {
$time5_insert = microtime ( true );
//echo $sql."<br />\n";
}
try {
$error = " false " ;
//$db->exec(check_sql($sql));
}
catch ( PDOException $e ) {
$tmp_dir = $_SESSION [ 'switch' ][ 'log' ][ 'dir' ] . '/xml_cdr/failed/' ;
2012-06-04 16:58:40 +02:00
if ( ! file_exists ( $tmp_dir )) {
2016-06-28 23:18:40 +02:00
event_socket_mkdir ( $tmp_dir );
2012-06-04 16:58:40 +02:00
}
2014-06-14 09:07:04 +02:00
if ( $_SESSION [ 'cdr' ][ 'format' ][ 'text' ] == " xml " ) {
$tmp_file = $uuid . '.xml' ;
$fh = fopen ( $tmp_dir . '/' . $tmp_file , 'w' );
fwrite ( $fh , $xml_string );
}
else {
$tmp_file = $uuid . '.json' ;
$fh = fopen ( $tmp_dir . '/' . $tmp_file , 'w' );
fwrite ( $fh , json_encode ( $xml ));
}
2012-06-04 16:58:40 +02:00
fclose ( $fh );
2013-04-30 17:50:46 +02:00
if ( $debug ) {
echo $e -> getMessage ();
}
$error = " true " ;
}
2014-06-14 09:07:04 +02:00
if ( $_SESSION [ 'cdr' ][ 'storage' ][ 'text' ] == " dir " && $error != " true " ) {
2013-04-30 17:50:46 +02:00
if ( strlen ( $uuid ) > 0 ) {
$tmp_time = strtotime ( $start_stamp );
$tmp_year = date ( " Y " , $tmp_time );
$tmp_month = date ( " M " , $tmp_time );
$tmp_day = date ( " d " , $tmp_time );
$tmp_dir = $_SESSION [ 'switch' ][ 'log' ][ 'dir' ] . '/xml_cdr/archive/' . $tmp_year . '/' . $tmp_month . '/' . $tmp_day ;
if ( ! file_exists ( $tmp_dir )) {
2016-06-28 23:18:40 +02:00
event_socket_mkdir ( $tmp_dir );
2013-04-30 17:50:46 +02:00
}
2014-06-14 09:07:04 +02:00
if ( $_SESSION [ 'cdr' ][ 'format' ][ 'text' ] == " xml " ) {
$tmp_file = $uuid . '.xml' ;
$fh = fopen ( $tmp_dir . '/' . $tmp_file , 'w' );
fwrite ( $fh , $xml_string );
}
else {
$tmp_file = $uuid . '.json' ;
$fh = fopen ( $tmp_dir . '/' . $tmp_file , 'w' );
fwrite ( $fh , json_encode ( $xml ));
}
2013-04-30 17:50:46 +02:00
fclose ( $fh );
}
}
unset ( $error );
2013-04-30 18:44:51 +02:00
2013-04-30 17:50:46 +02:00
if ( $debug ) {
GLOBAL $insert_time , $insert_count ;
2014-06-14 09:19:56 +02:00
$insert_time += microtime ( true ) - $time5_insert ; //add this current query.
2013-04-30 17:50:46 +02:00
$insert_count ++ ;
2012-06-04 16:58:40 +02:00
}
}
2013-04-30 17:50:46 +02:00
unset ( $sql );
}
2012-06-04 16:58:40 +02:00
//get cdr details from the http post
if ( strlen ( $_POST [ " cdr " ]) > 0 ) {
2014-06-18 03:40:17 +02:00
if ( $debug ){
print_r ( $_POST [ " cdr " ]);
}
2015-07-02 17:08:48 +02:00
2012-06-04 16:58:40 +02:00
//authentication for xml cdr http post
2015-03-18 13:53:40 +01:00
if ( $_SESSION [ " cdr " ][ " http_enabled " ][ " boolean " ] == " true " && strlen ( $_SESSION [ " xml_cdr " ][ " username " ]) == 0 ) {
2012-06-04 16:58:40 +02:00
//get the contents of xml_cdr.conf.xml
$conf_xml_string = file_get_contents ( $_SESSION [ 'switch' ][ 'conf' ][ 'dir' ] . '/autoload_configs/xml_cdr.conf.xml' );
//parse the xml to get the call detail record info
try {
$conf_xml = simplexml_load_string ( $conf_xml_string );
}
catch ( Exception $e ) {
echo $e -> getMessage ();
}
foreach ( $conf_xml -> settings -> param as $row ) {
if ( $row -> attributes () -> name == " cred " ) {
$auth_array = explode ( " : " , $row -> attributes () -> value );
2015-03-18 13:53:40 +01:00
//echo "username: ".$auth_array[0]."<br />\n";
//echo "password: ".$auth_array[1]."<br />\n";
2013-04-30 11:30:44 +02:00
}
if ( $row -> attributes () -> name == " url " ) {
2015-03-18 13:53:40 +01:00
//check name is equal to url
2012-06-04 16:58:40 +02:00
}
}
}
2013-04-30 18:44:51 +02:00
//if http enabled is set to false then deny access
2015-03-18 13:53:40 +01:00
if ( $_SESSION [ " cdr " ][ " http_enabled " ][ " boolean " ] == " false " ) {
2013-04-30 18:44:51 +02:00
echo " access denied<br /> \n " ;
return ;
}
2013-04-30 11:30:44 +02:00
2013-04-30 18:44:51 +02:00
//check for the correct username and password
2015-03-18 13:53:40 +01:00
if ( $_SESSION [ " cdr " ][ " http_enabled " ][ " boolean " ] == " true " ) {
if ( $auth_array [ 0 ] == $_SERVER [ " PHP_AUTH_USER " ] && $auth_array [ 1 ] == $_SERVER [ " PHP_AUTH_PW " ]) {
//echo "access granted<br />\n";
$_SESSION [ " xml_cdr " ][ " username " ] = $auth_array [ 0 ];
$_SESSION [ " xml_cdr " ][ " password " ] = $auth_array [ 1 ];
}
else {
echo " access denied<br /> \n " ;
return ;
}
2013-04-30 18:44:51 +02:00
}
//loop through all attribues
//foreach($xml->settings->param[1]->attributes() as $a => $b) {
// echo $a,'="',$b,"\"<br />\n";
//}
2012-06-04 16:58:40 +02:00
//get the http post variable
$xml_string = trim ( $_POST [ " cdr " ]);
//get the leg of the call
if ( substr ( $_REQUEST [ 'uuid' ], 0 , 2 ) == " a_ " ) {
$leg = " a " ;
}
else {
$leg = " b " ;
}
2015-07-02 17:08:48 +02:00
xml_cdr_log ( " process cdr via post \n " );
2012-06-04 16:58:40 +02:00
//parse the xml and insert the data into the db
process_xml_cdr ( $db , $leg , $xml_string );
}
//check the filesystem for xml cdr records that were missed
$xml_cdr_dir = $_SESSION [ 'switch' ][ 'log' ][ 'dir' ] . '/xml_cdr' ;
$dir_handle = opendir ( $xml_cdr_dir );
$x = 0 ;
2015-02-12 17:42:58 +01:00
while ( $file = readdir ( $dir_handle )) {
2012-06-04 16:58:40 +02:00
if ( $file != '.' && $file != '..' ) {
if ( ! is_dir ( $xml_cdr_dir . '/' . $file ) ) {
//get the leg of the call
if ( substr ( $file , 0 , 2 ) == " a_ " ) {
$leg = " a " ;
}
else {
$leg = " b " ;
}
//get the xml cdr string
$xml_string = file_get_contents ( $xml_cdr_dir . '/' . $file );
//parse the xml and insert the data into the db
process_xml_cdr ( $db , $leg , $xml_string );
//delete the file after it has been imported
unlink ( $xml_cdr_dir . '/' . $file );
$x ++ ;
}
}
}
closedir ( $dir_handle );
//debug true
if ( $debug ) {
$content = ob_get_contents (); //get the output from the buffer
ob_end_clean (); //clean the buffer
$time = " \n \n $insert_count inserts in: " . number_format ( $insert_time , 5 ) . " seconds. \n " ;
$time .= " Other processing time: " . number_format (( microtime ( true ) - $time5 - $insert_time ), 5 ) . " seconds. \n " ;
2015-07-02 17:08:48 +02:00
xml_cdr_log ( $content . $time );
2012-06-04 16:58:40 +02:00
}
2015-05-01 19:39:13 +02:00
?>