-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathApi.php
More file actions
executable file
·157 lines (144 loc) · 5.46 KB
/
Api.php
File metadata and controls
executable file
·157 lines (144 loc) · 5.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
<?php
namespace Acms\Plugins\GoogleSheets;
use DB;
use SQL;
use Acms\Services\Facades\LocalStorage;
use Acms\Services\Facades\Config;
use Acms\Services\Facades\Cache;
use Acms\Services\Facades\Logger;
use Acms\Services\Facades\Common;
use Google\Client;
use Google\Service\Sheets;
use Google\Exception as GoogleException;
class Api
{
protected $client = null;
protected $config;
/**
* Api constructor.
*/
public function __construct()
{
$scopes = implode(' ', [Sheets::SPREADSHEETS]);
$this->config = Config::loadDefaultField();
$this->config->overload(Config::loadBlogConfig(BID));
$accessTokenJson = $this->config->get('google_spreadsheet_accesstoken');
$accessToken = $accessTokenJson ? json_decode($accessTokenJson, true) : null;
if ($accessTokenJson && json_last_error() !== JSON_ERROR_NONE) {
$accessToken = null;
}
$refreshTokenJson = $this->config->get('google_spreadsheet_refreshtoken');
$refreshToken = $refreshTokenJson ? json_decode($refreshTokenJson, true) : null;
if ($refreshTokenJson && json_last_error() !== JSON_ERROR_NONE) {
$refreshToken = null;
}
$this->client = new Client();
$idJsonPath = $this->config->get('spreadsheet_clientid_json');
$this->client->setApplicationName('ACMS');
$this->client->setScopes($scopes);
if ($idJsonPath !== '') {
$this->setAuthConfig($idJsonPath);
}
$this->client->setAccessType('offline');
$this->client->setPrompt("consent");
$redirect_uri = acmsLink([
'bid' => BID,
'admin' => 'app_google_sheets_callback',
]);
$this->client->setRedirectUri($redirect_uri);
if ($accessToken) {
$this->client->setAccessToken($accessToken);
if ($this->client->isAccessTokenExpired()) {
try {
$this->client->refreshToken($refreshToken);
$accessToken = $this->client->getAccessToken();
$refreshToken = $this->client->getRefreshToken();
$this->updateAccessToken(json_encode($accessToken), json_encode($refreshToken));
} catch (\Exception $e) {
Logger::error(
'【Google Sheets】Google Sheets API のアクセストークンの更新に失敗しました。',
Common::exceptionArray($e)
);
}
}
}
}
/**
* @param string $json
* @return void
*/
public function setAuthConfig($json): void
{
if ($json === '') {
throw new \InvalidArgumentException('Empty JSON file path.');
}
if (!LocalStorage::exists($json)) {
throw new \RuntimeException('Failed to open ' . $json);
}
$json = file_get_contents($json);
$data = json_decode($json);
$key = isset($data->installed) ? 'installed' : 'web';
if (!isset($data->$key)) {
throw new GoogleException("Invalid client secret JSON file.");
}
$this->client->setClientId($data->$key->client_id);
$this->client->setClientSecret($data->$key->client_secret);
if (isset($data->$key->redirect_uris)) {
$this->client->setRedirectUri($data->$key->redirect_uris[0]);
}
}
/**
* @return \Google\Client
*/
public function getClient(): Client
{
return $this->client;
}
/**
* @return array|null
*/
public function getAccessToken(): ?array
{
$accessTokenJson = $this->config->get('google_spreadsheet_accesstoken');
$accessToken = $accessTokenJson ? json_decode($accessTokenJson, true) : null;
if ($accessTokenJson && json_last_error() !== JSON_ERROR_NONE) {
$accessToken = null;
}
return $accessToken;
}
/**
* @param string|null $accessToken
* @param string|null $refreshToken
* @return void
*/
public function updateAccessToken(?string $accessToken, ?string $refreshToken): void
{
if (!!$accessToken) {
$DB = DB::singleton(dsn());
$RemoveSQL = SQL::newDelete('config');
$RemoveSQL->addWhereOpr('config_blog_id', BID);
$RemoveSQL->addWhereOpr('config_key', 'google_spreadsheet_accesstoken');
$DB->query($RemoveSQL->get(dsn()), 'exec');
$InsertSQL = SQL::newInsert('config');
$InsertSQL->addInsert('config_key', 'google_spreadsheet_accesstoken');
$InsertSQL->addInsert('config_value', $accessToken);
$InsertSQL->addInsert('config_blog_id', BID);
$DB->query($InsertSQL->get(dsn()), 'exec');
}
if (!!$refreshToken) {
$DB = DB::singleton(dsn());
$RemoveSQL = SQL::newDelete('config');
$RemoveSQL->addWhereOpr('config_blog_id', BID);
$RemoveSQL->addWhereOpr('config_key', 'google_spreadsheet_refreshtoken');
$DB->query($RemoveSQL->get(dsn()), 'exec');
$InsertSQL = SQL::newInsert('config');
$InsertSQL->addInsert('config_key', 'google_spreadsheet_refreshtoken');
$InsertSQL->addInsert('config_value', $refreshToken);
$InsertSQL->addInsert('config_blog_id', BID);
$DB->query($InsertSQL->get(dsn()), 'exec');
}
if (class_exists('Cache')) {
Cache::flush('config');
}
}
}