forked from cosmocode/dokuwiki-plugin-oauth
-
Notifications
You must be signed in to change notification settings - Fork 1
/
action.php
259 lines (210 loc) · 8.74 KB
/
action.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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
<?php
/**
* DokuWiki Plugin oauth (Action Component)
*
* @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
* @author Andreas Gohr <[email protected]>
*/
// must be run within Dokuwiki
if(!defined('DOKU_INC')) die();
class action_plugin_oauth extends DokuWiki_Action_Plugin {
/**
* Registers a callback function for a given event
*
* @param Doku_Event_Handler $controller DokuWiki's event controller object
* @return void
*/
public function register(Doku_Event_Handler $controller) {
global $conf;
if($conf['authtype'] != 'oauth') return;
$conf['profileconfirm'] = false; // password confirmation doesn't work with oauth only users
$controller->register_hook('DOKUWIKI_STARTED', 'BEFORE', $this, 'handle_start');
$controller->register_hook('HTML_LOGINFORM_OUTPUT', 'BEFORE', $this, 'handle_loginform');
$controller->register_hook('HTML_UPDATEPROFILEFORM_OUTPUT', 'BEFORE', $this, 'handle_profileform');
$controller->register_hook('AUTH_USER_CHANGE', 'BEFORE', $this, 'handle_usermod');
$controller->register_hook('ACTION_ACT_PREPROCESS', 'BEFORE', $this, 'handle_dologin');
}
/**
* Start an oAuth login or restore environment after successful login
*
* @param Doku_Event $event event object by reference
* @param mixed $param [the parameters passed as fifth argument to register_hook() when this
* handler was registered]
* @return void
*/
public function handle_start(Doku_Event &$event, $param) {
if (isset($_SESSION[DOKU_COOKIE]['oauth-done']['do']) || !empty($_SESSION[DOKU_COOKIE]['oauth-done']['rev'])){
$this->restoreSessionEnvironment();
return;
}
$this->startOAuthLogin();
}
private function startOAuthLogin() {
global $INPUT, $ID;
/** @var helper_plugin_oauth $hlp */
$hlp = plugin_load('helper', 'oauth');
$servicename = $INPUT->str('oauthlogin');
$service = $hlp->loadService($servicename);
if(is_null($service)) return;
// remember service in session
session_start();
$_SESSION[DOKU_COOKIE]['oauth-inprogress']['service'] = $servicename;
$_SESSION[DOKU_COOKIE]['oauth-inprogress']['id'] = $ID;
session_write_close();
$service->login();
}
private function restoreSessionEnvironment() {
global $INPUT, $ACT, $TEXT, $PRE, $SUF, $SUM, $RANGE, $DATE_AT, $REV;
$ACT = $_SESSION[DOKU_COOKIE]['oauth-done']['do'];
$_REQUEST = $_SESSION[DOKU_COOKIE]['oauth-done']['$_REQUEST'];
$REV = $INPUT->int('rev');
$DATE_AT = $INPUT->str('at');
$RANGE = $INPUT->str('range');
if($INPUT->post->has('wikitext')) {
$TEXT = cleanText($INPUT->post->str('wikitext'));
}
$PRE = cleanText(substr($INPUT->post->str('prefix'), 0, -1));
$SUF = cleanText($INPUT->post->str('suffix'));
$SUM = $INPUT->post->str('summary');
unset($_SESSION[DOKU_COOKIE]['oauth-done']);
}
/**
* Save groups for all the services a user has enabled
*
* @param Doku_Event $event event object by reference
* @param mixed $param [the parameters passed as fifth argument to register_hook() when this
* handler was registered]
* @return void
*/
public function handle_usermod(Doku_Event &$event, $param) {
global $ACT;
global $USERINFO;
global $auth;
global $INPUT;
if($event->data['type'] != 'modify') return;
if($ACT != 'profile') return;
// we want to modify the user's groups
$groups = $USERINFO['grps']; //current groups
if(isset($event->data['params'][1]['grps'])) {
// something already defined new groups
$groups = $event->data['params'][1]['grps'];
}
/** @var helper_plugin_oauth $hlp */
$hlp = plugin_load('helper', 'oauth');
// get enabled and configured services
$enabled = $INPUT->arr('oauth_group');
$services = $hlp->listServices();
$services = array_map(array($auth, 'cleanGroup'), $services);
// add all enabled services as group, remove all disabled services
foreach($services as $service) {
if(isset($enabled[$service])) {
$groups[] = $service;
} else {
$idx = array_search($service, $groups);
if($idx !== false) unset($groups[$idx]);
}
}
$groups = array_unique($groups);
// add new group array to event data
$event->data['params'][1]['grps'] = $groups;
}
/**
* Add service selection to user profile
*
* @param Doku_Event $event event object by reference
* @param mixed $param [the parameters passed as fifth argument to register_hook() when this
* handler was registered]
* @return void
*/
public function handle_profileform(Doku_Event &$event, $param) {
global $USERINFO;
/** @var auth_plugin_authplain $auth */
global $auth;
/** @var helper_plugin_oauth $hlp */
$hlp = plugin_load('helper', 'oauth');
/** @var Doku_Form $form */
$form =& $event->data;
$pos = $form->findElementByAttribute('type', 'submit');
$services = $hlp->listServices();
if(!$services) return;
$form->insertElement($pos, form_closefieldset());
$form->insertElement(++$pos, form_openfieldset(array('_legend' => $this->getLang('loginwith'), 'class' => 'plugin_oauth')));
foreach($services as $service) {
$group = $auth->cleanGroup($service);
$elem = form_makeCheckboxField(
'oauth_group['.$group.']',
1, $service, '', 'simple',
array(
'checked' => (in_array($group, $USERINFO['grps'])) ? 'checked' : ''
)
);
$form->insertElement(++$pos, $elem);
}
$form->insertElement(++$pos, form_closefieldset());
$form->insertElement(++$pos, form_openfieldset(array()));
}
/**
* Add the oAuth login links
*
* @param Doku_Event $event event object by reference
* @param mixed $param [the parameters passed as fifth argument to register_hook() when this
* handler was registered]
* @return void
*/
public function handle_loginform(Doku_Event &$event, $param) {
global $conf;
/** @var helper_plugin_oauth $hlp */
$hlp = plugin_load('helper', 'oauth');
$singleService = $this->getConf('singleService');
$enabledServices = $hlp->listServices();
/** @var Doku_Form $form */
$form =& $event->data;
$html = '';
$validDomains = $hlp->getValidDomains();
if (count($validDomains) > 0) {
$html .= sprintf($this->getLang('eMailRestricted'), join(', ', $validDomains));
}
if ($singleService == '') {
foreach($hlp->listServices() as $service) {
$html .= $this->service_html($service);
}
if(!$html) return;
}else{
if (in_array($singleService, $enabledServices, true) === false) {
msg($this->getLang('wrongConfig'),-1);
return;
}
$form->_content = array();
$html = $this->service_html($singleService);
}
$form->_content[] = form_openfieldset(array('_legend' => $this->getLang('loginwith'), 'class' => 'plugin_oauth'));
$form->_content[] = $html;
$form->_content[] = form_closefieldset();
}
function service_html ($service){
global $ID;
$html = '';
$html .= '<a href="' . wl($ID, array('oauthlogin' => $service)) . '" class="plugin_oauth_' . $service . '">';
$html .= $service;
$html .= '</a> ';
return $html;
}
public function handle_dologin(Doku_Event &$event, $param) {
global $lang;
global $ID;
$singleService = $this->getConf('singleService');
if ($singleService == '') return true;
$lang['btn_login'] = $this->getLang('loginButton') . $singleService;
if($event->data != 'login') return true;
/** @var helper_plugin_oauth $hlp */
$hlp = plugin_load('helper', 'oauth');
$enabledServices = $hlp->listServices();
if (in_array($singleService, $enabledServices, true) === false) {
msg($this->getLang('wrongConfig'),-1);
return false;
}
$url = wl($ID, array('oauthlogin' => $singleService), true, '&');
send_redirect($url);
}
}
// vim:ts=4:sw=4:et: