<commit_before><?php
require('soap-wsse.php');
require('JWT.php');

class ET_Client extends SoapClient {
	public $authToken;
	private $authTokenExpiration, $internalAuthToken, $wsdlLoc,
			$lastHTTPCode, $clientId, $clientSecret, $appsignature, $endpoint, $refreshKey;
		
	function __construct($getWSDL = false, $params = null) {	
		$config = include 'config.php';
		$this->wsdlLoc = $config['defaultwsdl'];
		$this->clientId = $config['clientid'];
		$this->clientSecret = $config['clientsecret'];
		$this->appsignature = $config['appsignature'];		

		if ($getWSDL){
			$this->CreateWSDL($this->wsdlLoc);	
		}
		
		if ($params && array_key_exists('jwt', $params)){
			$decodedJWT = JWT::decode($params['jwt'], $this->appsignature);					
			$this->authToken = $decodedJWT->request->user->oauthToken;			
			$this->internalAuthToken = $decodedJWT->request->user->internalOauthToken;
			$dv = new DateInterval('PT'.$decodedJWT->request->user->expiresIn.'S');
			$newexpTime = new DateTime();
			$this->authTokenExpiration = $newexpTime->add($dv);	
			$this->refreshKey = $decodedJWT->request->user->refreshToken;
		}		
		
		$this->refreshToken();

		try {
			$url = "https://www.exacttargetapis.com//platform/v1/endpoints/soap?access_token=".$this->authToken;
			$endpointResponse = restGet($url);			
			$endpointObject = json_decode($endpointResponse->body);			
			if ($endpointResponse && property_exists($endpointObject,"url")){		
				$this->endpoint = $endpointObject->url;			
			} else {
				throw new Exception('Unable to determine stack using /platform/v1/tokenContext:'.$endpointResponse->body);			
			}
			} catch (Exception $e) {
			throw new Exception('Unable to determine stack using /platform/v1/tokenContext: '.$e->getMessage());
		} 		
		parent::__construct('ExactTargetWSDL.xml', array('trace'=>1, 'exceptions'=>0));
		parent::__setLocation($this->endpoint);
	}
	
	function refreshToken() {
		try {							
			$currentTime = new DateTime();
			if (is_null($this->authToken) || ($currentTime->diff($this->authTokenExpiration)->format('%i') < 5) ){
				$url = "https://auth.exacttargetapis.com/v1/requestToken?legacy=1";
				$jsonRequest = new stdClass(); 
				$jsonRequest->clientId = $this->clientId;
				$jsonRequest->clientSecret = $this->clientSecret;	
				$jsonRequest->accessType = "offline";				
				if (!is_null($this->refreshKey)){
					$jsonRequest->refreshToken = $this->refreshKey;
				}			
				
				$authResponse = restPost($url, json_encode($jsonRequest));
				$authObject = json_decode($authResponse->body);
				
				if ($authResponse && property_exists($authObject,"accessToken")){		
					
					$this->authToken = $authObject->accessToken;
					$this->internalAuthToken = $authObject->legacyToken;
					$dv = new DateInterval('PT'.$authObject->expiresIn.'S');
					$newexpTime = new DateTime();
					$this->authTokenExpiration = $newexpTime->add($dv);	
					$this->refreshKey = $authObject->refreshToken;
				} else {
					throw new Exception('Unable to validate App Keys(ClientID/ClientSecret) provided, requestToken response:'.$authResponse );			
				}				
			}
		} catch (Exception $e) {<commit_msg>Fixed issue with Refreshing LegacyTokens<commit_after><?php
require('soap-wsse.php');
require('JWT.php');

class ET_Client extends SoapClient {
	public $authToken;
	private $authTokenExpiration, $internalAuthToken, $wsdlLoc,
			$lastHTTPCode, $clientId, $clientSecret, $appsignature, $endpoint, $refreshKey;
		
	function __construct($getWSDL = false, $params = null) {	
		$config = include 'config.php';
		$this->wsdlLoc = $config['defaultwsdl'];
		$this->clientId = $config['clientid'];
		$this->clientSecret = $config['clientsecret'];
		$this->appsignature = $config['appsignature'];		

		if ($getWSDL){
			$this->CreateWSDL($this->wsdlLoc);	
		}
		
		if ($params && array_key_exists('jwt', $params)){
			$decodedJWT = JWT::decode($params['jwt'], $this->appsignature);					
			$this->authToken = $decodedJWT->request->user->oauthToken;			
			$this->internalAuthToken = $decodedJWT->request->user->internalOauthToken;
			$dv = new DateInterval('PT'.$decodedJWT->request->user->expiresIn.'S');
			$newexpTime = new DateTime();
			$this->authTokenExpiration = $newexpTime->add($dv);	
			$this->refreshKey = $decodedJWT->request->user->refreshToken;
		}		
		
		$this->refreshToken();

		try {
			$url = "https://www.exacttargetapis.com//platform/v1/endpoints/soap?access_token=".$this->authToken;
			$endpointResponse = restGet($url);			
			$endpointObject = json_decode($endpointResponse->body);			
			if ($endpointResponse && property_exists($endpointObject,"url")){		
				$this->endpoint = $endpointObject->url;			
			} else {
				throw new Exception('Unable to determine stack using /platform/v1/tokenContext:'.$endpointResponse->body);			
			}
			} catch (Exception $e) {
			throw new Exception('Unable to determine stack using /platform/v1/tokenContext: '.$e->getMessage());
		} 		
		parent::__construct('ExactTargetWSDL.xml', array('trace'=>1, 'exceptions'=>0));
		parent::__setLocation($this->endpoint);
	}
	
	function refreshToken($forceRefresh = true) {
		try {							
			$currentTime = new DateTime();
			if (is_null($this->authToken) || ($currentTime->diff($this->authTokenExpiration)->format('%i') < 5) || $forceRefresh ){
				$url = "https://auth.exacttargetapis.com/v1/requestToken?legacy=1";
				$jsonRequest = new stdClass(); 
				$jsonRequest->clientId = $this->clientId;
				$jsonRequest->clientSecret = $this->clientSecret;
				$jsonRequest->accessType = "offline";
				$jsonRequest->scope = "cas:".$this->internalAuthToken;
				if (!is_null($this->refreshKey)){
					$jsonRequest->refreshToken = $this->refreshKey;
				}			
				$authResponse = restPost($url, json_encode($jsonRequest));
				$authObject = json_decode($authResponse->body);
				
				if ($authResponse && property_exists($authObject,"accessToken")){		
					
					$this->authToken = $authObject->accessToken;
					$this->internalAuthToken = $authObject->legacyToken;
					$dv = new DateInterval('PT'.$authObject->expiresIn.'S');
					$newexpTime = new DateTime();
					$this->authTokenExpiration = $newexpTime->add($dv);	
					$this->refreshKey = $authObject->refreshToken;
				} else {
					throw new Exception('Unable to validate App Keys(ClientID/ClientSecret) provided, requestToken response:'.$authResponse );			
				}				
			}
		} catch (Exception $e) {
{'fraction_non_alphanumeric': 0.17098445595854922, 'fraction_numerical': 0.0025121683152771234, 'mean_word_length': 4.375527426160337, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 117, 'https://': 4, 'lorem ipsum': 0, 'www.': 2, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '859441', 'n_tokens_mistral': 2392, 'n_tokens_neox': 2017, 'n_words': 324}