xref: /plugin/combo/ComboStrap/Identity.php (revision c3437056399326d621a01da73b649707fbb0ae69)
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 TestRequest;
18
19class Identity
20{
21
22    const CANONICAL = "identity";
23    const CONF_ENABLE_LOGO_ON_IDENTITY_FORMS = "enableLogoOnIdentityForms";
24    const JS_NAVIGATION_ANONYMOUS_VALUE = "anonymous";
25    const JS_NAVIGATION_SIGNED_VALUE = "signed";
26    /**
27     * A javascript indicator
28     * to know if the user is logged in or not
29     * (ie public or not)
30     */
31    const JS_NAVIGATION_INDICATOR = "navigation";
32
33    /**
34     * Is logged in
35     * @return boolean
36     */
37    public static function isLoggedIn()
38    {
39        $loggedIn = false;
40        global $INPUT;
41        if ($INPUT->server->has('REMOTE_USER')) {
42            $loggedIn = true;
43        }
44        return $loggedIn;
45    }
46
47    /**
48     * @param TestRequest $request
49     * @param string $user
50     */
51    public static function becomeSuperUser(&$request = null, $user = 'admin')
52    {
53        global $conf;
54        $conf['useacl'] = 1;
55        $conf['superuser'] = $user;
56        $conf['remoteuser'] = $user;
57
58        if ($request != null) {
59            $request->setServer('REMOTE_USER', $user);
60        } else {
61            global $INPUT;
62            $INPUT->server->set('REMOTE_USER', $user);
63            // same as $_SERVER['REMOTE_USER'] = $user;
64        }
65
66        // $_SERVER[] = $user;
67        // global $USERINFO;
68        // $USERINFO['grps'] = array('admin', 'user');
69
70        // global $INFO;
71        // $INFO['ismanager'] = true;
72
73    }
74
75    /**
76     * @param $request
77     * @param string $user - the user to login
78     */
79    public static function logIn(&$request, $user = 'defaultUser')
80    {
81
82        $request->setServer('REMOTE_USER', $user);
83
84        /**
85         * The {@link getSecurityToken()} needs it
86         */
87        global $INPUT;
88        $INPUT->server->set('REMOTE_USER', $user);
89
90    }
91
92    /**
93     * @return bool if edit auth
94     */
95    public static function isWriter($pageId = null): bool
96    {
97        if ($pageId == null) {
98            $pageId = Page::createPageFromGlobalDokuwikiId();
99        }
100        if ($_SERVER['REMOTE_USER']) {
101            $perm = auth_quickaclcheck($pageId);
102        } else {
103            $perm = auth_aclcheck($pageId, '', null);
104        }
105
106        if ($perm >= AUTH_EDIT) {
107            return true;
108        } else {
109            return false;
110        }
111
112    }
113
114    public static function isAdmin()
115    {
116        global $INFO;
117        if (!empty($INFO)) {
118            return $INFO['isadmin'];
119        } else {
120            return auth_isadmin(self::getUser(), self::getUserGroups());
121        }
122    }
123
124    public static function isMember($group)
125    {
126
127        return auth_isMember($group, self::getUser(), self::getUserGroups());
128
129    }
130
131    public static function isManager()
132    {
133        global $INFO;
134        if ($INFO !== null) {
135            return $INFO['ismanager'];
136        } else {
137            /**
138             * In test
139             */
140            return auth_ismanager();
141        }
142    }
143
144    public static function getUser(): string
145    {
146        global $INPUT;
147        $user = $INPUT->server->str('REMOTE_USER');
148        if (empty($user)) {
149            return "Anonymous";
150        }
151        return $user;
152    }
153
154    private static function getUserGroups()
155    {
156        global $USERINFO;
157        return is_array($USERINFO) ? $USERINFO['grps'] : array();
158    }
159
160    public static function getLogoHtml()
161    {
162        /**
163         * Logo
164         */
165        $tagAttributes = TagAttributes::createEmpty("register");
166        $tagAttributes->addComponentAttributeValue(Dimension::WIDTH_KEY, "72");
167        $tagAttributes->addComponentAttributeValue(Dimension::HEIGHT_KEY, "72");
168        $tagAttributes->addClassName("logo");
169        return Site::getLogoImgHtmlTag($tagAttributes);
170    }
171
172    /**
173     * @param Doku_Form $form
174     * @param string $classPrefix
175     * @param bool $includeLogo
176     * @return string
177     */
178    public static function getHeaderHTML(Doku_Form $form, $classPrefix, $includeLogo = true)
179    {
180        if (isset($form->_content[0]["_legend"])) {
181
182            $title = $form->_content[0]["_legend"];
183            /**
184             * Logo
185             */
186            $logoHtmlImgTag = "";
187            if (
188                PluginUtility::getConfValue(Identity::CONF_ENABLE_LOGO_ON_IDENTITY_FORMS, 1)
189                &&
190                $includeLogo === true
191            ) {
192                $logoHtmlImgTag = Identity::getLogoHtml();
193            }
194            /**
195             * Don't use `header` in place of
196             * div because this is a HTML5 tag
197             *
198             * On php 5.6, the php test library method {@link \phpQueryObject::htmlOuter()}
199             * add the below meta tag
200             * <meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/>
201             *
202             */
203            return <<<EOF
204<div class="$classPrefix-header">
205    $logoHtmlImgTag
206    <h1>$title</h1>
207</div>
208EOF;
209        }
210        return "";
211    }
212
213    public static function isReader(string $pageId): bool
214    {
215        $perm = self::getPerm($pageId);
216
217        if ($perm >= AUTH_READ) {
218            return true;
219        } else {
220            return false;
221        }
222
223    }
224
225    private static function getPerm(string $pageId)
226    {
227        if ($pageId == null) {
228            $pageId = Page::createPageFromRequestedPage()->getDokuwikiId();
229        }
230        if ($_SERVER['REMOTE_USER']) {
231            $perm = auth_quickaclcheck($pageId);
232        } else {
233            $perm = auth_aclcheck($pageId, '', null);
234        }
235        return $perm;
236    }
237
238
239}
240