xref: /plugin/combo/ComboStrap/Identity.php (revision 0bb301e4810e02240c0e53e1eea739c8f546dd14)
1<?php
2/**
3 * Copyright (c) 2021. ComboStrap, Inc. and its affiliates. All Rights Reserved.
4 *
5 * This source code is licensed under the GPL license found in the
6 * COPYING  file in the root directory of this source tree.
7 *
8 * @license  GPL 3 (https://www.gnu.org/licenses/gpl-3.0.en.html)
9 * @author   ComboStrap <support@combostrap.com>
10 *
11 */
12
13namespace ComboStrap;
14
15
16use Doku_Form;
17use dokuwiki\Form\Form;
18use TestRequest;
19
20class Identity
21{
22
23    const CANONICAL = "identity";
24    const CONF_ENABLE_LOGO_ON_IDENTITY_FORMS = "enableLogoOnIdentityForms";
25    const JS_NAVIGATION_ANONYMOUS_VALUE = "anonymous";
26    const JS_NAVIGATION_SIGNED_VALUE = "signed";
27    /**
28     * A javascript indicator
29     * to know if the user is logged in or not
30     * (ie public or not)
31     */
32    const JS_NAVIGATION_INDICATOR = "navigation";
33
34    const FORM_IDENTITY_CLASS = "form-identity";
35
36    /**
37     * Is logged in
38     * @return boolean
39     */
40    public static function isLoggedIn()
41    {
42        $loggedIn = false;
43        global $INPUT;
44        if ($INPUT->server->has('REMOTE_USER')) {
45            $loggedIn = true;
46        }
47        return $loggedIn;
48    }
49
50    /**
51     * @param TestRequest $request
52     * @param string $user
53     */
54    public static function becomeSuperUser(&$request = null, $user = 'admin')
55    {
56        global $conf;
57        $conf['useacl'] = 1;
58        $conf['superuser'] = $user;
59        $conf['remoteuser'] = $user;
60
61        if ($request != null) {
62            $request->setServer('REMOTE_USER', $user);
63        }
64
65        /**
66         * used by {@link getSecurityToken()}
67         */
68        global $INPUT;
69        $INPUT->server->set('REMOTE_USER', $user);
70        // same as $_SERVER['REMOTE_USER'] = $user;
71
72
73        // $_SERVER[] = $user;
74        // global $USERINFO;
75        // $USERINFO['grps'] = array('admin', 'user');
76
77        // global $INFO;
78        // $INFO['ismanager'] = true;
79
80    }
81
82    /**
83     * @param $request
84     * @param string $user - the user to login
85     */
86    public static function logIn(&$request, $user = 'defaultUser')
87    {
88
89        $request->setServer('REMOTE_USER', $user);
90
91        /**
92         * The {@link getSecurityToken()} needs it
93         */
94        global $INPUT;
95        $INPUT->server->set('REMOTE_USER', $user);
96
97    }
98
99    /**
100     * @return bool if edit auth
101     */
102    public static function isWriter($pageId = null): bool
103    {
104        if ($pageId == null) {
105            $pageId = Page::createPageFromGlobalDokuwikiId();
106        }
107        if ($_SERVER['REMOTE_USER']) {
108            $perm = auth_quickaclcheck($pageId);
109        } else {
110            $perm = auth_aclcheck($pageId, '', null);
111        }
112
113        if ($perm >= AUTH_EDIT) {
114            return true;
115        } else {
116            return false;
117        }
118
119    }
120
121    public static function isAdmin()
122    {
123        global $INFO;
124        if (!empty($INFO)) {
125            return $INFO['isadmin'];
126        } else {
127            return auth_isadmin(self::getUser(), self::getUserGroups());
128        }
129    }
130
131    public static function isMember($group)
132    {
133
134        return auth_isMember($group, self::getUser(), self::getUserGroups());
135
136    }
137
138    public static function isManager()
139    {
140        global $INFO;
141        if ($INFO !== null) {
142            return $INFO['ismanager'];
143        } else {
144            /**
145             * In test
146             */
147            return auth_ismanager();
148        }
149    }
150
151    public static function getUser(): string
152    {
153        global $INPUT;
154        $user = $INPUT->server->str('REMOTE_USER');
155        if (empty($user)) {
156            return "Anonymous";
157        }
158        return $user;
159    }
160
161    private static function getUserGroups()
162    {
163        global $USERINFO;
164        return is_array($USERINFO) ? $USERINFO['grps'] : array();
165    }
166
167    /**
168     * @param Doku_Form|Form $form
169     * @param string $classPrefix
170     * @param bool $includeLogo
171     * @return string
172     */
173    public static function getHeaderHTML($form, $classPrefix, $includeLogo = true)
174    {
175
176        $class = get_class($form);
177        switch ($class) {
178            case Doku_Form::class:
179                /**
180                 * Old one
181                 * @var Doku_Form $form
182                 */
183                $legend = $form->_content[0]["_legend"];
184                if (!isset($legend)) {
185                    return "";
186                }
187
188                $title = $legend;
189                break;
190            case Form::class;
191                /**
192                 * New One
193                 * @var Form $form
194                 */
195                $pos = $form->findPositionByType("fieldsetopen");
196                if ($pos == false) {
197                    return "";
198                }
199
200                $title = $form->getElementAt($pos)->val();
201                break;
202            default:
203                LogUtility::msg("Internal Error: Unknown form class " . $class);
204                return "";
205        }
206
207        /**
208         * Logo
209         */
210        $logoHtmlImgTag = "";
211        if (
212            PluginUtility::getConfValue(Identity::CONF_ENABLE_LOGO_ON_IDENTITY_FORMS, 1)
213            &&
214            $includeLogo === true
215        ) {
216            $logoHtmlImgTag = Site::getLogoHtml();
217        }
218        /**
219         * Don't use `header` in place of
220         * div because this is a HTML5 tag
221         *
222         * On php 5.6, the php test library method {@link \phpQueryObject::htmlOuter()}
223         * add the below meta tag
224         * <meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/>
225         *
226         */
227        return <<<EOF
228<div class="$classPrefix-header">
229    $logoHtmlImgTag
230    <h1>$title</h1>
231</div>
232EOF;
233
234    }
235
236    public static function isReader(string $pageId): bool
237    {
238        $perm = self::getPerm($pageId);
239
240        if ($perm >= AUTH_READ) {
241            return true;
242        } else {
243            return false;
244        }
245
246    }
247
248    private static function getPerm(string $pageId)
249    {
250        if ($pageId == null) {
251            $pageId = Page::createPageFromRequestedPage()->getDokuwikiId();
252        }
253        if ($_SERVER['REMOTE_USER']) {
254            $perm = auth_quickaclcheck($pageId);
255        } else {
256            $perm = auth_aclcheck($pageId, '', null);
257        }
258        return $perm;
259    }
260
261    public static function addPrimaryColorCssRuleIfSet(?string $content): ?string
262    {
263        if ($content === null) {
264            return null;
265        }
266        $primaryColor = Site::getPrimaryColorValue();
267        if ($primaryColor !== null) {
268            $identityClass = self::FORM_IDENTITY_CLASS;
269            $cssFormControl = BrandColors::getCssFormControlFocusColor($primaryColor);
270            $content .= <<<EOF
271.$identityClass button[type="submit"]{
272   background-color: $primaryColor;
273   border-color: $primaryColor;
274}
275$cssFormControl
276EOF;
277        }
278        return $content;
279    }
280
281    public static function getHtmlStyleTag(string $componentId): string
282    {
283        $loginCss = Snippet::createInternalCssSnippet($componentId);
284        $content = $loginCss->getInternalInlineAndFileContent();
285        $content = Identity::addPrimaryColorCssRuleIfSet($content);
286        $class = $loginCss->getClass();
287        return <<<EOF
288<style class="$class">
289$content
290</style>
291EOF;
292
293    }
294
295    public static function addIdentityClass(&$class, string $formClass)
296    {
297
298        $formClass = Identity::FORM_IDENTITY_CLASS . " " . $formClass;
299        if (isset($class)) {
300            $class .= " " . $formClass;
301        } else {
302            $class = $formClass;
303        }
304
305    }
306
307
308}
309