summaryrefslogblamecommitdiffstats
path: root/library/Pbs/Filter.php
blob: 9e1f6dbd37bbcd4e9e9d63b498858be09e92582b (plain) (tree)
1
2
3
4
5
6
7
8






                            
                                  






                                                                             





                                                                                         



































                                                                                                             


                                                                    

























































































































                                                                                                                




                                                                                   
                                 









                                                                                                            
 


                                                                                         


                                                                                 




                                                                                        
                                 









                                                                                                            
 


                                                                                         



                                                                                 



                                                              
                         


                                                                  
        







                                                                                                                         




































































































                                                                                                                                




                                                                                  

















































                                                                                                                                          
         




















                                                                          
<?php

class Pbs_Filter{

	private $db = null;
	private $membership;
	
	public function evaluate()
    {
    	$this->db = Zend_Db_Table::getDefaultAdapter();
        $db = $this->db;	
		$showPartResults = false;
		
		$membershipMapper = new Application_Model_MembershipMapper();
    	$this->membership = new Application_Model_Membership();
    	if(isset($_SESSION['membershipID'])){
	    	$membershipMapper->find($_SESSION['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('alphasessionID',$_SESSION['alphasessionID']);
			print_a($sessionarray[0]);
			$session->setOptions($sessionarray[0]);
			$session->setID($sessionarray['sessionID']);
			#########################################################
			// 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));

			$select = $db->select()
			     ->from(array('pbs_filterentries')
					)
				->where('filtertypeID  = ?',$filtertypID)
				->where('REPLACE(filtervalue,".","") <= ?',$ipAdress)
				->where('? <= REPLACE(filtervalue2,".","")',$ipAdress);
	
			$stmt = $select->query();
			$result = $stmt->fetchAll();
			foreach($result as $r){
				$set[$filtertypID][] = $r['filterID'];	
			}

			if($showPartResults)
				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 WHERE 
								filtertypeID  = ".$filtertypID." AND
								filtervalue <= '".$macAdress."' AND
								'".$macAdress."' <= filtervalue2");
				$result = $stmt->fetchAll();
				foreach($result as $r){
					$set[$filtertypID][] = $r['filterID'];	
				}
			}
			catch(Exception $e){}
			if($showPartResults)
				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('clientID',$client->getID());
				$poolentry = $poolentry[0];		
			
				$poolID = $poolentry['poolID'];
			}			
			
			try{
				$stmt = $db->query("SELECT * FROM pbs_filterentries WHERE 
								filtertypeID  = ".$filtertypID." AND
								filtervalue = ".$poolID." ");			
				$result = $stmt->fetchAll();
				if(count($result)>0){
					foreach($result as $r){
						$set[$filtertypID][] = $r['filterID'];	
					}
				}
			}
			catch(Exception $e){}

			if($showPartResults)
				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 WHERE 
								filtertypeID  = ".$filtertypID." AND
								filtervalue = ".$clientID." ");
				$result = $stmt->fetchAll();
				foreach($result as $r){
					$set[$filtertypID][] = $r['filterID'];	
				}
			}
			catch(Exception $e){}

			if($showPartResults)
				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 WHERE 
								filtertypeID  = ".$filtertypID." AND
								filtervalue = ".$bootisoID." ");
				$result = $stmt->fetchAll();
				foreach($result as $r){
					$set[$filtertypID][] = $r['filterID'];	
				}
			}
			catch(Exception $e){}

			if($showPartResults)
				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 WHERE 
									filtertypeID  = ".$filtertypID." AND
									filtervalue = ".$membershipID." ");
					$result = $stmt->fetchAll();
					foreach($result as $r){
						$set[$filtertypID][] = $r['filterID'];	
					}
				}
				catch(Exception $e){}

				if($showPartResults)
					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
					$groupID = $this->membership->getGroupID();	
				}
				try{
					$stmt = $db->query("SELECT * FROM pbs_filterentries WHERE 
									filtertypeID  = ".$filtertypID." AND
									filtervalue = ".$groupID." ");
					$result = $stmt->fetchAll();
					foreach($result as $r){
						$set[$filtertypID][] = $r['filterID'];	
					}
				}
				catch(Exception $e){}

				if($showPartResults)
					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 WHERE 
									filtertypeID  = '.$filtertypID.' AND
									REPLACE(filtervalue,":","") <= '.$nowShort.' AND
									REPLACE(filtervalue2,":","") >= '.$nowShort.' ');
				print_a('SELECT * FROM pbs_filterentries WHERE 
									filtertypeID  = '.$filtertypID.' AND
									REPLACE(filtervalue,":","") <= '.$nowShort.' AND
									REPLACE(filtervalue2,":","") >= '.$nowShort.' ');
				$result = $stmt->fetchAll();
				foreach($result as $r){
					$set[$filtertypID][] = $r['filterID'];	
				}
			}
			catch(Exception $e){}

			if($showPartResults)
				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{
				$select = $db->select()
					 ->from(array('pbs_filterentries')
						)
					->where('filtertypeID  = ?',$filtertypID)
					->where('`filtervalue` = ?',$hardwarehash);	
				$stmt = $select->query();
				$result = $stmt->fetchAll();
				foreach($result as $r){
					$set[$filtertypID][] = $r['filterID'];	
				}
			}
			catch(Exception $e){}

			if($showPartResults)
				print_a('hardwarehash',$result,$set[$filtertypID]);	
			#########################################################
			// weekday
			$filtertypID = 10;
			if(!$testFilter){
				$weekday = date('N');
			}

			try{
				$select = $db->select()
					 ->from(array('pbs_filterentries')
						)
					->where('filtertypeID  = ?',$filtertypID)
					->where('filtervalue = ? ',$weekday);
	
				$stmt = $select->query();
				$result = $stmt->fetchAll();
				foreach($result as $r){
					$set[$filtertypID][] = $r['filterID'];	
				}
			}
			catch(Exception $e){}

			if($showPartResults)
				print_a('weekday',$result,$set[$filtertypID]);	
			#########################################################
			// weekday
			$filtertypID = 11;
			if(!$testFilter){
				$date = date('d.m.Y');
			}

			try{
				$select = $db->select()
					 ->from(array('pbs_filterentries')
						)
					->where('filtertypeID  = ?',$filtertypID)
					->where('REPLACE(filtervalue,".","") <= ? AND ? <= REPLACE(filtervalue2,".","")',$date);
	
				$stmt = $select->query();
				$result = $stmt->fetchAll();
				foreach($result as $r){
					$set[$filtertypID][] = $r['filterID'];	
				}
			}
			catch(Exception $e){}

			if($showPartResults)
				print_a('weekday',$result,$set[$filtertypID]);	
			########################################
			// ErgebnisSet auswerten
			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;
			}	
			print_a('Übereinstimmungen pro Filter',$a);
			// Get the number of filtertypes every filter has
			$stmt = $db->query("SELECT filterID,SUM(num) as num FROM (
SELECT *, '1' as num
FROM pbs_filterentries 
GROUP BY filterID, filtertypeID
) d GROUP BY d.filterID");			 
			$resultset = $stmt->fetchAll();
			foreach($resultset as $d){
				$database[$d['filterID']] = $d['num'];
			}
			print_a('Anzahl Filterentries in Datenbank',$database);
		
			// remove all filters from resultset, which to few filtertypes satisfied
			$result = array();
			foreach($a as $i=>$b){
				if($a[$i] < $database[$i]){
					unset($a[$i]);
				}
				else{
					$result[] = $i;
				}
			}
		
			print_a('True 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);
				echo "<h1><a href='/bootmenu/index/bootmenuid/".$bootmenuID."'>Goto BootmenuID: ".$bootmenuID."</a></h1>";
				return $bootmenuID;
			}
			else{
				echo "<h1>No Result :-(</h1>";
				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);
    }
}
?>