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    const FORM_IDENTITY_CLASS = "form-identity";
34
35    /**
36     * Is logged in
37     * @return boolean
38     */
39    public static function isLoggedIn()
40    {
41        $loggedIn = false;
42        global $INPUT;
43        if ($INPUT->server->has('REMOTE_USER')) {
44            $loggedIn = true;
45        }
46        return $loggedIn;
47    }
48
49    /**
50     * @param TestRequest $request
51     * @param string $user
52     */
53    public static function becomeSuperUser(&$request = null, $user = 'admin')
54    {
55        global $conf;
56        $conf['useacl'] = 1;
57        $conf['superuser'] = $user;
58        $conf['remoteuser'] = $user;
59
60        if ($request != null) {
61            $request->setServer('REMOTE_USER', $user);
62        } else {
63            global $INPUT;
64            $INPUT->server->set('REMOTE_USER', $user);
65            // same as $_SERVER['REMOTE_USER'] = $user;
66        }
67
68        // $_SERVER[] = $user;
69        // global $USERINFO;
70        // $USERINFO['grps'] = array('admin', 'user');
71
72        // global $INFO;
73        // $INFO['ismanager'] = true;
74
75    }
76
77    /**
78     * @param $request
79     * @param string $user - the user to login
80     */
81    public static function logIn(&$request, $user = 'defaultUser')
82    {
83
84        $request->setServer('REMOTE_USER', $user);
85
86        /**
87         * The {@link getSecurityToken()} needs it
88         */
89        global $INPUT;
90        $INPUT->server->set('REMOTE_USER', $user);
91
92    }
93
94    /**
95     * @return bool if edit auth
96     */
97    public static function isWriter($pageId = null): bool
98    {
99        if ($pageId == null) {
100            $pageId = Page::createPageFromGlobalDokuwikiId();
101        }
102        if ($_SERVER['REMOTE_USER']) {
103            $perm = auth_quickaclcheck($pageId);
104        } else {
105            $perm = auth_aclcheck($pageId, '', null);
106        }
107
108        if ($perm >= AUTH_EDIT) {
109            return true;
110        } else {
111            return false;
112        }
113
114    }
115
116    public static function isAdmin()
117    {
118        global $INFO;
119        if (!empty($INFO)) {
120            return $INFO['isadmin'];
121        } else {
122            return auth_isadmin(self::getUser(), self::getUserGroups());
123        }
124    }
125
126    public static function isMember($group)
127    {
128
129        return auth_isMember($group, self::getUser(), self::getUserGroups());
130
131    }
132
133    public static function isManager()
134    {
135        global $INFO;
136        if ($INFO !== null) {
137            return $INFO['ismanager'];
138        } else {
139            /**
140             * In test
141             */
142            return auth_ismanager();
143        }
144    }
145
146    public static function getUser(): string
147    {
148        global $INPUT;
149        $user = $INPUT->server->str('REMOTE_USER');
150        if (empty($user)) {
151            return "Anonymous";
152        }
153        return $user;
154    }
155
156    private static function getUserGroups()
157    {
158        global $USERINFO;
159        return is_array($USERINFO) ? $USERINFO['grps'] : array();
160    }
161
162    /**
163     * @param Doku_Form $form
164     * @param string $classPrefix
165     * @param bool $includeLogo
166     * @return string
167     */
168    public static function getHeaderHTML(Doku_Form $form, $classPrefix, $includeLogo = true)
169    {
170        if (isset($form->_content[0]["_legend"])) {
171
172            $title = $form->_content[0]["_legend"];
173            /**
174             * Logo
175             */
176            $logoHtmlImgTag = "";
177            if (
178                PluginUtility::getConfValue(Identity::CONF_ENABLE_LOGO_ON_IDENTITY_FORMS, 1)
179                &&
180                $includeLogo === true
181            ) {
182                $logoHtmlImgTag = Site::getLogoHtml();
183            }
184            /**
185             * Don't use `header` in place of
186             * div because this is a HTML5 tag
187             *
188             * On php 5.6, the php test library method {@link \phpQueryObject::htmlOuter()}
189             * add the below meta tag
190             * <meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/>
191             *
192             */
193            return <<<EOF
194<div class="$classPrefix-header">
195    $logoHtmlImgTag
196    <h1>$title</h1>
197</div>
198EOF;
199        }
200        return "";
201    }
202
203    public static function isReader(string $pageId): bool
204    {
205        $perm = self::getPerm($pageId);
206
207        if ($perm >= AUTH_READ) {
208            return true;
209        } else {
210            return false;
211        }
212
213    }
214
215    private static function getPerm(string $pageId)
216    {
217        if ($pageId == null) {
218            $pageId = Page::createPageFromRequestedPage()->getDokuwikiId();
219        }
220        if ($_SERVER['REMOTE_USER']) {
221            $perm = auth_quickaclcheck($pageId);
222        } else {
223            $perm = auth_aclcheck($pageId, '', null);
224        }
225        return $perm;
226    }
227
228    public static function addPrimaryColorCssRuleIfSet(?string $content): ?string
229    {
230        if ($content === null) {
231            return null;
232        }
233        $primaryColor = Site::getPrimaryColorValue();
234        if ($primaryColor !== null) {
235            $identityClass = self::FORM_IDENTITY_CLASS;
236            $content .= <<<EOF
237.$identityClass button[type="submit"]{
238   background-color: $primaryColor;
239   border-color: $primaryColor;
240}
241EOF;
242        }
243        return $content;
244    }
245
246    public static function getHtmlStyleTag(string $componentId): string
247    {
248        $loginCss = Snippet::createInternalCssSnippet($componentId);
249        $content = $loginCss->getInternalInlineAndFileContent();
250        $content = Identity::addPrimaryColorCssRuleIfSet($content);
251        $class = $loginCss->getClass();
252        return <<<EOF
253<style class="$class">
254$content
255</style>
256EOF;
257
258    }
259
260    public static function addIdentityClass(&$class, string $formClass)
261    {
262
263        $formClass = Identity::FORM_IDENTITY_CLASS . " " . $formClass;
264        if (isset($class)) {
265            $class .= " " . $formClass;
266        } else {
267            $class = $formClass;
268        }
269
270    }
271
272
273}
274