<?php

//important

$db_mysql_database="pcloudco_psync_pcart";
$db_mysql_host="localhost";
$db_mysql_user="root";
$db_mysql_password="root";


$db_mysql_user="pcloudco_psync";
$db_mysql_password="gJnvas@1v07c";



$key_table_prefix="server";


//============Database Connection for MySQL  ============================
$dbconn = mysql_connect($db_mysql_host, $db_mysql_user, $db_mysql_password) or die("Could not connect to Database");
//============Select MySQL Database============================================
mysql_select_db($db_mysql_database) or die ("Database not exist");





if(($_REQUEST["u"]=="demo")and($_REQUEST["p"]==md5("demo")))
{
    
    //success
    
    $client_id=0;
    exit;
    
    
}
else
{
    
    
    //1. check portal
        $query_check_portal="SELECT * FROM pcloudco_portal.t_portal_customer ";
        $query_check_portal=$query_check_portal."WHERE cus_login_name ='".$_REQUEST["u"]."' AND cus_login_password ='".$_REQUEST["p"]."' ;";	
        $result_check_portal = mysqli_query($dbconn,$query_check_portal) or die("Checking failed- Err msg  ". mysqli_error($dbconn));

		if($line_check_portal=mysqli_fetch_array($result_check_portal,MYSQL_ASSOC))
		{
            
            //2. check subscription
            $query_subscription="select * from pcloudco_portal.t_portal_subscription_email_reporting
                                where sub2_customer_id = '".$line_check_portal["cus_id"]."'
                                and sub2_product_type = 'PCART'
                                and sub2_date_start <= '".date("Y-m-d")."' 
                                and sub2_date_end >= '".date("Y-m-d")."'
                                and sub2_status  = 'YES'
                                
                                ";
            $result_subscription = mysqli_query($dbconn,$query_subscription) or die("Checking failed- Err msg  ". mysqli_error($dbconn));
            if($line_subscription = mysqli_fetch_array($result_subscription,MYSQL_ASSOC))
            {
                        
        
        
        
                //3. check local userid  
                $query_check="SELECT * FROM t_".$key_table_prefix."_user ";
                $query_check=$query_check."WHERE usr_name='".$_REQUEST["u"]."' ";	
                $result_check = mysqli_query($dbconn,$query_check) or die("Checking failed- Err msg  ". mysqli_error($dbconn));
                if($line=mysqli_fetch_array($result_check,MYSQL_ASSOC))
                {
                    $query_update="update t_".$key_table_prefix."_user
                                set
                                usr_full_name='".$line_check_portal["cus_name"]."',
                                usr_password='".$line_check_portal["cus_login_password"]."',
                                usr_group_id= 1,
                                usr_status_id = 1,
                                usr_last_login_date='".date("Y-m-d H:i:s")."',
                                modified_date = '".date("Y-m-d H:i:s")."'
                                where usr_name ='".$line_check_portal["cus_login_name"]."'
                                
                                ";
                    $result_update = mysqli_query($dbconn,$query_update) or die("Error Update:".mysqli_error($dbconn));
                    $usr_id = $line["usr_id"];
                 
                }
                else
                {
                    
                    $query_insert="insert into t_".$key_table_prefix."_user
                                (
                                usr_full_name,
                                usr_name,
                                usr_password,
                                usr_group_id,
                                usr_status_id,
                                usr_last_login_date,
                                created_date,
                                modified_date
                                )
                                values
                                (
                                '".$line_check_portal["cus_name"]."',
                                '".$line_check_portal["cus_login_name"]."',
                                '".$line_check_portal["cus_login_password"]."',
                                '1',
                                '1',    
                                '".date("Y-m-d H:i:s")."',
                                '".date("Y-m-d H:i:s")."',
                                '".date("Y-m-d H:i:s")."'
                                );
                                ";
                    $result_insert = mysqli_query($dbconn,$query_insert) or die("Error Insert: ".mysqli_error($dbconn));
                    $usr_id = mysqli_insert_id($dbconn);
                    
                }
                
                
                
                
                    
            }
            else
            {
                $login_status="FAIL";
                $message="No Active Subscription!";
                
                
        
   
                                                    
                echo "<email>
                ";
                echo "<status>".$login_status."</status>
                ";
                
                echo "<message>".str_replace("&","&amp;",$message)."</message>
                ";
                
                
                echo "</email>
                ";
                exit;
            
            }
                
                
        
        }
        else
        {
            $login_status="FAIL";
            $message="Invalid login information!";
                
            
            
            
                    
                                                 
             echo "<email>
             ";
             echo "<status>".$login_status."</status>
             ";
             
             echo "<message>".str_replace("&","&amp;",$message)."</message>
             ";
             
             
             echo "</email>
             ";
             exit;
            
            
        }             
    
}





?>