This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include, or for example
via PHP's auto-loading mechanism.
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 1 | <?php |
||
| 2 | |||
| 3 | /** |
||
| 4 | * This file is part of the m1\vars library |
||
| 5 | * |
||
| 6 | * (c) m1 <[email protected]> |
||
| 7 | * |
||
| 8 | * For the full copyright and license information, please view the LICENSE |
||
| 9 | * file that was distributed with this source code. |
||
| 10 | * |
||
| 11 | * @package m1/vars |
||
| 12 | * @version 1.1.0 |
||
| 13 | * @author Miles Croxford <[email protected]> |
||
| 14 | * @copyright Copyright (c) Miles Croxford <[email protected]> |
||
| 15 | * @license http://github.com/m1/vars/blob/master/LICENSE |
||
| 16 | * @link http://github.com/m1/vars/blob/master/README.MD Documentation |
||
| 17 | */ |
||
| 18 | |||
| 19 | namespace M1\Vars; |
||
| 20 | |||
| 21 | use M1\Vars\Cache\CacheProvider; |
||
| 22 | use M1\Vars\Loader\LoaderProvider; |
||
| 23 | use M1\Vars\Resource\AbstractResource; |
||
| 24 | use M1\Vars\Resource\ResourceProvider; |
||
| 25 | use M1\Vars\Traits\PathTrait; |
||
| 26 | use M1\Vars\Traits\TransformerTrait; |
||
| 27 | use M1\Vars\Variables\VariableProvider; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * Vars core class |
||
| 31 | * |
||
| 32 | * @since 0.1.0 |
||
| 33 | */ |
||
| 34 | class Vars extends AbstractResource |
||
| 35 | { |
||
| 36 | /** |
||
| 37 | * Used for path functions and variables |
||
| 38 | */ |
||
| 39 | use PathTrait; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * Used for to* functions |
||
| 43 | */ |
||
| 44 | use TransformerTrait; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * The cache object if the cache is wanted, else false |
||
| 48 | * |
||
| 49 | * @var \M1\Vars\Cache\CacheProvider $cache |
||
| 50 | */ |
||
| 51 | public $cache; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * The default options for Vars |
||
| 55 | * |
||
| 56 | * @var array $default_options |
||
| 57 | */ |
||
| 58 | private $default_options = array( |
||
| 59 | 'path' => null, |
||
| 60 | 'cache' => true, |
||
| 61 | 'cache_path' => null, |
||
| 62 | 'cache_expire' => 300, // 5 minutes |
||
| 63 | 'loaders' => array('env', 'ini', 'json', 'php', 'toml', 'yaml', 'xml',), |
||
| 64 | 'merge_globals' => true, |
||
| 65 | ); |
||
| 66 | |||
| 67 | /** |
||
| 68 | * The global file variables |
||
| 69 | * |
||
| 70 | * @var array globals |
||
| 71 | */ |
||
| 72 | private $globals = array(); |
||
| 73 | |||
| 74 | /** |
||
| 75 | * The loaderProvider for Vars supplies the file loaders and the extensions that are supported |
||
| 76 | * |
||
| 77 | * @var \M1\Vars\Loader\LoaderProvider $loader |
||
| 78 | */ |
||
| 79 | public $loader; |
||
| 80 | |||
| 81 | /** |
||
| 82 | * Have the base and cache paths been set |
||
| 83 | * |
||
| 84 | * @var bool $paths_loaded |
||
| 85 | */ |
||
| 86 | private $paths_loaded = false; |
||
| 87 | |||
| 88 | /** |
||
| 89 | * The imported resources |
||
| 90 | * |
||
| 91 | * @var array $resources |
||
| 92 | */ |
||
| 93 | private $resources = array(); |
||
| 94 | |||
| 95 | /** |
||
| 96 | * The variable provider |
||
| 97 | * |
||
| 98 | * @var \M1\Vars\Variables\VariableProvider $variables |
||
| 99 | */ |
||
| 100 | public $variables; |
||
| 101 | |||
| 102 | /** |
||
| 103 | * Creates a new instance of Vars |
||
| 104 | * |
||
| 105 | * @param string|array $resource The main configuration resource |
||
| 106 | * @param array $options The options being used for Vars |
||
| 107 | */ |
||
| 108 | 86 | public function __construct($resource, $options = array()) |
|
| 109 | { |
||
| 110 | 86 | $options = $this->parseOptions($options); |
|
| 111 | 86 | $this->makeCache($options, $resource); |
|
| 112 | 85 | $this->makePaths($options); |
|
| 113 | 84 | if (!$this->cache->checkCache()) { |
|
| 114 | 84 | $this->makeLoader($options); |
|
| 115 | 82 | $this->makeVariables($options); |
|
| 116 | |||
| 117 | 81 | $resource = new ResourceProvider($this, $resource); |
|
| 118 | } |
||
| 119 | |||
| 120 | 66 | if ($this->cache->isHit()) { |
|
| 121 | 3 | $this->loadFromCache(); |
|
| 122 | } else { |
||
| 123 | 66 | $resource->mergeParentContent(); |
|
|
0 ignored issues
–
show
|
|||
| 124 | 66 | $this->content = $this->mergeGlobals($resource->getContent(), $options); |
|
| 125 | 66 | $this->cache->setTime(time()); |
|
| 126 | 66 | $this->cache->makeCache($this); |
|
| 127 | } |
||
| 128 | 66 | } |
|
| 129 | |||
| 130 | /** |
||
| 131 | * Parses the options so Vars can use them |
||
| 132 | * |
||
| 133 | * @param array $options The options being used for Vars |
||
| 134 | * |
||
| 135 | * @return array The parsed options |
||
| 136 | */ |
||
| 137 | 86 | private function parseOptions(array $options) |
|
| 138 | { |
||
| 139 | 86 | $parsed_options = array_merge($this->default_options, $options); |
|
| 140 | 86 | $parsed_options['loaders'] = (isset($options['loaders'])) ? |
|
| 141 | 86 | $options['loaders'] : $this->default_options['loaders']; |
|
| 142 | |||
| 143 | 86 | return $parsed_options; |
|
| 144 | } |
||
| 145 | |||
| 146 | /** |
||
| 147 | * Makes the CacheProvider with the options |
||
| 148 | * |
||
| 149 | * @param array $options The options being used for Vars |
||
| 150 | * @param array|string $resource The main configuration resource |
||
| 151 | */ |
||
| 152 | 86 | private function makeCache($options, $resource) |
|
| 153 | { |
||
| 154 | 86 | $cache = new CacheProvider($resource, $options); |
|
| 155 | 85 | $this->cache = $cache; |
|
| 156 | 85 | } |
|
| 157 | |||
| 158 | /** |
||
| 159 | * Sets the base path if the options have been set and the cache path if the cache path has not been set but the |
||
| 160 | * base path has |
||
| 161 | * |
||
| 162 | * @param array $options The options being used for Vars |
||
| 163 | */ |
||
| 164 | 85 | private function makePaths($options) |
|
| 165 | { |
||
| 166 | 85 | $this->setPath($options['path']); |
|
| 167 | |||
| 168 | 84 | if (is_null($options['cache_path']) && !is_null($options['path'])) { |
|
| 169 | 1 | $this->cache->setPath($options['path']); |
|
| 170 | 1 | $this->paths_loaded = true; |
|
| 171 | } |
||
| 172 | 84 | } |
|
| 173 | |||
| 174 | /** |
||
| 175 | * Makes the LoaderProvider with the options |
||
| 176 | * |
||
| 177 | * @param array $options The options being used for Vars |
||
| 178 | */ |
||
| 179 | 84 | private function makeLoader($options) |
|
| 180 | { |
||
| 181 | 84 | $loader = new LoaderProvider($options, $this->default_options['loaders']); |
|
| 182 | 82 | $this->loader = $loader; |
|
| 183 | 82 | } |
|
| 184 | |||
| 185 | /** |
||
| 186 | * Sets the replacement variables if the option has been set |
||
| 187 | * |
||
| 188 | * @param array|null $options The options being used for Vars |
||
| 189 | */ |
||
| 190 | 82 | private function makeVariables($options) |
|
| 191 | { |
||
| 192 | 82 | $this->variables = new VariableProvider($this); |
|
| 193 | |||
| 194 | 82 | if (isset($options['replacements'])) { |
|
| 195 | 4 | $this->variables->rstore->load($options['replacements']); |
|
| 196 | } |
||
| 197 | 81 | } |
|
| 198 | |||
| 199 | /** |
||
| 200 | * Loads the cached file into the current class |
||
| 201 | */ |
||
| 202 | 3 | private function loadFromCache() |
|
| 203 | { |
||
| 204 | 3 | $this->cache->load(); |
|
| 205 | |||
| 206 | $passed_keys = array( |
||
| 207 | 3 | 'path', |
|
| 208 | 'content', |
||
| 209 | 'extensions', |
||
| 210 | 'loaders', |
||
| 211 | 'resources', |
||
| 212 | 'replacements', |
||
| 213 | 'globals', |
||
| 214 | ); |
||
| 215 | |||
| 216 | 3 | $loaded_vars = get_object_vars($this->cache->getLoadedVars()); |
|
| 217 | |||
| 218 | 3 | foreach ($loaded_vars as $key => $value) { |
|
| 219 | 3 | if (in_array($key, $passed_keys)) { |
|
| 220 | 3 | $this->$key = $value; |
|
| 221 | } |
||
| 222 | } |
||
| 223 | |||
| 224 | 3 | $this->cache->setTime($loaded_vars['cache']->getTime()); |
|
| 225 | 3 | } |
|
| 226 | |||
| 227 | /** |
||
| 228 | * Checks if the base and cache paths have been set, if not\ set then will use the $resource as the base path |
||
| 229 | * |
||
| 230 | * @param string $resource The resource to use to set the paths if they haven't been set |
||
| 231 | */ |
||
| 232 | 75 | public function pathsLoadedCheck($resource) |
|
| 233 | { |
||
| 234 | 75 | if (!$this->paths_loaded) { |
|
| 235 | 74 | $path = $this->getPath(); |
|
| 236 | |||
| 237 | 74 | if (!$path) { |
|
| 238 | 72 | $file = pathinfo(realpath($resource)); |
|
| 239 | 72 | $path = $file['dirname']; |
|
| 240 | 72 | $this->setPath($path); |
|
| 241 | } |
||
| 242 | |||
| 243 | 74 | if ($this->cache->getProvide() && !$this->cache->getPath()) { |
|
| 244 | 2 | $this->cache->setPath($path); |
|
| 245 | } |
||
| 246 | |||
| 247 | 74 | $this->paths_loaded = true; |
|
| 248 | } |
||
| 249 | 75 | } |
|
| 250 | |||
| 251 | |||
| 252 | /** |
||
| 253 | * Gets the _globals from the file and merges them if merge_globals is true |
||
| 254 | * |
||
| 255 | * @param array $content The unparsed content |
||
| 256 | * @param array $options The options being used for Vars |
||
| 257 | * |
||
| 258 | * @return array $content The parsed content |
||
| 259 | */ |
||
| 260 | 66 | private function mergeGlobals($content, $options) |
|
| 261 | { |
||
| 262 | 66 | if (array_key_exists('_globals', $content)) { |
|
| 263 | 3 | $this->globals = $content['_globals']; |
|
| 264 | |||
| 265 | 3 | if ($options['merge_globals']) { |
|
| 266 | 1 | $content = array_replace_recursive($content, $content['_globals']); |
|
| 267 | } |
||
| 268 | |||
| 269 | 3 | unset($content['_globals']); |
|
| 270 | } |
||
| 271 | |||
| 272 | 66 | return $content; |
|
| 273 | } |
||
| 274 | |||
| 275 | /** |
||
| 276 | * Adds a resource to $this->resources |
||
| 277 | * |
||
| 278 | * @param string $resource Resource to add to the stack |
||
| 279 | * |
||
| 280 | * @return int The position of the added resource |
||
| 281 | */ |
||
| 282 | 75 | public function addResource($resource) |
|
| 283 | { |
||
| 284 | 75 | $r = realpath($resource); |
|
| 285 | 75 | $pos = count($this->resources); |
|
| 286 | 75 | $this->resources[$pos] = $r; |
|
| 287 | 75 | return $pos; |
|
| 288 | } |
||
| 289 | |||
| 290 | /** |
||
| 291 | * Updates the string resource with the FileResource |
||
| 292 | * |
||
| 293 | * @param \M1\Vars\Resource\FileResource $resource The FileResource to add |
||
| 294 | * @param int $pos The position of the string resource |
||
| 295 | * |
||
| 296 | * @return \M1\Vars\Vars |
||
| 297 | */ |
||
| 298 | 64 | public function updateResource($resource, $pos) |
|
| 299 | { |
||
| 300 | 64 | $this->resources[$pos] = $resource; |
|
| 301 | 64 | return $this; |
|
| 302 | } |
||
| 303 | |||
| 304 | /** |
||
| 305 | * Tests to see if the resource has been imported already -- this is to avoid getting into a infinite loop |
||
| 306 | * |
||
| 307 | * @param \M1\Vars\Resource\FileResource|string $resource Resource to check |
||
| 308 | * |
||
| 309 | * @return bool Has resource already been imported |
||
| 310 | */ |
||
| 311 | 75 | public function resourceImported($resource) |
|
| 312 | { |
||
| 313 | 75 | $resource = realpath($resource); |
|
| 314 | 75 | foreach ($this->getResources() as $r) { |
|
| 315 | 49 | if ((is_a($r, 'M1\Vars\Resource\FileResource') && $resource === $r->getFile()) || |
|
| 316 | 49 | (is_string($r) && $resource === $r)) { |
|
| 317 | 49 | return true; |
|
| 318 | } |
||
| 319 | } |
||
| 320 | 75 | return false; |
|
| 321 | } |
||
| 322 | |||
| 323 | /** |
||
| 324 | * Searches the resource stack for a certain resource |
||
| 325 | * |
||
| 326 | * @param string $resource The resource to search for |
||
| 327 | * |
||
| 328 | * @return \M1\Vars\Resource\FileResource|bool Returns the resource if found |
||
| 329 | */ |
||
| 330 | 3 | public function getResource($resource) |
|
| 331 | { |
||
| 332 | 3 | foreach ($this->getResources() as $r) { |
|
| 333 | 3 | if ($resource === $r->getFilename()) { |
|
| 334 | 3 | return $r; |
|
| 335 | } |
||
| 336 | } |
||
| 337 | |||
| 338 | 1 | return false; |
|
| 339 | } |
||
| 340 | |||
| 341 | /** |
||
| 342 | * Returns the imported resources |
||
| 343 | * |
||
| 344 | * @return array The Vars imported resources |
||
| 345 | */ |
||
| 346 | 75 | public function getResources() |
|
| 347 | { |
||
| 348 | 75 | return $this->resources; |
|
| 349 | } |
||
| 350 | |||
| 351 | /** |
||
| 352 | * Returns the imported resources |
||
| 353 | * |
||
| 354 | * @return array The Vars imported resources |
||
| 355 | */ |
||
| 356 | 2 | public function getGlobals() |
|
| 357 | { |
||
| 358 | 2 | return $this->globals; |
|
| 359 | } |
||
| 360 | |||
| 361 | /** |
||
| 362 | * Returns the CacheProvider if set |
||
| 363 | * |
||
| 364 | * @return \M1\Vars\Cache\CacheProvider The CacheProvider |
||
| 365 | */ |
||
| 366 | 7 | public function getCache() |
|
| 367 | { |
||
| 368 | 7 | return $this->cache; |
|
| 369 | } |
||
| 370 | } |
||
| 371 |
If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe: