<?php
require_once("$CFG->dirroot/local/badiunet/lib/cript.php");
require_once("$CFG->dirroot/local/badiunet/lib/pluginconfig.php");
require_once("$CFG->dirroot/local/badiunet/lib/util.php");
require_once("$CFG->dirroot/local/badiunet/fcservice/maccess.php");
class local_badiunet_templatecript {
    
    private $type=null;
    private $identify=0;
    private $cript = null;
    private $criptk1 = null;
    private $criptk2 = null;
    private $criptks = null;
    function __construct() {
         $this->cript = new local_badiunet_cript();
     }
    
    function decode($content) {
        $util = new local_badiunet_util();
      $p= explode("|",$content);
      $type=$util->getVlueOfArray($p, 0);
     
      $identify=$util->getVlueOfArray($p, 1);
      $this->identify=$identify;
      if(!empty($type)){
          $lenghscriptconf=strlen("$type|$identify|");
          $lenghscontent=strlen($content);
          $content=substr($content,$lenghscriptconf, $lenghscontent);  
          $ckey=$this->getCriptKey($type,$identify);
          $this->cript->setKey($ckey);
         
          $content=$this->cript->decode($content);
      }
      return $content;
    }
    function encode($type,$content,$identify=0) {
        if(empty($identify)){$identify=$this->identify;}
        $ckey=$this->getCriptKey($type,$identify);
        $this->cript->setKey($ckey);
        $content=$this->cript->encode($content);
        $content="$type|$identify|$content";
        return $content;
    }
    
    function getCriptKey($type,$identify=0) {
        $ckey=null;
        if($type=='s1'){
            $ckey= $this->cript->getKey();
        }else if($type=='k1'){
            if(!empty($this->criptk1)){$ckey=$this->criptk1;}
            else{ 
                $plugin=new local_badiunet_pluginconfig('local_badiunet');  
                $ckey= $plugin->getValue('criptk1');
                $ckey=base64_decode($ckey);
                $this->criptk1=$ckey;
            }
        }else if($type=='k2'){
            if(!empty($this->criptk2)){$ckey=$this->criptk2;}
            else{
                 $plugin=new local_badiunet_pluginconfig('local_badiunet');  
                $ckey= $plugin->getValue('criptk2');
                $ckey=base64_decode($ckey);
                 $this->criptk2=$ckey;
            }
           
        }else if($type=='ks'){
             if(!empty($this->criptks)){$ckey=$this->criptks;}
             else{
                 $maccess=new local_badiunet_maccess();
                 //$ckey=$maccess->getByKey('criptykey');
                 $ckey=$maccess->getCriptykey($identify);
                 $ckey=base64_decode($ckey);
                 $this->criptks=$ckey;
                
             }
           
        }
        
        return $ckey;
    }
    function getCriptyType($content=null) {
        if(empty($content)){$content=file_get_contents('php://input');}
        if(empty($content)){return null;}
        $util = new local_badiunet_util();
        $p= explode("|",$content);
        $type=$util->getVlueOfArray($p, 0);
        $identify=$util->getVlueOfArray($p, 1);
        $this->identify=$identify;
        return $type;
    }
    function getType() {
        return $this->type;
    }


    function getCript() {
        return $this->cript;
    }

    function setType($type) {
        $this->type = $type;
    }

    

    function setCript($cript) {
        $this->cript = $cript;
    }
  function getIdentify() {
        return $this->identify;
    }

    function setIdentify($identify) {
        $this->identify = $identify;
    }

    function getCriptk1() {
        return $this->criptk1;
    }

    function getCriptk2() {
        return $this->criptk2;
    }

    function getCriptks() {
        return $this->criptks;
    }

    function setCriptk1($criptk1) {
        $this->criptk1 = $criptk1;
    }

    function setCriptk2($criptk2) {
        $this->criptk2 = $criptk2;
    }

    function setCriptks($criptks) {
        $this->criptks = $criptks;
    }


}

?>
