Location: PHPKode > projects > Banshee PHP Framework > libraries/posting.php
<?php
	/* libraries/posting.php
	 *
	 * Copyright (C) by Hugo Leisink <hide@address.com>
	 * This file is part of the Banshee PHP framework
	 * http://www.banshee-php.org/
	 */

	/* Log message (used by message_is_spam())
	 *
	 * INPUT:  string message
	 * OUTPUT: -
	 * ERROR:  -
	 */
	function _antispam_log($reason) {
		if (($fp = fopen("../logfiles/spam.log", "a")) != false) {
			fputs($fp, $_SERVER["REMOTE_ADDR"]."|".date("D d M Y H:i:s")."|".$reason."\n");
			fclose($fp);
		}
	}

	/* Set form build time. Use during form rendering and before using message_is_spam().
	 *
	 * INPUT:  [string Unix timestamp]
	 * OUTPUT: -
	 * ERROR:  -
	 */
	function set_form_build_time($time = null) {
		$_SESSION["antispam_form_build_time"] = ($time == null ? time() : $time);
	}

	/* Determine whether a message is spam or not
	 *
	 * INPUT:  string message
	 * OUTPUT: boolean message is span
	 * ERROR:  -
	 */
	function message_is_spam($message) {
		$antispam = array();
		$index = false;

		/* Read the configuration file
		 */
		foreach (config_file("antispam") as $line) {
			if ($line[0] == "%") {
				$index = substr($line, 1);
				$antispam[$index] = array();
			} else if ($index === false) {
				list($key, $value) = explode("=", $line, 2);
				$antispam[trim($key)] = trim($value);
			} else {
				array_push($antispam[$index], $line);
			}
		}

		/* Check for blocked IP address
		 */
		foreach ($antispam["blocked_ip"] as $blocked_ip) {
			if (ip_match($_SERVER["REMOTE_ADDR"], $blocked_ip)) {
				_antispam_log("blocked ip");
				return true;
			}
		}

		/* Check if POST is done too quickly
		 */
		if (isset($antispam["min_delay"])) {
			if (isset($_SESSION["antispam_form_build_time"]) == false) {
				_antispam_log("post without requesting form");
				return true;
			} else {
				if (time() - $_SESSION["antispam_form_build_time"] < $antispam["min_delay"]) {
					_antispam_log("post too quickly");
					return true;
				}
			}
		}

		/* Check for forbidden user agents
		 */
		foreach ($antispam["forbidden_user_agents"] as $word) {
			if ($_SERVER["HTTP_USER_AGENT"] == $word) {
				_antispam_log("forbidden user agent");
				return true;
			}
		}

		/* Check for forbidden words
		 */
		foreach ($antispam["forbidden_words"] as $word) {
			if (stristr($message, $word) != false) {
				_antispam_log("forbidden word");
				return true;
			}
		}

		/* Check for maximum allowed number of links
		 */
		if (isset($antispam["max_links"])) {
			$link_count = max(substr_count($message, "[url"), substr_count($message, "http://"));
			if ($link_count > $antispam["max_links"]) {
				_antispam_log("+".$antispam["max_links"]." links");
				return true;
			}
		}

		/* Check for unreadable characters
		 */
		$letters = 0;
		$numbers = 0;
		$symbols = 0;
		$other   = 0;
		for ($i = 0; $i < strlen($message); $i++) {
			$char = $message[$i];
			if (($char >= "0") && ($char <= "9")) {
				$numbers++;
			} else  if (($char >= "A") && ($char <= "Z")) {
				$letters++;
			} else  if (($char >= "a") && ($char <= "z")) {
				$letters++;
			} else  if (strchr(" !@#$%^&*()_+-={}[]<>\|/;:,.'\"", $char) != false)  {
				$symbols++;
			} else {
				$other++;
			}
		}
		if ($other > ($letters + $numbers + $symbols)) {
			_antispam_log("unreadable message");
			return true;
		}

		return false;
	}

	/* Translate BB-codes to HTML tags
	 *
	 * INPUT:  string BB-code text
	 * OUTPUT: string HTML text
	 * ERROR:  -
	 */
	function translate_bbcodes($str) {
		foreach (config_file("bbcodes") as $line) {
			$line = str_replace("'", "\"", chop($line));
			list($bbcode, $begin, $end) = explode("|", $line, 3);
			$bbcode_len = strlen($bbcode) + 2;
			do {
				$changed = false;
				$link = false;
				if (($open = strpos($str, "[".$bbcode."]")) === false) {
					$open = strpos($str, "[".$bbcode."=");
				}
				$open_end = strpos($str, "]", $open);

				if (($open !== false) && ($open_end !== false)) {
					$new_begin = $begin;
					if ($open + $bbcode_len < $open_end) {
						$param = substr($str, $open + $bbcode_len, $open_end - $open - $bbcode_len);
						$param = str_replace("\"", "%22", $param);
						$new_begin = str_replace("%param%", $param, $new_begin);
					}
					if ($end == "") {
						$str = substr($str, 0, $open).$new_begin.substr($str, $open_end + 1);
						$changed = true;
					} else if (($close = strpos($str, "[/".$bbcode."]", $open_end)) !== false) {
						$text = substr($str, $open_end + 1, $close - $open_end - 1);
						$text = str_replace("\"", "%22", $text);
						$new_begin = preg_replace("/\%param\%/", $text, $new_begin);

						$str = substr($str, 0, $close).$end.substr($str, $close + $bbcode_len + 1);
						$str = substr($str, 0, $open).$new_begin.substr($str, $open_end + 1);
						$changed = true;
					}
				}
			} while ($changed);
		}

		return $str;
	}

	/* Translate text smilies to smiley images
	 *
	 * INPUT:  string text
	 * OUTPUT: string text
	 * ERROR:  -
	 */
	function translate_smilies($str) {
		foreach (config_file("smilies") as $smiley) {
			$smiley = explode("\t", chop($smiley));
			$text = array_shift($smiley);
			$image = "<img src=\"/images/smilies/".array_pop($smiley)."\">";

			$text_len = strlen($text);
			if (substr($str, 0, $text_len + 1) == $text." ") {
				$str = $image.substr($str, $text_len);
			}
			$str = str_replace(" ".$text, " ".$image, $str);
		}

		return $str;
	}
?>
Return current item: Banshee PHP Framework