예제 #1
0
 /**
  * Decrypt this encrypted node.
  *
  * The behaviour of this function depends on the value of $replace.
  * If $replace is FALSE, we will return the decrypted data as a string.
  * If $replace is TRUE, we will insert the decrypted element(s) into the
  * document, and return the decrypted element(s).
  *
  * @params XMLSecurityKey $objKey  The decryption key that should be used when decrypting the node.
  * @params boolean $replace  Whether we should replace the encrypted node in the XML document with the decrypted data. The default is TRUE.
  *
  * @param $objKey
  * @param bool $replace
  *
  * @return DOMElement|string The decrypted data.
  * @throws Exception
  */
 public function decryptNode(XMLSecurityKey $objKey, $replace = true)
 {
     if (!$objKey instanceof XMLSecurityKey) {
         throw new Exception('Invalid Key');
     }
     $encryptedData = $this->getCipherValue();
     if ($encryptedData) {
         $decrypted = $objKey->decryptData($encryptedData);
         if ($replace) {
             switch ($this->type) {
                 case XMLSecEnc::Element:
                     $newdoc = new DOMDocument();
                     $newdoc->loadXML($decrypted);
                     if ($this->rawNode->nodeType == XML_DOCUMENT_NODE) {
                         return $newdoc;
                     }
                     $importEnc = $this->rawNode->ownerDocument->importNode($newdoc->documentElement, true);
                     $this->rawNode->parentNode->replaceChild($importEnc, $this->rawNode);
                     return $importEnc;
                     break;
                 case XMLSecEnc::Content:
                     if ($this->rawNode->nodeType == XML_DOCUMENT_NODE) {
                         $doc = $this->rawNode;
                     } else {
                         $doc = $this->rawNode->ownerDocument;
                     }
                     $newFrag = $doc->createDocumentFragment();
                     $newFrag->appendXML($decrypted);
                     $parent = $this->rawNode->parentNode;
                     $parent->replaceChild($newFrag, $this->rawNode);
                     return $parent;
                     break;
                 default:
                     return $decrypted;
             }
         } else {
             return $decrypted;
         }
     } else {
         throw new \Exception("Cannot locate encrypted data");
     }
 }