1<?php
2/**
3 * Federated Login for DokuWiki - register local account class
4 *
5 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 * @link       http://www.dokuwiki.org/plugin:fedauth
7 * @author     Aoi Karasu <aoikarasu@gmail.com>
8 */
9
10/**
11 * Class responsible for registering a local account once user is successfully
12 * authenticated but his identity is not associated with any existing local account.
13 *
14 * @author     Aoi Karasu <aoikarasu@gmail.com>
15 */
16class fa_register extends fa_login {
17
18    /**
19     * Creates the class instance bound to a plugin instance and an authentication provider.
20     *
21     * @param objref $manager object reference to the admin plugin
22     * @param string $cmd name of the command to handle
23     * @param string $provid (optional) an authentication provider id
24     */
25    function __construct(&$manager, $cmd, $provid='') {
26        parent::__construct(&$manager, $cmd, $provid);
27    }
28
29    function process_register() {
30        global $conf, $ID;
31
32        $this->success = true;
33        if (!($data = $_SESSION[DOKU_COOKIE]['fedauth']['tmpr'])) {
34            // cannot register without fedauth temp data, but suggest standard registration
35            send_redirect(wl($ID, 'do=register', true, '&'));
36        }
37        if (!empty($_POST)) {
38            // TODO: ensure remote identity is still not associated
39            //       with any account; if it is, just login using it
40            // attempt account creation
41            $_POST['save'] = true; // investigate, why register() requires it
42            $conf['autopasswd'] = true; // yup, force auto-password
43            if (register()) {
44                // account created, save all fedauth data and save the cookie
45                @session_start(); // restore session to update it
46                $store =& $this->getUserStore();
47                $this->manager->cookie->set($_POST['login'], $data['prid'], $data['svcd'], false /*$sticky*/);
48                $store->addUserDataEntry($data['prid'], $data['ident']);
49                $this->msg($this->success('loginadded', array('@PROVID@' => $data['prnm'])));
50                unset($_SESSION[DOKU_COOKIE]['fedauth']['tmpr']);
51                send_redirect($this->restoreLocation());
52            }
53            // no custom message on fail, dokiwiki's register() prints error messages
54        }
55        return array('msg'=>('<pre>'.print_r($_SESSION[DOKU_COOKIE]['fedauth']['tmpr'], true).'</pre>'),'code'=>2);
56    }
57
58    function html_register() {
59        global $conf, $ID, $INPUT, $lang;
60
61        $data = $_SESSION[DOKU_COOKIE]['fedauth']['tmpr'];
62        $prep = $this->_prepareFormData($data);
63
64        $form = new Doku_Form(array('id' => 'dw__register'));
65        $form->startFieldset($lang['btn_register']);
66        $form->addHidden('do', 'fedauth');
67        $form->addHidden('fa[register]', '');
68        $form->addHidden('provid', $data['prid']);
69
70        $form->addElement(form_makeTextField('login', $prep['login'], $lang['user'], '', 'block', array('size'=>'50')));
71        $form->addElement(form_makeTextField('fullname', $prep['fullname'], $lang['fullname'], '', 'block', array('size'=>'50')));
72        $form->addElement(form_makeTextField('email', $prep['email'], $lang['email'], '', 'block', array('size'=>'50')));
73        $form->addElement(form_makeButton('submit', '', $lang['btn_register']));
74        $form->endFieldset();
75
76        $out = $this->manager->locale_xhtml('register');
77        $out = str_replace('@PROVID@', $data['prnm'], $out);
78        $out = str_replace('@REGFORM@', '<div class="centeralign">'.NL.$form->getForm().'</div>'.NL, $out);
79
80        echo $out;
81    }
82
83    function _prepareFormData($data) {
84        if (!empty($_POST)) {
85            return array('login' => $_POST['login'], 'email' => $_POST['email'], 'fullname' => $_POST['fullname']);
86        }
87        return array('login' => $data['nickname'], 'email' => $data['email'], 'fullname' => $data['fullname']);
88    }
89
90} /* fa_register */
91
92/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
93