width = $width; // Height. $this->height = $height; // Grid Width. $this->gridWidth = $gridWidth; // Cell Id. $this->cellId = $cellId; // Options. $this->values = $this->decoders($this->getOptionsWidget()); // Page. $this->page = basename(__FILE__); // ClassName. $class = new \ReflectionClass($this); $this->className = $class->getShortName(); // Title. $this->title = __('Agent hive'); // Name. if (empty($this->name) === true) { $this->name = 'AgentHive'; } // This forces at least a first configuration. $this->configurationRequired = false; if (empty($this->values['groups']) === true) { $this->configurationRequired = true; } } /** * Decoders hack for retrocompability. * * @param array $decoder Values. * * @return array Returns the values ​​with the correct key. */ public function decoders(array $decoder): array { $values = []; // Retrieve global - common inputs. $values = parent::decoders($decoder); if (isset($decoder['groups']) === true) { $values['groups'] = $decoder['groups']; } return $values; } /** * Generates inputs for form (specific). * * @return array Of inputs. * * @throws Exception On error. */ public function getFormInputs(): array { $values = $this->values; // Retrieve global - common inputs. $inputs = parent::getFormInputs(); // Filters. $inputs[] = [ 'label' => __('Groups'), 'id' => 'li_groups', 'arguments' => [ 'type' => 'select_groups', 'name' => 'groups[]', 'returnAllGroup' => false, 'privilege' => 'AR', 'selected' => (isset($values['groups'][0]) === true) ? explode(',', $values['groups'][0]) : [], 'return' => true, 'multiple' => true, 'required' => true, ], ]; return $inputs; } /** * Get Post for widget. * * @return array */ public function getPost(): array { // Retrieve global - common inputs. $values = parent::getPost(); $values['groups'] = \get_parameter('groups', 0); return $values; } /** * Draw widget. * * @return string; */ public function load() { global $config; $groups = $this->values['groups']; $groups = explode(',', $groups[0]); $user_groups = array_keys( users_get_groups( false, 'AR', false, false, $groups ) ); foreach ($groups as $key => $group) { if (in_array($group, $user_groups) === false) { unset($groups[$key]); } } $table = 'tagente'; if (is_metaconsole()) { $table = 'tmetaconsole_agent'; } $sql = sprintf( 'SELECT * FROM %s WHERE id_grupo IN('.implode(',', $groups).')', $table ); $all_agents = db_get_all_rows_sql($sql); $output = ''; $output .= '
'; foreach ($all_agents as $agent) { $output .= $this->drawSquare($agent); } $output .= '
'; $output .= ''; return $output; } /** * Draw square agent. * * @param array $data Info agent. * * @return string Output. */ private function drawSquare(array $data): string { global $config; $id = (is_metaconsole() === true) ? $data['id_tagente'] : $data['id_agente']; $status = agents_get_status_from_counts($data); switch ($status) { case 1: case 4: case 100: // Critical (BAD or ALERT). $color = '#e63c52'; break; case 0: case 300: // Normal (OK). $color = '#82b92e'; break; case 2: case 200: // Warning. $color = '#f3b200'; break; case 5: // Not init. $color = '#4a83f3'; break; default: // Default is Grey (Other). $color = '#b2b2b2'; break; } $style_contact = 'flex-grow: 9; font-size: 8pt; display: flex; justify-content: start;align-items: start; color: #9FA5B1; font-weight: 600;'; // Last contact. $lastContactDate = ui_print_timestamp( $data['ultimo_contacto'], true, ['style' => $style_contact] ); // Url. $console_url = ui_get_full_url('/'); if (is_metaconsole()) { $server = metaconsole_get_servers($data['id_tmetaconsole_setup']); $console_url = $server['server_url'].'/'; } $url_view = $console_url.'index.php?sec=estado&sec2=operation/agentes/ver_agente&id_agente='.$id; $url_manage = $console_url.'index.php?sec=gagente&sec2=godmode/agentes/configurar_agente&id_agente='.$id; $output = '
'; $output .= ''; $output .= '
'; $output .= '
'; // Last contact and img. $output .= ''; // OS and alias. $output .= '
'; $icon = (string) db_get_value( 'icon_name', 'tconfig_os', 'id_os', (int) $data['id_os'] ); $output .= '
'; $output .= file_get_contents( ui_get_full_url('images/'.$icon, false, false, false) ); $output .= '
'; $output .= ui_print_truncate_text( ucfirst(io_safe_output($data['alias'])), 12, false, true, true, '…', 'font-size: 11pt;color: #14524f;white-space: nowrap; font-weight: 600;text-align: left;width: 80%; overflow: hidden;', ); $output .= '
'; $style = 'font-size: 6pt; display: flex; justify-content: start; align-items: start; color: #9FA5B1; font-weight: 600; line-height:normal; text-align:left;'; $style_div = $style.' margin-bottom: 15px;'; // OS description. $output .= html_print_div( [ 'content' => (empty($data['os_version']) === true) ? ui_print_truncate_text( get_os_name((int) $data['id_os']), 32, false, true, true, '…', $style ) : ui_print_truncate_text( $data['os_version'], 32, false, true, true, '…', $style ), 'style' => $style_div, ], true ); // Description. $output .= html_print_div( [ 'content' => ui_print_truncate_text( io_safe_output($data['comentarios']), 38, false, true, true, '…', ), 'style' => 'text-align: left; min-height: 42px; font-size: 8pt; max-height: 42px; line-height: normal; margin: 2px 0px 2px 0px', ], true ); // IP. $output .= html_print_div( [ 'content' => $data['direccion'], 'style' => 'font-size: 10pt;color: #14524f; font-weight: 600; text-align: left; margin-top: 5px', ], true ); $output .= '
'; $output .= '
'; return $output; } /** * Get description. * * @return string. */ public static function getDescription() { return __('Agents hive'); } /** * Get Name. * * @return string. */ public static function getName() { return 'AgentHive'; } /** * Get size Modal Configuration. * * @return array */ public function getSizeModalConfiguration(): array { $size = [ 'width' => (is_metaconsole() === true) ? 700 : 600, 'height' => 610, ]; return $size; } }