<?/*
Copyright (C) 2003 Media Net Link, Inc. Danville, CA 94526 USA

This file is part of AthenaRMS.

AthenaRMS 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.

AthenaRMS 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
*/?>
<?php
  $time1 = time ();

  require_once 'db.php';
  require_once 'person.php';
  require_once 'server_functions.php';
  require_once 'web_root.php';
  
  $person = new Person();
  
  ## db session handler, must have session.* params set in php.ini or .htaccess
  require_once 'adodb/adodb-session-clob.php';

  session_set_cookie_params (0, '/', $_SERVER['SERVER_NAME']);

  session_start ();

  # need auth flag or user and pass
  if (!array_key_exists('AUTHENTICATED', $_SESSION) and
      !array_key_exists('PHP_AUTH_USER', $_SERVER) and
      !array_key_exists('PHP_AUTH_PW', $_SERVER)) do_prompt();


  global $REMOTE_USER;

  if (array_key_exists('AUTHENTICATED', $_SESSION)) {

    $REMOTE_USER = $_SESSION['REMOTE_USER'];
  } else {
    $pida = $person->To_Person_ID ($_SERVER['PHP_AUTH_USER']);

    if (!$person->Valid_Person_Password($pida,$_SERVER['PHP_AUTH_PW'])) do_prompt();

    $REMOTE_USER = $pida;
    $_SESSION['REMOTE_USER'] = $pida;
    $_SESSION['IS_SUPER_ADMIN']  = $person->Is_Super_Admin($REMOTE_USER);
    $_SESSION['AUTHENTICATED'] = 1;
  }

  $crm_id = '';

  if (!$_SESSION['AUTHENTICATED']) {
    require_once 'crm.php';
    $crm = new CRM;

    $crm_id = $crm->CRM_ID_By_Host_Path();
    if (!set_globals($crm_id)) do_prompt('invalid crm_id');

  } elseif ($_SESSION['AUTHENTICATED'] &&
     (!array_key_exists('RUSER_REC', $_SESSION) 
	|| $_SESSION['RUSER_REC']['WEB_DOMAIN'] != $_SERVER['SERVER_NAME'] ||
	!path_under_in($_SESSION['RUSER_REC']['WEB_PATH'], $_SERVER['PHP_SELF']))) {
    require_once 'crm.php';
    $crm = new CRM;

    $crm_id = $crm->CRM_ID_By_Host_Path();

    ## if we don't have a crm_id don't fail because we might be on the 
    ## home page in which case we aren't really in a crm

    if ($crm_id and !set_globals($crm_id)) do_prompt("invalid crm_id on domain name match ($crm_id $_SERVER[SERVER_NAME])");
  }

  function set_globals ($crm_id) {
    require_once 'crm_view.php';

    $cv = new CRM_View;
 
    if (!$crm_id or !$cv->Valid_CRM_ID($crm_id, $_SESSION['REMOTE_USER'])) return 0;
 
    require_once 'person.php';

    $person = new Person;

    $person_info = $person->Info_by_Person_CRM_ID($_SESSION['REMOTE_USER'], $crm_id);

    $RUSER_REC = array();
    foreach ($person_info as $k => $v) {
      $RUSER_REC[strtoupper($k)] = $v;
    }

    $_SESSION['RUSER_REC'] = $RUSER_REC;

    if (is_null($user_types 
	= $person->Get_Types( $_SESSION['REMOTE_USER'], $crm_id )))
      die($person->error);

    $user_type_hash = array();
    $_SESSION['USER_TYPES'] = array();
    foreach ($user_types as $type) {
      $_SESSION['USER_TYPES'][$type] = 1;
    }

    return 1;
  }

  global $RUSER_REC;
  $RUSER_REC = $_SESSION['RUSER_REC'];
  global $REMOTE_USER_CRM_ID;
  $REMOTE_USER_CRM_ID = $RUSER_REC['CRM_ID'];
  $_SESSION['REMOTE_USER_CRM_ID'] = $RUSER_REC['CRM_ID'];
  global $REMOTE_USER_PCRM_MAP_ID;
  $REMOTE_USER_PCRM_MAP_ID = $RUSER_REC['PCRM_MAP_ID'];
  global $REMOTE_USER_COMPANY;
  $REMOTE_USER_COMPANY = $RUSER_REC['COMPANY_ID'];
  
  ## do this on every request to show "my open" link in navigation,
  ## I would rather not do this...
  require_once 'ticket.php';
  $ticket = new Ticket;
  $_SESSION['MY_OPEN_COUNT'] = $ticket->Open_by_Person_RMS($_SESSION['REMOTE_USER'],$REMOTE_USER_CRM_ID);

  if (array_key_exists('REQUIRE_USER_TYPE', $_SERVER) 
      && $reqd_user_types = $_SERVER['REQUIRE_USER_TYPE']) {
    if ($person->Is_Super_Admin($REMOTE_USER)) {
    } elseif (!$_SESSION['REMOTE_USER_CRM_ID']) {
      die ('unable to determine CRM_ID');
      do_prompt("unable to determine CRM_ID, please contact your AthenaRMS admin.\n");
    } else {
      if (!$person->Valid_User_Type($REMOTE_USER,$_SESSION['REMOTE_USER_CRM_ID'],explode('|', $reqd_user_types)))
        do_prompt("Your account doesn't have access to this area.\n");
    }
  } else {
  }

  if (array_key_exists('goto', $_REQUEST)) {
    require_once 'server_functions.php';
    redirect ($_REQUEST['goto'], 0, 0);
  }

  function do_prompt ($msg='') {
    if (!$msg) $msg = "Please enter your username/password to login.\n";

    Header('WWW-Authenticate: Basic realm="Athena"');
    Header('HTTP/1.0 401 Unauthorized');
    print $msg;
    exit;
  }
?>
