<?php
/***************************************************************************
* Copyright 2003 Ian Meyer, Ian Pitcher
*
* This program 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; either version 2 of the License, or
* (at your option) any later version.
*
***************************************************************************/
function bco_login($_POST)
{
global $cookie_name_user;
/* Log the user out first before setting the cookie */
if ($GLOBALS['logged_in']) {
bco_logout();
}
/* We want to return the userid for storing in the cookie. */
if (!$myuserid = bco_authorize_user($_POST['username'],$_POST['password'])) {
exit("Auth failed.");
}
$year = 31536000;
$month = $year / 12;
$day = $year / 365;
switch ($_POST['time']) {
case "day":
$duration = time()+$day;
break;
case "month":
$duration = time()+$month;
break;
case "year":
$duration = time()+$year;
break;
case "session":
default:
$duration = 0;
break;
}
$tmp_array = array("$myuserid","$_POST[password]","$_POST[username]");
$tmp_data = bco_encode(urlencode(implode(",",$tmp_array)));
$cookie_dir = $GLOBALS['cookie_dir'];
$cookie_url = $GLOBALS['cookie_url'];
setcookie("$cookie_name_user", "$tmp_data", $duration, "$cookie_dir", "$cookie_url"); // this is the login cookie
unset($tmp_data, $tmp_array);
return true;
}
function bco_check_login()
{
global $user_array, $cookie_name_user;
$back = urlencode(basename($_SERVER['REQUEST_URI']));
if (isset($_COOKIE[$cookie_name_user])) {
$cookie_data = urldecode(bco_decode($_COOKIE[$cookie_name_user]));
list($myuserid,$password,$username) = split(",",$cookie_data);
$user_array = array("myuserid" => "$myuserid",
"password" => "$password",
"username" => "$username");
return true;
} else {
return false;
}
}
function bco_logout()
{
global $cookie_name_user;
$cookie_dir = $GLOBALS['cookie_dir'];
$cookie_url = $GLOBALS['cookie_url'];
setcookie("$cookie_name_user", "$_COOKIE[$cookie_name_user]", time() - 3600, "$cookie_dir", "$cookie_url");
// setcookie("$cookie_name_color", "$_COOKIE[$cookie_name_color]", time() - 3600, "$cookie_dir", "$cookie_url");
}
//
// Generate a password.
//
function bco_randompassword ($length)
{
$possible = '123456789' .
'abcdefghijklmnopqrstuvwxyz' .
'ABCDEFGHIJKLMNOPQRSTUVWXYZ' ;
$str = "";
mt_srand((double)microtime() * 1000000);
while (strlen($str) < $length) {
$str .= substr($possible, mt_rand(0, strlen($possible) - 1), 1);
}
return($str);
}
function bco_encode($string)
{
$tmp_string = base64_encode($string);
$tmp_string = $tmp_string . bco_randompassword(6);
return $tmp_string;
}
function bco_decode($string)
{
$tmp_string = substr($string, 0, -6);
$tmp_string = base64_decode($tmp_string);
return $tmp_string;
}
?>