sync
authorAlan Knowles <alan@roojs.com>
Wed, 22 Apr 2020 04:36:59 +0000 (12:36 +0800)
committerAlan Knowles <alan@roojs.com>
Wed, 22 Apr 2020 04:36:59 +0000 (12:36 +0800)
Net/Gapi/OAuth2.php [deleted file]
Net/Gapi/Request.php [deleted file]

diff --git a/Net/Gapi/OAuth2.php b/Net/Gapi/OAuth2.php
deleted file mode 100644 (file)
index c6cc498..0000000
+++ /dev/null
@@ -1,116 +0,0 @@
-<?php
-/**
- * OAuth2 Google API authentication
- *
- */
-class Services_Gapi_OAuth2
-{
-    const scope_url = 'https://www.googleapis.com/auth/analytics.readonly'; // fixme...
-    const request_url = 'https://www.googleapis.com/oauth2/v3/token';
-    const grant_type = 'urn:ietf:params:oauth:grant-type:jwt-bearer';
-    const header_alg = 'RS256';
-    const header_typ = 'JWT';
-
-    private function base64URLEncode($data)
-    {
-        return rtrim(strtr(base64_encode($data), '+/', '-_'), '=');
-    }
-
-    private function base64URLDecode($data)
-    {
-        return base64_decode(str_pad(strtr($data, '-_', '+/'), strlen($data) % 4, '=', STR_PAD_RIGHT));
-    } 
-
-    /**
-     * Authenticate Google Account with OAuth2
-     *
-     * @param String $client_email
-     * @param String $key_file
-     * @param String $delegate_email
-     * @return String Authentication token
-     */
-    public function fetchToken($client_email, $key_file, $delegate_email = null)
-    {
-        $header = array(
-            "alg" => self::header_alg,
-            "typ" => self::header_typ,
-        );
-
-        $claimset = array(
-            "iss" => $client_email,
-            "scope" => self::scope_url,
-            "aud" => self::request_url,
-            "exp" => time() + (60 * 60),
-            "iat" => time(),
-        );
-
-        if(!empty($delegate_email)) {
-            $claimset["sub"] = $delegate_email;
-        }
-
-        $data = $this->base64URLEncode(json_encode($header)) . '.' . $this->base64URLEncode(json_encode($claimset));
-
-        if (!file_exists($key_file)) {
-            if ( !file_exists(__DIR__ . DIRECTORY_SEPARATOR . $key_file) ) {
-                throw new Exception('GAPI: Failed load key file "' . $key_file . '". File could not be found.');
-            } else {
-                $key_file = __DIR__ . DIRECTORY_SEPARATOR . $key_file;
-            }
-        }
-
-        $key_data = file_get_contents($key_file);
-        
-        if (empty($key_data)) {
-            throw new Exception('GAPI: Failed load key file "' . $key_file . '". File could not be opened or is empty.');
-        }
-
-        openssl_pkcs12_read($key_data, $certs, 'notasecret');
-
-        if (!isset($certs['pkey'])) {
-            throw new Exception('GAPI: Failed load key file "' . $key_file . '". Unable to load pkcs12 check if correct p12 format.');
-        }
-
-        openssl_sign($data, $signature, openssl_pkey_get_private($certs['pkey']), "sha256");
-
-        $post_variables = array(
-            'grant_type' => self::grant_type,
-            'assertion' => $data . '.' . $this->base64URLEncode($signature),
-        );
-
-        $url = new Services_Gapi_Request(self::request_url);
-        $response = $url->post(null, $post_variables);
-        $auth_token = json_decode($response['body'], true);
-
-        if (substr($response['code'], 0, 1) != '2' || !is_array($auth_token) || empty($auth_token['access_token'])) {
-            throw new Exception('GAPI: Failed to authenticate user. Error: "' . strip_tags($response['body']) . '"');
-        }
-
-        $this->auth_token = $auth_token['access_token'];
-
-        return $this->auth_token;
-    }
-
-    /**
-     * Return the auth token string retrieved from Google
-     *
-     * @return String
-     */
-    public function getToken()
-    {
-        return $this->auth_token;
-    }
-    
-    /**
-     * Generate authorization token header for all requests
-     *
-     * @param String $token
-     * @return Array
-     */
-    public function generateAuthHeader($token=null)
-    {
-        if ($token == null) {
-            $token = $this->auth_token;
-        }
-        return array('Authorization' => 'Bearer ' . $token);
-    }
-}
diff --git a/Net/Gapi/Request.php b/Net/Gapi/Request.php
deleted file mode 100644 (file)
index b78e5a9..0000000
+++ /dev/null
@@ -1,170 +0,0 @@
-<?php
-/**
- * Google Analytics API request
- *
- */
-class Services_Gapi_Request
-{
-    const http_interface = 'auto'; //'auto': autodetect, 'curl' or 'fopen'
-    //const interface_name = gapi::interface_name;
-
-    private $url = null;
-
-    public function __construct($url)
-    {
-        $this->url = $url;
-    }
-
-    /**
-     * Return the URL to be requested, optionally adding GET variables
-     *
-     * @param Array $get_variables
-     * @return String
-     */
-    public function getUrl($get_variables=null)
-    {
-        if (is_array($get_variables)) {
-            $get_variables = '?' . str_replace('&amp;', '&', urldecode(http_build_query($get_variables, '', '&')));
-        } else {
-            $get_variables = null;
-        }
-
-        return $this->url . $get_variables;
-    }
-
-    /**
-     * Perform http POST request
-     * 
-     *
-     * @param Array $get_variables
-     * @param Array $post_variables
-     * @param Array $headers
-     */
-    public function post($get_variables=null, $post_variables=null, $headers=null)
-    {
-        return $this->request($get_variables, $post_variables, $headers);
-    }
-
-    /**
-     * Perform http GET request
-     * 
-     *
-     * @param Array $get_variables
-     * @param Array $headers
-     */
-    public function get($get_variables=null, $headers=null)
-    {
-        return $this->request($get_variables, null, $headers);
-    }
-
-    /**
-     * Perform http request
-     * 
-     *
-     * @param Array $get_variables
-     * @param Array $post_variables
-     * @param Array $headers
-     */
-    public function request($get_variables=null, $post_variables=null, $headers=null)
-    {
-        $interface = self::http_interface;
-
-        if (self::http_interface == 'auto')
-            $interface = function_exists('curl_exec') ? 'curl' : 'fopen';
-
-        switch ($interface) {
-            case 'curl':
-                return $this->curlRequest($get_variables, $post_variables, $headers);
-            case 'fopen':
-                return $this->fopenRequest($get_variables, $post_variables, $headers);
-            default:
-                throw new Exception('Invalid http interface defined. No such interface "' . self::http_interface . '"');
-        }
-    }
-
-    /**
-     * HTTP request using PHP CURL functions
-     * Requires curl library installed and configured for PHP
-     * 
-     * @param Array $get_variables
-     * @param Array $post_variables
-     * @param Array $headers
-     */
-    private function curlRequest($get_variables=null, $post_variables=null, $headers=null)
-    {
-        $ch = curl_init();
-
-        if (is_array($get_variables)) {
-            $get_variables = '?' . str_replace('&amp;', '&', urldecode(http_build_query($get_variables, '', '&')));
-        } else {
-            $get_variables = null;
-        }
-
-        curl_setopt($ch, CURLOPT_URL, $this->url . $get_variables);
-        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
-        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); //CURL doesn't like google's cert
-
-        if (is_array($post_variables)) {
-            curl_setopt($ch, CURLOPT_POST, true);
-            curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post_variables, '', '&'));
-        }
-
-        if (is_array($headers)) {
-            $string_headers = array();
-            foreach ($headers as $key => $value) {
-                $string_headers[] = "$key: $value";
-            }
-            curl_setopt($ch, CURLOPT_HTTPHEADER, $string_headers);
-        }
-
-        $response = curl_exec($ch);
-        $code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
-
-        curl_close($ch);
-
-        return array('body' => $response, 'code' => $code);
-    }
-
-    /**
-     * HTTP request using native PHP fopen function
-     * Requires PHP openSSL
-     *
-     * @param Array $get_variables
-     * @param Array $post_variables
-     * @param Array $headers
-     */
-    private function fopenRequest($get_variables=null, $post_variables=null, $headers=null)
-    {
-        $http_options = array('method'=>'GET', 'timeout'=>3);
-
-        $string_headers = '';
-        if (is_array($headers)) {
-            foreach ($headers as $key => $value) {
-                $string_headers .= "$key: $value\r\n";
-            }
-        }
-
-        if (is_array($get_variables)) {
-            $get_variables = '?' . str_replace('&amp;', '&', urldecode(http_build_query($get_variables, '', '&')));
-        }
-        else {
-            $get_variables = null;
-        }
-
-        if (is_array($post_variables)) {
-            $post_variables = str_replace('&amp;', '&', urldecode(http_build_query($post_variables, '', '&')));
-            $http_options['method'] = 'POST';
-            $string_headers = "Content-type: application/x-www-form-urlencoded\r\n" . "Content-Length: " . strlen($post_variables) . "\r\n" . $string_headers;
-            $http_options['header'] = $string_headers;
-            $http_options['content'] = $post_variables;
-        } else {
-            $post_variables = '';
-            $http_options['header'] = $string_headers;
-        }
-
-        $context = stream_context_create(array('http'=>$http_options));
-        $response = @file_get_contents($this->url . $get_variables, null, $context);
-
-        return array('body'=>$response!==false?$response:'Request failed, consider using php5-curl module for more information.', 'code'=>$response!==false?'200':'400');
-    }
-}
\ No newline at end of file