<!--
 -
 -  $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
 -
-->
<v:variable name="requested_url" type="any" default="null" persist="0" param-name="URL" xmlns:v="http://www.openlinksw.com/vspx/"/>
<script type="text/javascript">
  <![CDATA[
  function adm_post (f, btn)
  {
    var s = f.password_plain.value;
    f.__submit_func.value = '__submit__';
    f.__submit_func.name = btn.name;
    f.password.value = hex_md5 (f.nonce.value + f.password_plain.value);
    f.password_plain.value = "";
    f.submit ();
    f.password_plain.value = s;
  }
  ]]></script>
<v:login name="wa_login"
         realm="wa"
         mode="url"
         user-password-check="web_user_password_check"
         xmlns:v="http://www.openlinksw.com/vspx/">
  <v:template name="ifnl" type="if-no-login" redirect="index.vspx"/>
  <v:login-form name="admin_login_form"
                required="1"
                title="Login"
                user-title="name"
                password-title="pass"
                submit-title="Login">
    <table class="login_fm_ctr">
      <tr>
  <td>
        <img class="title_icon"
             src="images/icons/lock_16.png"
             alt="Not logged in"
       title="Not logged in"/>
     <v:template type="simple" name="login_not_valid" enabled="1">
       <v:after-data-bind><![CDATA[
         if (
              e.ve_is_post and
              e.ve_button is not null and
        e.ve_button.vc_name = 'login'
      )
      control.vc_enabled := 1;
         else
                  control.vc_enabled := 0;
      ]]></v:after-data-bind>
       <div class="error_msg">Invalid credentials. <?vsp
            if(self.login_blocked is not null) {
              http(self.login_blocked);
            }
            ?>
</div>
     </v:template>
   </td>
       </tr>
       <tr>
   <td>
     <label for="t_login_usr">Account</label>
   </td>
       </tr>
       <tr>
   <td>
            <v:text name="username" value="" xhtml_id="t_login_usr"/>
  </td>
      </tr>
      <tr>
  <td>
    <label for="t_login_pwd">Password</label>
  </td>
      </tr>
      <tr>
  <td>
            <v:text name="password_plain"
                    value=""
                    type="password"
        xhtml_id="t_login_pwd"/>
            <v:text name="password" value=""
        type="hidden" />
  </td>
      </tr>
      <tr>
  <td>
          <v:button action="simple"
                    name="login"
                    value="Login"
        xhtml_id="login_btn"
        xhtml_onclick="javascript: adm_post (this.form, this); return false">
        <v:on-post>
                      <v:script>
                        <![CDATA[
                          declare _blocked_until any;
                          _blocked_until := (select
                                               WAB_DISABLE_UNTIL
                                             from
                                               WA_BLOCKED_IP
                                             where
                                               WAB_IP = http_client_ip());
                          if(_blocked_until is not null and _blocked_until > now()) {
                            self.login_blocked := 'Your login attempts are blocked.';
                            return;
                          }
                          self.login_attempts := coalesce(self.login_attempts, 0) + 1;
                          -- during login processing the post are called twice
                          -- so 6 instead 3
                          if(self.login_attempts > 6)
                          {
                            insert replacing
                              WA_BLOCKED_IP(WAB_IP, WAB_DISABLE_UNTIL)
                            values
                              (http_client_ip(), dateadd('hour', 1, now()));
                          }
                        ]]>
                      </v:script>
                    </v:on-post>
        <v:before-render>
                      <![CDATA[
                        if(self.login_blocked is not null) {
                          control.vc_enabled := 0;
                        }
                      ]]>
                    </v:before-render>
          </v:button>
        </td>
      </tr>
    </table>
  </v:login-form>
  <v:template name='il' type='if-login'>
    <?vsp
      self.login_attempts := 0;
     ?>
    <table class="login_fm_ctr">
      <tr>
  <td>
    <img src="images/icons/user_16.png"
      alt="User logged in"
      title="User logged in"/>
  </td>
      </tr>
      <tr>
  <td>
    Logged in as&nbsp;
          <?vsp
            http (connection_get ('vspx_user'));
          ?>
      </td>
    </tr>
    <tr>
      <td>
          <v:button action="logout"
                    name="admin_logout_button"
                    value="Logout"
                    style="button"
        xhtml_id="logout_btn"/>
    </td>
        </tr>
      </table>
  </v:template>
  <v:after-data-bind>
    <![CDATA[
    if (control.vl_authenticated)
        {
           set isolation = 'committed';
           select U_ID, U_NAME, U_FULL_NAME, U_E_MAIL
           into self.u_id, self.u_name, self.u_full_name, self.u_e_mail
              from SYS_USERS
              where U_NAME = connection_get ('vspx_user') with (prefetch 1);
        }

        -- Redirect if the user needs to update his/her data
        --declare _path varchar;
        --declare _updated integer;
        --if (not(isnull(self.u_name)))
        --{
        -- _updated := WA_USER_GET_OPTION(self.u_name,'WA_INTERNAL_REGISTRATION_UPDATED');
        --  if (isnull(_updated) or _updated < 1)
        --  {
        --    _path := regexp_match('[^/]*$',http_path());
        --    if (not(isnull(_path)) and _path <> 'settings.vspx' and _path <> 'index.vspx')
        --      self.vc_redirect(sprintf('settings.vspx?update=1&URL=%s',http_path()));
        --  };
        --};

      if (
     e.ve_is_post and
     e.ve_button is not null and
     e.ve_button.vc_name = 'admin_logout_button'
   )
        {
    self.username.ufl_value := '';
  }
    ]]>
  </v:after-data-bind>
</v:login>
