Location: PHPKode > projects > Obsessive Website Statistics > ows/include/base.inc.php
	$Id: base.inc.php 95 2007-08-17 21:38:13Z randomperson83 $

	Obsessive Web Statistics
    Copyright (C) 2007 Dustin Spicuzza <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 3 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
    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, see <http://www.gnu.org/licenses/>.
	This file includes the base functions needed to make this thing work


global $cfg;

require_once 'util.inc.php';
require_once 'default.inc.php';
require_once 'db.inc.php';

// theme globals
global $output;
$output = array();
$output['head'] = '';
$output['menu'] = '';
$output['title'] = 'Obsessive Web Statistics';

// add an item to the head
function add_to_head($content){
	global $output;
	$output['head'] .= $content;

// add an item to the menu
function add_to_menu($item){
	global $output;
	$output['menu'] .= $item;

// set the title
function set_title($title){
	global $output;
	$output['title'] = $title;

// this forms the final output
function ob_callback($content){

	global $output,$cfg;
	// try and include the theme.. if the user screwed up, not our fault
	$themepath = 'themes/' . $cfg['theme'];
	$themefile = realpath(dirname(__FILE__) . "/../$themepath") . '/index.php';
	if ((@include $themefile) != 1)
		return "<html><body><strong>Error:</strong> Could not load theme &quot;" . $cfg['theme'] . "&quot;!</body></html>";
	return get_theme_html($content, $themepath);

// this checks to see if we're running as CLI and dies if thats not the case
function require_cli(){
	if (php_sapi_name() != "cli")
		die ("<strong>Error</strong>: This MUST be ran from a command line");
		if (!function_exists('readline')){
			function readline($prompt){
				echo $prompt;
				return rtrim(fgets(fopen('php://stdin','r'), 128));

// config values particular to a website that can be changed by the program (as opposed to config files)
function get_config_var($website,$key,$default_value = null){
	$result = db_query("SELECT config_value FROM " . db_escape_string(str_replace('.','_',$website) . '_config') . " WHERE config_key = '" . db_escape_string($key) . "'");
	if (db_is_valid_result($result) && $row = db_fetch_row($result))
		return $row[0];
	return $default_value;

// config values particular to a website that can be changed by the program (as opposed to config files)
function set_config_var($website,$key,$value){

	$table = db_escape_string(str_replace('.','_',$website) . '_config');
	$key = db_escape_string($key);
	$value = db_escape_string($value);

	// see if it exists first
	$result = db_query("SELECT config_value FROM $table WHERE config_key = '$key'");
	if (!db_is_valid_result($result))
		return false;
	if (db_has_rows($result))
		// exists, need to update
		return db_is_valid_result(db_query("UPDATE $table SET config_value = '$value' WHERE config_key = '$key'"));

	// doesn't exist, need to insert
	return db_is_valid_result(db_query("INSERT INTO $table (config_key, config_value) VALUES ('$key','$value')"));

// get array of website names
function get_website_names(){
	global $cfg;
	$sites = array();
	foreach ($cfg['websites'] as $site => $options)
		$sites[] = $site;
	return $sites;

function get_website_options($website){
	global $cfg;
	if (array_key_exists($website,$cfg['websites']))
		return $cfg['websites'][$website];
	return false;

function get_current_website_options(){
	global $cfg;
	$current = get_get_var('domain');
	if (array_key_exists($current,$cfg['websites']))
		return $cfg['websites'][$current];
	return false;

// validate current website
function validate_website($website){
	global $cfg;
	if (!array_key_exists($website,$cfg['websites']))
		return show_error("OWS is not configured for $website, you will need to edit config.inc.php.", true);
	return true;

function validate_website_table($website,$show_table_error = true){

	if (!validate_website($website))
		return false;
	// check to see if table exists
	if (db_has_rows(db_query("SHOW TABLES LIKE '" . db_escape_string(str_replace('.','_',$website)) . "'")))
		return true;
	if ($show_table_error)
		return show_error("OWS is configured for $website, however the database tables have not been created for it yet. You will need to use installer.php.", true);
		return false;

if ($cfg['debug']){

	function the_error_handler( $errno, $errstr, $errfile = "", $errline = -1, $errcontext = null){
		if (error_reporting() == 0)
		$errstr = "<div class=\"internal_error\"><strong>Internal Error:</strong> " . htmlentities("$errstr") . ($errfile != "" ? " at <strong>" . htmlentities($errfile) . "</strong> at line $errline" : "") . '</div>';
		if (php_sapi_name() == "cli")
			echo html_entity_decode(strip_tags($errstr),ENT_QUOTES) . "\n";
			echo $errstr;
	// this displays errors despite output buffering
	function base_error_handler( $errno, $errstr, $errfile = "", $errline = -1 ,$errcontext = null){
		global $cfg;
		$errstr = "<p><strong>Internal Error:</strong> ";
		// respect user settings if debug is not on
		if ($cfg['debug'] || (ini_get('display_errors') && $errno < error_reporting()))
			$errstr .= htmlentities("$errstr") . ($errfile != "" ? " at <strong>" . htmlentities($errfile) . "</strong> at line $errline" : "");
			$errstr .= "An error occurred, set \$cfg['debug'] to true to see the error!";
		if (php_sapi_name() == "cli")
			echo html_entity_decode(strip_tags($errstr),ENT_QUOTES) . "\n";
			echo $errstr . "</p>";
	// this floats all of the errors up to a level where it can be seen, and is guaranteed
	// to not be hidden somewhere. Unfortunately, somehow needs to be synced with ajax results
	add_to_head('<script type="text/javascript">
$(document).ready(function () {
	var errors = new Array();
		errors[errors.length] = $(this).html() + "<br/>";
	if (errors.length > 0){
		var errstr = \'\';
		for (i = 0;i < errors.length;i++)
			errstr += errors[i];

// do initialization stuff here

// connect to the database
db_connect($cfg['db_host'],$cfg['db_user'],$cfg['db_pass'],$cfg['db_db']) || die("Could not connect to DB!\n");

// this is *really* inefficient, so you should disable magic quotes somewhere else. 
// However, in case you forget, this is here. Also, if you're running php from CGI 
// instead of an apache module and you cant change it... :(
// Code is from the PHP manual

if (php_sapi_name() != "cli" && get_magic_quotes_gpc()) {
   function stripslashes_deep($value)
	   $value = is_array($value) ?
				   array_map('stripslashes_deep', $value) :

	   return $value;

   $_POST = array_map('stripslashes_deep', $_POST);
   $_GET = array_map('stripslashes_deep', $_GET);
   $_COOKIE = array_map('stripslashes_deep', $_COOKIE);
   $_REQUEST = array_map('stripslashes_deep', $_REQUEST);

Return current item: Obsessive Website Statistics