Issues (8)

Security Analysis    no request data  

This project does not seem to handle request data directly as such no vulnerable execution paths were found.

  Cross-Site Scripting
Cross-Site Scripting enables an attacker to inject code into the response of a web-request that is viewed by other users. It can for example be used to bypass access controls, or even to take over other users' accounts.
  File Exposure
File Exposure allows an attacker to gain access to local files that he should not be able to access. These files can for example include database credentials, or other configuration files.
  File Manipulation
File Manipulation enables an attacker to write custom data to files. This potentially leads to injection of arbitrary code on the server.
  Object Injection
Object Injection enables an attacker to inject an object into PHP code, and can lead to arbitrary code execution, file exposure, or file manipulation attacks.
  Code Injection
Code Injection enables an attacker to execute arbitrary code on the server.
  Response Splitting
Response Splitting can be used to send arbitrary responses.
  File Inclusion
File Inclusion enables an attacker to inject custom files into PHP's file loading mechanism, either explicitly passed to include, or for example via PHP's auto-loading mechanism.
  Command Injection
Command Injection enables an attacker to inject a shell command that is execute with the privileges of the web-server. This can be used to expose sensitive data, or gain access of your server.
  SQL Injection
SQL Injection enables an attacker to execute arbitrary SQL code on your database server gaining access to user data, or manipulating user data.
  XPath Injection
XPath Injection enables an attacker to modify the parts of XML document that are read. If that XML document is for example used for authentication, this can lead to further vulnerabilities similar to SQL Injection.
  LDAP Injection
LDAP Injection enables an attacker to inject LDAP statements potentially granting permission to run unauthorized queries, or modify content inside the LDAP tree.
  Header Injection
  Other Vulnerability
This category comprises other attack vectors such as manipulating the PHP runtime, loading custom extensions, freezing the runtime, or similar.
  Regex Injection
Regex Injection enables an attacker to execute arbitrary code in your PHP process.
  XML Injection
XML Injection enables an attacker to read files on your local filesystem including configuration files, or can be abused to freeze your web-server process.
  Variable Injection
Variable Injection enables an attacker to overwrite program variables with custom data, and can lead to further vulnerabilities.
Unfortunately, the security analysis is currently not available for your project. If you are a non-commercial open-source project, please contact support to gain access.

src/Container.php (6 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
/**
3
 * @package    Fuel\Config
4
 * @version    2.0
5
 * @author     Fuel Development Team
6
 * @license    MIT License
7
 * @copyright  2010 - 2015 Fuel Development Team
8
 * @link       http://fuelphp.com
9
 */
10
11
namespace Fuel\Config;
12
13
use Fuel\Common\Arr;
14
use Fuel\FileSystem\Finder;
15
use Fuel\Common\DataContainer;
16
17
/**
18
 * Holds configuration data
19
 */
20
class Container extends DataContainer implements ContainerInterface
21
{
22
	/**
23
	 * @var string
24
	 */
25
	protected $environment;
26
27
	/**
28
	 * @var Finder
29
	 */
30
	protected $finder;
31
32
	/**
33
	 * @var array
34
	 */
35
	protected $handlers;
36
37
	/**
38
	 * @var string
39
	 */
40
	protected $configFolder = 'config';
41
42
	/**
43
	 * @var string
44
	 */
45
	protected $defaultFormat = 'php';
46
47
	/**
48
	 * @param string $environment
49
	 * @param Finder $finder
50
	 * @param string $defaultFormat
51
	 */
52 15
	public function __construct($environment = null, $finder = null, $defaultFormat = 'php')
53
	{
54 15
		if ($environment instanceof Finder)
55
		{
56 1
			$finder = $environment;
57 1
			$environment = null;
58
		}
59
60 15
		if ( ! $finder)
61
		{
62 14
			$finder = new Finder();
63
		}
64
65 15
		if ($environment)
0 ignored issues
show
Bug Best Practice introduced by
The expression $environment of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
66
		{
67 3
			$this->setEnvironment($environment);
68
		}
69
70 15
		$this->defaultFormat = $defaultFormat;
71 15
		$this->finder = $finder;
72 15
	}
73
74
	/**
75
	 * Returns the default format
76
	 *
77
	 * @return string
78
	 */
79 1
	public function getDefaultFormat()
80
	{
81 1
		return $this->defaultFormat;
82
	}
83
84
	/**
85
	 * Sets the default format
86
	 *
87
	 * @param string $format
88
	 */
89 1
	public function setDefaultFormat($format)
90
	{
91 1
		$this->defaultFormat = $format;
92 1
	}
93
94
	/**
95
	 * Ensures a default config format
96
	 *
97
	 * @param string $file
98
	 *
99
	 * @return string
100
	 */
101 10
	public function ensureDefaultFormat($file)
102
	{
103 10
		if ( ! pathinfo($file, PATHINFO_EXTENSION))
104
		{
105 6
			$file .= '.'.$this->defaultFormat;
106
		}
107
108 10
		return empty($this->configFolder) ? $file : $this->configFolder.DIRECTORY_SEPARATOR.$file;
109
	}
110
111
	/**
112
	 * Returns the environment
113
	 *
114
	 * @return string
115
	 */
116 1
	public function getEnvironment()
117
	{
118 1
		return $this->environment;
119
	}
120
121
	/**
122
	 * Sets the environment
123
	 *
124
	 * @param string $environment
125
	 */
126 3
	public function setEnvironment($environment)
127
	{
128 3
		if ($environment)
129
		{
130 3
			$environment = trim($environment, DIRECTORY_SEPARATOR);
131
		}
132
133 3
		$this->environment = $environment;
134 3
	}
135
136
	/**
137
	 * Unloads a config group
138
	 *
139
	 * @param string $group
140
	 */
141 1
	public function unload($group)
142
	{
143 1
		$this->delete($group);
144 1
	}
145
146
	/**
147
	 * Reloads a group
148
	 *
149
	 * @param string         $name
150
	 * @param string|boolean $group
151
	 *
152
	 * @return array|null
153
	 */
154 1
	public function reload($name, $group = true)
155
	{
156 1
		if ($group === true)
157
		{
158 1
			$group = pathinfo($name, PATHINFO_FILENAME);
159
		}
160
161 1
		$this->delete($group);
162
163 1
		return $this->load($name, $group);
164
	}
165
166
	/**
167
	 * Loads a config file
168
	 *
169
	 * @param string              $name
170
	 * @param null|string|boolean $group
171
	 *
172
	 * @return array|null
173
	 */
174 8
	public function load($name, $group = null)
175
	{
176 8
		if ($group === true)
177
		{
178 6
			$group = pathinfo($name, PATHINFO_FILENAME);
179
		}
180
181 8
		if ($group and $cached = $this->get($group))
182
		{
183 1
			return $cached;
184
		}
185
186 8
		$name = $this->ensureDefaultFormat($name);
187 8
		$paths = $this->finder->findAllFiles($name);
188
189 8
		if (empty($paths))
190
		{
191 1
			return false;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return false; (false) is incompatible with the return type declared by the interface Fuel\Config\ContainerInterface::load of type array|null.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
192
		}
193
194 7
		$config = array();
195
196 7
		foreach ($paths as $path)
0 ignored issues
show
The expression $paths of type array|string is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
197
		{
198 7
			$extension = pathinfo($path, PATHINFO_EXTENSION);
199 7
			$handler = $this->getHandler($extension);
200 7
			$config = Arr::merge($config, $handler->load($path));
201
		}
202
203 7
		if ($group)
204
		{
205 7
			$this->set($group, $config);
206
		}
207
		elseif ($group === null)
208
		{
209
			$this->merge($config);
210
		}
211
212 7
		return $config;
213
	}
214
215
	/**
216
	 * Stores a config file
217
	 *
218
	 * @param string      $group
219
	 * @param string|null $destination
220
	 *
221
	 * @throws \RuntimeException
222
	 */
223 6
	public function save($group, $destination = null)
224
	{
225 6
		if ($destination === null)
226
		{
227 2
			$destination = $group;
228
		}
229
230 6
		if ( ! $this->has($group))
231
		{
232 1
			throw new \RuntimeException('Unable to save non-existig config group: '.$group);
233
		}
234
235 5
		$destination = $this->ensureDefaultFormat($destination);
236 5
		$format = pathinfo($destination, PATHINFO_EXTENSION);
237 5
		$handler = $this->getHandler($format);
238 5
		$data = $this->get($group);
239 5
		$output = $handler->format($data);
240 4
		$path = $this->findDestination($destination);
241
242 4
		if ( ! $path)
243
		{
244 1
			throw new \RuntimeException(sprintf('Could not save group "%" as "%s".', $group, $destination));
245
		}
246
247 3
		return file_put_contents($path, $output);
248
	}
249
250
	/**
251
	 * Finds a config file
252
	 *
253
	 * @param string $destination
254
	 *
255
	 * @return string
256
	 */
257 5
	public function findDestination($destination)
258
	{
259 5
		if (is_file($destination))
260
		{
261 1
			return $destination;
262
		}
263
264 5
		if ($location = $this->finder->findFileReversed($destination))
265
		{
266 1
			return $location;
267
		}
268
269 5
		$paths = $this->finder->getPaths();
270
271 5
		if (empty($paths))
272
		{
273 2
			return false;
274
		}
275
276 3
		$last = end($paths);
277
278 3
		return $last.ltrim($destination, DIRECTORY_SEPARATOR);
279
	}
280
281
	/**
282
	 * Retrieves the handler for a file type
283
	 *
284
	 * @param string $extension
285
	 *
286
	 * @return Handler
287
	 *
288
	 * @throws \RuntimeException
289
	 */
290 11
	public function getHandler($extension)
291
	{
292 11
		if (isset($this->handlers[$extension]))
293
		{
294 6
			return $this->handlers[$extension];
295
		}
296
297 10
		$class = 'Fuel\Config\Handler\\'.ucfirst($extension);
298
299 10
		if ( ! class_exists($class, true))
300
		{
301 1
			throw new \RuntimeException('Could not find config handler for extension: '.$extension);
302
		}
303
304 9
		$handler = new $class;
305 9
		$this->handlers[$extension] = $handler;
306
307 9
		return $handler;
308
	}
309
310
	/**
311
	 * Sets a handler for an extension
312
	 *
313
	 * @param string  $extension
314
	 * @param Handler $handler
0 ignored issues
show
There is no parameter named $handler. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
315
	 */
316 1
	public function setHandler($extension, Handler $loader)
317
	{
318 1
		$this->handlers[$extension] = $loader;
319 1
	}
320
321
	/**
322
	 * Sets the config folder
323
	 *
324
	 * @param string $folder
325
	 */
326 6
	public function setConfigFolder($folder)
327
	{
328 6
		$this->configFolder = rtrim($folder, DIRECTORY_SEPARATOR);
329 6
	}
330
331
	/**
332
	 * Adds a path
333
	 *
334
	 * @param string $path
335
	 *
336
	 * @return $this
337
	 */
338 8 View Code Duplication
	public function addPath($path)
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
339
	{
340 8
		$path = rtrim($path, DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR;
341 8
		$this->finder->addPath($path);
342
343 8
		if ($this->environment)
344
		{
345 2
			$this->finder->addPath($path.$this->environment);
346
		}
347
348 8
		return $this;
349
	}
350
351
	/**
352
	 * Adds paths to look in
353
	 *
354
	 * @param array $paths
355
	 */
356 1
	public function addPaths(array $paths)
357
	{
358 1
		array_map(array($this, 'addPath'), $paths);
359 1
	}
360
361
	/**
362
	 * Removes a path
363
	 *
364
	 * @param string $path
365
	 */
366 1 View Code Duplication
	public function removePath($path)
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
367
	{
368 1
		$path = rtrim($path, DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR;
369 1
		$this->finder->removePath($path);
370
371 1
		if ($this->environment)
372
		{
373 1
			$this->finder->removePath($path.$this->environment);
374
		}
375 1
	}
376
377
	/**
378
	 * Removes paths
379
	 *
380
	 * @param array $paths
381
	 */
382 1
	public function removePaths(array $paths)
383
	{
384 1
		array_map(array($this, 'removePath'), $paths);
385 1
	}
386
}
387