Advertisement
Evengard

Гвнкд

Mar 13th, 2012
240
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.52 KB | None | 0 0
  1. <?php
  2. @ini_set('zlib.output_compression',0);
  3.  
  4. @ini_set('implicit_flush',1);
  5.  
  6. @ob_end_clean();
  7.  
  8. set_time_limit(0);
  9. if($_GET["pass"]!="***")
  10.         die("Auth error");
  11. //session_start();
  12. //$_SESSION["auth"]="ok";
  13.  
  14. $temp_fifo_file = '/tmp/dolphin-pipe-'.uniqid('dolph');
  15. if (!posix_mkfifo($temp_fifo_file, 0600)) {
  16.     echo "Fatal error: Cannot create fifo file: something wrong with the system.\n";
  17.     exit(1);
  18. }
  19. if (!posix_mkfifo($temp_fifo_file."-ctrl", 0600)) {
  20.     echo "Fatal error: Cannot create fifo file: something wrong with the system.\n";
  21.     exit(1);
  22. }
  23.  
  24. //die("ok2");
  25. function deleteTempFifo() {
  26.         unlink($GLOBALS['temp_fifo_file']);
  27.         popen("/bin/rm -rf ".$GLOBALS['temp_fifo_file'], "r");
  28.         unlink($GLOBALS['temp_fifo_file']."-ctrl");
  29.         popen("/bin/rm -rf ".$GLOBALS['temp_fifo_file']."-ctrl", "r");
  30. }
  31. register_shutdown_function('deleteTempFifo');
  32.  
  33. $cmdfp = fopen($temp_fifo_file, 'r+');
  34. $cmdfpctrl = fopen($temp_fifo_file."-ctrl", 'r+');
  35. stream_set_blocking($cmdfp, 0);
  36. stream_set_blocking($cmdfpctrl, 0);
  37. putenv('TERM=vt100');
  38. $cols = 80;
  39. $rows = 24;
  40.  
  41. chdir(dirname(__FILE__));
  42. $cmd = "bash --rcfile ./bashrc -i 2>&1";
  43. // try to compile pseudo-terminal emulator if we can
  44. /*if (!file_exists('pt')) {
  45.     system('cc -D__'.strtoupper(trim(`uname`)).'__ -o pt pt.c -lutil 2>&1', $retval);
  46.     if ($retval) echo('<b>Warning:</b> Cannot compile pseudotty helper');
  47. }*/
  48. clearstatcache();
  49. if (file_exists('pt')) $cmd = "./pt $rows $cols $cmd";
  50. $pp = proc_open($cmd, array(array('pipe','r'), array('pipe', 'w')), $pipes);
  51. stream_set_blocking($pipes[0], 0);
  52. stream_set_blocking($pipes[1], 0);
  53. ?>
  54. <html><head><title>Terminal</title></head><body>
  55. <script>
  56.     var pipeName = <?=json_encode($temp_fifo_file)?>, pending_str = '', processing = false, timeout=0, before=new Date();
  57.     var sendCmdInterv = setInterval(function() {
  58.         var now = new Date();
  59.         var elapsedTime = (now.getTime() - before.getTime());
  60.         timeout=timeout+elapsedTime;
  61.         //console.log(timeout);
  62.         if (processing) return;
  63.         if (pending_str.length) {
  64.             timeout=0;
  65.             processing = true;
  66.             var previous_str = pending_str;
  67.             pending_str = '';
  68.             var http = new XMLHttpRequest();
  69.             http.open("GET", "send-cmd.php?pass=<?=$_GET["pass"] ?>&pipe=" + pipeName + "&cmd=" + encodeURIComponent(previous_str), true);
  70.             http.onreadystatechange = function() {
  71.                 if (http.readyState == 4 && http.status == 200) {
  72.                     processing = false;
  73.                     pending_str = '';
  74.                 } else {
  75.                     pending_str = previous_str + pending_str;
  76.                 }
  77.             };
  78.             http.send(null);
  79.         }
  80.         if(timeout>30*1000)
  81.         {
  82.                 timeout=0;
  83.                  processing = true;
  84.                  var http = new XMLHttpRequest();
  85.             http.open("GET", "send-cmd.php?pass=<?=$_GET["pass"] ?>&ctrl=ok&pipe=" + pipeName + "&cmd=0", true);
  86.          http.onreadystatechange = function() {
  87.                     processing = false;
  88.             };
  89.             http.send(null);
  90.         }
  91.         before=new Date();
  92.     }, 16);
  93.  
  94.     function send_cmd(val) {
  95.         pending_str += val;
  96.     }
  97. </script>
  98. <style>
  99.     .term {
  100.         font-family: monaco,courier,fixed,monospace,swiss,sans-serif;
  101.         font-size: 13px;
  102.         line-height: 16px;
  103.         color: #f0f0f0;
  104.         background: #000000;
  105.     }
  106.  
  107.     tr {
  108.         height: 16px;
  109.     }
  110.  
  111.     .termReverse {
  112.         color: #000000;
  113.         background: #00ff00;
  114.     }
  115. </style>
  116. <script src="utils.js"></script>
  117. <script src="term.js"></script>
  118. <script>var term = new Term(<?=$cols?>, <?=$rows?>, send_cmd); term.open();</script>
  119. <?php
  120. echo "<!-- ".str_repeat('-', 104096)." -->\n";
  121. //flush();
  122. ob_implicit_flush(1);
  123. echo str_repeat(' ',1024*64);
  124. sleep(1);
  125. $waited=0;
  126. while (!feof($pipes[1])) {
  127.         $ln = fgets($pipes[1], 4096);
  128.         if ($ln !== false) {
  129.         $ln = str_replace("\n", "\r\n", $ln);
  130.                 echo '<script>term.write('.json_encode($ln).');</script>';
  131.         //flush();
  132.         echo str_repeat(' ',1024*64);
  133.         $waited=0;
  134. //      sleep(0.1);
  135.                 continue;
  136.         }
  137.  
  138.     $inp_ln = fgets($cmdfp, 4096);
  139.     if ($inp_ln !== false) {
  140.         // ensure that command is fully written by setting blocking to 1
  141.         stream_set_blocking($pipes[0], 1);
  142.         fwrite($pipes[0], $inp_ln);
  143.         stream_set_blocking($pipes[0], 0);
  144.     }
  145.         $ctrl_ln = fgets($cmdfpctrl, 4096);
  146.         if ($ctrl_ln !== false)
  147.         {
  148.                 for($i=0; $i<strlen($ctrl_ln); $i++)
  149.                 {
  150.                         switch($ctrl_ln[$i])
  151.                         {
  152.                                 case '0':
  153.                                         $waited=0;
  154.                                         echo str_repeat(' ',1024*64);
  155.                                         //die("oke");
  156.                                         break;
  157.                                 default:
  158.                                         break;
  159.                         }
  160.                 }
  161.         }
  162.         $waited=$waited+20000;
  163.         if($waited>50000000)
  164.         {
  165.                 //echo str_repeat(' ',1024*64);
  166.                 //$waited=0;
  167.                 die("<script>clearInterval(sendCmdInterv);</script>
  168. </body>
  169. </html>");
  170.         }
  171.         usleep(20000);
  172. }
  173. proc_close($pp);
  174. ?>
  175. <script>clearInterval(sendCmdInterv);</script>
  176. </body>
  177. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement