<?
/* MixedEval.php (c) 2001 John Dickinson (john@techwise.com.au)
*
* TERMS OF USAGE:
* This file was written and developed by John Dickinson (john@techwise.com.au)
* for educational and demonstration purposes only. You are granted the
* rights to use, modify, and redistribute this file as you like. The only
* requirement is that you must retain this notice, without modifications, at
* the top of your source code. No warranties or guarantees are expressed or
* implied. DO NOT use this code in a production environment without
* understanding the limitations and weaknesses pretaining to or caused by the
* use of these scripts, directly or indirectly. USE AT YOUR OWN RISK!
*
* If you are looking for someone to code in php, perl, asp, java,
* javascript (etc) feel free to email or visit our website at
* www.techwise.com.au
*
* If you improve upon this script, please send me a copy so i can make it
* available
* If you find any bugs, let me know and I will try to find time to fix them
*/
class MixedEval {
function MixedEval ($Code) {
$this->Code = stripslashes($Code);
$this->setCodeArray();
}
function setCodeArray() {
$token = ">:<:>:<"; //make sure it is rare
$phptoken = "<:>:<:>";
if(eregi('<\?=', $this->Code, $regs)) {
$this->delimitererror = "<b>PHParse error: <font color=red> tag \"<?=\" is not supported </font>";
echo "<br></b>";
}
$this->Code = ereg_replace("<\?php", "<?", $this->Code);
$this->Code = ereg_replace("<\?", "<?$phptoken ", $this->Code);
$this->Code = ereg_replace("<\?", $token, $this->Code);
$this->Code = ereg_replace("\?>", $token, $this->Code);
$CodeArray = explode($token, $this->Code);
for($i=0; $i<sizeof($CodeArray); $i++) {
if(eregi("^$phptoken", $CodeArray[$i])) {
$this->CodeArray[$i][TYPE] = "php";
$CodeText = substr($CodeArray[$i], strlen($phptoken));
} else {
$this->CodeArray[$i][TYPE] = "html";
$CodeText = $CodeArray[$i];
}
$this->CodeArray[$i][TEXT] = $CodeText;
}
}
function formatError($offender) {
eval($offender);
if($this->delimitererror)
echo $this->delimitererror;
else
echo "<b>check the line number of the following evaled of code:</b><br>";
echo "<hr>";
$offender = ereg_replace("\n", "\n", $offender);
$offender = ereg_replace("\t", ' \t', $offender);
$offender = ereg_replace(" ", ' ', $offender);
echo '<b><font color=green>0. <?';
$offenderlines = split("\n", $offender);
for($j=0; $j<sizeof($offenderlines); $j++) {
$jNext = $j + 1;
echo '<br>'.$jNext.". ";
echo $offenderlines[$j];
}
$StrangeJ_ThingNext = $StrangeJ_ThingNext + 1;
echo '<br>'.$StrangeJ_ThingNext.'. ?></font></b>';
echo "<hr>";
}
function show() {
//Need a strange incrementor name to prevent code interference, something no one will ever use.
for($StrangeI_Thing=0; $StrangeI_Thing<sizeof($this->CodeArray); $StrangeI_Thing++) {
if($this->CodeArray[$StrangeI_Thing]['TYPE'] == "php") {
@eval($this->CodeArray[$StrangeI_Thing]['TEXT']." \$eval_ok = true; ");
if(!$eval_ok) {
$offender = $this->CodeArray[$StrangeI_Thing]['TEXT'];
$this->formatError($offender);
}
$eval_ok = false;
} else {
echo $this->CodeArray[$StrangeI_Thing]['TEXT'];
}
}
}
}
/*
//Examples of usage
$str1 = 'this is a string';
$str2 = 'this is a string also';
$mixedcode = "<a href='blah'>a html link</a>
<font color=red><br>
<? echo 'php code'; ?><br>
<?php echo \"more php code\";?>
<?=$str2?>
</font><Br>
<a href='blah'>another html link</a><br>
... and this is just some text
";
$myPHP = new MixedEval($mixedcode);
$myPHP->Show();
*/
?>
|