<?php
require('includes/global.php');
$tpl->assign('area', 'Log In');

if(isset($_POST['login'])) {
   $error = array();
   if(empty($_POST['username'])) $error['username'] = ERROR;
   if(empty($_POST['password'])) $error['password'] = ERROR;

   if(empty($error)) {
      $validated = false;
      $admin     = false;

      for($i = 1; defined('user'.$i.'name'); $i++) {
         if(constant('user'.$i.'name') != $_POST['username']) continue;
         if($_POST['username'] == '2nd User') break; // little lock if some guys forgets to delete the sample user from the cfg

         if(constant('user'.$i.'pass') == $_POST['password']) {
            $validated = true;
            if(defined('user'.$i.'admin') && constant('user'.$i.'admin') > 0) {
               $admin = true;
            }
         }
         break;
      }

      if($validated) {
         setcookie('ettvd_user', $_POST['username']);
         setcookie('ettvd_pass', md5($_POST['password']));

         if($admin) {
            $ip = $_SERVER['REMOTE_ADDR'];
         } else {
            $ip = $_SERVER['REMOTE_ADDR'].'-X';
         }

         $ettvd->init();
         $ettvd->send('adminip '.$ip.' '.$_POST['username']);


         send('index.php');
      } else {
         error('<pre>\|/ ____ \|/<br />~@./ o0 \.@~<br />/ ( \__/ ) \<br />   \__U_/   </pre>');
      }
   } else {
      $tpl->assign('error', $error);
   }
}

$tpl->assign('body', $tpl->fetch('login.tpl'));
require('finish.php');
?>