Chapter 12. Remote Applications-P2
12.4.1.1. PHP
When users of your application may not have configured their browser to
support the correct MIME type, you can use the PHP header function to send
the browser the correct type.
Remember that when using XUL with PHP, you need to edit your php.ini
file on the server and change the default configuration to:
short_open_tag = Off
By default, this configuration is set to "On." When "Off," this setting tells
the PHP interpreter to parse only the files with escape tag identifiers, such as
<?php ?> (and not <? ?>, which are used by XML). This process is
separate from the XML parsing process that occurs when Mozilla receives
and tries to render the generated content. If you don't change the .ini file,
you will see an error like this:
Parse error: parse error in
/usr/local/share/doc/apache/remote_xul.php on line
2
This error occurs when PHP sees your XUL syntax as invalid PHP code.
Once PHP is properly configured to handle XUL files, you're ready to go.
To start out with something simple, use the code in Example 12-4 to produce
a simple XUL window with the xFly logo by using PHP. Be sure to save the
file with a .php extension.
Example 12-4. Using PHP to generate the correct XUL MIME type
<?php header( "Content-type:
application/vnd.mozilla.xul+xml" ); ?>
<?xml version="1.0"?>
<!DOCTYPE window>
<window
id = "remote"
xmlns =
"http://www.mozilla.org/keymaster/gatekeeper/there.
is.only.xul"
title = "A Remote Image"
style = "min-width:282px; min-height:137px;"
orient = "vertical">
<image
src="http://books.mozdev.org/screenshots/logo5.gif"
/>
</window>
Also remember that a space below the PHP tag results in a parse error in
Mozilla. The next example shows the PHP header and the XML declaration
at the start of the PHP file. The space between the two renders it invalid:
<?php header( "Content-type:
application/vnd.mozilla.xul+xml" ); ?>
<?xml version="1.0"?>
After PHP parses its content on the server, the rest of the document is sent to
Mozilla on the client. Put this file (remote_xul.php) somewhere in your
document root on the server with PHP installed and launch it from Mozilla
like this:
./mozilla -chrome http://my.domain/remote_xul.php
The window defined in Example 12-4 now appears, displaying the image.
You can take advantage of this relatively straightforward technique to serve
up more feature-rich user interfaces, inclusive of buttons, menus, and any
other pieces of XUL that are needed.
12.4.1.2. Perl
Although PHP is rising in popularity, Perl is still a very popular web-
scripting language. Perl is often used to drive back end web applications
with CGI scripts. To process Perl scripts, no extra configuration is needed
once you have the Perl interpreter set up to run in a web server environment.
If, for example, you're already using Perl to serve up dynamic HTML pages,
you're all set. Otherwise, you should grab a distribution of Perl
(http://perl.com/) and set up the paths to the binary files in your server
scripts. This procedure is done by placing the path in the header, otherwise
known as the shebang line. This script usually takes a form similar to
#!/usr/local/bin/perl or #!/usr/bin/perl. You must also
make sure that the server knows where the Perl executable is, which
involves including the path to it in the systems PATH environment variable.
Depending on the platform and the web server you use, other environments
may need to be set.
Example 12-5 shows a simple CGI script that generates a minimal amount of
XUL for display in the browser. Perl is useful for this task because you can
set up several possible scripts and call selected ones, depending on what
choices the user makes. You can even have different forks in the same script
that displays different widgets or data. For example, imagine that your
remote XUL application is an online travel planner. You would display
maps, information, and links to resources based on the user's geographic
location or preferences for destinations they entered earlier.
Example 12-5. A simple Perl-generated XUL file
#!/usr/bin/perl
print "Content-type:
application/vnd.mozilla.xul+xml";
print qq{
<?xml version="1.0"?>
<!DOCTYPE window&gt;
<window
id = "remote"
xmlns = "http://www.mozilla.org/keym
style = "min-width:282px; min-height:137px;"
orient = "vertical">
<image
src="http://books.mozdev.org/screenshots/logo5.gif"
/>
</window>
};
In Example 12-5, the MIME type must be specified as part of the first line in
the CGI script after the shebang line, and rest of the script is the XUL
code used by Mozilla. Although this example does not display real dynamic
content (such as the kind you get from CGI forms or other user-supplied
data), it shows how you can interpolate dynamic data into a simple CGI
application model.
12.4.1.3. Python
Like Perl, Python provides modules that make it easy to create CGI
applications that generate content dynamically. Python is already an
important language in the Mozilla environment because of the PyXPCOM
bindings discussed in Chapter 8.
If Python is a language that you like to code in, using it in a remote XUL
environment would also make sense. Python combines the features of a
lower-level language like object-oriented class and function design with the
flexibility and simplicity of a scripting language. The latter feature (ease of
use) is relative to a language like C++. Example 12-6 shows how to use
Python to create a simple form consisting of three checkboxes.
Example 12-6. A Python-generated dynamically updated form
#!/usr/local/bin/python
import cgi
form = cgi.FieldStorage( )