/** add_post() Actually add's the host. */ public function add_post() { // Hook $this->HookManager->processEvent('HOST_ADD_POST'); // POST ? try { // Error checking if (empty($_REQUEST['host'])) { throw new Exception(_('Hostname is required')); } if (!$this->getClass('HostManager')->isHostnameSafe($_REQUEST['host'])) { throw new Exception(_('Please enter a valid hostname')); } if (empty($_REQUEST['mac'])) { throw new Exception(_('MAC Address is required')); } $MAC = new MACAddress($_REQUEST['mac']); if (!$MAC || !$MAC->isValid()) { throw new Exception(_('MAC Format is invalid')); } // Check if host exists with MAC Address. $Host = $this->getClass('HostManager')->getHostByMacAddresses($MAC); if ($Host && $Host->isValid()) { throw new Exception(_('A host with this MAC already exists with Hostname: ') . $Host->get('name')); } if ($this->getClass('HostManager')->exists($_REQUEST['host'])) { throw new Exception(_('Hostname already exists')); } // Get all the service id's so they can be enabled. $ModuleIDs = $this->getClass('ModuleManager')->find('', '', '', '', '', '', '', 'id'); $password = $_REQUEST['domainpassword']; if ($this->FOGCore->getSetting('FOG_NEW_CLIENT') && $_REQUEST['domainpassword']) { $password = $this->encryptpw($_REQUEST['domainpassword']); } // Define new Image object with data provided $Host = new Host(array('name' => $_REQUEST['host'], 'description' => $_REQUEST['description'], 'imageID' => $_REQUEST['image'], 'kernel' => $_REQUEST['kern'], 'kernelArgs' => $_REQUEST['args'], 'kernelDevice' => $_REQUEST['dev'], 'useAD' => $_REQUEST["domain"] == "on" ? '1' : '0', 'ADDomain' => $_REQUEST['domainname'], 'ADOU' => $_REQUEST['ou'], 'ADUser' => $_REQUEST['domainuser'], 'ADPass' => $password, 'productKey' => base64_encode($_REQUEST['key']))); $Host->addModule($ModuleIDs); $Host->addPriMAC($MAC); $useAD = isset($_REQUEST['domain']); $domain = trim($_REQUEST['domainname']); $ou = trim($_REQUEST['ou']); $user = trim($_REQUEST['domainuser']); $pass = trim($_REQUEST['domainpassword']); $Host->setAD($useAD, $domain, $ou, $user, $pass, true, true); // Save to database if ($Host->save()) { // Hook $this->HookManager->processEvent('HOST_ADD_SUCCESS', array('Host' => &$Host)); // Log History event $this->FOGCore->logHistory(sprintf('%s: ID: %s, Name: %s', _('Host added'), $Host->get('id'), $Host->get('name'))); // Set session message $this->FOGCore->setMessage(_('Host added')); // Redirect to new entry $this->FOGCore->redirect(sprintf('?node=%s&sub=edit&%s=%s', $this->REQUEST['node'], $this->id, $Host->get('id'))); } else { throw new Exception('Database update failed'); } } catch (Exception $e) { // Hook $this->HookManager->processEvent('HOST_ADD_FAIL', array('Host' => &$Host)); // Log History event $this->FOGCore->logHistory(sprintf('%s add failed: Name: %s, Error: %s', 'Host', $_REQUEST['name'], $e->getMessage())); // Set session message $this->FOGCore->setMessage($e->getMessage()); // Redirect to new entry $this->FOGCore->redirect($this->formAction); } }