<?php

function passSid($pass,$so=""){
    return md5(sha1($pass).$so);
}

function pass_ell_login($pass_sid,$pass,$so=""){
    return ($pass_sid==passSid($pass,$so));
       
}


function pass_ell_reg($pass1,$pass2,$so=""){
    $pass="";
     if((!empty($pass1) && !empty($pass2)) && ($pass1==$pass2) && ((strlen($pass1)>=6))){
        $pass=passSid($pass1,$so);
     }
     return $pass;
       
}



class User{
    private $nev;
    private $email;
    private $id;
    private $rang;
    private $date;
    private $sex;
    private $szul_date;
    private $pass; 
    private $kep_id;
    private $zene_id;


    function __construct($nev,$email,$pass="",$id=-1,$date="",$szul_date="",$sex='-',$kep_id=3,$zene_id=0,$rang=false) {
       if(!empty($nev)&&  preg_match("/^[a-zA-ZáéíúűüőóöÁÉÍÚŰÜÖŐÓ0-9 _:.-]*$/u",$nev)){
        $this->nev= trim($nev);
       }else{
        $this->nev="";
       }
      
        if(!empty($email) && filter_var($email, FILTER_VALIDATE_EMAIL)){
            $this->email=$email;
           }else{
            $this->email="";
           }


           
            $this->pass=$pass;
           
       
           if( $id>=0) {
            $this->id=$id;
           }else{
            $this->id=id_generator(userrang_keres($rang,$GLOBALS['user_json']));
           }
        $this->rang=$rang;
        if(!empty($date)){
            $this->date=$date;
        }else{
            $this->date=date("Y-m-d");
        }


        
         $this->szul_date=$szul_date;
       

         if($sex=="Nem adom meg")
         {
            $this->sex ="-";
               
        }  
         else
         {
                                                     
            $this->sex =$sex; 
                                                           
         }

         $this->kep_id=$kep_id;
         $this->zene_id=$zene_id;
        
    }

    //get
    function get_nev(){
        return $this->nev;
    }
    function get_email(){
        return $this->email;
    }
    
    function get_rang(){
        return $this->rang;
    }
    function get_kepid(){
        return $this->kep_id;
    }
    function get_zeneid(){
        return $this->zene_id;
    }
    //get amihez nincs set
    function get_id(){
        return $this->id;
    }
    function get_regdate(){
        return $this->date;
    }
    function get_sex(){
        return $this->sex;
    }
    function get_szuldate(){
        return $this->szul_date;
    }

    
    //set
    function set_rang($rang,$uzenet_Json,$user_Json,$kep_Json,$barat_Json,$user_index,$mappa){
       
       $ujid=id_generator(userrang_keres($rang,$user_Json));
       while(report_index_keres($this->id)!=-1){
            $uzenet_Json->report[report_index_keres($this->id)]=null;
        }

        while(pchat_index_keres($this->id)!=-1){
            $uzenet_Json->pchat[pchat_index_keres($this->id)]=null;
        }

        file_put_contents($mappa."Json/uzenet.Json",json_encode( $uzenet_Json,JSON_PRETTY_PRINT));

        $regi_mappa="media/users/jatekosok/".$this->id;
        $uj_mappa="media/users/Adminok/".$ujid;
        rename($mappa.$regi_mappa,$mappa.$uj_mappa);

        foreach(pkepek(  $kep_Json,$this->id,$this->rang) as $i){
            if(  $kep_Json[kep_index_keres($i,  $kep_Json)]->cimke!="alap_profil"){
        
                $s1="jatekosok/".$this->id;
                $s2="Adminok/".$ujid;
                $file=str_replace($s1,$s2,($kep_Json[kep_index_keres($i,$kep_Json)]->url) );
        
                kep_athejez($kep_Json[kep_index_keres($i,  $kep_Json)]->id,$kep_Json,$file);
            }
        }

       ertekelo_jatekos_torol($this->id);

      
       $barat_inex=barat_index_keres($id);
        foreach($barat_Json[$barat_inex]-> baratokT as $i){
            for($j=0;$j<count($barat_Json[barat_index_keres($i)]-> baratokT);$j++){
                if($barat_Json[barat_index_keres($i)]-> baratokT[$j]==$id){
                    $barat_Json[barat_index_keres($i)]-> baratokT[$j]=-1;
                }
            }
        }

       file_put_contents($mappa."Json/barat.Json",json_encode( $barat_Json,JSON_PRETTY_PRINT));

        $this->rang=$rang;
        $this->id=$ujid;
        
       
    }

   function set_nev($nev){
       
        if(!empty($nev)&&  preg_match("/^[a-zA-ZáéíúűüőóöÁÉÍÚŰÜÖŐÓ0-9 _:.-]*$/u",$nev)){
            $this->nev= trim($nev);
           }
        
     }
     function set_email($email){
         if(!empty($email) && filter_var($email, FILTER_VALIDATE_EMAIL)){
             $this->email=$email;
            }
     }
   
     
     
     function set_kepid($id){
        $this->kep_id=$id;
     }

     function set_zeneid($id){
        $this->zene_id=$id;
     }

     //set amihez nincs get
     function set_pass($uj_pass1,$uj_pass2,$so){
        if(!empty($uj_pass1)){
         $this->pass=pass_ell_reg($uj_pass1,$uj_pass2,$so);
        }
      }
    
    //profil funkciok
    function rangsz()
    {
        if($this->rang){
            return "@";
        }
        return "#";
    }
    
    function profil(){
        return $this->nev." ".$this->rangsz().$this->id;
    }

    //reg/login funkciok
     function get_json(){
       
        return '{
            "id": '.$this->id.',
            "nev": "'.$this->nev.'",
            "jelszo": "'.$this->pass.'",
            "email": "'.$this->email.'",
            "rang_admin":  "'.$this->rang.'",
            "pkep_id": '.$this->kep_id.',
            "zene_id":'.$this->zene_id.',
            "nem": "'.$this->sex.'", 
            "szul_date": "'.$this->szul_date.'",
            "reg_date": "'.$this->date.'"
        }';
     }

     function hibak(){
        $hibak=[];
        if($this->nev==""){
            $hibak['nev']="A felhasznaló név nem megfelelö!";
        }

        if($this->email==""){
            $hibak['email']="Az email cim nem megfelelö!";
        }

        
        if($this->pass==""){
            $hibak['pass']="A jelszó nem megfelelö!";
        }

        

        return $hibak;
     }


     
     function reg(){

        array_push( $GLOBALS['user_json'], json_decode($this->get_json(),JSON_PRETTY_PRINT));
        if(!$this->rang){
            $barat_list='{
                "id": '.$this->id.',
                "baratokT": []
            }';
            array_push( $GLOBALS['barat_json'], json_decode($barat_list,JSON_PRETTY_PRINT));
            file_put_contents("Json/barat_list.Json",json_encode( $GLOBALS['barat_json'],JSON_PRETTY_PRINT));
        }
         file_put_contents("Json/user.Json",json_encode( $GLOBALS['user_json'],JSON_PRETTY_PRINT));
         $rm="jatekosok/";
         if($this->rang){
            $rm="Adminok/";
         }
         $m="media/users/".$rm.$this->id;
        if (!file_exists($m)) {
            mkdir($m, 0777, true);
        }
     }

     //save funkcio
     function save($Json,$index,$mappa=""){
       
        $Json[$index]->nev=$this->nev;
        $Json[$index]->email=$this->email;
        $Json[$index]->jelszo=$this->pass;
        $Json[$index]->pkep_id=$this->kep_id;
        $Json[$index]->zene_id=$this->zene_id;
        if($_SESSION['user']->get_rang()){
            $Json[$index]->rang_admin=$this->rang;
            $Json[$index]->id=$this->id;
        }

        file_put_contents($mappa."Json/user.Json",json_encode( $GLOBALS['user_json'],JSON_PRETTY_PRINT));


     }
    
    
}

?>