This document explains, via a simple example, how to configure gretlcli (CVS source as of 2013-01-31) to play nicely as a "slave" to PHP under the Apache web server. It was tested on a Fedora 17 system with PHP 5.4.10 and Apache/2.2.22. The example basically consists of two PHP files, supporting this functionality:
The user types a gretl script into a textarea.
This script is run by gretlcli in batch mode and the results are displayed.
Let's say that the set-up to call gretlcli is located in the directory /var/www/html/gretl. In our simple test case that directory contains these files:
-rw-r--r-- 1 allin allin 310 Jan 30 22:07 gretl1.php -rw-r--r-- 1 allin allin 607 Jan 30 22:13 gretl2.php -rw-r--r-- 1 allin allin 81 Jan 30 22:13 gretl.conf -rw-rw-r-- 1 allin apache 0 Jan 30 22:14 gretltmp.inp
Note that gretltmp.inp, which will hold a gretl script, is writable by the apache group.
The file gretl1.php, the starting point for the user, has this content:
<html> <head> <title>gretlcli test: start</title> </head> <body> <?php echo '<p>Hello from gretlcli</p>'; ?> <form action="gretl2.php" method="post"> <p>Your script:</p> <p><textarea rows="20" cols="60" name="code" wrap="physical"></textarea></p> <p><input type="submit" /></p> </form> </body> </html>
That is, it just invites the user to type a gretl script, and on submit it calls gretl2.php, which looks like this:
<html> <head> <title>gretlcli test: response</title> </head> <body> <?php $code = htmlspecialchars($_POST['code']); echo '<p>Here is the script you typed:</p>'; echo '<pre>'; echo $code; echo '</pre>'; $infile = '/var/www/html/gretl/gretltmp.inp'; file_put_contents($infile, $code); echo '<p>And here is the output:</p>'; putenv("GRETL_CONFIG_FILE=/var/www/html/gretl/gretl.conf"); $out = array(); exec("/usr/bin/gretlcli -b $infile 2>&1", $out); echo '<pre>'; echo ' '; foreach ($out as $line) { echo $line; echo ' '; } echo '</pre>'; ?> </body> </html>
Note the use of the environment variable GRETL_CONFIG_FILE. This is new in CVS. Its effect is to bypass the usual gretlcli start-up procedure, which references the $HOME environment variable. Instead the program is directed to read its configuration from a specified file, in this case /var/www/html/gretl/gretl.conf. Note that the lines starting with $infile and putenv will have to be adjusted if you are using a location other than /var/www/html/gretl. In addition the exec line will have to to be amended if gretlcli is not installed in /usr/bin.
The config file referenced above is quite simple:
# content of /var/www/html/gretl/gretl.conf gretldir = /usr/share/gretl/ userdir = /var/www/html/gretl/ no_dotdir = true
Here's what's going on:
The gretldir line enables gretlcli to find its datafiles and help files. The value shown is valid if gretl has been installed under the /usr tree; if your installation differs you will have to change the prefix.
The line specifying userdir tells gretl where any required temporary files should be written, and the last line tells gretl that we don't need a distinct hidden directory for user files. With these specifications, gretl should not try to write files anywhere other than in /var/www/html/gretl. In fact, it shouldn't write any files even to that location so long as the submitted script is "plain vanilla," without any calls to third-party programs. (This means not using the gretl commands "gnuplot" or "foreign", and not using the --x-12-arima option with the "arima" command.)
Allin Cottrell
Department of Economics
Wake Forest University
2013-01-31