/** import_post() Actually imports the post. */ public function import_post() { try { // Error checking if ($_FILES["file"]["error"] > 0) { throw new Exception(sprintf('Error: ' . (is_array($_FILES["file"]["error"]) ? implode(', ', $_FILES["file"]["error"]) : $_FILES["file"]["error"]))); } if (!file_exists($_FILES["file"]["tmp_name"])) { throw new Exception('Could not find tmp filename'); } $numSuccess = $numFailed = $numAlreadyExist = 0; $handle = fopen($_FILES["file"]["tmp_name"], "r"); // Get all the service id's so they can be enabled. $ModuleIDs = $this->getClass('ModuleManager')->find('', '', '', '', '', '', '', 'id'); while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) { // Ignore header data if left in CSV if (preg_match('#ie#', $data[0])) { continue; } $totalRows++; if (count($data) < 7 && count($data) >= 2) { try { // Error checking $MACs = $this->parseMacList($data[0]); $Host = $this->getClass('HostManager')->getHostByMacAddresses($MACs); if ($Host && $Host->isValid()) { throw new Exception('A Host with any or one of these MACs already exists'); } if ($this->getClass('HostManager')->exists($data[1])) { throw new Exception('A host with this name already exists'); } $Host = new Host(array('name' => $data[1], 'description' => $data[3] . ' Uploaded by batch import on', 'ip' => $data[2], 'imageID' => $data[4], 'createdTime' => $this->nice_date()->format('Y-m-d H:i:s'), 'createdBy' => $this->FOGUser->get('name'))); $Host->addModule($ModuleIDs); $Host->addPriMAC($MACs[0]); array_shift($MACs); $Host->addAddMAC($MACs); if ($Host->save()) { $this->HookManager->processEvent('HOST_IMPORT', array('data' => &$data, 'Host' => &$Host)); $numSuccess++; } else { $numFailed++; } } catch (Exception $e) { $numFailed++; $uploadErrors .= sprintf('%s #%s: %s<br />', _('Row'), $totalRows, $e->getMessage()); } } else { $numFailed++; $uploadErrors .= sprintf('%s #%s: %s<br />', _('Row'), $totalRows, _('Invalid number of cells')); } } fclose($handle); } catch (Exception $e) { $error = $e->getMessage(); } // Title $this->title = _('Import Host Results'); unset($this->headerData); $this->attributes = array(array(), array()); $this->templates = array('${field}', '${input}'); $fields = array(_('Total Rows') => $totalRows, _('Successful Hosts') => $numSuccess, _('Failed Hosts') => $numFailed, _('Errors') => $uploadErrors); foreach ((array) $fields as $field => $input) { $this->data[] = array('field' => $field, 'input' => $input); } // Hook $this->HookManager->processEvent('HOST_IMPORT_FIELDS', array('headerData' => &$this->headerData, 'data' => &$this->data, 'templates' => &$this->templates, 'attributes' => &$this->attributes)); // Output $this->render(); }
} if ($Image->isValid() && $Host->getImageMemberFromHostID()) { if ($Host->createImagePackage(1, 'AutoRegTask')) { print _('Done, with imaging!'); } else { print _('Done, but unable to create task!'); } } else { print _('Done!'); } } else { $realhost = $macsimple; if (!$Host || !$Host->isValid()) { $Host = new Host(array('name' => $realhost, 'description' => sprintf('%s %s', _('Created by FOG Reg on'), date('F j, Y, g:i a')), 'createdTime' => $FOGCore->formatTime('now', 'Y-m-d H:i:s'), 'createdBy' => 'FOGREG')); $Host->addPriMAC($PriMAC); $Host->addAddMAC($MACs); $Host->addModule($ids); if (!$Host->save()) { throw new Exception(_('Failed to save new Host!')); } print _('Done'); } else { print _('Already registered as') . ': ' . $Host->get('name'); } } } else { print _('Already registered as') . ': ' . $Host->get('name'); } } } catch (Exception $e) { print $e->getMessage();