<?php
/**************************************************************************************
* Change the following value to the absolute path and url to your vision client portal
 **************************************************************************************
* Make sure there is a trailing slash at the very end.
**************************************************************************************/

define("VIS_PORTAL_PATH", "");
$hostName = "support.domain.com";
/***
* Additional constants for setup on another server
*
* define("VIS_DOMAIN_HIJAK","domain_added_in_manage");
* define("VIS_PATH_IMAGES","modules/support/vision3/vision3/design/images/");
* define("VIS_PATH_PROFILE","modules/support/vision3/vision3/awatar/");
*/

############ DO NOT CHANGE ANYTHING BELOW THIS LINE ############
global $smarty;
define("VIS_WORKING_DIR", getcwd());

define('_VIS_', 'Vision Helpdesk');
if (!empty($_SESSION['uid'])) {
    require_once ("./includes/clientfunctions.php");
    $GLOBALS['vis_whmcs_user']=getclientsdetails($_SESSION['uid']);
}


global $request_data ;
chdir(VIS_PORTAL_PATH);
ob_start();
require_once (VIS_PORTAL_PATH."config.php");
$path =  parse_url($_SERVER['HTTP_REFERER']);

$request_data['vis_email']=$GLOBALS['vis_whmcs_user']['email'];

$conn = mysqli_connect($database_host,$database_username,$database_password, $database_name);
$db = mysqli_select_db($conn,$database_name);

$domainSelectQuery =  mysqli_query($conn,"select domain_id,domain_url from ".$database_table_prefix."domain where domain ='".$hostName."'");
$arrDomainreult = mysqli_fetch_row($domainSelectQuery );
$domain_id = $arrDomainreult['0'];
$domainUrl =  $arrDomainreult['1'];
$filter = " email ='".$GLOBALS['vis_whmcs_user']['email']."' and domain_id =".$domain_id;
$query = mysqli_query($conn, "select salt from ".$database_table_prefix."client where ".$filter );

if(mysqli_num_rows($query) != 0 ){
    $result = mysqli_fetch_array($query);
}
else{
    
    $str = 'abcdefghijklmnopqrstuvwxyz0123456789';
    $shuffled = str_shuffle($str);
    $password=substr($shuffled, 0, 10);
    $user = explode('@', $GLOBALS['vis_whmcs_user']['email']);
    $user = $user[0];
    $qur = 'insert into '.$database_table_prefix.'client ( `group_id`, `first_name`, `last_name`, `user_name`, `email`, `authentication`, `salt`, `domain_id`, `harvest_id` ) values (  1,  "'.$user.'", "",    "'.$GLOBALS['vis_whmcs_user']['email'].'",  "'.$GLOBALS['vis_whmcs_user']['email'].'",  "'.$password.'",  MD5(RAND()),  '.$domain_id.',  0 )';
   
    $query = mysqli_query($conn, $qur);
    $query = mysqli_query($conn, "select salt from ".$database_table_prefix."client where ".$filter );
    $result = mysqli_fetch_array($query);
}


$request_data['vis_email']=$GLOBALS['vis_whmcs_user']['email'];
$request_data['vis_salt'] = 
mysqli_close($conn);
if($GLOBALS['vis_whmcs_user']=='' && empty($result))
{
      header('location:'.$domainUrl.'index.php?/whmcs'.$request_data['vis_action']);
}
else if(!empty($result))
{
      $request_data['vis_salt']='/'.$result[0];
      header('location:'.$domainUrl.'index.php?/whmcs'.$request_data['vis_action'].'/'.$GLOBALS['vis_whmcs_user']['email'].$request_data['vis_salt']);
}
else
{
      header('location:'.$domainUrl.'index.php?/whmcsuser'.$request_data['vis_action'].'/'.$GLOBALS['vis_whmcs_user']['email'].'/'.$GLOBALS['vis_whmcs_user']['password']);
}
?>