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