smarty_internal_security_handler.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <?php
  2. /**
  3. * Smarty Internal Plugin Security Handler
  4. *
  5. * @package Smarty
  6. * @subpackage Security
  7. * @author Uwe Tews
  8. */
  9. /**
  10. * This class contains all methods for security checking
  11. */
  12. class Smarty_Internal_Security_Handler {
  13. function __construct($smarty)
  14. {
  15. $this->smarty = $smarty;
  16. }
  17. /**
  18. * Check if PHP function is trusted.
  19. *
  20. * @param string $function_name
  21. * @param object $compiler compiler object
  22. * @return boolean true if function is trusted
  23. */
  24. function isTrustedPhpFunction($function_name, $compiler)
  25. {
  26. if (empty($this->smarty->security_policy->php_functions) || in_array($function_name, $this->smarty->security_policy->php_functions)) {
  27. return true;
  28. } else {
  29. $compiler->trigger_template_error ("PHP function '{$function_name}' not allowed by security setting");
  30. return false;
  31. }
  32. }
  33. /**
  34. * Check if static class is trusted.
  35. *
  36. * @param string $class_name
  37. * @param object $compiler compiler object
  38. * @return boolean true if class is trusted
  39. */
  40. function isTrustedStaticClass($class_name, $compiler)
  41. {
  42. if (empty($this->smarty->security_policy->static_classes) || in_array($class_name, $this->smarty->security_policy->static_classes)) {
  43. return true;
  44. } else {
  45. $compiler->trigger_template_error ("access to static class '{$class_name}' not allowed by security setting");
  46. return false;
  47. }
  48. }
  49. /**
  50. * Check if modifier is trusted.
  51. *
  52. * @param string $modifier_name
  53. * @param object $compiler compiler object
  54. * @return boolean true if modifier is trusted
  55. */
  56. function isTrustedModifier($modifier_name, $compiler)
  57. {
  58. if (empty($this->smarty->security_policy->modifiers) || in_array($modifier_name, $this->smarty->security_policy->modifiers)) {
  59. return true;
  60. } else {
  61. $compiler->trigger_template_error ("modifier '{$modifier_name}' not allowed by security setting");
  62. return false;
  63. }
  64. }
  65. /**
  66. * Check if stream is trusted.
  67. *
  68. * @param string $stream_name
  69. * @param object $compiler compiler object
  70. * @return boolean true if stream is trusted
  71. */
  72. function isTrustedStream($stream_name)
  73. {
  74. if (empty($this->smarty->security_policy->streams) || in_array($stream_name, $this->smarty->security_policy->streams)) {
  75. return true;
  76. } else {
  77. throw new Exception ("stream '{$stream_name}' not allowed by security setting");
  78. return false;
  79. }
  80. }
  81. /**
  82. * Check if directory of file resource is trusted.
  83. *
  84. * @param string $filepath
  85. * @param object $compiler compiler object
  86. * @return boolean true if directory is trusted
  87. */
  88. function isTrustedResourceDir($filepath)
  89. {
  90. $_rp = realpath($filepath);
  91. if (isset($this->smarty->template_dir)) {
  92. foreach ((array)$this->smarty->template_dir as $curr_dir) {
  93. if (($_cd = realpath($curr_dir)) !== false &&
  94. strncmp($_rp, $_cd, strlen($_cd)) == 0 &&
  95. (strlen($_rp) == strlen($_cd) || substr($_rp, strlen($_cd), 1) == DS)) {
  96. return true;
  97. }
  98. }
  99. }
  100. if (!empty($this->smarty->security_policy->secure_dir)) {
  101. foreach ((array)$this->smarty->security_policy->secure_dir as $curr_dir) {
  102. if (($_cd = realpath($curr_dir)) !== false) {
  103. if ($_cd == $_rp) {
  104. return true;
  105. } elseif (strncmp($_rp, $_cd, strlen($_cd)) == 0 &&
  106. (strlen($_rp) == strlen($_cd) || substr($_rp, strlen($_cd), 1) == DS)) {
  107. return true;
  108. }
  109. }
  110. }
  111. }
  112. throw new Exception ("directory '{$_rp}' not allowed by security setting");
  113. return false;
  114. }
  115. /**
  116. * Check if directory of file resource is trusted.
  117. *
  118. * @param string $filepath
  119. * @param object $compiler compiler object
  120. * @return boolean true if directory is trusted
  121. */
  122. function isTrustedPHPDir($filepath)
  123. {
  124. $_rp = realpath($filepath);
  125. if (!empty($this->smarty->security_policy->trusted_dir)) {
  126. foreach ((array)$this->smarty->security_policy->trusted_dir as $curr_dir) {
  127. if (($_cd = realpath($curr_dir)) !== false) {
  128. if ($_cd == $_rp) {
  129. return true;
  130. } elseif (strncmp($_rp, $_cd, strlen($_cd)) == 0 &&
  131. substr($_rp, strlen($_cd), 1) == DS) {
  132. return true;
  133. }
  134. }
  135. }
  136. }
  137. throw new Exception ("directory '{$_rp}' not allowed by security setting");
  138. return false;
  139. }
  140. }
  141. ?>