Rfi   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 14
lcom 1
cbo 1
dl 0
loc 70
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
B match() 0 42 11
A applyExceptions() 0 13 1
A checkContent() 0 10 2
1
<?php
2
3
namespace Spinzar\Firewall\Middleware;
4
5
use Spinzar\Firewall\Abstracts\Middleware;
6
7
class Rfi extends Middleware
8
{
9
    public function match($pattern, $input)
10
    {
11
        $result = false;
12
13
        if (!is_array($input) && !is_string($input)) {
14
            return false;
15
        }
16
17
        if (!is_array($input)) {
18
            if (!$result = preg_match($pattern, $this->applyExceptions($input))) {
19
                return false;
20
            }
21
22
            return $this->checkContent($result);
23
        }
24
25
        foreach ($input as $key => $value) {
26
            if (is_array($value)) {
27
                if (!$result = $this->match($pattern, $value)) {
28
                    continue;
29
                }
30
31
                break;
32
            }
33
34
            if (!$this->isInput($key)) {
35
                continue;
36
            }
37
38
            if (!$result = preg_match($pattern, $this->applyExceptions($value))) {
39
                continue;
40
            }
41
42
            if (!$this->checkContent($result)) {
43
                continue;
44
            }
45
46
            break;
47
        }
48
49
        return $result;
50
    }
51
    
52
    protected function applyExceptions($string)
53
    {
54
        $exceptions = config('firewall.middleware.' . $this->middleware . '.exceptions');
55
56
        $domain = $this->request->getHost();
57
58
        $exceptions[] = 'http://' . $domain;
59
        $exceptions[] = 'https://' . $domain;
60
        $exceptions[] = 'http://&';
61
        $exceptions[] = 'https://&';
62
63
        return str_replace($exceptions, '', $string);
64
    }
65
    
66
    protected function checkContent($value)
67
    {
68
        $contents = @file_get_contents($value);
69
70
        if (!empty($contents)) {
71
            return (strstr($contents, '<?php') !== false);
72
        }
73
        
74
        return false;
75
    }
76
}
77