<?php
///////////////////////////////////////////////////////////////////////////////
// //
// Helpzilla: Got Help? (tm) //
// //
// Copyright (c) 2003 Jason Dillon. All rights reserved. //
// //
// This file is part of Helpzilla. //
// //
// Helpzilla 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. //
// //
// Helpzilla 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 //
// //
///////////////////////////////////////////////////////////////////////////////
//
// Version: $Id: helpzilla-util.inc.php,v 1.13 2003/04/06 08:38:46 user57 Exp $
//
// Author: Jason Dillon <hide@address.com>
//
require_once('common.inc.php');
define('UNDEFINED', '<_UNDEFINED_>');
class Object
{
var $log;
function Object()
{
// setup logging
$this->log =& LoggerManager::getLogger($this->getLoggerName());
// call user constructor
$args = func_get_args();
call_user_func_array(array(&$this, '__constructor'), &$args);
}
function __constructor()
{
// nothing
}
function __sleep()
{
// Do not serialize our logger
$fields = get_object_vars($this);
unset($fields['log']);
return array_keys($fields);
}
function __wakeup()
{
// re-attach logging
$this->log =& LoggerManager::getLogger($this->getLoggerName());
}
function getLoggerName()
{
return get_class($this);
}
function getLoggerPathName()
{
$name = null;
$classname = get_class($this);
$ancestors = get_ancestors_class($classname);
if ($ancestors != null) {
$c = count($ancestors);
if ($c >= 1) {
$name = $ancestors[$c-1];
}
}
if ($name != null) {
$name .= '.' . $classname;
}
else {
$name = $classname;
}
return $name;
}
}
define('HTTP_MOVED_PERMANENTLY', '301 Moved Permanently');
define('HTTP_FOUND', '302 Found');
define('HTTP_SERVER_ERROR', '500 Internal Server Error');
class HTTP
{
function MakeURL($uri)
{
$url = null;
if (!preg_match( "/^http.*/", $uri)) {
$url = (isset($_SERVER['HTTPS']) ? 'https://' : 'http://') . $_SERVER['SERVER_NAME'];
if (substr($uri, 0, 1) != '/') {
$tmp = split('/', $_SERVER["SCRIPT_NAME"]);
array_pop($tmp);
$current_dir = implode('/', $tmp);
$uri = $current_dir . '/' . $uri;
}
$url .= $uri;
}
return $url == null ? $uri : $url;
}
function Redirect($uri, $status=HTTP_FOUND)
{
header($_SERVER['SERVER_PROTOCOL'] . " " . $status, true);
// TODO: Should set Status: ?
$url = HTTP::MakeURL($uri);
header("Location: $url", true);
exit();
}
function RedirectWithQuery($uri, $sep='?', $status=HTTP_FOUND)
{
$url = HTTP::MakeURL($uri);
if (isset($_SERVER['QUERY_STRING']) && $_SERVER['QUERY_STRING'] != "") {
$url .= $sep . $_SERVER['QUERY_STRING'];
}
HTTP::Redirect($url, $status);
}
}
class HTTPObject
extends Object
{
function register() // ...
{
$args = func_get_args();
foreach ($args as $name) {
$GLOBALS[$name] = $this->get($name);
}
}
function contains() // ...
{
$args = func_get_args();
foreach ($args as $name) {
if ($this->get($name, null) == null) {
return false;
}
}
return true;
}
}
class HTTPSession
extends HTTPObject
{
function __constructor()
{
if (!isset($_SESSION)) {
error('Session not started');
}
}
function &get($name)
{
if (isset($_SESSION[$name])) {
return $_SESSION[$name];
}
$args = func_get_args();
array_shift($args);
if (count($args) == 0) {
error('Missing required session parameter: ' . $name);
}
return $args[0];
}
function put($name, &$obj)
{
$prev = null;
if (isset($_SESSION[$name])) {
$prev =& $_SESSION[$name];
}
$_SESSION[$name] =& $obj;
return $prev;
}
function &Create($start=true) // Static
{
if (isset($GLOBALS['hz_http_session'])) {
return $GLOBALS['hz_http_session'];
}
if ($start && !isset($_SESSION)) {
session_start();
}
$session =& new HTTPSession();
$GLOBALS['hz_http_session'] =& $session;
return $session;
}
}
class HTTPRequest
extends HTTPObject
{
var $method;
function __constructor()
{
global $REQUEST_METHOD;
//
// TODO: use this to determine which array to use
//
$this->method = $REQUEST_METHOD;
}
function &get($name)
{
if (isset($_POST[$name])) {
return $_POST[$name];
}
elseif (isset($_GET[$name])) {
return $_GET[$name];
}
$args = func_get_args();
array_shift($args);
if (count($args) == 0) {
error('Missing required request parameter: ' . $name);
}
return $args[0];
}
function &Create() // Static
{
if (isset($GLOBALS['hz_http_request'])) {
return $GLOBALS['hz_http_request'];
}
$req =& new HTTPRequest();
$GLOBALS['hz_http_request'] =& $req;
return $req;
}
}
class PageBuffer
{
function IsEnabled()
{
global $hz_config;
return $hz_config['pagebuffer']['enabled'];
}
function Begin()
{
if (PageBuffer::IsEnabled()) {
// Turn on the output buffer
ob_start(); // ob_start("ob_gzhandler");
}
}
function End()
{
if (PageBuffer::IsEnabled()) {
// Set the content length and end the output buffer
header('Content-Length: ' . ob_get_length(), true);
ob_end_flush();
}
}
}
function include_script($url, $lang='javascript', $type='text/javascript')
{
$url = HTTP::MakeURL($url);
?><script language="<?= $lang ?>" type="<?= $type ?>" src="<?= $url ?>"></script>
<?
}
function svar_dump($data)
{
ob_start();
var_dump($data);
$ret_val = ob_get_contents();
ob_end_clean();
return $ret_val;
}
function get_ancestors_class($classname)
{
$ancestors = null;
$parent = get_parent_class($classname);
if ($parent != "") {
$ancestors = get_ancestors_class($parent);
$ancestors[] = $parent;
}
return $ancestors;
}
?>