-
Notifications
You must be signed in to change notification settings - Fork 3
/
Main.php
205 lines (161 loc) · 6.25 KB
/
Main.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
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
<?php
namespace IdnoPlugins\Github {
class Main extends \Idno\Common\Plugin {
public static $AUTHORIZATION_ENDPOINT = 'https://github.com/login/oauth/authorize';
public static $TOKEN_ENDPOINT = 'https://github.com/login/oauth/access_token';
public static function getRedirectUrl() {
return \Idno\Core\site()->config()->url . 'github/callback';
}
public static function getState() {
return md5(\Idno\Core\site()->config()->site_secret . \Idno\Core\site()->config()->url . dirname(__FILE__));
}
function registerPages() {
// Register the callback URL
\Idno\Core\Idno::site()->routes()->addRoute('github/callback', '\IdnoPlugins\Github\Pages\Callback');
// Register admin settings
\Idno\Core\Idno::site()->routes()->addRoute('admin/github', '\IdnoPlugins\Github\Pages\Admin');
// Register settings page
\Idno\Core\Idno::site()->routes()->addRoute('account/github', '\IdnoPlugins\Github\Pages\Account');
/** Template extensions */
// Add menu items to account & administration screens
\Idno\Core\site()->template()->extendTemplate('admin/menu/items', 'admin/github/menu');
\Idno\Core\site()->template()->extendTemplate('account/menu/items', 'account/github/menu');
}
function registerTranslations() {
\Idno\Core\Idno::site()->language()->register(
new \Idno\Core\GetTextTranslation(
'github', dirname(__FILE__) . '/languages/'
)
);
}
function registerEventHooks() {
// Register syndication services
\Idno\Core\site()->syndication()->registerService('github', function() {
return $this->hasGithub();
}, ['note']);
\Idno\Core\Idno::site()->events()->addListener('user/auth/success', function (\Idno\Core\Event $event) {
if ($this->hasGithub()) {
if (is_array(\Idno\Core\site()->session()->currentUser()->github)) {
foreach (\Idno\Core\site()->session()->currentUser()-> github as $id => $details) {
if ($id = 'access_token') {
\Idno\Core\site()->syndication()->registerServiceAccount('github', $id, 'github');
}
}
}
}
});
// Activate syndication automatically, if replying to github
\Idno\Core\Idno::site()->events()->addListener('syndication/selected/github', function (\Idno\Core\Event $event) {
$eventdata = $event->data();
if (!empty($eventdata['reply-to'])) {
$replyto = $eventdata['reply-to'];
if (!is_array($replyto))
$replyto = [$replyto];
foreach ($replyto as $url) {
if (strpos(parse_url($url)['host'], 'github.com')!==false)
$event->setResponse(true);
}
}
});
$reply_func = function(\Idno\Core\Event $event) {
$object = $event->data()['object'];
if ($this->hasGithub()) {
if ($githubAPI = $this->connect()) {
$githubAPI->setAccessToken(\Idno\Core\site()->session()->currentUser()->github['access_token']);
$message = $object->getDescription();
$in_reply_to = array_merge((array) $object->inreplyto, (array) $object->syndicatedto); //\Idno\Core\site()->currentPage()->getInput('inreplyto');
if (!is_array($in_reply_to))
$in_reply_to = [$in_reply_to];
foreach ($in_reply_to as $url) {
$url = $this->normaliseCommentUrl($url);
try {
if (!$githubAPI->access_token) throw new \Exception("Access token is missing.");
$result = \Idno\Core\Webservice::post($url, json_encode([
'title' => $object->getTitle(),
'body' => $message,
]), ['Content-Type: application/json', 'Authorization: token '. $githubAPI->access_token]);
$content = json_decode($result['content']);
if ($result['response'] == 201) {
// Success
$link = $content->html_url;
$object->setPosseLink('github', $link);
$object->save();
} else {
\Idno\Core\site()->logging->log("Github Syndication failed with " . print_r($result, true), LOGLEVEL_ERROR);
if (!empty($content->error_description))
throw new \Exception($content->error_description);
if (!empty($content->message))
throw new \Exception("\"{$content->message}\" response code {$result['response']}");
throw new \Exception("Error code {$result['response']}");
}
} catch (\Exception $e) {
\Idno\Core\site()->session()->addErrorMessage('There was a problem posting reply to ' . $url . ': ' . $e->getMessage());
}
}
}
}
};
// Reply to a comment
\Idno\Core\Idno::site()->events()->addListener('post/note/github', $reply_func);
\Idno\Core\Idno::site()->events()->addListener('post/article/github', $reply_func);
}
/**
* Connect to Github
* @return bool|\IdnoPlugins\Github\Client
*/
function connect() {
if (!empty(\Idno\Core\site()->config()->github)) {
$api = new Client(
\Idno\Core\site()->config()->github['appId'], \Idno\Core\site()->config()->github['secret']
);
return $api;
}
return false;
}
/**
* Can the current user use Github?
* @return bool
*/
function hasGithub() {
if (\Idno\Core\site()->session()->currentUser()->github) {
return true;
}
return false;
}
/**
* When replying to a comment, we need to turn the HTML url to a correct post URL.
* @param type $htmlUrl
*/
function normaliseCommentUrl($htmlUrl) {
// Map github to api endpoint
$htmlUrl = preg_replace('#https:\/\/(www\.)?github\.com\/#', 'https://api.github.com/repos/', $htmlUrl);
// Strip fragment
$htmlUrl = $this->stripFragment($htmlUrl);
// Comment on issue
if ($this->isIssueUrl($htmlUrl))
$htmlUrl = trim($htmlUrl, '/') . '/comments';
// Comment on pull
//if (preg_match('#pull\/[0-9]+#', $htmlUrl))
// $htmlUrl = trim(str_replace('pull','pulls', $htmlUrl), '/') . '/comments';
return $htmlUrl;
}
/**
* Is this an issue URL (posting to create a new issue)
* @param type $url
*/
protected function isIssueUrl($url) {
if (preg_match('#issues\/[0-9]+#', $url))
return true;
return false;
}
/**
* Strip fragments from a url
* @param type $url
*/
protected function stripFragment($url) {
$bits = parse_url($url);
unset($bits['fragment']);
return \Idno\Common\Page::buildUrl($bits);
}
}
}