<?
/*********************** INCLUDES *********************************************/
  $includePath=realpath(dirname(__FILE__)."/../");
  require_once ($includePath.'/config/cfg.php');
  //require($includePath.'/constants.php');
  require_once ($includePath.'/lib/phpagi/phpagi.php');
  //require($includePath.'/agi/agifunction.php');
/*********************** INITIALIZATION ***************************************/

  $agi = new AGI();
  $triggeredRuleId=$_GET["rule"];
  $triggeredEntityId=$_GET["entity"];
  $triggeredCallerid=$agi->get_variable("CALLERID");
  $triggeredCallerid=$triggeredCallerid["data"];
  $triggeredAnswered=$_GET["answered"];
  $triggeredChannelId=$_GET["channel"];

  $canContinue=true;

  // sanitize callerid
  preg_match("/(?:\"(.*)\"|(\s*))\s*(?:\<(.*)\>|(.*))/", $triggeredCallerid, $matches);

  $triggeredCallername = $matches[1];

  if ($matches[3]) {
    $triggeredCallerid = $matches[3];
  }
  else if ($matches[4]) {
    $triggeredCallerid = $matches[4];
  }
  else {
    error_log("WARNING: No caller ID matched!");
  }
  
  error_log("-----------------------------------------------");
  error_log("Incoming Rule Set id: $triggeredRuleId (callerid: $triggeredCallerid)");
  error_log("-----------------------------------------------");

/*********************** PAGE LOGIC ********************************************/

  $qry="SELECT * FROM cw_rule_incoming WHERE rule=$triggeredRuleId ORDER BY priority";
  $res=$db->Execute($qry);
  while ($arr=$res->FetchRow()) {
    $id=$arr["id"];
    $rule=$arr["rule"];
    $priority=$arr["priority"];
    $timesegment=$arr["timesegment"];
    $callerid=$arr["callerid"];
    $channel=$arr["from_channel"];
    $number=$arr["from_number"];
    $macro=$arr["macro"];
    $parameter=$arr["parameter"];
    $duration=$arr["duration"];
    error_log("Rule: $id (callerid: $callerid, channel: $channel)");

    //extract the numbers
    $numberArr=split("\|",$number);
    
    //check the time segment
    if (checkTime($timesegment)===false) continue;    

    //fetch the entity type    
    if ($triggeredEntityId!="") {
      $qry="SELECT * FROM cw_channel WHERE `key`=$triggeredEntityId";
      $resT=$db->Execute($qry);
      $t=$resT->FetchRow();
      $channelId=$t["id"];
    } else $channelId=$triggeredChannelId;
     
    //check if some rule are triggered
    if  ( 
          (($callerid=="") OR ($callerid!="" AND $triggeredCallerid==$callerid)) AND
          (($channel==0) OR ($channel!=0 AND $channel==$channelId)) AND
          (
            ($number=="") OR  //the rule number is * 
            ($number!="" AND in_array($triggeredAnswered,$numberArr)) OR //some numbers in the rule match to the triggered one 
            (in_array("X",$numberArr) AND $triggeredAnswered!="s" )  //some numbers are dialed and a catch all matched
          ) AND
          $canContinue===true
        ) {

      error_log("  TRIGGERED RULE $id!");
      
      $arrMacro=prepareMacro($macro,$parameter);      
      error_log("  Executing macro ".$arrMacro[0].$arrMacro[1]." ...");
      $agi->exec("macro ".$arrMacro[0].$arrMacro[1]);
      
      if ($duration=="1") $canContinue=true; else $canContinue=false;
    } 
      
  }
  error_log("Exit from incoming.php");
    
/*********************** PRIVATE FUNCTION *************************************/


  
?>
