1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
|
<?php
class Pbs_Filter{
private $db = null;
private $membership;
public function evaluate()
{
$this->db = Zend_Db_Table::getDefaultAdapter();
$db = $this->db;
$debuglevel = 0;
$userIDsNamespace = Zend_Session::namespaceGet('userIDs');
$membershipMapper = new Application_Model_MembershipMapper();
$this->membership = new Application_Model_Membership();
if(isset($userIDsNamespace['membershipID'])){
$membershipMapper->find($userIDsNamespace['membershipID'],$this->membership);
}
else{
$this->membership->setID(null);
}
// For Testing Filters
$testFilter = (isset($_POST['submit']))?true:false;
if($testFilter){
$ipAdress = $_POST['ip'];
$macAdress = $_POST['mac'];
$poolID = $_POST['poolID'];
$bootisoID = $_POST['bootisoID'];
$membershipID = $_POST['membershipID'];
$groupID = $_POST['groupID'];
$time = ($_POST['time']);
$clientID = $_POST['clientID'];
$weekday = $_POST['weekday'];
$date = $_POST['date'];
print_a('Input',
array('ipAdress'=>$ipAdress,
'macAdress'=>$macAdress,
'poolID' => $poolID,
'bootisoID' => $bootisoID,
'membershipID'=>$membershipID,
'groupID'=>$groupID,
'time'=> $time." - ".date('d.m.Y H:i:s',$time),
'clientID' => $clientID,
'hardwarehash' => $hardwarehash,
'weekday' => $weekday,
'date' => $date));
}
try{
$set = array();
$session = new Application_Model_Session();
$sessionmapper = new Application_Model_SessionMapper();
$sessionarray = $sessionmapper->findBy(array('alphasessionID' => $_SESSION['alphasessionID']),true);
$session->setOptions($sessionarray[0]);
$session->setID($sessionarray['sessionID']);
$bootisoMapper = new Application_Model_BootisoMapper();
$bootiso = new Application_Model_BootIso();
$bootisoMapper->find($session->getBootisoID(),$bootiso);
$groupID = $bootiso->getGroupID();
#########################################################
// IP Adress
$filtertypID = 1;
// get it from session_table with session_id from the session
if(!$testFilter){
$ipAdress = $session->getIp();
}
$ipAdress = str_replace(".","",$this->fillIP($ipAdress));
$stmt = $db->query("SELECT * FROM pbs_filterentries fe, pbs_filter f WHERE
fe.filtertypeID = ".$filtertypID." AND
REPLACE(fe.filtervalue,'.','') <= '".$ipAdress."' AND
'".$ipAdress."' <= REPLACE(fe.filtervalue2,'.','') AND
fe.filterID = f.filterID AND
f.groupID = '".$groupID."'");
$result = $stmt->fetchAll();
foreach($result as $r){
$set[$filtertypID][] = $r['filterID'];
}
if($debuglevel >= 3 )
print_a('ipAdress',$result,$set[$filtertypID]);
#########################################################
// Mac range
$filtertypID = 2;
// get it from session_table with session_id from the session
if(!$testFilter){
$client = new Application_Model_Client();
$clientmapper = new Application_Model_ClientMapper();
$clientmapper->find($session->getClientID(),$client);
$macAdress = $client->getMacadress();
}
try{
$macAdress = $this->fillMac($macAdress);
$stmt = $db->query("SELECT * FROM pbs_filterentries fe, pbs_filter f WHERE
fe.filtertypeID = ".$filtertypID." AND
fe.filtervalue <= '".$macAdress."' AND
'".$macAdress."' <= fe.filtervalue2 AND
fe.filterID = f.filterID AND
f.groupID = '".$groupID."'");
$result = $stmt->fetchAll();
foreach($result as $r){
$set[$filtertypID][] = $r['filterID'];
}
}
catch(Exception $e){}
if($debuglevel >= 3)
print_a('macAdress',$result,$set[$filtertypID]);
#########################################################
// PoolID
$filtertypID = 3;
// get PoolID from client_ID from session_id from the session
if(!$testFilter){
$poolentry = new Application_Model_PoolEntries();
$poolentrymapper = new Application_Model_PoolEntriesMapper();
$poolentry = $poolentrymapper->findby(array('clientID' => $client->getID()),true);
$poolentry = $poolentry[0];
$poolID = $poolentry['poolID'];
}
try{
$stmt = $db->query("SELECT * FROM pbs_filterentries fe, pbs_filter f WHERE
fe.filtertypeID = ".$filtertypID." AND
fe.filtervalue = ".$poolID." AND
fe.filterID = f.filterID AND
f.groupID = '".$groupID."'");
$result = $stmt->fetchAll();
if(count($result)>0){
foreach($result as $r){
$set[$filtertypID][] = $r['filterID'];
}
}
}
catch(Exception $e){}
if($debuglevel >= 3)
print_a('poolID',$result,$set[$filtertypID]);
#########################################################
// ClientID
$filtertypID = 8;
// get client_ID from session_id from the session
if(!$testFilter){
$clientID = $session->getClientID();
}
try{
$stmt = $db->query("SELECT * FROM pbs_filterentries fe, pbs_filter f WHERE
fe.filtertypeID = ".$filtertypID." AND
fe.filtervalue = ".$clientID." AND
fe.filterID = f.filterID AND
f.groupID = '".$groupID."'");
$result = $stmt->fetchAll();
foreach($result as $r){
$set[$filtertypID][] = $r['filterID'];
}
}
catch(Exception $e){}
if($debuglevel >= 3)
print_a('clientID',$result,$set[$filtertypID]);
#########################################################
// BootIsoID
$filtertypID = 4;
// get BootIsoID from client_ID from session_id from the session
if(!$testFilter){
$bootisoID = $session->getBootisoID();
}
try{
$stmt = $db->query("SELECT * FROM pbs_filterentries fe, pbs_filter f WHERE
fe.filtertypeID = ".$filtertypID." AND
fe.filtervalue = ".$bootisoID." AND
fe.filterID = f.filterID AND
f.groupID = '".$groupID."'");
$result = $stmt->fetchAll();
foreach($result as $r){
$set[$filtertypID][] = $r['filterID'];
}
}
catch(Exception $e){}
if($debuglevel >= 3)
print_a('bootisoID',$result,$set[$filtertypID]);
#########################################################
// MembershipID
$filtertypID = 5;
if($this->membership->getID() != null){
if(!$testFilter){
// get membership from the session
//TODO: GET MEMBERSHIP from SESSION
$membershipID = $this->membership->getID();
}
try{
$stmt = $db->query("SELECT * FROM pbs_filterentries fe, pbs_filter f WHERE
fe.filtertypeID = ".$filtertypID." AND
fe.filtervalue = ".$membershipID." AND
fe.filterID = f.filterID AND
f.groupID = '".$groupID."'");
$result = $stmt->fetchAll();
foreach($result as $r){
$set[$filtertypID][] = $r['filterID'];
}
}
catch(Exception $e){}
if($debuglevel >= 3)
print_a('membership',$result,$set[$filtertypID]);
}
#########################################################
// GroupID
$filtertypID = 6;
if($this->membership->getID() != null){
if(!$testFilter){
// get membership from the session
//TODO: GET GROUP from SESSION
$membergroupID = $this->membership->getGroupID();
}
}
if(isset($groupID)){
try{
$stmt = $db->query("SELECT * FROM pbs_filterentries fe, pbs_filter f WHERE
fe.filtertypeID = ".$filtertypID." AND
fe.filtervalue = ".$membergroupID." AND
fe.filterID = f.filterID AND
f.groupID = '".$groupID."'");
$result = $stmt->fetchAll();
foreach($result as $r){
$set[$filtertypID][] = $r['filterID'];
}
}
catch(Exception $e){}
if($debuglevel >= 3)
print_a('membership',$result,$set[$filtertypID]);
}
#########################################################
// Time
$filtertypID = 7;
if(!$testFilter){
$nowShort = date("H:i");
}
else{
$nowShort = date("H:i",$time);
}
$nowShort = str_replace(":","",$nowShort);
try{
$stmt = $db->query('SELECT * FROM pbs_filterentries fe, pbs_filter f WHERE
fe.filtertypeID = '.$filtertypID.' AND
REPLACE(fe.filtervalue,":","") <= '.$nowShort.' AND
REPLACE(fe.filtervalue2,":","") >= '.$nowShort." AND
fe.filterID = f.filterID AND
f.groupID = '".$groupID."'");
$result = $stmt->fetchAll();
foreach($result as $r){
$set[$filtertypID][] = $r['filterID'];
}
}
catch(Exception $e){}
if($debuglevel >= 3)
print_a('time',$result,$set[$filtertypID]);
#########################################################
// Hardwarehash
$filtertypID = 9;
if(!$testFilter){
// get hardwarehash from session
$client = new Application_Model_Client();
$clientmapper = new Application_Model_ClientMapper();
$clientmapper->find($session->getClientID(),$client);
$macAdress = $client->getHardwarehash();
}
try{
$stmt = $db->query("SELECT * FROM pbs_filterentries fe, pbs_filter f WHERE
fe.filtertypeID = ".$filtertypID." AND
fe.filtervalue = ".$hardwarehash." AND
fe.filterID = f.filterID AND
f.groupID = '".$groupID."'");
$result = $stmt->fetchAll();
foreach($result as $r){
$set[$filtertypID][] = $r['filterID'];
}
}
catch(Exception $e){}
if($debuglevel >= 3)
print_a('hardwarehash',$result,$set[$filtertypID]);
#########################################################
// weekday
$filtertypID = 10;
if(!$testFilter){
$weekday = date('N');
}
try{
$stmt = $db->query("SELECT * FROM pbs_filterentries fe, pbs_filter f WHERE
fe.filtertypeID = ".$filtertypID." AND
fe.filtervalue <= ".$weekday." AND
".$weekday." <= fe.filtervalue2 AND
fe.filterID = f.filterID AND
f.groupID = '".$groupID."'");
$result = $stmt->fetchAll();
foreach($result as $r){
$set[$filtertypID][] = $r['filterID'];
}
}
catch(Exception $e){}
if($debuglevel >= 3)
print_a('weekday',$result,$set[$filtertypID]);
#########################################################
// date
$filtertypID = 11;
if(!$testFilter){
$date = date('d.m.Y');
}
try{
$stmt = $db->query("SELECT * FROM pbs_filterentries fe, pbs_filter f WHERE
fe.filtertypeID = ".$filtertypID." AND
REPLACE(fe.filtervalue,'.','') <= ".$date." AND
".$date." <= REPLACE(fe.filtervalue2,'.','') <= AND
fe.filterID = f.filterID AND
f.groupID = '".$groupID."'");
$result = $stmt->fetchAll();
foreach($result as $r){
$set[$filtertypID][] = $r['filterID'];
}
}
catch(Exception $e){}
if($debuglevel >= 3)
print_a('date',$result,$set[$filtertypID]);
########################################
$result = array();
// ErgebnisSet auswerten
if($debuglevel >= 2)
print_a('the result sets of the filtertypes',$set);
// Merging of the resultsets of the filter
$kk = array();
foreach($set as $myset){
$kk = array_merge($kk,$myset);
}
$set = $kk;
// counting how often a filter is evaluated at database
$a = array();
foreach($set as $k=> $s){
$a[$s] += 1;
}
if($debuglevel >= 2)
print_a('Übereinstimmungen pro Filter',$a);
// Get the number of filtertypes every filter has
$stmt = $db->query(
"SELECT f.filterID,SUM(num) as num FROM (
SELECT *, '1' as num
FROM pbs_filterentries
GROUP BY filterID, filtertypeID
) d RIGHT OUTER JOIN pbs_filter f ON d.filterID = f.filterID GROUP BY d.filterID");
$resultset = $stmt->fetchAll();
foreach($resultset as $d){
if($d['num'] == NULL){
$d['num'] = 0;
// write filter with empty filterrules in resultset
$result[] = $d['filterID'];
}
$database[$d['filterID']] = $d['num'];
}
if($debuglevel >= 2)
print_a('Anzahl Filterentries in Datenbank',$database);
// remove all filters from resultset, which to few filtertypes satisfied
foreach($a as $i=>$b){
if($a[$i] < $database[$i]){
unset($a[$i]);
}
else{
$result[] = $i;
}
}
if($debuglevel >= 1)
print_a('Valid Filters',$result);
// now selecting the filter with the highest priority
$query = 'SELECT filterID, bootmenuID, priority
FROM pbs_filter WHERE ';
foreach($result as $e){
$queryTmp[] = "filterID = '$e'";
}
$query .= implode(" OR ", $queryTmp);
$query .= 'ORDER BY priority DESC LIMIT 0,1';
$bootmenuID = 'null';
try{
$stmt = $db->query($query);
$resultset = $stmt->fetchAll();
$bootmenuID = $resultset[0]['bootmenuID'];
}
catch(Exception $e){
}
if($bootmenuID != 'null'){
$bm = new Application_Model_BootMenu();
$bmmapper = new Application_Model_BootMenuMapper();
$bmmapper->find($bootmenuID,$bm);
return $bootmenuID;
}
else{
return null;
}
}catch (Zend_Exception $e) {
echo "class: ".get_class($e)."<br>\n";
echo "Error message 2: " . $e->getMessage() . "\n";
}
}
private function fillIP($ip)
{
$ar = explode(".",$ip);
$representation = array();
foreach($ar as $part){
$representation[] = sprintf("%03s",$part);
}
return implode(".",$representation);
}
private function fillMac($ip)
{
$ar = explode(":",$ip);
$representation = array();
foreach($ar as $part){
$representation[] = sprintf("%02s",$part);
}
return implode(":",$representation);
}
}
?>
|