dtpk04526211 2014-11-25 13:15
浏览 21

在节点值上运行PHP函数然后输出XML?

I have an XML that im getting from file_get_contents. It contains a bunch of values including encrypted ones which are inside <strStringValue>. I have a function that will decrypt them, i tried to use this code but it just printed out the values inside strStringValue:

    <?php
if (!isset($HTTP_RAW_POST_DATA)) {
    $HTTP_RAW_POST_DATA = file_get_contents("php://input"); 
    $doc = new DOMDocument();
    $doc->loadXML($HTTP_RAW_POST_DATA);
    $stringvalue = $doc->getElementsByTagName('strStringValue');
    foreach ($stringvalue as $strstringvalue) {
             echo $strstringvalue->nodeValue, PHP_EOL;
    }

    include("Decrypt1.php");
    $in = "$strstringvalue";

    $HTTP_RAW_POST_DATA2=Decrypt1($in);
    echo("$HTTP_RAW_POST_DATA2");
    }
?>

Here is an example of what the XML looks like:

Encrypted

<string>
<Id>1</Id>
<strStringCaption>file-commision</strStringCaption>
<strStringValue enc="0">2734sad87asd78asdt8a7d6874slwe9832t</strStringValue>
</string>
<string>
<Id>2</Id>
<strStringCaption>file-shop</strStringCaption>
<strStringValue enc="0">78sd7asgd87adg87g873g78dad</strStringValue>
</string>

Decrypted

<string>
<Id>1</Id>
<strStringCaption>file-commision</strStringCaption>
<strStringValue enc="0">commisions.txt</strStringValue>
</string>
<string>
<Id>2</Id>
<strStringCaption>file-shop</strStringCaption>
<strStringValue enc="0">shoplist.xml</strStringValue>
</string>
<string>
<string>
  • 写回答

1条回答 默认 最新

  • duanduan8439 2014-11-25 14:10
    关注

    You could do it via XSL(T) and XSLTProcessor::registerPHPFunctions

    (sorry for the tons and tons of boilerplate code)

    <?php
    echo "
    ------- src -------
    ";
    $xml = getDocData();
    echo $xml;
    
    echo "
    ------- process #1 (encrypt) -------
    ";
    $xml = process($xml);
    echo $xml;
    
    echo "
    ------- process #2 (decrypt) -------
    ";
    $xml = process($xml);
    echo $xml;
    
    function myEncrypt($plain) { 
        // just a demo, not even pretending to do proper encryption
        return 'sosecret:'.strrev($plain);
    }
    
    function myDecrypt($encrypted) {
        $prefix = 'sosecret:';
        $cbPrefix = strlen($prefix);
        if ( 0===strncmp($encrypted, $prefix, $cbPrefix) ) {
            return strrev(substr($encrypted, $cbPrefix));
        }
        return 'error';
    }
    
    function process($xml) {
        $processor = getXSLT(getStyleData());
        $doc = getDocument($xml);
        return $processor->transformToXML($doc);
    }
    
    function getXSLT($xml) {
        $doc = getDocument($xml);
        $proc = new XSLTProcessor();
        $proc->registerPHPFunctions( array('myEncrypt', 'myDecrypt'));
        $proc->importStyleSheet($doc);
        return $proc;
    }
    
    function getDocument($xml) {
        $doc = new DOMDocument;
        $doc->loadxml($xml);
        return $doc;
    }
    
    
    function getDocData() {
        return <<< EOX
    <doc>   
        <string>
            <Id>1</Id>
            <strStringCaption>file-commision</strStringCaption>
            <strStringValue enc="0">commisions.txt</strStringValue>
        </string>
        <string>
            <Id>2</Id>
            <strStringCaption>file-shop</strStringCaption>
            <strStringValue enc="0">shoplist.xml</strStringValue>
        </string>
    </doc>
    EOX;
    }
    
    function getStyleData() {
        return <<< EOX
    <?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:php="http://php.net/xsl">
    <xsl:output method="xml"/>
        <xsl:template match="strStringValue[@enc='0']">
            <strStringValue enc="1">
                <xsl:value-of select="php:function('myEncrypt',string(.))"/>
            </strStringValue>
        </xsl:template>
        <xsl:template match="strStringValue[@enc='1']">
            <strStringValue enc="0">
                <xsl:value-of select="php:function('myDecrypt',string(.))"/>
            </strStringValue>
        </xsl:template>
        <xsl:template match="@*|node()">
          <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
          </xsl:copy>
        </xsl:template>
    </xsl:stylesheet>
    EOX;
    }
    

    prints

    ------- src -------
    <doc>   
        <string>
            <Id>1</Id>
            <strStringCaption>file-commision</strStringCaption>
            <strStringValue enc="0">commisions.txt</strStringValue>
        </string>
        <string>
            <Id>2</Id>
            <strStringCaption>file-shop</strStringCaption>
            <strStringValue enc="0">shoplist.xml</strStringValue>
        </string>
    </doc>
    ------- process #1 (encrypt) -------
    <?xml version="1.0"?>
    <doc>   
        <string>
            <Id>1</Id>
            <strStringCaption>file-commision</strStringCaption>
            <strStringValue xmlns:php="http://php.net/xsl" enc="1">sosecret:txt.snoisimmoc</strStringValue>
        </string>
        <string>
            <Id>2</Id>
            <strStringCaption>file-shop</strStringCaption>
            <strStringValue xmlns:php="http://php.net/xsl" enc="1">sosecret:lmx.tsilpohs</strStringValue>
        </string>
    </doc>
    
    ------- process #2 (decrypt) -------
    <?xml version="1.0"?>
    <doc>   
        <string>
            <Id>1</Id>
            <strStringCaption>file-commision</strStringCaption>
            <strStringValue xmlns:php="http://php.net/xsl" enc="0">commisions.txt</strStringValue>
        </string>
        <string>
            <Id>2</Id>
            <strStringCaption>file-shop</strStringCaption>
            <strStringValue xmlns:php="http://php.net/xsl" enc="0">shoplist.xml</strStringValue>
        </string>
    </doc>
    

    but how to get rid of the @!#§! xmlns:php="http://php.net/xsl" attribute is beyond my capabilities right now.

    <xsl:template match="@*|node()"> is a basic "copy-it-all template". It matches almost anything that has no "better" match and just copies the source node as-is to the target.

    <xsl:template match="strStringValue[@enc='0']"> is the template that matches the strStringValue elements that are not encrypted and "replaces" it in the target with the encrypted version via myEncrypt($plain).

    <xsl:template match="strStringValue[@enc='1']"> does the reverse thing via myDecrypt($encrypted).

    评论

报告相同问题?

悬赏问题

  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答