Passed
Push — master ( d44cfc...57a389 )
by Patrick
02:07
created
library/Trapdirector/TrapsActions/TrapDBQuery.php 3 patches
Indentation   +171 added lines, -171 removed lines patch added patch discarded remove patch
@@ -17,201 +17,201 @@
 block discarded – undo
17 17
 trait TrapDBQuery
18 18
 {
19 19
     
20
-    /** Add handler rule in traps DB
21
-     *	@param array $params : array(<db item>=><value>)
22
-     *	@return int inserted id
23
-     */
24
-    public function addHandlerRule($params)
25
-    {
26
-        // TODO Check for rule consistency
20
+	/** Add handler rule in traps DB
21
+	 *	@param array $params : array(<db item>=><value>)
22
+	 *	@return int inserted id
23
+	 */
24
+	public function addHandlerRule($params)
25
+	{
26
+		// TODO Check for rule consistency
27 27
         
28
-        $dbConn = $this->getDbConn();
29
-        if ($dbConn === null) throw new \ErrorException('uncatched db error');
30
-        // Add last modified date = creation date and username
31
-        $params['created'] = new Zend_Db_Expr('NOW()');
32
-        $params['modified'] = new 	Zend_Db_Expr('NOW()');
33
-        $params['modifier'] = $this->Auth()->getUser()->getUsername();
28
+		$dbConn = $this->getDbConn();
29
+		if ($dbConn === null) throw new \ErrorException('uncatched db error');
30
+		// Add last modified date = creation date and username
31
+		$params['created'] = new Zend_Db_Expr('NOW()');
32
+		$params['modified'] = new 	Zend_Db_Expr('NOW()');
33
+		$params['modifier'] = $this->Auth()->getUser()->getUsername();
34 34
         
35
-        $query=$dbConn->insert(
36
-            $this->getTrapCtrl()->getModuleConfig()->getTrapRuleName(),
37
-            $params
38
-            );
39
-        if($query==false)
40
-        {
41
-            return null;
42
-        }
43
-        return $dbConn->lastInsertId();
44
-    }
35
+		$query=$dbConn->insert(
36
+			$this->getTrapCtrl()->getModuleConfig()->getTrapRuleName(),
37
+			$params
38
+			);
39
+		if($query==false)
40
+		{
41
+			return null;
42
+		}
43
+		return $dbConn->lastInsertId();
44
+	}
45 45
     
46
-    /** Update handler rule in traps DB
47
-     *	@param array $params : (<db item>=><value>)
48
-     *   @param integer $ruleID : rule id in db
49
-     *	@return array affected rows
50
-     */
51
-    public function updateHandlerRule($params,$ruleID)
52
-    {
53
-        // TODO Check for rule consistency
54
-        $dbConn = $this->getDbConn();
55
-        if ($dbConn === null) throw new \ErrorException('uncatched db error');
56
-        // Add last modified date = creation date and username
57
-        $params['modified'] = new 	Zend_Db_Expr('NOW()');
58
-        $params['modifier'] = $this->getTrapCtrl()->Auth()->getUser()->getUsername();
46
+	/** Update handler rule in traps DB
47
+	 *	@param array $params : (<db item>=><value>)
48
+	 *   @param integer $ruleID : rule id in db
49
+	 *	@return array affected rows
50
+	 */
51
+	public function updateHandlerRule($params,$ruleID)
52
+	{
53
+		// TODO Check for rule consistency
54
+		$dbConn = $this->getDbConn();
55
+		if ($dbConn === null) throw new \ErrorException('uncatched db error');
56
+		// Add last modified date = creation date and username
57
+		$params['modified'] = new 	Zend_Db_Expr('NOW()');
58
+		$params['modifier'] = $this->getTrapCtrl()->Auth()->getUser()->getUsername();
59 59
         
60
-        $numRows=$dbConn->update(
61
-            $this->getTrapCtrl()->getModuleConfig()->getTrapRuleName(),
62
-            $params,
63
-            'id='.$ruleID
64
-            );
65
-        return $numRows;
66
-    }
60
+		$numRows=$dbConn->update(
61
+			$this->getTrapCtrl()->getModuleConfig()->getTrapRuleName(),
62
+			$params,
63
+			'id='.$ruleID
64
+			);
65
+		return $numRows;
66
+	}
67 67
     
68
-    /** Delete rule by id
69
-     *	@param int $ruleID rule id
70
-     */
71
-    public function deleteRule($ruleID)
72
-    {
73
-        if (!preg_match('/^[0-9]+$/',$ruleID)) { throw new Exception('Invalid id');  }
68
+	/** Delete rule by id
69
+	 *	@param int $ruleID rule id
70
+	 */
71
+	public function deleteRule($ruleID)
72
+	{
73
+		if (!preg_match('/^[0-9]+$/',$ruleID)) { throw new Exception('Invalid id');  }
74 74
         
75
-        $dbConn = $this->getDbConn();
76
-        if ($dbConn === null) throw new \ErrorException('uncatched db error');
75
+		$dbConn = $this->getDbConn();
76
+		if ($dbConn === null) throw new \ErrorException('uncatched db error');
77 77
         
78
-        $query=$dbConn->delete(
79
-            $this->getTrapCtrl()->getModuleConfig()->getTrapRuleName(),
80
-            'id='.$ruleID
81
-            );
82
-        return $query;
83
-    }
78
+		$query=$dbConn->delete(
79
+			$this->getTrapCtrl()->getModuleConfig()->getTrapRuleName(),
80
+			'id='.$ruleID
81
+			);
82
+		return $query;
83
+	}
84 84
     
85
-    /** Delete trap by ip & oid
86
-     *	@param $ipAddr string source IP (v4 or v6)
87
-     *	@param $oid string oid
88
-     */
89
-    public function deleteTrap($ipAddr,$oid)
90
-    {
85
+	/** Delete trap by ip & oid
86
+	 *	@param $ipAddr string source IP (v4 or v6)
87
+	 *	@param $oid string oid
88
+	 */
89
+	public function deleteTrap($ipAddr,$oid)
90
+	{
91 91
         
92
-        $dbConn = $this->getDbConn();
93
-        if ($dbConn === null) throw new \ErrorException('uncatched db error');
94
-        $condition=null;
95
-        if ($ipAddr != null)
96
-        {
97
-            $condition="source_ip='$ipAddr'";
98
-        }
99
-        if ($oid != null)
100
-        {
101
-            $condition=($condition===null)?'':$condition.' AND ';
102
-            $condition.="trap_oid='$oid'";
103
-        }
104
-        if($condition === null) return null;
105
-        $query=$dbConn->delete(
106
-            $this->getTrapCtrl()->getModuleConfig()->getTrapTableName(),
107
-            $condition
108
-            );
109
-        // TODO test ret code etc...
110
-        return $query;
111
-    }
92
+		$dbConn = $this->getDbConn();
93
+		if ($dbConn === null) throw new \ErrorException('uncatched db error');
94
+		$condition=null;
95
+		if ($ipAddr != null)
96
+		{
97
+			$condition="source_ip='$ipAddr'";
98
+		}
99
+		if ($oid != null)
100
+		{
101
+			$condition=($condition===null)?'':$condition.' AND ';
102
+			$condition.="trap_oid='$oid'";
103
+		}
104
+		if($condition === null) return null;
105
+		$query=$dbConn->delete(
106
+			$this->getTrapCtrl()->getModuleConfig()->getTrapTableName(),
107
+			$condition
108
+			);
109
+		// TODO test ret code etc...
110
+		return $query;
111
+	}
112 112
     
113 113
     
114
-    /** count trap by ip & oid
115
-     *	@param $ipAddr string source IP (v4 or v6)
116
-     *	@param $oid string oid
117
-     */
118
-    public function countTrap($ipAddr,$oid)
119
-    {
114
+	/** count trap by ip & oid
115
+	 *	@param $ipAddr string source IP (v4 or v6)
116
+	 *	@param $oid string oid
117
+	 */
118
+	public function countTrap($ipAddr,$oid)
119
+	{
120 120
         
121
-        $dbConn = $this->getDbConn();
122
-        if ($dbConn === null) throw new \ErrorException('uncatched db error');
121
+		$dbConn = $this->getDbConn();
122
+		if ($dbConn === null) throw new \ErrorException('uncatched db error');
123 123
         
124
-        $condition=null;
125
-        if ($ipAddr != null)
126
-        {
127
-            $condition="source_ip='$ipAddr'";
128
-        }
129
-        if ($oid != null)
130
-        {
131
-            $condition=($condition===null)?'':$condition.' AND ';
132
-            $condition.="trap_oid='$oid'";
133
-        }
134
-        if($condition === null) return 0;
135
-        $query=$dbConn->select()
136
-            ->from(
137
-                $this->getTrapCtrl()->getModuleConfig()->getTrapTableName(),
138
-                array('num'=>'count(*)'))
139
-            ->where($condition);
140
-        $returnRow=$dbConn->fetchRow($query);
141
-        return $returnRow->num;
142
-    }
124
+		$condition=null;
125
+		if ($ipAddr != null)
126
+		{
127
+			$condition="source_ip='$ipAddr'";
128
+		}
129
+		if ($oid != null)
130
+		{
131
+			$condition=($condition===null)?'':$condition.' AND ';
132
+			$condition.="trap_oid='$oid'";
133
+		}
134
+		if($condition === null) return 0;
135
+		$query=$dbConn->select()
136
+			->from(
137
+				$this->getTrapCtrl()->getModuleConfig()->getTrapTableName(),
138
+				array('num'=>'count(*)'))
139
+			->where($condition);
140
+		$returnRow=$dbConn->fetchRow($query);
141
+		return $returnRow->num;
142
+	}
143 143
     
144
-    /** get configuration value
145
-     *	@param string $element : configuration name in db
146
-     */
147
-    public function getDBConfigValue($element)
148
-    {
144
+	/** get configuration value
145
+	 *	@param string $element : configuration name in db
146
+	 */
147
+	public function getDBConfigValue($element)
148
+	{
149 149
         
150
-        $dbConn = $this->getDbConn();
151
-        if ($dbConn === null) throw new \ErrorException('uncatched db error');
150
+		$dbConn = $this->getDbConn();
151
+		if ($dbConn === null) throw new \ErrorException('uncatched db error');
152 152
         
153
-        $query=$dbConn->select()
154
-        ->from(
155
-            $this->getTrapCtrl()->getModuleConfig()->getDbConfigTableName(),
156
-            array('value'=>'value'))
157
-            ->where('name=?',$element);
158
-            $returnRow=$dbConn->fetchRow($query);
159
-            if ($returnRow==null)  // value does not exists
160
-            {
161
-                $default=$this->getTrapCtrl()->getModuleConfig()->getDBConfigDefaults();
162
-                if ( ! isset($default[$element])) return null; // no default and not value
153
+		$query=$dbConn->select()
154
+		->from(
155
+			$this->getTrapCtrl()->getModuleConfig()->getDbConfigTableName(),
156
+			array('value'=>'value'))
157
+			->where('name=?',$element);
158
+			$returnRow=$dbConn->fetchRow($query);
159
+			if ($returnRow==null)  // value does not exists
160
+			{
161
+				$default=$this->getTrapCtrl()->getModuleConfig()->getDBConfigDefaults();
162
+				if ( ! isset($default[$element])) return null; // no default and not value
163 163
                 
164
-                $this->addDBConfigValue($element,$default[$element]);
165
-                return $default[$element];
166
-            }
167
-            if ($returnRow->value == null) // value id empty
168
-            {
169
-                $default=$this->getTrapCtrl()->getModuleConfig()->getDBConfigDefaults();
170
-                if ( ! isset($default[$element])) return null; // no default and not value
171
-                $this->setDBConfigValue($element,$default[$element]);
172
-                return $default[$element];
173
-            }
174
-            return $returnRow->value;
175
-    }
164
+				$this->addDBConfigValue($element,$default[$element]);
165
+				return $default[$element];
166
+			}
167
+			if ($returnRow->value == null) // value id empty
168
+			{
169
+				$default=$this->getTrapCtrl()->getModuleConfig()->getDBConfigDefaults();
170
+				if ( ! isset($default[$element])) return null; // no default and not value
171
+				$this->setDBConfigValue($element,$default[$element]);
172
+				return $default[$element];
173
+			}
174
+			return $returnRow->value;
175
+	}
176 176
     
177
-    /** add configuration value
178
-     *	@param string $element : name of config element
179
-     *   @param string $value : value
180
-     */
177
+	/** add configuration value
178
+	 *	@param string $element : name of config element
179
+	 *   @param string $value : value
180
+	 */
181 181
     
182
-    public function addDBConfigValue($element,$value)
183
-    {
182
+	public function addDBConfigValue($element,$value)
183
+	{
184 184
         
185
-        $dbConn = $this->getDbConn();
186
-        if ($dbConn === null) throw new \ErrorException('uncatched db error');
185
+		$dbConn = $this->getDbConn();
186
+		if ($dbConn === null) throw new \ErrorException('uncatched db error');
187 187
         
188
-        $query=$dbConn->insert(
189
-            $this->getTrapCtrl()->getModuleConfig()->getDbConfigTableName(),
190
-            array(
191
-                'name' => $element,
192
-                'value'=>$value
193
-            )
194
-            );
195
-        return $query;
196
-    }
188
+		$query=$dbConn->insert(
189
+			$this->getTrapCtrl()->getModuleConfig()->getDbConfigTableName(),
190
+			array(
191
+				'name' => $element,
192
+				'value'=>$value
193
+			)
194
+			);
195
+		return $query;
196
+	}
197 197
     
198
-    /** set configuration value
199
-     *	@param string $element : name of config element
200
-     *   @param string $value : value
201
-     */
202
-    public function setDBConfigValue($element,$value)
203
-    {
198
+	/** set configuration value
199
+	 *	@param string $element : name of config element
200
+	 *   @param string $value : value
201
+	 */
202
+	public function setDBConfigValue($element,$value)
203
+	{
204 204
         
205
-        $dbConn = $this->getDbConn();
206
-        if ($dbConn === null) throw new \ErrorException('uncatched db error');
205
+		$dbConn = $this->getDbConn();
206
+		if ($dbConn === null) throw new \ErrorException('uncatched db error');
207 207
         
208
-        $query=$dbConn->update(
209
-            $this->getTrapCtrl()->getModuleConfig()->getDbConfigTableName(),
210
-            array('value'=>$value),
211
-            'name=\''.$element.'\''
212
-            );
213
-        return $query;
214
-    }
208
+		$query=$dbConn->update(
209
+			$this->getTrapCtrl()->getModuleConfig()->getDbConfigTableName(),
210
+			array('value'=>$value),
211
+			'name=\''.$element.'\''
212
+			);
213
+		return $query;
214
+	}
215 215
     
216 216
     
217 217
 }
218 218
\ No newline at end of file
Please login to merge, or discard this patch.
Spacing   +30 added lines, -30 removed lines patch added patch discarded remove patch
@@ -25,18 +25,18 @@  discard block
 block discarded – undo
25 25
     {
26 26
         // TODO Check for rule consistency
27 27
         
28
-        $dbConn = $this->getDbConn();
28
+        $dbConn=$this->getDbConn();
29 29
         if ($dbConn === null) throw new \ErrorException('uncatched db error');
30 30
         // Add last modified date = creation date and username
31
-        $params['created'] = new Zend_Db_Expr('NOW()');
32
-        $params['modified'] = new 	Zend_Db_Expr('NOW()');
33
-        $params['modifier'] = $this->Auth()->getUser()->getUsername();
31
+        $params['created']=new Zend_Db_Expr('NOW()');
32
+        $params['modified']=new 	Zend_Db_Expr('NOW()');
33
+        $params['modifier']=$this->Auth()->getUser()->getUsername();
34 34
         
35 35
         $query=$dbConn->insert(
36 36
             $this->getTrapCtrl()->getModuleConfig()->getTrapRuleName(),
37 37
             $params
38 38
             );
39
-        if($query==false)
39
+        if ($query == false)
40 40
         {
41 41
             return null;
42 42
         }
@@ -48,14 +48,14 @@  discard block
 block discarded – undo
48 48
      *   @param integer $ruleID : rule id in db
49 49
      *	@return array affected rows
50 50
      */
51
-    public function updateHandlerRule($params,$ruleID)
51
+    public function updateHandlerRule($params, $ruleID)
52 52
     {
53 53
         // TODO Check for rule consistency
54
-        $dbConn = $this->getDbConn();
54
+        $dbConn=$this->getDbConn();
55 55
         if ($dbConn === null) throw new \ErrorException('uncatched db error');
56 56
         // Add last modified date = creation date and username
57
-        $params['modified'] = new 	Zend_Db_Expr('NOW()');
58
-        $params['modifier'] = $this->getTrapCtrl()->Auth()->getUser()->getUsername();
57
+        $params['modified']=new 	Zend_Db_Expr('NOW()');
58
+        $params['modifier']=$this->getTrapCtrl()->Auth()->getUser()->getUsername();
59 59
         
60 60
         $numRows=$dbConn->update(
61 61
             $this->getTrapCtrl()->getModuleConfig()->getTrapRuleName(),
@@ -70,9 +70,9 @@  discard block
 block discarded – undo
70 70
      */
71 71
     public function deleteRule($ruleID)
72 72
     {
73
-        if (!preg_match('/^[0-9]+$/',$ruleID)) { throw new Exception('Invalid id');  }
73
+        if (!preg_match('/^[0-9]+$/', $ruleID)) { throw new Exception('Invalid id'); }
74 74
         
75
-        $dbConn = $this->getDbConn();
75
+        $dbConn=$this->getDbConn();
76 76
         if ($dbConn === null) throw new \ErrorException('uncatched db error');
77 77
         
78 78
         $query=$dbConn->delete(
@@ -86,10 +86,10 @@  discard block
 block discarded – undo
86 86
      *	@param $ipAddr string source IP (v4 or v6)
87 87
      *	@param $oid string oid
88 88
      */
89
-    public function deleteTrap($ipAddr,$oid)
89
+    public function deleteTrap($ipAddr, $oid)
90 90
     {
91 91
         
92
-        $dbConn = $this->getDbConn();
92
+        $dbConn=$this->getDbConn();
93 93
         if ($dbConn === null) throw new \ErrorException('uncatched db error');
94 94
         $condition=null;
95 95
         if ($ipAddr != null)
@@ -98,10 +98,10 @@  discard block
 block discarded – undo
98 98
         }
99 99
         if ($oid != null)
100 100
         {
101
-            $condition=($condition===null)?'':$condition.' AND ';
101
+            $condition=($condition === null) ? '' : $condition.' AND ';
102 102
             $condition.="trap_oid='$oid'";
103 103
         }
104
-        if($condition === null) return null;
104
+        if ($condition === null) return null;
105 105
         $query=$dbConn->delete(
106 106
             $this->getTrapCtrl()->getModuleConfig()->getTrapTableName(),
107 107
             $condition
@@ -115,10 +115,10 @@  discard block
 block discarded – undo
115 115
      *	@param $ipAddr string source IP (v4 or v6)
116 116
      *	@param $oid string oid
117 117
      */
118
-    public function countTrap($ipAddr,$oid)
118
+    public function countTrap($ipAddr, $oid)
119 119
     {
120 120
         
121
-        $dbConn = $this->getDbConn();
121
+        $dbConn=$this->getDbConn();
122 122
         if ($dbConn === null) throw new \ErrorException('uncatched db error');
123 123
         
124 124
         $condition=null;
@@ -128,10 +128,10 @@  discard block
 block discarded – undo
128 128
         }
129 129
         if ($oid != null)
130 130
         {
131
-            $condition=($condition===null)?'':$condition.' AND ';
131
+            $condition=($condition === null) ? '' : $condition.' AND ';
132 132
             $condition.="trap_oid='$oid'";
133 133
         }
134
-        if($condition === null) return 0;
134
+        if ($condition === null) return 0;
135 135
         $query=$dbConn->select()
136 136
             ->from(
137 137
                 $this->getTrapCtrl()->getModuleConfig()->getTrapTableName(),
@@ -147,28 +147,28 @@  discard block
 block discarded – undo
147 147
     public function getDBConfigValue($element)
148 148
     {
149 149
         
150
-        $dbConn = $this->getDbConn();
150
+        $dbConn=$this->getDbConn();
151 151
         if ($dbConn === null) throw new \ErrorException('uncatched db error');
152 152
         
153 153
         $query=$dbConn->select()
154 154
         ->from(
155 155
             $this->getTrapCtrl()->getModuleConfig()->getDbConfigTableName(),
156 156
             array('value'=>'value'))
157
-            ->where('name=?',$element);
157
+            ->where('name=?', $element);
158 158
             $returnRow=$dbConn->fetchRow($query);
159
-            if ($returnRow==null)  // value does not exists
159
+            if ($returnRow == null)  // value does not exists
160 160
             {
161 161
                 $default=$this->getTrapCtrl()->getModuleConfig()->getDBConfigDefaults();
162
-                if ( ! isset($default[$element])) return null; // no default and not value
162
+                if (!isset($default[$element])) return null; // no default and not value
163 163
                 
164
-                $this->addDBConfigValue($element,$default[$element]);
164
+                $this->addDBConfigValue($element, $default[$element]);
165 165
                 return $default[$element];
166 166
             }
167 167
             if ($returnRow->value == null) // value id empty
168 168
             {
169 169
                 $default=$this->getTrapCtrl()->getModuleConfig()->getDBConfigDefaults();
170
-                if ( ! isset($default[$element])) return null; // no default and not value
171
-                $this->setDBConfigValue($element,$default[$element]);
170
+                if (!isset($default[$element])) return null; // no default and not value
171
+                $this->setDBConfigValue($element, $default[$element]);
172 172
                 return $default[$element];
173 173
             }
174 174
             return $returnRow->value;
@@ -179,10 +179,10 @@  discard block
 block discarded – undo
179 179
      *   @param string $value : value
180 180
      */
181 181
     
182
-    public function addDBConfigValue($element,$value)
182
+    public function addDBConfigValue($element, $value)
183 183
     {
184 184
         
185
-        $dbConn = $this->getDbConn();
185
+        $dbConn=$this->getDbConn();
186 186
         if ($dbConn === null) throw new \ErrorException('uncatched db error');
187 187
         
188 188
         $query=$dbConn->insert(
@@ -199,10 +199,10 @@  discard block
 block discarded – undo
199 199
      *	@param string $element : name of config element
200 200
      *   @param string $value : value
201 201
      */
202
-    public function setDBConfigValue($element,$value)
202
+    public function setDBConfigValue($element, $value)
203 203
     {
204 204
         
205
-        $dbConn = $this->getDbConn();
205
+        $dbConn=$this->getDbConn();
206 206
         if ($dbConn === null) throw new \ErrorException('uncatched db error');
207 207
         
208 208
         $query=$dbConn->update(
Please login to merge, or discard this patch.
Braces   +44 added lines, -14 removed lines patch added patch discarded remove patch
@@ -26,7 +26,9 @@  discard block
 block discarded – undo
26 26
         // TODO Check for rule consistency
27 27
         
28 28
         $dbConn = $this->getDbConn();
29
-        if ($dbConn === null) throw new \ErrorException('uncatched db error');
29
+        if ($dbConn === null) {
30
+        	throw new \ErrorException('uncatched db error');
31
+        }
30 32
         // Add last modified date = creation date and username
31 33
         $params['created'] = new Zend_Db_Expr('NOW()');
32 34
         $params['modified'] = new 	Zend_Db_Expr('NOW()');
@@ -52,7 +54,9 @@  discard block
 block discarded – undo
52 54
     {
53 55
         // TODO Check for rule consistency
54 56
         $dbConn = $this->getDbConn();
55
-        if ($dbConn === null) throw new \ErrorException('uncatched db error');
57
+        if ($dbConn === null) {
58
+        	throw new \ErrorException('uncatched db error');
59
+        }
56 60
         // Add last modified date = creation date and username
57 61
         $params['modified'] = new 	Zend_Db_Expr('NOW()');
58 62
         $params['modifier'] = $this->getTrapCtrl()->Auth()->getUser()->getUsername();
@@ -73,7 +77,9 @@  discard block
 block discarded – undo
73 77
         if (!preg_match('/^[0-9]+$/',$ruleID)) { throw new Exception('Invalid id');  }
74 78
         
75 79
         $dbConn = $this->getDbConn();
76
-        if ($dbConn === null) throw new \ErrorException('uncatched db error');
80
+        if ($dbConn === null) {
81
+        	throw new \ErrorException('uncatched db error');
82
+        }
77 83
         
78 84
         $query=$dbConn->delete(
79 85
             $this->getTrapCtrl()->getModuleConfig()->getTrapRuleName(),
@@ -90,7 +96,9 @@  discard block
 block discarded – undo
90 96
     {
91 97
         
92 98
         $dbConn = $this->getDbConn();
93
-        if ($dbConn === null) throw new \ErrorException('uncatched db error');
99
+        if ($dbConn === null) {
100
+        	throw new \ErrorException('uncatched db error');
101
+        }
94 102
         $condition=null;
95 103
         if ($ipAddr != null)
96 104
         {
@@ -101,7 +109,9 @@  discard block
 block discarded – undo
101 109
             $condition=($condition===null)?'':$condition.' AND ';
102 110
             $condition.="trap_oid='$oid'";
103 111
         }
104
-        if($condition === null) return null;
112
+        if($condition === null) {
113
+        	return null;
114
+        }
105 115
         $query=$dbConn->delete(
106 116
             $this->getTrapCtrl()->getModuleConfig()->getTrapTableName(),
107 117
             $condition
@@ -119,7 +129,9 @@  discard block
 block discarded – undo
119 129
     {
120 130
         
121 131
         $dbConn = $this->getDbConn();
122
-        if ($dbConn === null) throw new \ErrorException('uncatched db error');
132
+        if ($dbConn === null) {
133
+        	throw new \ErrorException('uncatched db error');
134
+        }
123 135
         
124 136
         $condition=null;
125 137
         if ($ipAddr != null)
@@ -131,7 +143,9 @@  discard block
 block discarded – undo
131 143
             $condition=($condition===null)?'':$condition.' AND ';
132 144
             $condition.="trap_oid='$oid'";
133 145
         }
134
-        if($condition === null) return 0;
146
+        if($condition === null) {
147
+        	return 0;
148
+        }
135 149
         $query=$dbConn->select()
136 150
             ->from(
137 151
                 $this->getTrapCtrl()->getModuleConfig()->getTrapTableName(),
@@ -148,7 +162,9 @@  discard block
 block discarded – undo
148 162
     {
149 163
         
150 164
         $dbConn = $this->getDbConn();
151
-        if ($dbConn === null) throw new \ErrorException('uncatched db error');
165
+        if ($dbConn === null) {
166
+        	throw new \ErrorException('uncatched db error');
167
+        }
152 168
         
153 169
         $query=$dbConn->select()
154 170
         ->from(
@@ -156,18 +172,28 @@  discard block
 block discarded – undo
156 172
             array('value'=>'value'))
157 173
             ->where('name=?',$element);
158 174
             $returnRow=$dbConn->fetchRow($query);
159
-            if ($returnRow==null)  // value does not exists
175
+            if ($returnRow==null) {
176
+            	// value does not exists
160 177
             {
161 178
                 $default=$this->getTrapCtrl()->getModuleConfig()->getDBConfigDefaults();
162
-                if ( ! isset($default[$element])) return null; // no default and not value
179
+            }
180
+                if ( ! isset($default[$element])) {
181
+                	return null;
182
+                }
183
+                // no default and not value
163 184
                 
164 185
                 $this->addDBConfigValue($element,$default[$element]);
165 186
                 return $default[$element];
166 187
             }
167
-            if ($returnRow->value == null) // value id empty
188
+            if ($returnRow->value == null) {
189
+            	// value id empty
168 190
             {
169 191
                 $default=$this->getTrapCtrl()->getModuleConfig()->getDBConfigDefaults();
170
-                if ( ! isset($default[$element])) return null; // no default and not value
192
+            }
193
+                if ( ! isset($default[$element])) {
194
+                	return null;
195
+                }
196
+                // no default and not value
171 197
                 $this->setDBConfigValue($element,$default[$element]);
172 198
                 return $default[$element];
173 199
             }
@@ -183,7 +209,9 @@  discard block
 block discarded – undo
183 209
     {
184 210
         
185 211
         $dbConn = $this->getDbConn();
186
-        if ($dbConn === null) throw new \ErrorException('uncatched db error');
212
+        if ($dbConn === null) {
213
+        	throw new \ErrorException('uncatched db error');
214
+        }
187 215
         
188 216
         $query=$dbConn->insert(
189 217
             $this->getTrapCtrl()->getModuleConfig()->getDbConfigTableName(),
@@ -203,7 +231,9 @@  discard block
 block discarded – undo
203 231
     {
204 232
         
205 233
         $dbConn = $this->getDbConn();
206
-        if ($dbConn === null) throw new \ErrorException('uncatched db error');
234
+        if ($dbConn === null) {
235
+        	throw new \ErrorException('uncatched db error');
236
+        }
207 237
         
208 238
         $query=$dbConn->update(
209 239
             $this->getTrapCtrl()->getModuleConfig()->getDbConfigTableName(),
Please login to merge, or discard this patch.
library/Trapdirector/TrapsActions/UIDatabase.php 1 patch
Indentation   +256 added lines, -256 removed lines patch added patch discarded remove patch
@@ -19,30 +19,30 @@  discard block
 block discarded – undo
19 19
  */
20 20
 class DBException extends Exception
21 21
 {
22
-    /** @var array $returnArray */
23
-    private $returnArray;
22
+	/** @var array $returnArray */
23
+	private $returnArray;
24 24
     
25
-    /**
26
-     * Buil DBException
27
-     * @param array $retarray
28
-     * @param string $message
29
-     * @param int $code
30
-     * @param Exception $previous
31
-     */
32
-    public function __construct(array $retarray, string $message = null, int $code = 0, Exception $previous = null)
33
-    {
34
-        parent::__construct($message,$code,$previous);
35
-        $this->returnArray = $retarray;
36
-    }
25
+	/**
26
+	 * Buil DBException
27
+	 * @param array $retarray
28
+	 * @param string $message
29
+	 * @param int $code
30
+	 * @param Exception $previous
31
+	 */
32
+	public function __construct(array $retarray, string $message = null, int $code = 0, Exception $previous = null)
33
+	{
34
+		parent::__construct($message,$code,$previous);
35
+		$this->returnArray = $retarray;
36
+	}
37 37
     
38
-    /**
39
-     * Get exception array
40
-     * @return array
41
-     */
42
-    public function getArray()
43
-    {
44
-        return $this->returnArray;
45
-    }
38
+	/**
39
+	 * Get exception array
40
+	 * @return array
41
+	 */
42
+	public function getArray()
43
+	{
44
+		return $this->returnArray;
45
+	}
46 46
 }
47 47
 
48 48
 /**
@@ -56,274 +56,274 @@  discard block
 block discarded – undo
56 56
  */
57 57
 class UIDatabase //extends TrapDBQuery
58 58
 {
59
-    use TrapDBQuery;
59
+	use TrapDBQuery;
60 60
     
61
-    /** @var TrapsController $trapController TrapController 'parent' class */
62
-    protected  $trapController=null;
61
+	/** @var TrapsController $trapController TrapController 'parent' class */
62
+	protected  $trapController=null;
63 63
     
64
-    /** @var Selectable $trapDB Trap Database*/
65
-    protected $trapDB=null;
64
+	/** @var Selectable $trapDB Trap Database*/
65
+	protected $trapDB=null;
66 66
  
67
-    /** @var Selectable $trapDB Icinga IDO database*/
68
-    protected $idoDB=null;
67
+	/** @var Selectable $trapDB Icinga IDO database*/
68
+	protected $idoDB=null;
69 69
     
70
-    /** @var array $testResult */
71
-    protected $testResult;
70
+	/** @var array $testResult */
71
+	protected $testResult;
72 72
     
73
-    /**
74
-     * 
75
-     * @param TrapsController $trapCtrl
76
-     */
77
-    function __construct(TrapsController $trapCtrl)
78
-    {
79
-        $this->trapController=$trapCtrl;
80
-    }
73
+	/**
74
+	 * 
75
+	 * @param TrapsController $trapCtrl
76
+	 */
77
+	function __construct(TrapsController $trapCtrl)
78
+	{
79
+		$this->trapController=$trapCtrl;
80
+	}
81 81
     
82
-    protected function getTrapCtrl()
83
-    {
84
-        return $this->trapController;
85
-    }
82
+	protected function getTrapCtrl()
83
+	{
84
+		return $this->trapController;
85
+	}
86 86
     
87
-    /**
88
-     * Test if database version >= min database version
89
-     * 
90
-     * @param \Zend_Db_Adapter_Abstract $dbConn
91
-     * @param int $min Minimum version
92
-     * @param bool $test Test mode
93
-     * @param string $DBname Name of DB
94
-     * @return bool true if OK, false if version < min version
95
-     * @throws Exception if error and test = true
96
-     */
97
-    protected function testDbVersion($dbAdapter,int $min,bool $test, string $DBname)
98
-    {
99
-        try
100
-        {
101
-            $query = $dbAdapter->select()
102
-            ->from($this->trapController->getModuleConfig()->getDbConfigTableName(),'value')
103
-            ->where('name=\'db_version\'');
104
-            $version=$dbAdapter->fetchRow($query);
105
-            if ( ($version == null) || ! property_exists($version,'value') )
106
-            {
107
-                if ($test === true) 
108
-                {
109
-                    $this->testResult = array(4,$DBname);
110
-                    return false;
111
-                }
112
-                $this->trapController->redirectNow('trapdirector/settings?dberror=4');
113
-                return false;
114
-            }
115
-            if ($version->value < $min)
116
-            {
117
-                if ($test === true) 
118
-                {
119
-                    $this->testResult = array(5,$version->value,$min);
120
-                    return false;
121
-                }
122
-                $this->trapController->redirectNow('trapdirector/settings?dberror=5');
123
-                return false;
124
-            }
125
-        }
126
-        catch (Exception $e)
127
-        {
128
-            if ($test === true) 
129
-            {
130
-                $this->testResult = array(3,$DBname,$e->getMessage());
131
-                return false;
132
-            }
133
-            $this->trapController->redirectNow('trapdirector/settings?dberror=4');
134
-            return false;
135
-        }
136
-        return true;
137
-    }
87
+	/**
88
+	 * Test if database version >= min database version
89
+	 * 
90
+	 * @param \Zend_Db_Adapter_Abstract $dbConn
91
+	 * @param int $min Minimum version
92
+	 * @param bool $test Test mode
93
+	 * @param string $DBname Name of DB
94
+	 * @return bool true if OK, false if version < min version
95
+	 * @throws Exception if error and test = true
96
+	 */
97
+	protected function testDbVersion($dbAdapter,int $min,bool $test, string $DBname)
98
+	{
99
+		try
100
+		{
101
+			$query = $dbAdapter->select()
102
+			->from($this->trapController->getModuleConfig()->getDbConfigTableName(),'value')
103
+			->where('name=\'db_version\'');
104
+			$version=$dbAdapter->fetchRow($query);
105
+			if ( ($version == null) || ! property_exists($version,'value') )
106
+			{
107
+				if ($test === true) 
108
+				{
109
+					$this->testResult = array(4,$DBname);
110
+					return false;
111
+				}
112
+				$this->trapController->redirectNow('trapdirector/settings?dberror=4');
113
+				return false;
114
+			}
115
+			if ($version->value < $min)
116
+			{
117
+				if ($test === true) 
118
+				{
119
+					$this->testResult = array(5,$version->value,$min);
120
+					return false;
121
+				}
122
+				$this->trapController->redirectNow('trapdirector/settings?dberror=5');
123
+				return false;
124
+			}
125
+		}
126
+		catch (Exception $e)
127
+		{
128
+			if ($test === true) 
129
+			{
130
+				$this->testResult = array(3,$DBname,$e->getMessage());
131
+				return false;
132
+			}
133
+			$this->trapController->redirectNow('trapdirector/settings?dberror=4');
134
+			return false;
135
+		}
136
+		return true;
137
+	}
138 138
     
139
-    /**	Get Database connexion
140
-     *	@param $DBname string DB name in resource.ini_ge
141
-     *	@param $test bool if set to true, returns error code and not database
142
-     *	@param $test_version bool if set to flase, does not test database version of trapDB
143
-     *  @throws DBException if test = true and error
144
-     *	@return Selectable|null : if test=false, returns DB connexion, else array(error_num,message) or null on error.
145
-     */
146
-    protected function getDbByName($DBname , $test = false , $test_version = true)
147
-    {
148
-        try
149
-        {
150
-            $dbconn = IcingaDbConnection::fromResourceName($DBname);
151
-        }
152
-        catch (Exception $e)
153
-        {
154
-            if ($test === true) 
155
-            {
156
-                throw new DBException(array(2,$DBname));
157
-            }
158
-            $this->trapController->redirectNow('trapdirector/settings?dberror=2');
159
-            return null;
160
-        }
139
+	/**	Get Database connexion
140
+	 *	@param $DBname string DB name in resource.ini_ge
141
+	 *	@param $test bool if set to true, returns error code and not database
142
+	 *	@param $test_version bool if set to flase, does not test database version of trapDB
143
+	 *  @throws DBException if test = true and error
144
+	 *	@return Selectable|null : if test=false, returns DB connexion, else array(error_num,message) or null on error.
145
+	 */
146
+	protected function getDbByName($DBname , $test = false , $test_version = true)
147
+	{
148
+		try
149
+		{
150
+			$dbconn = IcingaDbConnection::fromResourceName($DBname);
151
+		}
152
+		catch (Exception $e)
153
+		{
154
+			if ($test === true) 
155
+			{
156
+				throw new DBException(array(2,$DBname));
157
+			}
158
+			$this->trapController->redirectNow('trapdirector/settings?dberror=2');
159
+			return null;
160
+		}
161 161
         
162
-        try
163
-        {
164
-            $dbAdapter=$dbconn->getDbAdapter();
162
+		try
163
+		{
164
+			$dbAdapter=$dbconn->getDbAdapter();
165 165
             
166
-        }
167
-        catch (Exception $e)
168
-        {
169
-            if ($test === true)
170
-            {
171
-                throw new DBException(array(3,$DBname,$e->getMessage()));
172
-            }
173
-            $this->trapController->redirectNow('trapdirector/settings?dberror=3');
174
-            return null;
175
-        }
166
+		}
167
+		catch (Exception $e)
168
+		{
169
+			if ($test === true)
170
+			{
171
+				throw new DBException(array(3,$DBname,$e->getMessage()));
172
+			}
173
+			$this->trapController->redirectNow('trapdirector/settings?dberror=3');
174
+			return null;
175
+		}
176 176
         
177
-        if ($test_version == true) {
178
-            $testRet=$this->testDbVersion($dbAdapter, $this->trapController->getModuleConfig()->getDbMinVersion(), $test, $DBname);
179
-            if ($testRet !== true) 
180
-            {
181
-                throw new DBException($this->testResult);
182
-            }
183
-        }
177
+		if ($test_version == true) {
178
+			$testRet=$this->testDbVersion($dbAdapter, $this->trapController->getModuleConfig()->getDbMinVersion(), $test, $DBname);
179
+			if ($testRet !== true) 
180
+			{
181
+				throw new DBException($this->testResult);
182
+			}
183
+		}
184 184
         
185
-        return $dbconn;
186
-    }
185
+		return $dbconn;
186
+	}
187 187
 
188
-    /**
189
-     * Get Trap database
190
-     * @return Selectable|null : returns DB connexion or null on error.
191
-     */
192
-    public function getDb()
193
-    {
194
-        if ( $this->trapDB != null ) return $this->trapDB;
188
+	/**
189
+	 * Get Trap database
190
+	 * @return Selectable|null : returns DB connexion or null on error.
191
+	 */
192
+	public function getDb()
193
+	{
194
+		if ( $this->trapDB != null ) return $this->trapDB;
195 195
         
196 196
         
197
-        $dbresource=$this->trapController->Config()->get('config', 'database');
197
+		$dbresource=$this->trapController->Config()->get('config', 'database');
198 198
         
199
-        if ( ! $dbresource )
200
-        {
201
-            $this->trapController->redirectNow('trapdirector/settings?dberror=1');
202
-            return null;
203
-        }
199
+		if ( ! $dbresource )
200
+		{
201
+			$this->trapController->redirectNow('trapdirector/settings?dberror=1');
202
+			return null;
203
+		}
204 204
 
205
-        try {
206
-            $this->trapDB = $this->getDbByName($dbresource,false,true);
207
-        } catch (DBException $e) {
208
-            return null; // Should not happen as test = false
209
-        }
205
+		try {
206
+			$this->trapDB = $this->getDbByName($dbresource,false,true);
207
+		} catch (DBException $e) {
208
+			return null; // Should not happen as test = false
209
+		}
210 210
         
211
-        //$this->trapDB->getConnection();
211
+		//$this->trapDB->getConnection();
212 212
         
213
-        return $this->trapDB;
214
-    }
213
+		return $this->trapDB;
214
+	}
215 215
 
216
-    /**
217
-     * Get Zend adapter of DB.
218
-     * @return \Zend_Db_Adapter_Abstract|null
219
-     */
220
-    public function getDbConn()
221
-    {
222
-        if ($this->getDb() == null) return null;
223
-        return $this->getDb()->getConnection();
224
-    }
216
+	/**
217
+	 * Get Zend adapter of DB.
218
+	 * @return \Zend_Db_Adapter_Abstract|null
219
+	 */
220
+	public function getDbConn()
221
+	{
222
+		if ($this->getDb() == null) return null;
223
+		return $this->getDb()->getConnection();
224
+	}
225 225
     
226
-    /**
227
-     * Test Trap database
228
-     * @param boolean $test
229
-     * @throws DBException on error.
230
-     * @return \Zend_Db_Adapter_Abstract|array|null : if test=false, returns DB connexion, else array(error_num,message) or null on error.
231
-     */
232
-    public function testGetDb()
233
-    {       
234
-        $dbresource=$this->trapController->Config()->get('config', 'database');
226
+	/**
227
+	 * Test Trap database
228
+	 * @param boolean $test
229
+	 * @throws DBException on error.
230
+	 * @return \Zend_Db_Adapter_Abstract|array|null : if test=false, returns DB connexion, else array(error_num,message) or null on error.
231
+	 */
232
+	public function testGetDb()
233
+	{       
234
+		$dbresource=$this->trapController->Config()->get('config', 'database');
235 235
         
236
-        if ( ! $dbresource )
237
-        {
238
-                throw new DBException(array(1,''));
239
-        }
236
+		if ( ! $dbresource )
237
+		{
238
+				throw new DBException(array(1,''));
239
+		}
240 240
         
241
-        $this->trapDB = $this->getDbByName($dbresource,true,true);       
242
-        return;
243
-    }
241
+		$this->trapDB = $this->getDbByName($dbresource,true,true);       
242
+		return;
243
+	}
244 244
     
245 245
 
246
-    /**
247
-     * Get IDO Database
248
-     * @return \Zend_Db_Adapter_Abstract|NULL  returns DB connexion or null on error.
249
-     */
250
-    public function getIdoDb()
251
-    {
252
-        if ( $this->idoDB != null ) return $this->idoDB;
253
-        // TODO : get ido database directly from icingaweb2 config -> (or not if using only API)
254
-        $dbresource=$this->trapController->Config()->get('config', 'IDOdatabase');;
246
+	/**
247
+	 * Get IDO Database
248
+	 * @return \Zend_Db_Adapter_Abstract|NULL  returns DB connexion or null on error.
249
+	 */
250
+	public function getIdoDb()
251
+	{
252
+		if ( $this->idoDB != null ) return $this->idoDB;
253
+		// TODO : get ido database directly from icingaweb2 config -> (or not if using only API)
254
+		$dbresource=$this->trapController->Config()->get('config', 'IDOdatabase');;
255 255
         
256
-        if ( ! $dbresource )
257
-        {
258
-            $this->trapController->redirectNow('trapdirector/settings?idodberror=1');
259
-            return null;
260
-        }
256
+		if ( ! $dbresource )
257
+		{
258
+			$this->trapController->redirectNow('trapdirector/settings?idodberror=1');
259
+			return null;
260
+		}
261 261
         
262
-        try
263
-        {
264
-            $dbconn = IcingaDbConnection::fromResourceName($dbresource);
265
-        }
266
-        catch (Exception $e)
267
-        {
268
-            $this->trapController->redirectNow('trapdirector/settings?idodberror=2');
269
-            return null;
270
-        }
262
+		try
263
+		{
264
+			$dbconn = IcingaDbConnection::fromResourceName($dbresource);
265
+		}
266
+		catch (Exception $e)
267
+		{
268
+			$this->trapController->redirectNow('trapdirector/settings?idodberror=2');
269
+			return null;
270
+		}
271 271
 
272
-        $this->idoDB = $dbconn;
273
-        return $this->idoDB;
274
-    }
272
+		$this->idoDB = $dbconn;
273
+		return $this->idoDB;
274
+	}
275 275
 
276 276
 
277
-    /**
278
-     * Get Zend adapter of DB.
279
-     * @return \Zend_Db_Adapter_Abstract|null
280
-     */
281
-    public function getIdoDbConn()
282
-    {
283
-        if ($this->getIdoDb() == null) return null;
284
-        return $this->getIdoDb()->getConnection();
285
-    }
277
+	/**
278
+	 * Get Zend adapter of DB.
279
+	 * @return \Zend_Db_Adapter_Abstract|null
280
+	 */
281
+	public function getIdoDbConn()
282
+	{
283
+		if ($this->getIdoDb() == null) return null;
284
+		return $this->getIdoDb()->getConnection();
285
+	}
286 286
     
287
-    /**
288
-     * Get IDO Database
289
-     * @param boolean $test
290
-     * @throws DBException on error
291
-     */
292
-    public function testGetIdoDb()
293
-    {
294
-        // TODO : get ido database directly from icingaweb2 config -> (or not if using only API)
295
-        $dbresource=$this->trapController->Config()->get('config', 'IDOdatabase');;
287
+	/**
288
+	 * Get IDO Database
289
+	 * @param boolean $test
290
+	 * @throws DBException on error
291
+	 */
292
+	public function testGetIdoDb()
293
+	{
294
+		// TODO : get ido database directly from icingaweb2 config -> (or not if using only API)
295
+		$dbresource=$this->trapController->Config()->get('config', 'IDOdatabase');;
296 296
         
297
-        if ( ! $dbresource )
298
-        {
299
-            throw new DBException(array(1,'No database in config.ini'));
300
-        }
297
+		if ( ! $dbresource )
298
+		{
299
+			throw new DBException(array(1,'No database in config.ini'));
300
+		}
301 301
         
302
-        try
303
-        {
304
-            $dbconn = IcingaDbConnection::fromResourceName($dbresource);
305
-        }
306
-        catch (Exception $e)
307
-        {
308
-            throw new DBException( array(2,"Database $dbresource does not exists in IcingaWeb2") );
309
-        }
302
+		try
303
+		{
304
+			$dbconn = IcingaDbConnection::fromResourceName($dbresource);
305
+		}
306
+		catch (Exception $e)
307
+		{
308
+			throw new DBException( array(2,"Database $dbresource does not exists in IcingaWeb2") );
309
+		}
310 310
                
311
-        try
312
-        {
313
-            $query = $dbconn->select()
314
-            ->from('icinga_dbversion',array('version'));
315
-            $version=$dbconn->fetchRow($query);
316
-            if ( ($version == null) || ! property_exists($version,'version') )
317
-            {
318
-                throw new DBException( array(4,"$dbresource does not look like an IDO database"));
319
-            }
320
-        }
321
-        catch (Exception $e)
322
-        {
323
-            throw new DBException( array(3,"Error connecting to $dbresource : " . $e->getMessage()));
324
-        }
311
+		try
312
+		{
313
+			$query = $dbconn->select()
314
+			->from('icinga_dbversion',array('version'));
315
+			$version=$dbconn->fetchRow($query);
316
+			if ( ($version == null) || ! property_exists($version,'version') )
317
+			{
318
+				throw new DBException( array(4,"$dbresource does not look like an IDO database"));
319
+			}
320
+		}
321
+		catch (Exception $e)
322
+		{
323
+			throw new DBException( array(3,"Error connecting to $dbresource : " . $e->getMessage()));
324
+		}
325 325
         
326
-        return;
327
-    }
326
+		return;
327
+	}
328 328
     
329 329
 }
330 330
\ No newline at end of file
Please login to merge, or discard this patch.