/**
  * Method for import XML by webservice
  *
  * @param $params array ID of the agent
  * @param $protocol value the communication protocol used
  *
  * @return array or error value
  *
  **/
 static function loadInventory($params, $protocol)
 {
     if (isset($params['help'])) {
         return array('base64' => 'string, mandatory', 'help' => 'bool, optional');
     }
     if (!isset($_SESSION['glpiID'])) {
         return PluginWebservicesMethodCommon::Error($protocol, WEBSERVICES_ERROR_NOTAUTHENTICATED);
     }
     $content = base64_decode($params['base64']);
     $pfCommunication = new PluginFusioninventoryCommunication();
     $pfCommunication->handleOCSCommunication('', $content);
     $msg = __('Computer injected into GLPI', 'fusioninventory');
     return PluginWebservicesMethodCommon::Error($protocol, WEBSERVICES_ERROR_FAILED, '', $msg);
 }
Example #2
0
 static function methodGetAppliance($params, $protocol)
 {
     global $DB;
     if (isset($params['help'])) {
         return array('help' => 'bool,optional', 'id2name' => 'bool,optional', 'externalid OR id' => 'string');
     }
     if (!Session::getLoginUserID()) {
         return PluginWebservicesMethodCommon::Error($protocol, WEBSERVICES_ERROR_NOTAUTHENTICATED);
     }
     if (!isset($params['externalid']) && !isset($params['id'])) {
         return PluginWebservicesMethodCommon::Error($protocol, WEBSERVICES_ERROR_MISSINGPARAMETER);
     }
     $appli = new self();
     $found = false;
     if (isset($params['id'])) {
         $found = $appli->getFromDB(intval($params['id']));
     } else {
         if (isset($params['externalid'])) {
             $found = $appli->getFromDBbyExternalID(addslashes($params["externalid"]));
         }
     }
     if (!$found || !$appli->can($appli->fields["id"], 'r')) {
         return PluginWebservicesMethodCommon::Error($protocol, WEBSERVICES_ERROR_NOTFOUND);
     }
     $resp = $appli->fields;
     if (isset($params['id2name'])) {
         $resp['plugin_appliances_appliancetypes_name'] = Html::clean(Dropdown::getDropdownName('glpi_plugin_appliances_appliancetypes', $resp['plugin_appliances_appliancetypes_id']));
         $resp['plugin_appliances_environments_name'] = Html::clean(Dropdown::getDropdownName('glpi_plugin_appliances_environments', $resp['plugin_appliances_environments_id']));
         $resp['users_name'] = Html::clean(Dropdown::getDropdownName('glpi_users', $resp['users_id']));
         $resp['groups_name'] = Html::clean(Dropdown::getDropdownName('glpi_groups', $resp['groups_id']));
     }
     return $resp;
 }
 /**
  * Method to run shellcommands by webservice
  * 
  * @param array $params       Options
  * @param string $protocol    Communication protocol used
  * 
  * @return array or error value
  */
 static function methodRun($params, $protocol)
 {
     global $DB;
     if (isset($params['help'])) {
         return array('command_id' => 'integer,optional,multiple', 'command_name' => 'string,optional,multiple', 'target_type' => 'string,mandatory', 'target_id' => 'integer,optional,multiple', 'target_name' => 'string,optional', 'help' => 'bool,optional');
     }
     if (!Session::getLoginUserID()) {
         return PluginWebservicesMethodCommon::Error($protocol, WEBSERVICES_ERROR_NOTAUTHENTICATED);
     }
     $executionOutputs = array();
     $invalidCommands = array();
     $invalidTargets = array();
     /** Parameters check **/
     if ((!isset($params['command_id']) || empty($params['command_id'])) && (!isset($params['command_name']) || empty($params['command_name']))) {
         return PluginWebservicesMethodCommon::Error($protocol, WEBSERVICES_ERROR_MISSINGPARAMETER, '', 'one among "command_id" and "command_name"');
     }
     //Post-relation: Either Shellcommand ID or name is given
     if (!isset($params['target_type']) || empty($params['target_type']) || (!isset($params['target_id']) || empty($params['target_id'])) && (!isset($params['target_name']) || empty($params['target_name']))) {
         return PluginWebservicesMethodCommon::Error($protocol, WEBSERVICES_ERROR_MISSINGPARAMETER, '', 'must specify "target_type" and one among "target_id" and "target_name"');
     }
     //Post-relation: Either Target ID or name is given
     $possibleTargetTypes = PluginShellcommandsShellcommand_Item::getClasses(true);
     if (!in_array($params['target_type'], $possibleTargetTypes) || !class_exists($params['target_type'])) {
         return PluginWebservicesMethodCommon::Error($protocol, WEBSERVICES_ERROR_BADPARAMETER, '', '"target_type" is "' . $params['target_type'] . '" must be one of: ' . implode(', ', $possibleTargetTypes) . '');
     }
     //Post-relation: Given target type ($params['target_type']) is valid
     /** /Parameters check **/
     /** Command determination **/
     $commandIds = array();
     if (isset($params['command_id']) && !empty($params['command_id'])) {
         // ID(s) given
         $commandIds = (array) $params['command_id'];
     } else {
         // Name(s) given
         $command_names = (array) $params['command_name'];
         foreach ($command_names as $currentCommandName) {
             $command = new PluginShellcommandsShellcommand();
             if ($command->getFromDBbyName($currentCommandName)) {
                 $commandIds[] = $command->fields['id'];
             } else {
                 $invalidCommands[] = $currentCommandName;
             }
         }
     }
     //Post-relation: $commandIds is an array containing either provided or found (via given name) shellcommand IDs
     /** /Command determination **/
     /** Target determination **/
     $targetIds = array();
     if (isset($params['target_id']) && !empty($params['target_id'])) {
         // ID(s) given
         $targetIds = (array) $params['target_id'];
     } else {
         // Name(s) given
         $target_names = (array) $params['target_name'];
         foreach ($target_names as $currentTargetName) {
             $target = new $params['target_type']();
             if ($found = $target->find("`name` LIKE '" . $DB->escape($currentTargetName) . "'")) {
                 $targetIds = array_merge($targetIds, array_keys($found));
             } else {
                 $invalidTargets[] = $currentTargetName;
             }
         }
     }
     //Post-relation: $targetIds is an array containing either provided or found (via given name) targets IDs (of type $params['target_type'])
     /** /Target determination **/
     $commandIds = array_unique($commandIds);
     $targetIds = array_unique($targetIds);
     foreach ($targetIds as $currentTargetId) {
         $item = new $params['target_type']();
         $targetFound = $item->getFromDB($currentTargetId);
         if (!$targetFound) {
             $invalidTargets[] = $currentTargetId;
         }
         foreach ($commandIds as $currentCommandId) {
             $targetParams = PluginShellcommandsShellcommand_Item::resolveLinkOfCommand($currentCommandId, $item);
             if ($targetParams !== false) {
                 foreach ((array) $targetParams as $currentTargetParam) {
                     list($error, $executionOutput) = PluginShellcommandsShellcommand_Item::execCommand($currentCommandId, $currentTargetParam);
                     $executionOutputs[] = trim($executionOutput);
                 }
             } else {
                 $invalidCommands[] = $currentCommandId;
             }
         }
     }
     return array('callResults' => implode(PHP_EOL, $executionOutputs), 'invalidCommands' => $invalidCommands, 'invalidTargets' => $invalidTargets);
 }
Example #4
0
 /**
  * Once file is uploaded, process it
  *
  * @param $options   array of possible options:
  *   - file_encoding
  *   - mode
  *
  * @return boolean
  **/
 function processUploadedFile($options = array())
 {
     $file_encoding = isset($options['file_encoding']) ? $options['file_encoding'] : PluginDatainjectionBackend::ENCODING_AUTO;
     $mode = isset($options['mode']) ? $options['mode'] : self::PROCESS;
     //Get model & model specific fields
     $this->loadSpecificModel();
     $response = $this->readUploadedFile($options);
     if (!$this->injectionData) {
         if (!isset($options['webservice'])) {
             return false;
         }
         return PluginWebservicesMethodCommon::Error($options['protocol'], WEBSERVICES_ERROR_FAILED, sprintf(__('Not data to import', 'datainjection')));
     }
     if ($mode == self::PROCESS) {
         $this->loadMappings();
         $check = $this->isFileCorrect();
     } else {
         $check['status'] = PluginDatainjectionCommonInjectionLib::SUCCESS;
     }
     //There's an error
     if ($check['status'] != PluginDatainjectionCommonInjectionLib::SUCCESS) {
         if ($mode == self::PROCESS) {
             if (!isset($options['webservice'])) {
                 Session::addMessageAfterRedirect($check['error_message'], true, ERROR);
                 return false;
             }
             return PluginWebservicesMethodCommon::Error($options['protocol'], WEBSERVICES_ERROR_FAILED, $check['error_message']);
         }
     }
     $mappingCollection = new PluginDatainjectionMappingCollection();
     //Delete existing mappings only in model creation mode !!
     if ($mode == self::CREATION) {
         //If mapping still exists in DB, delete all of them !
         $mappingCollection->deleteMappingsFromDB($this->fields['id']);
     }
     $rank = 0;
     //Build the mappings list
     foreach (PluginDatainjectionBackend::getHeader($this->injectionData, $this->specific_model->isHeaderPresent()) as $data) {
         $mapping = new PluginDatainjectionMapping();
         $mapping->fields['models_id'] = $this->fields['id'];
         $mapping->fields['rank'] = $rank;
         $mapping->fields['name'] = $data;
         $mapping->fields['value'] = PluginDatainjectionInjectionType::NO_VALUE;
         $mapping->fields['itemtype'] = PluginDatainjectionInjectionType::NO_VALUE;
         $mappingCollection->addNewMapping($mapping);
         $rank++;
     }
     if ($mode == self::CREATION) {
         //Save the mapping list in DB
         $mappingCollection->saveAllMappings();
         self::changeStep($this->fields['id'], self::MAPPING_STEP);
         //Add redirect message
         Session::addMessageAfterRedirect(__('The file is ok.', 'datainjection'), true, INFO);
     }
     return true;
 }
 static function methodInject($params, $protocol)
 {
     if (isset($params['help'])) {
         return array('uri' => 'string,mandatory', 'base64' => 'string,optional', 'additional' => 'array,optional', 'models_id' => 'integer, mandatory', 'entities_id' => 'integer,mandatory', 'mandatory' => 'array,optional', 'uri' => 'uri,mandatory', 'help' => 'bool,optional');
     }
     $model = new PluginDatainjectionModel();
     //-----------------------------------------------------------------
     //-------------------------- Check parameters ---------------------
     //-----------------------------------------------------------------
     if (!isset($_SESSION['glpiID'])) {
         return PluginWebservicesMethodCommon::Error($protocol, WEBSERVICES_ERROR_NOTAUTHENTICATED);
     }
     if (!isset($params['uri']) && !isset($params['base64'])) {
         return PluginWebservicesMethodCommon::Error($protocol, WEBSERVICES_ERROR_MISSINGPARAMETER, '', 'uri or base64');
     }
     if (!isset($params['models_id'])) {
         return PluginWebservicesMethodCommon::Error($protocol, WEBSERVICES_ERROR_MISSINGPARAMETER, 'models_id');
     }
     if (!$model->getFromDB($params['models_id'])) {
         return PluginWebservicesMethodCommon::Error($protocol, WEBSERVICES_ERROR_NOTFOUND, __('Model unknown', 'datainjection'));
     }
     if (!$model->can($params['models_id'], 'r')) {
         return PluginWebservicesMethodCommon::Error($protocol, WEBSERVICES_ERROR_NOTALLOWED, __('You cannot access this model', 'datainjection'));
     }
     if ($model->fields['step'] < PluginDatainjectionModel::READY_TO_USE_STEP) {
         return PluginWebservicesMethodCommon::Error($protocol, WEBSERVICES_ERROR_NOTALLOWED, __('You cannot access this model', 'datainjection'));
     }
     //Check entity
     if (!isset($params['entities_id'])) {
         return PluginWebservicesMethodCommon::Error($protocol, WEBSERVICES_ERROR_MISSINGPARAMETER, 'entities_id');
     }
     $entities_id = $params['entities_id'];
     if ($entities_id > 0) {
         $entity = new Entity();
         if (!$entity->getFromDB($entities_id)) {
             return PluginWebservicesMethodCommon::Error($protocol, WEBSERVICES_ERROR_NOTFOUND, __('Entity unknown', 'datainjection'));
         }
         if (!Session::haveAccessToEntity($entities_id)) {
             return PluginWebservicesMethodCommon::Error($protocol, WEBSERVICES_ERROR_NOTALLOWED, __('You cannot access this entity', 'datainjection'));
         }
     }
     //Mandatory fields
     $additional_infos = array();
     if (isset($params['additional']) && is_array($params['additional'])) {
         $additional_infos = $params['additional'];
     }
     //Upload CSV file
     $document_name = basename($params['uri']);
     $filename = tempnam(PLUGIN_DATAINJECTION_UPLOAD_DIR, 'PWS');
     $response = PluginWebservicesMethodCommon::uploadDocument($params, $protocol, $filename, $document_name);
     if (PluginWebservicesMethodCommon::isError($protocol, $response)) {
         return $response;
     }
     //Uploade successful : now perform import !
     $options = array('file_encoding' => PluginDatainjectionBackend::ENCODING_AUTO, 'webservice' => true, 'original_filename' => $params['uri'], 'unique_filename' => $filename, 'mode' => PluginDatainjectionModel::PROCESS, 'delete_file' => false, 'protocol' => $protocol);
     //The Webservice protocol used
     $results = array();
     $response = $model->processUploadedFile($options);
     if (!PluginWebservicesMethodCommon::isError($protocol, $response)) {
         $engine = new PluginDatainjectionEngine($model, $additional_infos, $params['entities_id']);
         //Remove first line if header is present
         $first = true;
         foreach ($model->injectionData->getDatas() as $id => $data) {
             if ($first && $model->getSpecificModel()->isHeaderPresent()) {
                 $first = false;
             } else {
                 $results[] = $engine->injectLine($data[0], $id);
             }
         }
         $model->cleanData();
         return $results;
     }
     return $response;
 }