<%pre>

#include <setup.h>
#include <users.h>

using namespace vdrlive;

</%pre>
<%args>
std::string login;
std::string password;
std::string action;
std::string redirect;
</%args>
<%session scope="global">
bool logged_in(false);
</%session>
<{

std::string message;
std::string targetUrl = qparam.has("redirect") && !redirect.empty() ? redirect : LiveSetup().GetStartScreenLink();

if (LiveSetup().CheckLocalNet(request.getPeerIp())) {
  logged_in = true;
  return reply.redirect(targetUrl);
}
if (action == "login") {
  if (Users.ValidLogin(login, password)) {
    logged_in = true;
    cUsers::logged_in_user = login;
  } else {
    message = tr("Wrong username or password");
  }
} else if (action == "logout") {
  logged_in = false;
  cUsers::logged_in_user = "";
}
if (logged_in || !LiveSetup().UseAuth()) return reply.redirect(targetUrl);

}>
<& pageelems.doc_type &>
<html>
  <head>
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>VDR-Live - <$ tr("Login") $></title>
    <& pageelems.stylesheets &>
<#  the Ajax components cause loading the login page twice, so better skip them
    <& pageelems.ajax_js &>
#>
  </head>
  <body onload="document.auth.login.focus()">
  <div class="loginwin">
    <div class="boxheader"><div class="caption"><$ tr("VDR Live Login") $></div></div>
    <div class="login_content">
      <img src="<$ LiveSetup().GetThemedLink("img", "logo_login.png") $>" alt="VDR Live" />
      <form name="auth">
        <input type="hidden" name="action" value="login" />
        <input type="hidden" name="redirect" value="<$ targetUrl $>" />
        <table class="login">
          <tr>
            <td><$ tr("User") $>:</td>
            <td><input type="text" name="login" value="<$ login $>" size="15" maxlength="15" /></td>
            <td>&nbsp;</td>
          </tr>
          <tr>
            <td><$ tr("Password") $>:</td>
            <td><input type="password" name="password" value="<$ password $>" size="15" maxlength="15" /></td>
            <td><button class="login" type="submit"><$ tr("Login") $></button></td>
          </tr>
        </table>
      </form>
      <div class="message">
        <$ message $>
      </div>
    </div>
  </div>
  </body>
</html>
