指向性メモ::2004-07-14::PHP5で行こう

ページ情報
制作日
2004-07-14T22:30:27+09:00
最終更新日
2004-07-14T22:30:27+09:00
ページ内目次

interceptor.phpをPHP5で書き直していく。メインのクラスになるHIMMELは大体できあがった。

class HIMMEL
{
    protected $id;
    protected $mode;
    protected $xhtml_version;
    protected $sdf_source;

    function HIMMEL($mode, $id, $sdf_source = NULL)
    {
        $this->id = is_file((ROOT == "/" ? "" : ROOT) . ($id == "/" ? "" : $id) . "/index.xml") ? $id : xmlError();
        $this->mode = $mode;
        $this->sdf_source = $sdf_source;

        /* Select XHTML version */
        $request_headers = apache_request_headers();
        $accept = explode(",", $request_headers["Accept"]);
        if (in_array("application/xhtml+xml", $accept)) {
            $this->xhtml_version = (double) 1.1;
        } else {
            $this->xhtml_version = (double) 1.0;
        }

        /* Apply S3 */
        $xhtml = $this->applyS3();

        /* HTTP Headers */
        $this->HTTPHeaders($xhtml);

        print $xhtml;
    }

    function applyS3()
    {
        /* Apply S3 */
        if ($this->mode == "intercept") {
            $result = $this->SmartXSLT((ROOT == "/" ? "" : ROOT) . ($this->id == "/" ? "" : $this->id) . "/index.xml",
                                       (ROOT == "/" ? "" : ROOT) . "/s3/interface.xsl"
                                      );
        } elseif($this->mode == "receive") {
            $result = $this->SmartXSLT($this->sdf_source, (ROOT == "/" ? "" : ROOT) . "/s3/interface.xsl");
        }

        /* For old brosers */
        if ($this->xhtml_version == 1.0) {
            $result = $this->SmartXSLT($result, (ROOT == "/" ? "" : ROOT) . "/s3/11to10.xsl");
        }

        /* Strip space nodes and indent */
        $indent = new DomDocument();
        $indent->preserveWhiteSpace = FALSE;
        $indent->loadXML($result);
        $result = $indent->saveXML();
        $result = $this->SmartXSLT($result, (ROOT == "/" ? "" : ROOT) . "/himmel/indent.xsl");

        /* Clean up namespace */
        $result = $this->SmartXSLT($result, (ROOT == "/" ? "" : ROOT) . "/himmel/valid.xsl");

        // Hack for IE6
        if (preg_match("/MSIE 6.0/", $_SERVER["HTTP_USER_AGENT"])) {
            $result = preg_replace("/<\?xml.*?\?>\n/", "", $result);
        }

        // Hacks
        $result = preg_replace("/(<.*?[^ ])\/>/is", "$1 />", $result);
        $result = preg_replace("/<textarea(.*?)\/>/is", '<textarea$1></textarea>', $result);
        $result = preg_replace("/<script(.*?)\/>/is", '<script$1></script>', $result);

        return $result;
    }

    function SmartXSLT($xmls, $xsls, $param = NULL)
    {
        /* Load the sources */
        $xml = new DomDocument();
        is_file($xmls) ? $xml->load($xmls) : $xml->loadXML($xmls);
        $xsl = new DomDocument();
        is_file($xsls) ? $xsl->load($xsls) : $xsl->loadXML($xsls);

        /* XSLT */
        $proc = new xsltprocessor;
        $proc->importStyleSheet($xsl);
        return $proc->transformToXML($xml);
    }

    function HTTPHeaders($xhtml)
    {
        /* Get Modified */
        $dom_mod = new DomDocument;
        $dom_mod->loadXML($xhtml);
        $xph_mod = new domXPath($dom_mod);
        $xph_mod->registerNamespace("xh", "http://www.w3.org/1999/xhtml");
        $node_mod = $xph_mod->query("/xh:html/xh:head/xh:meta[./@name = 'WWWC']");
        $modified = $node_mod->item(0)->getAttribute("content");
        $modified = str_replace("/", "-", substr($modified, 0, 10)) . "T" . substr($modified, -5, 5) . "Z";
        $modified = parse_w3cdtf($modified);
        $modified = $modified["timestamp"];

        // If-Modified-Since
        if ($request_headers["If-Modified-Since"]) {
            $since = parse_http_date($request_headers["If-Modified-Since"]);
            if ($since["timestamp"] >= $modified) {
                header("HTTP/1.1 304 Not Modified");
                exit();
            }
        }

        // Send HTTP Headers
        if ($this->version_xhtml >= 1.1) {
            header("Content-Type: application/xhtml+xml; charset=UTF-8");
        } elseif ($this->version_xhtml == 1.0) {
            header("Content-Type: text/html; charset=UTF-8");
        }
        header("Last-Modified: " . gmdate("D, d M Y H:i:s", $modified) . " GMT");
        header("Content-Style-Type: text/css");
        header("Content-Script-Type: text/javascript");
    }
}

オブジェクト指向なんて何年も前にJavaScriptで触って以来なのでよく分からない。特に変数をどうするかが難しい。そもそも処理がほとんど1本道なのでオブジェクト指向の利点が無いような気もする。

Comments

Trackbacks

Trackback Ping URI

http://yudai.arielworks.com/memo/2004/07/14/223027.trackback

末尾に「7 + 2」の計算結果を繋げて下さい。例えば計算結果が「17」の場合、「223027.trackback17」です。これは機械的なトラックバックスパムを防止するための措置です。

Post a comment

Name (optional)
Email address or URI (optional)
Do the math below (required to filter comment spams)
7 + 2 + 6 =
Message (required)
Submit
連絡先、リンク、転載や複製などについては『サイト案内』をご覧ください。Powered by HIMMEL

I ♥ Validator