1<?php
2/**
3 * Adminn 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
15/**
16 * All DokuWiki plugins to extend the admin function
17 * need to inherit from this class
18 */
19class admin_plugin_securelogin extends DokuWiki_Admin_Plugin {
20    protected $slhlp = null;
21
22    function __construct() {
23        $this->slhlp = plugin_load('helper', $this->getPluginName());
24        if(!$this->slhlp) msg('Loading the '.$this->getPluginName().' helper failed. Make sure that the '.$this->getPluginName().' plugin is installed.', -1);
25    }
26
27    /**
28     * return sort order for position in admin menu
29     */
30    function getMenuSort() {
31        return 999;
32    }
33
34    function getMenuText($lang) {
35        return $this->getLang('securelogin_conf');
36    }
37
38    /**
39     * handle user request
40     */
41    function handle() {
42        if(!$this->slhlp->canWork())
43            msg("You need openssl php module for this plugin work!", -1);
44        elseif($this->slhlp->haveKey() && !$this->slhlp->workCorrect())
45            msg("Your version of dokuwiki not generate AUTH_LOGIN_CHECK event, plugin not work!");
46
47        $fn = $_REQUEST['fn'];
48
49        if (is_array($fn)) {
50            $cmd = key($fn);
51            $param = $fn[$cmd];
52        } else {
53            $cmd = $fn;
54            $param = null;
55        }
56
57        switch($cmd) {
58            case "newkey": $this->slhlp->generateKey($param); break;
59            case "test": msg(urldecode($this->slhlp->decrypt($param['message']))); break;
60        }
61    }
62
63    /**
64     * output appropriate html
65     */
66    function html() {
67        if(!$this->slhlp->canWork()) {
68            print $this->locale_xhtml('needopenssl');
69            return;
70        }
71        elseif($this->slhlp->haveKey() && !$this->slhlp->workCorrect())
72            print $this->locale_xhtml('needpatch');
73        ptln('<div id="secure__login">');
74        $this->_html_generateKey();
75
76        if($this->slhlp->haveKey()) {
77            $this->_html_test();
78
79//      print $this->render("===== ".$this->getLang('public_key')." ===== \n".
80//          "<code>\n".
81//          $this->slhlp->getPublicKey().
82//          "</code>",
83//          $format='xhtml');
84        }
85        ptln('</div>');
86    }
87
88    function _html_generateKey() {
89        global $ID;
90        $form = new Doku_Form('generate__key', wl($ID,'do=admin,page='.$this->getPluginName(), false, '&'));
91        $form->startFieldset($this->getLang('generate_key'));
92        $form->addElement(form_makeMenuField('fn[newkey]', $this->slhlp->getKeyLengths(), $this->slhlp->getKeyLength(), $this->getLang('key_length'), 'key__length', 'block', array('class' => 'edit')));
93        $form->addElement(form_makeButton('submit', '', $this->getLang('generate')));
94        $form->endFieldset();
95        ptln('<div class="half">');
96        html_form('generate', $form);
97        ptln('</div>');
98    }
99
100    function _html_test() {
101        global $ID;
102        $form = new Doku_Form('test__publicKey', wl($ID,'do=admin,page='.$this->getPluginName(), false, '&'));
103        $form->startFieldset($this->getLang('test_key'));
104        $form->addElement(form_makeTextField('fn[test][message]', $this->getLang('sample_message'), $this->getLang('test_message'), 'test__message', 'block'));
105        $form->addElement(form_makeButton('submit', '', $this->getLang('test')));
106        $form->endFieldset();
107        html_form('test__publicKey', $form);
108    }
109}
110