summaryrefslogblamecommitdiffstats
path: root/application/controllers/ResourceController.php
blob: f6b63e292e4fbb9abbb0835d758e3b5a2734e004 (plain) (tree)
1
2
3
4
5
6
7
8
9
10





                                                       
                      


                              

                                                            
                

                                                            
                
                                                         
                                                            
                                  
                                                 
                                                                                               




                                                                              
                                                                                        

                                                                        




                                                                                                         
                                                                             
                                                                                     
                                                


                                                                            
                                                                                                                  



                                                        
                        

                        
























                                                                                                                                          
                                                                                    
                        
                                                    

                                                         




                                         
                




                                                                            
                

                                                          

                                                             




                                                                                                                                          






























                                                                                                                                                                                                   

                                                         
                        



                                         
                













                                                                                                                                          
                                                                                      
                        
                                                       

                                                         
                 
                



                                      
                
 





                                                                                   
                                                                             







                                                                                                                                          


                                                                                         
                                                                                                                                                                                                                                                  
                              
                                                                                                                                                                                                                                          
                         
                        
                                                                     
                        












                                                                                           
                





                                                                                                                                         
                                                                                                                                    

                  





                                                                                                                                         
                                                                                                                                        

                                        


                                                                              
                
                                                                                                                                                                    











                                                                    
<?php

class ResourceController extends Zend_Controller_Action
{

	private $thisSession;
	private $page;

	public function init()
	{
		$this->_helper->layout->disableLayout();
		$this->_helper->viewRenderer->setNoRender();
		
		$session = new Application_Model_Session();
		$sm = new Application_Model_SessionMapper();
		
		//TODO Error Messages if something failed
		$alpha = $this->_request->getParam('alpha');
		if($alpha != "0"){
			$alphasessionID = $alpha;
			$result = $sm->findBy(array('alphasessionID' => $alphasessionID),true);
			# print_a($result);
			$this->thisSession = $session->setOptions($result[0]);
			$this->thisSession->setID($result[0]['sessionID']);
		}
		else{
			$bootmenuentryID = $this->_request->getParam('bootmenuentryID');

			$this->page = $this->_request->getParam('page');
			if(!is_numeric($bootmenuentryID))
				$this->_redirect('/user/bootmenu/index/page/'.$this->page.'/json/error');
			
			$bootmenuentry = new Application_Model_BootMenuEntries();
			$bootmenuentryMapper = new Application_Model_BootMenuEntriesMapper();
			$bootosMapper = new Application_Model_BootOsMapper();
			$bootmenuentryMapper->find($bootmenuentryID, $bootmenuentry);
						
			$session->setBootosID($bootmenuentry->getBootosID())
			->setBootmenuentryID($bootmenuentry->getID())
			->setTime(time())
			->setMembershipID($bootosMapper->find($bootmenuentry->getBootmenuID())->getMembershipID())
			->setIp($_SERVER['REMOTE_ADDR'])
			->setAlphasessionID('0');
			
		    $this->thisSession = $session;
			
		}
			
	}

	public function indexAction()
	{
		 
		 
	}

	public function getinitramfsAction()
	{

		$bootosID = $this->thisSession->getBootosID();

		if(is_dir("../resources/bootos/$bootosID/initramfs/") && is_numeric($bootosID)){
			 
			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="initramfs"');
			header('Pragma: no-cache');
			header('Expires: 0');
			 
			// create file.
			chdir("../resources/bootos/$bootosID/initramfs/");
			$initname = array_pop(scandir("./"));
			
			header("Content-Length: ".filesize(getcwd()."/".$initname));
			
			passthru( "cat ".$initname);
		}else{
			header('HTTP/1.0 404 Not Found');
		}
	}

	public function getconfigAction()
	{
		
		$bootmenuentryID = $this->thisSession->getBootmenuentryID();

		$bootmenuentry = new Application_Model_BootMenuEntries();
		$bmm = new Application_Model_BootMenuEntriesMapper();
		$bmm->find($bootmenuentryID,$bootmenuentry);
		
		$configID = $bootmenuentry->getConfigID();

		if(is_dir("../resources/config/$configID/")){
			
			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="default.tgz"');
			header('Pragma: no-cache');
			header('Expires: 0');
			
			if($this->thisSession->getMembershipID() != null){

				$path_userconf = "../resources/config/$configID/".$this->thisSession->getMembershipID()."/";
			
				if(!is_dir($path_userconf)){
					
					$person = new Application_Model_Person();
					$personMapper = new Application_Model_PersonMapper();
					$membershipMapper = new Application_Model_MembershipMapper();
					$personID = $membershipMapper->find($this->thisSession->getMembershipID())->getPersonID();
					$person = $personMapper->find($personID);
					$loginname = strtolower($person->getName());
					$loginname = preg_replace("!\s!","",$loginname);
					
					mkdir($path_userconf ,0777, true);
					exec("tar -C $path_userconf -xvf ../resources/config/$configID/default.tgz");
					exec("cp ../resources/config/loginfiles/* $path_userconf/rootfs/etc/");
					exec("tar -C $path_userconf -xvf ../resources/config/$configID/default.tgz");
					exec("echo '$loginname:x:1000:1000:".$person->getName()." ".$person->getFirstname().",,,:/home/$loginname:/bin/bash' >> $path_userconf/rootfs/etc/passwd");
					exec("md5pass ".$person->getPassword(), $pwhash);
					exec("echo '$loginname:$pwhash[0]:0:0:99999:7:::' >> $path_userconf/rootfs/etc/shadow");
					exec("echo 'mkdir /home/$loginname' >> $path_userconf/initramfs/postinit.local");
				}
				// create the gzipped tarfile.
				chdir($path_userconf);
				passthru( "tar cz *");
			}else {
				chdir("../resources/config/$configID/");
				passthru( "cat default.tgz");	
			}	
		}else{
			header('HTTP/1.0 404 Not Found');
		}	
	}

	public function getkernelAction()
	{
		
		$bootosID = $this->thisSession->getBootosID();

		if(is_dir("../resources/bootos/$bootosID/kernel/") && is_numeric($bootosID)){
			 
			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="kernel"');
			header('Pragma: no-cache');
			header('Expires: 0');
			 
			// create the gzipped tarfile.
			chdir("../resources/bootos/$bootosID/kernel/");
			$kernelname = array_pop(scandir("./"));
			
			header("Content-Length: ".filesize(getcwd()."/".$kernelname));
			
			passthru( "cat ". $kernelname);
		}else{
			header('HTTP/1.0 404 Not Found');
		}
		
	}

	public function getkclAction()
	{
		

		$bmeID = $this->thisSession->getBootmenuentryID();

		if(is_numeric($bmeID)){
			
			$bmemapper = new Application_Model_BootMenuEntriesMapper();
			$bme = new Application_Model_BootMenuEntries();
			$bootosmapper = new Application_Model_BootOsMapper();
			$bmemapper->find($bmeID,$bme);
				
			header('Content-Type: text/html');
			$content_disp = ( ereg('MSIE ([0-9].[0-9]{1,2})', $_SERVER['HTTP_USER_AGENT']) == 'IE') ? 'inline' : 'attachment';
			header('Content-Disposition:  ' . $content_disp . '; filename="kcl.txt"');
			header('Pragma: no-cache');
			header('Expires: 0');
			
			$kcl = $bootosmapper->find($bme->getBootosID())->getDefaultkcl();
			
			if($bme->getKcl() && $kcl != null){ 
				$result = $kcl . " alpha=" . $this->_request->getParam('alpha') . " file=http://".$_SERVER['HTTP_HOST']."/resource/getconfig/alpha/".$this->_request->getParam('alpha')."/file/default.tgz ".$bme->getKclappend();
			}else{
				$result = "alpha=" . $this->_request->getParam('alpha') . " file=http://".$_SERVER['HTTP_HOST']."/resource/getconfig/alpha/".$this->_request->getParam('alpha')."/file/default.tgz ".$bme->getKclappend();
			}
			
			header("Content-Length: ".(strlen($result)));
			
			echo $result;
			

		}

	}

	public function getbootmenuentryAction()
	{
		// obsolete function (now only for debugging)
		// after selecting the BootOS it will be saved in session
		// so getkclAction, getkernelAction, getconfigAction and getinitramfsAction
		// can be called with session-identifier
		
		$return_val =
		array(
   			'info' => 'This function is for debugging purpose only',
   			'kernel' => $this->generateURL('getkernel', 'alpha', $this->thisSession->getAlphasessionID(), "kernel"), 
   			'initramfs' => $this->generateURL('getinitramfs', 'alpha', $this->thisSession->getAlphasessionID(), "initramfs"),
   			'kcl' => $this->generateURL('getkcl', 'alpha', $this->thisSession->getAlphasessionID(), "kcl.txt"),
   			'config' => $this->generateURL('getconfig', 'alpha', $this->thisSession->getAlphasessionID(), 'default.tgz')
		);
		
		$result = $return_val;
		$result2 = "<table class='json'>";
		$result2 .= "<tr><td><b>info</b>: </td><td>".$result['info']."</td></tr>";
		$result2 .= "<tr><td><b>kernel</b>:  </td><td><a href=".$result['kernel'].">".$result['kernel']."<a></td></tr>";
		$result2 .= "<tr><td><b>initramfs</b>:  </td><td><a href=".$result['initramfs'].">".$result['initramfs']."<a></td></tr>";
		$result2 .= "<tr><td><b>kcl</b>:  </td><td><a href=".$result['kcl'].">".$result['kcl']."<a></td></tr>";
		$result2 .= "<tr><td><b>config</b>:  </td><td><a href=".$result['config'].">".$result['config']."<a></td></tr>";	
		$result2 .= "</table>";	
		echo $result2;
	}

	private function generateURL($action, $varname, $varvalue, $filename){
		
		$path = '/resource/'. $action .'/' . $varname .'/' . $varvalue .'/bootmenuentryID/'.$this->thisSession->getBootmenuentryID().'/file/' . $filename  ;
		$path = "http://" . $_SERVER['SERVER_NAME'] . $path;
		return $path;
	}

}