1<?php
2/**
3 * Federated Login for DokuWiki - sign-in processing 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 the sign-in process using selected authentication service.
12 *
13 * @author     Aoi Karasu <aoikarasu@gmail.com>
14 */
15class fa_signin extends fa_login {
16
17    /**
18     * Creates the class instance bound to a plugin instance and an authentication provider.
19     *
20     * @param objref $manager object reference to the admin plugin
21     * @param string $cmd name of the command to handle
22     * @param string $provid (optional) an authentication provider id
23     */
24    function __construct(&$manager, $cmd, $provid='') {
25        parent::__construct(&$manager, $cmd, $provid);
26    }
27
28    function process_signin() {
29        global $ID;
30
31        if ($pro = $this->manager->providers->get($this->provid)) {
32            $uname = $_REQUEST['fa_signinname'];
33            if ($pro->hasUsername() && empty($uname)) {
34                return $this->error('signinnamereq', array('@PROVID@' => '<b>'.$pro->getName().'</b>'));
35            }
36            if ($this->provid == 'openid') {
37                $oid = $_REQUEST['fa_openidurl'];
38                if (empty($oid) || $oid == 'http://') {
39                    return $this->error('oidurlreq');
40                }
41                $uname = $oid;
42            }
43            return $this->callService($pro, $uname);
44        }
45    }
46
47    function html_signin() {
48        if (!$this->success) {
49            $this->html_login_service_from();
50        }
51    }
52
53    function callService($pro, $data, $renew=false) {
54        global $ID;
55
56        if ($renew) {
57            // in case of reneval we want to store all data and retrieve it on auth success
58            $_SESSION[DOKU_COOKIE]['fedauth']['stor']['rq'] = $_REQUEST;
59            $_SESSION[DOKU_COOKIE]['fedauth']['stor']['gt'] = $_GET;
60            $_SESSION[DOKU_COOKIE]['fedauth']['stor']['pt'] = $_POST;
61            $_SESSION[DOKU_COOKIE]['fedauth']['stor']['fs'] = $_FILES;
62            $_SESSION[DOKU_COOKIE]['fedauth']['stor']['rp'] = $HTTP_RAW_POST_DATA;
63            $_SESSION[DOKU_COOKIE]['fedauth']['stor']['hg'] = $HTTP_GET_VARS;
64            $_SESSION[DOKU_COOKIE]['fedauth']['stor']['hp'] = $HTTP_POST_VARS;
65            $_SESSION[DOKU_COOKIE]['fedauth']['stor']['hf'] = $HTTP_POST_FILES;
66        }
67        // if current command is not 'fedauth' this means
68/*print('<pre>reqe='.print_r($_REQUEST, true).'</pre>');
69print('<pre>get='.print_r($_GET, true).'</pre>');
70print('<pre>post='.print_r($_POST, true).'</pre>');
71exit; // */
72
73        $svcadd = ($renew || empty($_SERVER['REMOTE_USER'])) ? '' : '&mode=add';
74        $svcdata = (empty($data)) ? '' : '&svcdata=' . urlencode(base64_encode($data));
75        $return_to = wl($ID, 'do=fedauth', true, '&') . '&id=' . $ID . '&fa[signedin]['.$this->provid.']=1' . $svcdata . $svcadd;
76        // process the request
77        $svc =& $this->getService($pro);
78        $result = $svc->request($data, $return_to);
79        if ($result == -1) {
80            return $this->error('oidurlreq');
81        }
82        $this->success = true;
83
84        // redirect to OpenID provider for authentication
85        header('Location: ' . $result);
86        exit;
87    }
88
89} /* fa_signin */
90
91/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
92