<?php


function AUTHORIZE_USER_LOGIN_BY_AUTH_MODULE($ADM_ACCOUNT,$ADM_PASSWD) {
global $SQL_USER,$SQL_DATABASE,$INTERFACE_ADDR,$SQL_USER_PASS,$AUTH_API;
//echo "<hr>AUTHORIZE_USER_LOGIN User $SQL_USER Adm_Account=$ADM_ACCOUNT mysql_pconnect($INTERFACE_ADDR,$SQL_USER,$SQL_USER_PASS);";
  $NIP = '';
  
	$REQ = $AUTH_API."?user=".$ADM_ACCOUNT."&pass=".urlencode($ADM_PASSWD)."&request=auth";//&request_module=sew";
	DEBUG_S(4, 'AUTH_API', $REQ);
	try {
		$tresc = file_get_contents($REQ);
	} catch (Exception $e) {
		DEBUG_S(3, "AUTH_API error", $e->getMessage());
		echo'AUTH_API error';
		return;
  }
	if ($tresc !== FALSE) {
			$tresc = str_replace(array("\n","\r"),"",$tresc);// remove ne lines for preg_match_all
			if (strpos($tresc,'<error>') !== FALSE) {
				if (preg_match_all("/<error>(.*?)<\/error>/", $tresc, $matches, PREG_SET_ORDER)) {
					$ERROR = $matches[0][0];
				}
		} else if (strpos($tresc,'<auth>') !== FALSE && strpos($tresc,'<nip>') !== FALSE) {
				if (preg_match_all("/<auth>(.*?)<\/auth>/", $tresc, $matches, PREG_SET_ORDER)) {
					$AUTH = $matches[0][0];
				$tags="nip";
					if (preg_match_all("/<nip>(.*?)<\/nip>/", $AUTH, $matches, PREG_SET_ORDER)) {
					$match = $matches[0][0];
					$match=str_replace("<".$tags.">", "", $match);	$match=str_replace("</".$tags.">", "", $match);
					$_SESSION['AUTHORIZE_USER_LOGIN_BY_AUTH_MODULE']['P_NIP']=$match;
					}
				$tags="status";
				if (preg_match_all("/<status>(.*?)<\/status>/", $tresc, $matches, PREG_SET_ORDER)) {
					$match=$matches[0][0];
					$match=str_replace("<".$tags.">", "", $match);	$match=str_replace("</".$tags.">", "", $match);
					$_SESSION['AUTHORIZE_USER_LOGIN_BY_AUTH_MODULE']['status'] = $match;
				}
				$tags="DUTIES_GID";
				if (preg_match_all("/<DUTIES_GID>(.*?)<\/DUTIES_GID>/", $tresc, $matches, PREG_SET_ORDER)) {
					$match=$matches[0][0];
					$match=str_replace("<".$tags.">", "", $match);	$match=str_replace("</".$tags.">", "", $match);
					$_SESSION['AUTHORIZE_USER_LOGIN_BY_AUTH_MODULE']['DUTIES_GID'] = $match;
					$_SESSION['DUTIES_GID']=$match;
			}
			}
		}
		else {
			$ERROR = "Bﾳﾱd konwersji";
		}
	} else {
		DEBUG_S(3, "LOGOWANIE", $AUTH_API);
		echo "<br><h2>Wystﾱpiﾳy bﾳ￪dy podczas poﾳﾱczenia do zewn￪trznego moduﾳu</h2>";
  }
	//if(isset($_SESSION['AUTHORIZE_USER_LOGIN_BY_AUTH_MODULE']['status']))
	if ($_SESSION['AUTHORIZE_USER_LOGIN_BY_AUTH_MODULE']['status'] == 'OK') {
		$_SESSION['ADM_PASSWD']="$ADM_PASSWD";
		$_SESSION['AUTHORIZE_USER']="$ADM_ACCOUNT";//TODO: read from XML
		$_SESSION['ADM_ACCOUNT']="$ADM_ACCOUNT";
		$_SESSION['ADM_AREA']="";
		$_SESSION['ADM_NAME']="";
		$_SESSION['ADM_TECH_WORKER']="";
		$_SESSION['ADM_COMPANY']="";
		$_SESSION['ADM_ADMIN_LEVEL']="";
		$_SESSION['ADM_PHONE']="";
		$_SESSION['ADM_ADMIN_EXPIRE']="";

		echo "<br><h2>Nastﾱpiﾳo poprawne logowanie do zewn￪trznego moduﾳu autoryzacji </h2><br>Informacje: <font color=green>".$AUTH."</font>";
    return;
  }
  else {
		echo "<br><h2>Wystﾱpiﾳy bﾳ￪dy w logowaniu z zewn￪trznego moduﾳu </h2><br><font color=red>".$ERROR."</font>";
}

	SEF("DEBUG_S");
	DEBUG_S(3, "STATUS", $_SESSION['AUTHORIZE_USER_LOGIN_BY_AUTH_MODULE']['status']);
	DEBUG_S(3, "NIP", $NIP);
	DEBUG_S(3, "ADM_NAME", $_SESSION['ADM_NAME']);
	DEBUG_S(3, "DUTIES_GID", $_SESSION['DUTIES_GID']);
}

?>
