1<?php
2/**
3 * Action Component for Securelogin Dokuwiki Plugin
4 *
5 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 * @author     Mikhail I. Izmestev
7 * @maintainer Matt Bagley
8 *
9 * @see also   https://www.dokuwiki.org/plugin:securelogin
10 */
11
12// must be run within Dokuwiki
13if(!defined('DOKU_INC')) die();
14
15class action_plugin_securelogin extends DokuWiki_Action_Plugin {
16    protected $slhlp;
17
18    function __construct() {
19        $this->slhlp = plugin_load('helper', $this->getPluginName());
20    }
21
22    /**
23     * Register its handlers with the DokuWiki's event controller
24     */
25    function register(Doku_Event_Handler $controller) {
26        $controller->register_hook('AUTH_LOGIN_CHECK', 'BEFORE',  $this, '_auth');
27        $controller->register_hook('AJAX_CALL_UNKNOWN', 'BEFORE',  $this, '_ajax_handler');
28    }
29
30    function _auth(Doku_Event $event, $param) {
31        $this->slhlp->workCorrect(true);
32        if(!$this->slhlp || !$this->slhlp->canWork() || !$this->slhlp->haveKey(true)) return;
33
34        if(isset($_REQUEST['use_securelogin']) && $_REQUEST['use_securelogin'] && isset($_REQUEST['securelogin'])) {
35            list($request,) = explode(';', $this->slhlp->decrypt($_REQUEST['securelogin']));
36            if($request) {
37                foreach(explode("&", $request) as $var) {
38                    list($key, $value) = explode("=",$var,2);
39                    $value = urldecode($value);
40                    $_REQUEST[$key] = $value;
41                    $_POST[$key] = $value;
42                }
43            }
44            unset($_REQUEST['securelogin']);
45            unset($_REQUEST['use_securelogin']);
46        }
47        if($_REQUEST['do'] == "login") {
48            auth_login($_REQUEST['u'], $_REQUEST['p'], $_REQUEST['r'], $_REQUEST['http_credentials']);
49            $event->preventDefault();
50        }
51    }
52
53    function _ajax_handler(Doku_Event $event, $param) {
54        if($event->data != 'securelogin_public_key') return;
55        if(!$this->slhlp || !$this->slhlp->canWork() || !$this->slhlp->haveKey(true)) return;
56
57        header('Content-Type: text/javascript; charset=utf-8');
58        print 'function encrypt(text) {
59        var rsa = new RSAKey();
60        rsa.setPublic("'.$this->slhlp->getModulus().'", "'.$this->slhlp->getExponent().'");
61        var res = rsa.encrypt(text);
62        if(res) {
63                return hex2b64(res);
64        }
65        }
66        var securelogin_login_label = "'.$this->getLang('use_securelogin').'";
67        var securelogin_update_label = "'.$this->getLang('use_secureupdate').'";';
68
69        $event->preventDefault();
70        return;
71    }
72}
73