File: /home/posscale/subdomains/ccm/import_old.php
<?
error_reporting(0);
include("includes/php/top.php");
require_once("includes/php/CsvInterface.class.php");
require_once("includes/php/Csv.class.php");
$start_file = '/home/posscale/quadro_cdr';
if($handle = opendir($start_file))
{
while(false !== ($file = readdir($handle)))
{
if ($file != "." && $file != "..")
{
$client_id = $file;
if($handle1 = opendir($start_file.'/'.$client_id))
{
while(false !== ($file1 = readdir($handle1)))
{
if ($file1 != "." && $file1 != "..")
{
$pbx_id = $file1;
if($handle2 = opendir($start_file.'/'.$client_id.'/'.$pbx_id.'/unparsed'))
{
while(false !== ($file2 = readdir($handle2)))
{
if ($file2 != "." && $file2 != "..")
{
/*/ START GENERATION /*/
/*/ START GENERATION /*/
/*/ START GENERATION /*/
try
{
$Csv = new Csv($filename = $start_file.'/'.$client_id.'/'.$pbx_id.'/unparsed/'.$file2, $separator = ",");
$csvData = $Csv->readAll();
for($i=1; $i<count($csvData); $i++)
{
if(trim($csvData[$i][0]) == 'SC')
{
$calling = determine_call_type($csvData[$i][3]);
$called = determine_call_type($csvData[$i][4]);
$extension = "";
$number = "";
$direction = "";
if($calling['type']=="PBX")
{
$extension = $calling['number'];
if($called['type']=="EXTERNAL" || $called['type']=="GATEWAY")
{
$direction = "OUT";
}
elseif($called['type']=="PBX")
{
$direction = "INTERNAL";
}
else
{
$direction = "OTHER";
}
$number = $called['number'];
}
if($called['type']=="PBX")
{
$extension = $called['number'];
if($calling['type']=="EXTERNAL" || $calling['type']=="GATEWAY")
{
$direction = "IN";
$number = $calling['number'];
}
}
if(!empty($extension))
{
$date_time = explode(' ', $csvData[$i][1]);
$exp_date = explode('-', $date_time[0]);
$date = $exp_date[2].'-'.$exp_date[1].'-'.$exp_date[0].' '.$date_time[1];
$sql = "
INSERT INTO
calls
SET
user_id = ".$client_id.",
pbx_id = ".$pbx_id.",
time = ".strtotime($date).",
ext = ".$extension.",
direction = '".$direction."',
duration = ".$csvData[$i][2].",
number = '".$number."',
id = ".next_id('calls').';';
db_query($sql, 0);
}
}
}
}
catch (Exception $e)
{
// do nudda
}
rename($start_file.'/'.$client_id.'/'.$pbx_id.'/unparsed/'.$file2, $start_file.'/'.$client_id.'/'.$pbx_id.'/parsed/'.$file2);
/*/ END GENERATION /*/
/*/ END GENERATION /*/
/*/ END GENERATION /*/
}
}
closedir($handle2);
}
}
}
closedir($handle1);
}
}
}
closedir($handle);
}
function determine_call_type($string)
{
if(strpos($string, '@') !== false)
{
$call_type = "EXTERNAL";
$datatemp = explode(' ', $string);
$number = str_replace('"', '', $datatemp[0]);
if(strpos($number, '@') !== false)
{
$datatemp = explode('@', $number);
$number = $datatemp[0];
}
}
elseif(strpos($string, 'GW[') !== false)
{
$call_type = "GATEWAY";
}
else
{
$call_type = "PBX";
$datatemp = explode('" ', $string);
$number = $datatemp[1];
}
return array('type'=>$call_type, 'number'=>$number);
}
?>