<?php
/*
Plugin Name: Rudd-O's Wordspew
Plugin URI: http://rudd-o.com/projects/wordspew/
Description: A live shoutbox with online user tracking and new messages fade-in for WordPress
Author: Rudd-O
Version: 2.0
Author URI: http://rudd-o.com/
*/

// Version of this plugin. Not very useful for you, but for the dev
$jal_version = "2.0";

// The required user level needed to access the admin page for this plugin
$jal_admin_user_level = 8;

// The number of comments that should show up in one viewing.
$jal_number_of_comments = 35;

/*
==================// Important Info //=======================================

This file is called in three different places. First, it is called by Wordpress
so that it can display the jal_get_shoutbox function.

Secondly, it is called by javascript. Because it is called by javascript and not by wordpress,
normal plugin procedure won't work without directly calling the wp-config file in your root
directory. We cannot use the $wpdb object. So instead, we open up the config file, comment out
the bit about calling the rest of Wordpress, and then evaluate the config file to extract how to 
access the database. We do all this so that the entirety of Wordpress is not loaded by each call
to the server, which would put great unneccessary load on the server and conflict with other plugins.

To differentiate between the two remote calls, I've used the $jalGetChat and $jalSendChat variables.
These two variables are sent by the javascript file as "yes". That way, the script will only perform
certain actions if it is called by the javascript file.

Thirdly, it is called by the wordpress admin panel, which allows users to edit settings for this plugin

==================// End of Info //==========================================
*/


/* useronline code */

if (!isset($table_prefix)) {
	$html = implode('', file("../../../wp-config.php"));
	$html = str_replace ("require_once", "// ", $html);
	$html = str_replace ("<?php", "", $html);
	eval($html);
}
$jal_table_prefix = $table_prefix;
$tableuseronline = $jal_table_prefix . 'liveshoutbox_useronline';

### Get IP
function jal_get_IP() {
	if (empty($_SERVER["HTTP_X_FORWARDED_FOR"])) {
		$ip_address = $_SERVER["REMOTE_ADDR"];
	} else {
		$ip_address = $_SERVER["HTTP_X_FORWARDED_FOR"];
	}
	if(strpos($ip_address, ',') !== false) {
		$ip_address = explode(',', $ip_address);
		$ip_address = $ip_address[0];
	}
	return $ip_address;
}

function jal_get_useronline_engine($usertimeout = 60) {

	global $tableuseronline;

	$conn = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
	mysql_select_db(DB_NAME, $conn);

	// Search Bots
	$bots = array('Google Bot' => 'googlebot', 'MSN' => 'msnbot', 'Alex' => 'ia_archiver', 'Lycos' => 'lycos', 'Ask Jeeves' => 'askjeeves', 'Altavista' => 'scooter', 'AllTheWeb' => 'fast-webcrawler', 'Inktomi' => 'slurp@inktomi', 'Turnitin.com' => 'turnitinbot');

	// Useronline Settings
	$timeoutseconds = $usertimeout;
	$timestamp = time();
	$timeout = $timestamp-$timeoutseconds;
	
	global $jal_table_prefix;
	$sql = "SELECT option_value from {$jal_table_prefix}options where option_name = 'siteurl'";
	$result = mysql_query($sql,$conn);
	while($element = mysql_fetch_array($result)) $siteurl = $element["option_value"];

	if (!defined("COOKIEHASH")) {
		$cookiehash = md5($siteurl); // Remove in 1.4
        	define('COOKIEHASH', $cookiehash);
	}

	
	// Check Members
	if(isset($_COOKIE['comment_author_'.COOKIEHASH]))  {
		$memberonline = trim($_COOKIE['comment_author_'.COOKIEHASH]);
		$where = "WHERE username='$memberonline'";
	// Check Guests
	} else { 
		$memberonline = 'Guest';
		$where = "WHERE ip='".jal_get_IP()."'";
	}
	// Check For Bot
	foreach ($bots as $name => $lookfor) { 
		if (stristr($_SERVER['HTTP_USER_AGENT'], $lookfor) !== false) { 
			$memberonline = mysql_real_escape_string($name);
			$where = "WHERE ip='".jal_get_IP()."'";
		} 
	} 
	// Update User First
// 	$make_page = wp_title('&raquo;', false);
// 	if(empty($make_page)) { 
// 		$make_page = get_bloginfo('name');
// 	} else {
// 		$make_page = get_bloginfo('name').' &raquo; Blog Archive'.$make_page;
// 	}
	$make_page = "Live chat";

	mysql_query("LOCK TABLES $tableuseronline WRITE", $conn);	

	mysql_query("UPDATE $tableuseronline SET timestamp = '$timestamp', ip = '".jal_get_IP()."', location = '".mysql_real_escape_string($make_page)."', url = '".$_SERVER['REQUEST_URI']."' $where", $conn);

	// If No User Insert It
	if (mysql_affected_rows($conn) == 0) {
		mysql_query("INSERT INTO $tableuseronline VALUES ('$timestamp', '$memberonline', '".jal_get_IP()."', '".mysql_real_escape_string($make_page)."', '".$_SERVER['REQUEST_URI']."')",$conn);
	}

	mysql_query("DELETE FROM $tableuseronline WHERE timestamp < $timeout",$conn);

	mysql_query("UNLOCK TABLES", $conn);

	$result = mysql_query("SELECT username FROM $tableuseronline",$conn);
	
	$useronline = array();
	while($element = mysql_fetch_array($result)) $useronline[] = $element["username"];

	$detected_bots = array();
	$registered_users = array();
	$guests = 0;
	foreach ($useronline as $element) {
		if (array_key_exists($element,$bots)) $detected_bots[] = $element;
		elseif ($element == "Guest") $guests = $guests + 1;
		else $registered_users[] = $element;
	}


/* retornamos el número de guests en línea, un array con los bots indexando y un array con los non-guest usernames */

// 	mysql_close($conn);
	return array("num_guests"=>$guests,"bots"=>$detected_bots,"users"=>$registered_users);

}

function jal_implode_human($glue,$lastglue,$array) {
	if (count($array) == 0) return ""; // only one element
	if (count($array) == 1) return implode("",$array); // only one element
	if (count($array) == 2) return implode($lastglue,$array); // only one element

	$last_element = array_pop($array);

	$finalstring = implode($glue,$array);
	$finalstring .= $lastglue . $array;
	
	return $firststring;
}

function jal_get_useronline_extended($usertimeout = 60) {
	
	$array = jal_get_useronline_engine($usertimeout);
	$u = $array["users"];
	$g = $array["num_guests"];
	$b = $array["bots"];

	/* desired verbiage: */
	/* "Rudd-O, nita- and 2 guests are online.  Googlebot, Inktomi are crawling this site." */
	/* "Rudd-O, nita- and 1 guest are online.  Googlebot, Inktomi are crawling this site." */

	/* thus we get an array with nicknames and a string describing the number of guests */
	$tobeimploded = $u;
	if($g == 0) { /* do not do anything */ }
	elseif ($g > 1) { $tobeimploded[] = $g ." guests"; }
	else { $tobeimploded[] = $g ." guest"; }

	if ($g + count($u) + count($b) == 0) { return "No one is online."; } // no one's here

	$users_online = jal_implode_human(", "," and ",$tobeimploded);
	if (($g + count($u)) > 1) { $users_online .= " are online."; }
	else { $users_online .= " is online."; }

	if (count($b)) {
		$bots_online = jal_implode_human(", "," and ",$b);
		if (count($b) > 1) { $bots_online .= " are crawling this site."; }
		else { $bots_online .= " is crawling this site."; }
	} else {
		$bots_online = "";
	}
	
	return $users_online . "  " . $bots_online;

}


// Register globals - Thanks Karan et Etienne
$jal_lastID    = isset($_GET['jal_lastID']) ? $_GET['jal_lastID'] : "";
$jal_user_name = isset($_POST['n']) ? $_POST['n'] : ""; 
$jal_user_url  = isset($_POST['u']) ? $_POST['u'] : "";
$jal_user_text = isset($_POST['c']) ? $_POST['c'] : "";
$jalGetChat    = isset($_GET['jalGetChat']) ? $_GET['jalGetChat'] : "";
$jalSendChat   = isset($_GET['jalSendChat']) ? $_GET['jalSendChat'] : "";

function jal_install_shout () {
	global $jal_table_prefix, $wpdb, $user_level, $jal_admin_user_level;
	
    get_currentuserinfo();
    if ($user_level < $jal_admin_user_level) return; 
	
  	$result = mysql_list_tables(DB_NAME);
  	$tables = array();

  	while ($row = mysql_fetch_row($result)) { $tables[] = $row[0]; }
  
    if (!in_array($jal_table_prefix."liveshoutbox", $tables)) {
    	$first_install = "yes";
    }

	require_once(ABSPATH . 'wp-admin/upgrade-functions.php');
	
	dbDelta("CREATE TABLE ".$jal_table_prefix."liveshoutbox (
		     id mediumint(7) NOT NULL AUTO_INCREMENT,
		     time bigint(11) DEFAULT '0' NOT NULL,
		     name tinytext NOT NULL,
		     text text NOT NULL,
		     url text NOT NULL,
		     UNIQUE KEY id (id)
		    );");

	dbDelta("CREATE TABLE ".$jal_table_prefix."liveshoutbox_useronline (
  		    timestamp int(15) NOT NULL default '0',
		    username varchar(50) NOT NULL default '',
		    ip varchar(40) NOT NULL default '',
		    location varchar(255) NOT NULL default '',
		    url varchar(255) NOT NULL default '',
		    PRIMARY KEY  (timestamp),
		    KEY username (username),
		    KEY ip (ip),
		    KEY file (location)
		    );");

		
	if ($first_install == "yes") {
		
		$welcome_name = "Rudd-O's Wordspew";
		$welcome_text = "Congratulations, you just completed the installation of this shoutbox.";
	
		$wpdb->query("INSERT INTO ".$jal_table_prefix."liveshoutbox (time,name,text) VALUES ('".time()."','".mysql_real_escape_string($welcome_name)."','".mysql_real_escape_string($welcome_text)."')");
		
		// Default shoutbox color config
		add_option('shoutbox_fade_from', "666666");
		add_option('shoutbox_fade_to', "FFFFFF");
		add_option('shoutbox_update_seconds', 4000);
		add_option('shoutbox_fade_length', 1500);
		add_option('shoutbox_text_color', "333333");
		add_option('shoutbox_name_color', "0066CC");
		add_option('shoutbox_regisitered_only', '0');
	}
}

if (isset($_GET['activate']) && $_GET['activate'] == 'true') {
	add_action('init', 'jal_install_shout');
}


// function to print the external javascript and css links
function jal_add_to_head () {
    global $jal_version;
  
      //$jal_wp_url = (dirname($_SERVER['PHP_SELF']) == "/") ? "/" : dirname($_SERVER['PHP_SELF']) . "/";
      $jal_wp_url = get_bloginfo('wpurl') . "/";
    
    echo '
    <!-- Added By Wordspew Plugin. Version '.$jal_version.' -->
    <link rel="stylesheet" href="'.$jal_wp_url.'wp-content/plugins/wordspew/css.php" type="text/css" />
    <script type="text/javascript" src="'.$jal_wp_url.'wp-content/plugins/wordspew/fatAjax.php"></script>
		';
}

// In the administration page, add some style...
function jal_add_to_admin_head () {
?>
<style type="text/css">

/* Styles added by Wordspew shoutbox plugin */

input[name=jal_delete]:hover, #jal_truncate_all:hover {
background: #c22;
color: #fff;
cursor: pointer;
}

input[name=jal_edit]:hover {
background: #2c2;
color: #fff;
cursor: pointer;
}

#shoutbox_options p {
text-indent: 15px;
padding: 5px 0;
color: #555;
}

#shoutbox_options span {
border: 1px dotted #ccc;
padding: 4px 14px;
}
</style>

<?php
}
	
// HTML printed to the admin panel
function jal_shoutbox_admin () { 
	global $jal_admin_user_level, $wpdb, $user_level, $jal_table_prefix, $jal_number_of_comments;
	get_currentuserinfo(); // Gets logged in user.
	
	// If user is not allowed to use the admin page
	if ($user_level < $jal_admin_user_level) { 
		echo '<div class="wrap"><h2>No Access for you!</h2></div>';
	} else {
 ?>
 		<?php if (isset($_GET['jal_delete'])) { ?>
            <div class="updated">
                <p>The comment was deleted successfully.</p>
            </div>
		<?php } if (isset($_GET['jal_edit'])) { ?>
            <div class="updated">
                <p>The comment was edited successfully.</p>
            </div>
		<?php } if (isset($_GET['jal_truncate'])) { ?>
            <div class="updated">
                <p>The shoutbox database has been wiped. You now have a fresh slate!</p>
            </div>
		<?php } ?>
		
	<div class="wrap">
		<h2>Rudd-O's Wordspew</h2>

		<p>When you update the Times and Colors, you may need to refresh/empty cache before you see the changes take effect</p>
		<p>There have been <b><?php
		 $results = $wpdb->get_var("SELECT id FROM ".$jal_table_prefix."liveshoutbox ORDER BY id DESC LIMIT 1");
		 echo $results; ?></b> messages in this shoutbox
		<form name="shoutbox_options" action="" method="get" id="shoutbox_options"> 
  			<fieldset> 
  				<legend><b>Colors (Must be 6 digit hex)</b></legend>
  				<input type="hidden" name="page" value="wordspew" />
  				Fade from: #<input type="text" maxlength="6" name="fade_from" value="<?php echo get_option('shoutbox_fade_from'); ?>" size="6" /> <span style="background: #<?php echo get_option('shoutbox_fade_from'); ?>;">&nbsp;</span>
  				<p>The color that new messages fade in from. Default: <span style="color: #666">666666</span></p>
  				Fade to: #<input type="text" maxlength="6" name="fade_to" value="<?php echo get_option('shoutbox_fade_to'); ?>" size="6" /> <span style="background: #<?php echo get_option('shoutbox_fade_to'); ?>;">&nbsp;</span>
				<p>Also used as the background color of the box. Default: FFFFFF (white)</p>
  				Text Color: #<input type="text" maxlength="6" name="text_color" value="<?php echo get_option('shoutbox_text_color'); ?>" size="6" /> <span style="background: #<?php echo get_option('shoutbox_text_color'); ?>;">&nbsp;</span>
				<p>The color of text within the box. Default: <span style="color: #333">333333</span></p>
				Name Color: #<input type="text" maxlength="6" name="name_color" value="<?php echo get_option('shoutbox_name_color'); ?>" size="6" /> <span style="background: #<?php echo get_option('shoutbox_name_color'); ?>;">&nbsp;</span>
				<p>The color of peoples' names. Default: <span style="color: #06c">0066CC</span></p>

  			</fieldset>
  			<br />
  			<fieldset> 
  				<legend><b>Other</b></legend>
  				Update Every: <input type="text" maxlength="3" name="update_seconds" value="<?php echo get_option('shoutbox_update_seconds') / 1000; ?>" size="2" /> Seconds <br />
  					<p>This determines how "live" the shoutbox is. With a bigger number, it will take more time for messages to show up, but also decrease the server load. You may use decimals. This number is used as the base for the first 8 javascript loads. After that, the number gets successively bigger. Adding a new comment or mousing over the shoutbox will reset the interval to the number suplied above. Default: 4 Seconds</p>
  				Fade Length: <input type="text" maxlength="3" name="fade_length" value="<?php echo get_option('shoutbox_fade_length') / 1000; ?>" size="2" /> Seconds <br />
					<p>The amount of time it takes for the fader to completely blend with the background color. You may use decimals. Default 1.5 seconds</p>
            Use textarea: <input type="checkbox" name="use_textarea" <?php if(get_option('shoutbox_use_textarea') == 'true') { echo 'checked="checked" '; } ?>/>
               <p>A textarea is a bigger type of input box. Users will have more room to type their comments, but it will take up more space.</p>
            Use URL field: <input type="checkbox" name="use_url" <?php if(get_option('shoutbox_use_url') == 'true') echo 'checked="checked" '; ?>/>
               <p>Check this if you want users to have an option to add their URL when submitting a message.</p>
				Only allow registered users: <input type="checkbox" name="registered_only" <?php if(get_option('shoutbox_registered_only') == '1') echo 'checked="checked" '; ?>/>
  					<p>This will only let your registered users use the form that allows one to type messages. Users who are NOT logged in will be able to watch the chat and a message saying they must be logged in to comment. <b>Note:</b> this is not completely "secure" .. If someone REALLY wanted to, they could write a script that interacts directly with the message receiving file. They'd have to know what they're doing and it would be quite pointless.</p>
</fieldset><br />
  						 
  			<input type="submit" name="jal_admin_options" value="Save" class="button" style="font-size: 140%"  /><br /><br />
  			<input type="submit" name="jal_truncate" id="jal_truncate_all" onclick="return confirm('You are about to delete ALL messages in the shoutbox. It will completely erase all messages. Are you sure you want to do this?');" value="Delete ALL messages" /><br /><br />
		</form>
  			
  			<fieldset> 
  				<legend><b>Data</b> (showing the last <b><?php echo $jal_number_of_comments; ?></b> messages)</legend>
  				<p>Reminder: You MUST have at LEAST one comment in your shoutbox at all times. This is not live. New comments made while viewing this page will not magically appear like they do in the real thing.</p>
				
				<?php
 						$results = $wpdb->get_results("SELECT * FROM ".$jal_table_prefix."liveshoutbox ORDER BY id DESC LIMIT ". $jal_number_of_comments);
 						if (!$results) { echo "<b>You must have at least 1 message in your shoutbox at all times!<br /> Go to your shoutbox and add a messages.</b>"; } else {
 						$jal_first_time = "yes"; // Will only add the last message div if it is looping for the first time
						foreach( $results as $r ) { // Loops the messages into a list
							
							$url = (empty($r->url) && $r->url = "http://") ? $r->name : '<a href="'.$r->url.'">'.$r->name.'</a>';

							if ($jal_first_time == "yes") {	echo '<div id="lastMessage"><span>'.__("Latest Message").'</span> <em id="responseTime">'.jal_time_since( $r->time ).' ago</em></div>'.'<div id="usersOnline">'.htmlspecialchars(jal_get_useronline_extended()).'</div>'.'
 						<ul id="outputList">
 						'; }
						        echo '<li><form name="shoutbox_options" action="" method="get"><span>'.stripslashes($url).' : </span><input type="text" name="jal_text" value="'.stripslashes($r->text).'" size="60" />
						        <input type="hidden" name="page" value="wordspew" />
						        <input type="hidden" name="jal_comment_id" value="'.$r->id.'" />
						        <input type="submit" name="jal_delete" value="Delete" />
						        <input type="submit" name="jal_edit" value="Edit" /></form></li>

						        '; 
					$jal_first_time = "0";	} }
				?>
				</ul>


  			</fieldset>
	</div>

<?php } }

// To add administration page under Management Section
function shoutbox_admin_page() {
	global $jal_admin_user_level;
	add_management_page('Shoutbox Management', 'Live Shoutbox', $jal_admin_user_level, "wordspew", 'jal_shoutbox_admin');
}

// Time Since function courtesy 
// http://blog.natbat.co.uk/archive/2003/Jun/14/jal_time_since

// Works out the time since the entry post, takes a an argument in unix time (seconds)
function jal_time_since($original) {
    // array of time period chunks
    $chunks = array(
        array(60 * 60 * 24 * 365 , 'year'),
        array(60 * 60 * 24 * 30 , 'month'),
        array(60 * 60 * 24 * 7, 'week'),
        array(60 * 60 * 24 , 'day'),
        array(60 * 60 , 'hour'),
        array(60 , 'minute'),
    );
    $original = $original - 10; // Shaves a second, eliminates a bug where $time and $original match.
    $today = time(); /* Current unix time  */
    $since = $today - $original;
    
    // $j saves performing the count function each time around the loop
    for ($i = 0, $j = count($chunks); $i < $j; $i++) {
        
        $seconds = $chunks[$i][0];
        $name = $chunks[$i][1];
        
        // finding the biggest chunk (if the chunk fits, break)
        if (($count = floor($since / $seconds)) != 0) {
            break;
        }
    }
    
    $print = ($count == 1) ? '1 '.$name : "$count {$name}s";
    
    if ($i + 1 < $j) {
        // now getting the second item
        $seconds2 = $chunks[$i + 1][0];
        $name2 = $chunks[$i + 1][1];
        
        // add second item if it's greater than 0
        if (($count2 = floor(($since - ($seconds * $count)) / $seconds2)) != 0) {
            $print .= ($count2 == 1) ? ', 1 '.$name2 : ", $count2 {$name2}s";
        }
    }
    return $print;
}

////////////////////////////////////////////////////////////
// Functions Below are for getting comments from the database
////////////////////////////////////////////////////////////

// Never cache this page
if ($jalGetChat == "yes" || $jalSendChat == "yes") {
	header( "Expires: Mon, 26 Jul 1997 05:00:00 GMT" ); 
	header( "Last-Modified: ".gmdate( "D, d M Y H:i:s" )."GMT" ); 
	header( "Cache-Control: no-cache, must-revalidate" ); 
	header( "Pragma: no-cache" );
	header("Content-Type: text/html; charset=utf-8");
	
	//if the request does not provide the id of the last know message the id is set to 0
	if (!$jal_lastID) $jal_lastID = 0;
}

// retrieves all messages with an id greater than $jal_lastID
if ($jalGetChat == "yes") {
	jal_getData($jal_lastID);
}

// Where the shoutbox receives information
function jal_getData ($jal_lastID) {

// 	$html = implode('', file("../../../wp-config.php"));
// 	$html = str_replace ("require_once", "// ", $html);
// 	$html = str_replace ("<?php", "", $html);
// 	eval($html);
	global $jal_table_prefix;
	$conn = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
	mysql_select_db(DB_NAME, $conn);

	/* to prevent SQL injection attacks */
	if (intval($jal_lastID) . "" != $jal_lastID)
		trigger_error("Malicious ID supplied to Wordspew's jal_getData(): '".$jal_lastID."'",E_USER_ERROR);
	$jal_lastID = intval($jal_lastID);

	$sql = "SELECT * FROM ".$jal_table_prefix."liveshoutbox WHERE id > ".$jal_lastID." ORDER BY id DESC";
	$results = mysql_query($sql, $conn);
	if (!$results)
		trigger_error("MySQL error in Wordspew-triggered SQL: \"" . $sql . "\": " . mysql_error(),E_USER_ERROR);
	
	$useronline = jal_get_useronline_extended();
	echo $useronline . "\n";

	$loop = "";
	while ($row = mysql_fetch_array($results)) {

		$id   = $row[0];
		$time = $row[1];
		$name = $row[2];
		$text = $row[3];
		$url  = $row[4];
		
		// append the new id's to the beginning of $loop
		$loop = $id."---".stripslashes($name)."---".stripslashes($text)."---".jal_time_since($time)." ago---".stripslashes($url)."---" . $loop; // --- is being used to separate the fields in the output
	}
	echo $loop;
	
	// if there's no new data, send one byte. Fixes a bug where safari gives up w/ no data
	if (empty($loop)) { echo "0"; }
}

function jal_special_chars ($s) {
  $s = htmlspecialchars($s, ENT_COMPAT,'UTF-8');
  return str_replace("---","&minus;-&minus;",$s);
}

////////////////////////////////////////////////////////////
// Functions Below are for submitting comments to the database
////////////////////////////////////////////////////////////

// When user submits and javascript fails
if (isset($_POST['shout_no_js'])) {
	if ($_POST['shoutboxname'] != '' && $_POST['chatbarText'] != '') {
		jal_addData($_POST['shoutboxname'], $_POST['chatbarText'], $_POST['shoutboxurl']);
		
		jal_deleteOld(); //some database maintenance
    	
    	setcookie("jalUserName",$_POST['shoutboxname'],time()+60*60*24*30*3,'/');
    	setcookie("jalUrl",$_POST['shoutboxurl'],time()+60*60*24*30*3,'/');
        //take them right back where they left off
		header('location: '.$_SERVER['HTTP_REFERER']);
	} else echo "You must have a name and a comment";
}

	//only if a name and a message have been provides the information is added to the db
if ($jal_user_name != '' && $jal_user_text != '' && $jalSendChat == "yes") {
		jal_addData($jal_user_name,$jal_user_text,$jal_user_url); //adds new data to the database
		jal_deleteOld(); //some database maintenance
		echo "0";
}

function jal_addData($jal_user_name,$jal_user_text,$jal_user_url) {
	//the message is cut of after 500 letters
	$jal_user_text = substr($jal_user_text,0,500); 
	
	$jal_user_name = substr(trim($jal_user_name), 0,18);

///// The code below can mess up multibyte strings

// If there isn't a url, truncate the words to 25 chars each
//	if (!preg_match("`(http|ftp)+(s)?:(//)((\w|\.|\-|_)+)(/)?(\S+)?`i", $jal_user_text, $matches))
//		$jal_user_text = preg_replace("/([^\s]{25})/","$1 ",$jal_user_text);


	// CENSORS .. default is off. To turn it on, uncomment the line below. Add new lines with new censors as needed.	
	//$jal_user_text = str_replace("fuck", "****", $jal_user_text);

	$jal_user_text = jal_special_chars(trim($jal_user_text));
	$jal_user_name = (empty($jal_user_name)) ? "Anonymous" : jal_special_chars($jal_user_name);
	$jal_user_url = ($jal_user_url == "http://") ? "" : jal_special_chars($jal_user_url);

// 	$html = implode('', file("../../../wp-config.php"));
// 	$html = str_replace ("require_once", "// ", $html);
// 	$html = str_replace ("<?php", "", $html);
// 	eval($html);
	global $jal_table_prefix;
	$conn = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
	mysql_select_db(DB_NAME, $conn);
	
	mysql_query("INSERT INTO ".$jal_table_prefix."liveshoutbox (time,name,text,url) VALUES ('".time()."','".mysql_real_escape_string($jal_user_name)."','".mysql_real_escape_string($jal_user_text)."','".mysql_real_escape_string($jal_user_url)."')", $conn);
}

//Maintains the database by deleting past comments
function jal_deleteOld() {
	global $jal_number_of_comments;

// 	$html = implode('', file("../../../wp-config.php"));
// 	$html = str_replace ("require_once", "// ", $html);
// 	$html = str_replace ("<?php", "", $html);
// 	eval($html);
	global $jal_table_prefix;
	$conn = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
	mysql_select_db(DB_NAME, $conn);

	$results = mysql_query("SELECT * FROM ".$jal_table_prefix."liveshoutbox ORDER BY id DESC LIMIT ".$jal_number_of_comments, $conn);	

	while ($row = mysql_fetch_array($results)) { $id = $row[0]; }
	
	if ($id) mysql_query("DELETE FROM ".$jal_table_prefix."liveshoutbox WHERE id < ".$id, $conn);

}

// Prints the html structure for the shoutbox
	function jal_get_shoutbox () {
	global $wpdb, $jal_table_prefix, $jal_number_of_comments;
?>
            <div id="wordspew">
				<div id="chatoutput">
					<?php
								 								
 								$wpdb->hide_errors();
								$results = $wpdb->get_results("SELECT * FROM ".$jal_table_prefix."liveshoutbox ORDER BY id DESC LIMIT ".$jal_number_of_comments);
								$wpdb->show_errors();
								
								// Will only add the last message div if it is looping for the first time
								$jal_first_time = true; 
								
								// Loops the messages into a list
								if($results) {foreach( $results as $r ) { 
								
								// Add links								
								$r->text = preg_replace( "`(http|ftp)+(s)?:(//)((\w|\.|\-|_)+)(/)?(\S+)?`i", "<a href=\"\\0\">&laquo;link&raquo;</a>", $r->text);

								if ($jal_first_time == true) { echo '<div id="lastMessage"><span>'.__("Latest Message").'</span> <em id="responseTime">'.jal_time_since( $r->time ).' ago</em></div>'.'<div id="usersOnline">'.htmlspecialchars(jal_get_useronline_extended()).'</div>'.'
 						<ul id="outputList">
 						'; }
 						
 						if ($jal_first_time == true) $lastID = $r->id;
 						
 						$url = (empty($r->url) && $r->url = "http://") ? $r->name : '<a href="'.$r->url.'">'.$r->name.'</a>';
 						 						
						        echo '<li><span title="'.jal_time_since( $r->time ).'">'.stripslashes($url).' : </span>'.convert_smilies(" ".stripslashes($r->text)).'</li>
						        '; 
						        
					$jal_first_time = false; } 
					
					// If there is less than one entry in the box
					} else {
					echo "You need <b>at least one entry</b> in your shoutbox! Just type in a message now and reload, then you should be fine.";
					}
					
					$use_url = (get_option('shoutbox_use_url') == "true") ? TRUE : FALSE;
					$use_textarea = (get_option('shoutbox_use_textarea') == "true") ? TRUE : FALSE;
					$registered_only = (get_option('shoutbox_registered_only') == "1") ? TRUE : FALSE;
					
					global $user_level, $user_nickname, $user_url, $user_ID, $jal_admin_user_level, $userdata;
					get_currentuserinfo(); // Gets logged in user.
					if (!$user_nickname && $userdata) $user_nickname = $userdata->nickname;

				?>
</ul>

				</div>
		<?php if (!$registered_only || ($registered_only && $user_ID)) { ?>
				<form id="chatForm" method="post" action="<?php bloginfo('wpurl'); ?>/wp-content/plugins/wordspew/wordspew.php">
				    <p><?php
					
					if ($user_level >= $jal_admin_user_level) { // If user is allowed to use the admin page
						echo '<a href="'.get_bloginfo("wpurl").'/wp-admin/edit.php?page=wordspew" id="shoutboxAdmin">'.__('Admin').'</a>';
					} 
					if (!empty($user_nickname)) { /* If they are logged in, then print their nickname */ ?>
						
					<label><?php _e('Name'); ?>: <em><?php echo $user_nickname ?></em></label>
					
					<input type="hidden" name="shoutboxname" id="shoutboxname" value="<?php echo $user_nickname; ?>" />
					<input type="hidden" name="shoutboxurl" id="shoutboxurl" value="<?php if($use_url) { echo $user_url; } ?>" />
					<?php } else { echo "\n"; /* Otherwise allow the user to pick their own name */ ?>
					<label for="shoutboxname"><?php _e('Name'); ?>:</label>
					<input type="text" name="shoutboxname" id="shoutboxname" value="<?php if ($_COOKIE['jalUserName']) { echo $_COOKIE['jalUserName']; } ?>" />
					<?php if (!$use_url) { echo '<span style="display: none">'; } ?>
					<label for="shoutboxurl"><?php _e('URL'); ?>:</label>
					<input type="text" name="shoutboxurl" id="shoutboxurl" value="<?php if ($_COOKIE['jalUrl']) { echo $_COOKIE['jalUrl']; } else { echo 'http://'; } ?>" />
					<?php if (!$use_url) { echo "</span>"; } ?>
					<?php  } echo "\n"; ?>
					<label for="chatbarText"><?php _e('Message') ?>:</label>
					<?php if ($use_textarea) { ?>
					<textarea rows="4" cols="16" name="chatbarText" id="chatbarText" onkeypress="return pressedEnter(this,event);"></textarea>
					<?php } else { ?>
					<input type="text" name="chatbarText" id="chatbarText" />
					<?php } ?>
					<input type="hidden" id="jal_lastID" value="<?php echo $lastID + 1; ?>" name="jal_lastID" />
					<input type="hidden" name="shout_no_js" value="true" />
					<input type="submit" id="submitchat" name="submit" value="<?php _e('Send'); ?>" />
					</p>
				</form>
		<?php } else echo "<p>You must be a registered user to participate in this chat</p>"; ?>
            </div>
<?php }

function jal_admin_options() {
    global $wpdb, $jal_table_prefix, $user_level, $jal_admin_user_level;
    
    // Security
    get_currentuserinfo();
    if ($user_level <  $jal_admin_user_level)
        die('Nice try, you cheeky monkey!');

	// Convert from milliseconds
	$fade_length = $_GET['fade_length'] * 1000;
	$update_seconds = $_GET['update_seconds'] * 1000;

	// Update choices from admin panel
	update_option('shoutbox_fade_from', $_GET['fade_from']);
	update_option('shoutbox_fade_to', $_GET['fade_to']);
	update_option('shoutbox_update_seconds', $update_seconds);
	update_option('shoutbox_fade_length', $fade_length);
	update_option('shoutbox_text_color', $_GET['text_color']);
	update_option('shoutbox_name_color', $_GET['name_color']);
	
	$use_url = ($_GET['use_url']) ? "true" : "";
	$use_textarea = ($_GET['use_textarea']) ? "true" : "";
	$registered_only = ($_GET['registered_only']) ? "1" : "0";

	update_option('shoutbox_use_url', $use_url);
	update_option('shoutbox_use_textarea', $use_textarea);
	update_option('shoutbox_registered_only', $registered_only);

}

function jal_shout_edit() {
    global $wpdb, $jal_table_prefix, $user_level, $jal_admin_user_level;
    
    // Security
    get_currentuserinfo();
    if ($user_level <  $jal_admin_user_level)
        die('Nice try, you cheeky monkey!');

	$commentid = intval($_GET['jal_comment_id']);
	if ($commentid . "" != $_GET['jal_comment_id'])
		trigger_error("Malicious comment ID supplied to jal_shout_edit: ".$_GET['jal_comment_id'],
			E_USER_ERROR);

	$wpdb->query("UPDATE ".$jal_table_prefix."liveshoutbox SET text = '".$wpdb->escape($_GET['jal_text'])."' WHERE id = $commentid");
}

function jal_shout_delete() {
    global $wpdb, $jal_table_prefix, $user_level, $jal_admin_user_level;
    
    // Security
    get_currentuserinfo();
    if ($user_level <  $jal_admin_user_level)
        die('Nice try, you cheeky monkey!');

	$commentid = intval($_GET['jal_comment_id']);
	if ($commentid . "" != $_GET['jal_comment_id'])
		trigger_error("Malicious comment ID supplied to jal_shout_delete: ".$_GET['jal_comment_id'],
			E_USER_ERROR);

	$wpdb->query("DELETE FROM ".$jal_table_prefix."liveshoutbox WHERE id = $commentid");
}

function jal_shout_truncate() {
    global $wpdb, $jal_table_prefix, $user_level, $jal_admin_user_level;
    
    // Security
    get_currentuserinfo();
    if ($user_level <  $jal_admin_user_level)
        die('Nice try, you cheeky monkey!');

	$wpdb->query("TRUNCATE TABLE ".$jal_table_prefix."liveshoutbox");
			
   $welcome_name = "Rudd-O";
   $welcome_text = "Your shoutbox is blank. Add a message!";

	$wpdb->query("INSERT INTO ".$jal_table_prefix."liveshoutbox (time,name,text) VALUES ('".time()."','".$welcome_name."','".$welcome_text."')");
}

// If user has updated the admin panel
if (isset($_GET['jal_admin_options']))
    add_action('init', 'jal_admin_options');

// If someone has deleted an entry through the admin panel
if (isset($_GET['jal_delete']))
    add_action('init', 'jal_shout_delete');

// If someone has edited an entry through the admin panel
if (isset($_GET['jal_edit']))
    add_action('init', 'jal_shout_edit');

// If someone has clicked the "delete all" button
if (isset($_GET['jal_truncate']))
    add_action('init', 'jal_shout_truncate');

// Print to the <script> and <link> (for css) to the head of the document
// And adds the admin menu
if (function_exists('add_action')) {
	add_action('wp_head', 'jal_add_to_head');
	add_action('admin_menu', 'shoutbox_admin_page');
	if (strstr($_SERVER['REQUEST_URI'], 'wordspew'))
	   add_action('admin_head', 'jal_add_to_admin_head');
} ?>