<?php
// AuthPassword.php
// Copyright P.Kassel, M.Seliger, G.Trietsch, all Rights Reserved
namespace Docuform\Api\Authentication\login;use Psr\Http\Message\ResponseInterface;use DI\Container;use Nyholm\Psr7\Stream;use Docuform\Api\Authentication\Repositories\UserRepository;use Docuform\Api\Authentication\Repositories\ApiClientRepository;use Docuform\Api\ApiScopes;use Docuform\Api\Exception\ApiHttpAuthException;global $lcfg;$lcfg=array();global $g_lang;$g_lang=getLanguage(['de','en']);global $g_params;$g_params=array();if(file_exists('auth/login-config.php'))include 'auth/login-config.php';require_once 'AuthInterface.php';class AuthPassword implements AuthInterface{private $m_container;public function __construct(Container $container,array $params){global $g_params;$this->m_container=$container;$g_params=$params;if($g_params===null){}}public function Authorize($request,$response):ResponseInterface{$this->initialize();$response=$this->showDialog($response);return $response;}public function ProcessResponse($response,$params):string{$result='';$scopes=explode(' ',$params['scope']);if(in_array(ApiScopes::mcs,$scopes)){throw new ApiHttpAuthException(400,'Scope mcs not allowed','','');}if(!empty($params['username'])&&!empty($params['password'])){$clientrepo=new ApiClientRepository($this->m_container);$userrepo=new UserRepository($this->m_container);$client=$clientrepo->getClientEntity($params['client_id']);if($client!=null)$user=$userrepo->getUserEntityByUserCredentials($params['username'],$params['password'],$params['response_type'],$client);if($user!=null)$result=$user->getIdentifier();}return $result;}private function initialize():void{global $g_res,$g_lang,$lcfg;$g_res=[];$g_res['site_name']=SetString($lcfg['site_name'],'docuFORM');$g_res['site_logo']=SetString($lcfg['logo'],'auth/logo.png');$g_res['site_url']=SetString($lcfg['site_url'],'https://www.docuform.de');$g_res['htmlinfo']=SetString($lcfg['htmlinfo'],'');if($g_lang==='de'){$g_res['page_title']=SetString($lcfg['page_title'],$g_res['site_name'].' Anmeldung');$g_res['title']=SetString($lcfg['title'],'Bei '.$g_res['site_name'].' anmelden');$g_res['user_name']=SetString($lcfg['user_name'],'Benutzername');$g_res['password']=SetString($lcfg['password'],'Kennwort');$g_res['show_password']=SetString($lcfg['show_password'],'Kennwort anzeigen');$g_res['button_text']=SetString($lcfg['button_text'],'Anmelden');}else{$g_res['page_title']=SetString($lcfg['title'],'Sign in to '.$g_res['site_name']);$g_res['title']=SetString($lcfg['title'],'Sign in to '.$g_res['site_name']);$g_res['user_name']=SetString($lcfg['user_name'],'Username');$g_res['password']=SetString($lcfg['password'],'Password');$g_res['show_password']=SetString($lcfg['show_password'],'Show password');$g_res['button_text']=SetString($lcfg['button_text'],'Sign in');}}private function showDialog($response):ResponseInterface{ob_start();require 'password.html.php';$body=ob_get_clean();$bodyStream=Stream::create('');$bodyStream->write($body);$response=$response->withStatus(200)->withHeader('Content-Type','text/html')->withBody($bodyStream);return $response;}}function getLanguage(array $availableLanguages):string{$language='en';$languages=acceptedLanguages();for($i=0;isset($languages[$i]);$i++){$langParts=explode('-',$languages[$i]['language']);$lang=strtolower($langParts[0]);if(in_array($lang,$availableLanguages,true)){$language=$lang;break;}}return $language;}function acceptedLanguages():array{$languages=[];if(!empty($_SERVER['HTTP_ACCEPT_LANGUAGE'])){$httpAcceptLanguage=explode(',',str_replace(' ','',$_SERVER['HTTP_ACCEPT_LANGUAGE']));foreach($httpAcceptLanguage as $i=>$halItem){$hal=explode(';',$halItem);$languages[$i]=[];$languages[$i]['position']=$i;$languages[$i]['language']=$hal[0];$languages[$i]['quality']=$hal[1]?? 'q=1.0';}$quality=[];$position=[];foreach($languages as $key=>$value){$quality[$key]=$value['quality'];$position[$key]=$value['position'];}array_multisort($quality,SORT_DESC,$position,SORT_ASC,$languages);}else{$languages[0]['language']='en';}return $languages;}function SetString(& $text,$default):string{$ret=$default;if(!empty($text))$ret=$text;return $ret;}