Tài liệu Creating Applications with Mozilla-Chapter 12. Remote Applications-P2 pptx

14 278 0
Tài liệu Creating Applications with Mozilla-Chapter 12. Remote Applications-P2 pptx

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

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 /> </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( ) print """Content-type: application/vnd.mozilla.xul+xml\n <?xml version=\"1.0\"?> <!DOCTYPE window> <window id = "remote" xmlns = "http://www.mozilla.org/keymaster/gatekeeper/there. is.only.xul" title = "listbox" style = "min-width:282px; min-height:137px;" orient = "vertical"> <box>""" print ' <checkbox label="%s" />' % form['op1'].value print ' <checkbox label="%s" />' % form['op2'].value print ' <checkbox label="%s" />' % form['op3'].value print """</box> </window>""" In this example, the CGI module is loaded with the Python import statement, and the form object is initialized so that data input to the page in the form of URL ?name=value pairs create the XUL content dynamically. Example 12-6 takes a URL, such as http://www.brownhen.com/cgi- bin/xulgen?opt1=peter?opt2=paul?opt3=mary, and displays a checkbox for each value of the named form fields. The content type is printed before the XUL content to tell the web server what to pass when the script produces its output. 12.4.2. Generating Content from a Database One of the important facets of dynamically generated content is interaction with a database to store and retrieve values for the user. For example, a public forum or subscription-based service could have a client that is written in XUL and requires authentication of users. Having such a client could require some form of database lookup. This section covers a simple example that uses data stored in a database to generate a XUL tree. We will use PHP to retrieve the data from the SQL-driven database and format it into XUL. Theoretically, this database could be any relational model, such as Oracle or MySQL. Example 12-7 generates a simple tree listing with the columns "User" and "Project." The SQL script creates a table called "sample" and then inserts value pairs into the table. To use this type content generation, your database must be set up to handle table creation and dynamic updating via SQL calls. Example 12-7. SQL script with User and Project data # table to be inserted into database CREATE TABLE sample ( User char(16) NOT NULL default '', Project char(32) NOT NULL default '' ); INSERT INTO sample VALUES ('Bob','moz_bob'); INSERT INTO sample VALUES ('Joe','skinner'); INSERT INTO sample VALUES ('Bret','bretzilla'); INSERT INTO sample VALUES ('Sally','mozstream'); The code in Example 12-7 creates a table with two fields, "User" and "Project." It then inserts four records into the newly created table in the database by using INSERT INTO calls. Now the script has tangible data to query. Example 12-8 shows the creation of a simple XUL tree with PHP and a MySQL database. The "User" and "Project" columns display the data in the table. Example 12-8. XUL generated from database <?php header( "Content-type: application/vnd.mozilla.xul+xml" ); ?> <?xml version="1.0"?> <?php // connect code $host = "127.0.0.1"; $user = "nobody"; $database = "test"; $password = "mypass"; $connect = mysql_connect($host, $user, $password); mysql_select_db($database); $query = "SELECT * FROM sample ORDER BY User"; $result = mysql_query($query, $connect); $e = mysql_error( ); if($e) print "ERROR:projects: $e"; $row = mysql_num_rows($result); ?> <!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;"> <image /> <hbox> <tree> <treecols> <treecol id="userCol" label="User" /> <treecol id="projectCol" label="Project" flex="1"/> </treecols> <treechildren> <?php // generate data from db for($i=0;$i<$row;$i++) { $user = mysql_result($result, $i, "User"); $project = mysql_result($result, $i, "Project"); print "<treeitem container=\"true\" open=\"true\">\n"; print "<treerow>\n"; print "<treecell label=\"".ucwords($user)."\" />"; print "<treecell label=\"".ucwords($project)."\" flex=\"1\"/>"; [...]... , plus the CSS used for controlling presentation All of these widgets can be generated on the fly from the database and used to accept user-inputted information 12.4 .3 Localizing Remote Applications Unfortunately, localizing remote applications is a not as straightforward since there is no http: protocol equivalent for chrome:-included locales You could use HTTP/1.1 content negotiation to serve... multiple files exist for multiple languages in a client distribution A remote application would require the same process, but in a different format 12.5 Certificate Authorities and Digital Signatures Instead of changing the Universal XPConnect privileges (see Section 12.3 earlier in this chapter), you could create signed remote applications that can be granted access to users' computers A signed application... what matters for signed applications Fortunately, to get your remote applications signed by a CA, you don't have to pay for a Verisign Netscape Object Signing CA because other options are available You can use the MozDev CA, for example, and even create your own The next section tells you how use Mozilla tools to become your own certificate authority so you can sign your own applications and those of... in many web browsers, enforce validity through large fees For example, if you can afford $600, then you are an organization with whom the CA would be glad to associate That $600 then also buys your application respectability with user's web browsers You can see the CAs that come with the Mozilla browser by going to Privacy & Security > Certificates in your preferences panel and then by selecting the... a MySQL database Finally, a loop is used to print out the stored data's treerows and treecells This kind of operation provides insight into the possibilities of using remote XUL when you have information stored in a database Many web applications already do this via HTML forms and other mechanisms, but Mozilla's XPFE toolkit provides a richer widget set than HTML to display and retrieve information... and even create your own The next section tells you how use Mozilla tools to become your own certificate authority so you can sign your own applications and those of other Mozilla developers The Section 12.6 section later in this chapter uses the MozDev CA to discuss both avenues . Chapter 12. Remote Applications- P2 12. 4.1.1. PHP When users of your application may not have configured. used to accept user-inputted information. 12. 4.3. Localizing Remote Applications Unfortunately, localizing remote applications is a not as straightforward

Ngày đăng: 21/01/2014, 06:20

Tài liệu cùng người dùng

Tài liệu liên quan