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