PHP Code:
function login() {
/*
This function logs the user in, and assigns the values name, avatar, IP, room and isAdmin to session variables,
and to the data divs on the target page, through ajaxPostEval
*/
$formData = parse_form();
foreach ($formData as $key => $value) {
$$key = htmlentities(mysql_escape_string($value)); // split the POST variable into constituent parts
$_SESSION[$key] = ($key != "avatar") ? $value : ""; // Save them to the session, unless it's the avatar, which is set later
}
$isAdmin = isAdmin($name, $pass); // See if we're dealing with an admin
$_SESSION['avatar'] = ($isAdmin !== false) ? $isAdmin : $avatar; // All admins have special avatars, so if the user's an admin, assign it
$out = <<<endScript
document.getElementByID('uName').innerHTML = '$name'
document.getElementByID("uAvatar").innerHTML = "$avatar";
document.getElementByID("uRoom").innerHTML = "$room";
document.getElementByID("lStat").innerHTML = "true";
showDiv("chatForm");
updateDivs();
endScript;
$warn = "alert(\"You cannot use foul language for your name. Shame on you!\")" ;
$out = (badWords($name)) ? die($warn) : $out; // if the user used foul language in his name, then warn them
$_SESSION['lStat'] = "true";
$message = " has just entered the $room";
$IP = $_SERVER['REMOTE_ADDR'];
addLine($name, $avatar, $room, $message, $IP, 0);
updateUser($name, $avatar, $room);
return $out; // spit out the results and leave
}
// end function login
function addLine ($name, $avatar, $room, $msg) {
/*
This function creates the SQL string that inserts the message into the correct table of the database.
it takes the variables $name, $avatar, $room and $msg (which should all be self explanatory), and creates
the variables $curTime and $IP, to round out the necessary data.
*/
$curTime = time(); // Gets the current time
$IP = $_SERVER['REMOTE_ADDR']; // Obtains user's IP address
$sql = "insert into `$room` (`user_name`, `avatar`, `message`, `IP`, `time`) values ('$name', '$avatar', '$msg', '$IP', '$curTime');";
updateDB($sql);
updateUser($name, $avatar, $room);
}
// end function addLine
function updateDB($sql) {
/*
This is the home for any task that involves writing to the DB, or for generic DB queries that don't require a response.
*/
global $dsn, $lastFunction;
$lastFunction .= "_updateDB";
$dbh = DB::connect($dsn); // Open a channel, Mr. Worf!
$dbh->setErrorHandling (PEAR_ERROR_CALLBACK, 'handleErrors'); // Set up some error handling
$dbh->setFetchMode (DB_FETCHMODE_ASSOC); // Set the output to something usable.
$result = $dbh->query($sql); // Perform the desired DB action
$updatedRows = $dbh->affectedRows() or false; // If the affected rows is zero, send boolean false.
$out = $updatedRows or $result; // Decide what to return.
$dbh->disconnect(); // Unplug, and (un)play?
return $out; // Send 'er out, and shut 'er down.
}
// end function updateDB
function handleErrors($error) {
/*
This function handles any errors that may occur from any of the above DB accessing functions.
I'll update this error routine as needed, during the development of this file.
*/
// [nativecode=1146 ** Table 'pchat.family room' doesn't exist]
global $lastFunction;
$errorMessage = $error->getMessage();
$errorReport = "An error occurred while trying to execute a DB function in $lastFunction.\r\n";
$errorReport .= "Session Vars:\r\n";
$errorReport .= "Error message: $errorMessage\r\n";
$errorReport .= "A more detailed error description: " . $error->getDebugInfo() . "\r\n";
foreach ($_SESSION as $key => $value) {
$errorReport .= "$key = $value\r\n";
}
$errorReport .= "End Session Vars:\r\n\r\n";
// If it's a non-existant room, just make it, and re-run the query
if (strpos($errorMessage,"nativecode=1146") !== false) {
$room = $_SESSION['room'];
makeRoom($room);
updateDB("$sql;");
}
else {
$fh = fopen("db_errors.txt", "a");
$x = fwrite($fh,$errorReport, strlen($errorReport));
fclose($fh);
}
return;
}
// end function handleErrors
function updateUser($name, $avatar, $room) {
global $lastFunction;
$lastFunction = "updateUser";
$curTime = time();
$IP = $_SERVER['REMOTE_ADDR'];
$sql = "delete from `online_users` where `user_name` = '$name' order by `user_name`";
$result = updateDB($sql);
$rowCount = count($result);
$sql = "insert into `online_users` (`user_name`, `avatar`, `room`, `IP`, `lastpost`, `lastPing`, `hasPM`) values ('$name','$avatar','$room','$IP','$curTime','$curTime',0);";
$result = updateDB($sql);
}
function makeRoom($room) {
global $lastFunction;
$lastFunction = "makeRoom";
$sql = "CREATE TABLE `pchat`.`$room` (`user_name` TEXT NOT NULL, `avatar` TEXT NOT NULL, `message` DATE NOT NULL, `IP` TEXT NOT NULL, `time` TEXT NOT NULL)TYPE=MyISAM COMMENT='Messages within $room';";
$result = updateDB($sql);
}
The full script (minus certain sensitive data, of course) can be seen at
Bookmarks