%%generic-spellcheck-anonymous-html%%
$Date: 2003/01/20 06:13:02 $

%%preprocess%%
$text= $_POST['text'];

// if text+check is supplied, first open and create $temptext, then spell check
if (trim($text)!="" && ($_POST['submit']=="check" || $_POST['submit']=="re-check")) {
	// set up some vars and create a tempfile
	$temptext= tempnam("/tmp", "spelltext");
	$aspellcommand= "cat $temptext | /usr/local/bin/aspell -a";
	$opener= "<font color='red'>";
	$indicator= "red";
	$closer= "</font>";

	if ($fd=fopen($temptext,"w")) {
		$textarray= explode("\n",$text);
		fwrite($fd,"!\n");
		foreach($textarray as $key=>$value) {
			// adding the carat to each line prevents the use of aspell commands within the text...
			fwrite($fd,"^$value\n");
			}
		fclose($fd);

		// next run aspell
		$return= shell_exec($aspellcommand);

		//next parse $return and $text line by line, eh?
		$returnarray= explode("\n",$return);
		$returnlines= count($returnarray);
		$textlines= count($textarray);

		//print "text has $textlines lines and return has $returnlines lines.";
		$lineindex= -1;
		$poscorrect= 0;
		$counter= 0;
		foreach($returnarray as $key=>$value) {
			// if there is a correction here, processes it, else move the $textarray pointer to the next line
			if (substr($value,0,1)=="&") {
				//print "Line $lineindex correction:".$value."<br>";
				$correction= explode(" ",$value);
				$word= $correction[1];
				$absposition= substr($correction[3],0,-1)-1;
				$position= $absposition+$poscorrect;
				$niceposition= $lineindex.",".$absposition;
				$suggstart= strpos($value,":")+2;
				$suggestions= substr($value,$suggstart);
				$suggestionarray= explode(", ",$suggestions);
				//print "I found <b>$word</b> at $position. Will suggest $suggestions.<br>";

				// highlight in text
				$beforeword= substr($textarray[$lineindex],0,$position);
				$afterword= substr($textarray[$lineindex],$position+strlen($word));
				$textarray[$lineindex]= $beforeword.$opener.$word.$closer.$afterword;

				// kludge for multiple words in one line ("<b></b>" adds 7 chars to subsequent positions)
				$poscorrect= $poscorrect + ( strlen($opener) + strlen($closer));

				// build the correction form
				$counter= $counter+1;
				$formbody.= "<tr>
						<td align='right'>$word</td>
						<td>
						<input type='hidden' name='position$counter' value='$niceposition'>
						<input type='hidden' name='incorrect$counter' value=\"$word\">
						<select name='suggest$counter' onChange=\"document.corrector.correct$counter.value=this.value;\">
						<option value=\"$word\" selected>$word (as-is)</option>
						";
				foreach ($suggestionarray as $key=>$value) {
					$formbody.= "<option value=\"$value\">$value</option>
							";
					}
				$inputlen= strlen($word)+5;
				$formbody.= "<option value=''>custom:</option>
						</select>
						<input type='text' name='correct$counter' value=\"$word\" size='$inputlen'>
						</td>
					</tr>";
				}
			elseif (substr($value,0,1)=="#") {
				//print "Line $lineindex unknown:".$value."<br>";
				$correction= explode(" ",$value);
				$word= $correction[1];
				$absposition= $correction[2];
				$position= $absposition+$poscorrect;
				$niceposition= $lineindex.",".$absposition;
				$suggestions= "no suggestions";
				$suggestionarray= explode(", ",$suggestions);
				//print "I found <b>$word</b> at $position. Will suggest $suggestions.<br>";

				// highlight in text
				$beforeword= substr($textarray[$lineindex],0,$position-1);
				$afterword= substr($textarray[$lineindex],$position+strlen($word)-1);
				$textarray[$lineindex]= $beforeword.$opener.$word.$closer.$afterword;

				// kludge for multiple words in one line ("<b></b>" adds 7 chars to subsequent positions)
				$poscorrect= $poscorrect + (strlen($opener) + strlen($closer) );

				// build the correction form
				$counter= $counter+1;
				$formbody.= "<tr>
						<td align='right'>$word</td>
						<td>
						<input type='hidden' name='position$counter' value='$niceposition'>
						<input type='hidden' name='incorrect$counter' value=\"$word\">
						<select name='suggest$counter' onChange=\"document.corrector.correct$counter.value=this.value;\">
						<option value=\"$word\" selected>$word (as-is)</option>
						";
				$inputlen= strlen($word)+3;
				$formbody.= "<option value=''>custom:</option>
						</select>
						<input type='text' name='correct$counter' value=\"$word\" size='$inputlen'>
						</td>
					</tr>";
				}
			else {
				//print "Done with line $lineindex, next line...<br><br>";
				$poscorrect=0;
				$lineindex= $lineindex+1;
				}
			}
		}
	print "<h3>Spellcheck Results:</h3> Potential errors are shown in $opener$indicator$closer.  <a href='#correction'>Jump to Correction Form</a><blockquote>";
	foreach ($textarray as $key=>$value) {
		print $value."<br>";
		}
	print "</font><!-- comment catcher --></blockquote>";

	$htmltext= htmlentities($text);
	if ($formbody=="") $formbody= "<tr><td>&nbsp;</td><td><br><b>No errors!</b><br>Click 'correct' to continue with text unchanged.<br>&nbsp;</td></tr>";
	print "<br><a name='correction'><br><h3>Correction form:</h3>
		The errors marked above can be changed using this form. Click the &quot;correct&quot; button when you are ready.<br><br>&nbsp;
		<form name='corrector' action='' method='post'>
		<input type='hidden' name='text' value=\"$htmltext\">
		<input type='hidden' name='myform' value='$_POST[myform]'>
		<input type='hidden' name='myfield' value='$_POST[myfield]'>
		<table>
		$formbody
		<tr>
		<td>&nbsp;</td>
		<td><input type='submit' name='submit' value='correct'>
			<input type='reset' name='reset' value='reset form'>
		</td>
		</tr>
		</table>
		</form>";

	//print "<hr>Return:".nl2br($return);
	
	// unlink the tempfile
	unlink($temptext);
	}

// or if text+correct is specified, make the indicated corrections
elseif (trim($text)!="" && $_POST['submit']=="correct") {
	$textarray= explode("\n",$text);

	$index= 1;
	$lastlineindex= 0;
	$poscorrect= 0;

	// look through list of positions and make corrections
	while (isset($_POST["position$index"])) {
		$positionarray= explode(",",$_POST["position$index"]);
		$lineindex= $positionarray[0];
		$absposition= $positionarray[1];

		if ($lastlineindex==$lineindex) {
			$position= $absposition+$poscorrect;
			}
		else {
			$poscorrect= 0;
			$position= $absposition;
			}
		$lastlineindex= $lineindex;
		$correct= $_POST["correct$index"];
		$incorrect= $_POST["incorrect$index"];
		//print "Found correction at $lineindex,$absposition. Replacing ";

		$before= substr($textarray[$lineindex],0,$position);
		$after= substr($textarray[$lineindex],$position+strlen($incorrect));
		$textarray[$lineindex]= $before.$correct.$after;

		$poscorrect= (strlen($correct)-strlen($incorrect))+$poscorrect;
		//print "Position correction is now $poscorrect.<br>";
		$index= $index+1;
		}

	//print "Original text:<br>";
	//print nl2br($text);
	//print "<hr>";

	foreach ($textarray as $key=>$value) {
		$newtext.=$value;
		}
	print "
	<form action='' method='post' name='spellcheck'>
		<h3>Your Corrected Text:</h3><br>
		<textarea name='text' cols='60' rows='10'>$newtext</textarea>
		<input type='hidden' name='myform' value='$_POST[myform]'>
		<input type='hidden' name='myfield' value='$_POST[myfield]'>
		<br>
		<button type='button' onClick='window.opener.document.$_POST[myform].$_POST[myfield].value= document.spellcheck.text.value; window.close();'>Paste</button> or <input type='submit' name='submit' value='re-check'>
	</form>";
	}

// otherwise, show the initial form
else {
	print "
		<form action='' method='post'>
			Text to Check:<br>
			<textarea name='text' cols='60' rows='10'>$newtext</textarea><br>
			<input type='submit' name='submit' value='check'>
			<input type='hidden' name='myform' value='$_POST[myform]'>
			<input type='hidden' name='myfield' value='$_POST[myfield]'>
		</form>";
	}

%%header%%


%%css%%


%%template%%
<br>
<br>&nbsp;


%%listrow%%


%%nullobject%%



%%footer%%


%%postprocess%%


%%end of context%%
