LoggerTransform.php 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php
  2. /**
  3. * log4php is a PHP port of the log4j java logging package.
  4. *
  5. * <p>This framework is based on log4j (see {@link http://jakarta.apache.org/log4j log4j} for details).</p>
  6. * <p>Design, strategies and part of the methods documentation are developed by log4j team
  7. * (Ceki Gülcü as log4j project founder and
  8. * {@link http://jakarta.apache.org/log4j/docs/contributors.html contributors}).</p>
  9. *
  10. * <p>PHP port, extensions and modifications by VxR. All rights reserved.<br>
  11. * For more information, please see {@link http://www.vxr.it/log4php/}.</p>
  12. *
  13. * <p>This software is published under the terms of the LGPL License
  14. * a copy of which has been included with this distribution in the LICENSE file.</p>
  15. *
  16. * @package log4php
  17. * @subpackage helpers
  18. */
  19. /**
  20. * @ignore
  21. */
  22. if (!defined('LOG4PHP_DIR')) define('LOG4PHP_DIR', dirname(__FILE__) . '/..');
  23. define('LOG4PHP_LOGGER_TRANSFORM_CDATA_START', '<![CDATA[');
  24. define('LOG4PHP_LOGGER_TRANSFORM_CDATA_END', ']]>');
  25. define('LOG4PHP_LOGGER_TRANSFORM_CDATA_PSEUDO_END', ']]&gt;');
  26. define('LOG4PHP_LOGGER_TRANSFORM_CDATA_EMBEDDED_END',
  27. LOG4PHP_LOGGER_TRANSFORM_CDATA_END .
  28. LOG4PHP_LOGGER_TRANSFORM_CDATA_PSEUDO_END .
  29. LOG4PHP_LOGGER_TRANSFORM_CDATA_START
  30. );
  31. /**
  32. * Utility class for transforming strings.
  33. *
  34. * @log4j-class org.apache.log4j.helpers.Transform
  35. *
  36. * @author VxR <vxr@vxr.it>
  37. * @package log4php
  38. * @subpackage helpers
  39. * @since 0.7
  40. */
  41. class LoggerTransform {
  42. /**
  43. * This method takes a string which may contain HTML tags (ie,
  44. * &lt;b&gt;, &lt;table&gt;, etc) and replaces any '&lt;' and '&gt;'
  45. * characters with respective predefined entity references.
  46. *
  47. * @param string $input The text to be converted.
  48. * @return string The input string with the characters '&lt;' and '&gt;' replaced with
  49. * &amp;lt; and &amp;gt; respectively.
  50. * @static
  51. */
  52. function escapeTags($input)
  53. {
  54. //Check if the string is null or zero length -- if so, return
  55. //what was sent in.
  56. if(empty($input))
  57. return $input;
  58. //Use a StringBuffer in lieu of String concatenation -- it is
  59. //much more efficient this way.
  60. return htmlspecialchars($input, ENT_NOQUOTES);
  61. }
  62. /**
  63. * Ensures that embeded CDEnd strings (]]&gt;) are handled properly
  64. * within message, NDC and throwable tag text.
  65. *
  66. * @param string $buf String holding the XML data to this point. The
  67. * initial CDStart (<![CDATA[) and final CDEnd (]]>)
  68. * of the CDATA section are the responsibility of
  69. * the calling method.
  70. * @param string &str The String that is inserted into an existing
  71. * CDATA Section within buf.
  72. * @static
  73. */
  74. function appendEscapingCDATA(&$buf, $str)
  75. {
  76. if(empty($str))
  77. return;
  78. $rStr = str_replace(
  79. LOG4PHP_LOGGER_TRANSFORM_CDATA_END,
  80. LOG4PHP_LOGGER_TRANSFORM_CDATA_EMBEDDED_END,
  81. $str
  82. );
  83. $buf .= $rStr;
  84. }
  85. }
  86. ?>