1<?php
2/**
3 * Action Plugin for authphpbb3.
4 *
5 * @license  GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 * @author   Eole <eole.dev@outlook.com>
7 */
8
9if (!defined('DOKU_INC')) {
10    die();
11}
12
13/**
14 * Action class for authphpbb3 plugin.
15 */
16class action_plugin_authphpbb3 extends DokuWiki_Action_Plugin {
17
18    /**
19     * Registers a callback function for a given event.
20     *
21     * @param Doku_Event_Handler $controller.
22     */
23    public function register(Doku_Event_Handler $controller) {
24        $controller->register_hook('HTML_LOGINFORM_OUTPUT', 'BEFORE', $this, 'handle_login_form');
25        $controller->register_hook('COMMON_USER_LINK', 'AFTER', $this, 'handle_user_link');
26    }
27
28    /**
29     * Replaces the DokuWiki form by the phpBB login form.
30     *
31     * @param Doku_Event    $event    Event.
32     * @param object        $param    Parameters.
33     */
34    public function handle_login_form(&$event, $param) {
35        global $auth;
36        global $ID;
37        $use_inline_css = $this->getConf('phpbb_inline_style');
38        $inline_css1 = '';
39        $inline_css2 = '';
40        $phpbb_url = '';
41        $cache = null;
42        $elem = '';
43        $pos = 0;
44
45        if (!is_a($auth, 'auth_plugin_authphpbb3')) {
46            return;
47        }
48        $phpbb_url = $auth->get_phpbb_url();
49        if ($phpbb_url === false) {
50            return ;
51        }
52        $phpbb_url = rtrim($phpbb_url, '/');
53        // Form's PHP script.
54        $event->data->params['action'] = $phpbb_url . '/ucp.php?mode=login';
55        // Username field.
56        $inline_css1 = ($use_inline_css ? ' style="padding-right:10px"' : '');
57        $elem = '<label class="block" for="username">' .
58                    '<span' . $inline_css1 . '>' . $this->getLang('login_login') . '</span>' .
59                    '<input type="text" tabindex="1" name="username" id="username" class="edit">' .
60                '</label><br/>';
61        $pos = $event->data->findElementByAttribute('name', 'u');
62        if ($pos === false) {
63            return ;
64        }
65        $event->data->replaceElement($pos, null);
66        $event->data->insertElement($pos, $elem);
67        // Password field.
68        $inline_css1 = ($use_inline_css ? ' style="padding-right:10px"' : '');
69        $elem = '<label class="block" for="password">' .
70                    '<span' . $inline_css1 . '>' . $this->getLang('login_password') . '</span>' .
71                    '<input type="password" tabindex="2" name="password" id="password" class="edit">' .
72                '</label><br/>';
73        $pos = $event->data->findElementByAttribute('name', 'p');
74        if ($pos === false) {
75            return ;
76        }
77        $event->data->replaceElement($pos, null);
78        $event->data->insertElement($pos, $elem);
79        // Remember me check box.
80        $inline_css1 = ($use_inline_css ? ' style="margin-left:20%;margin-bottom:10px;"' : '');
81        $inline_css2 = ($use_inline_css ? ' style="padding-left:5px"' : '');
82        $elem = '<label class="simple"' . $inline_css1 . ' for="autologin">' .
83                    '<input type="checkbox" name="autologin" id="autologin" tabindex="3">' .
84                    '<span' . $inline_css2 . '>' . $this->getLang('login_remember') . '</span>' .
85                '</label>';
86        $pos = $event->data->findElementByAttribute('name', 'r');
87        if ($pos === false) {
88            return ;
89        }
90        $event->data->replaceElement($pos, null);
91        $event->data->insertElement($pos, $elem);
92        // View online check box.
93        $inline_css1 = ($use_inline_css ? ' style="margin-left:20%;margin-bottom:10px;"' : '');
94        $inline_css2 = ($use_inline_css ? ' style="padding-left:5px"' : '');
95        $elem = '<label class="simple"' . $inline_css1 . ' for="viewonline">' .
96                    '<input type="checkbox" name="viewonline" id="viewonline" tabindex="4">' .
97                    '<span' . $inline_css2 . '>' . $this->getLang('login_viewonline') . '</span>' .
98                '</label>';
99        $event->data->insertElement($pos + 1, $elem);
100        // Log in button.
101        $elem = '<button type="submit" name="login" tabindex="5" value="' . $this->getLang('login_button') . '">' .
102                    $this->getLang('login_button') .
103                '</button>';
104        $pos = $event->data->findElementByType('button');
105        if ($pos === false) {
106            return ;
107        }
108        $event->data->replaceElement($pos, null);
109        $event->data->insertElement($pos, $elem);
110        // Hidden field for redirection.
111        $elem = '<input type="hidden" name="redirect" value="' . wl($ID, '', true) . '">';
112        $event->data->insertElement($pos - 1, $elem);
113        // Forum URL.
114        $event->data->addElement('<p>' . sprintf($this->getLang('login_bottom_text'), $phpbb_url) . '</p>');
115    }
116
117    /**
118     * Adds a link to phpBB profile on all users' names.
119     *
120     * @param Doku_Event    $event    Event.
121     * @param object        $param    Parameters.
122     */
123    public function handle_user_link(&$event, $param) {
124        global $auth, $conf;
125        $profile = '<a href="%s" class="interwiki iw_user" rel="nofollow" target="_blank">%s</a>';
126
127        if (!is_a($auth, 'auth_plugin_authphpbb3')) {
128            return;
129        }
130        if (($conf['showuseras'] !== 'username_link') || $event->data['textonly']) {
131            return ;
132        }
133        if (empty($event->data['name'])) {
134            $event->data['name'] = $event->data['username'];
135        }
136        $data = $auth->getUserData($event->data['username']);
137        if (is_array($data) &&
138            array_key_exists('phpbb_profile', $data) &&
139            array_key_exists('name', $data) &&
140            !empty($data['phpbb_profile']) &&
141            !empty($data['name'])) {
142            $profile = sprintf($profile, $data['phpbb_profile'], $data['name']);
143        } else {
144            $profile = sprintf($profile, '#', $event->data['name']);
145        }
146        $event->data = array(
147            'username'    => $event->data['username'],
148            'name'        => $event->data['name'],
149            'link'        => $event->data['link'],
150            'userlink'    => $profile,
151            'textonly'    => $event->data['textonly']
152        );
153    }
154}
155