public function postResource($data) { $objectController = ObjectController::create(); $config = CmdbConfig::create(); try { //decode json data $decodedData = json_decode($data); if (!isset($decodedData->objectType) || !isset($decodedData->objectFields) || !isset($decodedData->status)) { return new RestResponse(400); } $objectType = $decodedData->objectType; $objectStatus = $decodedData->status; $objectFields = array(); foreach ($decodedData->objectFields as $group) { foreach ($group as $field) { $fieldname = $field->name; $fieldvalue = $field->value; $objectFields[$fieldname] = $fieldvalue; } } //generate object $object = $objectController->addObject($objectType, $objectStatus, $objectFields, $this->user); $objectId = $object->getId(); $url = "rest.php/objects/{$objectId}"; } catch (Exception $e) { return new RestResponse(400); } return new RestResponse(201, $url); }
/** * Returns the value of variable for the given CmdbObject * @param CmdbObject $object the object to get the value */ public function getValue(\yourCMDB\entities\CmdbObject $object) { //get ObjectController $objectController = ObjectController::create(); //get object type config $config = CmdbConfig::create(); $configObjecttype = $config->getObjectTypeConfig(); $value = $this->defaultValue; //if there is a configuration for that object type //use the content of the specified field as value $objectType = $object->getType(); if (isset($this->fieldValue[$objectType]['name'])) { $fieldname = $this->fieldValue[$objectType]['name']; $value = $object->getFieldvalue($fieldname); //check if field is an object reference (type objectref) if (preg_match('/objectref-.*/', $configObjecttype->getFieldType($objectType, $fieldname)) == 1) { try { //get referenced object $refObject = $objectController->getObject($value, "yourCMDB-exporter"); //get value of referenced field if configured if ($this->fieldValue[$objectType]['refobjectfield'] != "") { $refFieldname = $this->fieldValue[$objectType]['refobjectfield']; $value = $refObject->getFieldvalue($refFieldname); } } catch (Exception $e) { } } } //return the value return $value; }
function __construct($taskname) { //create configuration object $config = CmdbConfig::create(); //get configuration for exporter task $this->task = $taskname; $this->exportSources = $config->getExporterConfig()->getSourcesForTask($taskname); $this->exportDestination = $config->getExporterConfig()->getDestinationForTask($taskname); $this->exportVariables = $config->getExporterConfig()->getVariablesForTask($taskname); //run export $this->runExport(); }
/** * private constructor * creates a new ORM Controller */ private function __construct() { //get configuration for connection $config = CmdbConfig::create(); $configDatastore = $config->getDatastoreConfig(); $connectionParameters = array('driver' => $configDatastore->getDriver(), 'host' => $configDatastore->getServer(), 'port' => $configDatastore->getPort(), 'dbname' => $configDatastore->getDatabaseName(), 'user' => $configDatastore->getUser(), 'password' => $configDatastore->getPassword(), 'charset' => 'utf8'); //configuration of Doctrine EntityManager $scriptBaseDir = dirname(__FILE__); $coreBaseDir = realpath("{$scriptBaseDir}/../../"); $paths = array("{$coreBaseDir}/" . self::ENTITY_PATH); $config = Setup::createAnnotationMetadataConfiguration($paths, self::DEVELOPMENT_MODE); $config->addEntityNamespace(self::ENTITY_NAMESPACE_ALIAS, self::ENTITY_NAMESPACE); //create entity manager $this->entityManager = EntityManager::create($connectionParameters, $config); }
public function getResource() { //check given resource $resource = $this->uri[1]; switch ($resource) { //resource is list case "list": $config = CmdbConfig::create(); $output = $config->getExporterConfig()->getTasks(); return new RestResponse(200, json_encode($output)); break; //other values: return 405 //other values: return 405 default: return new RestResponse(405); break; } }
/** * Interpreter: * Tries to find object types in search string and adds the given filter * @return boolean true if it was successful * false, if it was not successful */ private function interpreteObjectTypes($inputArray) { //get CMDB Config $config = CmdbConfig::create(); //find object types in input string $interpretedTypes = array(); $interpretedText = array(); $objectTypes = $config->getObjectTypeConfig()->getAllTypes(); //walk through every element of inputArray for ($i = 0; $i < count($inputArray); $i++) { $countMatches = count($interpretedTypes); //walk over all existing object types foreach ($objectTypes as $objectType) { //check, if there is a partial match with the object type //also check, if input element is min 40% of object type length if (stripos($objectType, $inputArray[$i]) !== FALSE && strlen($inputArray[$i]) / strlen($objectType) > 0.4) { $interpretedTypes[] = $objectType; } } //if no match, add input element to search text filter if (count($interpretedTypes) == $countMatches) { $interpretedText[] = $inputArray[$i]; } } //check if object detection was successful if (count($interpretedText) > 0) { //filter for text $filteredText = implode(" ", $interpretedText); $this->searchFilter->addFilter("text={$filteredText}"); //filter for objects foreach ($interpretedTypes as $interpretedType) { $this->searchFilter->addFilter("type={$interpretedType}"); } return true; } //return false, if detection was not successful return false; }
/** * returns all objects that matches the given filter * @return CmdbObject[] objects that matches the given filter */ public function getObjects($authUser) { //get CMDB config $config = CmdbConfig::create(); //create conditions from filter: text $conditionText = array(); if (isset($this->filter['text'][0])) { $conditionText = array_filter(explode(" ", $this->filter['text'][0])); } //create conditions from filter: object types $conditionTypes = array(); $conditionNotTypes = array(); $conditionAllTypes = $config->getObjectTypeConfig()->getAllTypes(); if (isset($this->filter['type'])) { $conditionTypes = $this->filter['type']; } if (isset($this->filter['notType']) && count($conditionTypes) > 0) { $conditionTypes = array_diff($conditionTypes, $this->filter['notType']); } if (isset($this->filter['notType']) && count($conditionTypes) == 0) { $conditionTypes = array_diff($conditionAllTypes, $this->filter['notType']); } if (count($conditionTypes) == 0) { $conditionTypes = null; } //create conditions from filter: status $conditionStatus = null; if (isset($this->filter['status'][0]) && $this->filter['status'][0] == 'A') { $conditionStatus = "A"; } //get objects only if a search text is given $objects = array(); if (count($conditionText) > 0) { $objectController = ObjectController::create(); $objects = $objectController->getObjectsByFieldvalue($conditionText, $conditionTypes, $conditionStatus, 0, 0, $authUser); } return $objects; }
public function getResource() { $config = CmdbConfig::create(); //try to get a list of objects try { $output = array(); $resource = $this->uri[1]; if (count($this->uri) > 2) { $param1 = $this->uri[2]; } else { $param1 = ""; } switch ($resource) { case "groups": $groups = $config->getObjectTypeConfig()->getObjectTypeGroups(); if ($param1 == "") { foreach (array_keys($groups) as $group) { $output[] = $group; } } else { if (!isset($groups[$param1])) { return new RestResponse(404); } foreach ($groups[$param1] as $group) { $output[] = $group; } } break; default: return new RestResponse(400); break; } } catch (Exception $e) { return new RestResponse(404); } return new RestResponse(200, json_encode($output)); }
function __construct() { //get configuration $config = CmdbConfig::create(); $this->configTaskScheduler = $config->getTaskSchedulerConfig(); }
use yourCMDB\controller\AccessGroupController; use yourCMDB\controller\ObjectController; use yourCMDB\controller\ObjectLinkController; use yourCMDB\controller\ObjectLogController; use yourCMDB\security\AuthorisationProviderLocal; use yourCMDB\taskscheduler\EventProcessor; use yourCMDB\info\InfoController; //define base directories $webScriptBaseDir = dirname(__FILE__); $coreBaseDir = realpath("{$webScriptBaseDir}/../../core"); //include yourCMDB bootstrap include "{$coreBaseDir}/bootstrap.php"; //include function definitions include "functions.inc.php"; //define variables $config = CmdbConfig::create(); $accessGroupController = AccessGroupController::create(); $objectController = ObjectController::create(); $objectLinkController = ObjectLinkController::create(); $objectLogController = ObjectLogController::create(); $authorisationProvider = new AuthorisationProviderLocal(); $eventProcessor = new EventProcessor(); $infoController = new InfoController(); //set default values of some variables $authUser = ""; //get configuration $installTitle = $config->getViewConfig()->getInstallTitle(); //setup i18n with gettext $i18nLocale = $config->getViewConfig()->getLocale(); $i18nDomain = "web"; $i18nCodeset = "utf-8";
public function import() { //check if required options are set $optionStart = $this->importOptions->getOptionValue("start", "0"); $optionLength = $this->importOptions->getOptionValue("length", "0"); $optionCols = $this->importOptions->getOptionValue("cols", "0"); $optionDelimiter = $this->importOptions->getOptionValue("delimiter", ";"); $optionEnclosure = $this->importOptions->getOptionValue("enclosure", ""); $optionType = $this->importOptions->getOptionValue("objectType", ""); if ($optionType == "") { throw new FileImportOptionsRequiredException(gettext("Missing option objectType for file import")); } //create object controller $objectController = ObjectController::create(); $config = CmdbConfig::create(); //get mapping of csv columns to object fiels $objectFieldConfig = $config->getObjectTypeConfig()->getFields($optionType); $objectFieldMapping = array(); $foreignKeyMapping = array(); $assetIdMapping = -1; for ($i = 0; $i < $optionCols; $i++) { $fieldname = $this->importOptions->getOptionValue("column{$i}", ""); //assetId mapping if ($fieldname == "yourCMDB_assetid") { $assetIdMapping = $i; } elseif (preg_match('#^yourCMDB_fk_(.*)/(.*)#', $fieldname, $matches) == 1) { $foreignKeyField = $matches[1]; $foreignKeyRefField = $matches[2]; $foreignKeyMapping[$foreignKeyField][$foreignKeyRefField] = $i; } elseif ($fieldname != "") { $objectFieldMapping[$fieldname] = $i; } } //open file $csvFile = fopen($this->importFilename, "r"); if ($csvFile == FALSE) { throw new FileImportException(gettext("Could not open file for import.")); } //create or update objects for each line in csv file $i = 0; while (($line = $this->readCsv($csvFile, 0, $optionDelimiter, $optionEnclosure)) !== FALSE) { // if ($i >= $optionLength + $optionStart && $optionLength != 0) { break; } //check start of import if ($i >= $optionStart) { //generate object fields $objectFields = array(); foreach (array_keys($objectFieldMapping) as $objectField) { if (isset($line[$objectFieldMapping[$objectField]])) { $objectFields[$objectField] = $line[$objectFieldMapping[$objectField]]; } } //resolve foreign keys foreach (array_keys($foreignKeyMapping) as $foreignKey) { foreach (array_keys($foreignKeyMapping[$foreignKey]) as $foreignKeyRefField) { //set foreign key object type $foreignKeyType = array(preg_replace("/^objectref-/", "", $objectFieldConfig[$foreignKey])); $foreignKeyLinePosition = $foreignKeyMapping[$foreignKey][$foreignKeyRefField]; if (isset($line[$foreignKeyLinePosition])) { $foreignKeyRefFieldValue = $line[$foreignKeyLinePosition]; //get object defined by foreign key $foreignKeyObjects = $objectController->getObjectsByField($foreignKeyRefField, $foreignKeyRefFieldValue, $foreignKeyType, null, 0, 0, $this->authUser); //if object was found, set ID as fieldvalue if (isset($foreignKeyObjects[0])) { $objectFields[$foreignKey] = $foreignKeyObjects[0]->getId(); } } } } //only create objects, if 1 or more fields are set if (count($objectFields) > 0) { //check if assetID is set in CSV file for updating objects if ($assetIdMapping != -1 && isset($line[$assetIdMapping])) { $assetId = $line[$assetIdMapping]; try { $objectController->updateObject($assetId, 'A', $objectFields, $this->authUser); } catch (Exception $e) { //if object was not found, add new one $objectController->addObject($optionType, 'A', $objectFields, $this->authUser); } } else { //generate object and save to datastore $objectController->addObject($optionType, 'A', $objectFields, $this->authUser); } } } //increment counter $i++; } //check, if CSV file could be deleted $deleteFile = false; if (feof($csvFile)) { $deleteFile = true; } //close file fclose($csvFile); //delete file from server if ($deleteFile) { unlink($this->importFilename); } //return imported objects return $i; }
/** * Returns all objects that have a reference to the given object * @param integer $objectId ID of the object * @param string $user name of the user that wants to execute the operation * @throws CmdbObjectNotFoundException * @return CmdbObject[] Array with CmdbObject objects that have a reference to the given object */ public function getObjectReferences($objectId, $user) { //get object and config object $object = $this->getObject($objectId, $user); $config = CmdbConfig::create(); $configObjectTypes = $config->getObjectTypeConfig(); //get reference fields $dataType = "objectref-" . $object->getType(); $referenceFields = $configObjectTypes->getFieldsByType($dataType); //if there are reference fields, get objects $objects = array(); if (count($referenceFields) > 0) { //create QueryBuilder $queryBuilder = $this->entityManager->createQueryBuilder(); //create query $queryBuilder->select("o"); $queryBuilder->from("yourCMDB:CmdbObjectField", "f"); $queryBuilder->from("yourCMDB:CmdbObject", "o"); $queryBuilder->andWhere("f.object = o.id"); $queryBuilder->andWhere("f.fieldvalue = ?1"); $queryBuilder->setParameter(1, $objectId); $j = 2; $k = 3; $querySubString = ""; for ($i = 0; $i < count($referenceFields); $i++) { $objectType = $referenceFields[$i][0]; $fieldkey = $referenceFields[$i][1]; $querySubString .= "(f.fieldkey = ?{$j} AND o.type = ?{$k})"; $queryBuilder->setParameter($j, $fieldkey); $queryBuilder->setParameter($k, $objectType); $j = $j + 2; $k = $k + 2; if ($i != count($referenceFields) - 1) { $querySubString .= " OR "; } } $queryBuilder->andWhere("({$querySubString})"); //get results $query = $queryBuilder->getQuery(); $objects = $query->getResult(); } //return return $objects; }