<commit_before><?php

if (!function_exists('curl_init')) {
    throw new Exception("cURL library not available");
}

if (!function_exists('hash_hmac')) {
    throw new Exception("hash_hmac function not available");
}


class OAuthRequest
{
    protected $curl;
    protected $consumerKey;
    protected $consumerSecret;
    protected $tokenSecret;
    protected $returnHeaders = array();
    protected $signatureMethod = 'HMAC-SHA1';

	protected function buildQuery(array $parameters) {

		if(empty($parameters)) return '';

		// encode the keys
		$keys = self::urlencode(array_keys($parameters));

		// encode the values
		$values = self::urlencode(array_values($parameters));

		// combine the key/value array
		$parameters = array_combine($keys, $values);

		// sort parameters as required by oauth
		uksort($parameters, 'strcmp');

		$params = array();
		foreach($parameters as $key => $value) {
			// sort by value
			if (is_array($value)) {
			    $value = natsort($value);
			}
		    $params[] = $key .'='. str_replace('%25', '%', $value);
		}
		
		// return
		return implode('&', $params);
	}

	protected function calculateHeader($url, $parameters) {

		// init var
		$params = array();

		// encode each parameter
		foreach($parameters as $key => $value) {
		    $params[] = self::urlencode($key) .'="'. self::urlencode($value) .'"';
		}

		// build return
		$return = 'Authorization: OAuth ' . implode(',', $params);

		return $return;
	}

    /* Builds the base string according to 3.4.1 of RFC 5849 */
	protected function calculateBaseString($method, $url, $parameters) {

		$parameters = is_array($parameters) ? $parameters : array();

		// init var
		$pairs = array();
		$params = array();

		// sort parameters by key
		uksort($parameters, 'strcmp');

		foreach($parameters as $key => $value) {
			// sort by value
			if(is_array($value)) { 
			    $value = natsort($value);
            }

			$params[] = self::urlencode($key) .'='. self::urlencode($value);
		}
		
		// builds base
		$parts = array(
		    strtoupper($method),
		    $url,
		    implode('&', $params)
        );
        
        $parts = self::urlencode($parts);
        $base = implode('&', $parts);
        return $base;
	}

    /* Encodes urls. This attempts to conform to 3.6 of RFC 5849 
       If there is a problem with an OAuth provider, likely it's going to be here 
    */
	protected static function urlencode($value) {
		if (is_array($value)) {
		    return array_map(array(__CLASS__, 'urlencode'), $value);
		}

        return str_replace('+',' ', str_replace('%7E', '~', rawurlencode($value)));
	}

    /* sign the request according to 3.1 of RFC 5849 */
	protected function oauthSignature($method, $url, $parameters) {
		// calculate the base string
		$baseString = $this->calculateBaseString($method, $url, $parameters);
		$key = rawurlencode($this->consumerSecret) .'&' . rawurlencode($this->tokenSecret);
		$sig = base64_encode(hash_hmac('SHA1', $baseString, $key, true));
		return $sig;
	}
	
	protected function baseURL($url)
	{
        $parts = parse_url($url);
    
        $scheme = (isset($parts['scheme'])) ? $parts['scheme'] : 'http';
        $port = (isset($parts['port'])) ? $parts['port'] : (($scheme == 'https') ? '443' : '80');
        $host = (isset($parts['host'])) ? $parts['host'] : '';
        $path = (isset($parts['path'])) ? $parts['path'] : '';
    
        if (($scheme == 'https' && $port != '443')
            || ($scheme == 'http' && $port != '80')) {
          $host = "$host:$port";
        }
        return "$scheme://$host$path";
	}
	
	protected function parseQueryString($queryString)
	{
	    $return = array();
	    $vars = explode('&', $queryString);
	    foreach ($vars as $value) {
	        $bits = explode("=", $value);
	        $return[$bits[0]] = $bits[1];
	    }
	    return $return;
	}
	
	public function setTokenSecret($tokenSecret)
	{
	    $this->tokenSecret = $tokenSecret;
	}

    /* public method to make an OAuth Request */
	public function request($method, $url, $parameters = null, $headers = null) {		
		$parameters = (array) $parameters;
		$options = array();
		$headers = (array) $headers;
		$curl_url = $url;
		$curl_headers = $headers;

		// append default parameters
		$oauth['oauth_consumer_key'] = $this->consumerKey;
		$oauth['oauth_nonce'] = md5(microtime() . rand());
		$oauth['oauth_signature_method'] = $this->signatureMethod;<commit_msg>added (untested) support for RSA-SHA1 signature methods<commit_after><?php

if (!function_exists('curl_init')) {
    throw new Exception("cURL library not available");
}

if (!function_exists('hash_hmac')) {
    throw new Exception("hash_hmac function not available");
}


class OAuthRequest
{
    protected $curl;
    protected $consumerKey;
    protected $consumerSecret;
    protected $tokenSecret;
    protected $cert;
    protected $returnHeaders = array();
    protected $signatureMethod = 'HMAC-SHA1';

	protected function buildQuery(array $parameters) {

		if(empty($parameters)) return '';

		// encode the keys
		$keys = self::urlencode(array_keys($parameters));

		// encode the values
		$values = self::urlencode(array_values($parameters));

		// combine the key/value array
		$parameters = array_combine($keys, $values);

		// sort parameters as required by oauth
		uksort($parameters, 'strcmp');

		$params = array();
		foreach($parameters as $key => $value) {
			// sort by value
			if (is_array($value)) {
			    $value = natsort($value);
			}
		    $params[] = $key .'='. str_replace('%25', '%', $value);
		}
		
		// return
		return implode('&', $params);
	}

	protected function calculateHeader($url, $parameters) {

		// init var
		$params = array();

		// encode each parameter
		foreach($parameters as $key => $value) {
		    $params[] = self::urlencode($key) .'="'. self::urlencode($value) .'"';
		}

		// build return
		$return = 'Authorization: OAuth ' . implode(',', $params);

		return $return;
	}

    /* Builds the base string according to 3.4.1 of RFC 5849 */
	protected function calculateBaseString($method, $url, $parameters) {

		$parameters = is_array($parameters) ? $parameters : array();

		// init var
		$pairs = array();
		$params = array();

		// sort parameters by key
		uksort($parameters, 'strcmp');

		foreach($parameters as $key => $value) {
			// sort by value
			if(is_array($value)) { 
			    $value = natsort($value);
            }

			$params[] = self::urlencode($key) .'='. self::urlencode($value);
		}
		
		// builds base
		$parts = array(
		    strtoupper($method),
		    $url,
		    implode('&', $params)
        );
        
        $parts = self::urlencode($parts);
        $base = implode('&', $parts);
        return $base;
	}

    /* Encodes urls. This attempts to conform to 3.6 of RFC 5849 
       If there is a problem with an OAuth provider, likely it's going to be here 
    */
	protected static function urlencode($value) {
		if (is_array($value)) {
		    return array_map(array(__CLASS__, 'urlencode'), $value);
		}

        return str_replace('+',' ', str_replace('%7E', '~', rawurlencode($value)));
	}

    /* sign the request according to 3.1 of RFC 5849 */
	protected function oauthSignature($method, $url, $parameters) {
		// calculate the base string
		$baseString = $this->calculateBaseString($method, $url, $parameters);
		$key = rawurlencode($this->consumerSecret) .'&' . rawurlencode($this->tokenSecret);
		
		switch ($this->signatureMethod)
		{
		    case 'HMAC-SHA1':
        		$sig = base64_encode(hash_hmac('SHA1', $baseString, $key, true));
        		break;
        	case 'RSA-SHA1':

                if (!$privatekeyid = openssl_get_privatekey($this->cert)) {
                    throw new Exception("Error getting private key for $this->cert");
                }

                // Sign using the key
                $ok = openssl_sign($base_string, $signature, $privatekeyid);

                // Release the key resource
                openssl_free_key($privatekeyid);

                $sig = base64_encode($signature);
        	    break;
        	default:
        	    throw new Exception("Signature method $this->signatureMethod not handled");
		}
		
		return $sig;
	}
	
	protected function baseURL($url) {
        $parts = parse_url($url);
    
        $scheme = (isset($parts['scheme'])) ? $parts['scheme'] : 'http';
        $port = (isset($parts['port'])) ? $parts['port'] : (($scheme == 'https') ? '443' : '80');
        $host = (isset($parts['host'])) ? $parts['host'] : '';
        $path = (isset($parts['path'])) ? $parts['path'] : '';
    
        if (($scheme == 'https' && $port != '443')
            || ($scheme == 'http' && $port != '80')) {
          $host = "$host:$port";
        }
        return "$scheme://$host$path";
	}
	
	protected function parseQueryString($queryString) {
	    $return = array();
	    $vars = explode('&', $queryString);
	    foreach ($vars as $value) {
	        $bits = explode("=", $value);
	        $return[$bits[0]] = $bits[1];
	    }
	    return $return;
	}
	
	public function setCertificate($cert) {
	    $this->cert = $cert;
	}
	
	public function setSignatureMethod($signatureMethod) {
	    if (!in_array($signatureMethod, array(
	        'HMAC-SHA1',
	        'RSA-SHA1'
            ))) {
            throw new Exception ("Invalid signature method $signatureMethod");
        }
        
        $this->signatureMethod = $signatureMethod;
	}
	
	public function setTokenSecret($tokenSecret) {
	    $this->tokenSecret = $tokenSecret;
	}

    /* public method to make an OAuth Request */
	public function request($method, $url, $parameters = null, $headers = null) {		
		$parameters = (array) $parameters;
		$options = array();
		$headers = (array) $headers;
		$curl_url = $url;
		$curl_headers = $headers;

		// append default parameters
		$oauth['oauth_consumer_key'] = $this->consumerKey;
		$oauth['oauth_nonce'] = md5(microtime() . rand());
		$oauth['oauth_signature_method'] = $this->signatureMethod;
{'fraction_non_alphanumeric': 0.16968781470292044, 'fraction_numerical': 0.008559919436052367, 'mean_word_length': 3.0012087026591456, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, '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': '3177353', 'n_tokens_mistral': 3379, 'n_tokens_neox': 3026, 'n_words': 749}