/home/wpollock1/public_html/PHP/prolog.php

 1: <?php
 2: /*
 3:   One problem with .php files is that the server sets a mime type
 4:   based on the file's extension.  This must be over-ridden with the
 5:   xhtml MIME type.  However not all browsers accept xthml (old IE),
 6:   so in this case it is better (?) to return an HTML 4.01 MIME type
 7:   (even though some of the XHTML page may not render in this case4).
 8:   And, if returning HTML then all self-closing tags must be replaced too.
 9: 
10:   This script from www.workingwith.me.uk/articles/scripting/mimetypes
11:   parses the HTTP_ACCEPT (and if needed, HTTP_USER_AGENT) headers to
12:   see if XHTML is accepted.  If so the proper file prolog is generated
13:   (xml, DOCTYPE, and head tags) and the correct MIME content type is
14:   set.  Else an HTML prolog and MIME type is generated.
15: 
16:   Posted by Neil Crosby on April 17, 2004 08:27 PM
17:   and modified by Wayne Pollock 6/2007 (according to the various
18:   follow-up postings.)
19: */
20: 
21: ob_start();  // Output buffering, or can't change headers later.
22: 
23: function unclose_tags ( $buffer ) // When generating HTML (vs. XHTML)
24: {
25:    return str_replace(" />", ">", $buffer);
26: }
27: 
28:   $mime = "text/html";
29:   $charset = "utf-8";
30:   # special check for the W3C_Validator
31:   if ( isset($_SERVER["HTTP_USER_AGENT"])
32:        && stristr($_SERVER["HTTP_USER_AGENT"],"W3C_Validator") )
33:   {
34:      $mime = "application/xhtml+xml";
35:   } elseif ( isset($_SERVER["HTTP_ACCEPT"])
36:              && stristr($_SERVER["HTTP_ACCEPT"],"application/xhtml+xml") )
37:   {
38:      if (preg_match("/application\/xhtml\+xml[a-z0-9,\s\/\*\-\+]*"
39:                     . ";[\s]?q=([0-1]{0,1}\.\d{0,4})/i",
40:                     $_SERVER["HTTP_ACCEPT"],$matches))
41:      {
42:         $xhtml_q = $matches[1];
43:         if ( preg_match("/text\/html[a-z0-9,\s\/\*\-\+]*"
44:              . ";[\s]?q=([0-1]{0,1}\.\d{0,4})/i",
45:              $_SERVER["HTTP_ACCEPT"],$matches) )
46:         {
47:            $html_q = $matches[1];
48:            if ( (float) $xhtml_q >= (float) $html_q )
49:              $mime = "application/xhtml+xml";
50:         }
51:      } else {
52:         $mime = "application/xhtml+xml";
53:      }
54:   }
55: 
56:   if ( $mime=="application/xhtml+xml" )
57: {
58:    $prolog = "<?xml version='1.0' encoding='$charset' ?>\n";
59:    $prolog .= "<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 "
60:             . "Strict//EN'\n";
61:    $prolog .= "          'http://www.w3.org/TR/xhtml11/DTD/"
62:             . "xhtml10-strict.dtd'>\n";
63:    $prolog .= "<html xmlns='http://www.w3.org/1999/xhtml' "
64:             . "xml:lang='en' lang='en'>\n";
65: } else {
66:    ob_start( "unclose_tags" );
67:    $prolog = "<!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 4.01//EN'\n";
68:    $prolog .= "      'http://www.w3.org/TR/html4/strict.dtd'>\n";
69:    $prolog .= "<html lang='en'>\n";
70: }
71: 
72: # finally, output the mime type and prolog type
73: header( "Content-Type: $mime;charset=$charset" );
74: header( "Vary: Accept" );
75: print $prolog;
76: ?>