Code

Updated command verifier
authorhickert <hickert@594d385d-05f5-0310-b6e9-bd551577e9d8>
Tue, 11 May 2010 13:27:16 +0000 (13:27 +0000)
committerhickert <hickert@594d385d-05f5-0310-b6e9-bd551577e9d8>
Tue, 11 May 2010 13:27:16 +0000 (13:27 +0000)
-Added code from php.net - a comment about large amount result.

git-svn-id: https://oss.gonicus.de/repositories/gosa/trunk@18371 594d385d-05f5-0310-b6e9-bd551577e9d8

gosa-core/plugins/addons/configViewer/class_commandVerifier.inc

index 92244ca9e24d879c517be67ca88c9620900f1138..c93fa33681650f938abdc0840d58cba56e91c446 100644 (file)
@@ -18,36 +18,55 @@ class commandVerifier
         $output= "";
 
         if(isset($_POST['execute'])){
-            $descriptorspec = array(0 => array("pipe", "w"),1 => array("pipe", "w"), 2 => array("pipe", "w"));
-            $process = proc_open($this->command, $descriptorspec, $pipes);
-            if (is_resource($process)) {
-                $send= stream_get_contents($pipes[0]);
-                $res = trim(htmlentities(stream_get_contents($pipes[1]),ENT_COMPAT,'UTF-8'));
-                $err = trim(htmlentities(stream_get_contents($pipes[2]),ENT_COMPAT,'UTF-8'));
-                fclose($pipes[0]);
-                fclose($pipes[1]);
-                fclose($pipes[2]);
-                $code = proc_close($process);
-
-                if(!empty($err)) $err = "<pre>{$err}</pre>";
-                if(!empty($res)) $res = "<pre>{$res}</pre>";
-
-                $output = "
-                    <table summary='"._("Results")."'>
-                    <tr><td><b>Result:</b></td><td>$res</td></tr>
-                    <tr><td><b>Error:</b></td><td>$err</td></tr>
-                    <tr><td><b>Return code:</b></td><td>$code</td></tr>
-                    </table>";
+
+
+            $descriptorSpec = array(0 => array("pipe", "r"),
+                    1 => array('pipe', 'w'),
+                    2 => array('pipe', 'w'));
+            $process = proc_open($this->command, $descriptorSpec, $pipes);
+            $txOff = 0; $txLen = strlen($stdin);
+
+            $stdout = ''; $stdoutDone = FALSE;
+            $stderr = ''; $stderrDone = FALSE;
+            stream_set_blocking($pipes[0], 0); // Make stdin/stdout/stderr non-blocking
+            stream_set_blocking($pipes[1], 0);
+            stream_set_blocking($pipes[2], 0);
+            if ($txLen == 0) fclose($pipes[0]);
+            while (TRUE) {
+                $rx = array(); // The program's stdout/stderr
+                if (!$stdoutDone) $rx[] = $pipes[1];
+                if (!$stderrDone) $rx[] = $pipes[2];
+                foreach ($rx as $r) {
+                    if ($r == $pipes[1]) {
+                        $stdout .= fread($pipes[1], 8192);
+                        if (feof($pipes[1])) { fclose($pipes[1]); $stdoutDone = TRUE; }
+                    } else if ($r == $pipes[2]) {
+                        $stderr .= fread($pipes[2], 8192);
+                        if (feof($pipes[2])) { fclose($pipes[2]); $stderrDone = TRUE; }
+                    }
+                }
+                if (!is_resource($process)) break;
+                if ($txOff >= $txLen && $stdoutDone && $stderrDone) break;
             }
+            $code = proc_close($process);
+
+            if(!empty($stdout)) $stdout = "<pre>{$stdout}</pre>";
+            if(!empty($stdout)) $stdout = "<pre>{$stdout}</pre>";
+            $output = "
+                <table summary='"._("Results")."'>
+                <tr><td><b>Result:</b></td><td>$stdout</td></tr>
+                <tr><td><b>Error:</b></td><td>$stdout</td></tr>
+                <tr><td><b>Return code:</b></td><td>$code</td></tr>
+                </table>";
         }
         $smarty->assign('value', htmlentities($this->command,ENT_COMPAT,'UTF-8'));
         $smarty->assign('output', $output);
         return($smarty->fetch(get_template_path('commandVerifier.tpl', 'TRUE')));
     }
 
-    function save_object()
-    {
-        if(isset($_POST['command'])) $this->command = get_post('command');
+function save_object()
+{
+    if(isset($_POST['command'])) $this->command = get_post('command');
     }
 
     function save()