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 } 64 65 // $_SERVER[] = $user; 66 // global $USERINFO; 67 // $USERINFO['grps'] = array('admin', 'user'); 68 69 // global $INFO; 70 // $INFO['ismanager'] = true; 71 72 } 73 74 /** 75 * @param $request 76 * @param string $user - the user to login 77 */ 78 public static function logIn(&$request, $user = 'defaultUser') 79 { 80 81 $request->setServer('REMOTE_USER', $user); 82 83 /** 84 * The {@link getSecurityToken()} needs it 85 */ 86 global $INPUT; 87 $INPUT->server->set('REMOTE_USER',$user); 88 89 } 90 91 /** 92 * @return bool if edit auth 93 */ 94 public static function isWriter() 95 { 96 97 return auth_quickaclcheck(PluginUtility::getPageId()) >= AUTH_EDIT; 98 99 } 100 101 public static function isAdmin() 102 { 103 global $INFO; 104 if (!empty($INFO)) { 105 return $INFO['isadmin']; 106 } else { 107 return auth_isadmin(self::getUser(), self::getUserGroups()); 108 } 109 } 110 111 public static function isMember($group) 112 { 113 114 return auth_isMember($group, self::getUser(), self::getUserGroups()); 115 116 } 117 118 public static function isManager() 119 { 120 global $INFO; 121 return $INFO['ismanager']; 122 } 123 124 private static function getUser() 125 { 126 global $INPUT; 127 return $INPUT->server->str('REMOTE_USER'); 128 } 129 130 private static function getUserGroups() 131 { 132 global $USERINFO; 133 return is_array($USERINFO) ? $USERINFO['grps'] : array(); 134 } 135 136 public static function getLogoHtml() 137 { 138 /** 139 * Logo 140 */ 141 $tagAttributes = TagAttributes::createEmpty("register"); 142 $tagAttributes->addComponentAttributeValue(Dimension::WIDTH_KEY, "72"); 143 $tagAttributes->addComponentAttributeValue(Dimension::HEIGHT_KEY, "72"); 144 $tagAttributes->addClassName("logo"); 145 return Site::getLogoImgHtmlTag($tagAttributes); 146 } 147 148 /** 149 * @param Doku_Form $form 150 * @param string $classPrefix 151 * @param bool $includeLogo 152 * @return string 153 */ 154 public static function getHeaderHTML(Doku_Form $form, $classPrefix, $includeLogo = true) 155 { 156 if (isset($form->_content[0]["_legend"])) { 157 158 $title = $form->_content[0]["_legend"]; 159 /** 160 * Logo 161 */ 162 $logoHtmlImgTag = ""; 163 if ( 164 PluginUtility::getConfValue(Identity::CONF_ENABLE_LOGO_ON_IDENTITY_FORMS, 1) 165 && 166 $includeLogo === true 167 ) { 168 $logoHtmlImgTag = Identity::getLogoHtml(); 169 } 170 /** 171 * Don't use `header` in place of 172 * div because this is a HTML5 tag 173 * 174 * On php 5.6, the php test library method {@link \phpQueryObject::htmlOuter()} 175 * add the below meta tag 176 * <meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/> 177 * 178 */ 179 return <<<EOF 180<div class="$classPrefix-header"> 181 $logoHtmlImgTag 182 <h1>$title</h1> 183</div> 184EOF; 185 } 186 return ""; 187 } 188 189 190} 191