-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathMain.php
89 lines (72 loc) · 3.77 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
<?php
namespace IdnoPlugins\IndiePub {
use IdnoPlugins\IndiePub\Pages\IndieAuth\Token;
class Main extends \Idno\Common\Plugin
{
function registerTranslations()
{
\Idno\Core\Idno::site()->language()->register(
new \Idno\Core\GetTextTranslation(
'indiepub', dirname(__FILE__) . '/languages/'
)
);
}
function registerEventHooks()
{
\Idno\Core\site()->events()->addListener('user/auth/request', function (\Idno\Core\Event $event) {
if ($user = \IdnoPlugins\IndiePub\Main::authenticate()) {
$event->setResponse($user);
}
});
}
function registerPages()
{
\Idno\Core\Idno::site()->routes()->addRoute('/indieauth/auth/?', '\IdnoPlugins\IndiePub\Pages\IndieAuth\Auth', true);
\Idno\Core\Idno::site()->routes()->addRoute('/indieauth/approve/?', '\IdnoPlugins\IndiePub\Pages\IndieAuth\Approve', true);
\Idno\Core\Idno::site()->routes()->addRoute('/indieauth/token/?', '\IdnoPlugins\IndiePub\Pages\IndieAuth\Token', true);
\Idno\Core\Idno::site()->routes()->addRoute('/micropub/endpoint/?', '\IdnoPlugins\IndiePub\Pages\MicroPub\Endpoint', true);
\Idno\Core\Idno::site()->routes()->addRoute('/account/indiepub/?', '\IdnoPlugins\IndiePub\Pages\Account');
\Idno\Core\Idno::site()->routes()->addRoute('/account/indiepub/revoke/?', '\IdnoPlugins\IndiePub\Pages\Revoke');
\Idno\Core\Idno::site()->routes()->addRoute('/account/indiepub/add/?', '\IdnoPlugins\IndiePub\Pages\Add');
\Idno\Core\Idno::site()->template()->extendTemplate('shell/head', 'indiepub/shell/head');
\Idno\Core\Idno::site()->template()->extendTemplate('account/menu/items', 'account/menu/items/indiepub');
header('Link: <' . \Idno\Core\Idno::site()->config()->getURL() . 'indieauth/auth>; rel="authorization_endpoint"', false);
header('Link: <' . \Idno\Core\Idno::site()->config()->getURL() . 'indieauth/token>; rel="token_endpoint"', false);
header('Link: <' . \Idno\Core\Idno::site()->config()->getURL() . 'micropub/endpoint>; rel="micropub"', false);
}
/**
* Check that this token is either a user token or the
* site's API token, and auth the current request for that user if so.
*
* @return \Idno\Entities\User user on success
*/
private static function authenticate()
{
$access_token = \Idno\Core\Input::getInput('access_token');
$headers = \Idno\Common\Page::getallheaders();
if (!empty($headers['Authorization'])) {
$token = $headers['Authorization'];
$token = trim(str_replace('Bearer', '', $token));
} else if ($token = \Idno\Core\Input::getInput('access_token')) {
$token = trim($token);
}
if (!empty($token)) {
$found = Token::findUserForToken($token);
if (!empty($found)) {
\Idno\Core\Idno::site()->session()->setIsAPIRequest(true);
$user = $found['user'];
\Idno\Core\Idno::site()->session()->refreshSessionUser($user);
return $user;
}
$user = \Idno\Entities\User::getOne(array('admin' => true));
if ($user)
if ($token == $user->getAPIkey()) {
\Idno\Core\Idno::site()->session()->setIsAPIRequest(true);
\Idno\Core\Idno::site()->session()->refreshSessionUser($user);
return $user;
}
}
return false;
}
}
}