<?php
/*
Bad Behavior for TightURL - detects and blocks unwanted Web accesses
Ron Guerin <ron@vnetworx.net>

I wrote almost none of this, but if you're using this file, you should contact
me (Ron Guerin) and not Michael Hampton, the author of Bad Behavior if you
have questions about this file or the installation of Bad Behavior that you
got with TightURL.

Based on bad-behavior-generic.php 
Copyright (C) 2005-2006 Michael Hampton  badbots AT ioerror DOT us

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., 675 Mass Ave, Cambridge, MA 02139, USA.

*/

###############################################################################
###############################################################################

define('BB2_CWD'dirname(__FILE__));

// Settings you can adjust for Bad Behavior.
// Change these by changing the corresponding TightURL settings in
// tighturl.config.inc.php instead of editing this file.

$bb2_settings_defaults = array( 'log_table'     => $dbtable '_bb2',
                                
'display_stats' => $BBstats,
                                
'strict'        => $BBstrict,
                                
'verbose'       => $BBverbose,
                                
'logging'       => $BBLogging
);

// Bad Behavior callback functions.

// Return current time in the format preferred by your database.
function bb2_db_date() {
    return 
gmdate('Y-m-d H:i:s');    // Example is MySQL format
}

// Return affected rows from most recent query.
function bb2_db_affected_rows() {
    return 
mysql_affected_rows();
}

// Escape a string for database usage
function bb2_db_escape($string) {
    return 
mysql_real_escape_string($string);
}

// Return the number of rows in a particular query.
function bb2_db_num_rows($result) {
    if (
$result !== FALSE) return mysql_num_rows($result);
    return 
0;
}

// Run a query and return the results, if any.
// Should return FALSE if an error occurred.
// Bad Behavior will use the return value here in other callbacks.
function bb2_db_query($query) {
    return 
mysql_query($query);
}

// Return all rows of a particular query in an array.

function bb2_db_rows($result) {
    
$rows = array();
    while (
$row mysql_fetch_assoc($result)) {
        
$rows[] = $row;
    }
    return 
$rows;
}

// Return emergency contact email address.
function bb2_email() {
    return 
$SiteAdminEmail;
}

// read settings from TightURL
function bb2_read_settings() {
    global 
$bb2_settings_defaults;
        
$settings $bb2_settings_defaults;
    return 
$settings;
}

// write settings to TightURL
function bb2_write_settings($settings) {
    return 
false;
}

// installation
function bb2_install() {
  return 
bb2_db_query("CREATE TABLE IF NOT EXISTS `".$settings['log_table']."` (
        `id` INT(11) NOT NULL auto_increment,
        `ip` TEXT NOT NULL,
        `date` DATETIME NOT NULL default '0000-00-00 00:00:00',
        `request_method` TEXT NOT NULL,
        `request_uri` TEXT NOT NULL,
        `server_protocol` TEXT NOT NULL,
        `http_headers` TEXT NOT NULL,
        `user_agent` TEXT NOT NULL,
        `request_entity` TEXT NOT NULL,
        `key` TEXT NOT NULL,
        INDEX (`ip`(15)),
        INDEX (`user_agent`(10)),
        PRIMARY KEY (`id`))"
);
}

// Screener
// Insert this into the <head> section of your HTML through a template call
// or whatever is appropriate. This is optional we'll fall back to cookies
// if you don't use it.
function bb2_insert_head() {
    global 
$bb2_javascript;
    return 
$bb2_javascript;
}

// Display stats
function bb2_insert_stats($force false) {
    
$settings bb2_read_settings();

    if (
$force || $settings['display_stats']) {
        
$blocked bb2_db_query("SELECT COUNT(*) FROM " $settings['log_table'] . " WHERE `key` NOT LIKE '00000000'");
        if (
$blocked !== FALSE) {
                    if (! isset(
$blocked[0]["COUNT(*)"])) {
                
$count 0;
            }
            else {
                
$count $blocked[0]["COUNT(*)"];
            }
            
$ret '<br /><a href="http://www.bad-behavior.ioerror.us/">Bad Behavior</a> has blocked ' $count ' access attempt(s) in the last 7 days.';
        }
        else {
            
$ret "";
        }
    }
    return(
$ret);
}

// Return the top-level relative path of wherever we are (for cookies)
// You should provide in $url the top-level URL for your site.
function bb2_relative_path() {
        global 
$self;
    return 
$self;
}

// Calls inward to Bad Behavior itself.
require_once(BB2_CWD "/bad-behavior/version.inc.php");
require_once(
BB2_CWD "/bad-behavior/core.inc.php");

bb2_start(bb2_read_settings());

?>