Creating Applications with Mozilla-Chapter 12. Remote Applications-P2

Chia sẻ: Thanh Cong | Ngày: | Loại File: PDF | Số trang:14

lượt xem

Creating Applications with Mozilla-Chapter 12. Remote Applications-P2

Mô tả tài liệu
  Download Vui lòng tải xuống để xem tài liệu đầy đủ

Tham khảo tài liệu 'creating applications with mozilla-chapter 12. remote applications-p2', công nghệ thông tin, quản trị web phục vụ nhu cầu học tập, nghiên cứu và làm việc hiệu quả

Chủ đề:

Nội dung Text: Creating Applications with Mozilla-Chapter 12. Remote Applications-P2

  1. Chapter 12. Remote Applications-P2 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 (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.
  2. Example 12-4. Using PHP to generate the correct XUL MIME type 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:
  3. 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. 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 ( 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.
  4. 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{
  5. }; 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. 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( )
  6. print """Content-type: application/vnd.mozilla.xul+xml\n """ print ' ' % form['op1'].value print ' ' % form['op2'].value print ' ' % form['op3'].value print """ """
  7. 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 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 (
  8. 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
  9. $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); ?>
  10. print "\n"; print "\n"; } ?> The PHP header method is placed at the top of the file to properly format the output as XUL. The PHP MySQL APIs prepare the code for connection to 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 (see the section Section 3.6). Some XUL widgets are created specifically to display complex tabular data such as , and some widgets provide user selection such as , , and , 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
  11. 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 the users-preferred language, but Mozilla 1.0 does not read DTD files over the wire. To overcome this problem, use server-side page processing. For example, this code includes the DTD by using PHP:
  12. 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 means that the application has a digital signature, which verifies that a file or group of files was created by the person or organization from which you download and that they are trustworthy. In essence, if you trust the person or organization signing the files, then you trust the files themselves. Digital signatures originate from a certificate authority (CA), an organization that claims responsibility for any digital signature it creates. CAs act as gatekeepers by allowing only people who the organization trusts to create digital signatures. Large CAs like Verisign, whose certificates come preinstalled 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 Manage Certificates option. Of the different types of CAs -- there's a type for SSL connections, for example, and another one for S/MIME -- the Netscape Object Signing certificate is 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
  13. other Mozilla developers. The Section 12.6 section later in this chapter uses the MozDev CA to discuss both avenues.
Đồng bộ tài khoản