0 && preg_match('/[\+\-\*\/\^\,]/', $ch))) { break; } $lim++; } $z = substr($z, 0, $lim).')'.substr($z, $lim); $x = $a.','.$z; } return $x; } // Converts the text by the user to the Javascript functions. function makeJS($x) { $x = strtolower($x); $x = preg_replace('/ln\((.*?)\)/','log($1)',$x); $x = preg_replace('/rand(om)\((.*?)\)/','rand()',$x); $x = preg_replace('/acsc\((.*?)\)/','acos(1/($1))',$x); $x = preg_replace('/sech\((.*?)\)/','2/(exp($1)+exp(-1*($1)))',$x); $x = preg_replace('/acot\((.*?)\)/','atan(-1*($1))',$x); $x = preg_replace('/sec\((.*?)\)/','1/sin($1)',$x); $x = preg_replace('/cot\((.*?)\)/','1/tan($1)',$x); $x = preg_replace('/acoth\((.*?)\)/','log(($1+1)/($1-1))/2',$x); $x = preg_replace('/asech\((.*?)\)/','log(1/($1)+sqrt(1/(($1)*($1))-1))',$x); $x = preg_replace('/acsch\((.*?)\)/','log(1/($1)+sqrt(1/(($1)*($1))+1))',$x); $reg = '/(x|e|pi|\)|\}|\])(e|\d)/'; $x = polyJS($x); for ($l=0;$l<=strlen($x)-1;$l++) { $a = substr($x,$l,2); if (preg_match($reg,$a)) { $x = substr($x,0,$l+1)."*".substr($x,$l+1); } } $reg = '/(\d|e|pi|x|\)|\}|\])(\(|\{|\[|x|a|c|f|l|m|p|r|s|tan|pi)/'; for ($l=0;$l<=strlen($x)-1;$l++) { $a = substr($x,$l,2); if (preg_match($reg,$a)) { $x = substr($x,0,$l+1)."*".substr($x,$l+1); } } $x = preg_replace('/x(?!p)/', '$x', $x); return $x; } // The important actual calculations. Hopefully this is secure. function f($x, $fu) { eval("return \$return=" . $fu . ";"); return $return; } function validate($input) { $input = preg_replace("/[^a-z0-9()\*\/\\\^\+-\s]+/", "", $input); $input = preg_replace("/[fd]/","", $input); // Just in case, strip BAD php commands $input = preg_replace("/(exec|shell|file|passthru|escapeshellcmd|popen|pcntl_exec)/", "", $input); $input = preg_replace("/'/","",$input); $input = preg_replace('/"/','',$input); return $input; } function charAt($str, $pos){ return (substr($str, $pos, 1)) ? substr($str, $pos, 1) : -1; } // Get the user information and validate it. $fu = strip_tags(preg_replace('/plus/','+',$_GET['f'])); $fu = preg_replace('/\{\}/', '', $fu); $fuarray = split(';', $fu); // Create a hash of the information the user has entered AFTER it's been validated, and // check to see if this image has been done before. If it has, return the image, otherwise, redraw // the image and cache it. $image_path = "cache/".md5(implode("",$fuarray)); if (file_exists($image_path)) { // Read the cached file. readfile($image_path); } else { // Get User Defined Properties // Not sure yet if I want to do this by reading a config file (added security problems) or finding a way to output // an image when the parameters are called through $_POST $size = 270; $width = $size; $height = 270; $xmin = -10; $xmax = 10; $ymin = -10; $ymax = 10; // Start New Image creation $image = imagecreatetruecolor($width,$height); imageantialias($image, true); $white = imagecolorallocate($image, 255, 255, 255); $black = imagecolorallocate($image, 0, 0, 0); $red = imagecolorallocate($image, 255, 0, 0); $blue = imagecolorallocate($image, 0, 0, 255); $green = imagecolorallocate($image, 0, 255, 0); $grey = imagecolorallocate($image, 228, 226, 235); // White background imagefilledrectangle($image, 0, 0, $width, $height, $grey); // $axis imagefilledrectangle($image, 136, 15, 138, 255, $red); imagefilledrectangle($image, 17, 134, 257, 136, $red); // This section creates the labels for the $axis // Numbers on left side for ($i=0;$i<=16;$i++) { $num = -1*($i-8); if ($num<0) { $$x = 2; } else { $$x = 8; } $y = 15*$i+7; ImageString($image, 3, $$x, $y, $num, $black); } // Numbers across bottom for ($i=0;$i<=16;$i++) { $num = $i-8; If ($num >= 0){ $$x = 15*$i + 14; } else { $$x = 15*$i + 8; } $y = 255; ImageString($image, 3, $$x, $y, $num, $black); } // Gridlines // Vertical lines for ($i=15;$i<=$width-14;$i+=15) { imageline($image, $i+2, 15, $i+2, 255, $black); } // Horizontal lines for ($i=15;$i<=$width-14;$i+=15) { imageline($image, 17, $i, 257, $i, $black); } $color = $blue; // Draw the actual functions themselves for ($j=0; $j<=count($fuarray)-1; $j++) { $fu = validate($fuarray[$j]); $fu = makeJS($fu); for ($i=15;$i<=$width-14;$i++) { $newx = round($i/15, 2) - 9; $y = -f($newx,$fu); if (is_infinite($y)) { /*if ($y > 0) { $y = -1*$size; } else { $y = $size; }*/ continue; } $newj = round(($y+9)*15, 0); $oldx = round(($i-1)/15, 2) - 9; $y = -f($oldx,$fu); if (is_infinite($y)) { /*if ($y > 0) { $y = -1*($size-14); } else { $y = $size-14; }*/ continue; } $oldj = round(($y+9)*15, 0); if (($newj>=15)&&($oldj>=15)) { imageline($image, $i+1, $oldj, $i+3, $newj, $color); } } } imagegif($image); imagegif($image, $image_path); imagedestroy($image); } ?>