JailedPath::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 2
dl 0
loc 7
ccs 5
cts 5
cp 1
crap 1
rs 10
c 0
b 0
f 0
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