JailedPath   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 14
dl 0
loc 62
ccs 16
cts 16
cp 1
rs 10
c 0
b 0
f 0
wmc 6

5 Methods

Rating   Name   Duplication   Size   Complexity  
A hasJailbreakAttempt() 0 3 1
A __construct() 0 7 1
A getJailPath() 0 3 1
A getAbsolutePath() 0 5 2
A getRelativePath() 0 3 1
1
<?php
2
3
/**
4
 * (c) Dennis Meckel
5
 *
6
 * For the full copyright and license information,
7
 * please view the LICENSE file that was distributed with this source code.
8
 */
9
10
namespace Rayne\VirtualPath;
11
12
/**
13
 * A jailed path is a path with a fixed prefix (the jail path)
14
 * and an optional normalised relative suffix path.
15
 *
16
 * `JailedPath` utilises `VirtualPath` for path normalisation
17
 * and detecting jailbreak attempts.
18
 *
19
 * @see VirtualPath
20
 */
21
class JailedPath
22
{
23
    /**
24
     * @var bool
25
     */
26
    private $hasJailbreakAttempt = false;
27
28
    /**
29
     * @var string
30
     */
31
    private $jailPath;
32
33
    /**
34
     * @var string
35
     */
36
    private $relativePath;
37
38
    /**
39
     * @param string $jail Trusted path, e.g. on a local or remote file system.
40
     * @param mixed $path Untrusted user input which gets normalised.
41
     */
42 38
    public function __construct($jail, $path)
43
    {
44 38
        $virtualPath = new VirtualPath($path);
45
46 38
        $this->jailPath = (string)$jail;
47 38
        $this->relativePath = ltrim($virtualPath, '/');
48 38
        $this->hasJailbreakAttempt = !$virtualPath->isTrusted();
49 38
    }
50
51
    /**
52
     * @return string
53
     */
54 38
    public function getAbsolutePath()
55
    {
56 38
        return $this->relativePath === ''
57 38
            ? $this->jailPath
58 38
            : rtrim($this->jailPath, '/') . '/' . $this->relativePath;
59
    }
60
61
    /**
62
     * @return string
63
     */
64 38
    public function getJailPath()
65
    {
66 38
        return $this->jailPath;
67
    }
68
69
    /**
70
     * @return string
71
     */
72 38
    public function getRelativePath()
73
    {
74 38
        return $this->relativePath;
75
    }
76
77
    /**
78
     * @return bool
79
     */
80 38
    public function hasJailbreakAttempt()
81
    {
82 38
        return $this->hasJailbreakAttempt;
83
    }
84
}
85