#!/usr/bin/php -q
<?php
define("VERB",5);
    set_time_limit(30);
    require('include/phpagi.php');
    error_reporting(E_ALL);

class Channel_class{
private $CH_tech='';
private  $CH_caller='';
private  $CH_trunk='';
private	 $CH_channel='';

public function Load_tech(){
 	return $this->CH_tech;
}
public function Load_caller(){
	return $this->CH_caller;
}
public function Load_channel(){
	return $this->CH_channel;
}
public function Load_trunk(){
	if ($this->CH_trunk!='')
	  return $this->CH_trunk;
	else return "Nepouzit";
}
function Channel_class($pom){
	$polepom = explode("/",$pom);
	if (count($polepom) == 2){
	# Spojeni bez trunku
	  $this->CH_tech = $polepom[0];
	  $pompom = explode("-",$polepom[1]);
	  $this->CH_caller = $pompom[0];
	  $this->CH_channel = $pompom[1];
	}
	else if (count($polepom) == 3){
	# Spojeni pres trunk
	  $this->CH_tech = $polepom[0];
	  $this->CH_trunk = $polepom[1];
	  $pompom = explode("-",$polepom[2]);
	  $this->CH_caller = $pompom[0];
	  $this->CH_channel = $pompom[1];
	}
  }
}	


    
    $agi = new AGI();
#Spojeni s MySQL.Jestli nelze navazat,je zaveseno
	$conn_user  = 'root';
	$conn_pass  = 'root';
    	$conn_server  = "localhost"   ;
    	$conn_db  = "mydb"   ;
    	$link=mysql_connect($conn_server,$conn_user,$conn_pass) ;
    	if (!$link) {
		
      		$agi->verbose('Could not connect:'.mysql_error(),VERB);
		$agi->hangup();
		
    	}
    	mysql_select_db($conn_db);
    	$dial_destnum = $agi->get_variable("EXTEN");
	$agi->verbose("Cilove cislo - {$dial_destnum['data']}",VERB);
	$delkadst=strlen($dial_destnum['data']);
	$dial_caller = $agi->get_variable("CALLERID(name)");
	$agi->verbose("Volajici - {$dial_caller['data']}",VERB);
	$dial_channel = $agi->get_variable("CHANNEL");
	$channel = new Channel_class($dial_channel['data']);
	$agi->verbose("Kanal tech - {$channel->Load_tech()}",VERB);
	$agi->verbose("Kanal trunk - {$channel->Load_trunk()}",VERB);
	$agi->verbose("Kanal volajici - {$channel->Load_caller()}",VERB);
	$agi->verbose("Kanal idkanalu - {$channel->Load_channel()}",VERB);
###########################################################################
#Zjisteni databazoveho ID volajiciho
###########################################################################
#Urceni buddies tabulky pro hledani
	$query="SELECT table_name
		FROM technology
		WHERE technology =\"".$channel->Load_tech()."\"
		";
	$result = mysql_query($query);
	if (!$result) {
      		$agi->verbose('Chyba'.mysql_error(),VERB);
	}
	list($table,)=mysql_fetch_row($result);
#Vyber ID z urcene tabulky
	$query="SELECT id
		FROM ".$table."
		WHERE name = \"".$channel->Load_caller()."\"
		";
	$result = mysql_query($query);
	if (!$result) {
      		$agi->verbose('Chyba'.mysql_error(),VERB);
	}
	list($id)=mysql_fetch_row($result);
	$agi->verbose("ID volajiciho ze {$table} - {$id}",VERB);


###########################################################################
	do {
		$dest = substr($dial_destnum['data'],0,$delkadst); 
		$agi->verbose("Hledam pravidlo pro - {$dest}",VERB);
		$query="SELECT idPAYTYPE,value,TrunkName
			FROM call_check_view
			WHERE DestNumber = \"".$dest."\"
			AND technology = \"".$channel->Load_tech()."\"
			AND id = ".$id."
			AND StartDate < now()

			";
		$result = mysql_query($query);
		if (!$result) {
      			$agi->verbose('Chyba'.mysql_error(),VERB);
		}
		$pocet=mysql_num_rows($result);
	//	$delkadst--;
		$agi->verbose("delka dst- {$delkadst}",VERB);
		if (mysql_num_rows($result)!=0){
			$agi->verbose("!!!shoda!!!",VERB);
		}
		else{$agi->verbose("Nenalezeno",VERB);
		    $delkadst--;
		}	 
	} while(mysql_num_rows($result) == 0 && $delkadst != 0);
	if (($delkadst) ==0){
		//$agi->verbose("delka dst- {$delkadst}",VERB);
	#nebylo nalezeno pravidlo a hovor je ukoncen
		$agi->verbose("!!!NEBYLO VYTVORENO TARIFIKACNI PRAVIDLO!!!",VERB);
		$agi->stream_file('cz/sorry');
		$agi->hangup();	
	}
	#bylo nalezeno pravidlo jsou kontrolovana dalsi data
	list($idPAYTYPE,$value,$trunk)=mysql_fetch_row($result);
	if ($idPAYTYPE==2){
	#kontrola kreditu pro predplacene uctovani
		if($value>0) {
			$agi->verbose("Zbyva {$value} creditu - OK",VERB);	
		}
		else{
			$agi->verbose("Nedostatek creditu",VERB);
			$agi->hangup();
		}

	}
    //$agi->verbose("tady",VERB);
    if ($trunk=='Local'){
	$agi->verbose("Vytacim mistne-Local",VERB);
	$agi->exec(Dial,"SIP/{$dial_destnum['data']}");
    }else{
	$agi->verbose("Normuji cislo {$dial_destnum['data']}",VERB);
	$normdial_destnum=substr($dial_destnum['data'],$delkadst);
	$agi->verbose("Normovane cislo {$normdial_destnum}",VERB);
	
	$agi->verbose("Vytacim pres trunk-{$trunk}",VERB);
	$agi->exec(Dial,"SIP/{$trunk}/{$normdial_destnum}");
    }
//    $agi->exec(Dial,"SIP/SIP_TRN/1100");
    unset($channel);
    unset($agi);
    ?>
    
