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
21/**
22 * Class admin_plugin_vbsso.
23 *
24 * @codingStandardsIgnoreStart
25 */
26class admin_plugin_vbsso extends DokuWiki_Admin_Plugin {
27
28    /**
29     * Constructor.
30     *
31     * @return mixed.
32     */
33    function admin_plugin_vbsso() {
34        $this->setupLocale();
35    }
36
37    /**
38     * @codingStandardsIgnoreEnd
39     */
40
41    /**
42     * Menu text.
43     *
44     * @param mixed $language language
45     *
46     * @return string
47     */
48    function getMenuText($language) {
49        $tpl = $this->make_html_from_tpl(__DIR__ . '/views/admin.getmenutext.tpl', array(DOKU_URL, VBSSO_PRODUCT_ID));
50
51        return $tpl . $this->getLang('vbsso_settings');
52    }
53
54    /**
55     * Handle user request.
56     *
57     * @return void
58     */
59    function handle() {
60        global $ID;
61        if (isset($_POST['vbsso_submit'])) {
62            vbsso_save_mediawiki_settings();
63            //Page content already loaded with previous settings, we have to reload page
64            sharedapi_url_redirect(wl($ID, array('do' => 'admin', 'page' => 'vbsso'), TRUE, '&'));
65        }
66    }
67
68    /**
69     * Output appropriate html
70     *
71     * @return void
72     */
73    function html() {
74        global $vbsso_settings, $vbsso_platform_settings;
75
76        $roles = array_merge(vbsso_get_acl_groups(), array('', 'admin'));
77        ksort($roles, SORT_STRING);
78
79        $show_vbsso_show_footer[VBSSO_PLATFORM_FOOTER_LINK_SHOW_EVERYWHERE] =
80            ($vbsso_settings[VBSSO_PLATFORM_FOOTER_LINK_PROPERTY]) ? 'checked' : '';
81        $show_vbsso_show_footer[VBSSO_PLATFORM_FOOTER_LINK_SHOW_NONE] =
82            (!$vbsso_settings[VBSSO_PLATFORM_FOOTER_LINK_PROPERTY]) ? 'checked' : '';
83        $login_through_vb = ($vbsso_settings[VBSSO_NAMED_EVENT_FIELD_LOGIN_THROUGH_VB_PAGE]) ? 'checked' : '';
84
85        $extensions = vbsso_verify_loaded_extensions();
86        $error_message = '';
87        if (count($extensions)) {
88            $error_message =
89                $this->make_html_from_tpl(__DIR__ . '/views/admin.htm1.tpl', array(implode(', ', $extensions)));
90        }
91
92        $footer_options = vbsso_get_platform_footer_link_options();
93        $disabled =
94            ($vbsso_platform_settings[VBSSO_NAMED_EVENT_FIELD_LOGIN_VBULLETIN_URL]) ? 'disabled="disabled"' : '';
95        $vbsso_form = $error_message;
96        $params = array($this->getLang('vbsso_settings'),
97            VBSSO_PLATFORM_FOOTER_LINK_DESCRIPTION_HTML,
98            VBSSO_PLATFORM_FOOTER_LINK_PROPERTY . VBSSO_PLATFORM_FOOTER_LINK_SHOW_EVERYWHERE,
99            VBSSO_PLATFORM_FOOTER_LINK_PROPERTY,
100            VBSSO_PLATFORM_FOOTER_LINK_SHOW_EVERYWHERE,
101            $show_vbsso_show_footer[VBSSO_PLATFORM_FOOTER_LINK_SHOW_EVERYWHERE],
102            VBSSO_PLATFORM_FOOTER_LINK_PROPERTY . VBSSO_PLATFORM_FOOTER_LINK_SHOW_EVERYWHERE,
103            $footer_options[1],
104            VBSSO_PLATFORM_FOOTER_LINK_PROPERTY . VBSSO_PLATFORM_FOOTER_LINK_SHOW_NONE,
105            VBSSO_PLATFORM_FOOTER_LINK_PROPERTY,
106            VBSSO_PLATFORM_FOOTER_LINK_SHOW_NONE,
107            $show_vbsso_show_footer[VBSSO_PLATFORM_FOOTER_LINK_SHOW_NONE],
108            VBSSO_PLATFORM_FOOTER_LINK_PROPERTY . VBSSO_PLATFORM_FOOTER_LINK_SHOW_NONE,
109            $footer_options[0],
110            VBSSO_NAMED_EVENT_FIELD_API_KEY,
111            VBSSO_NAMED_EVENT_FIELD_API_KEY_TITLE,
112            VBSSO_NAMED_EVENT_FIELD_API_KEY,
113            VBSSO_NAMED_EVENT_FIELD_API_KEY,
114            $vbsso_settings[VBSSO_NAMED_EVENT_FIELD_API_KEY],
115            $disabled,
116            VBSSO_NAMED_EVENT_FIELD_LISTENER_URL,
117            VBSSO_NAMED_EVENT_FIELD_LISTENER_URL_TITLE,
118            VBSSO_NAMED_EVENT_FIELD_LISTENER_URL,
119            VBSSO_NAMED_EVENT_FIELD_LISTENER_URL,
120            DOKU_URL,
121            VBSSO_PRODUCT_ID,
122            VBSSO_PRODUCT_ID,
123            VBSSO_PRODUCT_ID,
124            VBSSO_NAMED_EVENT_FIELD_LOGIN_THROUGH_VB_PAGE,
125            VBSSO_NAMED_EVENT_FIELD_LOGIN_THROUGH_VB_PAGE,
126            $login_through_vb,
127            VBSSO_NAMED_EVENT_FIELD_LOGIN_THROUGH_VB_PAGE,
128            VBSSO_NAMED_EVENT_FIELD_LOGIN_THROUGH_VB_PAGE_TITLE);
129
130        $vbsso_form .= $this->make_html_from_tpl(__DIR__ . '/views/admin.html2-1.tpl', $params);
131
132        //Usergroups Associations
133        if ($vbsso_platform_settings[VBSSO_NAMED_EVENT_FIELD_USERGROUPS_URL]) {
134
135            $groups = array();
136            foreach ($roles as $group) {
137                $groups[$group] = $group;
138            }
139            $vb_usergroups = vbsso_get_vb_usergroups();
140            $vbsso_usergroups_assoc = json_decode($vbsso_settings[VBSSO_NAMED_EVENT_FIELD_USERGROUPS_ASSOC]);
141
142            $vbsso_form .= $this->make_html_from_tpl(__DIR__ . '/views/admin.html2-2.open.tpl');
143
144            foreach ($vb_usergroups as $vb_usergroup) {
145                $gid = $vb_usergroup->usergroupid;
146                $params_select = array (VBSSO_NAMED_EVENT_FIELD_USERGROUPS_ASSOC,
147                                        $gid,
148                                        $vb_usergroup->title,
149                                        VBSSO_NAMED_EVENT_FIELD_USERGROUPS_ASSOC,
150                                        $gid,
151                                        VBSSO_NAMED_EVENT_FIELD_USERGROUPS_ASSOC,
152                                        $gid);
153
154                $vbsso_form .= $this->make_html_from_tpl(__DIR__ . '/views/admin.html2-2.select.open.tpl',
155                        $params_select);
156                foreach ($groups as $group) {
157                    $select =
158                        ($vbsso_usergroups_assoc && $vbsso_usergroups_assoc->$gid == $group) ? 'selected="selected"'
159                            : '';
160                    $vbsso_form .= $this->make_html_from_tpl(__DIR__ . '/views/admin.html2-2.option.tpl',
161                        array($group, $select, $group));
162                }
163                $vbsso_form .= $this->make_html_from_tpl(__DIR__ . '/views/admin.html2-2.select.close.tpl');
164            }
165
166            $vbsso_form .= $this->make_html_from_tpl(__DIR__ . '/views/admin.html2-2.close.tpl');
167        }
168
169        $vbsso_form .= $this->make_html_from_tpl(__DIR__ . '/views/admin.html2-3.tpl');
170        if (!is_writable(VBSSO_VBSSO_CONFIG_FILE)) {
171            ptln($this->make_html_from_tpl("./views/admin.html3.tpl", array(VBSSO_VBSSO_CONFIG_FILE)));
172        }
173        if (!is_writable(VBSSO_PLATFORM_CONFIG_FILE)) {
174            ptln($this->make_html_from_tpl("./views/admin.html3.tpl", array(VBSSO_PLATFORM_CONFIG_FILE)));
175        }
176
177        ptln($vbsso_form);
178
179    }
180
181    /**
182     * Output string with html
183     *
184     * @param string $file path to template file
185     * @param array $params array of parametrs to insert in template file
186     *
187     * @return void
188     */
189    private function make_html_from_tpl($file, $params = array()) {
190        $tpl = file_get_contents($file);
191        foreach ($params as $key => $param) {
192            $search = '[[content-' . $key . ']]';
193            $tpl = str_replace($search, $param, $tpl);
194        }
195
196        return $tpl;
197    }
198}
199