<?vsp
--
--  $Id$
--
--  This file is part of the OpenLink Software Virtuoso Open-Source (VOS)
--  project.
--
--  Copyright (C) 1998-2018 OpenLink Software
--
--  This project is free software; you can redistribute it and/or modify it
--  under the terms of the GNU General Public License as published by the
--  Free Software Foundation; only version 2 of the License, dated June 1991.
--
--  This program is distributed in the hope that it will be useful, but
--  WITHOUT ANY WARRANTY; without even the implied warranty of
--  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
--  General Public License for more details.
--
--  You should have received a copy of the GNU General Public License along
--  with this program; if not, write to the Free Software Foundation, Inc.,
--  51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
--

WV.WIKI.VSPHEADER (path,params,lines, 'Login');	 

declare _ods_home varchar;
_ods_home := registry_get ('wa_home_link');
 if (not isstring (_ods_home))
   _ods_home := '/ods';


if ({?'user'} is not null and {?'err'} is null) 
  {
    declare page varchar;
    page := coalesce ({?'page'}, 'Main/' || WV.WIKI.CLUSTERPARAM ('Main', 'index-page', 'WelcomeVisitors'));
    if ({?'submit'} = 'Register')
      {
	http_request_status ('HTTP/1.1 302 Found');
	http_header(sprintf ('Location: %s/register.vspx?reguid=' || {?'user'} || '\r\n', _ods_home));
	return;
      }
    if (WV.WIKI.AUTHENTICATE ({?'user'}, {?'passwd'}) = 1)
      {
	declare  sid varchar;
	sid := md5 (concat (datestring (now ()), http_client_ip (), http_path ()));
	insert into DB.DBA.VSPX_SESSION (VS_REALM, VS_SID, VS_UID, VS_STATE, VS_EXPIRY)
	  values ('wa', sid, {?'user'},
		  serialize ( vector ( 'vspx_user', {?'user'} ) ),
		  now());
	http_request_status ('HTTP/1.1 302 Found');
	if (strchr (page, '?') is null) 
	http_header('Location: ../main/' || page || '?realm=wa&sid=' || sid || '\r\n');
	else
		http_header('Location: ../main/' || page || '&realm=wa&sid=' || sid || '\r\n');
      }
    else 
      {
	http_request_status ('HTTP/1.1 302 Found');
	http_header(sprintf ('Location: login.vsp?user=%V&err=1&page=%V \r\n', {?'user'}, page));
      }
  } 
else 
  {
?>
<img src="images/wikibanner_sml.jpg"></img>
<h2>oWiki Login</h2>
<div class="center">
  <div class="login-form">
    <form name="login" action="login.vsp" method="post">
      <input type="hidden" name="page" value="<?={?'page'}?>"/>
      <input type="hidden" name="command" value="login"/>
      <table>
  	<tr>
	  <td>Account:</td><td><input name="user" type="text" size="14"/></td>
	</tr>
	<tr>
	  <td>Password:</td><td><input name="passwd" type="password" size="14" id="password" value=""/></td>
	</tr>
	<tr>
	  <td/>
	  <td>
	    <input name="submit" type="submit" value="Login"/>
	    <input name="submit" type="submit" value="Register"/>
	  </td>
	</tr>
      </table>
    </form>
<?vsp
    if ({?'err'} is not null)
      {
?>
    <div class="error">
      Invalid user name or password.
    </div>
<?vsp
    http (sprintf('<a href="%s/pass_recovery.vspx?usr=%V">Forgot your password?</a>', _ods_home,
    	{?'user'}));
      }
?>
  </div>
</div>
<?vsp
  }
WV.WIKI.VSPFOOTER(path, params, lines);
?>
