aboutsummaryrefslogtreecommitdiffstats
path: root/include/smarty/libs/sysplugins/smarty_internal_runtime_getincludepath.php
blob: 72149cd3592320af59fd337c441afa64548e8338 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
<?php
/**
 * Smarty read include path plugin
 *
 * @package    Smarty
 * @subpackage PluginsInternal
 * @author     Monte Ohrt
 */

/**
 * Smarty Internal Read Include Path Class
 *
 * @package    Smarty
 * @subpackage PluginsInternal
 */
class Smarty_Internal_Runtime_GetIncludePath
{
    /**
     * include path cache
     *
     * @var string
     */
    public $_include_path = '';

    /**
     * include path directory cache
     *
     * @var array
     */
    public $_include_dirs = array();

    /**
     * include path directory cache
     *
     * @var array
     */
    public $_user_dirs = array();

    /**
     * stream cache
     *
     * @var string[]
     */
    public $isFile = array();

    /**
     * stream cache
     *
     * @var string[]
     */
    public $isPath = array();

    /**
     * stream cache
     *
     * @var int[]
     */
    public $number = array();

    /**
     * status cache
     *
     * @var bool
     */
    public $_has_stream_include = null;

    /**
     * Number for array index
     *
     * @var int
     */
    public $counter = 0;

    /**
     * Check if include path was updated
     *
     * @param \Smarty $smarty
     *
     * @return bool
     */
    public function isNewIncludePath(Smarty $smarty)
    {
        $_i_path = get_include_path();
        if ($this->_include_path != $_i_path) {
            $this->_include_dirs = array();
            $this->_include_path = $_i_path;
            $_dirs = (array) explode(PATH_SEPARATOR, $_i_path);
            foreach ($_dirs as $_path) {
                if (is_dir($_path)) {
                    $this->_include_dirs[] = $smarty->_realpath($_path . DS, true);
                }
            }
            return true;
        }
        return false;
    }

    /**
     * return array with include path directories
     *
     * @param \Smarty $smarty
     *
     * @return array
     */
    public function getIncludePathDirs(Smarty $smarty)
    {
        $this->isNewIncludePath($smarty);
        return $this->_include_dirs;
    }

    /**
     * Return full file path from PHP include_path
     *
     * @param  string[] $dirs
     * @param  string   $file
     * @param \Smarty   $smarty
     *
     * @return bool|string full filepath or false
     *
     */
    public function getIncludePath($dirs, $file, Smarty $smarty)
    {
        //if (!(isset($this->_has_stream_include) ? $this->_has_stream_include : $this->_has_stream_include = false)) {
           if (!(isset($this->_has_stream_include) ? $this->_has_stream_include : $this->_has_stream_include = function_exists('stream_resolve_include_path'))) {
                $this->isNewIncludePath($smarty);
        }
        // try PHP include_path
        foreach ($dirs as $dir) {
            $dir_n = isset($this->number[$dir]) ? $this->number[$dir] : $this->number[$dir] = $this->counter ++;
            if (isset($this->isFile[$dir_n][$file])) {
                if ($this->isFile[$dir_n][$file]) {
                    return $this->isFile[$dir_n][$file];
                } else {
                    continue;
                }
            }
            if (isset($this->_user_dirs[$dir_n])) {
                if (false === $this->_user_dirs[$dir_n]) {
                    continue;
                } else {
                    $dir = $this->_user_dirs[$dir_n];
                }
            } else {
                if ($dir[0] == '/' || $dir[1] == ':') {
                    $dir = str_ireplace(getcwd(), '.', $dir);
                    if ($dir[0] == '/' || $dir[1] == ':') {
                        $this->_user_dirs[$dir_n] = false;
                        continue;
                    }
                }
                $dir = substr($dir, 2);
                $this->_user_dirs[$dir_n] = $dir;
            }
            if ($this->_has_stream_include) {
                $path = stream_resolve_include_path($dir . (isset($file) ? $file : ''));
                if ($path) {
                    return $this->isFile[$dir_n][$file] = $path;
                }
            } else {
                foreach ($this->_include_dirs as $key => $_i_path) {
                    $path = isset($this->isPath[$key][$dir_n]) ? $this->isPath[$key][$dir_n] : $this->isPath[$key][$dir_n] = is_dir($_dir_path = $_i_path .
                        $dir) ? $_dir_path : false;
                    if ($path === false) {
                        continue;
                    }
                    if (isset($file)) {
                        $_file = $this->isFile[$dir_n][$file] = (is_file($path . $file)) ? $path . $file : false;
                        if ($_file) {
                            return $_file;
                        }
                    } else {
                        // no file was given return directory path
                        return $path;
                    }
                }
            }
        }
        return false;
    }
}