<?/*
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
  function pretty_die($mesg) {
    global $web_root;

    echo('<pre>' . htmlentities($mesg) . '</pre>');
    if ($app = ini_get('auto_append_file')) require $app;
    exit;
  }

  function pretty_print_r($what, $return = false) {
    $str = '<pre>';
    $str .= htmlentities(print_r($what, true));
    $str .= '</pre>';

    if ($return) return $str;
    print $str;
  }

  function warnings_off() {
    $GLOBALS['warnings_old_error_level'] =
      error_reporting(error_reporting() & ~(E_NOTICE | E_WARNING));
  }

  function warnings_on() {
    error_reporting($GLOBALS['warnings_old_error_level']);
  }

  function notices_off() {
    $GLOBALS['notices_old_error_level'] = 
      error_reporting(error_reporting() & ~E_NOTICE);
  }

  function notices_on() {
    error_reporting($GLOBALS['notices_old_error_level']);
  }

  function globalize_request_keys() {
    $keys = func_get_args();

    foreach ($keys as $key) {
      $GLOBALS[$key] = (array_key_exists($key, $_REQUEST))
		     ? $_REQUEST[$key]
		     : NULL;
    }
  }

  function print_error($str) {
    echo_error($str);
  }

  function echo_error($str) {
    echo("<font color=\"red\"><pre>\n".htmlentities($str)."\n</pre></font>");
  }

  function redirect ($url='', $relative=0, $noheader=0, $do_sess=1) {

    if ($relative) $url = gen_relative_url()."/$url";

    if ($do_sess and session_id()) {
      if (preg_match ('/\?/', $url)) {
        $type = '&';
      } else {
        $type = '?';
      }

      $url .= $type.session_name().'='.session_id();
    }

    $redirect = gen_server_name().$url;

    if ($noheader) return $redirect;

    header ("Location: $redirect");
    exit;
  }

  function gen_relative_url () {
    $a = explode ('/', $_SERVER['PHP_SELF']);
    $url = '';
    for ($i=1; $i< count($a)-1; $i++) {
      $url .= '/'.$a[$i];
    }

    return $url;
  }

  function gen_server_url () {
    return gen_server_name().gen_relative_url();
  }

  function gen_server_name () {
    $server_name = $_SERVER['SERVER_NAME'];
    $server_port = $_SERVER['SERVER_PORT'];
    $url = (array_key_exists('HTTPS', $_SERVER)) ? 'https' : 'http';

    $url .= "://$server_name";

    if (!in_array ($server_port, array(80, 443)))
      $url .= ":$server_port";

     return $url;
  }

  function path_under_in($under, $in) {
    return preg_match('/^' . preg_quote($under, '/') . '/', $in);
  }
?>
