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.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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 . DIRECTORY_SEPARATOR, 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 :
  114. $this->_has_stream_include = function_exists('stream_resolve_include_path'))
  115. ) {
  116. $this->isNewIncludePath($smarty);
  117. }
  118. // try PHP include_path
  119. foreach ($dirs as $dir) {
  120. $dir_n = isset($this->number[ $dir ]) ? $this->number[ $dir ] : $this->number[ $dir ] = $this->counter++;
  121. if (isset($this->isFile[ $dir_n ][ $file ])) {
  122. if ($this->isFile[ $dir_n ][ $file ]) {
  123. return $this->isFile[ $dir_n ][ $file ];
  124. } else {
  125. continue;
  126. }
  127. }
  128. if (isset($this->_user_dirs[ $dir_n ])) {
  129. if (false === $this->_user_dirs[ $dir_n ]) {
  130. continue;
  131. } else {
  132. $dir = $this->_user_dirs[ $dir_n ];
  133. }
  134. } else {
  135. if ($dir[ 0 ] === '/' || $dir[ 1 ] === ':') {
  136. $dir = str_ireplace(getcwd(), '.', $dir);
  137. if ($dir[ 0 ] === '/' || $dir[ 1 ] === ':') {
  138. $this->_user_dirs[ $dir_n ] = false;
  139. continue;
  140. }
  141. }
  142. $dir = substr($dir, 2);
  143. $this->_user_dirs[ $dir_n ] = $dir;
  144. }
  145. if ($this->_has_stream_include) {
  146. $path = stream_resolve_include_path($dir . (isset($file) ? $file : ''));
  147. if ($path) {
  148. return $this->isFile[ $dir_n ][ $file ] = $path;
  149. }
  150. } else {
  151. foreach ($this->_include_dirs as $key => $_i_path) {
  152. $path = isset($this->isPath[ $key ][ $dir_n ]) ? $this->isPath[ $key ][ $dir_n ] :
  153. $this->isPath[ $key ][ $dir_n ] = is_dir($_dir_path = $_i_path . $dir) ? $_dir_path : false;
  154. if ($path === false) {
  155. continue;
  156. }
  157. if (isset($file)) {
  158. $_file = $this->isFile[ $dir_n ][ $file ] = (is_file($path . $file)) ? $path . $file : false;
  159. if ($_file) {
  160. return $_file;
  161. }
  162. } else {
  163. // no file was given return directory path
  164. return $path;
  165. }
  166. }
  167. }
  168. }
  169. return false;
  170. }
  171. }