$sql = mysql_query("select * from $table where imageID='$id' order by date DESC");
if( mysql_errno()!=0 )
echo( reportError("Select in showAllPhotoRemarks()", mysql_errno(), mysql_error()) );
while( $row=mysql_fetch_array($sql))
{
$msg = str_replace("\n"," ",$row["msg"]);
$name = $row["name"];
//restore quotes, etc., if needed
if( !get_magic_quotes_gpc() )
{
$name = stripslashes($name);
$msg = stripslashes($msg);
}
echo("\n");
} //end while
mysql_free_result($sql);
}
if( $admin==false ) //exhibit Add Remark form
showForm($script, $admin, "", "", "0");
}
function showForm($script, $admin, $name, $msg, $id) //displays the remarks input form
{
$action = $admin ? "5" : "2";
echo( "\n\t" );
}
function fetchRemarksCount($table, $id)
{
$sql = mysql_query("select * from $table where imageID='$id'");
if( mysql_errno()!=0 )
echo( reportError("Select in fetchRemarksCount()", mysql_errno(), mysql_error()) );
$numOfRows = mysql_num_rows($sql);
mysql_free_result($sql);
return $numOfRows;
}
function validateEntry()
{
//check to see if either name or msg is empty & display warning message if so
$name = (IsSet($_POST["name"]))?$_POST["name"]:"";
$msg = (IsSet($_POST["msg"]))?$_POST["msg"]:"";
$valid = true;
if( (strlen($name)<1) || (strlen($msg)<1) )
{
echo("".MSG_EMPTY_AUTHOR_OR_REMARK." ");
$valid = false;
}
//escape quotes and slashes if necessary
if( !get_magic_quotes_gpc() )
{
$name = addslashes($name);
$msg = addslashes($msg);
}
//create allowable opening html tags array
$open_tag[1]=""; //permit bold and italics
$open_tag[2]="";
$open_tag[3]="";
$open_tag[4]="";
$allowable_tags=""; //initialize and build allowable html tags string
foreach ($open_tag as $tag) {$allowable_tags=$allowable_tags.$tag."".substr($tag,1);}
$name = strip_tags($name); //strip off all html tags but permit foreign chars
$msg = strip_tags($msg, $allowable_tags); //strip off unallowed html tags but permit foreign chars
//check for unbalanced open tags..close tags if needed (do not worry about order)
foreach ($open_tag as $tag)
{
$pos1=strpos($msg,$tag); $t1=!($pos1===false);//$t1 is true if open tag exists
$pos2=strpos($msg,"".substr($tag,1)); $t2=!($pos2===false);//$t2 is true if close tag exists
if ($t1 && !$t2) $msg=$msg."".substr($tag,1); //create close tag if needed
}
return new Remark($name,$msg,$valid);
}
function fetchDate($date)
{
return( date(DATEFMT, strtotime($date)).DATE_SUFFIX );
}
function sendNotification($table_name, $imageID, $name, $msg)
{
$notification_msg = "\n".NOTIFY_PHOTO_ID.": ".$imageID."\n".NOTIFY_AUTHOR.": ".$name."\n".NOTIFY_MSG.":\n"."\n".$msg;
$x = mail(NOTIFY_TO, NOTIFY_SUB, $notification_msg, "From: ".NOTIFY_FROM);
}
function addPhotoRemark($table, $imageID, $remarkEntry)
{
$query = "insert into $table (imageID, name, date, msg, ip1, ip2, refer) VALUES('$imageID','$remarkEntry->name', NOW(), '$remarkEntry->msg', '".gethostbyaddr($_SERVER["REMOTE_ADDR"])."',";
if( !empty($_SERVER['HTTP_X_FORWARDED_FOR']) )
$query .= "'" . gethostbyaddr($_SERVER['HTTP_X_FORWARDED_FOR']) . "'";
else
$query .= " NULL";
$query .= ",'".addslashes($_SERVER["HTTP_REFERER"])."')";
mysql_query($query);
if( mysql_errno()!=0 )
echo( reportError("Add Remark", mysql_errno(), mysql_error()) );
else
{
if( NOTIFY ) // send notification email if enabled
sendNotification($table, $imageID, $remarkEntry->name, $remarkEntry->msg);
}
}
function updatePhotoRemark($table, $remarkEntry)
{
mysql_query("update $table SET name= '$remarkEntry->name' , msg= '$remarkEntry->msg' where id = '$_POST[id]'");
if( mysql_errno()!=0 )
echo( reportError("Update Remark", mysql_errno(), mysql_error()) );
}
function deletePhotoRemark($table)
{
mysql_query("delete from $table where id = '$_POST[id]'");
if( mysql_errno()!=0 )
echo( reportError("Delete remark for photo", mysql_errno(), mysql_error()) );
else
echo("");
}
function deleteAllPhotoRemarks($table, $imageID)
{
mysql_query("delete from $table where imageID = '".addslashes($imageID)."'");
if( mysql_errno()!=0 )
echo( reportError("Delete all remarks for photo", mysql_errno(), mysql_error()) );
else
echo("");
}
//////////
// super admin features not availble through GUI--browse to special URL only
//////////
/**
* deletes ALL remarks from this album
*
* Example:
* http://www.server.com/picture1234.php?action=666&password=myDbPassword
*
* NOTE: database password required
*/
function deleteAllRemarks($table)
{
$password = (IsSet($_POST["password"]))?$_POST["password"]:"";
$x=!strcmp(DB_PASSWORD, $password);
if( !$x )
echo("");
else
{
mysql_query("drop table ".$table);
if( mysql_errno()!=0 )
echo( reportError("Drop Table", mysql_errno(), mysql_error()) );
else
echo("");
}
}
/**
* Shows all remarks for pix in this album
*
* Example:
* http://www.server.com/picture1234.php?action=100&password=myDbPassword
*
* NOTE: database password required
*/
function showAllAlbumRemarks($table_name)
{
$result = mysql_query("select * from ".$table_name);
if( mysql_errno()!=0 )
echo( reportError("Dump table", mysql_errno(), mysql_error() ) );
$table .= "\n\n";
$noFields = mysql_num_fields($result);
$table .= "\n";
for( $i = 0; $i<$noFields; $i++ )
{
$field = mysql_field_name($result, $i);
$table .= "\t| $field | \n";
}
while( $r = mysql_fetch_row($result) )
{
$table .= " \n";
foreach ($r as $col)
{
$table .= "\t| $col | \n";
}
$table .= " \n";
}
$table .= " \n\n";
echo $table;
}
function reportError($errloc, $errno, $errmsg)
{
$msg = "\n";
$msg .= "\n\t A database error has occurred!!";
$msg .= "\n\t Please report the following error details to site administrators:";
$msg .= "\n\t ";
$msg .= "\n\t\t- Error location: $errloc
";
$msg .= "\n\t\t- Error no.: $errno
";
$msg .= "\n\t\t- Error message: $errmsg
";
$msg .= "\n\t ";
$msg .= "\n ";
return $msg;
}
include "connect.php";
/////// open connection to the database
$connect = mysql_connect(DB_URL, DB_USERNAME, DB_PASSWORD) or die("Cannot connect to database: ".mysql_error());
mysql_select_db(DB_NAME,$connect) or die("Cannot select database: ".mysql_error());
/////// create table if needed
//ensure that table name has only allowable characters
$bad_chars = "!@#$%^&*()+<>?{}|[]\:,.-= "; //translate non-allowed characters to '_'
$table = strtolower(strtr(DB_TABLE_NAME, $bad_chars, str_repeat("_", strlen($bad_chars))));
mysql_query("create table if not exists ".$table." (id int not null AUTO_INCREMENT, imageID char(200), date datetime not null default '0000-00-00 00:00:00', name char(100), msg text, ip1 char(100), ip2 char(100), refer char(255), primary key (id))");
if( mysql_errno()!=0 )
echo( reportError("Error Creating Table $table", mysql_errno(), mysql_error()) );
?>
|