-
Notifications
You must be signed in to change notification settings - Fork 81
/
Copy pathhelper.php
135 lines (119 loc) · 3.57 KB
/
helper.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
<?php
// phpcs:disable PSR1.Files.SideEffects.FoundWithSymbols
/**
* DokuWiki Plugin oauth (Helper Component)
*
* @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
* @author Andreas Gohr <[email protected]>
*/
use dokuwiki\Extension\Plugin;
use dokuwiki\Extension\Event;
use dokuwiki\plugin\oauth\Adapter;
require_once(__DIR__ . '/vendor/autoload.php'); // @todo can be removed with next dw release
/**
* Basic helper methods for the oauth flow
*/
class helper_plugin_oauth extends Plugin
{
/**
* Load the needed libraries and initialize the named oAuth service
*
* @param string $servicename
* @return null|Adapter
*/
public function loadService($servicename)
{
$services = $this->listServices(true);
if (!isset($services[$servicename])) return null;
return $services[$servicename];
}
/**
* The redirect URI used in all oAuth requests
*
* @return string
*/
public function redirectURI()
{
if ($this->getConf('custom-redirectURI') !== '') {
return $this->getConf('custom-redirectURI');
} else {
return DOKU_URL . DOKU_SCRIPT;
}
}
/**
* List available Services
*
* Services returned here, do not have initialized oAuth providers yet!
*
* @param bool $enabledonly list only services that have been configured
* @triggers PLUGIN_OAUTH_BACKEND_REGISTER
* @return Adapter[] list of service objects
*/
public function listServices($enabledonly = true)
{
$services = [];
$event = new Event('PLUGIN_OAUTH_BACKEND_REGISTER', $services);
$event->advise_before(false);
$event->advise_after();
// filter out unconfigured services
if ($enabledonly) {
$services = array_filter($services, static fn($service) =>
/** @var Adapter $service */
(bool)$service->getKey());
}
return $services;
}
/**
* @return array
*/
public function getValidDomains()
{
if ($this->getConf('mailRestriction') === '') {
return [];
}
$validDomains = explode(',', trim($this->getConf('mailRestriction'), ','));
return array_map('trim', $validDomains);
}
/**
* @param string $mail
*
* @return bool
*/
public function checkMail($mail)
{
$validDomains = $this->getValidDomains();
if (empty($validDomains)) return true;
foreach ($validDomains as $validDomain) {
if (str_ends_with($mail, $validDomain)) {
return true;
}
}
return false;
}
/**
* Display an exception to the user
*
* @param Exception $e
* @param string $friendly - user friendly explanation if available
*/
public function showException(Exception $e, $friendly = '')
{
global $conf;
$msg = $e->getMessage();
// translate the message if possible, using context if available
$trans = $this->getLang($msg);
if ($trans) {
if ($e instanceof \dokuwiki\plugin\oauth\Exception) {
$context = $e->getContext();
$trans = sprintf($trans, ...$context);
}
$msg = $trans;
}
msg('OAuth: ' . $friendly . ' ' . hsc($msg), -1);
if ($conf['allowdebug']) {
$msg = get_class($e) . ' at ' . $e->getFile() . ':' . $e->getLine() . '<br>';
$msg .= hsc($e->getTraceAsString());
msg("<pre>$msg</pre>", -1);
}
}
}