summaryrefslogblamecommitdiffstats
path: root/library/Poolctrl/Pbs2Api.php
blob: 42911821fca2cab26db7052e94320eca060347ae (plain) (tree)























                                                                                                                                                                                                             

         









                                                                                                                                                                
                        




                                                                                                                                                      
                 
                                                     



                                                                
                                    


                 
                                                             




                                                                                                                   
 





















































































                                                                                                                           
                                                                            
                                                      





                                                                                                                              




                                                                                             
                                      


                 






                                                                                                                   






                                                                                          
                                     


                 






                                                                                                                       













                                                                                                  
                                                                            

                                                              

         
                                                                                                      
                                                                           

                                                                                                        


                                                                  
                                                                            


















                                                                                                          
                                                                      











                                                                                                                                                             
                                                                                                                    








                                                                                                                                                                                                              

                                                                                                                       


                                                     

                                                                                                                                    



                                                                                                                                                                                                                                                          
                                                                                                              








                                                                                                                                                                                                        

                                                                                                                  


                                                 

                                                                                                                              


                                                                                              
                                                                                                                                 





                                                                                                                                                                                             

                                                                                                                                     

         
<?php
/*
 * Copyright (c) 2011 - OpenSLX GmbH, RZ Uni Freiburg
 * This program is free software distributed under the GPL version 2.
 * See http://gpl.openslx.org/
 *
 * If you have any feedback please consult http://feedback.openslx.org/ and
 * send your suggestions, praise, or complaints to feedback@openslx.org
 *
 * General information about OpenSLX can be found at http://openslx.org/
 */

class Poolctrl_Pbs2Api {
	protected $config;
	protected $pbs2host;
	protected $userIDsNamespace;

	public function Poolctrl_Pbs2Api($config) {
		$this->config = $config;
		$this->pbs2host = $this->config['pbs2']['host'];
		$this->userIDsNamespace = Zend_Session::namespaceGet('userIDs');
		if(isset($this->userIDsNamespace['apikey'])) {
			$this->acl = new Poolctrl_Acl($this->pbs2host, $this->config['pbs2']['checkright'] . $this->userIDsNamespace['apikey'], 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']);
		}
	}

	private function runApi($host, $path, $data = null, $apikey = null) {
		if(!isset($apikey) && isset($this->userIDsNamespace['apikey'])) {
			$apikey = $this->userIDsNamespace['apikey'];
		}
		if(isset($apikey)) {
			if($data) {
				$apiResult = PostToHost($host, $path . $apikey, 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'], 'poolctrl', $data);
			} else {
				$apiResult = PostToHost($host, $path . $apikey, 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'], 'poolctrl', '');
			}
		} else {
			if($data) {
				$apiResult = PostToHost($host, $path, 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'], 'poolctrl', $data);
			} else {
				$apiResult = PostToHost($host, $path, 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'], 'poolctrl', '');
			}
		}
		$xmlString = $apiResult['http-body'];
		if(strlen($xmlString) > 0) {
			$xml = new SimpleXMLElement($xmlString);
			return $xml;
		} else {
			return null;
		}
	}

	public function getPool(&$poollist, $poolID = null) {
		if(isset($poolID)) {
			$poolRequest = "poolid=" . $poolID;
			$poolXML = $this->runApi($this->pbs2host, $this->config['pbs2']['getpools'], $poolRequest);
		} else {
			$poolXML = $this->runApi($this->pbs2host, $this->config['pbs2']['getpools']);

		}
		if(isset($poolXML)) {
			foreach($poolXML->poollist->pool as $pool) {
				$poolobj = new Application_Model_Pool();
				$poolobj->setID(sprintf("%s", $pool->id));
				$poolobj->setDescription(sprintf("%s", $pool->description));
				$poolobj->setGroupID(sprintf("%s", $pool->groupid));
				$poolobj->setLocation(sprintf("%s", $pool->location));
				$poolobj->setTitle(sprintf("%s", $pool->title));
				$poollist[$poolobj->getID()] = $poolobj;
			}
		}
	}

	public function getPoolClients(&$clientlist, $poolID) {
		$poolRequest = "poolid=" . $poolID;
		$poolXML = $this->runApi($this->pbs2host, $this->config['pbs2']['getpools'], $poolRequest);
		if(isset($poolXML)) {
			foreach($poolXML->poollist->pool as $pool) {
				$clientsXML = $pool->clients;
				foreach($clientsXML->client as $clientNew) {
					$client = new Application_Model_Client();
					$client->setID(sprintf("%s", $clientNew->clientID));
					$client->setCreated(sprintf("%s", $clientNew->created));
					$client->setGroupID(sprintf("%s", $clientNew->groupid));
					$client->setHardwarehash(sprintf("%s", $clientNew->location));
					$client->setIp(sprintf("%s", $clientNew->ip));
					$client->setIp6(sprintf("%s", $clientNew->ip6));
					$client->setMacadress(sprintf("%s", $clientNew->macadress));
					$clientArray = array(
								'id' => $client->getID(),
								'ip' => $client->getIp(),
								'mac' => $client->getMacadress());
					$clientlist[] = $clientArray;
				}
			}
		}
	}

	public function getBootos(&$bootoslist, &$configlist, $bootosID = null) {
		if(isset($bootosID)) {
			$bootOsRequest = "bootosid=" . $bootosID;
			$bootOsXML = $this->runApi($this->pbs2host, $this->config['pbs2']['getbootoss'], $bootOsRequest);
		} else {
			$bootOsXML = $this->runApi($this->pbs2host, $this->config['pbs2']['getbootoss']);
		}
		if(isset($bootOsXML)) {
			foreach($bootOsXML->bootoslist->bootos as $bootos) {
				$bootosobj = new Application_Model_BootOs();
				$bootosobj->setID(sprintf("%s", $bootos->id));
				$bootosobj->setCreated(sprintf("%s", $bootos->created));
				$bootosobj->setDefaultkcl(sprintf("%s", $bootos->defaultkcl));
				$bootosobj->setDescription(sprintf("%s", $bootos->description));
				$bootosobj->setDistro(sprintf("%s", $bootos->distro));
				$bootosobj->setDistroversion(sprintf("%s", $bootos->distroversion));
				$bootosobj->setExpires(sprintf("%s", $bootos->expires));
				$bootosobj->setGroupID(sprintf("%s", $bootos->groupid));
				$bootosobj->setMembershipID(sprintf("%s", $bootos->membershipid));
				$bootosobj->setPublic(sprintf("%s", $bootos->public));
				$bootosobj->setShare(sprintf("%s", $bootos->share));
				$bootosobj->setShortname(sprintf("%s", $bootos->shortname));
				$bootosobj->setSource(sprintf("%s", $bootos->source));
				$bootosobj->setTitle(sprintf("%s", $bootos->title));
				$bootoslist[$bootosobj->getID()] = $bootosobj;
				foreach($bootos->configs->config as $config) {
					$configobj = new Application_Model_Config();
					$configobj->setID(sprintf("%s", $config->configid));
					$configobj->setBootosID(sprintf("%s", $bootosobj->getID()));
					$configobj->setCreated(sprintf("%s", $config->created));
					$configobj->setMembershipID(sprintf("%s", $config->membershipid));
					$configobj->setDescription(sprintf("%s", $config->description));
					$configobj->setGroupID(sprintf("%s", $config->groupid));
					$configobj->setTitle(sprintf("%s", $config->title));
					$configobj->setVisible(sprintf("%s", $config->visible));
					if($configobj->getVisible()) {
						if($configobj->getTitle() == 'Default') {
							$configlist[$bootosobj->getID()][0] = $configobj;
						} else {
							$configlist[$bootosobj->getID()][$configobj->getID()] = $configobj;
						}
					}
				}
			}
		}
	}

	public function getGroup(&$groupSession, $groupID, $apikey = null) {
		$grouprequest = "groupid=" . $groupID;
		if(isset($apikey)) {
			$groupXML = $this->runApi($this->pbs2host, $this->config['pbs2']['getgroup'], $grouprequest, $apikey);
		} else {
			$groupXML = $this->runApi($this->pbs2host, $this->config['pbs2']['getgroup'], $grouprequest);
		}
		if(isset($groupXML)) {
			$group = new Application_Model_Group();
			$group->setID(sprintf("%s", $groupXML->group->id));
			$group->setTitle(sprintf("%s", $groupXML->group->title));
			$group->setDescription(sprintf("%s", $groupXML->group->description));
			$groupSession->$groupID = $group;
			return $group;
		}
	}

	public function getRole(&$roleSession, $roleID, $apikey = null) {
		if(isset($apikey)) {
			$roleXML = $this->runApi($this->pbs2host, $this->config['pbs2']['getrole'], null, $apikey);
		} else {
			$roleXML = $this->runApi($this->pbs2host, $this->config['pbs2']['getrole']);
		}
		if(isset($roleXML)) {
			$role = new Application_Model_Role();
			$role->setID(sprintf("%s", $roleXML->role->id));
			$role->setGroupID(sprintf("%s", $roleXML->role->groupid));
			$role->setTitle(sprintf("%s", $roleXML->role->title));
			$role->setDescription(sprintf("%s", $roleXML->role->description));
			$role->setInheritance(sprintf("%s", $roleXML->role->inheritance));
			$roleSession->$roleID = $role;
			return $role;
		}
	}

	public function getPerson(&$personSession, $apikey) {
		if(isset($apikey)) {
			$personXML = $this->runApi($this->pbs2host, $this->config['pbs2']['getperson'], null, $apikey);
		} else {
			$personXML = $this->runApi($this->pbs2host, $this->config['pbs2']['getperson']);
		}
		if(isset($personXML)) {
			$person = new Application_Model_Person();
			$person->setID(sprintf("%s", $personXML->person->id));
			$person->setCity(sprintf("%s", $personXML->person->city));
			$person->setEmail(sprintf("%s", $personXML->person->email));
			$person->setFirstname(sprintf("%s", $personXML->person->firstname));
			$person->setHousenumber(sprintf("%s", $personXML->person->housenumber));
			$person->setLogin(sprintf("%s", $personXML->person->login));
			$person->setLogindate(sprintf("%s", $personXML->person->logindate));
			$person->setName(sprintf("%s", $personXML->person->name));
			$person->setPostalcode(sprintf("%s", $personXML->person->postalcode));
			$person->setRegisterdate(sprintf("%s", $personXML->person->registerdate));
			$person->setStreet(sprintf("%s", $personXML->person->street));
			$person->setSuspend(sprintf("%s", $personXML->person->suspend));
			$person->setTitle(sprintf("%s", $personXML->person->title));
			$personName = sprintf("%s", $personXML->person->id);
			$personSession->$personName = $person;
		}
	}

	public function login($email, $password, &$membershipSession, &$error, &$personID, &$apikey) {
		$loginquery = "email=" . $email . "&password=" . $password;
		$loginXML = $this->runApi($this->pbs2host, $this->config['pbs2']['login'], $loginquery);
		if(isset($loginXML)) {
			$login = $loginXML->login;
			$success = sprintf("%s", $login->success);
			if ($success === "true") {
				$personID = sprintf("%s", $login->personid);
				$count = 0;
				foreach($login->membershiplist->membership as $membershipXML)
				{
					$membership = new Application_Model_Membership();
					$membership->setID(sprintf("%s", $membershipXML->id));
					$membership->setPersonID(sprintf("%s", $membershipXML->personid));
					$membership->setGroupID(sprintf("%s", $membershipXML->groupid));
					$membership->setRoleID(sprintf("%s", $membershipXML->roleid));
					$membership->setApikey(sprintf("%s", $membershipXML->apikey));
					$membership->setSuspend(sprintf("%s", $membershipXML->suspend));
					$membershipID = $membership->getID();
					$membershipSession->$membershipID = $membership;
					if($count == 0) {
						$apikey = sprintf("%s", $membershipXML->apikey);
					}
					$count++;
				}
				return true;
			} else {
				$error = sprintf("%s", $login->error);
				return false;
			}
		} else {
			return false;
		}
	}

	public function addBootmenu($eventTitle, $bootosID, $configID = null) {
		$bootmenuquery = "bootosID=" . $bootosID . "&startcounter=0&title=Poolctrl-Bootmenu for " . $eventTitle . "&defaultbootmenu=0&order=0&kcl=0";
		if(isset($configID)) {
			$bootmenuquery .= "&configID=" . $configID;
		}
		$bootmenuXML = $this->runApi($this->pbs2host, $this->config['pbs2']['addbootmenu'], $bootmenuquery);
		$bootmenuID = sprintf("%s", $bootmenuXML->bootmenu->id);
		return $bootmenuID;
	}

	public function changeBootmenu($bootmenuID, $eventTitle, $bootosID, $configID = null) {
		$bootmenuquery = "bootmenuid=" . $bootmenuID() . "&bootosID=" . $bootosID() . "&startcounter=0&title=Poolctrl-Bootmenu for " . $eventTitle . "&defaultbootmenu=0&order=0&kcl=0&changeentry=1";
		if(isset($configID)) {
			$bootmenuquery .= "&configID=" . $configID;
		}
		$bootmenuXML = $this->runApi($this->pbs2host, $this->config['pbs2']['changebootmenu'], $bootmenuquery);
		return $bootmenuXML;
	}

	public function deleteBootmenu($bootmenuID) {
		$bootmenuXML = $this->runApi($this->pbs2host, $this->config['pbs2']['deletebootmenu'], "bootmenuid=" . $bootmenuID);
		return $bootmenuXML;
	}

	public function addFilter($eventTitle, $bootmenuID) {
		$filterquery = "bootmenuID=" . $bootmenuID . "&title=Poolctrl-Filter for " . $event->getTitle() . "&description=This Filter was automatically created by the Poolctrl for Event " . $eventTitle . "&created= " . time() . "&priority=100";
		$filterXML = $this->runApi($this->pbs2host, $this->config['pbs2']['addfilter'], $filterquery);
		$filterID = sprintf("%s", $filterXML->filter->id);
		return $filterID;
	}

	public function changeFilter($filterID, $eventTitle, $bootmenuID = null) {
		$filterQuerie = "filterid=" . $filterID . "&title=Poolctrl-Filter for " . $eventTitle() . "&description=This Filter was automatically created by the Poolctrl for Event " . $eventTitle;
		if(isset($bootmenuID)) {
			"&bootmenuID=" . $bootmenuID;
		}
		$filterXML = $this->runApi($this->pbs2host, $this->config['pbs2']['changefilter'], $filterQuerie);
		return $filterXML;
	}

	public function deleteFilter($filterID) {
		$filterXML = $this->runApi($this->pbs2host, $this->config['pbs2']['deletefilter'], "bootmenuid=" . $filterID);
		return $filterXML;
	}

	public function addFilterentry($filterID, $filterType, $filterValue1, $filterValue2) {
		$filterentriesXML = $this->runApi($this->pbs2host, $this->config['pbs2']['addfilterentry'], $filterentriesquery);
		$filterentriesID = sprintf("%s", $filterentriesXML->filterentry->id);
		return $filterentriesID;
	}

	public function changeFilterentry($oldValue1, $oldValue2, $newValue1, $newValue2) {
		$filterentriesQuerie = "filterid=" . $event->getPbs_filterID() . "&oldvalue1=" . $oldValue1 . "&oldvalue2=" . $oldValue2 . "&value1=" . $newValue1 . "&value2=" . $newValue2;
		$filterentriesXML = $this->runApi($this->pbs2host, $this->config['pbs2']['changefilterentry'], $filterentriesQuerie);
		return $filterentriesXML;
	}
}