summaryrefslogblamecommitdiffstats
path: root/application/modules/user/controllers/BootisoController.php
blob: 8fe750d5cb023fd2ab0dfe12fca0bf72f5809efb (plain) (tree)
1
2
3
4
5
6
7
8
9

     
                                                           

 

                                    

                                
        
                          
                
                                                      



                                                                                  
                        






                                                                                                           


                                                                    
                                                                                



                                 






















                                                                                             


                                                        

                                                                      









                                                                         
                                                                                         


                                                                        

                                                                                                     



                                                                           










                                                                                                                                       




                                                              



                                                                                                                
                                                                  


                                                
                                                        



                                                                                                                                
                
                                                                    
                                                                                

     



                                                                                  




                                                                
                
                

                                                  
                                                                                                      
                
                                                                


                                                                                                      
 


                                                                            
                                                          
                                                                                                
                



















                                                                                                                                  

                                                 
                                                                                                         







                                                                                   
                                                                                    
                                                     



                                                                                                       
                                                                                              

                        



                                                                                                       
                                                                                                     




                                                                         





















                                                                                                                                               
                                                                                                                     

                                                                                   

                                                                                                                  







                                                    
                                         
                                                                     
                                                                                                    

                                                             
                                    
                                                                                                            
                        


                                                               
                                                                            



                                                                           

                                                         
                   




                                                                                                    



                                                                                                       
                                                                                                


                                                             



                                                                                                       
                                                                                                       


                                                    
                                               

                                                                         








                                                                                           
                                                                                
                                                              
                                                                                                                            

                     
                    














                                                                                                                                   
                                                                                                                


                                                              
                                                                                                                




                                                

     
                                         
                

                                        
                                                                                                    

                
 

                                                                            
                                                                                                                    




                                                                                     
                                                                                                                    





                                                                    
                                                                                                        
         
                                                                                                     

     

 





 
<?php

class user_BootisoController extends Zend_Controller_Action
{

	protected $bootisoMapper;
	protected $membershipMapper;
	protected $membership;	
	protected $page;
	
    public function init()
    {    	
        if (Zend_Auth::getInstance()->hasIdentity()) {
			$userIDsNamespace = Zend_Session::namespaceGet('userIDs');
			if($userIDsNamespace['membershipID'] ==''){
				$this->_redirect('/user/index');
			}
			
			$this->bootisoMapper = new Application_Model_BootIsoMapper();
			
			$this->membershipMapper = new Application_Model_MembershipMapper();
			$this->membership = new Application_Model_Membership();
			$this->membershipMapper->find($userIDsNamespace['membershipID'],$this->membership);

			$this->db = Zend_Db_Table::getDefaultAdapter();
		} else {
			$this->_helper->redirector('login', 'auth');
		}
		$this->page = $this->_request->getParam('page');		
    }

    public function indexAction()
    {
        $result = $this->_request->getParam('addresult');
		if($result != ""){
			$pbsNotifier = new Pbs_Notifier();
			$this->view->notification = $pbsNotifier->notify('create',$result);
		}
		$result = $this->_request->getParam('deleteresult');
		if($result != ""){
			$pbsNotifier = new Pbs_Notifier();
			$this->view->notification = $pbsNotifier->notify('delete',$result);
		}
		$result = $this->_request->getParam('modifyresult');
		if($result != ""){
			$pbsNotifier = new Pbs_Notifier();
			$this->view->notification = $pbsNotifier->notify('modify',$result);
		}
        $result = $this->_request->getParam('downloadresult');
		if($result != ""){
			$pbsNotifier = new Pbs_Notifier();
			$this->view->notification = $pbsNotifier->notify('download',$result);
		}
		
    	
		$groupMapper = new Application_Model_GroupMapper();    	
    	
    	$groupID = $this->membership->getGroupID();    	
    	
    	//ACL Darf er BootISOMenu sehen?
    	if(!Pbs_Acl::checkRight('bai') && !Pbs_Acl::checkRight('bui'))
    		$this->_redirect('/user/index');

    	$groupgroupsMapper = new Application_Model_GroupGroupsMapper();
    	
    	$parents = array();
    	$this->view->bootisolist = array();
 		$parents = $groupgroupsMapper->getParentGroups($groupID);
 		   	
    	foreach($parents as $k => $parent){
    		foreach($parent as  $p){
    			 $bootiso = $this->bootisoMapper->findBy(array("groupID" => $p));
    			 foreach ($bootiso as $b)
    			 	if($b->getPublic() - $k >= 0 )
    			 		$this->view->bootisolist[] = $b;
    			 	elseif($k == 0 && $b->getPublic() == -1 && Pbs_Acl::checkRight('bc'))
    			 		$this->view->bootisolist[] = $b;
    		}
    	}
    	
    	$this->view->bootisolist = array_reverse($this->view->bootisolist);
		
    	$prebootMapper = new Application_Model_PreBootMapper(); 
       	$this->view->prebootlist = array();

    	foreach ($this->view->bootisolist as $bootiso){
    		  $bootiso->setPrebootID("[".$bootiso->getPrebootID()."] ".$prebootMapper->find($bootiso->getPrebootID())->getTitle());
    		  $bootiso->setGroupID("[".$bootiso->getGroupID()."] ".$groupMapper->find($bootiso->getGroupID())->getTitle());
    		  $bootiso->setCreated(date(Zend_Registry::get('dateformat'),$bootiso->getCreated()));
    		  $bootiso->setExpires(date(Zend_Registry::get('dateformat'),$bootiso->getExpires()));
    	}
    
    	// Search
		$search = $this->_request->getParam('search');
		$mySearch = new Pbs_Search();
		$mySearch->setSearchTerm($search);
		$mySearch->setModule('bootiso');
		if($search != ''){
			$this->view->search = $mySearch->getSearchTerm();			
			$this->view->bootisolist = $mySearch->search($this->view->bootisolist);			
		}
		$this->view->searchform = $mySearch->searchForm();
    	
	    // Pagination
    	$pagination = new Pbs_Pagination();	
		$pagination->setPerPage(10);		
		$pagination->setElement($this->view->bootisolist);
		$pagination->setRequestPage($this->_request->getParam('page'));		
		$pagination->setPageUrl('/user/bootiso/index'.((isset($this->view->search))?'/search/'.$this->view->search:''));
		$this->view->bootisolist = $pagination->getElements();		
		
		$this->view->pagination = $pagination->pagination();
		$this->view->page 		= $pagination->getRequestPage();
    }
    
    public function searchAction(){
		$this->_redirect('/user/bootiso/index/search/'.($_GET['search']));
	}
    
    public function downloadbootisoAction()
    {
    	$this->_helper->layout->disableLayout();
		$this->_helper->viewRenderer->setNoRender(true);
		
		
		
		//ACL Darf er BootISOs downloaden?
		if(!Pbs_Acl::checkRight('bdld'))
    		$this->_redirect('/user/bootiso/index/page/'.$this->page.'/downloadresult/forbidden');
		
    	$bootisoID = $this->_request->getParam('bootisoID');   	
    	
    	if(!is_numeric($bootisoID))
    		$this->_redirect('/user/bootiso/index/page/'.$this->page.'/downloadresult/forbidden');

    	$prebootID = $this->bootisoMapper->find($bootisoID)->getPrebootID();
    		
    		
    	if(!is_dir("../resources/bootmedium/$prebootID/"))
    		$this->_redirect('/user/bootiso/index/page/'.$this->page.'/downloadresult/404');
    		
    	chdir("../resources/bootmedium/$prebootID/");
    
    	header("X-Sendfile: $bootisoID".".zip");
		header('Content-Type: application/x-gzip');
		$content_disp = ( ereg('MSIE ([0-9].[0-9]{1,2})', $_SERVER['HTTP_USER_AGENT']) == 'IE') ? 'inline' : 'attachment';
		header('Content-Disposition:  ' . $content_disp . '; filename="preboot.zip"');
		header('Pragma: no-cache');
		header('Expires: 0');	
			
    	$handle = fopen($bootisoID.".zip", 'r');
		$chunk_size = 8192;
		while ($chunk = fread($handle, $chunk_size)) {
			echo $chunk;
			ob_flush();
		}
    
    }

    public function createbootisoAction()
    {
    		//ACL Darf er BootISOs erstellen?
    		if(!Pbs_Acl::checkRight('bc'))
    			$this->_redirect('/user/bootiso/index/page/'.$this->page.'/addresult/forbidden');

    		$groupgroupsMapper = new Application_Model_GroupGroupsMapper();
    		$groupID = $this->membership->getGroupID();
    		
    		$groupgroupsMapper = new Application_Model_GroupGroupsMapper();
    		$childgroups = count($groupgroupsMapper->getChildGroups($groupID));
    		
    		$prebootMapper = new Application_Model_PreBootMapper();
    		$prebootlist = $prebootMapper->findBy(array("groupID" => $groupID));
           	if (!isset($_POST["createbootiso"])){
    	        $bootisoForm = new user_Form_Bootiso(array(
    	        							'action' => 'createbootiso',
    	        							'prebootlist' => $prebootlist, 
    	        							'groupdepth' => $childgroups, 
    	        							'page'=>$this->page));
    		} else {
    			
    	        $bootisoForm = new user_Form_Bootiso(array(
    	        							'action' => 'createbootiso',
    	        							'prebootlist' => $prebootlist, 
    	        							'groupdepth' => $childgroups, 
    	        							'page'=>$this->page),$_POST);
    			    			
    		    if ($bootisoForm->isValid($_POST)) {

    		    	$bootiso = new Application_Model_BootIso($_POST);
    		    	
    		    	$bootiso->setGroupID($this->membership->getGroupID());    	
    		        $bootiso->setCreated(time()); 
    		        
    		        $prebootID = $bootiso->getPrebootID();     
    		        
    		    	try {  
    		    		
    		            $bootisoID = $this->bootisoMapper->save($bootiso);
    		            
    		            copy("../resources/bootmedium/$prebootID/preboot.zip", "../resources/bootmedium/$prebootID/$bootisoID".".zip");
    		            $zip = new ZipArchive();
    		            $res = $zip->open("../resources/bootmedium/$prebootID/$bootisoID".".zip");
    		            if($res === true){
    		            	$rootdir = $zip->getNameIndex(0);
							$zip->addFromString($rootdir."build/rootfs/tmp/serial", $bootiso->getSerialnumber());  
							$zip->close(); 		            	
    		            }
	
    		        }catch(Zend_Exception $e)
    		        {
    		            echo "Caught exception: " . get_class($e) . "<br/>";
    					echo "Message: " . $e->getMessage() . "<br/>";
    					$this->_redirect('/user/bootiso/index/page/'.$this->page.'/addresult/error');
    					//TODO Delete File & delete bootiso from DB
    					
    		        }    		        
					$this->_redirect('/user/bootiso/index/page/'.$this->page.'/addresult/ok');
    		     }
    		}
    
            $this->view->bootisoForm = $bootisoForm;
    }

    public function editbootisoAction()
    {
    	//ACL Darf er BootISOs editieren?
    	if(!Pbs_Acl::checkRight('be') && !Pbs_Acl::checkRight('bem'))
    		$this->_redirect('/user/bootiso/index/page/'.$this->page.'/modifyresult/forbidden');
    	
    	$bootisoID = $this->_request->getParam('bootisoID'); 
    	if (!is_numeric($bootisoID))
   			$this->_redirect('/user/bootiso/index/page/'.$this->page.'/modifyresult/forbidden');
   			
    	$groupID = $this->membership->getGroupID();
    	
    	$prebootMapper = new Application_Model_PreBootMapper();
    	$prebootlist = $prebootMapper->findBy(array("groupID" => $groupID));
    	
    	$groupgroupsMapper = new Application_Model_GroupGroupsMapper();
    	$childgroups = count($groupgroupsMapper->getChildGroups($groupID));
    	
    	$bootiso = new Application_Model_BootIso();
    	$this->bootisoMapper->find($bootisoID, $bootiso);
    		   
    	if($this->membership->getGroupID() != $bootiso->getGroupID())
    		$this->_redirect('/user/bootiso/index/page/'.$this->page.'/modifyresult/forbidden');
    	
    	if (!isset($_POST["editbootiso"])){

    		$bootisoForm = new user_Form_Bootiso(array(
    									'action' => 'editbootiso',
    									'prebootlist' => $prebootlist, 
    									'groupdepth' => $childgroups, 
    									'page' => $this->page));
    		$bootisoForm->populate($bootiso->toArray()); 
    		
    	}else{
    		$bootisoForm = new user_Form_Bootiso(array(
    									'action' => 'editbootiso',
    									'prebootlist' => $prebootlist, 
    									'groupdepth' => $childgroups, 
    									'page' => $this->page),$_POST);
    			    			
    		if ($bootisoForm->isValid($_POST)) {

    			$bootisoold = $bootiso;
    			
    			$bootiso = new Application_Model_BootIso($_POST);
    			$bootiso->setGroupID($this->membership->getGroupID());
    		    $bootiso->setCreated(time());
    		    $bootiso->setID($bootisoID);
    		    $prebootID = $bootiso->getPrebootID();
    		    
    		  	if(	$bootiso->getPrebootID() != $bootisoold->getPrebootID() || 
    		        $bootiso->getExpires() != $bootisoold->getExpires() || 
    		        $bootiso->getPublic() != $bootisoold->getPublic() || 
    		        $bootiso->getSerialnumber() != $bootisoold->getSerialnumber()){
    				//ACL Is he allowed to edit other than Metadata?
	    			if(!Pbs_Acl::checkRight('be'))
	    				$this->_redirect('/user/bootiso/index/page/'.$this->page.'/modifyresult/forbidden');
    		    }
    		    
    		    
    		   	try {  
    		   	    $zip = new ZipArchive();
    		        $res = $zip->open("../resources/bootmedium/$prebootID/$bootisoID".".zip");
    		        if($res === true){
    		            $rootdir = $zip->getNameIndex(0);
						$zip->addFromString($rootdir."build/rootfs/serial", $bootiso->getSerialnumber());  
						$zip->close(); 		            	
    		        }
    		            
    		   		$this->bootisoMapper->save($bootiso);
    		       	
    		    }catch(Zend_Exception $e)
    		    {
    		    	echo "Caught exception: " . get_class($e) . "<br/>";
    				echo "Message: " . $e->getMessage() . "<br/>";	
    				$this->_redirect('/user/bootiso/index/page/'.$this->page.'/modifyresult/error');
    				//TODO Redo Serial in Files...
    		    }
    		    
				$this->_redirect('/user/bootiso/index/page/'.$this->page.'/modifyresult/ok');	
    		}
    			
    	}
    	
    	$this->view->bootisoForm = $bootisoForm;
    }

    public function deletebootisoAction()
    {    	
    	//ACL Darf er BootISOs löschen?
    	if(!Pbs_Acl::checkRight('bd'))
    		$this->_redirect('/user/bootiso/index/page/'.$this->page.'/deleteresult/forbidden');
    		
    	try{

	   		$bootisoID = $this->_request->getParam('bootisoID');
	     	if (!is_numeric($bootisoID))
	   			$this->_redirect('/user/bootiso/index/page/'.$this->page.'/deleteresult/forbidden');
		   		
	    	$bootiso = new Application_Model_BootIso();
	    	$this->bootisoMapper->find($bootisoID,$bootiso);
	    	   			
	   		if($this->membership->getGroupID() != $bootiso->getGroupID())
		   		$this->_redirect('/user/bootiso/index/page/'.$this->page.'/deleteresult/forbidden');
	    	
	    	$this->bootisoMapper->delete($bootiso);
    	
    	}catch(Zend_Exception $e){
    		echo "Caught exception: " . get_class($e) . "<br/>";
    		echo "Message: " . $e->getMessage() . "<br/>";
    		$this->_redirect('/user/bootiso/index/page/'.$this->page.'/deleteresult/error');	
    	}
	   		$this->_redirect('/user/bootiso/index/page/'.$this->page.'/deleteresult/ok');
    }
   
}