Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2013-06-14 03:37:34

skewray
Member
From: Sunny Southern California
Registered: 2013-04-25
Posts: 182
Website

Where does POST data go?

I have some online utilities that are compiled. They generate a form that is filled out, submitted back, computation ensues, and then the form is re-issued with the results. I managed to integrate this into Textpattern using GET:

<form method="GET" action='/tools/<txp:article_url_title />' >
  <txp:php>
    passthru( "/cgi-bin/tools" ) ;
  </txp:php>
</form>

When I try this with POST, it doesn’t work. The program never gets the input. I can’t use GET for some tools because the GET string would be too long. Any idea what in Textpattern is eating the POST data, and if I can get it back?

Offline

#2 2013-06-14 11:33:35

Gocom
Developer Emeritus
From: Helsinki, Finland
Registered: 2006-07-14
Posts: 4,533
Website

Re: Where does POST data go?

skewray wrote:

Any idea what in Textpattern is eating the POST data, and if I can get it back?

Textpattern doesn’t eat POST data. More precisely, Textpattern doesn’t modify global server data, it uses its own aliases. But that’s beside the point, since you use an external script, and nothing that Textpattern does, affects it. The script likely gets its input from the raw request or web server (handling itself), where PHP doesn’t have access to.

Textpattern also supports HTTP POST for any GET interface (not that its necessarily a good thing), so that you will be able to get to those pages even when using POST. E.g.

$ curl --data '' http://www.skewray.com/tools/survey-size-calculator

Returns the right page just fine. The only thing you need to be vary about are collisions. The requests can not contain any query parameters reserved by Textpattern. If the request contains parameters used by Textpattern, it will act according to those values. These parameters contain names such as id, s, c, pg, m, month, context, q etc. E.g.

$ curl --data 'id=1337' http://www.skewray.com/tools/survey-size-calculator

Returns 404 page.

If the issue is that the article page doesn’t show up after sending the form, then there might be an collision. If the page does load, then there likely is no issue on Textpattern’s end, but it could be that the tool doesn’t support HTTP POST.

Last edited by Gocom (2013-06-14 11:34:05)

Offline

#3 2013-06-19 03:49:44

skewray
Member
From: Sunny Southern California
Registered: 2013-04-25
Posts: 182
Website

Re: Where does POST data go?

I am not sure I understand. I just wrote a program that prints out the POST and GET results, as well as a Textpattern webpage that calls it four ways: POST and GET directly and POST and GET through Textpattern. As far as I can tell, Textpattern is eating the POST data. When called directly, the POST and GET data pass directly, and the form inputs are printed in plain text. When called through Textpattern, the GET passes through but the POST does not. Give it a try.

Offline

#4 2013-06-19 09:10:00

Gocom
Developer Emeritus
From: Helsinki, Finland
Registered: 2006-07-14
Posts: 4,533
Website

Re: Where does POST data go?

skewray wrote:

Textpattern is eating the POST data

How are you trying to access that HTTP POST data in Textpattern? Textpattern isn’t supposed to care whether you request pages using HTTP POST or GET request method, nor modify the request directly.

If I put <txp:php> echo ps('text'); </txp:php> to a Textpattern template, it outputs it just fine. When I send it a POST request.

Offline

#5 2013-06-19 14:12:24

skewray
Member
From: Sunny Southern California
Registered: 2013-04-25
Posts: 182
Website

Re: Where does POST data go?

I am reading it from standard input. Does php have some other way to access POST data?

Offline

#6 2013-06-19 20:04:08

Gocom
Developer Emeritus
From: Helsinki, Finland
Registered: 2006-07-14
Posts: 4,533
Website

Re: Where does POST data go?

skewray wrote:

I am reading it from standard input.

In an external script? If you are simply running scripts using passthru(), it doesn’t do more than executes a command and echoes the output. It doesn’t pass data along, it runs a command.

Does php have some other way to access POST data?

It being designed for web server use, yes. There is APIs for handling HTTP related data. Textpattern also offers an additional wrapper for PHP too.

Keep in mind too that anything you return through Textpattern needs to be properly sanitized. Otherwise you will run into some big security issues.

Last edited by Gocom (2013-06-19 20:08:35)

Offline

#7 2013-06-19 23:26:09

skewray
Member
From: Sunny Southern California
Registered: 2013-04-25
Posts: 182
Website

Re: Where does POST data go?

I see now that php reads the POST data and puts it into the $_POST variable. That is why it is gone by the time my program runs. I think I have to use proc_open() rather than passthru() in order to have access to standard input, and then pipe $_POST to it.

Offline

#8 2013-06-20 16:09:07

skewray
Member
From: Sunny Southern California
Registered: 2013-04-25
Posts: 182
Website

Re: Where does POST data go?

The following works:

        $desc = array(
            0 => array("pipe", "r"),
            1 => array("pipe", "w"),
            2 => array("pipe", "w")
            ) ;
        $post = file_get_contents( 'php://input' ) ;
        $command = '.../cgi-bin/tooltest' ;
        $process = proc_open( $command, $desc, $pipes ) ;
        if( is_resource($process) ) {
            fwrite( $pipes[0], $post ) ;
            fclose( $pipes[0] ) ;
            echo stream_get_contents( $pipes[1] ) ;
            fclose( $pipes[1] ) ;
            fclose( $pipes[2] );
            $return_value = proc_close( $process ) ;
            echo "command returned $return_value\n" ;
            }

I was amazing at how many variations of this I found online that failed.

Last edited by skewray (2013-09-14 18:21:38)

Offline

Board footer

Powered by FluxBB