<?php

class Checkdata extends CI_Model{
        
    function checkInputData($var){                
        $theData = strip_tags(addslashes($var)); 
        return $theData;        
    }
   
    function checkInputDataDB($var){           
        $var = str_replace(array('"',"'","#","SELECT","ORDER","EXTRACTVALUE","CONCAT"),"",$var);
        
        $theData = strip_tags(addslashes($var)); 
        return $theData;        
    }
    
    function checkFilesData($var){                
        $theData = $var;
        return $theData;        
    } 
    
    function checkUploadedData($extension){                
       $extBlock = array("php", "asp", "php5", "aspx", "dll", "bat", "PIF", "MSI","BIN","CSH","KSH","OUT","RUN","htaccess");
       
       if (in_array(end($extension), $extBlock)) {
           return exit();
        }
    }
    function checkFileExt($extension){                
       $extBlock = array("php", "asp", "php5", "aspx", "dll", "bat", "PIF", "MSI","BIN","CSH","KSH","OUT","RUN","htaccess");
       
       if (in_array(end($extension), $extBlock)) {
           return false;
        }
        return true;
    }
    
    function isNotNumeric($var){                
        if(!is_numeric($var))
        $this->load->view("general/page_error");
    }
    
    function ifEmptyOrZero($var){                
        if(empty($var) || $var == 0){
              $this->load->view("general/forbidden");
        }       
    } 
    
    /* check if iam on class */
    function securityCheckIfStudentInThisClass($studentId,$classId){

            $this->isNotNumeric($classId);
            $this->isNotNumeric($studentId);
            $this->ifEmptyOrZero($studentId);          
            
        if($classId != 0){
            $query = $this->db->query("SELECT  clId FROM classes WHERE clId = $classId AND schoolId = ".$this->session->userdata("schoolId")." AND students LIKE '%\"$studentId\"%' ")->result_object();
            if(empty($query)){
                $this->load->view("general/forbidden");
                exit();            
            }            
        }
    }
    
    function checkIfTheTeacherMakeThisClass($teachertId,$classId,$memberType){

            $this->isNotNumeric($classId);
            $this->ifEmptyOrZero($classId);
            $this->isNotNumeric($teachertId);
            $this->ifEmptyOrZero($teachertId);          
            
            if($memberType == 'teacher'){
                $query = $this->db->query("SELECT  clId FROM classes WHERE clId = $classId AND teacherId = $teachertId AND schoolId = ".$this->session->userdata("schoolId")."")->result_object();
                if(empty($query)){
                    $this->load->view("general/forbidden");
                    exit();            
                }                    
            }else{
                $query = $this->db->query("SELECT  clId FROM classes WHERE schoolId = ".$this->session->userdata("schoolId")." ")->result_object();
                if(empty($query)){
                    $this->load->view("general/forbidden");
                    exit();            
                }
            }
            
    } 
    
    function checkIfMemberInMySchool($memberId){

            $this->isNotNumeric($memberId);
            $this->ifEmptyOrZero($memberId);
            
            $query = $this->db->query("SELECT  meId FROM members WHERE meId = $memberId AND schoolId = ".$this->session->userdata("schoolId")."")->result_object();
            if(empty($query)){
                $this->load->view("general/forbidden");
                exit();            
            }   
            
    }      
    
}  

?>
