<?php
//session_start();
// if($_SESSION['user']=='')  {
// echo "<script>window.location.href='https://prs.cryoliteindia.com/login.php';</script>";
// }

date_default_timezone_set("asia/kolkata");
class Initialize
{
    private $host = "localhost";
    private $db_name = "r050822a_rcs";
    private $username = "r050822a_rcs";
    private $password = "r050822a_rcs";
    public $con;
    public $leads;

    public function getConnection()
    {
        $this->conn = '';

        try 
        {
            //$attrs = array(PDO::ATTR_PERSISTENT => true);
            //$this->conn = new PDO("mysql:host=$this->host;dbname=$this->db_name",$this->username,$this->password,$attrs);
            //CREATE DEFINER = root@localhost FUNCTION fnc_calcWalkedDistance;
            
            $this->conn = new PDO("mysql:host=$this->host;dbname=$this->db_name",$this->username,$this->password,array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET sql_mode="TRADITIONAL"'));
            $this->conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
			$this->conn->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
            if (!$this->conn) {
                echo "Failed to connect to Database";
                exit();
            } else {
                return $this->conn;
                //return "Conected";
            }
            
        } catch (Exception $exception) {
            echo "Connection error in PDO: " . $exception->getMessage();
        }
    }
    
    public function cors() 
    {

        // Allow from any origin
        if (isset($_SERVER['HTTP_ORIGIN'])) {
            // Decide if the origin in $_SERVER['HTTP_ORIGIN'] is one
            // you want to allow, and if so:
            header("Access-Control-Allow-Origin: {$_SERVER['HTTP_ORIGIN']}");
            header('Access-Control-Allow-Credentials: true');
            header('Access-Control-Max-Age: 86400');    // cache for 1 day
        }
    
        // Access-Control headers are received during OPTIONS requests
        if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {
    
            if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_METHOD']))
                // may also be using PUT, PATCH, HEAD etc
                header("Access-Control-Allow-Methods: GET, POST, OPTIONS");         
    
            if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']))
                header("Access-Control-Allow-Headers: {$_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']}");
    
            exit(0);
        }
    }
    // 212.71.250.201:2083
    // 1Ei6BTAldiXKOZ
    // a190112c
    public function get_client_ip()
    {
        $ipaddress = '';
        if (isset($_SERVER['HTTP_CLIENT_IP'])) {
            $ipaddress = $_SERVER['HTTP_CLIENT_IP'];
        } else if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
            $ipaddress = $_SERVER['HTTP_X_FORWARDED_FOR'];
        } else if (isset($_SERVER['HTTP_X_FORWARDED'])) {
            $ipaddress = $_SERVER['HTTP_X_FORWARDED'];
        } else if (isset($_SERVER['HTTP_FORWARDED_FOR'])) {
            $ipaddress = $_SERVER['HTTP_FORWARDED_FOR'];
        } else if (isset($_SERVER['HTTP_FORWARDED'])) {
            $ipaddress = $_SERVER['HTTP_FORWARDED'];
        } else if (isset($_SERVER['REMOTE_ADDR'])) {
            $ipaddress = $_SERVER['REMOTE_ADDR'];
        } else {
            $ipaddress = 'UNKNOWN';
        }

        return $ipaddress;
    }

    public function getToken($length){
        $token = "";
        $codeAlphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        $codeAlphabet.= "abcdefghijklmnopqrstuvwxyz";
        $codeAlphabet.= "0123456789";
        $codeAlphabet.= "@#$%&*()";
        $max = strlen($codeAlphabet);
    
        for ($i=0; $i < $length; $i++) {
            //$token .= $codeAlphabet[random_int(0, $max-1)];
        }
    
        return $token;
    }

    public function usm_tns_page_access_log($company_id,$session_id,$user_id,$page_id,$date)
    {
        try {
            $sql = "INSERT INTO usm_tns_page_access_log (fk_cnf_company_id, fk_cnf_session_id, fk_usm_user_id, fk_usm_page_id, access_date)
            VALUES ('".$company_id."', '".$session_id."', '".$user_id."', '".$page_id."', '".$date."')";
            $this->conn->exec($sql);
          } catch(PDOException $e) {
            echo $sql . "<br>" . $e->getMessage();
          }
    }
    public function usm_tns_activity_log($activity_id,$company_id,$session_id,$user_id,$page_id,$date)
    {
        try {
            $sql = "INSERT INTO usm_tns_activity_log (fk_cnf_company_id, fk_cnf_session_id, fk_usm_user_id, fk_usm_page_id,fk_usm_activitytype_id, activity_date)
            VALUES ('".$company_id."', '".$session_id."', '".$user_id."', '".$page_id."', '".$activity_id."', '".$date."')";
            $this->conn->exec($sql);
          } catch(PDOException $e) {
            echo $sql . "<br>" . $e->getMessage();
          }
    }

   public function usm_cnf_application_controller($id)
    {
        try {
            $Check_ERP_Status = $this->conn->prepare('SELECT application_status from usm_cnf_application_controller where application_type = :value and application_status = :status');
            if ($Check_ERP_Status->execute(array(':value' => 'ERP',':status' => '1')))
            {
                $Check_ERP_Status_row = $Check_ERP_Status->rowCount();
                if($Check_ERP_Status_row == 0)
                {
                    // if($id != 'COM1SAD001')
                    $id = strtoupper($id);
                    if(strcmp($id,'COM1SAD001') !=0)
                    {
                        echo "<script>window.location.href='https://prs.cryoliteindia.com/error1.html';</script>";
                    }
                }
            }
          } catch (Exception $exception) {
            echo "Connection error: " . $exception->getMessage();
        }
    }
   public function unv_active_block_data($id,$clid,$table,$column,$status)
    {
        try {
            $query="update $table set $column=:status where $clid=:id";
            $update_query=$this->conn->prepare($query);
            $update_query->bindParam(':status',$status);
            $update_query->bindParam(':id',$id);
            $resut=$update_query->execute();
			if ($resut) return 1; else return 0;
          } catch(PDOException $e) {
            echo $query . "<br>" . $e->getMessage();
          }
    } 
    //**************************************************************************************************************************
    //*********************************************************USM**************************************************************
    public function usm_user_get_com_by_rights($con,$id,$com)
    {
        try 
        {
            $sql = "SELECT a.* FROM cnf_mst_company a LEFT JOIN usm_tns_company_rights b on a.pk_cnf_company_id=b.fk_cnf_company_id WHERE b.fk_usm_user_id = '".$id."' and a.company_Status = '1'  and a.pk_cnf_company_id = '".$com."' ";
            $stmt = $con->prepare($sql);
            $stmt->execute();
            if ($stmt->rowCount() > 0) {
                // $data = $stmt->fetchAll();
                // return $data;
                return $stmt->rowCount();
            }
            else {
                return 0;
            }
            
        } catch (Exception $exception) {
            echo "Execution error: " . $exception->getMessage();
        }
    }
    //**************************************************************************************************************************
    //******************************************************Accessories*********************************************************
    
    public function select_ent_cnf_individual_type_by_id($id)
    {
        try 
        {
            $fsql=$this->conn->prepare("select type_name from ent_cnf_individual_type where pk_ent_indtype_id=:var1");
            $fsql->bindParam(':var1',$id);
            $fsql->execute();
            $checks=$fsql->fetch(PDO::FETCH_ASSOC);
			return $checks['type_name'];
            
        } catch (Exception $exception) {
            echo "Execution error: " . $exception->getMessage();
        }
    }
    public function select_utm_mst_state_by_id($id)
    {
        if(empty($id) || $id == 'null' || $id == 'NA')
        {
            return 'NA';
        }
        else
        {
            try 
            {
                $fsql=$this->conn->prepare("select state_name from utm_mst_state where pk_utm_state_id=:var1");
                $fsql->bindParam(':var1',$id);
                $fsql->execute();
                $checks=$fsql->fetch(PDO::FETCH_ASSOC);
    			return $checks['state_name'];
                
            } catch (Exception $exception) {
                echo "Execution error: " . $exception->getMessage();
            }   
        }
    }
    public function select_utm_mst_districts_by_id($id)
    {
        if(empty($id) || $id == 'null' || $id == 'NA')
        {
            return 'NA';
        }
        else
        {
            try 
            {
                $fsql=$this->conn->prepare("select district_name from utm_mst_districts where pk_utm_district_id=:var1");
                $fsql->bindParam(':var1',$id);
                $fsql->execute();
                $checks=$fsql->fetch(PDO::FETCH_ASSOC);
    			return $checks['district_name'];
                
            } catch (Exception $exception) {
                echo "Execution error: " . $exception->getMessage();
            }    
        }
    }
    
    public function get_page_by_userID($con,$id,$mod)
    {
        try 
        {
            $sql = "SELECT a.* FROM usm_mst_pages a LEFT JOIN usm_tns_user_page_rights b on a.pk_usm_page_id=b.fk_usm_page_id WHERE b.fk_usm_user_id = '".$id."' and a.page_status = '1' AND a.fk_usm_module_id = '".$mod."' ORDER by a.page_sequence asc ";
            $stmt = $con->prepare($sql);
            $stmt->execute();
            if ($stmt->rowCount() > 0) {
                $data = $stmt->fetchAll();
                return $data;
            }
            else {
                return array();
            }
            
        } catch (Exception $exception) {
            echo "Execution error: " . $exception->getMessage();
        }
    }
    public function get_page_by_userID1($con,$id,$mod,$sub)
    {
        try 
        {
            $sql = "SELECT a.* FROM usm_mst_pages a LEFT JOIN usm_tns_user_page_rights b on a.pk_usm_page_id=b.fk_usm_page_id WHERE b.fk_usm_user_id = '".$id."' and a.page_status = '1' AND a.fk_usm_module_id = '".$mod."' AND a.fk_usm_submodule_id = '".$sub."' ORDER by a.page_sequence asc ";
            $stmt = $con->prepare($sql);
            $stmt->execute();
            if ($stmt->rowCount() > 0) {
                $data = $stmt->fetchAll();
                return $data;
            }
            else {
                return array();
            }
            
        } catch (Exception $exception) {
            echo "Execution error: " . $exception->getMessage();
        }
    }
	public function get_first_page_by_userID($con,$id,$mod,$sbmd)
    {
        try 
        {
            $sql = "SELECT a.actual_url,a.pk_usm_page_id FROM usm_mst_pages a LEFT JOIN usm_tns_user_page_rights b on a.pk_usm_page_id=b.fk_usm_page_id WHERE b.fk_usm_user_id = '".$id."' and a.page_status = '1' AND a.fk_usm_module_id = '".$mod."' AND a.fk_usm_submodule_id = '".$sbmd."' ORDER by a.page_sequence asc limit 1 ";
            $stmt = $con->prepare($sql);
            $stmt->execute();
            if ($stmt->rowCount() > 0) {
                $data = $stmt->fetch();
                return $data;
            }
            else {
                return array();
            }
            
        } catch (Exception $exception) {
            echo "Execution error: " . $exception->getMessage();
        }
    }
	public function get_submodue_page_by_userID($con,$id,$mod)
    {
        try 
        {
            $sql = "SELECT DISTINCT a.fk_usm_submodule_id, c.submodule_name,c.dashboard_url,c.icon_image_name FROM usm_mst_pages a LEFT JOIN usm_tns_user_page_rights b on a.pk_usm_page_id=b.fk_usm_page_id LEFT JOIN usm_mst_submodule c on c.pk_usm_submodule_id=a.fk_usm_submodule_id WHERE b.fk_usm_user_id = '".$id."' and a.page_status = '1' AND a.fk_usm_module_id = '".$mod."' and c.submodule_status=1 ";
            $stmt = $con->prepare($sql);
            $stmt->execute();
            if ($stmt->rowCount() > 0) {
                $data = $stmt->fetchAll();
                return $data;
            }
            else {
                return $stmt->rowCount();
            }
            
        } catch (Exception $exception) {
            echo "Execution error: " . $exception->getMessage();
        }
    }
 
}
$db = new Initialize();
$con = $db->getConnection();
$ip = $db->get_client_ip();
$db->cors();
$rnd_pass = $db->getToken(6);
//***********************************************************************************************************************************

//***********************************************************************************************************************************
/*ini_set('max_execution_time', 3000);
set_time_limit(3000);
ini_set('memory_limit', -1);
ini_set('post_max_size', '200M');
ini_set('upload_max_filesize', '200M'); */
?>