summaryrefslogblamecommitdiffstats
path: root/application/modules/dev/controllers/PrebootController.php
blob: 985fa35f500e2d107eea62077699911eeda8f0e6 (plain) (tree)






















































                                                                                                                                                                                         
                                                             
                            


                                                                                                                            





                                                                                      
                                                                         







                                                                

                                                                                                                                           



                                                                                                   
        

                                                                                                  














                                                             
        
                                                            
        




                                                               
        



















                                                                                                                                               
                                                                                                             



                                                                                
                






























































                                                                                                                   
<?php

class dev_PrebootController extends Zend_Controller_Action
{

    public function init()
    {
   		$db = Zend_Db_Table::getDefaultAdapter();
    }

    public function indexAction()
    {
    	$prebootmapper = new Application_Model_PreBootMapper();
    	$groupmapper = new Application_Model_GroupMapper();
    	$membershipmapper = new Application_Model_MembershipMapper();
    	$personmapper = new Application_Model_PersonMapper();
    	
    	$this->view->prebootlist = $prebootmapper->fetchAll();
    	$this->view->update = array();
    	
    	foreach ($this->view->prebootlist as $preboot){
    		$this->view->update[$preboot->getID()] = $this->checkupdateAction($preboot);    
    		$preboot->setGroupID("[".$preboot->getGroupID()."] ".$groupmapper->find($preboot->getGroupID())->getTitle());
    		$preboot->setMembershipID("[".$preboot->getMembershipID()."] ".$personmapper->find($membershipmapper->find($preboot->getMembershipID())->getPersonID())->getFirstname());
    	}
    }
    
    public function createprebootAction()
    {
    		$groupmapper = new Application_Model_GroupMapper();
    	
           	if (!isset($_POST["createpreboot"])){
    	        $createprebootForm = new dev_Form_PrebootCreate(array('grouplist' => $groupmapper->fetchAll()));
    		} else {
    			
    	        $createprebootForm = new dev_Form_PrebootCreate(array('grouplist' => $groupmapper->fetchAll()),$_POST);
    			    			
    		    if ($createprebootForm->isValid($_POST)) {

    		    	$preboot = new Application_Model_PreBoot($_POST);
    		    	$preboot->setMembershipID('1');
    		        $prebootmapper = new Application_Model_PreBootMapper();
    		        
    		    	try {  
    		    		
    		      		exec("wget --spider ".$preboot->getPath_preboot()." 2>&1 | grep 'Remote file exists.'", $exists_preboot);
    		      		
				    	if(!array_pop($exists_preboot)){
 							$this->view->createprebootForm = $createprebootForm;
 							return;		
				    	}		    	
    		    		
    		            $prebootID = $prebootmapper->save($preboot);
				    							
    		            $path_preboot = "../resources/bootmedium/$prebootID/";
    		            mkdir($path_preboot ,0777, true);
    		            
						exec("wget -O '".$path_preboot."preboot.zip' ".$preboot->getPath_preboot());

						
    		        }catch(Zend_Exception $e)
    		        {
    		            echo "Caught exception: " . get_class($e) . "<br/>";
    					echo "Message: " . $e->getMessage() . "<br/>";
    					
    		        }
					$this->_redirect('/dev/preboot');
    		     }
    		}
    
            $this->view->createprebootForm = $createprebootForm;
    }
    
    public function checkupdateAction($preboot)
    {
    	$prebootID = $preboot->getID();
    	exec("wget --server-response --spider ".escapeshellcmd($preboot->getPath_preboot())." 2>&1 | grep 'Last-Modified:'", $prebootdate);

    	//print_a($prebootdate);
    	
    	$prebootdate = strtotime(trim(str_replace('Last-Modified:', '', array_pop($prebootdate))));
    	
    	if(is_file("../resources/bootmedium/$prebootID/preboot.zip"))
    		$prebootolddate = filemtime("../resources/bootmedium/".$prebootID."/preboot.zip");
    	else 
    		$prebootolddate = false;    	
    	
    	//print_a($prebootname,$prebootdate,$prebootolddate);
    	
    		
    	if($prebootdate > $prebootolddate)
    		return true;
    	else 
    		return false;
    	   	
    }
    
    public function updateprebootAction()
    {
    	
    	$prebootID = $this->_request->getParam('prebootID');
    	
    	$prebootmapper = new Application_Model_PreBootMapper();
    	$preboot = new Application_Model_PreBoot();
    	$prebootmapper->find($prebootID,$preboot);
    	
    	$path_preboot = "../resources/bootmedium/$prebootID/";
    	
		exec("wget -O '".$path_preboot."preboot.zip' ".$preboot->getPath_preboot());   
		
		$filelist = array();
		$filelist = scandir($path_preboot);
		
		$bootisoMapper = new Application_Model_BootIsoMapper();
		
		foreach($filelist as $file){
			if($file == "." || $file == ".." || $file == "preboot.zip"){
				//do nothing
			}
			else{
				$bootisoID = str_replace('.zip', '', $file);
				$serialnumber = $bootisoMapper->find($bootisoID)->getSerialnumber();
				
				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/serial", $serialnumber);  
					$zip->close(); 		            	
    		    }
			}		
		}
		
    	$this->_redirect('/dev/preboot');
    }

    public function editprebootAction()
    {
    	$prebootID = $this->_request->getParam('prebootID');
    	$groupmapper = new Application_Model_GroupMapper();
    	
    	if (!isset($_POST["editpreboot"])){
    		$prebootID = $this->_request->getParam('prebootID');
     		if (!isset($prebootID) || !is_numeric($prebootID)){
   				$this->_redirect('/dev/preboot');
    		} else {
    		   $preboot = new Application_Model_PreBoot();
    		   $prebootmapper = new Application_Model_PreBootMapper();
    		   $prebootmapper->find($prebootID, $preboot);
    		   
    		   $editprebootForm = new dev_Form_PrebootEdit(array('grouplist' => $groupmapper->fetchAll()));
    		   $editprebootForm->populate($preboot->toArray()); 
    		}
    	}else{
    		$editprebootForm = new dev_Form_PrebootEdit(array('grouplist' => $groupmapper->fetchAll()),$_POST);
    			    			
    		if ($editprebootForm->isValid($_POST)) {

    			$preboot = new Application_Model_PreBoot($_POST);
    			$preboot->setMembershipID('1');
    		    $prebootmapper = new Application_Model_PreBootMapper();
    		    $preboot->setID($prebootID);
    		        
    		   	try {  
    		       	$prebootmapper->save($preboot);
    		    }catch(Zend_Exception $e)
    		    {
    		    	echo "Caught exception: " . get_class($e) . "<br/>";
    				echo "Message: " . $e->getMessage() . "<br/>";	
    		    }
    		    
				$this->_redirect('/dev/preboot');
    		}
    			
    	}
    	
    	$this->view->editprebootForm = $editprebootForm;
    }

    public function deleteprebootAction()
    {
   		$prebootID = $this->_request->getParam('prebootID');
     	if (!isset($prebootID)){
   			$this->_redirect('/dev/preboot');
    	} else {
    		   $preboot = new Application_Model_PreBoot();
    		   $preboot->setID($prebootID);
    		   $prebootmapper = new Application_Model_PreBootMapper();
    		   $prebootmapper->delete($preboot);
    	}
    	$this->_redirect('/dev/preboot');
    }


}