<?

include('user_vars.inc');

// validate the user
function validate_user($db, $user_login, $user_password) {
  global $user_table, $user_login_field, $user_password_field;
  $sql_statement = "select $user_login_field, $user_password_field from $user_table ".
    "where $user_login_field = '$user_login' limit 1";

  // and $user_password_field = BINARY '$user_password'

  $result = mysql_query($sql_statement, $db);

  $row = mysql_fetch_array($result);
  $num_rows = 0;
  if ($user_password == $row[$user_password_field]):
    $num_rows = 1;
  endif;

  //$num_rows = mysql_num_rows($result);
  mysql_free_result($result);
  return $num_rows;
}

function get_password($db, $user_login) {
  global $user_table, $user_login_field, $user_password_field;
  $sql_statement = "select $user_password_field from $user_table ".
    "where $user_login_field = '$user_login' limit 1";

  $result = mysql_query($sql_statement, $db);

  $row = mysql_fetch_array($result);
  return $row[$user_password_field];

  mysql_free_result($result); 
}

// decrypt the string
function DecryptString($encryptedString, $key){

  $t = 0;
  $decryptedString = '';

  $ENC_LENGTH = 1000;

  $encryptionLength = strlen($encryptedString);
  if ($encryptionLength > $ENC_LENGTH) {
    $encryptionLength = $ENC_LENGTH;
  }

  for ($i=0; $i < $encryptionLength ; $i++) {
    $ckey = ord($key[$t]);
    $cdata = ord($encryptedString[$i]);
    $decoded = $cdata ^ $ckey;
    $byte = chr($decoded);
    $decryptedString = $decryptedString.$byte;
    $t=$t+1;
    if ($t==strlen($key)):
      $t=0;
    endif;
  }
  
  if (strlen($encryptedString) > $ENC_LENGTH) {
    $decryptedString = $decryptedString.substr($encryptedString,$ENC_LENGTH);
  }
  return $decryptedString;
}

?>