<?
$font 
4;
$xfont 1;
$W 80;
$H 25;
$rw 4;
$rh 5;
$colors = array(
    
"bg" => array(255255255),
    
"text" => array(000),
    
"nbg" => array(646464),
    
"ntext" => array(255255255),
    
"flows" => array(
        array(
255153255), array(255153204),
        array(
255153153), array(255204153),
        array(
255255153), array(204255153),
        array(
153255153), array(153255204),
        array(
153255255), array(153204255),
        array(
153153255), array(204153255)
    )
);

$DIR = array(array(10), array(01), array(-10), array(0, -1));
$color_cur 1;
$page $vpage = array();

function 
error($msg) {
    
$im imagecreate(strlen($msg) * imagefontwidth(5), imagefontheight(5));
    
$w imagecolorallocate($im255255255);
    
$b imagecolorallocate($im000);
    
imagefill($im00$w);
    
imagestring($im500$msg$b);
    
imagepng($im);
    
imagedestroy($im);
    exit;
}

function 
bef_parse($contents$bf93 true) {
    global 
$WW$HH$page$W$H;
    
    
$max_W 0;
    
    
$contents str_replace("\r""\n"str_replace("\r\n""\n"$contents));
    foreach(
explode("\n"$contents) as $_) {
        if(
$bf93 && strlen($_) > $W) { error(".error: line is too long."); }
        if(
strlen($_) > $max_W$max_W strlen($_);
        
$page[] = $_;
        if(
$bf93 && count($page) > $H) { error(".error: Too many lines."); }
    }
    
    if(
$bf93) {
        list(
$WW$HH) = array($W$H);
        
$W $max_W;
        
$H count($page);
    } else {
        
$WW $W $max_W;
        
$HH $H count($page);
    }
}

function 
rec() {
    global 
$DIR$color_cur$page$vpage$WW$HH;
    
$stack = array(array(000000));
    
    while(
count($stack)) {
        list(
$x$y$dir$out$str$newcolor) = array_pop($stack);
        
            if(
$newcolor$color_cur++;
            if(
$out) {
                if(
$vpage[$y][$x][($dir+2)&3]["o"][$str]) continue;
                
$vpage[$y][$x][($dir+2)&3]["o"][$str] = $color_cur;
                
$x += $DIR[$dir][0] + $WW$x %= $WW;
                
$y += $DIR[$dir][1] + $HH$y %= $HH;
            }
            if(
$vpage[$y][$x][$dir]["i"][$str]) continue;
            
$vpage[$y][$x][$dir]["i"][$str] = $color_cur;
            
            if(!(
$c $page[$y]{$x})) $c " ";
            if(
$str == 1) {
                
array_push($stack, array($x$y$dir1$c != '"'0));
            } elseif(
$str == 2) {
                
array_push($stack, array($x$y$dir100));
            } elseif(
$c == "#") {
                
$X $x $DIR[$dir][0] + $WW$X %= $WW;
                
$Y $y $DIR[$dir][1] + $HH$Y %= $HH;
                
array_push($stack, array($X$Y$dir000));
            } elseif(
strpos(">v<^"$c) !== false) {
                
$dir strpos(">v<^"$c);
                
array_push($stack, array($x$y$dir100));
            } elseif(
$c == "|") {
                
array_push($stack, array($x$y3101));
                
array_push($stack, array($x$y1101));
            } elseif(
$c == "_") {
                
array_push($stack, array($x$y2101));
                
array_push($stack, array($x$y0101));
            } elseif(
$c == "?") {
                
array_push($stack, array($x$y3101));
                
array_push($stack, array($x$y2101));
                
array_push($stack, array($x$y1101));
                
array_push($stack, array($x$y0101));
            } elseif(
$c == '"') {
                
array_push($stack, array($x$y$dir110));
            } elseif(
$bf93) {
            if(
$c != "@") {
                    
array_push($stack, array($x$y$dir100));
            }
        } else {
            if(
strpos("ABCDEFGHIJKLMNOPQRSTUVWYXZ()t"$c) !== false) {
                    
$X $x $DIR[($dir+2)&3][0] + $WW$X %= $WW;
                    
$Y $y $DIR[($dir+2)&3][1] + $HH$Y %= $HH;
                
array_push($stack, array($X$Y, ($dir+2)&3001));
                
array_push($stack, array($x$y$dir100));
            } elseif(
$c == ";") {
                
$X $x$Y $y;
                do {
                    
$X += $DIR[$dir][0] + $WW$X %= $WW;
                    
$Y += $DIR[$dir][1] + $HH$Y %= $HH;
                } while(
$page[$Y]{$X} != ";");
                
$X += $DIR[$dir][0] + $WW$X %= $WW;
                
$Y += $DIR[$dir][1] + $HH$Y %= $HH;
                    
array_push($stack, array($X$Y$dir000));
            } elseif(
$c == "[") {
                
array_push($stack, array($x$y, ($dir+3)&3100));
            } elseif(
$c == "]") {
                
array_push($stack, array($x$y, ($dir+1)&3100));
            } elseif(
$c == "w") {
                
array_push($stack, array($x$y, ($dir+1)&3101));
                
array_push($stack, array($x$y, ($dir+3)&3101));
                
array_push($stack, array($x$y$dir100));
            } elseif(
$c == "'") {
                
array_push($stack, array($x$y$dir120));
            } elseif(
$c != "@" && $c != "q") {
                    
array_push($stack, array($x$y$dir100));
            }
        }
        }
}

function 
vis_draw($number false) {
    global 
$colors$font$xfont$im$w$h$rw$rh$W$H;
    global 
$vpage$page;
    
    
$msg "Generated by .tokinized online Befunge visualizer 0.71";
    
    
$w imagefontwidth($font);
    
$h imagefontheight($font);
    
$xw $number imagefontwidth($xfont) * strlen($H-1) + 0;
    
$xh $number imagefontwidth($xfont) * strlen($W-1) + 0;
    
$xxw 0$xxh imagefontheight($xfont) + 2;
    if(
$W*$w+$xw+$xxw imagefontwidth($xfont)*strlen($msg)+4$xxh 0;
    
$im imagecreate($W*$w+$xw+$xxw$H*$h+$xh+$xxh);
    
    
$color_bg imagecolorallocate($im$colors["bg"][0], $colors["bg"][1], $colors["bg"][2]);
    
$color_text imagecolorallocate($im$colors["text"][0], $colors["text"][1], $colors["text"][2]);
    
$color_nbg imagecolorallocate($im$colors["nbg"][0], $colors["nbg"][1], $colors["nbg"][2]);
    
$color_ntext imagecolorallocate($im$colors["ntext"][0], $colors["ntext"][1], $colors["ntext"][2]);
    foreach(
$colors["flows"] as $k => $v)
        
$color[$k] = imagecolorallocate($im$v[0], $v[1], $v[2]);
    
    
imagefill($im00$color_bg);
    if(
$xxh) {
        
imagestring($im$xfont$W*$w+$xw+$xxw-2-strlen($msg)*imagefontwidth($xfont), 1$msg$color_nbg);
    }
    if(
$number) {
        
imagefilledrectangle($im$xxw$xxh$W*$w+$xw+$xxw-1$xh+$xxh-1$color_nbg);
        
imagefilledrectangle($im$xxw$xxh$xw+$xxw-1$H*$h+$xh+$xxh-1$color_nbg);
        for(
$i=0;$i<$H;$i++)
            
imagestring($im$xfont2+$xxw$xh+$i*$h+$xxh+4sprintf("%".strlen($H-1)."d",$i), $color_ntext);
        for(
$i=0;$i<$W;$i++)
            
imagestringup($im$xfont$xw+$xxw+$i*$w$xh+$xxh-3$i$color_ntext);
    }
    
    for(
$y=0;$y<=$H;$y++) { for($x=0;$x<=$W;$x++) { for($dir=0;$dir<4;$dir++) {
        if(
$vpage[$y][$x][$dir]["o"][0]) $c $vpage[$y][$x][$dir]["o"][0];
        elseif(
$vpage[$y][$x][$dir]["o"][1]) $c $vpage[$y][$x][$dir]["o"][1];
        elseif(
$vpage[$y][$x][$dir]["i"][0]) $c $vpage[$y][$x][$dir]["i"][0];
        elseif(
$vpage[$y][$x][$dir]["i"][1]) $c $vpage[$y][$x][$dir]["i"][1];
        else 
$c false;
        
        if(
$c !== false) {
            
imagefilledrectangle($im,
                
$xxw $xw $w $x + ($dir==$w/$rw),
                
$xxh $xh $h $y + ($dir==$h/$rh),
                
$xxw $xw $w $x + ($rw - ($dir==1)) * $w $rw,
                
$xxh $xh $h $y + ($rh - ($dir==1)) * $h $rh,
                
$color[$c count($color)]);
        }
    } } }
    
    for(
$y=0;$y<=$H;$y++) { for($x=0;$x<=$W;$x++) {
        if(
$page[$y]{$x} != " ")
            
imagechar($im$font,
                
$xxw $xw $x $w$xxh $xh $y $h,
                
$page[$y]{$x}, $color_text);
    } }
}

function 
vis_out() {
    global 
$im;
    
imagepng($im);
    
imagedestroy($im);
}

if(
$_GET["source"] == 42) { echo "<meta http-equiv=\"content-type\" content=\"text/html; charset=utf-8\" />\n\n"show_source(__FILE__); exit; }

header("Content-type: image/png");
$baseuri "http://page.tokigun.net/visbef/";
if(!isset(
$_GET['path']))
if(
substr($_SERVER["HTTP_REFERER"], 0strlen($baseuri)) != $baseurierror(".error: referer check was failed. (your referer is: ".($_SERVER["HTTP_REFERER"] ? "$_SERVER[HTTP_REFERER])" "manual adress typing.. :p)"));
if(isset(
$_GET['path']) && preg_match('/^[0-9a-zA-Z.]+$/'$_GET['path']))
    
$contents file_get_contents($_GET['path']);
else
    
$contents base64_decode($_GET["contents"]);
if(
$contents) { bef_parse($contents$_GET["bf93"]=="yeah!"); rec(); vis_draw($_GET["number"]==42); vis_out($_GET["number"]==42); }
else { 
error(".error: no code was inputed."); }
?>