Transform.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_InfoCard
  17. * @subpackage Zend_InfoCard_Xml_Security
  18. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. /**
  23. * A class to create a transform rule set based on XML URIs and then apply those rules
  24. * in the correct order to a given XML input
  25. *
  26. * @category Zend
  27. * @package Zend_InfoCard
  28. * @subpackage Zend_InfoCard_Xml_Security
  29. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  30. * @license http://framework.zend.com/license/new-bsd New BSD License
  31. */
  32. class Zend_InfoCard_Xml_Security_Transform
  33. {
  34. /**
  35. * A list of transforms to apply
  36. *
  37. * @var array
  38. */
  39. protected $_transformList = array();
  40. /**
  41. * Returns the name of the transform class based on a given URI
  42. *
  43. * @throws Zend_InfoCard_Xml_Security_Exception
  44. * @param string $uri The transform URI
  45. * @return string The transform implementation class name
  46. */
  47. protected function _findClassbyURI($uri)
  48. {
  49. switch($uri) {
  50. case 'http://www.w3.org/2000/09/xmldsig#enveloped-signature':
  51. return 'Zend_InfoCard_Xml_Security_Transform_EnvelopedSignature';
  52. case 'http://www.w3.org/2001/10/xml-exc-c14n#':
  53. return 'Zend_InfoCard_Xml_Security_Transform_XmlExcC14N';
  54. default:
  55. require_once 'Zend/InfoCard/Xml/Security/Exception.php';
  56. throw new Zend_InfoCard_Xml_Security_Exception("Unknown or Unsupported Transformation Requested");
  57. }
  58. }
  59. /**
  60. * Add a Transform URI to the list of transforms to perform
  61. *
  62. * @param string $uri The Transform URI
  63. * @return Zend_InfoCard_Xml_Security_Transform
  64. */
  65. public function addTransform($uri)
  66. {
  67. $class = $this->_findClassbyURI($uri);
  68. $this->_transformList[] = array('uri' => $uri,
  69. 'class' => $class);
  70. return $this;
  71. }
  72. /**
  73. * Return the list of transforms to perform
  74. *
  75. * @return array The list of transforms
  76. */
  77. public function getTransformList()
  78. {
  79. return $this->_transformList;
  80. }
  81. /**
  82. * Apply the transforms in the transform list to the input XML document
  83. *
  84. * @param string $strXmlDocument The input XML
  85. * @return string The XML after the transformations have been applied
  86. */
  87. public function applyTransforms($strXmlDocument)
  88. {
  89. foreach($this->_transformList as $transform) {
  90. if (!class_exists($transform['class'])) {
  91. require_once 'Zend/Loader.php';
  92. Zend_Loader::loadClass($transform['class']);
  93. }
  94. $transformer = new $transform['class'];
  95. // We can't really test this check because it would require logic changes in the component itself
  96. // @codeCoverageIgnoreStart
  97. if(!($transformer instanceof Zend_InfoCard_Xml_Security_Transform_Interface)) {
  98. require_once 'Zend/InfoCard/Xml/Security/Exception.php';
  99. throw new Zend_InfoCard_Xml_Security_Exception("Transforms must implement the Transform Interface");
  100. }
  101. // @codeCoverageIgnoreEnd
  102. $strXmlDocument = $transformer->transform($strXmlDocument);
  103. }
  104. return $strXmlDocument;
  105. }
  106. }