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