<?php
function connectToDB() {
global $dbhost, $dbuser, $dbpass, $dbname,$mysql,$mysqli;
($mysql = mysql_pconnect("$dbhost", "$dbuser", "$dbpass")) || die("Couldn't connect to MySQL");
// select db:
mysql_select_db("$dbname", $mysql) || die("Couldn't open db: $dbname. Error if any was: ".mysql_error() );
} // end func dbConnect();
function newUser($full_name,$email,$username, $password,$ip) {
global $ip;
global $mysql;
mysql_query("INSERT INTO users (full_name,email,username, password,updated,ip) VALUES('$full_name','$email','$username', '$password',now(),'$ip')");
return true;
} // end func newUser($username, $pass)
function displayErrors($messages) {
/*
Error Handling functions:
An error handling function is useful to have in any project.
This particular function takes an array of messages, and
for each message displays it in a list using HTML
<ul><li></li></ul> tags.
*/
print("<b>There were problems with the previous action. Following is a list of the error messages generated:</b>\n<ul>\n");
foreach($messages as $msg){
print("<li>$msg</li>\n");
}
print("</ul>\n");
} // end func displayErrors($messages)
function checkLoggedIn($status){
global $status;
switch($status){
// if yes, check user is logged in:
// ie for actions where, yes, user must be logged in(!)
case "yes":
if(!isset($_SESSION["loggedIn"])){
header("Location: addtopic.php");
exit;
}
break;
// if no, check NOT logged in:
// ie for actions where user can't already be logged in
// (ie for joining up or logging in)
case "no":
/*
The '===' operator differs slightly from the '=='
equality operator.
$a === $b if and only if $a is equal to $b AND
$a is the same variable type as $b.
for example, if:
$a="2"; <-- $a is a string here
$b=2; <-- $b is an integer here
then this test returns false:
if($a===$b)
whereas this test returns true:
if($a==$b)
*/
if(isset($_SESSION["loggedIn"]) && $_SESSION["loggedIn"] === true ){
header("Location: login.php");
}
break;
}
// if got here, all ok, return true:
return true;
} // end func checkLoggedIn($status)
function checkPass($username, $password) {
/*
Password checking function:
This is a simple function that takes the $username name and
$password that a user submits in a form and checks that a
row exists in the database where:
the value of the 'username' column is the same as the value in $username
and
the value of the 'password' column is the same as the value in $password
If exactly one row is returned, then that row of data is returned.
If no row is found, the function returns 'false'.
*/
global $mysql;
$query="SELECT username, password status FROM users WHERE username='$username' and password='$password'";
$result=mysql_query($query) or die("checkPass fatal error: ".mysql_error());
// Check exactly one row is found:
if(mysql_num_rows($result)==1) {
$row=mysql_fetch_array($result);
return $row;
}
//Bad username:
return false;
} // end func checkPass($username, $password)
function cleanMemberSession($username, $password) {
/*
Member session initialization function:
This function initializes 3 session variables:
$username, $password and $loggedIn.
$username and $password are used on member pages (where you
could allow the user to change their password for example).
$loggedIn is a simple boolean variable which indicates
whether or not the user is currently logged in.
*/
$_SESSION["username"]=$username;
$_SESSION["password"]=$password;
$_SESSION["loggedIn"]=true;
$sql = mysql_query("update users set status='yes' where username='$username'")or die(mysql_error());
$expire=time()+60*60*24*30;
setcookie("user", $username, $expire);
$dt=$username;
} // end func cleanMemberSession($username, $pass)
function flushMemberSession() {
/*
Member session destruction function:
This function unsets all the session variables initialized
above and then destroys the current session.
*/
// use unset to destroy the session variables
global $username,$password,$mysql;
$StringData = "\n";
fwrite($handling, $StringData);
unset($_SESSION["username"]);
unset($_SESSION["password"]);
unset($_SESSION["loggedIn"]);
// and use session_destroy to destroy all data associated
// with current session:
session_destroy();
return true;
} // send func flushMemberSession()
function doCSS() {
/*
CSS Output:
This function simply outputs some cascading style sheet
data for markup by the user's browser.
*/
?>
<style type="text/css">
body{font-family: Arial, Helvetica; font-size: 10pt}
h1{font-size: 12pt}
</style>
<?php
} // end func doCSS()
# function validates HTML form field data passed to it:
function field_validator($field_descr, $field_data,
$field_type, $min_length="", $max_length="",
$field_required=1) {
/*
Field validator:
This is a handy function for validating the data passed to
us from a user's <form> fields.
Using this function we can check a certain type of data was
passed to us (email, digit, number, etc) and that the data
was of a certain length.
*/
# array for storing error messages
global $messages;
# first, if no data and field is not required, just return now:
if(!$field_data && !$field_required){ return; }
# initialize a flag variable - used to flag whether data is valid or not
$field_ok=false;
# this is the regexp for email validation:
$email_regexp="^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|";
$email_regexp.="(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$";
# a hash array of "types of data" pointing to "regexps" used to validate the data:
$data_types=array(
"email"=>$email_regexp,
"digit"=>"^[0-9]$",
"number"=>"^[0-9]+$",
"alpha"=>"^[a-zA-Z]+$",
"alpha_space"=>"^[a-zA-Z ]+$",
"alphanumeric"=>"^[a-zA-Z0-9]+$",
"alphanumeric_space"=>"^[a-zA-Z0-9 ]+$",
"string"=>""
);
# check for required fields
if ($field_required && empty($field_data)) {
$messages[] = "$field_descr is a required field.";
return;
}
# if field type is a string, no need to check regexp:
if ($field_type == "string") {
$field_ok = true;
} else {
# Check the field data against the regexp pattern:
$field_ok = ereg($data_types[$field_type], $field_data);
}
# if field data is bad, add message:
if (!$field_ok) {
$messages[] = "Please enter a valid $field_descr.";
return;
}
# field data min length checking:
if ($field_ok && ($min_length > 0)) {
if (strlen($field_data) < $min_length) {
$messages[] = "$field_descr is invalid, it should be at least $min_length character(s).";
return;
}
}
# field data max length checking:
if ($field_ok && ($max_length > 0)) {
if (strlen($field_data) > $max_length) {
$messages[] = "$field_descr is invalid, it should be less than $max_length characters.";
return;
}
}
}
//***************************************
function confirm($dbpass){
global $pass;
if($pass!=$dbpass){header("location:login.php");}
}
//--------------------------------------------------------------------
function ccount($ip,$ccount){
global $ccount,$files,$mysql;
$ip = $_SERVER['REMOTE_ADDR'];
mysql_query("update uploads set ccount=$ccount where files=$files);");
}
?>