summaryrefslogblamecommitdiffstats
path: root/application/modules/user/controllers/BootisoController.php
blob: 20f57746ca1a06c7aa5dc891e1ee55ef03d3c509 (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();    	
    	$personMapper = new Application_Model_PersonMapper();
    	
    	$groupID = $this->membership->getGroupID();    	
    	
    	//TODO ACL Darf er BootISOMenu sehen?
    	if(false)
    		$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("groupID", $p);
    			 foreach ($bootiso as $b)
    			 	if($b->getPublic() - $k >= 0 )
    			 		$this->view->bootisolist[] = $b;
    		}
    	}
    	
    	$this->view->bootisolist = array_reverse($this->view->bootisolist);
    	
    	// Pagination
    	$perpage 	= 2;
		$req_page 	= $this->_request->getParam('page');
		$all 		= count($this->view->bootisolist);
		$numpages 	= ceil($all/$perpage);		
		if($req_page < 0 || !is_numeric($req_page) )
			$req_page = 0;
		if($req_page >= $numpages)
			$req_page = $numpages-1;
		$startitem 	= $req_page * $perpage;		
		
		$pagination = new Pbs_Pagination();	
		$this->view->pagination = $pagination->pagination('/user/bootiso/index',$req_page,$numpages);
		$this->view->page = $req_page;
		$this->view->bootisolist = array_slice($this->view->bootisolist,$startitem,$perpage);    	
    	
    	
    	$prebootMapper = new Application_Model_PreBootMapper(); 
       	$this->view->prebootlist = array();

    	foreach ($this->view->bootisolist as $bootiso){
    		  $this->view->prebootlist[$bootiso->getID()] = $prebootMapper->find($bootiso->getPrebootID())->getTitle();
    		  $bootiso->setGroupID("[".$bootiso->getGroupID()."] ".$groupMapper->find($bootiso->getGroupID())->getTitle());
    		  $bootiso->setMembershipID("[".$bootiso->getMembershipID()."] ".$personMapper->find($this->membershipMapper->find($bootiso->getMembershipID())->getPersonID())->getFirstname());
    	}
    
    }
    
    public function downloadbootisoAction()
    {
    	$this->_helper->layout->disableLayout();
		$this->_helper->viewRenderer->setNoRender(true);
		
		
		
		//TODO ACL Darf er BootISOs downloaden?
		if(false)
    		$this->_redirect('/user/bootiso/index/page/'.$this->page.'/downloadresult/forbidden');
		
    	$prebootID = $this->_request->getParam('prebootID');
    	$bootisoID = $this->_request->getParam('bootisoID');   	

    	if(!is_dir("../resources/bootmedium/$prebootID/"))
    		$this->_redirect('/user/bootiso/index/page/'.$this->page.'/downloadresult/404');
    		
    	if(!is_numeric($prebootID) || !is_numeric($bootisoID))
    		$this->_redirect('/user/bootiso/index/page/'.$this->page.'/downloadresult/forbidden');
			 
    	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()
    {
    		//TODO ACL Darf er BootISOs erstellen?
    		if(false)
    			$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("groupID", $groupID);
           	if (!isset($_POST["createbootiso"])){
    	        $bootisoForm = new user_Form_Bootiso(array(
    	        							'action' => 'createbootiso',
    	        							'prebootlist' => $prebootlist, 
    	        							'groupdepth' => $childgroups, 
    	        							'rights' => null,
    	        							'page'=>$this->page));
    		} else {
    			
    	        $bootisoForm = new user_Form_Bootiso(array(
    	        							'action' => 'createbootiso',
    	        							'prebootlist' => $prebootlist, 
    	        							'groupdepth' => $childgroups, 
    	        							'rights' => null,
    	        							'page'=>$this->page),$_POST);
    			    			
    		    if ($bootisoForm->isValid($_POST)) {

    		    	$bootiso = new Application_Model_BootIso($_POST);
    		    	
    		    	$bootiso->setMembershipID($this->membership->getID());
    		    	$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()
    {
    	//TODO ACL Darf er BootISOs editieren?
    	if(false)
    		$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("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, 
    									'rights' => 'meta',
    									'page' => $this->page));
    		$bootisoForm->populate($bootiso->toArray()); 
    		
    	}else{
    		$bootisoForm = new user_Form_Bootiso(array(
    									'action' => 'editbootiso',
    									'prebootlist' => $prebootlist, 
    									'groupdepth' => $childgroups, 
    									'rights' => 'meta',
    									'page' => $this->page),$_POST);
    			    			
    		if ($bootisoForm->isValid($_POST)) {

    			$bootisoold = $bootiso;
    			
    			$bootiso = new Application_Model_BootIso($_POST);
    			$bootiso->setMembershipID($this->membership->getID());
    			$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()){
    				//TODO ACL Is he allowed to edit other than Metadata?
	    			if(false)
	    				$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()
    {    	
    	//TODO ACL Darf er BootISOs löschen?
    	if(false)
    		$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');
    }
   
}