Passed
Push — master ( 84c1e8...225d38 )
by Patrick
02:08
created

NetworkRule::testParam()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 4
c 0
b 0
f 0
nc 3
nop 1
dl 0
loc 8
rs 10
1
<?php
2
3
namespace Trapdirector\Plugins;
4
5
use Trapdirector\PluginTemplate;
6
use Exception;
7
8
/**
9
 * Network functions plugin
10
 * This class is declaring a single function : inNetwork
11
 * If something goes wrong, just throw exception as it will be catched by caller
12
 * Logging is provided with $this->log(<message>,<level>) with level = DEBUG|INFO|WARN|CRIT.
13
 * A CRIT level throws an exception from the log function.
14
 * 
15
 * Default directory for plugins is : ../Plugins/
16
 *
17
 * @license GPL
18
 * @author Patrick Proy
19
 * @package trapdirector
20
 * @subpackage plugins
21
 */
22
class NetworkRule extends PluginTemplate
23
{        
24
    /** @var string $description Description of plugin */
25
    public $description='Network functions to use into rules
26
test test test';
27
    
28
    /** @var array[] $functions Functions of this plugin for rule eval. 
29
     * If no functions are declared, set to empty array
30
     * $functions[<name>]['function'] : Name of the function to be called in this class
31
     * $functions[<name>]['params'] : Description of input parameters of function.
32
     * $functions[<name>]['description'] : Description. Can be multiline.
33
    */
34
    public $functions=array(
35
        'inNetwork' => array( // The name of the function in rules
36
            'function'      =>  'isInNetwork', // Name of the function 
37
            'params'        =>  '<IP to test>,<Network IP>,<Network mask (CIDR)>', // parameters description
38
            'description'   =>  'Test if IP is in network, ex : __inNetwork(192.168.123.5,192.168.123.0,24) returns true
39
Does not work with IPV6' // Description (can be multiline).
40
        ),
41
        'test' => array( // The name of the function in rules
42
            'function'      =>  'testParam', // Name of the function
43
            'params'        =>  '<boolean to return as string>', // parameters description
44
            'description'   =>  'Returns value passed as argument' // Description (can be multiline).
45
        )
46
    );
47
    
48
    /** @var boolean $catchAllTraps Set to true if all traps will be sent to the plugin NOT IMPLEMENTED */
49
    public $catchAllTraps=false;
50
    
51
    /** @var boolean $processTraps Set to true if plugins can handle traps NOT IMPLEMENTED */
52
    public $processTraps=false;
53
    
54
    /**
55
     * Constructor. Can throw exceptions on error, but no logging at this point.
56
     * @throws \Exception
57
     * @return \Trapdirector\Plugins\NetworkRule
58
     */
59
    function __construct()
60
    {
61
        $this->name=basename(__FILE__,'.php');
62
        return $this;
63
    }
64
    
65
    /**
66
     * Function called by trapdirector if found in rules
67
     * Parameters check has to be done in function.
68
     * @param array $params Function parameters
69
     * @throws Exception
70
     * @return bool Evaluation 
71
     */
72
    public function isInNetwork(array $params) : bool
73
    {
74
        // Check param numbers and thrown exception if not correct.
75
        if (count($params)!=3)
76
        {
77
            throw new Exception('Invalid number of parameters : ' . count($params));
78
        }
79
        
80
        $ip = $params[0];
81
        $net = $params[1];
82
        $masq = $params[2];
83
        
84
        
85
        $this->log('#'. $ip . '# / #' . $net . '# / #' . $masq,DEBUG);
86
        
87
        $ip2 = ip2long($ip);
88
        $net2 = ip2long($net);
89
        
90
        if ($ip2 === false )
91
        {
92
            $this->log('Invalid IP : #' . $ip.'#',WARN);
93
            throw new Exception('Invalid IP');
94
        }
95
        if ($net2 === false)
96
        {
97
            $this->log('Invalid network',WARN);
98
            throw new Exception('Invalid net');
99
        }
100
        if ($masq<1 || $masq > 32)
101
        {
102
            $this->log('Invalid masq',WARN);
103
            throw new Exception('Invalid net masq');
104
        }
105
        // $range is in IP/CIDR format eg 127.0.0.1/24
106
107
        $masq = pow( 2, ( 32 - $masq ) ) - 1;
108
        $masq = ~ $masq;
109
        return ( ( $ip2 & $masq ) == ( $net2 & $masq ) );
110
        
111
    }
112
    
113
    public function testParam(array $param)
114
    {
115
        if (count($param)!=1)
116
        {
117
            throw new Exception('Invalid number of parameters : ' . count($param));
118
        }
119
        if ($param[0] == 'true') return true;
120
        return false;
121
    }
122
}
123
124
125