<?php  
// Include main file
include_once('../libs/main.php');
// Check admin Login
include_once('checkLogin.php');

if(isset($_REQUEST['act']) && $_REQUEST['act']=='getcode' && $_REQUEST['goto_clientId'] != '' && $_REQUEST['goto_client_secret'] != ''){
    $client_id = trim($_REQUEST['goto_clientId']);
    $client_secret = trim($_REQUEST['goto_client_secret']);
    $redirect_url = SITE_URL_GOTO."x-admin/gotocode.php";
    $url = "https://authentication.logmeininc.com/oauth/authorize?client_id=".$client_id."&response_type=code&redirect_uri=".$redirect_url."";

    $where = array("setting_name="=>'xcare_default');
    $data = array("where"=>$where);
    $xcaredefultDetail = $settingObj->fromFields($data);
    $xcaredefultDetail =json_decode($xcaredefultDetail->setting_value,1);
    
    if(!empty($xcaredefultDetail)){
        
        if(isset($client_id) && $client_id != ''){
            $xcaredefultDetail['clientid']=$client_id;    
        }else{
            unset($xcaredefultDetail['clientid']);
        }

        if(isset($client_secret) && $client_secret != '')
        {
            $xcaredefultDetail['clientsecret']=$client_secret;    
        }else{
            unset($xcaredefultDetail['clientsecret']);
        }
        
        
        $data=array(
            'setting_value'=>json_encode($xcaredefultDetail),
        );
        $where_up = array('setting_name=' => 'XCARE_DEFAULT');
        
        $rs=$settingObj->updateFromFields($data,$where_up);
        
    }

   echo "<script type=\"text/javascript\">
            window.open('".$url."', '_blank')
        </script>";

    exit;
}

if(isset($_REQUEST['act']) && $_REQUEST['act']=='addcodetoken')
{
    
    
    $client_id = trim($_REQUEST['goto_clientId']);
    
    if(isset($_REQUEST['goto_code']) && $_REQUEST['goto_code'] != ''){
      $curl = curl_init();

      
      $client_secret = trim($_REQUEST['goto_client_secret']);
      $allclient_data = $client_id.":".$client_secret;
      $authorizationdata = base64_encode($allclient_data);
      
      $redirect_url = urlencode(SITE_URL_GOTO."x-admin/gotocode.php");
      $data = 'redirect_uri='.$redirect_url.'&grant_type=authorization_code&code='.$_REQUEST['goto_code'];
      $authorized_val = $authorizationdata;
      //$authorized_header = "authorization: Basic ".GOTO_HEADER_TOKEN;
      $authorized_header = "authorization: Basic ".$authorized_val;

      curl_setopt_array($curl, array(
        CURLOPT_URL => "https://authentication.logmeininc.com/oauth/token",
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_ENCODING => "",
        CURLOPT_MAXREDIRS => 10,
        CURLOPT_TIMEOUT => 30,
        CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
        CURLOPT_CUSTOMREQUEST => "POST",
        CURLOPT_POSTFIELDS => $data,
        CURLOPT_HTTPHEADER => array(
          $authorized_header,
          "cache-control: no-cache",
          "content-type: application/x-www-form-urlencoded"
        ),
      ));
      
      $response = curl_exec($curl);
      $err = curl_error($curl);
      curl_close($curl);
      
      if ($err) {
        echo "cURL Error #:" . $err;
      }else{
        $gotoresponse =  json_decode($response,1);
        //print_r($gotoresponse);exit;
        if(isset($gotoresponse['error']) && $gotoresponse['error'] != ''){
          echo $gotoresponse['error'].' => '.$gotoresponse['error_description'];
          $responseerror = $gotoresponse['error'].' and '.$gotoresponse['error_description'];
          $admin_detail .= " <b>Xcare Goto Setup Error </b>: ".$responseerror;
        }else{
            
            echo 'success';
            $where = array("setting_name="=>'XCARE_DEFAULT');
            $data = array("where"=>$where);
            $xcaredefultDetail = $settingObj->fromFields($data);
            $xcaredefultDetail =json_decode($xcaredefultDetail->setting_value,1);
            if(isset($gotoresponse['access_token']) && $gotoresponse['access_token'] != '')
            {
                $xcaredefultDetail['gotoaccess_token']=$gotoresponse['access_token'];
                
            }
            if(isset($gotoresponse['refresh_token']) && $gotoresponse['refresh_token'] != '')
            {
                $xcaredefultDetail['gotorefersh_token']=$gotoresponse['refresh_token'];
                
            }
            if(isset($authorized_val) && $authorized_val != '')
            {
                $xcaredefultDetail['gotoheaderauthorized']=$authorized_val;
                
            }
            $data=array(
                'setting_value'=>json_encode($xcaredefultDetail),
            );
            $where_up = array('setting_name=' => 'XCARE_DEFAULT');
            
            $rs=$settingObj->updateFromFields($data,$where_up);
         }
      }
      exit;
    }
    exit;
}

$where = array("setting_name="=>'XCARE_DEFAULT');
$data = array("where"=>$where);
$xcaredefultDetail = $settingObj->fromFields($data);
$xcaredefultDetail =json_decode($xcaredefultDetail->setting_value,1);

// Include Template 
include_once('../libs/routs.php');