PageRenderTime 38ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/external_lib/Smarty/sysplugins/smarty_internal_security_handler.php

https://github.com/modulargaming/kittokittokitto
PHP | 130 lines | 80 code | 5 blank | 45 comment | 29 complexity | e0c336e12f275b5aafa099f73209eeff MD5 | raw file
  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 modifier is trusted.
  35. *
  36. * @param string $modifier_name
  37. * @param object $compiler compiler object
  38. * @return boolean true if modifier is trusted
  39. */
  40. function isTrustedModifier($modifier_name, $compiler)
  41. {
  42. if (empty($this->smarty->security_policy->modifiers) || in_array($modifier_name, $this->smarty->security_policy->modifiers)) {
  43. return true;
  44. } else {
  45. $compiler->trigger_template_error ("modifier \"" . $modifier_name . "\" not allowed by security setting");
  46. return false;
  47. }
  48. }
  49. /**
  50. * Check if stream is trusted.
  51. *
  52. * @param string $stream_name
  53. * @param object $compiler compiler object
  54. * @return boolean true if stream is trusted
  55. */
  56. function isTrustedStream($stream_name)
  57. {
  58. if (empty($this->smarty->security_policy->streams) || in_array($stream_name, $this->smarty->security_policy->streams)) {
  59. return true;
  60. } else {
  61. throw new Exception ("stream \"" . $stream_name . "\" not allowed by security setting");
  62. return false;
  63. }
  64. }
  65. /**
  66. * Check if directory of file resource is trusted.
  67. *
  68. * @param string $filepath
  69. * @param object $compiler compiler object
  70. * @return boolean true if directory is trusted
  71. */
  72. function isTrustedResourceDir($filepath)
  73. {
  74. $_rp = realpath($filepath);
  75. if (isset($this->smarty->template_dir)) {
  76. foreach ((array)$this->smarty->template_dir as $curr_dir) {
  77. if (($_cd = realpath($curr_dir)) !== false &&
  78. strncmp($_rp, $_cd, strlen($_cd)) == 0 &&
  79. (strlen($_rp) == strlen($_cd) || substr($_rp, strlen($_cd), 1) == DS)) {
  80. return true;
  81. }
  82. }
  83. }
  84. if (!empty($this->smarty->security_policy->secure_dir)) {
  85. foreach ((array)$this->smarty->security_policy->secure_dir as $curr_dir) {
  86. if (($_cd = realpath($curr_dir)) !== false) {
  87. if ($_cd == $_rp) {
  88. return true;
  89. } elseif (strncmp($_rp, $_cd, strlen($_cd)) == 0 &&
  90. (strlen($_rp) == strlen($_cd) || substr($_rp, strlen($_cd), 1) == DS)) {
  91. return true;
  92. }
  93. }
  94. }
  95. }
  96. throw new Exception ("directory \"" . $_rp . "\" not allowed by security setting");
  97. return false;
  98. }
  99. /**
  100. * Check if directory of file resource is trusted.
  101. *
  102. * @param string $filepath
  103. * @param object $compiler compiler object
  104. * @return boolean true if directory is trusted
  105. */
  106. function isTrustedPHPDir($filepath)
  107. {
  108. $_rp = realpath($filepath);
  109. if (!empty($this->smarty->security_policy->trusted_dir)) {
  110. foreach ((array)$this->smarty->security_policy->trusted_dir as $curr_dir) {
  111. if (($_cd = realpath($curr_dir)) !== false) {
  112. if ($_cd == $_rp) {
  113. return true;
  114. } elseif (strncmp($_rp, $_cd, strlen($_cd)) == 0 &&
  115. substr($_rp, strlen($_cd), 1) == DS) {
  116. return true;
  117. }
  118. }
  119. }
  120. }
  121. throw new Exception ("directory \"" . $_rp . "\" not allowed by security setting");
  122. return false;
  123. }
  124. }
  125. ?>