<?php
/*  loopback.php by whygee (a) f-cpu.org
    created Tue Sep  5 01:19:24 EDT 2006
    version Sun Jan 13 20:09:08 CET 2008
    version 2009-05-31 : isset(), found a few bugs
    méchant hack pour envoyer des données par POST
     et les renvoyer sous forme de fichier à sauver
    version jeu. fÃ©vr. 16 09:34:19 CET 2012 : nouvelle version pour YHX
 */

// flush any buffer, and make uncacheable
@ob_end_clean();
@ini_set('zlib.output_compression', 'Off');
header('Pragma: public');
header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
header('Cache-Control: must-revalidate, pre-check=0, post-check=0, max-age=0');

// Answer if asked kindly :
if (isset($_POST['probe']) && ($_POST['probe']=='yakekin')) {
  header('Content-Type: text/html');
  echo 'signaturemagique';
}

// simple loopback, save as a file :
else {
  if (isset($_POST['filename'])&& isset($_POST['filecontents'])) {
    header('Content-Transfer-Encoding: none');
    header('Content-Type: application/saveme; name="'.$_POST['filename'].'"');
    header('Content-Disposition: attachment; filename="'.$_POST['filename'].'"');

    // renvoie directement le contenu du formulaire :

// Corrects the automatic escaping
    if ( get_magic_quotes_gpc () ) {
      if ( $sybase ) {
        $s = str_replace ('\'\'', '\'', $_POST['filecontents']);
      }
      else {
        $s = stripslashes ($_POST['filecontents']);
      }
    }
    else {
      $s = $_POST['filecontents'];
    }

// strip the &#XXX; if mode=HTML

    echo $s;
  }

// Another loopback that transforms the input file into a JS string
  else {
    if (isset($_FILES['file']) && ($_FILES['file']['error']===UPLOAD_ERR_OK)) {
      header('Content-Type: text/html');
      echo "<html><script language=\"JavaScript\">\nwindow.parent.load_result=\n\"";

      $handle = fopen($_FILES['file']['tmp_name'], "rb");
      if ($handle) {
        $size=0;
        $l='';
        while (!feof($handle)) {

// Scan the file, char by char, and translate/escape each byte for a JS-compliant string
          $b = fgetc($handle);
          switch ($b) {
            case "\n" : $l.='\n';    $size=$size+2; break;
            case "\r" : $l.='\r';    $size=$size+2; break;
            case "\v" : $l.='\v';    $size=$size+2; break;
            case "\f" : $l.='\f';    $size=$size+2; break;
            case "\t" : $l.='\t';    $size=$size+2; break;
            case '\\' : $l.='\x5C';  $size=$size+4; break;
            case '\'' : $l.='\x27';  $size=$size+4; break;
            case '"'  : $l.='\"';    $size=$size+2; break;
            case '<'  : $l.='\x3C';  $size=$size+4; break; // break the HTML tags to prevent clash, particularly with "</script>" !
            default:
              $c=ord($b);
              // My PHP version chokes on "if (($c < 32) || ($c > 126)) {"
              // so i tried to split it, and it works :-/
              if ($c < 32) {
                $l.='\x'.sprintf("%02X",$c);
                $size=$size+4;
                break;
              }
              if ($c > 126) {
                $l.='\x'.sprintf("%02X",$c);
                $size=$size+4;
                break;
              }
              $l.=$b;
              $size=$size+1;
          }

          if (!feof($handle) && ($size>50)) {
            echo $l."\"+\n\"";
            $l='';
            $size=0;
          }
        } // end while(!feof)
      }

      fclose($handle);
      echo $l."\";\nwindow.parent.load_wait_timeout=-1;\n</script><body>OK</body></html>";
    }

// None of the above, hence something is wrong.
    else {
      header('Content-Type: text/html');
      echo "<html><script language=\"JavaScript\">\nwindow.parent.load_result='';\n";
      echo "window.parent.load_wait_timeout=-2;\n</script><body>error</body></html>";
    }
  }
}
