You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

smarty_internal_runtime_getincludepath.php 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. <?php
  2. /**
  3. * Smarty read include path plugin
  4. *
  5. * @package Smarty
  6. * @subpackage PluginsInternal
  7. * @author Monte Ohrt
  8. */
  9. /**
  10. * Smarty Internal Read Include Path Class
  11. *
  12. * @package Smarty
  13. * @subpackage PluginsInternal
  14. */
  15. class Smarty_Internal_Runtime_GetIncludePath
  16. {
  17. /**
  18. * include path cache
  19. *
  20. * @var string
  21. */
  22. public $_include_path = '';
  23. /**
  24. * include path directory cache
  25. *
  26. * @var array
  27. */
  28. public $_include_dirs = array();
  29. /**
  30. * include path directory cache
  31. *
  32. * @var array
  33. */
  34. public $_user_dirs = array();
  35. /**
  36. * stream cache
  37. *
  38. * @var string[]
  39. */
  40. public $isFile = array();
  41. /**
  42. * stream cache
  43. *
  44. * @var string[]
  45. */
  46. public $isPath = array();
  47. /**
  48. * stream cache
  49. *
  50. * @var int[]
  51. */
  52. public $number = array();
  53. /**
  54. * status cache
  55. *
  56. * @var bool
  57. */
  58. public $_has_stream_include = null;
  59. /**
  60. * Number for array index
  61. *
  62. * @var int
  63. */
  64. public $counter = 0;
  65. /**
  66. * Check if include path was updated
  67. *
  68. * @param \Smarty $smarty
  69. *
  70. * @return bool
  71. */
  72. public function isNewIncludePath(Smarty $smarty)
  73. {
  74. $_i_path = get_include_path();
  75. if ($this->_include_path != $_i_path) {
  76. $this->_include_dirs = array();
  77. $this->_include_path = $_i_path;
  78. $_dirs = (array) explode(PATH_SEPARATOR, $_i_path);
  79. foreach ($_dirs as $_path) {
  80. if (is_dir($_path)) {
  81. $this->_include_dirs[] = $smarty->_realpath($_path . DS, true);
  82. }
  83. }
  84. return true;
  85. }
  86. return false;
  87. }
  88. /**
  89. * return array with include path directories
  90. *
  91. * @param \Smarty $smarty
  92. *
  93. * @return array
  94. */
  95. public function getIncludePathDirs(Smarty $smarty)
  96. {
  97. $this->isNewIncludePath($smarty);
  98. return $this->_include_dirs;
  99. }
  100. /**
  101. * Return full file path from PHP include_path
  102. *
  103. * @param string[] $dirs
  104. * @param string $file
  105. * @param \Smarty $smarty
  106. *
  107. * @return bool|string full filepath or false
  108. *
  109. */
  110. public function getIncludePath($dirs, $file, Smarty $smarty)
  111. {
  112. //if (!(isset($this->_has_stream_include) ? $this->_has_stream_include : $this->_has_stream_include = false)) {
  113. if (!(isset($this->_has_stream_include) ? $this->_has_stream_include : $this->_has_stream_include = function_exists('stream_resolve_include_path'))) {
  114. $this->isNewIncludePath($smarty);
  115. }
  116. // try PHP include_path
  117. foreach ($dirs as $dir) {
  118. $dir_n = isset($this->number[$dir]) ? $this->number[$dir] : $this->number[$dir] = $this->counter ++;
  119. if (isset($this->isFile[$dir_n][$file])) {
  120. if ($this->isFile[$dir_n][$file]) {
  121. return $this->isFile[$dir_n][$file];
  122. } else {
  123. continue;
  124. }
  125. }
  126. if (isset($this->_user_dirs[$dir_n])) {
  127. if (false === $this->_user_dirs[$dir_n]) {
  128. continue;
  129. } else {
  130. $dir = $this->_user_dirs[$dir_n];
  131. }
  132. } else {
  133. if ($dir[0] == '/' || $dir[1] == ':') {
  134. $dir = str_ireplace(getcwd(), '.', $dir);
  135. if ($dir[0] == '/' || $dir[1] == ':') {
  136. $this->_user_dirs[$dir_n] = false;
  137. continue;
  138. }
  139. }
  140. $dir = substr($dir, 2);
  141. $this->_user_dirs[$dir_n] = $dir;
  142. }
  143. if ($this->_has_stream_include) {
  144. $path = stream_resolve_include_path($dir . (isset($file) ? $file : ''));
  145. if ($path) {
  146. return $this->isFile[$dir_n][$file] = $path;
  147. }
  148. } else {
  149. foreach ($this->_include_dirs as $key => $_i_path) {
  150. $path = isset($this->isPath[$key][$dir_n]) ? $this->isPath[$key][$dir_n] : $this->isPath[$key][$dir_n] = is_dir($_dir_path = $_i_path .
  151. $dir) ? $_dir_path : false;
  152. if ($path === false) {
  153. continue;
  154. }
  155. if (isset($file)) {
  156. $_file = $this->isFile[$dir_n][$file] = (is_file($path . $file)) ? $path . $file : false;
  157. if ($_file) {
  158. return $_file;
  159. }
  160. } else {
  161. // no file was given return directory path
  162. return $path;
  163. }
  164. }
  165. }
  166. }
  167. return false;
  168. }
  169. }