Passed
Push — master ( b3870e...b0b632 )
by Patrick
02:01
created

NetworkRule   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 31
dl 0
loc 76
rs 10
c 1
b 0
f 0
wmc 7

2 Methods

Rating   Name   Duplication   Size   Complexity  
B isInNetwork() 0 37 6
A __construct() 0 4 1
1
<?php
2
3
namespace Trapdirector\Plugins;
4
5
use Trapdirector\PluginTemplate;
6
use Exception;
7
8
/**
9
 * Network functions plugin
10
 * Used in rules to to load and execute plugins
11
 * Default directory for plugins is : ../Plugins/
12
 *
13
 * @license GPL
14
 * @author Patrick Proy
15
 * @package trapdirector
16
 * @subpackage plugins
17
 */
18
class NetworkRule extends PluginTemplate
19
{        
20
    /** @var string $description Description of plugin */
21
    public $description='Network functions to use into rules
22
test test test';
23
    
24
    /** @var array[] $functions Functions of this plugin for rule eval. 
25
     * If no functions are declared, set to empty array
26
    */
27
    public $functions=array(
28
        'inNetwork' => array( // The name of the function 
29
            'function'      =>  'isInNetwork', // Name of the function in rules
30
            'params'        =>  '<IP to test>,<Network IP>,<Network mask (CIDR)>', // parameters description
31
            'description'   =>  'Test if IP is in network, ex : __inNetwork(192.168.123.5,192.168.123.0,24) returns true
32
Does not work with IPV6' // Description (can be multiline).
33
        )
34
    );
35
    
36
    /** @var boolean $catchAllTraps Set to true if all traps will be sent to the plugin */
37
    public $catchAllTraps=false;
38
    
39
40
    /**
41
     * Constructor. Can throw exceptions on error, but no logging at this point.
42
     * @throws \Exception
43
     * @return \Trapdirector\Plugins\NetworkRule
44
     */
45
    function __construct()
46
    {
47
        $this->name=basename(__FILE__,'.php');
48
        return $this;
49
    }
50
    
51
    /**
52
     * 
53
     * @param array $params Function parameters
54
     * @throws Exception
55
     * @return bool Evaluation 
56
     */
57
    public function isInNetwork(array $params) : bool
58
    {
59
        $this->log('Function params : ' . print_r($params,true),DEBUG);
60
        if (count($params)!=3)
61
        {
62
            throw new Exception('Invalid number of parameters : ' . array_count_values($params));
0 ignored issues
show
Bug introduced by
Are you sure array_count_values($params) of type array can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

62
            throw new Exception('Invalid number of parameters : ' . /** @scrutinizer ignore-type */ array_count_values($params));
Loading history...
63
        }
64
        
65
        $ip = $params[0];
66
        $net = $params[1];
67
        $masq = $params[2];
68
        
69
        $this->log('#'. $ip . '# / #' . $net . '# / #' . $masq,DEBUG);
70
        
71
        $ip2 = ip2long($ip);
72
        $net2 = ip2long($net);
73
        
74
        if ($ip2 === false )
75
        {
76
            $this->log('Invalid IP : #' . $ip.'#',WARN);
77
            throw new Exception('Invalid IP');
78
        }
79
        if ($net2 === false)
80
        {
81
            $this->log('Invalid network',WARN);
82
            throw new Exception('Invalid net');
83
        }
84
        if ($masq<1 || $masq > 32)
85
        {
86
            $this->log('Invalid masq',WARN);
87
            throw new Exception('Invalid net masq');
88
        }
89
        // $range is in IP/CIDR format eg 127.0.0.1/24
90
91
        $masq = pow( 2, ( 32 - $masq ) ) - 1;
92
        $masq = ~ $masq;
93
        return ( ( $ip2 & $masq ) == ( $net2 & $masq ) );
94
        
95
    }
96
}
97
98
99