. * --------------------------------------------------------------------- */ /** * @since 9.1 */ class APIClient extends CommonDBTM { const DOLOG_DISABLED = 0; const DOLOG_LOGS = 1; const DOLOG_HISTORICAL = 2; static $rightname = 'config'; // From CommonDBTM public $dohistory = true; static $undisclosedFields = [ 'app_token' ]; static function canCreate() { return Session::haveRight(static::$rightname, UPDATE); } static function canPurge() { return Session::haveRight(static::$rightname, UPDATE); } static function getTypeName($nb = 0) { return _n("API client", "API clients", $nb); } function defineTabs($options = []) { $ong = []; $this->addDefaultFormTab($ong) ->addStandardTab('Log', $ong, $options); return $ong; } function rawSearchOptions() { $tab = []; $tab[] = [ 'id' => 'common', 'name' => self::GetTypeName() ]; $tab[] = [ 'id' => '1', 'table' => $this->getTable(), 'field' => 'name', 'name' => __('Name'), 'datatype' => 'itemlink', 'autocomplete' => true, ]; $tab[] = [ 'id' => '2', 'table' => $this->getTable(), 'field' => 'id', 'name' => __('ID'), 'massiveaction' => false, 'datatype' => 'number' ]; $tab[] = [ 'id' => '3', 'table' => $this->getTable(), 'field' => 'is_active', 'name' => __('Active'), 'datatype' => 'bool' ]; $tab[] = [ 'id' => '4', 'table' => $this->getTable(), 'field' => 'dolog_method', 'name' => __('Log connections'), 'datatype' => 'specific' ]; $tab[] = [ 'id' => 'filter', 'name' => __('Filter access') ]; $tab[] = [ 'id' => '5', 'table' => $this->getTable(), 'field' => 'ipv4_range_start', 'name' => __('IPv4 address range')." - ".__("Start"), 'datatype' => 'specific' ]; $tab[] = [ 'id' => '6', 'table' => $this->getTable(), 'field' => 'ipv4_range_end', 'name' => __('IPv4 address range')." - ".__("End"), 'datatype' => 'specific' ]; $tab[] = [ 'id' => '7', 'table' => $this->getTable(), 'field' => 'ipv6', 'name' => __('IPv6 address'), 'datatype' => 'text', 'autocomplete' => true, ]; $tab[] = [ 'id' => '8', 'table' => $this->getTable(), 'field' => 'app_token', 'name' => __('Application token'), 'massiveaction' => false, 'datatype' => 'text', 'autocomplete' => true, ]; return $tab; } static function getSpecificValueToDisplay($field, $values, array $options = []) { switch ($field) { case 'dolog_method' : $methods = self::getLogMethod(); return $methods[$values[$field]]; case 'ipv4_range_start': case 'ipv4_range_end': if (empty($values[$field])) { return ''; } return long2ip((int)$values[$field]); } return parent::getSpecificValueToDisplay($field, $values, $options); } /** * Show form * * @param integer $ID Item ID * @param array $options Options * * @return void */ function showForm ($ID, $options = []) { $this->initForm($ID, $options); $this->showFormHeader($options); echo ""; echo "".__('Name').""; echo ""; Html::autocompletionTextField($this, "name"); echo ""; echo "".__('Comments').""; echo ""; echo ""; echo ""; echo ""; echo "".__('Active').""; echo ""; Dropdown::showYesNo("is_active", $this->fields["is_active"]); echo ""; echo ""; echo "".__('Log connections').""; echo ""; Dropdown::showFromArray("dolog_method", self::getLogMethod(), ['value' => $this->fields["dolog_method"]]); echo ""; echo ""; echo ""; echo "
". __("Filter access")."
"; echo ""; echo ""; echo ""; echo "".__('Leave these parameters empty to disable API access restriction').""; echo "


"; echo ""; echo ""; echo "".__('IPv4 address range').""; echo ""; echo " - "; echo ""; echo ""; echo ""; echo "".__('IPv6 address').""; echo ""; Html::autocompletionTextField($this, "ipv6"); echo ""; echo ""; echo ""; echo ""; echo "".sprintf(__('%1$s (%2$s)'), __('Application token'), "app_token").""; echo ""; Html::autocompletionTextField($this, "app_token"); echo "
 "; echo ""; echo ""; $this->showFormButtons($options); } function prepareInputForAdd($input) { return $this->prepareInputForUpdate($input); } function prepareInputForUpdate($input) { if (isset($input['ipv4_range_start'])) { $input['ipv4_range_start'] = ip2long($input['ipv4_range_start']); } if (isset($input['ipv4_range_end'])) { $input['ipv4_range_end'] = ip2long($input['ipv4_range_end']); } if (isset($input['ipv4_range_start']) && isset($input['ipv4_range_end'])) { if (empty($input['ipv4_range_start'])) { $input['ipv4_range_start'] = "NULL"; $input['ipv4_range_end'] = "NULL"; } else { if (empty($input['ipv4_range_end'])) { $input['ipv4_range_end'] = $input['ipv4_range_start']; } if ($input['ipv4_range_end'] < $input['ipv4_range_start']) { $tmp = $input['ipv4_range_end']; $input['ipv4_range_end'] = $input['ipv4_range_start']; $input['ipv4_range_start'] = $tmp; } } } if (isset($input['ipv6']) && empty($input['ipv6'])) { $input['ipv6'] = "NULL"; } if (isset($input['_reset_app_token'])) { $input['app_token'] = self::getUniqueAppToken(); $input['app_token_date'] = $_SESSION['glpi_currenttime']; } return $input; } /** * Get log methods * * @return array */ static function getLogMethod() { return [self::DOLOG_DISABLED => __('Disabled'), self::DOLOG_HISTORICAL => __('Historical'), self::DOLOG_LOGS => _n('Log', 'Logs', Session::getPluralNumber())]; } /** * Get app token checking that it is unique * * @return string app token */ static function getUniqueAppToken() { $ok = false; do { $key = Toolbox::getRandomString(40); if (countElementsInTable(self::getTable(), ['app_token' => $key]) == 0) { return $key; } } while (!$ok); } }