<?php
// MEETING OVER - UNCOMMENT REDIRECT
header('Location:http://www.borealis.gi');
exit();

$page_title = 'Online Proxy Logon';

// SET YEAR, PATHS

//$year = date("Y",strtotime("-1 year"));

$path = realpath(dirname(__FILE__));
require($path.'/common/definitions.php');

$year = $proxy_year;


//require('/home/default/borealis.gi/user/htdocs/proxy'.$year.'/common/definitions.php');

$message = '';

session_name("PROXY".$proxy_year);
session_start();

$_SESSION['year'] = $year;
$_SESSION['user'] = '';
$_SESSION['user_id'] = '';
$_SESSION['company'] = '';
$_SESSION['logon'] = '';
$_SESSION['auth_level'] = 0;

//echo "<pre>";
//var_dump($_SESSION);
//echo "</pre>";

if (isset($_POST['submit'])) {

   $u = strtoupper($_POST['username']);
   $p = strtoupper($_POST['password']);
   $u = trim($u);
   $p = trim($p);


// SANITIZE LOGON
   $eviltext = '[#|:~!()@&%$^?<>`*\;\[\]\{\}\/\\\\]';
   if (eregi($eviltext, $u)) {
      $u = ereg_replace($eviltext,'',$u);
   }
   if (strlen($u) > 60) {
      $u = substr($u, 0, 60);
   } 
   if (eregi($eviltext, $p)) {
      $p = ereg_replace($eviltext,'',$p);
   }
   if (strlen($p) > 40) {
      $p = substr($p, 0, 40);
   }

   $u = mysql_real_escape_string ($u);
   $p = mysql_real_escape_string ($p);

// GRAB IP, SET TIME RANGE FOR LOGONS
   if (getenv("HTTP_CLIENT_IP")) {
      $ip = getenv("HTTP_CLIENT_IP");
   } else if(getenv("HTTP_X_FORWARDED_FOR")) {
      $ip = getenv("HTTP_X_FORWARDED_FOR");
   } else if(getenv("REMOTE_ADDR")) {
      $ip = getenv("REMOTE_ADDR");
   } else {
      $ip = "UNKNOWN";
   }
   
   $date = date("Y-m-d");
   require_once ($db_path.'/access.php'); // Connect to the db.


if ($u != 'GUEST') {
   // CHECK THE BANNED IP LIST
   $query = 'SELECT ip FROM events WHERE ip="'.$ip.'" AND timestamp LIKE "'.$date.'%" AND event_type="99"';
   $result = @mysql_query ($query); // Run the query.
   $count = mysql_num_rows($result);
   
   // CHECK FOR ADMIN OVERRIDE
   $query2 = 'SELECT ip FROM events WHERE ip="'.$ip.'" AND timestamp LIKE "'.$date.'%" AND event_type="98"';
   $result2 = @mysql_query ($query2); // Run the query.
   $count2 = mysql_num_rows($result2);
   
   if (($count > 0) && ($count2 == 0)) {
          
      $message = '<font face="Arial" size="4" color="#993333">TOO MANY FAILURES - LOGON DISABLED</font>';
   
      // LOG THE FAILED ATTEMPT
      $querykeys = 'event_type, user, ip, timestamp';
      $queryvalues = '"3", "'.$u.'", "'.$ip.'", NOW()';
      
      $log_query = "INSERT INTO events ($querykeys) VALUES ($queryvalues)"; 
      $log_result = @mysql_query ($log_query); // Run the query.
   
   } else {
      $queryx = "SELECT * FROM users WHERE user = '$u' AND password = MD5('$p') AND user_year = $year";

      $resultx = @mysql_query ($queryx); // Run the query.
      $countx  = mysql_num_rows($resultx);
 
      if ($countx == 0) {
         $message = '<font face="Arial" size="4" color="#993333">Invalid Username or Password</font>';
      
         // LOG THE FAILED ATTEMPT
         $querykeys = 'event_type, user, ip, timestamp';
         $queryvalues = '"2", "'.$u.'", "'.$ip.'", NOW()';
    
         $log_query = "INSERT INTO events ($querykeys) VALUES ($queryvalues)"; 
         $log_result = @mysql_query ($log_query); // Run the query.
      
         // CHECK FOR MULTIPLE FAILED ATTEMPTS
         $check_query = 'SELECT ip FROM events WHERE ip="'.$ip.'" AND '.
                     'timestamp LIKE "'.$date.'%" AND event_type="2"';
         $check_result = @mysql_query ($check_query); // Run the query.
         $check_count  = mysql_num_rows($check_result);
         
         if ($check_count > 10) {
            // BLOCK IP
            $querykeys = 'event_type, user, ip, timestamp';
            $queryvalues = '"99", "'.$u.'", "'.$ip.'", NOW()';
    
            $log_query = "INSERT INTO events ($querykeys) VALUES ($queryvalues)"; 
            $log_result = @mysql_query ($log_query); // Run the query.
       
           //$disable_query = 'UPDATE users SET disabled=1 where user="'.$u.'"';
           //$result = @mysql_query ($disable_query); // Run the query.
         }
      }
    
   
      if ($countx == 1) {
         while ($row = mysql_fetch_array ($resultx, MYSQL_ASSOC)) {
            if ($row['disabled'] == 1) {
               $message = '<font face="Arial" size="4" color="#993333">Account Disabled</font>';
             
               // LOG THE FAILED ATTEMPT
               $querykeys = 'event_type, user, ip, timestamp';
               $queryvalues = '"3", "'.$u.'", "'.$ip.'", NOW()';
   
               $log_query = "INSERT INTO events ($querykeys) VALUES ($queryvalues)"; 
               $log_result = @mysql_query ($log_query); // Run the query.

            } else {
               $_SESSION['logon'] = '1';
               $_SESSION['user_id'] = $row['user_id'];
               $_SESSION['user'] = $row['user'];
               $_SESSION['company'] = $row['company'];
               $_SESSION['auth_level'] = $row['auth_level'];   
               $_SESSION['year'] = $year; 

               // LOG THE EVENT
               $querykeys = 'event_type, user, user_id, ip, timestamp';
               $queryvalues = '"1", "'.$u.'", "'.$row['user_id'].'", "'.$ip.'", NOW()';
   
               $log_query = "INSERT INTO events ($querykeys) VALUES ($queryvalues)"; 
               $log_result = @mysql_query ($log_query); // Run the query.

               mysql_close(); // Close the database connection.      
             
		if ($_SESSION['user'] == 'rchbf' && $_SESSION['auth_level'] > 0) {
			header('Location: rchbf_votes.php');
			exit();
		}
 
               if ($_SESSION['auth_level'] >= '2') {
               //   header ('HTTP/1.0 302 Found');
               //   echo ' ';

                  header('Location: admin.php');
                  exit();
               } else {
               //   header ('HTTP/1.0 302 Found');
               //   echo ' ';
              
                  header('Location: proxy.php');
                  exit();
               }
            }
         }
      }
   } 
  
   mysql_close(); // Close the database connection.      

}
}

$_SESSION['year'] = $year;
$_SESSION['user'] = '';
$_SESSION['user_id'] = '';
$_SESSION['company'] = '';
$_SESSION['logon'] = '';
$_SESSION['auth_level'] = 0;

echo '<html><head>

<meta http-equiv="Content-Language" content="en-us">
<title>'.$page_title.'</title>
</head>
<body bgcolor="#EFEFE3" topmargin=4 onload="document.logon.username.focus()">
<!-- '.$year.' -->    
<form method="POST" action="logon.php" name="logon" ID="logon">
<center>
<img src="images/spacer.gif" border=0 height=100 width=1><BR>
<table border="4" cellspacing="1" cellpadding="0" bordercolor="#000066" width="400" height="200" bgcolor="#FFFFFF">
  <tr><td valign="top">
    <table border=0 cellspacing="4" cellpadding="2" width="100%">
    <tr>
        <td colspan=2 align="center"><img src="images/spacer.gif" height=10><BR>
    <font face="Arial, Helvetica" size=5>Online Proxy Voting</font><BR>
    <img src="images/spacer.gif" height=10></td>
    </tr>
    <tr>
    <td align="left"><b><font face="Arial" color="#000066">Username</font></b></td>
    <td align="right"><input type="text" name="username" size="28" style="width:200px"></td></tr>
    <tr>
    <td align="left"><b><font face="Arial" color="#000066">Password</font></b></td>
    <td align="right"><input type="password" name="password" size="28"  style="width:200px"></td></tr>
    <tr>
        <td colspan="2" align="right" valign="bottom" height="*">
    <input type="submit" name="submit" value="Submit"></td>
    </tr>
';
/*
echo '
    <tr>
    <td colspan=2 align="center" valign="middle">
    <b><font face="Arial" color="#000066"> ONLINE VOTING WILL BE AVAILABLE SHORTLY</font></b>
    </td>
    </tr>
    ';
*/


echo '
    <tr>
    <td valign="bottom" align="center" colspan="2">'.$message.'&nbsp;</td></tr>
    </table><BR>
</td></tr></table>
</form>

<BR>
<span style="font size:14px;font-family:Arial, Helvetica;font-weight:bold;color:#CC0000">
NOTE: Logon information can be found on your Proxy form, <BR>
sent via email from the Borealis Family of Companies<BR><BR>

<!-- 
If you received notification by postal mail and you do not yet have<br>
a username and password, you may 
<a href="request_password.php">request one now</a>.
-->

You may also <a href="download.php">download a copy of the proxy form</a>.

<BR><BR>
If you have not received or have misplaced your proxy notification, <br>please contact 
<a href="mailto:proxy@borealis.gi">proxy@borealis.gi</a> for assistance.

<BR><BR>
After voting, you can attend the annual meeting at:<Br><a href="https://www.borealis.gi/meeting2025/">https://www.borealis.gi/meeting2025/</a>


</center>
</body>
</html>';

exit();
?>
