Posted By

renatoelias on 09/22/09


Tagged

wrapper php cgi proxy workround


Versions (?)

PHP WRAPPER CGI


 / Published in: PHP
 

URL: http://renatoelias.art.br/blog/2009/09/22/php-wrapper-cgi-usando-phpwine-para-rodar-cgi/

This a simple wrapper for cgi, write in php, use stdin for post, and query_string env, for get

=)

  1. < ?
  2. // system('/Applications/Darwine/Wine.bundle/Contents/bin/wine cgi-proxy.exe');
  3.  
  4. function decode($to,$encode = false) {
  5. $arrayobject = new ArrayObject($to);
  6. $iterator = $arrayobject->getIterator();
  7. while($iterator->valid()){
  8. if ($encode)
  9. $keywords .= $iterator->key()."=".base64_encode($iterator->current())."&";
  10. else
  11. $keywords .= $iterator->key()."=".$iterator->current()."&";
  12.  
  13. $iterator->next();
  14. }
  15. return $keywords;
  16. }
  17.  
  18. $descriptorspec = array(
  19. 0 => array("pipe", "r"), // stdin is a pipe that the child will read from
  20. 1 => array("pipe", "w"), // stdout is a pipe that the child will write to
  21. 2 => array("file", "/Users/gards/KitWvbv200/componentes_vbv/error-output.txt", "a") // stderr is a file to write to
  22. );
  23.  
  24. $get = decode($_GET);
  25. $post = decode($_POST);
  26.  
  27. $cwd = '/Users/gards/KitWvbv200/componentes_vbv';
  28. $env = array('QUERY_STRING' => $get, 'REQUEST_METHOD' => 'GET');
  29. //
  30.  
  31. $process = proc_open('/Applications/Darwine/Wine.bundle/Contents/bin/wine cgi-proxy.exe', $descriptorspec, $pipes, $cwd, $env);
  32.  
  33. if (is_resource($process)) {
  34. // $pipes now looks like this:
  35. // 0 => writeable handle connected to child stdin
  36. // 1 => readable handle connected to child stdout
  37. // Any error output will be appended to /tmp/error-output.txt
  38.  
  39. // fwrite($pipes[0], $post);
  40. fclose($pipes[0]);
  41.  
  42. header('Content-Type:text/html;charset=ISO-8859-1');
  43. echo str_replace('Content-Type:text/html;charset=ISO-8859-1','',stream_get_contents($pipes[1]));
  44. fclose($pipes[1]);
  45.  
  46. // It is important that you close any pipes before calling
  47. // proc_close in order to avoid a deadlock
  48. $return_value = proc_close($process);
  49.  
  50. //echo "command returned $return_value\n";
  51. }
  52.  
  53. ?>

Report this snippet  

You need to login to post a comment.