Example #1
0
 /**
  * Exports the yourCMDB data
  *
  */
 private function runExport()
 {
     //get ObjectController
     $objectController = ObjectController::create();
     //set up exportDestination
     $exportDestinationClass = __NAMESPACE__ . "\\" . $this->exportDestination->getClass();
     $exportDestination = new $exportDestinationClass();
     $exportDestination->setUp($this->exportDestination, $this->exportVariables);
     //walk through all ExportSources
     foreach ($this->exportSources as $exportSource) {
         //get objects to export
         $objects = array();
         $exportSourceFieldname = $exportSource->getFieldname();
         $exportSourceFieldvalue = $exportSource->getFieldvalue();
         $exportSourceObjectTypes = array($exportSource->getObjectType());
         $exportSourceStatusActive = $exportSource->getStatus();
         if ($exportSourceFieldname == null || $exportSourceFieldvalue == null) {
             $objects = $objectController->getObjectsByType($exportSourceObjectTypes[0], "", "ASC", $exportSourceStatusActive, 0, 0, "yourCMDB-exporter");
         } else {
             $objects = $objectController->getObjectsByField($exportSourceFieldname, $exportSourceFieldvalue, $exportSourceObjectTypes, $exportSourceStatusActive, 0, 0, "yourCMDB-exporter");
         }
         //export objects
         foreach ($objects as $object) {
             $exportDestination->addObject($object);
         }
     }
     //finish export
     $exportDestination->finishExport();
 }
 public function getResource()
 {
     $objectController = ObjectController::create();
     //try to get a list of objects
     try {
         $listtype = $this->uri[1];
         $searchvalue = $this->uri[2];
         switch ($listtype) {
             case "by-fieldvalue":
                 $objects = $objectController->getObjectsByFieldvalue(array($searchvalue), null, null, 0, 0, $this->user);
                 break;
             case "by-objecttype":
                 $objects = $objectController->getObjectsByType(array($searchvalue), null, "ASC", null, 0, 0, $this->user);
                 break;
             default:
                 return new RestResponse(400);
                 break;
         }
         //generate output
         $output = array();
         foreach ($objects as $object) {
             $output[] = $object->getId();
         }
     } catch (Exception $e) {
         return new RestResponse(404);
     }
     return new RestResponse(200, json_encode($output));
 }
Example #3
0
 /**
  * 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;
 }
Example #4
0
 /**
  * creates a new object controller
  * @return ObjectController	ObjectController instance
  */
 public static function create()
 {
     //check, if an ObjectController instance already exists
     if (ObjectController::$objectController == null) {
         ObjectController::$objectController = new ObjectController();
     }
     return ObjectController::$objectController;
 }
 public function postResource($data)
 {
     $objectController = ObjectController::create();
     $objectLinkController = ObjectLinkController::create();
     try {
         $decodedData = json_decode($data);
         $objectIdB = $decodedData->objectIdB;
         $objectIdA = $this->uri[1];
         $objectA = $objectController->getObject($objectIdA, $this->user);
         $objectB = $objectController->getObject($objectIdB, $this->user);
         $objectLinkController->addObjectLink($objectA, $objectB, $this->user);
     } catch (Exception $e) {
         return new RestResponse(400);
     }
     $url = "rest.php/objectlinks/{$objectIdA}";
     return new RestResponse(201, $url);
 }
 public function getResource()
 {
     $objectController = ObjectController::create();
     $objectLogController = ObjectLogController::create();
     //try to get object log
     try {
         $objectId = $this->uri[1];
         $object = $objectController->getObject($objectId, $this->user);
         $objectLogEntries = $objectLogController->getLogEntries($object, 0, 0, $this->user);
         //generate output
         $output = array();
         $output['objectId'] = $objectId;
         $output['logEntries'] = array();
         foreach ($objectLogEntries as $logEntry) {
             $output['logEntries'][] = array('date' => $logEntry->getTimestamp(), 'action' => $logEntry->getAction(), 'description' => $logEntry->getDescription(), 'user' => $logEntry->getUser());
         }
     } catch (Exception $e) {
         return new RestResponse(404);
     }
     return new RestResponse(200, json_encode($output));
 }
 /**
  * Interpreter:
  * tries to find a valid object ID in user input
  * @return boolean	true if it was successful
  *			false, if it was not successful
  */
 public function interpreteObjectId($inputArray)
 {
     //if user input is a single numeric word
     if (count($inputArray) == 1 && is_numeric($inputArray[0])) {
         //try to find object
         try {
             $objectController = ObjectController::create();
             $this->interpretedObject = $objectController->getObject($inputArray[0], "yourCMDB API");
             return true;
         } catch (CmdbObjectNotFoundException $e) {
             return false;
         }
     }
     return false;
 }
Example #8
0
 /**
  * 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;
 }
Example #9
0
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";
$i18nBaseDir = realpath("{$webScriptBaseDir}/../../i18n");
setlocale(LC_ALL, $i18nLocale);
Example #10
0
 public function putResource($data)
 {
     $objectController = ObjectController::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;
         $objectId = $decodedData->objectId;
         $objectStatus = $decodedData->status;
         $objectFields = array();
         foreach ($decodedData->objectFields as $group) {
             foreach ($group as $field) {
                 $fieldname = $field->name;
                 $fieldvalue = $field->value;
                 $objectFields[$fieldname] = $fieldvalue;
             }
         }
         //change object fields and status
         $objectController->updateObject($objectId, $objectStatus, $objectFields, $this->user);
         $url = "rest.php/objects/{$objectId}";
     } catch (Exception $e) {
         return new RestResponse(404);
     }
     return new RestResponse(201, $url);
 }
Example #11
0
 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;
 }