[pmwiki-devel] Calling CGI from PHP

9 views
Skip to first unread message

Simon

unread,
Sep 6, 2012, 4:09:11 AM9/6/12
to PmWiki Devel Mailing List
I've got a legacy html page which calls a cgi (perl) script <!--#include virtual="/cgi-bin/tuesday.pl" -->

As an interim step to replacing this script (with something clever from PmWiki) I've tried to invoke the script from within a recipe on a page.

The recipe is
Markup('ClubNight', 'fulltext', '/\\(:clubnight:\\)/ei',
  "Keep(ClubNight())");
function ClubNight() {
  $retval = 'retval is ';  
  $lastline = '';
  $output = '';
  // Create a stream
$opts = array(
  'http'=>array(
    'method'=>"GET",
    'header'=>"Accept-language: en\r\n" 
  )
);
  $context = stream_context_create($opts);
// Open the file using the HTTP headers set above
  $retval .= 'a' . file_get_contents('http://ttc.org.nz/cgi-bin/tuesday.pl', false, $context);
  $lastline = system ('http://ttc.org.nz/cgi-bin/tuesday.pl', $output);
  $retval .= 'b' . $output;
  $lastline = exec ('http://ttc.org.nz/cgi-bin/tuesday.pl', $output);
  $retval .= 'c' . $output;
  $lastline = exec("./cgi-bin/tuesday.pl", $output);
  $retval .= 'd' . $output;

  return $retval;
}

I don't get the result I expect on the page,
and wonder if anyone can spot anything obvious that I have got wrong.
I'm sure safe mode is on.

tia

Simon

Petko Yotov

unread,
Sep 7, 2012, 3:44:39 AM9/7/12
to PmWiki Devel Mailing List
Simon writes:
> As an interim step to replacing this script (with something clever from
> PmWiki) I've tried to invoke the script from within a recipe on
> <URL:http://ttc.org.nz/pmwiki/pmwiki.php/TTC/Tuesday>a page.

>
>
> The recipe is
>
> Markup('ClubNight', 'fulltext', '/\\(:clubnight:\\)/ei',
>   "Keep(ClubNight())");
> function ClubNight() {
>   $retval = 'retval is ';  
>   $lastline = '';
>   $output = '';
>   // Create a stream
> $opts = array(
>   'http'=>array(
>     'method'=>"GET",
>     'header'=>"Accept-language: en\r\n" 
>   )
> );
>   $context = stream_context_create($opts);
> // Open the file using the HTTP headers set above
>   $retval .= 'a' . file_get_contents('<URL:http://ttc.org.nz/cgi-
> bin/tuesday.pl>http://ttc.org.nz/cgi-bin/tuesday.pl', false, $context);

This should work unless some restrictions are set on that PHP installation.
You may also try simply

file_get_contents('http://ttc.org.nz/cgi-bin/tuesday.pl');


>   $lastline = system ('http://ttc.org.nz/cgi-bin/tuesday.pl', $output);

This would probably never work. You normally cannot execute remote files.

>   $retval .= 'b' . $output;
>   $lastline = exec ('http://ttc.org.nz/cgi-bin/tuesday.pl', $output);
>   $retval .= 'c' . $output;

Same here.

>   $lastline = exec("./cgi-bin/tuesday.pl", $output);

This will likely be "../cgi-bin/tuesday.pl" with 2 dots, because it looks
like your running pmwiki.php file is in a directory pmwiki, while the cgi-
bin directory looks like it is in the document root, and not inside the
pmwiki directory.

>   return $retval;

For testing purposes, in order to see the array values rather than 'Array',
try

return pre_r($retval);

Petko


_______________________________________________
pmwiki-devel mailing list
pmwiki...@pmichaud.com
http://www.pmichaud.com/mailman/listinfo/pmwiki-devel

Petko Yotov

unread,
Sep 7, 2012, 3:48:34 AM9/7/12
to PmWiki Devel Mailing List
Petko Yotov writes:
>>   $retval .= 'c' . $output;
>
> For testing purposes, in order to see the array values rather than 'Array',
> try
>
> return pre_r($retval);

This was incorrect, sorry. You can use the pre_r() function on all $output
variables above, not on $retval:

$retval .= 'c' . pre_r($output);

Simon

unread,
Sep 8, 2012, 12:53:29 AM9/8/12
to Petko Yotov, PmWiki Devel Mailing List
A big thanks to Petko and Peter B who got me heading in the right direction.
I've documented this at

I hope it helps someone else

Simon
Reply all
Reply to author
Forward
0 new messages