Location: PHPKode > projects > Bradabra > bradabra/include/login_check.php
<?php
//****************************************************************************
//
// Copyright (C) 2001 Eric SEIGNE <hide@address.com>
//
// 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.
//
// 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
//
//****************************************************************************
//
// For more informations, get to the project's main source file.
//
//****************************************************************************

if(!defined("__login_check_php__")):
define("__login_check_php__", "1");


//----------------------------------------------------------------------------
// CheckAccess_InternalModule
//----------------------------------------------------------------------------
function CheckAccess_InternalModule ()
{
  if(!defined("__this_is_an_internal_module__"))
    return; // ok

  if($GLOBALS["user_access_intmods"] == INTMODS_FULLACCESS)
    return; // ok

  $intmod_array = GetInternalModuleArray();
  if(!$intmod_array)
    return; // ok

  if(!($GLOBALS["user_access_intmods"] & $intmod_array["id"]))
  {
    // oops
    @include $include_path."lang.php";
    die("\n<br><b>ERROR</b> : You are not allowed to use this module !<br>\n<a href=\"".RedirRobot(FALSE, "main_home", NULL)."\">Home</a>.");
  }
}

//----------------------------------------------------------------------------
// CheckSessionValidity - Check session ID validity.
//----------------------------------------------------------------------------
function CheckSessionValidity ()
{
  if(!$GLOBALS["sid"])
  {
    if(isset($GLOBALS[COOKIENAME_SID]))
    {
      $GLOBALS["sid"] = $GLOBALS[COOKIENAME_SID];
      $cookie_login = $GLOBALS[COOKIENAME_LOGIN];
    }
    else
      return NULL;
  }

  $rows = DB_Select(DBTN_USERSESSIONS, "*", "WHERE id='".$GLOBALS["sid"]."'");
  if((!$rows) || $GLOBALS["db_noentry"] || (!$rows[0]["login"]))
  {
    setcookie(COOKIENAME_LOGIN, "");
    setcookie(COOKIENAME_SID, "");
    $GLOBALS["sid"] = NULL;
    return NULL;
  }

  // check validity of cookie because we loaded info because of an existing cookie
  if($cookie_login)
  {
    if($rows[0]["login"] != $cookie_login)
    {
      setcookie(COOKIENAME_LOGIN, "");
      setcookie(COOKIENAME_SID, "");
      $GLOBALS["sid"] = NULL;
      return NULL;
    }

    $GLOBALS["cookie_is_ok"] = "ok";
  }

  return $rows[0]["login"];
}

//----------------------------------------------------------------------------
// LoadConfigInfo - Load configuration informations into config_* global
//                  variables.
//----------------------------------------------------------------------------
function LoadConfigInfo ()
{
  $rows_config = DB_Select(DBTN_CONFIG, "*", "");
  if(!$rows_config)
    die("LoadConfigInfo : ERROR while getting configuration informations from database.");

  // currencies
  $arr = split("[;]", trim($rows_config[0]["currencies"]));
  $n = count($arr);
  for($i=0, $j=0 ; $i<$n ; $i+=2, $j++)
  {
    $GLOBALS["config_currencies"][$j] = array(
      "label" => trim($arr[$i]),
      "rate"  => trim($arr[$i+1]));
  }

  // date format
  $GLOBALS["config_dateformat"] = $rows_config[0]["dateformat"];

  // vat
  $GLOBALS["config_vat"] = $rows_config[0]["vat"];

  // external modules
  $arr = split("[".REGEXP_NEWLINE."]", trim($rows_config[0]["external_modules"]));
  $n = count($arr);
  for($i=0, $j=0 ; $i<$n ; $i+=4, $j++)
  {
    $GLOBALS["config_external_modules"][$j] = array(
      "name" => trim($arr[$i]),
      "path" => trim($arr[$i+2]));
  }

  // alert thresholds mailing list
  $arr = split("[".REGEXP_NEWLINE."]", trim($rows_config[0]["mails_alertthresholds"]));
  $n = count($arr);
  for($i=0, $j=0 ; $i<$n ; $i+=2, $j++)
    $GLOBALS["config_mails_alertthresholds"][$j] = trim($arr[$i]);
}

//----------------------------------------------------------------------------
// LoadEnterpriseInfo - Load configuration informations into enterprise_*
//                      global variables.
//----------------------------------------------------------------------------
function LoadEnterpriseInfo ()
{
  $rows = DB_Select(DBTN_ENTERPRISE, "*", "");
  if(!$rows && !$GLOBALS["db_noentry"])
    die("LoadEnterpriseInfo : ERROR while getting enterprise informations from database.");

  $cols_names = DB_GetColNames(DBTN_ENTERPRISE);
  $cols_types = DB_GetColTypes(DBTN_ENTERPRISE);
  $cols_n = DB_GetColNbr(DBTN_ENTERPRISE);

  for($i=0 ; $i<$cols_n ; $i++)
  {
    if($cols_names[$i] == "emails")
    {
      $arr = split("[".REGEXP_NEWLINE."]", trim($rows[0][$i]));
      $n = count($arr);

      for($j=0, $k=0 ; $j<$n ; $j+=4, $k++)
        $GLOBALS["enterprise_".$cols_names[$i]][$k] = array(
          "caption" => trim($arr[$j]),
          "email"   => trim($arr[$j+2]));
    }
    else if(!($cols_types[$i] & DBT_SERIAL))
    {
      $GLOBALS["enterprise_".$cols_names[$i]] = $rows[0][$i];
    }
  }
}

//----------------------------------------------------------------------------
// LoadSystemInfo
//----------------------------------------------------------------------------
function LoadSystemInfo ()
{
  $GLOBALS["mq_flag"] = get_magic_quotes_gpc();
}

//----------------------------------------------------------------------------
// LoadUserConfigInfo
//----------------------------------------------------------------------------
function LoadUserSystemInfo ()
{
  // Browser name
  $http_user_agent = getenv("HTTP_USER_AGENT");
  $var_name = "userconfig_browsername";
  if(ereg("MSIE [0-9]", $http_user_agent))
    $GLOBALS[$var_name] = "MSIE";
  else if(ereg("Mozilla/[234]", $http_user_agent))
    $GLOBALS[$var_name] = "Netscape";
  else if(ereg("Mozilla/[5678]", $http_user_agent))
    $GLOBALS[$var_name] = "Mozilla";
  else
    $GLOBALS[$var_name] = "Unknown";
}

//----------------------------------------------------------------------------
// LoadUserInfo - Load current user informations into user_* global variables.
//----------------------------------------------------------------------------
function LoadUserInfo ($login)
{
  // get user informations
  $rows_user = DB_Select(DBTN_USER, "*", "WHERE login='".$login."'");
  if(!$rows_user)
    die("LoadUserInfo : ERROR while getting user informations from database.");

  $GLOBALS["user_id"]             = $rows_user[0]["id"];
  $GLOBALS["user_login"]          = $login;
  $GLOBALS["user_pass"]           = $rows_user[0]["pass"];
  $GLOBALS["user_lastname"]       = $rows_user[0]["lastname"];
  $GLOBALS["user_firstname"]      = $rows_user[0]["firstname"];
  $GLOBALS["user_email"]          = $rows_user[0]["email"];
  $GLOBALS["user_category"]       = $rows_user[0]["category"];
  $GLOBALS["user_access_intmods"] = $rows_user[0]["access_intmods"];

  if(strlen($GLOBALS["user_lastname"]) && strlen($GLOBALS["user_firstname"]))
    $GLOBALS["user_fullname"] = $GLOBALS["user_lastname"]." ".$GLOBALS["user_firstname"];
  else
    $GLOBALS["user_fullname"] = $login;
}

//----------------------------------------------------------------------------
// MAIN
//----------------------------------------------------------------------------
$login = CheckSessionValidity();
if(!$login)
{
  if(!strstr($GLOBALS["PHP_SELF"], "login.php")) // avoid infinite redirecting loop
  {
    RedirRobot(TRUE, "main", NULL);
    exit; // ensure we're going away...
  }
}
else
{
  LoadSystemInfo();
  LoadUserInfo($login);
  LoadConfigInfo();
  LoadEnterpriseInfo();

  LoadUserSystemInfo();
  CheckAccess_InternalModule();
}


endif;
?>
Return current item: Bradabra