Jump to content

Problem with php script...


Padgoi

Recommended Posts

So I have this script that basically checks the spelling of all words in a post and then deducts points from a user's intelligence level when it sees a word spelled wrong.  However, I am having a few issues with it. 

 

1. It is breaking the BBCode on my forum.  It is stripping all html tags in order to check the spelling, but in the process, the tags aren't being posted, just the actual words inside the tag, so it looks like a regular part of the post and not the tag.

 

2. A few of my links are not working anymore, such as the link to see new posts in a topic and after a user votes in a poll, it redirects them to a blank page after they vote. 

 

Any help would be greatly appreciated.

 

Here's the script for the file causing the problems:

 

<?php

$_PREFIX = "***_";

if(isset($_INSTALL) && $_INSTALL == true){

	echo "<div style='color:#fff;background-color:#000; border:2px solid #ff0000;padding:50px; margin:50px; width:500px;'>PERFORMING INSTALLATION!<br/>";
	$error = false;

	$maketable = mysql_query("CREATE TABLE `spelling_bad_words` (`id` INT( 255 ) NOT NULL AUTO_INCREMENT PRIMARY KEY ,`word` VARCHAR( 255 ) NOT NULL ,`posts` TEXT NOT NULL) ENGINE = MYISAM"); 
	if(!$maketable) {echo mysql_error(); $error = true;} else echo "Table created.";

	$checkPhpspellcheck = mysql_query("SELECT * FROM `spelling_words`");
	if(!$checkPhpspellcheck) {echo "<br/>WARNING: Phpspellcheck not installed.<br/>"; $error = true;} else echo "<br/>Phpspellcheck found.<br/>";

	$addSmartness = mysql_query("ALTER TABLE `".$_PREFIX."members` ADD `smartpoints` int(255) NOT NULL default '100'");
	if(!$addSmartness) {echo mysql_error(); $error = true;} else echo "Smartness added.";

	$addPrevious = mysql_query("ALTER TABLE `".$_PREFIX."members` ADD `previous_group` int(255) NOT NULL default '0'");
	if(!$addPrevious) {echo "<br/>".mysql_error(); $error = true;} else echo "<br/>Previous group added.";

	$addChange = mysql_query("ALTER TABLE `".$_PREFIX."members` ADD `group_change_time` int(255) NOT NULL default '0'");
	if(!$addChange) {echo "<br/>".mysql_error(); $error = true;} else echo "<br/>Change time added.";

	$addErrors = mysql_query("ALTER TABLE `".$_PREFIX."posts` ADD `points` int(255) NOT NULL default '2'");
	if(!$addErrors) {echo "<br/>".mysql_error(); $error = true;} else echo "<br/>Points added.";

	$addTime = mysql_query("ALTER TABLE `".$_PREFIX."posts` ADD `last_check` int(255) NOT NULL default '0'");
	if(!$addTime) {echo "<br/>".mysql_error(); $error = true;} else echo "<br/>Time added.";

	if(!$maketable && $checkPhpspellcheck && !$addSmartness && !$addErrors && !$addTime && !$addPrevious && !$addChange) $error = false;
	if($error) echo "<hr>Please REFRESH THE PAGE."; else {
	echo "<hr>INSTALLATION LOOKS SUCCESSFUL.<br/>Processing all posts.<hr>";
	//SmartnessCheckAllPosts();

	}

	echo "</div>";
	die();

}

function SmartnessCheckAllPosts(){
	$_PREFIX = "***_";

	$postSQL = mysql_query("SELECT pid, author_id, post, points FROM `".$_PREFIX."posts` ORDER BY `pid` ASC");
	while($row = mysql_fetch_array($postSQL)){
		echo "Processing: ".$row['pid'].": ".$row['author_id']." (".$row['points'].")";
		$post = SmartnessCheckPost($row);
		echo $post;
		echo "<br/>";
	}
	echo "<hr>DONE!<br/>Successfully installed.";
}

function SmartnessCheckPost($row, $checkit = false){
	$_PREFIX = "***_";
	$_ADMINGROUPS = array(1,4); // array of group IDs that are allowed to add words to dictionary
	$_DATE = 1191569315;		// timestamp for posts to be checked (should be left alone, I'll set it for you)
	$_DAYSTOBEMOVED = 2;		// number of days a member will be moved to a certain group for
	$_GROUPTOBEMOVED = 22;		// the group a member will be moved to for having <= 0 smartness

	$row = mysql_fetch_array(mysql_query("SELECT * FROM `".$_PREFIX."posts` WHERE `pid` = ".$row['pid']." LIMIT 1"));

	if($row['points'] == 2 && $row['post_date'] > $_DATE){
	// if post is new,
	// add 1 to author points
	mysql_query("UPDATE `".$_PREFIX."posts` SET `points` = 1 WHERE `pid` = ".$row['pid']." LIMIT 1") or die(mysql_error());
	$m = mysql_fetch_array(mysql_query("SELECT * FROM `".$_PREFIX."members` WHERE `id` = ".$row['author_id']." LIMIT 1"));
	mysql_query("UPDATE `".$_PREFIX."members` SET `smartpoints` = ".($m['smartpoints']+1)." WHERE `id` = ".$row['author_id']." LIMIT 1") or die(mysql_error());		

	$checkit = true;
	}
	$row['points'] = 1;

	if (($checkit == true || $row['edit_time'] >= $row['last_check']) && $row['post_date'] > $_DATE) {
		/* Re-spell check this post */
		/* First convert &#38;#38;#38;#39;s back to apostrophes, and such */
		$row['post'] = str_replace("&#38;#38;#38;#039;", "'", $row['post']);
		$row['post'] = str_replace("&#38;#38;#38;#39;", "'", $row['post']);			

		/* Give the author all their points back */
		$points = 1 - $row['points'];
		mysql_query("UPDATE `".$_PREFIX."posts` SET `points` = 1 WHERE `pid` = ".$row['pid']." LIMIT 1") or die(mysql_error());
		$m = mysql_fetch_array(mysql_query("SELECT * FROM `".$_PREFIX."members` WHERE `id` = ".$row['author_id']." LIMIT 1"));
		mysql_query("UPDATE `".$_PREFIX."members` SET `smartpoints` = ".($m['smartpoints']+$points)." WHERE `id` = ".$row['author_id']." LIMIT 1") or die(mysql_error());	
		$row['points'] = 1;

		/* remove all previous spelling thingies */
		$row['post'] = str_replace("<sp>", "", $row['post']);
		$row['post'] = str_replace("</sp>", "", $row['post']);

		/* get all the words in the post */
  		$split = preg_split("/[^a-zA-Z\']/", $row['post'], -1, PREG_SPLIT_OFFSET_CAPTURE);
  		foreach($split as $word){
  			$words[$word[1]] = $word[0];
  		}
  		
  		/* get rid of all the words that are inside HTML tags,
  			while still keeping each word's offset (i.e. would
  			be simpler to use strip_tags() but it removes offset...
  			hard to explain what I want but I got it working.)	 */
  		preg_match_all("/[<>]/", $row['post'], $matched, PREG_OFFSET_CAPTURE);
  		$matched = $matched[0];
  		$opened = array();
  		foreach($matched as $tag){
  			if($tag[0] == "<"){
  				$opened[] = $tag[1];
  			} else {
  				if(count($opened) > 0){
  					foreach ($words as $off => $word){
  						if($off > $opened[count($opened)-1] && $off < $tag[1]){
  							unset($words[$off]);
  						}
  						if($word == "" || $word == " "){
  							unset($words[$off]);
  						}
  					}					
  					array_pop($opened);
  				}
  			}
  		}
  		
  		/* Now we are left with an array, $words
  			which contains each word and its offset
  			i.e. $words[offset] = word				*/
  			
  		/* Spell check each word! */
		$i = 0;
  		foreach($words as $off => $word){
  			$check = SmartnessCheckWord($word, $row['pid']);
  			if(!$check){ //i.e. if it returns false (word is WRONG)
  				$row['points'] = $row['points'] - 1;
  				// replace word
  				$words[$off] = "<sp>".$word."</sp>";
  				$row['post'] = substr_replace($row['post'], $words[$off], $off+(9*$i), strlen($word));
				$i++;
  			}
  		}

		mysql_query("UPDATE `".$_PREFIX."posts` SET `post`='".str_replace("'", "&#38;#38;#38;#039;", $row['post'])."',`points`=".$row['points'].",`last_check`=".time()." WHERE `pid`=".$row['pid']." LIMIT 1") or die(mysql_error());
        $points = 1 - $row['points'];
		$m = mysql_fetch_array(mysql_query("SELECT * FROM `".$_PREFIX."members` WHERE `id` = ".$row['author_id']." LIMIT 1"));
		mysql_query("UPDATE `".$_PREFIX."members` SET `smartpoints` = ".($m['smartpoints']-$points)." WHERE `id` = ".$row['author_id']." LIMIT 1") or die(mysql_error());

		/* GET member information
			to check if they should be moved (less than or equal to 0 smartness) */
		$member = mysql_fetch_array(mysql_query("SELECT * FROM `".$_PREFIX."members` WHERE `id` = ".$row['author_id']." LIMIT 1"));
		if($member['smartpoints'] <= 0){
			// they are dumb, move them to a specific group, reset smartness to 100
			// have not tested this so no idea if it works
			mysql_query("UPDATE `".$_PREFIX."members` SET `mgroup`=".$_GROUPTOBEMOVED.",`previous_group`=".$member['mgroup'].",`group_change_time`=".(time()+($_DAYSTOBEMOVED*24*3600)).",`smartpoints`=50 WHERE `id`=".$row['author_id']." LIMIT 1") or die(mysql_error());
		} 

	}

	// replace every instance of <sp> etc with <a href=></a> etc
	// check if user is an administrator
    $sel = mysql_query("SELECT * FROM `".$_PREFIX."members` WHERE `id`=".$_COOKIE['member_id']." LIMIT 1");
    $user = mysql_fetch_array($sel);
    $_ISADMIN = false;
    foreach($_ADMINGROUPS as $group){
    	if($user['mgroup'] == $group){
    		$_ISADMIN = true;
    	}
    }
	if($_ISADMIN){
		$row['post'] = preg_replace("/(<sp>)((.)+?)(<\/sp>)/i", "<a href='".$_SERVER['REQUEST_URI']."&addWord=$2' class='spellingError'>$2</a>", $row['post']);
	} else {
		$row['post'] = str_replace("<sp>", "<span class='spellingError'>", $row['post']);
		$row['post'] = str_replace("</sp>", "</span>", $row['post']);
	}

	// style for the box 
	if($row['points'] <= 0){
		$row['post'] = '<div style="float: right; padding: 3px; margin: 5px; font-weight: bolder; background-color: #f00; border: 2px solid #000"><a href="/smartness.php" style="text-decoration: none; color: #fff">-'.abs($row['points']).'</a></div>' . $row['post'];
	}

	return $row['post'];
}

function SmartnessCheckWord($word, $pid){
	$checkit = mysql_fetch_array(mysql_query("SELECT * FROM `spelling_words` WHERE `word`=\"".strtolower($word)."\""));
	if($checkit || $word == "" || $word == " "){
		return true;
	} else {
		$badwords = mysql_fetch_array(mysql_query("SELECT * FROM `spelling_bad_words` WHERE `word`=\"".strtolower($word)."\""));
		if($badwords){
			mysql_query("UPDATE `spelling_bad_words` SET `posts` = \"".$badwords['posts'].",".$pid."\" WHERE `id`=".$badwords['id']." LIMIT 1") or die(mysql_error());
		} else {
			mysql_query("INSERT INTO `spelling_bad_words` (`word`, `posts`) VALUES (\"".strtolower($word)."\", \"0,$pid\")") or die(mysql_error());
		}

		return false;
	}
}

function SmartnessUnmoveAllMembers(){
	$_PREFIX = "***_";
	/* look at all the members that have been moved,
		and if they've been moved for longer than they should have,
		move them back */
	$query = mysql_query("SELECT * FROM `".$_PREFIX."members` WHERE `group_change_time` <= ".time()." AND `group_change_time` > 0");
	while($m = mysql_fetch_array($query)){
		mysql_query("UPDATE `".$_PREFIX."members` SET `group_change_time`=0,`mgroup`=".$m['previous_group']." WHERE `id`=".$m['id']." LIMIT 1") or die(mysql_error());
	} 
}

function SmartnessAddWord($word){
	$_PREFIX = "***_";
	$_ADMINGROUPS = array(1,4); // array of group IDs that are allowed to add words to dictionary

	// check if user is an administrator
    $sel = mysql_query("SELECT * FROM `".$_PREFIX."members` WHERE `id`=".$_COOKIE['member_id']." LIMIT 1");
    $user = mysql_fetch_array($sel);
    $_ISADMIN = false;
    foreach($_ADMINGROUPS as $group){
    	if($user['mgroup'] == $group){
    		$_ISADMIN = true;
    	}
    }

	if($_ISADMIN){
		$query = mysql_fetch_array(mysql_query("SELECT * FROM `spelling_bad_words` WHERE `word`=\"".strtolower($word)."\" LIMIT 1"));
		if($query){
			$listOfPosts = explode(",", $query['posts']);
			$listOfPosts = array_unique($listOfPosts);
			mysql_query("INSERT INTO `spelling_words` (`word`) VALUES (\"".strtolower($word)."\")") or die(mysql_error());
			mysql_query("DELETE FROM `spelling_bad_words` WHERE `word`=\"".strtolower($word)."\" LIMIT 1") or die(mysql_error());
			foreach ($listOfPosts as $pid){
				if($pid > 0){
  					$row = mysql_fetch_array(mysql_query("SELECT * FROM `".$_PREFIX."posts` WHERE `pid`=".$pid)); 
  					SmartnessCheckPost($row, true);
				}
			}
		}
	}
}
?> 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.