Transform.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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-2008 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. * Zend_Loader
  24. */
  25. require_once 'Zend/Loader.php';
  26. /**
  27. * A class to create a transform rule set based on XML URIs and then apply those rules
  28. * in the correct order to a given XML input
  29. *
  30. * @category Zend
  31. * @package Zend_InfoCard
  32. * @subpackage Zend_InfoCard_Xml_Security
  33. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  34. * @license http://framework.zend.com/license/new-bsd New BSD License
  35. */
  36. class Zend_InfoCard_Xml_Security_Transform
  37. {
  38. /**
  39. * A list of transforms to apply
  40. *
  41. * @var array
  42. */
  43. protected $_transformList = array();
  44. /**
  45. * Returns the name of the transform class based on a given URI
  46. *
  47. * @throws Zend_InfoCard_Xml_Security_Exception
  48. * @param string $uri The transform URI
  49. * @return string The transform implementation class name
  50. */
  51. protected function _findClassbyURI($uri)
  52. {
  53. switch($uri) {
  54. case 'http://www.w3.org/2000/09/xmldsig#enveloped-signature':
  55. return 'Zend_InfoCard_Xml_Security_Transform_EnvelopedSignature';
  56. case 'http://www.w3.org/2001/10/xml-exc-c14n#':
  57. return 'Zend_InfoCard_Xml_Security_Transform_XmlExcC14N';
  58. default:
  59. require_once 'Zend/InfoCard/Xml/Security/Exception.php';
  60. throw new Zend_InfoCard_Xml_Security_Exception("Unknown or Unsupported Transformation Requested");
  61. }
  62. }
  63. /**
  64. * Add a Transform URI to the list of transforms to perform
  65. *
  66. * @param string $uri The Transform URI
  67. * @return Zend_InfoCard_Xml_Security_Transform
  68. */
  69. public function addTransform($uri)
  70. {
  71. $class = $this->_findClassbyURI($uri);
  72. $this->_transformList[] = array('uri' => $uri,
  73. 'class' => $class);
  74. return $this;
  75. }
  76. /**
  77. * Return the list of transforms to perform
  78. *
  79. * @return array The list of transforms
  80. */
  81. public function getTransformList()
  82. {
  83. return $this->_transformList;
  84. }
  85. /**
  86. * Apply the transforms in the transform list to the input XML document
  87. *
  88. * @param string $strXmlDocument The input XML
  89. * @return string The XML after the transformations have been applied
  90. */
  91. public function applyTransforms($strXmlDocument)
  92. {
  93. foreach($this->_transformList as $transform) {
  94. Zend_Loader::loadClass($transform['class']);
  95. $transformer = new $transform['class'];
  96. // We can't really test this check because it would require logic changes in the component itself
  97. // @codeCoverageIgnoreStart
  98. if(!($transformer instanceof Zend_InfoCard_Xml_Security_Transform_Interface)) {
  99. require_once 'Zend/InfoCard/Xml/Security/Exception.php';
  100. throw new Zend_InfoCard_Xml_Security_Exception("Transforms must implement the Transform Interface");
  101. }
  102. // @codeCoverageIgnoreEnd
  103. $strXmlDocument = $transformer->transform($strXmlDocument);
  104. }
  105. return $strXmlDocument;
  106. }
  107. }