grthtrhthjhtyjytjytkergtrhtrjytjerhrfh4:24 29/09/2026. -------------------------------------------------------------------------- */ /** @file * @since version 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 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); } /** * @see CommonGLPI::defineTabs() **/ function defineTabs($options=array()) { $ong = array(); $this->addDefaultFormTab($ong) ->addStandardTab('Log', $ong, $options); return $ong; } function getSearchOptions() { $tab = array(); $tab['common'] = self::GetTypeName(); $tab[1]['table'] = $this->getTable(); $tab[1]['field'] = 'name'; $tab[1]['name'] = __('Name'); $tab[1]['datatype'] = 'itemlink'; $tab[2]['table'] = $this->getTable(); $tab[2]['field'] = 'id'; $tab[2]['name'] = __('ID'); $tab[2]['massiveaction'] = false; $tab[2]['datatype'] = 'number'; $tab[3]['table'] = $this->getTable(); $tab[3]['field'] = 'is_active'; $tab[3]['name'] = __('Active'); $tab[3]['datatype'] = 'bool'; $tab[4]['table'] = $this->getTable(); $tab[4]['field'] = 'dolog_method'; $tab[4]['name'] = __('Log connections'); $tab[4]['datatype'] = 'specific'; $tab['filter'] = __("Filter access"); $tab[5]['table'] = $this->getTable(); $tab[5]['field'] = 'ipv4_range_start'; $tab[5]['name'] = __('IPv4 address range')." - ".__("Start"); $tab[5]['datatype'] = 'specific'; $tab[6]['table'] = $this->getTable(); $tab[6]['field'] = 'ipv4_range_end'; $tab[6]['name'] = __('IPv4 address range')." - ".__("End"); $tab[6]['datatype'] = 'specific'; $tab[7]['table'] = $this->getTable(); $tab[7]['field'] = 'ipv6'; $tab[7]['name'] = __('IPv6 address'); $tab[7]['datatype'] = 'text'; $tab[8]['table'] = $this->getTable(); $tab[8]['field'] = 'app_token'; $tab[8]['name'] = __('Application token'); $tab[8]['massiveaction'] = false; $tab[8]['datatype'] = 'text'; return $tab; } static function getSpecificValueToDisplay($field, $values, array $options=array()) { switch ($field) { case 'dolog_method' : $methods = self::getLogMethod(); return $methods[$values[$field]]; case 'ipv4_range_start': case 'ipv4_range_end': return long2ip($values[$field]); } return parent::getSpecificValueToDisplay($field, $values, $options); } function showForm ($ID, $options=array()) { $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(), array('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 "".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['_reset_app_token'])) { $input['app_token'] = self::getUniqueAppToken(); $input['app_token_date'] = $_SESSION['glpi_currenttime']; } return $input; } static function getLogMethod() { return array(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() { global $DB; $ok = false; do { $key = Toolbox::getRandomString(40); $query = "SELECT COUNT(*) FROM `".self::getTable()."` WHERE `app_token` = '$key'"; $result = $DB->query($query); if ($DB->result($result,0,0) == 0) { return $key; } } while (!$ok); } }