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

     
                                                          

 



                                    

                          















                                                                                                           



                                 



















                                                                                           
        

                                                                        
        






                                                             
        
                                                                                   

                                      

                                                           

                                                                     



                                                                                                                                                                                                              







                                        






                                                                            




































































































































































                                                                                                                                                               




     





 
<?php

class user_BootosController extends Zend_Controller_Action
{

	protected $bootosMapper;
	protected $membershipMapper;
	protected $membership;
	
    public function init()
    {
        if (Zend_Auth::getInstance()->hasIdentity()) {
			$userIDsNamespace = Zend_Session::namespaceGet('userIDs');
			if($userIDsNamespace['membershipID'] ==''){
				$this->_redirect('/user/index');
			}
			
			$this->bootosMapper = new Application_Model_BootOsMapper();
			
			$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');
		}
    }

    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('updateresult');
		if($result != ""){
			$pbsNotifier = new Pbs_Notifier();
			$this->view->notification = $pbsNotifier->notify('update',$result);
		}
    	
		$groupMapper = new Application_Model_GroupMapper();    	
    	$personMapper = new Application_Model_PersonMapper();
    	
    	$groupID = $this->membership->getGroupID();  
		
		//TODO ACL Darf er BootOsMenu sehen?
    	if(false)
    		$this->_redirect('/user/index');
    		
       	$configMapper = new Application_Model_ConfigMapper();
    	
    	$this->view->bootoslist = $this->bootosMapper->findBy("groupID", $groupID);
    	$this->view->update = array();
    	
    	$update = $this->_request->getParam('checkupdate');
    	
    	if(count($this->view->bootoslist)>0){
			foreach ($this->view->bootoslist as $bootos){
				  $this->view->update[$bootos->getID()] = $update && $this->checkupdateAction($bootos);
				  $bootos->setGroupID("[".$bootos->getGroupID()."] ".$groupMapper->find($bootos->getGroupID())->getTitle());
				  $bootos->setMembershipID("[".$bootos->getMembershipID()."] ".$personMapper->find($this->membershipMapper->find($bootos->getMembershipID())->getPersonID())->getFirstname());
				  $bootos->setConfigID("[".$bootos->getConfigID()."] ".$configMapper->find($bootos->getConfigID())->getTitle());
				  
			}
		}
    	
    }

    public function createbootosAction()
    {
    	
    	//TODO ACL Darf er BootISOs erstellen?
    	if(false)
    		$this->_redirect('/user/bootiso/index/addresult/forbidden');
    	
    	$groupMapper = new Application_Model_GroupMapper();
    	$configMapper = new Application_Model_ConfigMapper();    	
    	
    	if (!isset($_POST["createbootos"])){
    		try{
    	        $createbootosForm = new user_Form_BootosCreate(array('grouplist' => $groupmapper->fetchAll() ,'configlist'=>$configmapper->fetchAll()));
    	    }catch(Zend_Exception $e){
    	        print_a($e);
    	    }
    	} else {
    			
    	        $createbootosForm = new user_Form_BootosCreate(array('grouplist' => $groupmapper->fetchAll() ,'configlist'=>$configmapper->fetchAll()),$_POST);
    			    			
    		    if ($createbootosForm->isValid($_POST)) {

    		    	$bootos = new Application_Model_BootOs($_POST);
    		    	
    		    	$bootos->setMembershipID('1');
    		    	if($bootos->getConfigID() == '')
    		    		$bootos->setConfigID(NULL);
    		        $bootosmapper = new Application_Model_BootOsMapper();
    		        $bootos->setCreated(time());
    		        
    		    	try {  
    		    		exec("wget --spider ".$bootos->getPath_kernel()." 2>&1 | grep 'Remote file exists.'", $exists_kernel);
    					exec("wget --spider ".$bootos->getPath_init()." 2>&1 | grep 'Remote file exists.'" , $exists_init);  
    		      		
				    	if(!array_pop($exists_init) || !array_pop($exists_kernel)){
 							$this->view->createbootosForm = $createbootosForm;
 							return;		
				    	}		    	
    		    		
    		            $bootosID = $bootosmapper->save($bootos);
						
    		            $initpath = "../resources/bootos/".$bootosID."/initramfs/";
    		            $kernelpath = "../resources/bootos/".$bootosID."/kernel/";

    		            mkdir($initpath ,0777, true);
    		            mkdir($kernelpath ,0777, true);
    		            
    		      		exec("wget -O  '".$kernelpath."kernel' ".$bootos->getPath_kernel());
    		      		exec("wget -O  '".$initpath."initramfs' ".$bootos->getPath_init());    	

    		      		
    		            
    		        }catch(Zend_Exception $e)
    		        {
    		            echo "Caught exception: " . get_class($e) . "<br/>";
    					echo "Message: " . $e->getMessage() . "<br/>";
    					
    		        }
    		        
					$this->_redirect('/user/bootos');
    		     }
    		}
    
            $this->view->createbootosForm = $createbootosForm;
    }
    
    public function checkupdateAction($bootos)
    {
    	exec("wget --server-response --spider '".escapeshellcmd($bootos->getPath_kernel())."' 2>&1 | grep 'Last-Modified:'", $kerneldate);
    	exec("wget --server-response --spider '".escapeshellcmd($bootos->getPath_init())."' 2>&1 | grep 'Last-Modified:'", $initdate);
	 	
    	$kerneldate = strtotime(trim(str_replace('Last-Modified:', '', array_pop($kerneldate))));
    	$kernelname = array_pop(scandir("../resources/bootos/".$bootos->getID()."/kernel/"));
    	
    	if($kernelname != "." && $kernelname != "..")
    		$kernelolddate = filemtime("../resources/bootos/".$bootos->getID()."/kernel/".$kernelname);
    	else 
    		$kernelolddate = false;
    	
    	$initdate = strtotime(trim(str_replace('Last-Modified:', '', array_pop($initdate))));
    	$initname = array_pop(scandir("../resources/bootos/".$bootos->getID()."/initramfs/"));
    	
    	if($initname != "." && $initname != "..")
    		$initolddate = filemtime("../resources/bootos/".$bootos->getID()."/initramfs/".$initname);
    	else 
    		$initolddate = false;    	
    	
    	//print_a($kernelname,$kerneldate,$kernelolddate,$initname,$initdate,$initolddate);
    	
    		
    	if($kerneldate > $kernelolddate || $initdate > $initolddate)
    		return true;
    	else 
    		return false;
    	   	
    }
    
    public function updatebootosAction()
    {
    	$bootosID = $this->_request->getParam('bootosID');
    	$bootosmapper = new Application_Model_BootOsMapper();
    	$bootos = $bootosmapper->find($bootosID);
    	
    	$initpath = "../resources/bootos/".$bootosID."/initramfs/";
    	$kernelpath = "../resources/bootos/".$bootosID."/kernel/";
    		            
    	exec("wget -P  ".$kernelpath." -N ".$bootos->getPath_kernel());
    	exec("wget -P  ".$initpath." -N ".$bootos->getPath_init());  
    	
    	$this->_redirect('/user/bootos');
    }

    public function editbootosAction()
    {
    	$bootosID = $this->_request->getParam('bootosID');
    	$groupmapper = new Application_Model_GroupMapper();
    	$configmapper = new Application_Model_ConfigMapper();
    	
    	if (!isset($_POST["editbootos"])){
    		$bootosID = $this->_request->getParam('bootosID');
     		if (!isset($bootosID) || !is_numeric($bootosID)){
   				$this->_redirect('/user/bootos');
    		} else {
    		   $bootos = new Application_Model_BootOs();
    		   $bootosmapper = new Application_Model_BootOsMapper();
    		   $bootos = $bootosmapper->find($bootosID);
    		   
    		   $editbootosForm = new user_Form_BootosEdit(array('grouplist' => $groupmapper->fetchAll() ,'configlist'=>$configmapper->fetchAll()));
    		   $editbootosForm->populate($bootos->toArray()); 
    		}
    	}else{
    		$editbootosForm = new user_Form_BootosEdit(array('grouplist' => $groupmapper->fetchAll() ,'configlist'=>$configmapper->fetchAll()),$_POST);
    			    			
    		if ($editbootosForm->isValid($_POST)) {

    			//TODO: Check for new URL and download new files.
    			$bootos = new Application_Model_BootOs($_POST);
    			$bootos->setMembershipID('1');
    		   	if($bootos->getConfigID() == '')
    		    	$bootos->setConfigID(NULL);
    		    $bootosmapper = new Application_Model_BootOsMapper();
    		    $bootos->setCreated(time());
    		    $bootos->setID($bootosID);
    		        
    		   	try {  
    		       	$bootosmapper->save($bootos);
    		    }catch(Zend_Exception $e)
    		    {
    		    	echo "Caught exception: " . get_class($e) . "<br/>";
    				echo "Message: " . $e->getMessage() . "<br/>";	
    		    }
    		    
				$this->_redirect('/user/bootos');
    		}
    			
    	}
    	
    	$this->view->editbootosForm = $editbootosForm;
    	
    }

    public function deletebootosAction()
    {
    	$bootosID = $this->_request->getParam('bootosID');
     	if (!isset($bootosID)){
   			$this->_redirect('/user/bootos');
    	} else {
    		   $bootos = new Application_Model_BootOs();
    		   $bootos->setID($bootosID);
    		   $bootosmapper = new Application_Model_BootOsMapper();
    		   $bootosmapper->delete($bootos);
    	}
    	$this->_redirect('/user/bootos');
    		
    }


}