1<?php
2/**
3 * -----------------------------------------------------------------------
4 * vBSSO is a solution which helps you connect to different software platforms
5 * via secure Single Sign-On.
6 *
7 * Copyright (c) 2011-2017 vBSSO. All Rights Reserved.
8 * This software is the proprietary information of vBSSO.
9 *
10 * Author URI: http://www.vbsso.com
11 * License: GPL version 2 or later -
12 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
13 * -----------------------------------------------------------------------
14 */
15
16// must be run within Dokuwiki
17if (!defined('DOKU_INC')) {
18    die();
19}
20
21if (!defined('DIR')) {
22    define('DIR', dirname(__FILE__) . '/');
23}
24if (!defined('VBSSO_PLATFORM_CONFIG_FILE')) {
25    define('VBSSO_PLATFORM_CONFIG_FILE', DIR . 'platform.conf');
26}
27if (!defined('VBSSO_VBSSO_CONFIG_FILE')) {
28    define('VBSSO_VBSSO_CONFIG_FILE', DIR . 'vbsso.conf');
29}
30require_once(DIR . 'vendor/com.extremeidea.vbsso/vbsso-connect-shared/vbsso_shared.php');
31require_once(DIR . 'includes/api.php');
32
33if (file_exists(DIR . 'config.custom.php')) {
34    include_once(DIR . 'config.custom.php');
35}
36
37global $vbsso_platform_settings, $vbsso_settings;
38$vbsso_platform_settings = ($vbsso_platform_settings) ? $vbsso_platform_settings : vbsso_get_platform_settings();
39$vbsso_settings = ($vbsso_settings) ? $vbsso_settings : vbsso_get_dokuwiki_settings();
40
41/**
42 * Class action_plugin_vbsso.
43 *
44 * @codingStandardsIgnoreStart
45 */
46class action_plugin_vbsso extends DokuWiki_Action_Plugin {
47
48    /**
49     * @codingStandardsIgnoreEnd
50     */
51    private $vbsso_platform_settings;
52    private $vbsso_settings;
53
54    /**
55     * Action_plugin_vbsso constructor.
56     *
57     * @return mixed
58     */
59    function __construct() {
60        $this->vbsso_platform_settings = vbsso_get_platform_settings();
61        $this->vbsso_settings = vbsso_get_dokuwiki_settings();
62    }
63
64    /**
65     * Register
66     *
67     * @param object $controller controller
68     *
69     * @return void
70     */
71    function register(Doku_Event_Handler $controller) {
72        if (vbsso_get_platform_config_property(SHAREDAPI_PLATFORM_DOKUWIKI, VBSSO_CONFIG_PROPERTY_OVERRIDE_LINKS,
73            TRUE)) {
74            if (!empty($this->vbsso_platform_settings[VBSSO_NAMED_EVENT_FIELD_LOGIN_URL])) {
75                $controller->register_hook('ACTION_ACT_PREPROCESS', 'BEFORE', $this, 'vbsso_redirect_page_hook');
76            }
77
78            if (!empty($this->vbsso_platform_settings[VBSSO_NAMED_EVENT_FIELD_LOGIN_VBULLETIN_URL])
79                and !$this->vbsso_settings[VBSSO_NAMED_EVENT_FIELD_LOGIN_THROUGH_VB_PAGE]) {
80                $controller->register_hook('HTML_LOGINFORM_OUTPUT', 'BEFORE', $this, 'vbsso_loginform_page_hook');
81            }
82        }
83
84        $controller->register_hook('TPL_METAHEADER_OUTPUT', 'BEFORE', $this, 'vbsso_add_header');
85
86        $extensions = vbsso_verify_loaded_extensions();
87        if (count($extensions)) {
88            $controller->register_hook('TPL_ACT_RENDER', 'BEFORE', $this, 'vbsso_show_warnings', $extensions);
89        }
90    }
91
92    /**
93     * Redirect page hook
94     *
95     * @param object $event event
96     * @param mixed  $param param
97     *
98     * @return void
99     *
100     * @codingStandardsIgnoreStart
101     */
102    function vbsso_redirect_page_hook(&$event, $param) {
103        /**
104         * @codingStandardsIgnoreEnd
105         */
106        global $INFO;
107
108        switch ($event->data) {
109            case 'register':
110                $url = $this->vbsso_platform_settings[VBSSO_NAMED_EVENT_FIELD_REGISTER_URL];
111                sharedapi_url_redirect(sharedapi_url_add_destination($url));
112                break;
113            case 'logout':
114                $url = $this->vbsso_platform_settings[VBSSO_NAMED_EVENT_FIELD_LOGOUT_URL];
115                sharedapi_url_redirect(sharedapi_url_add_destination($url));
116                exit;
117            case 'login':
118                if ($this->vbsso_settings[VBSSO_NAMED_EVENT_FIELD_LOGIN_THROUGH_VB_PAGE]) {
119                    $url = $this->vbsso_platform_settings[VBSSO_NAMED_EVENT_FIELD_LOGIN_URL];
120                    sharedapi_url_redirect(sharedapi_url_add_destination($url));
121                }
122                break;
123            case 'profile':
124                if (isset($INFO['userinfo']) and !in_array('admin', $INFO['userinfo']['grps'])
125                    and $this->vbsso_settings[VBSSO_NAMED_EVENT_FIELD_SHOW_VBULLETIN_PROFILE]) {
126                    sharedapi_url_redirect($this->vbsso_platform_settings[VBSSO_NAMED_EVENT_FIELD_PROFILE_URL]);
127                }
128                break;
129        }
130    }
131
132    /**
133     * Login form page hook
134     *
135     * @param object $event event
136     * @param mixed  $param param
137     *
138     * @return void
139     *
140     * @codingStandardsIgnoreStart
141     */
142    function vbsso_loginform_page_hook(&$event, $param) {
143
144        /**
145         * @codingStandardsIgnoreEnd
146         */
147
148        global $INPUT;
149
150        $form = new Doku_Form(array('id' => 'dw__login',
151            'action' => $this->vbsso_platform_settings[VBSSO_NAMED_EVENT_FIELD_LOGIN_VBULLETIN_URL]), TRUE, 'post');
152        $form->startFieldset($this->getLang('btn_login'));
153        $form->addHidden('do', 'login');
154        $form->addElement(form_makeTextField('vb_login_username',
155            ((!$INPUT->bool('http_credentials')) ? $INPUT->str('u') : ''), $this->getLang('user'), 'focus__this',
156            'block'));
157        $form->addElement(form_makePasswordField('vb_login_password', $this->getLang('pass'), '', 'block'));
158        $form->addElement(form_makeCheckboxField('cookieuser', '1', $this->getLang('remember'), 'remember__me',
159            'simple'));
160        $form->addElement(form_makeButton('submit', '', $this->getLang('btn_login')));
161        $form->endFieldset();
162
163        if (actionOK('register')) {
164            $url = $this->vbsso_platform_settings[VBSSO_NAMED_EVENT_FIELD_REGISTER_URL];
165            $form->addElement('<p>' . $this->getLang('reghere') . ': ' . tpl_link(sharedapi_url_add_destination($url),
166                    $this->getLang('btn_register'), '', TRUE) . '</p>');
167        }
168
169        if (actionOK('resendpwd')) {
170            $url = $this->vbsso_platform_settings[VBSSO_NAMED_EVENT_FIELD_LOSTPASSWORD_URL];
171            $form->addElement('<p>' . $this->getLang('pwdforget') . ': ' . tpl_link(sharedapi_url_add_destination($url),
172                    $this->getLang('btn_resendpwd'), '', TRUE) . '</p>');
173        }
174
175        $event->data = $form;
176    }
177
178    /**
179     * Add header
180     *
181     * @param object $event event
182     * @param mixed  $param param
183     *
184     * @return void
185     *
186     * @codingStandardsIgnoreStart
187     */
188    function vbsso_add_header(&$event, $param) {
189        /**
190         * @codingStandardsIgnoreEnd
191         */
192
193        $event->data["script"][] = array("type" => "text/javascript",
194            "_data" => strip_tags(VBSSO_PLATFORM_FOOTER_GA_HTML(sharedapi_get_platforms(SHAREDAPI_PLATFORM_DOKUWIKI))));
195        if ($this->vbsso_settings[VBSSO_PLATFORM_FOOTER_LINK_PROPERTY]) {
196            $event->data["script"][] = array("type" => "text/javascript", "_data" => "function vbsso_add_js() {
197                    var newDiv = document.createElement('div');
198                    newDiv.innerHTML = '" . VBSSO_PLATFORM_FOOTER_LINK_HTML . "';
199                    document.body.appendChild(newDiv);
200                }
201                window.onload = vbsso_add_js;
202                ");
203        }
204    }
205
206    /**
207     * Show warnings
208     *
209     * @param object $event event
210     * @param mixed  $param param
211     *
212     * @return void
213     *
214     * @codingStandardsIgnoreStart
215     */
216    function vbsso_show_warnings(&$event, $param) {
217
218        /**
219         * @codingStandardsIgnoreEnd
220         */
221
222        global $INFO;
223
224        if (isset($INFO['userinfo']) and in_array('admin', $INFO['userinfo']['grps'])) {
225            $tpl = file_get_contents(__DIR__.'/views/action.tpl');
226            $error_message = implode(', ', $param);
227            $tpl = str_replace('[[content]]', $error_message, $tpl);
228            echo $tpl;
229        }
230    }
231}
232