예제 #1
0
function xml_apply_stylesheet($s)
{
    $debugging = false;
    $message = '';
    if (preg_match_all("/<\\?xml-stylesheet[^>]+?\\?>/ms", $s, $matches, PREG_SET_ORDER)) {
        foreach ($matches as $m) {
            $pi = $m[0];
            if (preg_match_all('/\\s+(([a-zA-Z]+)=([\'"])(.*?)\\3)' . '/', $pi, $matches2, PREG_SET_ORDER)) {
                $attrs = array();
                //					$message .= "<pre>".array_dump($matches)."</pre>";
                foreach ($matches2 as $a) {
                    $attrs[$a[2]] = $a[4];
                    //$s .= "<pre>".$a[2]."=".$a[4]."</pre>";
                }
                if (!$attrs['href']) {
                    continue;
                }
                if (!$attrs['type']) {
                    $attrs['type'] = 'text/xsl';
                }
                if ($attrs['type'] == 'text/css') {
                    $stylesheets[] = array('href' => 'arg:/xsl', 'xsl' => XSL_APPLYCSS_STYLESHEET, 'type' => $attrs['type'], 'params' => array('cssfile' => $attrs['href']));
                } elseif ($attrs['type'] == 'debug') {
                    $debugging = TRUE;
                    break;
                } else {
                    $stylesheets[] = array('href' => $attrs['href'], 'type' => $attrs['type']);
                }
            }
        }
        /* $pi = preg_replace("/<\?xml-stylesheet (.*)\?>/", '\1', $pi);
        			$stylesheet = preg_replace("/href=(.)(.*)\\1/", '\2', $pi); */
    } else {
        return $s;
    }
    $s = preg_replace("/<\\?xml-stylesheet[^>]+?\\?>/ms", '', $s);
    if ($debugging) {
        return '<pre>' . htmlspecialchars($s) . '</pre>';
    }
    if (count($stylesheets) == 0) {
        $stylesheets[] = array('href' => 'arg:/xsl', 'type' => 'text/xsl', 'xsl' => XSL_DEFAULT_STYLESHEET);
    }
    $input = $s;
    //		$message .= 'md5-'.md5($input)."<br />";
    foreach ($stylesheets as $stylesheet) {
        $xsl = new DomDocument();
        $xsl->load($stylesheet['href']);
        $xml = new DomDocument();
        @$xml->loadXML($input);
        if (!isset($stylesheet['params'])) {
            $stylesheet['params'] = array();
        }
        $proc = new XSLTProcessor();
        $proc->importStyleSheet($xsl);
        foreach ($stylesheet['params'] as $param => $val) {
            $proc->setparameter('', $param, $val);
        }
        if (!($result = $proc->transformToXML($xml))) {
            $message .= "Error applying {$stylesheet['href']}: " . '';
            $message .= "Failed on:<pre>" . wordwrap(htmlspecialchars($input)) . "</pre>";
            $f = fopen("/tmp/dead.xml", "w");
            fputs($f, $input);
            fclose($f);
            system("xmllint /tmp/dead.xml > /tmp/xmllint.log 2>&1");
            if (XSL_PROC_MAILTO) {
                foreach ($_SERVER as $k => $v) {
                    $mailmessage .= "{$k} = {$v}\n";
                }
                $mailmessage .= $message;
                $message = 'Stylesheet failed';
                $result = $input;
                mail(XSL_PROC_MAILTO, 'XML Stylesheet Failed (' . $_SERVER['REQUEST_URI'] . ')', html_entity_decode($mailmessage));
            }
        } else {
            $message .= "<!-- applied {$stylesheet['href']} -->\n";
            //				$message .= 'md5-'.md5($input)."\n";
            $input = $result;
        }
        #xslt_free($xslt_engine);
    }
    return $result . $message;
}