<?
/*********************** INCLUDES *********************************************/
  $includePath=realpath(dirname(__FILE__)."/../");
  require_once($includePath.'/config/cfg.php');
  require_once ($includePath.'/lib/phpagi/phpagi.php');
/*********************** INITIALIZATION ***************************************/
  $agi = new AGI();
  if((!isset($_GET["code"]))||($_GET["code"]==0)){
    agiError("-----------------------------------------------");
    agiError("Utility Unknown: utility code is empty or not given");
    agiError("-----------------------------------------------"); 
  }else{
    switch($_GET["code"]){
      /***********   [1] SET/UNSET INCOMING RULESET JUMP    **********/
      case "1":
        agiError("-----------------------------------------------");
        agiError("Utility 1: set/unset incoming ruleset jump");
        agiError("-----------------------------------------------"); 
        $param_from=intval($_GET["from"]);
        $param_to=intval($_GET["to"]);
        $param_eol=$_GET["eol"];
        _rulesetJump($param_from,$param_to,$param_eol);
        break;
      /**************             WRONG CODE             ************/  
      default:
        agiError("-----------------------------------------------");
        agiError("Utility Unknown: utility code ".$_GET["code"]." is not defined on the system");
        agiError("-----------------------------------------------");
        break; 
    }
  } 
  
  
  
/****************************** UTILITY FUNCTIONS *****************************/  
  function _rulesetJump($from,$to,$eol,$strict){
    global $db,$agi;
    //check from ruleset
    $from_name="";
    $qry="SELECT name FROM `cw_rule` WHERE `id`='".$from."' AND `type`='2' ";
    $res=$db->Execute($qry);
    if($res!==false){
      if($arr=$res->FetchRow()){
        $from_name="[".$from."]".$arr["name"]."";
      }
    }
    //check to ruleset
    $to_name="";
    $qry="SELECT name FROM `cw_rule` WHERE `id`='".$to."' AND `type`='2' ";
    $res=$db->Execute($qry);
    if($res!==false){
      if($arr=$res->FetchRow()){
        $to_name="[".$to."]".$arr["name"]."";
      }
    }
    //check values
    $error="";
    if(trim($from_name)==""){
      $error="  -> INVALID FROM RULESET [".$from."] ";
    }else if(trim($to_name)==""){
      $error="  -> INVALID TO RULESET [".$to."] ";
    }else if(intval($eol)<1){
      $error="  -> INVALID END-OF-LIFE [".$eol."] ";
    }
    if($error!=""){ 
      agiError($error);
      return false;
    }
    //calculate end of life
    $eol=date("Y-m-d H:i:s",time()+($eol*60*60));
    agiError("FROM ".$from_name." TO ".$to_name." (UNTIL ".$eol.")");
    //verify jump already set for from ruleset
    $qry="SELECT rj.*, ri1.name AS from_name, ri2.name AS to_name ";
    $qry.="FROM `cw_rule_jump` AS rj ";
    $qry.="LEFT JOIN `cw_rule` AS ri1 ON rj.`ruleset_from` = ri1.`id` ";
    $qry.="LEFT JOIN `cw_rule` AS ri2 ON rj.`ruleset_to` = ri2.`id` ";
    $qry.="WHERE rj.`ruleset_from`='".$from."' ";
    $res=$db->Execute($qry);
    if($res!==false){
      $q=$res->RecordCount();
      //if another jump already exists for from ruleset delete it...
      if($q>0){
        $arr=$res->FetchRow();
        //verify if the jump that already exists is valid
        $qry="SELECT * FROM `cw_rule_jump` WHERE id='".$arr["id"]."' AND NOW()<'".$arr["end_of_life"]."'";
        $res=$db->Execute($qry);
        if($res!==false){
          $q=$res->RecordCount();
          //Other jump is valid
          if($q>0){
            agiError("  -> DELETE VALID JUMP: FROM [".$arr["ruleset_from"]."]".$arr["from_name"]." TO [".$arr["ruleset_to"]."]".$arr["to_name"]." (UNTIL ".$arr["end_of_life"].")");
            $qry="DELETE FROM `cw_rule_jump` WHERE ruleset_from='".$arr["ruleset_from"]."' ";
            $res=$db->Execute($qry);
            if($res!==false){
              agiError("  -> JUMP REMOVED");
              $agi->stream_file("de-activated");
              return true;
            }else{
              agiError("  -> ERROR: unable to delete jump");
              $agi->stream_file("an-error-has-occured");
              return false;
            }
            $continue=false;
          }
          //else the jump is expired
          else{
            agiError("  -> DELETE EXPIRED JUMP: FROM [".$arr["ruleset_from"]."]".$arr["from_name"]." TO [".$arr["ruleset_to"]."]".$arr["to_name"]." (UNTIL ".$arr["end_of_life"].")");
            $qry="DELETE FROM `cw_rule_jump` WHERE ruleset_from='".$arr["ruleset_from"]."' ";
            $res=$db->Execute($qry);
            if($res===false){
              agiError("  -> ERROR: unable to delete jump");
              $agi->stream_file("an-error-has-occured");
              return false;
            }
            $continue=true;
          }
        }else{
          agiError("  -> ERROR: unable to verify if jump is expired");
          $agi->stream_file("an-error-has-occured");
          return false;
        }  
      }else $continue=true;
      //else insert the jump
      if($continue===true){
        $qry="INSERT INTO `cw_rule_jump`(`ruleset_from`,`ruleset_to`,`end_of_life`) ";
        $qry.="VALUES('".$from."','".$to."','".$eol."'); ";
        $res=$db->Execute($qry);
        if($res!==false){
          agiError("  -> JUMP ACTIVATED");
          $agi->stream_file("activated");
          return true;
        }else{
          agiError("  -> ERROR: unable to insert jump");
          $agi->stream_file("an-error-has-occured");
          return false;
        }
      }    
    }else{
      agiError("  -> ERROR: unable to verify if jump already exists");
      $agi->stream_file("an-error-has-occured");
      return false;
    }
  }
?>