1<?php
2/**
3 * Plain CAS authentication plugin
4 *
5 * @licence   GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 * @author    Fabian Bircher
7 * @version   0.0.2
8 */
9
10// must be run within Dokuwiki
11if(!defined('DOKU_INC')) die();
12
13if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
14require_once(DOKU_PLUGIN.'action.php');
15
16class action_plugin_authplaincas extends DokuWiki_Action_Plugin {
17  function getInfo() {
18    return array (
19      'author' => 'Fabian Bircher',
20      'email' => 'fabian@esn.org',
21      'date' => '2013-06-13',
22      'name' => 'plain CAS Plugin',
23      'desc' => 'Authenticate DokuWiki users via CAS',
24    );
25  }
26
27  function register (Doku_Event_Handler $controller) {
28      $controller->register_hook ('HTML_LOGINFORM_OUTPUT', 'BEFORE', $this, 'handle_login_form');
29      $controller->register_hook ('ACTION_ACT_PREPROCESS', 'BEFORE', $this, 'handle_action');
30      $controller->register_hook ('ACTION_ACT_PREPROCESS', 'AFTER', $this, 'handle_action_after');
31      $controller->register_hook ('TPL_ACT_UNKNOWN', 'BEFORE', $this, 'handle_template');
32  }
33
34  function _self () {
35    global $ID;
36    return wl($ID, '', true, '');
37  }
38
39  function _selfdo ($do) {
40    global $ID;
41    return wl($ID, 'do=' . $do, true, '&');
42  }
43
44  function _redirect ($url) {
45    header ('Location: ' . $url);
46    exit;
47  }
48
49  function handle_login_form (&$event, $param) {
50    global $auth;
51    global $conf;
52    global $lang;
53    global $ID;
54
55    if($conf['authtype'] == 'authplaincas') {
56
57      if ($this->getConf('logourl') != '') {
58        $caslogo = '<img src="'.$this->getConf('logourl').'" alt="" style="vertical-align: middle;" /> ';
59      } else {
60        $caslogo = '';
61      }
62
63      //var_dump($event->data->_content);
64      $event->data->_content = array(); // remove the login form
65
66      $event->data->insertElement(0,'<fieldset><legend>'.$this->getConf('name').'</legend>');
67      $event->data->insertElement(1,'<p style="text-align: center;"><a href="'.$this->_selfdo('caslogin').'"><div>'.$caslogo.'</div>'.$lang['btn_login'].'</a></p>');
68      $event->data->insertElement(2,'</fieldset>');
69
70      //instead of removing, one could implement a local login here...
71      // if ($this->getConf('jshidelocal')) {
72        // $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>');
73        // $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>');
74      // } else {
75        // $event->data->replaceElement(3,'<fieldset><legend>'.$this->getConf('localname').'</legend>');
76      // }
77
78      $insertElement = 3;
79      if ($auth && $auth->canDo('modPass') && actionOK('resendpwd')) {
80        $event->data->insertElement($insertElement,'<p>'.$lang['pwdforget'].': <a href="'.wl($ID,'do=resendpwd').'" rel="nofollow" class="wikilink1">'.$lang['btn_resendpwd'].'</a></p>');
81      }
82
83    }
84
85  }
86
87  function handle_caslogin () {
88    global $auth;
89    $auth->logIn();
90  }
91
92  function handle_caslogout () {
93    auth_logoff();
94  }
95
96  function handle_action (&$event, $param) {
97    if ($event->data == 'caslogin') {
98      $event->preventDefault();
99      $this->handle_caslogin();
100    }
101    if ($event->data == 'logout') {
102      $this->handle_caslogout();
103    }
104  }
105
106  function handle_action_after (&$event, $param){
107    global $ACT, $auth, $USERINFO, $MSG;
108
109    if(
110        (($ACT == 'denied' && empty($USERINFO)) || $ACT == 'login') &&
111        $this->getConf('force_redirect') &&
112        !($auth && $auth->canDo('modPass') && actionOK('resendpwd'))
113      ){
114        // check $MSG
115        if(is_array($MSG)){
116            foreach ($MSG as $m) {
117              if($m && info_msg_allowed($m)){
118                return;
119                // Has messages, don't execute the redirector below
120              }
121            }
122        }
123
124        $this->handle_caslogin(); // will jump out if redirect is required
125    }
126  }
127
128  function handle_template (&$event, $param) {
129    if ($event->data == 'caslogin') {
130      $event->preventDefault();
131    }
132  }
133}
134