<%
H DECEDIT('0,') DATEDIT(*YMD.) COPYRIGHT('System og Metode (c), 2010')
H TEXT('mnu: Login and return session id')
H BNDDIR('MNUUTL')
H BNDDIR('SMDUTL')
 * ------------------------------------------------------------------------------------ *
 * Program ....: login
 * Author .....: Claus Rytter Larsen, System og Metode Aps - December 2010
 * ------------------------------------------------------------------------------------ *
FMNUUSRL4  UF   E           K disk
FMNUUGRL1  IF   E           K disk
FMNUUCLL1  IF   E           K disk
 * ------------------------------------------------------------------------------------ *
/include qsrc,mnuUtlP
/include qsrc,smdUtlP
 * ------------------------------------------------------------------------------------ *
d browserText     s             80
d languageID      s              3
d server          s             10

d loginKey        s            256    varying
d queryString     s            512    varying

d userID          s            128
d password        s            128
d fileName        s             21
d msg             s            512    varying
d pos             s              5  0	
 * ------------------------------------------------------------------------------------ *
 * Main
 * ------------------------------------------------------------------------------------ *
 /free
	SetContentType('text/plain; charset=windows-1252');
	*inlr = *on;

  exsr clrLogonVar;

  userID = lo2up(qryStr('user'));
  password = qryStr('password');
  loginKey = qryStr('loginKey');

  select;
  when loginKey <> '';
    queryString = getServerVar('REQUEST_URL');
    pos = %scan('?':queryString);
    if pos = 0;
      queryString = '';
    else;
      queryString = %subst(queryString:pos);
    endif;
    redirect('/menu/mnu001f.asp' + queryString);
  when userID = '';
    %>{success:false,error:"<%ResponseWriteLanguageMsg('IC00010');%>"}<%
     exsr clrLogonVar;
  when password = '';
      %>{success:false,error:"<%ResponseWriteLanguageMsg('IC00018');%>"}<%
  other;
    msg = Logon(UserId :Password);
    if msg = '';
      %>{success:true,sessionID:"<%=getServerVar('SESSION_URL')%>",msg:"successful login"}<%
      exsr setLogonVar;
    else;
      %>{success:false,error:"<%=%trim(msg)%>"}<%
    endif;
  endsl;

	return;
// -------------------------------------------------------------------------- *
// set Logon variables
// -------------------------------------------------------------------------- *
Begsr setLogonVar;
  sesSetVar('sys.site':GetServerVar('SERVER_ID'));
  if sesGetVar('sys.index') = '';
    sesSetVar('sys.index':'/'
      + %trim(getHive('PORTFOLIO')) + '/PORTFOLIO.asp');
  endif;

  chain USRUGRID MNUUGR0;
  if not %found;
    clear MNUUGR0;
  endif;
  chain USRUCLID MNUUCL0;
  if not %found;
    clear MNUUCL0;
  endif;

  server = getServerVar('SERVER_ID');

  sesSetVar('sys.login': *on);
  sesSetVar('sys.server.id': server);
  sesSetVar('sys.loggedin.site': sesGetVar('sys.site'));
  sesSetVarNum('sys.usr.ID': USRID);
  sesSetVar('sys.usr.login.ID': USRNAM);
  sesSetVar('sys.usr.password': password);
  if USRTXT = '';
    sesSetVar('sys.usr.name': rtvUsrNam(USRNAM));
  else;
    sesSetVar('sys.usr.name': USRTXT);
  endif;
  sesSetVarNum('sys.usr.group': UGRID);
  sesSetVarNum('sys.usr.class': USRUCLID);
	sesSetVar('sys.usr.email': USREML);
	sesSetVarNum('sys.usr.ccsid': USCCSID);
	
	if USRTHM = '';
  	sesSetVar('sys.usr.theme': 'xtheme-gray.css');
	else;
  	sesSetVar('sys.usr.theme': USRTHM);
	endif;
  if USRUCLID = 90;
    sesSetVar('sys.usr.administrator': *on);
  else;
    sesSetVar('sys.usr.administrator': *off);
  endif;
  sesSetVar('sys.usr.classID': UCLCOD);

  browserText = GetHeader('Accept-Language');
  languageID  = %subst(browserText:1:2);
  sesSetVar('sys.usr.language': languageID);

  chain (userID) MNUUSR0;
  if %found;
    USRLOGDAT = %date();
    USRLOGTIM = %time();
    update MNUUSR0;
  endif;	

  close MNUUGRL1;
  close MNUUCLL1;
Endsr;	
// -------------------------------------------------------------------------- *
// clear Logon variables
// -------------------------------------------------------------------------- *
Begsr clrLogonVar;
  sesSetVar('sys.login': *off);
  SesClrVar('sys.server.id');
  SesClrVar('sys.loggedin.site');
  SesClrVar('sys.usr.*');
Endsr;	
 /end-free
%>
