Passed
Push — master ( 57a389...e15d84 )
by Patrick
02:12
created

TrapsController::getServicesByHostGroupid()   B

Complexity

Conditions 8
Paths 14

Size

Total Lines 45
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
cc 8
eloc 28
c 4
b 0
f 0
nc 14
nop 1
dl 0
loc 45
rs 8.4444
1
<?php
2
3
namespace Icinga\Module\Trapdirector;
4
5
use Icinga\Web\Controller;
6
7
use Icinga\Data\Paginatable;
8
9
//use Exception;
10
11
use Icinga\Module\Trapdirector\Config\TrapModuleConfig;
12
use Icinga\Module\Trapdirector\Tables\TrapTableList;
13
use Icinga\Module\Trapdirector\Tables\TrapTableHostList;
14
use Icinga\Module\Trapdirector\Tables\HandlerTableList;
15
use Icinga\Module\Trapdirector\Config\MIBLoader;
16
use Icinga\Module\Trapdirector\TrapsActions\UIDatabase;
17
18
use Trapdirector\Trap;
19
20
use Icinga\Data\ConfigObject;
1 ignored issue
show
Bug introduced by
The type Icinga\Data\ConfigObject was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
21
22
class TrapsController extends Controller
23
{
24
	/** @var TrapModuleConfig $moduleConfig TrapModuleConfig instance */
25
	protected $moduleConfig;
26
	/** @var TrapTableList $trapTableList (by date)*/
27
	protected $trapTableList;
28
	/** @var TrapTableHostList $trapTableHostList TrapTableList (by hosts)*/
29
	protected $trapTableHostList;
30
	/** @var HandlerTableList $handlerTableList HandlerTableList instance*/
31
	protected $handlerTableList;
32
	/** @var ConfigObject $trapDB Trap database */
33
	protected $trapDB;
34
	/** @var ConfigObject $icingaDB Icinga IDO database */
35
	protected $icingaDB;
36
	/** @var MIBLoader $MIBData MIBLoader class */
37
	protected $MIBData;
38
	/** @var Trap $trapClass Trap class for bin/trap_class.php */
39
	protected $trapClass;
40
	/** @var UIDatabase $UIDatabase */
41
	protected $UIDatabase;
42
	
43
	
44
	
45
	/** Get instance of TrapModuleConfig class
46
	*	@return TrapModuleConfig
47
	*/
48
	public function getModuleConfig() 
49
	{
50
		if ($this->moduleConfig == Null) 
51
		{
52
			$db_prefix=$this->Config()->get('config', 'database_prefix');
53
			if ($db_prefix === null) 
54
			{
55
				$this->redirectNow('trapdirector/settings?message=No database prefix');
56
			}
57
			$this->moduleConfig = new TrapModuleConfig($db_prefix);
58
		}
59
		return $this->moduleConfig;
60
	}
61
	
62
	/**
63
	 * Get instance of TrapTableList
64
	 * @return \Icinga\Module\Trapdirector\Tables\TrapTableList
65
	 */
66
	public function getTrapListTable() {
67
		if ($this->trapTableList == Null) {
68
			$this->trapTableList = new TrapTableList();
69
			$this->trapTableList->setConfig($this->getModuleConfig());
70
		}
71
		return $this->trapTableList;
72
	}
73
	
74
	/**
75
	 * @return \Icinga\Module\Trapdirector\Tables\TrapTableHostList
76
	 */
77
	public function getTrapHostListTable()
78
	{
79
	    if ($this->trapTableHostList == Null) 
80
		{
81
	        $this->trapTableHostList = new TrapTableHostList();
82
	        $this->trapTableHostList->setConfig($this->getModuleConfig());
83
	    }
84
	    return $this->trapTableHostList;
85
	}
86
	
87
	/**
88
	 * @return \Icinga\Module\Trapdirector\Tables\HandlerTableList
89
	 */
90
	public function getHandlerListTable() 
91
	{
92
		if ($this->handlerTableList == Null) 
93
		{
94
			$this->handlerTableList = new HandlerTableList();
95
			$this->handlerTableList->setConfig($this->getModuleConfig());
96
		}
97
		return $this->handlerTableList;
98
	}	
99
100
	/**
101
	 * @return UIDatabase
102
	 */
103
	public function getUIDatabase()
104
	{
105
	    if ($this->UIDatabase == Null)
106
	    {
107
	        $this->UIDatabase = new UIDatabase($this);
108
	       
109
	    }
110
	    return $this->UIDatabase;
111
	}
112
	
113
    protected function applyPaginationLimits(Paginatable $paginatable, $limit = 25, $offset = null)
114
    {
115
        $limit = $this->params->get('limit', $limit);
116
        $page = $this->params->get('page', $offset);
117
118
        $paginatable->limit($limit, $page > 0 ? ($page - 1) * $limit : 0);
119
120
        return $paginatable;
121
    }	
122
	
123
	public function displayExitError($source,$message)
124
	{	// TODO : check better ways to transmit data (with POST ?)
125
		$this->redirectNow('trapdirector/error?source='.$source.'&message='.$message);
126
	}
127
	
128
	protected function checkReadPermission()
129
	{
130
        if (! $this->Auth()->hasPermission('trapdirector/view')) {
131
            $this->displayExitError('Permissions','No permission fo view content');
132
        }		
133
	}
134
135
	protected function checkConfigPermission()
136
	{
137
        if (! $this->Auth()->hasPermission('trapdirector/config')) {
138
            $this->displayExitError('Permissions','No permission fo configure');
139
        }		
140
	}
141
	
142
    /**
143
     * Check if user has write permission
144
     * @param number $check optional : if set to 1, return true (user has permission) or false instead of displaying error page
145
     * @return boolean : user has permission
146
     */
147
	protected function checkModuleConfigPermission($check=0)
148
	{
149
        if (! $this->Auth()->hasPermission('trapdirector/module_config')) {
150
            if ($check == 0)
151
            {
152
                $this->displayExitError('Permissions','No permission fo configure module');
153
            }
154
            return false;
155
        }
156
        return true;
157
	}
158
159
	/*************************  Trap class get **********************/
160
	public function getTrapClass()
161
	{ // TODO : try/catch here ? or within caller
162
		if ($this->trapClass == null)
163
		{
164
			require_once($this->Module()->getBaseDir() .'/bin/trap_class.php');
165
			$icingaweb2_etc=$this->Config()->get('config', 'icingaweb2_etc');
166
			//$debug_level=4;
167
			$this->trapClass = new Trap($icingaweb2_etc);
168
			//$Trap->setLogging($debug_level,'syslog');
169
		}
170
		return $this->trapClass;
171
	}
172
	
173
	/************************** MIB related **************************/
174
	
175
	/** Get MIBLoader class
176
	*	@return MIBLoader class
177
	*/
178
	protected function getMIB()
179
	{
180
		if ($this->MIBData == null)
181
		{
182
		    $dbConn = $this->getUIDatabase()->getDbConn();
183
		    if ($dbConn === null) throw new \ErrorException('uncatched db error');
184
			$this->MIBData=new MIBLoader(
185
				$this->Config()->get('config', 'snmptranslate'),
186
				$this->Config()->get('config', 'snmptranslate_dirs'),
187
			    $dbConn,
188
				$this->getModuleConfig()
189
			);
190
		}
191
		return $this->MIBData;
192
	}	
193
	
194
	/**************************  Database queries *******************/		
195
	
196
	/** Check if director is installed
197
	*	@return bool true/false
198
	*/
199
	protected function isDirectorInstalled()
200
	{
201
	    $output=array();
202
	    exec('icingacli module list',$output);
203
	    foreach ($output as $line)
204
		{
205
			if (preg_match('/^director .*enabled/',$line))
206
			{
207
				return true;
208
			}
209
		}
210
		return false;
211
	}
212
	
213
}
214
215