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 dokuwiki\Form\InputElement; 19use dokuwiki\Ui\UserProfile; 20use TestRequest; 21 22class Identity 23{ 24 25 const CANONICAL = "identity"; 26 const CONF_ENABLE_LOGO_ON_IDENTITY_FORMS = "enableLogoOnIdentityForms"; 27 const JS_NAVIGATION_ANONYMOUS_VALUE = "anonymous"; 28 const JS_NAVIGATION_SIGNED_VALUE = "signed"; 29 /** 30 * A javascript indicator 31 * to know if the user is logged in or not 32 * (ie public or not) 33 */ 34 const JS_NAVIGATION_INDICATOR = "navigation"; 35 36 const FORM_IDENTITY_CLASS = "form-identity"; 37 public const FIELD_SET_TO_DELETE = ["fieldsetopen", "fieldsetclose"]; 38 public const CONF_DESIGNER_GROUP_NAME = "combo-conf-006"; 39 40 /** 41 * Is logged in 42 * @return boolean 43 */ 44 public static function isLoggedIn(): bool 45 { 46 global $_SERVER; 47 if (empty($_SERVER['REMOTE_USER'])) { 48 return false; 49 } 50 return true; 51 } 52 53 /** 54 * @param TestRequest $request 55 * @param string $user 56 */ 57 public static function becomeSuperUser(&$request = null, $user = 'admin') 58 { 59 global $conf; 60 $conf['useacl'] = 1; 61 $conf['superuser'] = $user; 62 $conf['remoteuser'] = $user; 63 64 if ($request != null) { 65 $request->setServer('REMOTE_USER', $user); 66 } 67 68 /** 69 * used by {@link getSecurityToken()} 70 */ 71 // same as 72 // global $INPUT; 73 // $INPUT->server->set('REMOTE_USER', $user); 74 $_SERVER['REMOTE_USER'] = $user; 75 76 // global $INFO; 77 // $INFO['ismanager'] = true; 78 79 80 /** 81 * 82 * Userinfo 83 * 84 * Email is Mandatory otherwise the {@link UserProfile} 85 * does not work 86 * 87 * USERINFO is also available via $INFO['userinfo'] 88 * See {@link basicinfo()} 89 */ 90 global $USERINFO; 91 $USERINFO['mail'] = "email@example.com"; 92 // $USERINFO['grps'] = array('admin', 'user'); 93 94 95 } 96 97 /** 98 * @param $request 99 * @param string $user - the user to login 100 */ 101 public static function logIn(&$request, $user = 'defaultUser') 102 { 103 104 $request->setServer('REMOTE_USER', $user); 105 106 /** 107 * The {@link getSecurityToken()} needs it 108 */ 109 global $INPUT; 110 $INPUT->server->set('REMOTE_USER', $user); 111 112 } 113 114 /** 115 * @return bool if edit auth 116 */ 117 public static function isWriter($wikiId = null): bool 118 { 119 120 if ($wikiId === null) { 121 $executionContext = ExecutionContext::getActualOrCreateFromEnv(); 122 try { 123 $wikiId = $executionContext->getRequestedPath()->getWikiId(); 124 } catch (ExceptionNotFound $e) { 125 return false; 126 } 127 } 128 /** 129 * There is also 130 * $INFO['writable'] === true 131 * See true if writable See https://www.dokuwiki.org/devel:environment#info 132 */ 133 if ($_SERVER['REMOTE_USER']) { 134 $perm = auth_quickaclcheck($wikiId); 135 } else { 136 $perm = auth_aclcheck($wikiId, '', null); 137 } 138 139 if ($perm >= AUTH_EDIT) { 140 return true; 141 } else { 142 return false; 143 } 144 145 } 146 147 public static function isAdmin() 148 { 149 global $INFO; 150 if (!empty($INFO)) { 151 return $INFO['isadmin']; 152 } else { 153 return auth_isadmin(self::getUser(), self::getUserGroups()); 154 } 155 } 156 157 public static function isMember($group) 158 { 159 160 return auth_isMember($group, self::getUser(), self::getUserGroups()); 161 162 } 163 164 public static function isManager(): bool 165 { 166 167 return auth_ismanager(); 168 169 } 170 171 public static function getUser(): string 172 { 173 global $INPUT; 174 $user = $INPUT->server->str('REMOTE_USER'); 175 if (empty($user)) { 176 return "Anonymous"; 177 } 178 return $user; 179 } 180 181 private static function getUserGroups() 182 { 183 global $USERINFO; 184 return is_array($USERINFO) && isset($USERINFO['grps']) ? $USERINFO['grps'] : array(); 185 } 186 187 public static function isReader(string $wikiId): bool 188 { 189 $perm = self::getPermissions($wikiId); 190 191 if ($perm >= AUTH_READ) { 192 return true; 193 } else { 194 return false; 195 } 196 197 } 198 199 private static function getPermissions(string $wikiId): int 200 { 201 if ($wikiId == null) { 202 $wikiId = MarkupPath::createFromRequestedPage()->getWikiId(); 203 } 204 if ($_SERVER['REMOTE_USER']) { 205 $perm = auth_quickaclcheck($wikiId); 206 } else { 207 $perm = auth_aclcheck($wikiId, '', null); 208 } 209 return $perm; 210 } 211 212 public static function getSecurityTokenForAdminUser(): string 213 { 214 $request = null; 215 Identity::becomeSuperUser($request, 'admin'); 216 return getSecurityToken(); 217 } 218 219 public static function isAnonymous(): bool 220 { 221 return !self::isLoggedIn(); 222 } 223 224 225} 226