Scripts

Guestbook Spam Protected (MySQL DB)

Zeer simpel PHP gastenboek met MySQL DB door mij gemaakt. Sommige dingen zoals de functie van de emoticons zijn wat onhandig gedaan en zitten wat overbodige dingen in maar had geen zin om ze er uit te halen wat ook niet nodig is het heeft geen invloed op het script. Je kan comments toevoegen en bekijken, het heeft een captcha als beveiliging tegen spam. Het heeft ook de optie om je email te verbergen en om smileys te activeren of deactiveren, dus dat ":)"/":p"/":d" etc vervangen wordt door een smiley. De basis van UBB zit er ook in (I/B/U/CENTER). Het zet ook het ip van de poster in de DB, dit kan je eventueel nog gebruiken om als je het script wilt uitbreiden. Voorbeeld Zonder Opmaak: http://projects.paradox-productions.net/GUESTBOOK/not-styled/guestbook.php Voorbeld Met Opmaak: http://projects.paradox-productions.net/GUESTBOOK/styled/guestbook.php Download: http://downloads.paradox-productions.net/?f=GUESTBOOK.rar - Emoticons Alleen: http://downloads.paradox-productions.net/?f=emoticonsGuestbook.rar Fonts Alleen: http://downloads.paradox-productions.net/?f=fonts.rar

guestbook-spam-protected-mysql-db
SQL CODE:
[code]CREATE TABLE IF NOT EXISTS `guestbook` (
  `id` int(11) NOT NULL auto_increment,
  `name` varchar(128) NOT NULL,
  `email` varchar(512) NOT NULL,
  `showEmail` tinyint(1) NOT NULL,
  `enableEmoticons` tinyint(1) NOT NULL,
  `message` text NOT NULL,
  `time` datetime NOT NULL,
  `ip` varchar(15) NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;[/code]

PHP CODE:

captcha.php
[code]<?php
#######################################
## © 2008 Wouter De Schuyter (Paradox)
## <[email protected]>
## http://paradox-productions.net/
## CAPTCHA V1.0 (SPAM PROTECTION)
#######################################

session_start(); // START SESSION

// FUNCTION TO SELECT A RANDOM CHARACTER OUT OF A STRING
function random_char($string) {
	$length = strlen($string);
	$position = mt_rand(0, $length - 1);
	return $string[$position];
}

$width = 75; // IMG WIDTH (PX)
$height = 25; // IMG HEIGHT (PX)
$characters = "AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz0123456789"; // CHARACTERS FOR CAPTCHA STRING
$font = "fonts/font.ttf"; // FONT LOCATION
$fontS = 12; // FONT SIZE (PX)
$min = 50; // MIN NUMBER FOR THE RANDOM RGB TEXT COLOR
$max = 200; // MAX NUMBER FOR THE RANDOM RGB TEXT COLOR

// ADVANCED
////////////
$positionCharacterX = 3; // POSITION CHARACTER 1 ON THE X-AXIS (PX)
$characterSpace = 20; // SPACE FOR 1 CHARACTER (PX)
$positionCharactersY = 17; // SPACE ON THE Y-AXIS (PX)

$img = imagecreate($width, $height);
imagecolorallocate($img, 255, 255, 255); // BACKGROUND COLOR IN RGB

$randNr1 = rand($min, $max); // RANDOM NUMBER 1 BETWEEN $min & $max
$randNr2 = rand($min, $max); // RANDOM NUMBER 2 BETWEEN $min & $max
$randNr3 = rand($min, $max); // RANDOM NUMBER 3 BETWEEN $min & $max

$randomChar1 = random_char($characters); // RANDOM CHARACTER 1
$randomChar2 = random_char($characters); // RANDOM CHARACTER 2
$randomChar3 = random_char($characters); // RANDOM CHARACTER 3
$randomChar4 = random_char($characters); // RANDOM CHARACTER 4

$textcolor1 = imagecolorallocate($img, $randNr1, $randNr2, $randNr3); // TEXT COLOR 1
$textcolor2 = imagecolorallocate($img, $randNr2, $randNr3, $randNr1); // TEXT COLOR 2
$textcolor3 = imagecolorallocate($img, $randNr3, $randNr1, $randNr2); // TEXT COLOR 3
$textcolor4 = imagecolorallocate($img, $randNr3, $randNr2, $randNr1); // TEXT COLOR 4

imagettftext($img, $fontS, 0, $positionCharacterX + 0 * $characterSpace, $positionCharactersY, $textcolor1, $font, $randomChar1); // CHARACTER 1
imagettftext($img, $fontS, 0, $positionCharacterX + 1 * $characterSpace, $positionCharactersY, $textcolor2, $font, $randomChar2); // CHARACTER 2
imagettftext($img, $fontS, 0, $positionCharacterX + 2 * $characterSpace, $positionCharactersY, $textcolor3, $font, $randomChar3); // CHARACTER 3
imagettftext($img, $fontS, 0, $positionCharacterX + 3 * $characterSpace, $positionCharactersY, $textcolor4, $font, $randomChar4); // CHARACTER 4

$_SESSION['captcha'] = $randomChar1 . $randomChar2 . $randomChar3 . $randomChar4; // CAPTCHA STRING FOR SESSION

header("content-type: image/png"); // CONTENT TYPE

imagepng($img); // CREATE IMAGE
imagedestroy($img); // DESTROY IMAGE
?>[/code]

font: http://downloads.paradox-productions.net/?f=fonts.rar

connection.inc.php
[code]<?php
#######################################
## © 2008 Wouter De Schuyter (Paradox)
## <[email protected]>
## http://paradox-productions.net/
## CONNECTION
#######################################

	// CONFIG
	////////////
	$hostname = "localhost"; // HOST (99% of the time localhost)
	$username = ""; // USER
	$password = ""; // PASS
	$database = ""; // DATABASE

/////////////////////////////////////////////////


	if($_SERVER['REMOTE_ADDR'] == "127.0.0.1") {
		$host = "localhost";
		$user = "root";
		$pass = "usbw";
		$db_name = "guestbook";
	}
	else {
		$host = $hostname;
		$user = $username;
		$pass = $password;
		$db_name = $database;
	}
	mysql_connect($host, $user, $pass) or die("<b>Could not connect to MySQL..</b><br />" . mysql_error());
	mysql_select_db($db_name) or die("<b>Could not connect to Database..</b><br />" . mysql_error());

?>[/code]

guestbook.php
[code]<?php
session_start();
require('connection.inc.php');

#####################################
## © 2008 Wouter De Schuyter
## <[email protected]>
## Guestbook V1.0
#####################################

// SET VARIABLES
//////////////////

$minName = 2; // minimum lenght name
$maxName = 32; // maximum lenght name
$minEmail = 8; // minimum lenght email
$maxEmail = 256; // maximum lenght email
$minMessage = 8; // minimum lenght message
$maxMessage = 2560; // maximum lenght message

///////////////////

$act = $_GET['action'];

// FUNCTIONS
//////////////

// REPLACE TEXT BY EMOTICONS
function emoticons($string) {

$emoticonsDirectory = "img/emoticons"; // directory from emoticons | example: "img/emoticons"

    $array1 = array(
                    '^^',
                    '(!)',
                    '(?)',
                    '(A)',
                    '(a)',
                    ':)',
                    '=)',
                    '(:',
                    '(=',
                    '):',
                    ')=',
                    ':(',
                    '=(',
                    ';)',
                    ':d',
                    ':D',
                    '=d',
                    '=D',
                    ':p',
                    ':P',
                    '=p',
                    '=P',
                    '(8)',
                    ':s',
                    ':S',
                    '=s',
                    '=S',
                    's:',
                    'S:',
                    's=',
                    'S=',
                    ':o',
                    ':O',
                    '=o',
                    '=O',
                    'o:',
                    'O:',
                    'o=',
                    'O=',
                    '(l)',
                    '(L)',
                    ':$',
                    '=$',
                    '$:',
                    '$=',
                    ':\'(',
                    '=\'(',
                    ')\'=',
                    ')\':',
                    ':@',
                    '=@',
                    ' >< ',
                    '-_-',
                    '-.-',
                    'f5'
                    );
    $array2 = array(
                    '<img src="' . $emoticonsDirectory . '/happy.gif" alt="Emoticon" />',
                    '<img src="' . $emoticonsDirectory . '/exclamationmark.gif" alt="Emoticon" />',
                    '<img src="' . $emoticonsDirectory . '/questionmark.gif" alt="Emoticon" />',
                    '<img src="' . $emoticonsDirectory . '/angel.png" alt="Emoticon" />',
                    '<img src="' . $emoticonsDirectory . '/angel.png" alt="Emoticon" />',
                    '<img src="' . $emoticonsDirectory . '/smile.gif" alt="Emoticon" />',
                    '<img src="' . $emoticonsDirectory . '/smile.gif" alt="Emoticon" />',
                    '<img src="' . $emoticonsDirectory . '/smile.gif" alt="Emoticon" />',
                    '<img src="' . $emoticonsDirectory . '/smile.gif" alt="Emoticon" />',
                    '<img src="' . $emoticonsDirectory . '/sad.gif" alt="Emoticon" />',
                    '<img src="' . $emoticonsDirectory . '/sad.gif" alt="Emoticon" />',
                    '<img src="' . $emoticonsDirectory . '/sad.gif" alt="Emoticon" />',
                    '<img src="' . $emoticonsDirectory . '/sad.gif" alt="Emoticon" />',
                    '<img src="' . $emoticonsDirectory . '/wink.gif" alt="Emoticon" />',
                    '<img src="' . $emoticonsDirectory . '/biggrin.gif" alt="Emoticon" />',
                    '<img src="' . $emoticonsDirectory . '/biggrin.gif" alt="Emoticon" />',
                    '<img src="' . $emoticonsDirectory . '/biggrin.gif" alt="Emoticon" />',
                    '<img src="' . $emoticonsDirectory . '/biggrin.gif" alt="Emoticon" />',
                    '<img src="' . $emoticonsDirectory . '/tongue.gif" alt="Emoticon" />',
                    '<img src="' . $emoticonsDirectory . '/tongue.gif" alt="Emoticon" />',
                    '<img src="' . $emoticonsDirectory . '/tongue.gif" alt="Emoticon" />',
                    '<img src="' . $emoticonsDirectory . '/tongue.gif" alt="Emoticon" />',
                    '<img src="' . $emoticonsDirectory . '/bandit.gif" alt="Emoticon" />',
                    '<img src="' . $emoticonsDirectory . '/confused.gif" alt="Emoticon" />',
                    '<img src="' . $emoticonsDirectory . '/confused.gif" alt="Emoticon" />',
                    '<img src="' . $emoticonsDirectory . '/confused.gif" alt="Emoticon" />',
                    '<img src="' . $emoticonsDirectory . '/confused.gif" alt="Emoticon" />',
                    '<img src="' . $emoticonsDirectory . '/confused.gif" alt="Emoticon" />',
                    '<img src="' . $emoticonsDirectory . '/confused.gif" alt="Emoticon" />',
                    '<img src="' . $emoticonsDirectory . '/confused.gif" alt="Emoticon" />',
                    '<img src="' . $emoticonsDirectory . '/confused.gif" alt="Emoticon" />',
                    '<img src="' . $emoticonsDirectory . '/ooo.gif" alt="Emoticon" />',
                    '<img src="' . $emoticonsDirectory . '/ooo.gif" alt="Emoticon" />',
                    '<img src="' . $emoticonsDirectory . '/ooo.gif" alt="Emoticon" />',
                    '<img src="' . $emoticonsDirectory . '/ooo.gif" alt="Emoticon" />',
                    '<img src="' . $emoticonsDirectory . '/ooo.gif" alt="Emoticon" />',
                    '<img src="' . $emoticonsDirectory . '/ooo.gif" alt="Emoticon" />',
                    '<img src="' . $emoticonsDirectory . '/ooo.gif" alt="Emoticon" />',
                    '<img src="' . $emoticonsDirectory . '/ooo.gif" alt="Emoticon" />',
                    '<img src="' . $emoticonsDirectory . '/heart.gif" alt="Emoticon" />',
                    '<img src="' . $emoticonsDirectory . '/heart.gif" alt="Emoticon" />',
                    '<img src="' . $emoticonsDirectory . '/blush.gif" alt="Emoticon" />',    
                    '<img src="' . $emoticonsDirectory . '/blush.gif" alt="Emoticon" />',    
                    '<img src="' . $emoticonsDirectory . '/blush.gif" alt="Emoticon" />',    
                    '<img src="' . $emoticonsDirectory . '/blush.gif" alt="Emoticon" />',    
                    '<img src="' . $emoticonsDirectory . '/crying.gif" alt="Emoticon" />',    
                    '<img src="' . $emoticonsDirectory . '/crying.gif" alt="Emoticon" />',    
                    '<img src="' . $emoticonsDirectory . '/crying.gif" alt="Emoticon" />',    
                    '<img src="' . $emoticonsDirectory . '/crying.gif" alt="Emoticon" />',    
                    '<img src="' . $emoticonsDirectory . '/angry.gif" alt="Emoticon" />',    
                    '<img src="' . $emoticonsDirectory . '/angry.gif" alt="Emoticon" />',
                    ' <img src="' . $emoticonsDirectory . '/hmmpff.gif" alt="Emoticon" /> ',
                    '<img src="' . $emoticonsDirectory . '/hmmpff.gif" alt="Emoticon" />',
                    '<img src="' . $emoticonsDirectory . '/hmmpff.gif" alt="Emoticon" />',
                    '<img src="' . $emoticonsDirectory . '/hmmpff.gif" alt="Emoticon" />'
                    );
    $output = str_replace($array1, $array2, $string);
    return $output;
}

// UBB CODE
function ubb($string) {
    $array1 = array(
                    '[b]',
                    '[/b]',
                    '[u]',
                    '[/u]',
                    '[center]',
                    '[/center]',
                    '[i]',
                    '[/i]'
                    );
    $array2 = array(
                    '<b>',
                    '</b>',
                    '<u>',
                    '</u>',
                    '<center>',
                    '</center>',
                    '<i>',
                    '</i>'
                    );
    $output = str_replace($array1, $array2, $string);
    return $output;
}

// VALID
function valid($string) {
    $array1 = array(
                    '<br>',
                    '<noscript>'
                    );
    $array2 = array(
                    '<br />',
                    '*noscript*'
                    );
    $output = str_replace($array1, $array2, $string);
    return $output;
}

echo '
<a href="?action=homepage">Homepage</a>
|
<a href="?action=addComment">Add Comment</a>
|
<a href="?action=viewComments">View Comment(s)</a>';

// WHEN ACTION IS "Add Comment"
if($act == "addComment") {
    
    echo "<h3>Add Comment</h3>\n";
    
    if($_SERVER['REQUEST_METHOD'] == "POST") {
        $name = addslashes(ucfirst(trim($_POST['name']))); // NAME
        $email = addslashes($_POST['email']); // EMAIL
        $showEmail = $_POST['showEmail']; // SHOW/HIDE EMAIL
        $emoticons = $_POST['emoticons']; // ENABLE/DISABLE EMOTICONS
        $message = addslashes(ucfirst(trim($_POST['message']))); // MESSAGE
        $captcha = $_POST['captcha']; // CAPTCHA
        $captchaVer = $_SESSION['captcha']; // CAPTCHA CHECK
        $time = date("Y/m/d H:i:s"); // TIME
        $ip = $_SERVER['REMOTE_ADDR']; // IP
        $regexp    = "/^[a-z0-9_]+([_\\.-][a-z0-9_]+)*@([a-z0-9_]+([\.-][a-z0-9]+)*)+\\.[a-z]{2,}$/i"; // EMAIL CHECK
        
        // GENERAL FIELD CHECK
        if(strlen($name) < 1 or strlen($email) < 1 or strlen($message) < 1 or strlen($captcha) < 1) {
            echo "<p>Make sure all the required fields are correctly filled in!</p>\n";
            $generalError = true;
        }
        elseif($generalError !== true) {
            // CHECK NAME LENGHT
            if(strlen($name) < $minName) {
                echo "<p>Your name must contain at least " . $minName . " characters! Please enter a longer name.</p>\n";
                $lenghtError = true;
            }
            elseif(strlen($name) > $maxName) {
                echo "<p>Your name can not contain more than " . $maxName . " characters! Please enter a shorter name.</p>\n";
                $lenghtError = true;
            }
            
            // CHECK EMAIL LENGHT
            if(strlen($email) < $minEmail) {
                echo "<p>Your email must contain at least " . $minEmail . " characters! Please enter a longer email.</p>\n";
                $lenghtError = true;
            }
            elseif(strlen($email) > $maxEmail) {
                echo "<p>Your email can not contain more than " . $maxEmail . " characters! Please enter a shorter email.</p>\n";
                $lenghtError = true;
            }
            
            // CHECK MESSAGE LENGHT
            if(strlen($message) < $minMessage) {
                echo "<p>Your message must contain at least " . $minMessage . " characters! Please enter a longer message.</p>\n";
                $lenghtError = true;
            }
            elseif(strlen($message) > $maxMessage) {
                echo "<p>Your message can not contain more than " . $maxMessage . " characters! Please enter a shorter message.</p>\n";
                $lenghtError = true;
            }
            
            // CHECK CAPTCHA LENGHT
            if(strlen($captcha) !== 4) {
                echo "<p>Your captcha verefication can not contain more or less than 4 characters!</p>\n";
                $lenghtError = true;
            }
            
            if($lenghtError !== true) {
                
                // VALID EMAIL ?
                if(!preg_match($regexp, $email)) {
                    echo "<p>Your email is not valid! Please try again.</p>\n";
                    $error = true;
                }
                
                // CAPTCHA CORRECT?
                if($captcha !== $captchaVer) {
                    echo "<p>Your captcha verefication code was inccorect! Please try again.</p>\n";
                    $error = true;
                }
                
                if($error !== true) {
                    $insertQuery = "INSERT INTO `guestbook` (`name`, `email`, `showEmail`, `enableEmoticons`, `message`, `time`, `ip`) VALUES ('" . $name . "', '" . $email . "', '" . $showEmail . "', '" . $emoticons . "', '" . $message . "', '" . $time . "', '" . $ip . "')";
                    $insert = mysql_query($insertQuery);
                    if($insert) {
                        echo "<p>Your message was successfully loaded into the guestbook database!<br /><a href=\"?action=viewComments\">Click Here</a> to view the guestbook entries.</p>\n";
                        $success = true;
                    }
                    else {
                        echo "<p>Error<br />" . mysql_error() . "</p>\n";
                    }
                }
            }
        }
    }
    if($success !== true) {
?>
                <form action="<?php echo $_SERVER['PHP_SELF']; ?>?action=<?php echo $act; ?>" method="post">
                    <table>
                        <tr>
                            <td>Name</td>
                            <td><input type="text" name="name" maxlength="<?php echo $maxName; ?>" value="<?php echo stripslashes($name); ?>" /></td>
                        </tr>
                        <tr>
                            <td>Email</td>
                            <td><input type="text" name="email" maxlength="<?php echo $maxEmail; ?>" value="<?php echo stripslashes($email); ?>" /></td>
                        </tr>
                        <tr>
                            <td>Show Email</td>
                            <td>
                                Yes <input type="radio" name="showEmail" value="1"<?php if($showEmail == 1) { echo " checked=\"checked\""; } ?> />
                                No <input type="radio" name="showEmail" value="0"<?php if($showEmail == 0) { echo " checked=\"checked\""; } ?> />
                            </td>
                        </tr>
                        <tr>
                            <td>Enable Emoticons</td>
                            <td>
                                Yes <input type="radio" name="emoticons" value="1"<?php if($emoticons == 1) { echo " checked=\"checked\""; } ?> />
                                No <input type="radio" name="emoticons" value="0"<?php if($emoticons == 0) { echo " checked=\"checked\""; } ?> />
                            </td>
                        </tr>
                        <tr>
                            <td>Message</td>
                            <td><textarea name="message" rows="4" cols="30"><?php echo stripslashes($message); ?></textarea></td>
                        </tr>
                        <tr>
                            <td><img src="captcha/captcha.php" alt="Are You Human?" /></td>
                            <td><input type="text" name="captcha" maxlength="4" size="6" /> <span class="note">(Vereficate You Are Human)</span></td>
                        </tr>
                        <tr>
                            <td><input type="reset" value="Reset Form" /></td>
                            <td><input type="submit" value="Save Message" /></td>
                        </tr>
                    </table>
                </form>
<?php
    }
}
// WHEN ACTION IS "View Comment(s)"
elseif($act == "viewComments") {
    $sql = "SELECT `name`, `email`, `showEmail`, `enableEmoticons`, `message`, `time` FROM guestbook ORDER BY `id` DESC";
    $dataQuery = mysql_query($sql);
    
    echo "<h3>View Comments (" . mysql_num_rows($dataQuery) . ")</h3>\n";
    if(mysql_num_rows($dataQuery) == 0) {
        echo "<p>There are currently no guestbook entries. <a href=\"?action=addComment\">Be first to make one!</a></p>\n";
    }
    else {
        while($data = mysql_fetch_assoc($dataQuery)) {
    ?>
    <table border="1px" width="400px">
        <tr>
            <td><?php if($data['showEmail'] == 1) { ?><a href="mailto:<?php echo $data['email']; ?>"><?php } echo stripslashes($data['name']); if($data['showEmail'] == 1) { ?></a><?php } ?> wrote on <?php $date = new DateTime($data['time']); echo $date->format('d/m/Y, H:i:s'); ?></td>
        </tr>
        
        <tr>
            <td>
            <?php
            $message = ubb(nl2br(stripslashes(htmlentities(valid($data['message'])))));
                if($data['enableEmoticons'] == 1) {
                    echo emoticons($message);                
                }
                if($data['enableEmoticons'] == 0) {
                    echo $message;                
                }
            ?>
            </td>
        </tr>
    </table>
    <?php
        }
    }
}
else {
    echo "<h3>Guestbook V1.0 - Provided by www.paradox-productions.net</h3>\n";
?>
<ul>
    <li>Add Comment</li>
    <li>Read Comments</li>
    <li>Secured against
        <ul>
            <li>Spam(bots)</li>
            <li>SQL Injection</li>
            <li>Javascript Injection</li>
            <li>Form Injection</li>
            <li>(x)html/php filter</li>
        </ul>
    </li>
    <li>Option to hide your email</li>
    <li>Option to enable emoticons</li>
       <li>Config</li>
</ul>
<?php } echo "<center><p>&copy; 2008 All Rights Reserved To Paradox-Productions.Net | Designed &amp; Coded by Paradox</p></center>"; ?>[/code]

Hoe installeren?
Maak een map aan "captcha" maak daarin het bestand captcha.php aan. plaats in de captcha map een andere map "fonts" en plaats daarin het font (font.ttf).
Maak in de hoofdmap waar de captcha map in staat connection.inc.php aan en guestbook.php
Maak een dir aan in diezelfde hoofdmap met de emoticons en pas vervolgens guestbook.php aan naar de naam van de dir met de emoticons. Voor het standaard maak je een map "img" aan en daarin een map "emoticons" en daar zet je de emoticons in.

Maar het gemakkelijkste is dat je gewoon dit http://downloads.paradox-productions.net/?f=GUESTBOOK.rar download extract en dan de non styled version gebruikt om in je website te integreren.

Reacties

0
Nog geen reacties.