Advertisement
hjdr4

bug.php

Dec 12th, 2011
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.59 KB | None | 0 0
  1. <?php
  2. define("PROGRAM","php dummy.php");
  3.  
  4. class Forwarder
  5. {
  6.     var $program;
  7.    
  8.     public function __construct()
  9.     {      
  10.         echo stream_set_blocking(STDIN,1)?"STDIN is blocking\n":"STDIN is not blocking\n";
  11.     }  
  12.    
  13.     public function start()
  14.     {
  15.         $this->startPingPongProgram();
  16.        
  17.         while(true)
  18.         {
  19.             $readList=array($this->program->stdout,STDIN);
  20.             $writeList=null;
  21.             $errorList=null;
  22.             $tv=null;          
  23.                
  24.             $selected=stream_select($readList,$writeList,$errorList,$tv);
  25.             if($selected===false)
  26.             {
  27.                 echo "Error in stream_select()\n";
  28.             }
  29.             else
  30.             {
  31.                 echo "We have $selected descriptor(s) ready : \n".print_r($readList,true);             
  32.             }
  33.            
  34.             foreach($readList as $readable)
  35.             {
  36.                 if($readable==STDIN)
  37.                 {                  
  38.                     $line=fgets(STDIN);                
  39.                     if($line===false)
  40.                     {
  41.                         echo "line is false\n";
  42.                         $this->program->dispose();
  43.                         exit();                
  44.                     }                          
  45.                     $this->sendAllData($line);                                                         
  46.                 }
  47.                 else
  48.                 {
  49.                     echo "We got data on $readable : ".fread($readable,4096)."\n";             
  50.                 }
  51.             }          
  52.         }
  53.     }
  54.    
  55.     protected function sendAllData($data)
  56.     {
  57.         $remaining=strlen($data);
  58.                    
  59.         while($remaining!=0)
  60.         {          
  61.             $written=fwrite($this->program->stdin,$data);      
  62.             $data=substr($data,$written,$remaining);       
  63.             $remaining=strlen($data);  
  64.         }  
  65.     }
  66.    
  67.     protected function startPingPongProgram()
  68.     {
  69.         $program=new PingPongProgram(PROGRAM);     
  70.         $this->program=$program;
  71.     }
  72. }
  73.  
  74. class PingPongProgram
  75. {
  76.     var $stdin;
  77.     var $stdout;
  78.     var $stderr;   
  79.     var $proc;
  80.    
  81.     public function __construct($command)
  82.     {
  83.         $descriptorspec = array(
  84.            0 => array("pipe", "r"),  
  85.            1 => array("pipe", "w"),
  86.            2 => array("pipe", "w")
  87.         );
  88.        
  89.         $cwd=null;
  90.         $env=null;
  91.        
  92.         $process = proc_open($command, $descriptorspec, $pipes, $cwd, $env);       
  93.        
  94.         $this->proc=$process;
  95.         $this->stdin=$pipes[0];
  96.         $this->stdout=$pipes[1];
  97.         $this->stderr=$pipes[2];
  98.        
  99.         echo stream_set_blocking($this->stdin,1)?"Program STDIN is blocking\n":"Program STDIN is not blocking\n";
  100.         echo stream_set_blocking($this->stdout,1)?"Program STDOUT is blocking\n":"Program STDOUT is not blocking\n";
  101.         echo stream_set_blocking($this->stderr,1)?"Program STDERR is blocking\n":"Program STDERR is not blocking\n";       
  102.     }
  103.    
  104.     public function dispose()
  105.     {
  106.         if(fclose($this->stdin))
  107.             echo "Program's STDIN closed\n";
  108.        
  109.         if(fclose($this->stdout))
  110.             echo "Program's STDOUT closed\n";
  111.        
  112.         if(fclose($this->stderr))
  113.             echo "Program's STDERR closed\n";
  114.            
  115.         proc_close($this->proc);
  116.         echo "Program terminated\n";
  117.     }
  118. }
  119.  
  120. $fwd=new Forwarder();
  121. $fwd->start();
  122. ?>
  123.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement