Completed
Push — master ( 2f676f...ba4b05 )
by Patrick
02:03
created

TrapDBQuery::lastModification()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 8
nc 2
nop 0
dl 0
loc 11
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Icinga\Module\Trapdirector\TrapsActions;
4
5
use Exception;
6
use Zend_Db_Expr;
7
use Zend_Db_Adapter_Abstract;
8
use Zend_Db_Select;
1 ignored issue
show
Bug introduced by
The type Zend_Db_Select 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...
9
use Icinga\Module\Trapdirector\TrapsController;
10
11
/**
12
 * Database queries for UI (on Trap database)
13
 * Calling class must implement : getTrapCtrl , getDbConn
14
 * @license GPL
15
 * @author Patrick Proy
16
 * @package trapdirector
17
 * @subpackage UI
18
 *
19
 */
20
trait TrapDBQuery
21
{
22
    
23
    /** @return TrapsController */
24
    abstract protected function getTrapCtrl();
25
26
    /** @return Zend_Db_Adapter_Abstract : returns DB connexion or null on error */
27
    abstract public function getDbConn();
28
    
29
    /** Add handler rule in traps DB
30
     *	@param array $params : array(<db item>=><value>)
31
     *	@return int inserted id
32
     */
33
    public function addHandlerRule($params)
34
    {
35
        // TODO Check for rule consistency
36
        
37
        $dbConn = $this->getDbConn();
38
        if ($dbConn === null) throw new \ErrorException('uncatched db error');
39
        // Add last modified date = creation date and username
40
        $params['created'] = new Zend_Db_Expr('NOW()');
41
        $params['modified'] = new 	Zend_Db_Expr('NOW()');
42
        $params['modifier'] = $this->getTrapCtrl()->Auth()->getUser()->getUsername();
43
        
44
        $query=$dbConn->insert(
45
            $this->getTrapCtrl()->getModuleConfig()->getTrapRuleName(),
46
            $params
47
            );
48
        if($query==false)
49
        {
50
            return null;
51
        }
52
        return $dbConn->lastInsertId();
53
    }
54
    
55
    /** Update handler rule in traps DB
56
     *	@param array $params : (<db item>=><value>)
57
     *   @param integer $ruleID : rule id in db
58
     *	@return array affected rows
59
     */
60
    public function updateHandlerRule($params,$ruleID)
61
    {
62
        // TODO Check for rule consistency
63
        $dbConn = $this->getDbConn();
64
        if ($dbConn === null) throw new \ErrorException('uncatched db error');
65
        // Add last modified date = creation date and username
66
        $params['modified'] = new 	Zend_Db_Expr('NOW()');
67
        $params['modifier'] = $this->getTrapCtrl()->Auth()->getUser()->getUsername();
68
        
69
        $numRows=$dbConn->update(
70
            $this->getTrapCtrl()->getModuleConfig()->getTrapRuleName(),
71
            $params,
72
            'id='.$ruleID
73
            );
74
        return $numRows;
75
    }
76
    
77
    /** Delete rule by id
78
     *	@param int $ruleID rule id
79
     */
80
    public function deleteRule($ruleID)
81
    {
82
        if (!preg_match('/^[0-9]+$/',$ruleID)) { throw new Exception('Invalid id');  }
83
        
84
        $dbConn = $this->getDbConn();
85
        if ($dbConn === null) throw new \ErrorException('uncatched db error');
86
        
87
        $query=$dbConn->delete(
88
            $this->getTrapCtrl()->getModuleConfig()->getTrapRuleName(),
89
            'id='.$ruleID
90
            );
91
        return $query;
92
    }
93
94
    /**
95
     * Get last trap rule table modification
96
     * @throws \ErrorException
97
     * @return Zend_Db_Select
98
     */
99
    public function lastModification()
100
    {
101
        $dbConn = $this->getDbConn();
102
        if ($dbConn === null) throw new \ErrorException('uncatched db error');
103
        
104
        $query = $dbConn->select()
105
        ->from(
106
            $this->getTrapCtrl()->getModuleConfig()->getTrapRuleName(),
107
            array('lastModified'=>'UNIX_TIMESTAMP(MAX(modified))'));
108
        $returnRow=$dbConn->fetchRow($query);
109
        return $returnRow->lastmodified;
110
    }
111
    
112
    /** Delete trap by ip & oid
113
     *	@param $ipAddr string source IP (v4 or v6)
114
     *	@param $oid string oid
115
     */
116
    public function deleteTrap($ipAddr,$oid)
117
    {
118
        
119
        $dbConn = $this->getDbConn();
120
        if ($dbConn === null) throw new \ErrorException('uncatched db error');
121
        $condition=null;
122
        if ($ipAddr != null)
123
        {
124
            $condition="source_ip='$ipAddr'";
125
        }
126
        if ($oid != null)
127
        {
128
            $condition=($condition===null)?'':$condition.' AND ';
129
            $condition.="trap_oid='$oid'";
130
        }
131
        if($condition === null) return null;
132
        $query=$dbConn->delete(
133
            $this->getTrapCtrl()->getModuleConfig()->getTrapTableName(),
134
            $condition
135
            );
136
        // TODO test ret code etc...
137
        return $query;
138
    }
139
    
140
    
141
    /** count trap by ip & oid
142
     *	@param $ipAddr string source IP (v4 or v6)
143
     *	@param $oid string oid
144
     */
145
    public function countTrap($ipAddr,$oid)
146
    {
147
        
148
        $dbConn = $this->getDbConn();
149
        if ($dbConn === null) throw new \ErrorException('uncatched db error');
150
        
151
        $condition=null;
152
        if ($ipAddr != null)
153
        {
154
            $condition="source_ip='$ipAddr'";
155
        }
156
        if ($oid != null)
157
        {
158
            $condition=($condition===null)?'':$condition.' AND ';
159
            $condition.="trap_oid='$oid'";
160
        }
161
        if($condition === null) return 0;
162
        $query=$dbConn->select()
163
            ->from(
164
                $this->getTrapCtrl()->getModuleConfig()->getTrapTableName(),
165
                array('num'=>'count(*)'))
166
            ->where($condition);
167
        $returnRow=$dbConn->fetchRow($query);
168
        return $returnRow->num;
169
    }
170
    
171
    /** get configuration value
172
     *	@param string $element : configuration name in db
173
     */
174
    public function getDBConfigValue($element)
175
    {
176
        
177
        $dbConn = $this->getDbConn();
178
        if ($dbConn === null) throw new \ErrorException('uncatched db error');
179
        
180
        $query=$dbConn->select()
181
        ->from(
182
            $this->getTrapCtrl()->getModuleConfig()->getDbConfigTableName(),
183
            array('value'=>'value'))
184
            ->where('name=?',$element);
185
            $returnRow=$dbConn->fetchRow($query);
186
            if ($returnRow==null)  // value does not exists
187
            {
188
                $default=$this->getTrapCtrl()->getModuleConfig()->getDBConfigDefaults();
189
                if ( ! isset($default[$element])) return null; // no default and not value
190
                
191
                $this->addDBConfigValue($element,$default[$element]);
192
                return $default[$element];
193
            }
194
            if ($returnRow->value == null) // value id empty
195
            {
196
                $default=$this->getTrapCtrl()->getModuleConfig()->getDBConfigDefaults();
197
                if ( ! isset($default[$element])) return null; // no default and not value
198
                $this->setDBConfigValue($element,$default[$element]);
199
                return $default[$element];
200
            }
201
            return $returnRow->value;
202
    }
203
    
204
    /** add configuration value
205
     *	@param string $element : name of config element
206
     *   @param string $value : value
207
     */
208
    
209
    public function addDBConfigValue($element,$value)
210
    {
211
        
212
        $dbConn = $this->getDbConn();
213
        if ($dbConn === null) throw new \ErrorException('uncatched db error');
214
        
215
        $query=$dbConn->insert(
216
            $this->getTrapCtrl()->getModuleConfig()->getDbConfigTableName(),
217
            array(
218
                'name' => $element,
219
                'value'=>$value
220
            )
221
            );
222
        return $query;
223
    }
224
    
225
    /** set configuration value
226
     *	@param string $element : name of config element
227
     *   @param string $value : value
228
     */
229
    public function setDBConfigValue($element,$value)
230
    {
231
        
232
        $dbConn = $this->getDbConn();
233
        if ($dbConn === null) throw new \ErrorException('uncatched db error');
234
        
235
        $query=$dbConn->update(
236
            $this->getTrapCtrl()->getModuleConfig()->getDbConfigTableName(),
237
            array('value'=>$value),
238
            'name=\''.$element.'\''
239
            );
240
        return $query;
241
    }
242
    
243
    
244
}