1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
defined('BASEPATH') OR exit('No direct script access allowed'); |
4
|
|
|
|
5
|
|
|
require_once('GMailScopes.php'); |
6
|
|
|
require_once('GMailUtil.php'); |
7
|
|
|
|
8
|
|
|
class GMail { |
9
|
|
|
|
10
|
|
|
const AUTH_URL = 'https://accounts.google.com/o/oauth2/auth'; |
11
|
|
|
const TOKEN_URL = 'https://accounts.google.com/o/oauth2/token'; |
12
|
|
|
private $clientId; |
13
|
|
|
|
14
|
|
|
function __construct($params=null) { |
15
|
|
|
get_instance()->load->splint('francis94c/ci-gmail', '%curl'); |
|
|
|
|
16
|
|
|
$this->clientId = $params['client_id'] ?? $this->clientId; |
17
|
|
|
} |
18
|
|
|
/** |
19
|
|
|
* [init Initialize library with cofigs. Can be called multiple times to set |
20
|
|
|
* config items] |
21
|
|
|
* @param array $config Associative Config Array. |
22
|
|
|
*/ |
23
|
|
|
public function init(array $config=null):void { |
24
|
|
|
$this->clientId = $config['client_id'] ?? $this->clientId; |
25
|
|
|
} |
26
|
|
|
/** |
27
|
|
|
* [getAuthorizeUrl Gets/composes the authorize url to direct users to so they |
28
|
|
|
* can give your application access to their GMail accounts |
29
|
|
|
* based on the given scopes.] |
30
|
|
|
* @param string $scope Access Scope. |
31
|
|
|
* @param string $redirectUri URL to redirect to after access is granted. |
32
|
|
|
* @param string $responseType Response type. 'code' by default. |
33
|
|
|
* @return string Authorize URL |
34
|
|
|
*/ |
35
|
|
|
public function getAuthorizeUrl(string $scope, string $redirectUri='urn:ietf:wg:oauth:2.0:oob', string $responseType='code', string $accessType='offline'):string { |
36
|
|
|
if ($scope == null) throw new Exception("GMail scope cannot be null"); |
37
|
|
|
return self::AUTH_URL . build_url_query([ |
38
|
|
|
'client_id' => $this->clientId, |
39
|
|
|
'redirect_uri' => $redirectUri, |
40
|
|
|
'scope' => $scope, |
41
|
|
|
'response_type' => $responseType, |
42
|
|
|
'access_type' => $accessType |
43
|
|
|
], false); |
44
|
|
|
} |
45
|
|
|
/** |
46
|
|
|
* [getToken description] |
47
|
|
|
* @param string $code [description] |
48
|
|
|
* @return [type] [description] |
|
|
|
|
49
|
|
|
*/ |
50
|
|
|
public function getToken(string $code):?array { |
|
|
|
|
51
|
|
|
|
52
|
|
|
} |
53
|
|
|
/** |
54
|
|
|
* [getClientId Get Client ID.] |
55
|
|
|
* @return null|string Client ID. |
56
|
|
|
*/ |
57
|
|
|
public function getClientId():?string { |
58
|
|
|
return $this->clientId; |
|
|
|
|
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
?> |
|
|
|
|
62
|
|
|
|