forked from esn-org/authplaincas
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaction.php
161 lines (132 loc) · 5.55 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
<?php
/**
* Plain CAS authentication plugin
*
* @licence GPL 2 (http://www.gnu.org/licenses/gpl.html)
* @author Fabian Bircher
* @version 0.0.2
*/
// must be run within Dokuwiki
if(!defined('DOKU_INC')) die();
if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
require_once(DOKU_PLUGIN.'action.php');
class action_plugin_authplaincas extends DokuWiki_Action_Plugin {
function getInfo() {
return array (
'author' => 'Fabian Bircher',
'email' => '[email protected]',
'date' => '2013-06-13',
'name' => 'plain CAS Plugin',
'desc' => 'Authenticate DokuWiki users via CAS',
);
}
function register (Doku_Event_Handler $controller) {
$controller->register_hook ('HTML_LOGINFORM_OUTPUT', 'BEFORE', $this, 'handle_login_form'); // old < 2022 "igor"
$controller->register_hook ('FORM_LOGIN_OUTPUT', 'BEFORE', $this, 'handle_login_form'); // new >= 2022 "igor"
$controller->register_hook ('ACTION_ACT_PREPROCESS', 'BEFORE', $this, 'handle_action');
$controller->register_hook ('ACTION_ACT_PREPROCESS', 'AFTER', $this, 'handle_action_after');
$controller->register_hook ('TPL_ACT_UNKNOWN', 'BEFORE', $this, 'handle_template');
}
function _self () {
global $ID;
return wl($ID, '', true, '');
}
function _selfdo ($do) {
global $ID;
return wl($ID, 'do=' . $do, true, '&');
}
function _redirect ($url) {
header ('Location: ' . $url);
exit;
}
function handle_login_form (&$event, $param) {
global $auth;
global $conf;
global $lang;
global $ID;
if($conf['authtype'] == 'authplaincas') {
if ($this->getConf('logourl') != '') {
$caslogo = '<img src="'.$this->getConf('logourl').'" alt="" style="vertical-align: middle;" /> ';
} else {
$caslogo = '';
}
$form = $event->data;
if (is_a($form, \dokuwiki\Form\Form::class)) {
// new Form >= 2022 "igor"
// remove default login form
$nelement = $form->elementCount();
for($pos=0; $pos<$nelement; $pos++) {
$form->removeElement(0);
}
$form->addFieldsetOpen($this->getConf('name'));
$href = $form->addTagOpen('a');
$href->attr('href', $this->_selfdo('caslogin'));
$form->addHTML($caslogo);
$form->addHTML($lang['btn_login']);
$form->addTagClose('a');
$form->addFieldsetClose();
if ($auth && $auth->canDo('modPass') && actionOK('resendpwd')) {
$form->addHTML('<p>'.$lang['pwdforget'].': <a href="'.wl($ID,'do=resendpwd').'" rel="nofollow" class="wikilink1">'.$lang['btn_resendpwd'].'</a></p>');
}
} else {
// old form < 2022 "igor" (kept for backward compatibility)
//var_dump($event->data->_content);
$event->data->_content = array(); // remove the login form
$event->data->insertElement(0,'<fieldset><legend>'.$this->getConf('name').'</legend>');
$event->data->insertElement(1,'<p style="text-align: center;"><a href="'.$this->_selfdo('caslogin').'"><div>'.$caslogo.'</div>'.$lang['btn_login'].'</a></p>');
$event->data->insertElement(2,'</fieldset>');
//instead of removing, one could implement a local login here...
// if ($this->getConf('jshidelocal')) {
// $event->data->insertElement(3,'<p id="normalLoginToggle" style="display: none; text-align: center;"><a href="#" onClick="javascript:document.getElementById(\'normalLogin\').style.display = \'block\'; document.getElementById(\'normalLoginToggle\').style.display = \'none\'; return false;">Show '.$this->getConf('localname').'</a></p><p style="text-align: center;">Only use this if you cannot use the '.$this->getConf('name').' above.</p>');
// $event->data->replaceElement(4,'<fieldset id="normalLogin" style="display: block;"><legend>'.$this->getConf('localname').'</legend><script type="text/javascript">document.getElementById(\'normalLoginToggle\').style.display = \'block\'; document.getElementById(\'normalLogin\').style.display = \'none\';</script>');
// } else {
// $event->data->replaceElement(3,'<fieldset><legend>'.$this->getConf('localname').'</legend>');
// }
$insertElement = 3;
if ($auth && $auth->canDo('modPass') && actionOK('resendpwd')) {
$event->data->insertElement($insertElement,'<p>'.$lang['pwdforget'].': <a href="'.wl($ID,'do=resendpwd').'" rel="nofollow" class="wikilink1">'.$lang['btn_resendpwd'].'</a></p>');
}
}
}
}
function handle_caslogin () {
global $auth;
$auth->logIn();
}
function handle_caslogout () {
auth_logoff();
}
function handle_action (&$event, $param) {
if ($event->data == 'caslogin') {
$event->preventDefault();
$this->handle_caslogin();
}
if ($event->data == 'logout') {
$this->handle_caslogout();
}
}
function handle_action_after (&$event, $param){
global $ACT, $auth, $USERINFO, $MSG;
if(
(($ACT == 'denied' && empty($USERINFO)) || $ACT == 'login') &&
$this->getConf('force_redirect') &&
!($auth && $auth->canDo('modPass') && actionOK('resendpwd'))
){
// check $MSG
if(is_array($MSG)){
foreach ($MSG as $m) {
if($m && info_msg_allowed($m)){
return;
// Has messages, don't execute the redirector below
}
}
}
$this->handle_caslogin(); // will jump out if redirect is required
}
}
function handle_template (&$event, $param) {
if ($event->data == 'caslogin') {
$event->preventDefault();
}
}
}