-
Notifications
You must be signed in to change notification settings - Fork 2
/
Api.php
executable file
·127 lines (114 loc) · 3.68 KB
/
Api.php
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
<?php
namespace Acms\Plugins\GoogleCalendar;
use Acms\Services\Facades\Storage;
use DB;
use SQL;
use Config;
use Cache;
use Google\Client;
use Google\Service\Calendar;
use Google\Exception as GoogleException;
class Api
{
/**
* @var \Field
*/
public $config;
/**
* @var \Google_Client
*/
public $client;
/**
* Api constructor.
*/
public function __construct()
{
// 書き込み権限を指定
$scopes = implode(' ', array(Calendar::CALENDAR_EVENTS));
$client = new Client();
$this->config = Config::loadDefaultField();
$this->config->overload(Config::loadBlogConfig(BID));
$idJsonPath = $this->config->get('calendar_clientid_json');
$client->setApplicationName('ACMS');
$client->setScopes($scopes);
$this->client = $client;
$this->setAuthConfig($idJsonPath);
$client->setAccessType('offline');
$client->setApprovalPrompt("force");
$redirect_uri = acmsLink(array(
'bid' => BID,
'admin' => 'app_google_calendar_callback',
));
$client->setRedirectUri($redirect_uri);
$accessToken = json_decode($this->config->get('google_calendar_accesstoken'), true);
if ($accessToken) {
$client->setAccessToken($accessToken);
if ($client->isAccessTokenExpired()) {
$refreshToken = $client->getRefreshToken();
try {
$client->refreshToken($refreshToken);
$accessToken = $client->getAccessToken();
$this->updateAccessToken(json_encode($accessToken));
} catch (\Exception $e) {
userErrorLog('ACMS Error: In GoogleCalendar extension -> ' . $e->getMessage());
$this->updateAccessToken('');
}
}
}
}
/**
* @param string $json
* @throws Google_Exception
*/
public function setAuthConfig($json)
{
if (!Storage::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()
{
return $this->client;
}
/**
* @return string
*/
public function getAccessToken()
{
$accessToken = json_decode($this->config->get('google_calendar_accesstoken'), true);
return $accessToken;
}
/**
* @param string $accessToken
*/
public function updateAccessToken($accessToken)
{
if (class_exists('Cache')) {
Cache::flush('config');
}
$DB = DB::singleton(dsn());
$RemoveSQL = SQL::newDelete('config');
$RemoveSQL->addWhereOpr('config_blog_id', BID);
$RemoveSQL->addWhereOpr('config_key', 'google_calendar_accesstoken');
$DB->query($RemoveSQL->get(dsn()), 'exec');
$InsertSQL = SQL::newInsert('config');
$InsertSQL->addInsert('config_key', 'google_calendar_accesstoken');
$InsertSQL->addInsert('config_value', $accessToken);
$InsertSQL->addInsert('config_blog_id', BID);
$DB->query($InsertSQL->get(dsn()), 'exec');
}
}