We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 8869
    • 32 Posts
    Hi

    The following code works fine as a stand alone php web page but doesn’t work as a modx snippet - Can someone point me in the direction of what I need to change to cope with the fact the code is being executed from within modx. The purpose of the code is to execute a shell script on the server and return its output to the browser in real time by using php’s output buffer constantly flushing to the browser

    <?php
    ob_start();
    $scriptFile = "/stutest.sh";
    
            $descriptorspec = array(
               0 => array("pipe", "r"), // stdin
               1 => array("pipe", "w"), // stdout
               2 => array("pipe", "w") // stderr
            );
    
            $process = proc_open(escapeshellarg($scriptFile), $descriptorspec, $pipes, NULL);
    
    
    while (!feof($pipes[1])) {
    
                // Read StdOut
            print_r(fread($pipes[1], 1024));
            ob_flush();
            flush();
    
            }
    
    fclose($pipes[1]);
    proc_close($process);
    ?>
      • 28042 ☆ A M B ☆
      • 24,524 Posts
      Snippets are run via the PHP eval() function. That may have something to do with it?
        Studying MODX in the desert - http://sottwell.com
        Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
        Join the Slack Community - http://modx.org
        • 22303 MODX Staff
        • 10,725 Posts
        Try removing the ob_ functions; MODx already uses an output buffer, so this is most likely causing conflicts. Or you can flush the MODx output buffer and then create your own... though I think that is probably unnecessary.
          • 8869
          • 32 Posts
          I removed the ob_ functions but get this error when it tries to generate the page:

          Warning: Cannot modify header information - headers already sent in /home/xxxxx/manager/includes/document.parser.class.inc.php on line 2620
          « MODx Parse Error »
          MODx encountered the following error while attempting to parse the requested resource:
          « PHP Parse Error »

          PHP error debug
          Error: Cannot modify header information - headers already sent
          Error type/ Nr.: Warning - 2
          File: /home/xxxxx/manager/includes/document.parser.class.inc.php
          Line: 469
          Line 469 source: header(’Content-Type: ’ . $type . ’; charset=’ . $this->config[’modx_charset’]);
            • 8869
            • 32 Posts
            I removed the flush() function and now the pages parses but it is not bringing back the output of the script as it happens - rather it just shows the whole thing when it has completed - anyone know what modx command to use in the place of flush() ?
              • 22303 MODX Staff
              • 10,725 Posts
              Quote from: en8scl at Jul 01, 2009, 06:52 PM

              I removed the flush() function and now the pages parses but it is not bringing back the output of the script as it happens - rather it just shows the whole thing when it has completed - anyone know what modx command to use in the place of flush() ?
              You could kill the existing output buffer and then do your flush()’ing; otherwise, none of the output is going to be rendered until the page is finished processing.
                • 8869
                • 32 Posts
                I tried putting the following at the top of my snippet:

                ob_get_contents();
                ob_end_flush();


                But this doesn’t seem to be closing down the modx buffer before opening mine

                I get an error message at the end of the results of my script which all returns to the browser in one go which says:

                Warning: Cannot modify header information - headers already sent in /hsphere/local/home/zenbed/thehub.jswebconsole.co.uk/manager/includes/document.parser.class.inc.php on line 2620
                « MODx Parse Error »
                MODx encountered the following error while attempting to parse the requested resource:
                « PHP Parse Error »

                PHP error debug
                Error: Cannot modify header information - headers already sent
                Error type/ Nr.: Warning - 2
                File: home/mydomain.co.uk/manager/includes/document.parser.class.inc.php
                Line: 469
                Line 469 source: header(’Content-Type: ’ . $type . ’; charset=’ . $this->config[’modx_charset’]);
                  • 22303 MODX Staff
                  • 10,725 Posts
                  You would want ob_end_clean() to just discard the modx buffer completely, followed by an exit() to end execution at that point... set any header()’s before doing this however, as once you start sending output to the browser, you can no longer call the header() function.