<?php
if(!defined('_VIS_')) die('Error: Call from undefined function');
class client_portal {
    var $error;

    function home($operation) {
        global $vis_language;
        $database=new class_database();
        include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'selfhelp.php');
        $object=new class_selfhelp($database);
        list($recent_articles_info, $selfhelp)=$object->get_article('is_stick=0 and domain_id='.VIS_DOMAIN, VIS_DOMAIN, VIS_LOCATION_AREA);
        for($i=0;$i<count($selfhelp);$i++) {
			if($selfhelp[$i]['display_order_type']==0)
			$order=VIS_TABLE_PREFIX.'selfhelp_article.entry_date '.DESC .','.VIS_TABLE_PREFIX.'selfhelp_article.article_id '. DESC;
			else if($selfhelp[$i]['display_order_type']==1)
			$order=VIS_TABLE_PREFIX.'selfhelp_article.entry_date '.ASC.','.VIS_TABLE_PREFIX.'selfhelp_article.article_id '. DESC;
			else if($selfhelp[$i]['display_order_type']==2)
			$order=VIS_TABLE_PREFIX.'selfhelp_article.modified_date '.DESC.','.VIS_TABLE_PREFIX.'selfhelp_article.article_id '. DESC;
			list($recent_articles_info, $recent_articles)=$object->get_article('('.VIS_TABLE_PREFIX.'selfhelp_category.display_status=1 or '.VIS_TABLE_PREFIX.'selfhelp_category.display_status=0) and '.VIS_TABLE_PREFIX.'selfhelp_category.is_enabled=1 and '.VIS_TABLE_PREFIX.'selfhelp_article.is_enabled=1 and '.VIS_TABLE_PREFIX.'selfhelp_category.domain_id='.VIS_DOMAIN, VIS_STAFF_ID, VIS_LOCATION_AREA,'0,5',$order);
		}
		list($featured_articles_info, $selfhelp)=$object->get_article('is_stick=1 and domain_id='.VIS_DOMAIN, VIS_DOMAIN, VIS_LOCATION_AREA);
        for($i=0;$i<count($selfhelp);$i++) {
			if($selfhelp[$i]['display_order_type']==0)
			$order=VIS_TABLE_PREFIX.'selfhelp_article.entry_date '.DESC .','.VIS_TABLE_PREFIX.'selfhelp_article.article_id '. DESC;
			else if($selfhelp[$i]['display_order_type']==1)
			$order=VIS_TABLE_PREFIX.'selfhelp_article.entry_date '.ASC.','.VIS_TABLE_PREFIX.'selfhelp_article.article_id '. DESC;
			else if($selfhelp[$i]['display_order_type']==2)
			$order=VIS_TABLE_PREFIX.'selfhelp_article.modified_date '.DESC.','.VIS_TABLE_PREFIX.'selfhelp_article.article_id '. DESC;
			list($featured_articles_info, $featured_articles)=$object->get_article('('.VIS_TABLE_PREFIX.'selfhelp_category.display_status=1 or '.VIS_TABLE_PREFIX.'selfhelp_category.display_status=0) and '.VIS_TABLE_PREFIX.'selfhelp_category.is_enabled=1 and is_stick=1 and '.VIS_TABLE_PREFIX.'selfhelp_article.is_enabled=1 and '.VIS_TABLE_PREFIX.'selfhelp_category.domain_id='.VIS_DOMAIN, VIS_STAFF_ID, VIS_LOCATION_AREA,'',$order);	
		} 
        if($selfhelp!=-1 || IS_LOGIN) {
            $header_title=$vis_language['TXTHOME'];
            include(VIS_PATH_TEMPLATE.'header.php');
            include(VIS_PATH_TEMPLATE.'sticky.php');
            include(VIS_PATH_TEMPLATE.'footer.php');
        }
        else {
            $header_title=$vis_language['TXTLOGIN'];
            include(VIS_PATH_TEMPLATE.'header.php');
            include(VIS_PATH_TEMPLATE.'login.php');
            include(VIS_PATH_TEMPLATE.'footer.php');
        }
        return true;
    }

    function login($operation) {
		global $vis_language;
        global $request_data;
		$breadcrumb['Home'] = 'index.php';  
		$breadcrumb[$vis_language['TXTLOGIN']] = ''; 
		$vis_title = $header_title=$vis_language['TXTLOGIN'];
        if(!IS_LOGIN && VIS_HEAD_MENU_LOGIN==1){
		if(isset($operation['vis_forum_login']) && $operation['vis_forum_login']==1){
			include(VIS_PATH_TEMPLATE.'login.php');
		}
		else {
			include(VIS_PATH_TEMPLATE.'header.php');
			include(VIS_PATH_TEMPLATE.'login.php');
			include(VIS_PATH_TEMPLATE.'footer.php');
		} 
        }else $this->home($operation);
    }

    function forgot_password($operation) {
        global $vis_language;
        $breadcrumb['Home'] = 'index.php';  
		$breadcrumb[$vis_language['TXTFORGOTPASSWORD']] = ''; 
		$vis_title = $header_title=$vis_language['TXTFORGOTPASSWORD'];
        include(VIS_PATH_TEMPLATE.'header.php');
        include(VIS_PATH_TEMPLATE.'forgot_password.php');
        include(VIS_PATH_TEMPLATE.'footer.php');
        return true;
    }

    function reset_password($operation) {
        global $vis_language;
        $header_title=$vis_language['TXTFORGOTPASSWORD'];
        include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'client.php');
        include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'mail.php');
        $database=new class_database();
        $clobject=new class_client($database);
		 $vis_domain =  (VIS_SATELLITE_HELPDESK == 2 || VIS_SATELLITE_HELPDESK == 3) ? 1 : VIS_DOMAIN;
        list($clinfo, $client)=$clobject->get_client('email="'.$operation['vis_username'].'" and '.VIS_TABLE_PREFIX.'client.domain_id='.$vis_domain, '', 0, '', VIS_DOMAIN, VIS_LOCATION_AREA);        
        if($client==-1){
            $operation['message']='<div class="vis_error_message">'.$vis_language['MSGCLIENDEMLMISS'].'</div>';
            $this->forgot_password($operation);
            return true;
        }
        else {
            $mobject=new class_mail($database);
            $client=$client[0];
            $ret=$mobject->email_notify($client, 'send_password', VIS_STAFF_ID, VIS_LOCATION_AREA);
            $operation['message']='<div class="vis_message">'.$vis_language['MSGEMAILSENTTORESETPASSWORD'].'</div>';
            $operation['success']=1;
        }
        $database->database_disconnect();
        include(VIS_PATH_TEMPLATE.'header.php');
        include(VIS_PATH_TEMPLATE.'login.php');
        include(VIS_PATH_TEMPLATE.'footer.php');
        return true;
    }

    function check_login($operation){
        /*global $request_data;
        include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'client.php');
        $database=new class_database();
        $filter = ' (('.VIS_TABLE_PREFIX.'client.email="'.$request_data['vis_email'].'" and authentication="'.$request_data['vis_password'].'") or ('.VIS_TABLE_PREFIX.'client.user_name="'.$request_data['vis_email'].'" and authentication="'.$request_data['vis_password'].'")) ';
        $client_object=new class_client($database);
        list($client_count, $client_details) = $client_object->get_client($filter,'',0,'',0,1);
        if($client_details==0 || $client_details==-1)
            header('Location: index.php?vis_module=home&vis_operation=login&incorrect=password');
        else{
            //$session_id = $client_object->add_client_session($client_details[0]['client_id']);
            //setcookie(VIS_CLIENT_COOKIE, $client_details[0]['client_id'],time()+7200);
            //header('Location: index.php');
        }
        $database->database_disconnect();
        return true;*/
    }

    function change_password($operation) {
        global $request_data;
        $userid=(defined('VIS_CLIENT_ID'))?VIS_CLIENT_ID:VIS_STAFF_ID;
        if(VIS_LOCATION_AREA==1 && $userid==0){
             include(VIS_PATH_TEMPLATE.'header.php');
             include(VIS_PATH_TEMPLATE.'login.php');
             include(VIS_PATH_TEMPLATE.'footer.php');
             break;
         }
        include(VIS_PATH_TEMPLATE.'header.php');
        include(VIS_PATH_TEMPLATE.'change_password.php');
        include(VIS_PATH_TEMPLATE.'footer.php');
        return true;
    }

    function set_password($operation){
        include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'client.php');
        $database=new class_database();
        $client_object=new class_client($database);
       // $request_data['vis_client_id'] = $_COOKIE['vis_client_ld'];
        $client_details = $client_object->set_password($request_data, VIS_CLIENT_ID, VIS_LOCATION_AREA);
        $database->database_disconnect();
        if($client_details == 0 || $client_details == -1 ) $message = 0;
        if($client_details == 1) $message = 1;
        header('Location: index.php?vis_module=client&vis_operation=change_password&vis_message='.$message);
        return true;
    }

    /*function add_client($operation) {
        global $request_data;
        include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'client.php');
        $database=new class_database();
        $client_object=new class_client($database);
        $client_details = $client_object->add_client($request_data,0,1);
        $database->database_disconnect();
        if($client_details==0 || $client_details==-1) header('Location: index.php');
        return true;
    }

    /*function new_ticket($operation) {
        $database=new class_database();
        include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'ticket.php');
        $object=new class_ticket($database);
        $department_details=$object->get_departments_list($_COOKIE['vis_client_ld'], 1);
        $priority_details=$object->get_priorities_list($_COOKIE['vis_client_ld'], 1);
        $type_details=$object->get_type_list($_COOKIE['vis_client_ld'], 1);
        include(VIS_PATH_TEMPLATE.'header.php');
        include(VIS_PATH_TEMPLATE.'submit_ticket.php');
        include(VIS_PATH_TEMPLATE.'footer.php');
        $database->database_disconnect();
        return true;
    }

    function add_ticket($operation){
        global $request_data;
        $database=new class_database();
        include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'ticket.php');
        include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'client.php');
        $object=new class_ticket($database);
        $client_object = new class_client($database);
        $fields = 'first_name, last_name, email';
        $filter = 'client_id='.$_COOKIE['vis_client_ld'];
        $client_details = $client_object->get_client($filter,$fields,$_COOKIE['vis_client_ld'],1);
        $request_data['vis_from_name'] = $client_details['first_name'].' '.$client_details['last_name'];
        $request_data['vis_from_email']= $client_details['email'];
        $request_data['vis_domain'] = 1;
        $request_data['vis_status'] = 1;
        $request_data['vis_client'] = $_COOKIE['vis_client_ld'];

        $res = $object->add_ticket($request_data,$_COOKIE['vis_client_ld'],1);
        if($res == 0 || $res == -1 )
        header('Location: index.php?vis_module=client&vis_operation=new_ticket&error=invalid');
        else{
            include(VIS_PATH_TEMPLATE.'header.php');
            include(VIS_PATH_TEMPLATE.'view_tickets.php');
            include(VIS_PATH_TEMPLATE.'footer.php');
        }
        $database->database_disconnect();

        return true;
    }*/

    function logout($operation){
        global $request_data;
        include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'client.php');
        $database=new class_database();
        $client_object=new class_client($database);
        setcookie(VIS_CLIENT_COOKIE, '',time()-2);
        //$client_details = $client_object->delete_client_session($_COOKIE['vis_client_ld']);
        header('Location: index.php');
        return true;
    }
    function resend_verification_link($operation){
	    global $vis_language;
        $header_title=$vis_language['TXTRESENDVERIFICATIONLINK'];
        include(VIS_PATH_TEMPLATE.'header.php');
        include(VIS_PATH_TEMPLATE.'resend_verification.php');
        include(VIS_PATH_TEMPLATE.'footer.php');
        return true;
    }
    function resend_verification($operation)
    {
		global $vis_language;
		$database=new class_database();
		$header_title=$vis_language['TXTRESENDVERIFICATIONLINK'];
		include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'client.php');
		$object=new class_client($database);
		//$filter = VIS_TABLE_PREFIX.'client.client_id='.$operation['vis_client_id'];
        $client_details =$object->get_client('email="'.$operation['vis_username'].'" or additional_email="'.$operation['vis_username'].'" and '.VIS_TABLE_PREFIX.'client.domain_id='.VIS_DOMAIN, '', 0, '', VIS_DOMAIN, VIS_LOCATION_AREA,'',1);
		if($client_details[1]==-1){
            $operation['message']='<div class="vis_error_message">'.$vis_language['MSGCLIENDEMLMISS'].'</div>';
            $this->resend_verification_link($operation);
            return true;
        }
        else 
        { 
			$client_details = $client_details[1][0]; 
			
			if($client_details['is_verified']==0 || ($operation['vis_username']==$client_details['additional_email'] && $client_details['verify']==0))
			{
				include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'mail.php');
				$mobject=new class_mail($database);
				if($client_details['additional_email']==$operation['vis_username']){
				$client_details['email'] = $client_details['additional_email'];
				$client_details['is_verified'] = $client_details['verify'];
			    }
				$client_details['vis_module']='client';
				$client_details['vis_client_id'] = $client_details['client_id'];
				$client_details['vis_password']=password_decrypt($client_details['authentication'],VIS_PASSWORD_KEY);
				$client_details['vis_is_approved'] = $client_details['is_approved'];
				$client_details['vis_firstname'] = $client_details['first_name'];
				$client_details['vis_lastname'] = $client_details['last_name'];
				$client_details['vis_username'] = $client_details['user_name'];
				$client_details['vis_email'] =$client_details['email'];
				$ret=$mobject->email_notify($client_details, 'two_factor_auth', VIS_STAFF_ID, VIS_LOCATION_AREA); 
			    if($ret) 
			    {
					 $operation['message']='<div class="vis_message">'.$vis_language['TXTMSGEMAILSENDTOVERIFICATIONLINK'].'</div>';
					$operation['success']=1;
					$database->database_disconnect();
					include(VIS_PATH_TEMPLATE.'header.php');
					include(VIS_PATH_TEMPLATE.'login.php');
					include(VIS_PATH_TEMPLATE.'footer.php');
					return true;
				}
			}
          
        }
        
       
		
	}
}
?>
