'; // header row if (isset($options['columns'])) { $html .= get_table_header($options['columns']); unset($options['columns']); } else { $html .= ''; foreach ($array[0] as $key => $value) { $html .= '' . escape_html(nicecase($key)) . ''; } $html .= ''; } // data rows foreach($array as $key => $value) { $html .= ''; foreach($value as $key2 => $value2) { // Entry defaults $escape = TRUE; $style = ''; $class = ''; if (isset($options[$key2])) { $options2 = $options[$key2]; if (is_array($options2)) { $type = $options2['type']; // Allow extra options if (isset($options2['escape'])) { $escape = $options2['escape']; } if (isset($options2['style'])) { $style = $options2['style']; } if (isset($options2['class'])) { $class = $options2['class']; } } else { $type = $options2; } } else { $type = NULL; } switch ($type) { case 'unixtime': $value2 = format_unixtime($value2); break; case 'device': $value2 = generate_device_link($value2); break; case 'json': $json = safe_json_encode(safe_json_decode($value2), JSON_PRETTY_PRINT); $value2 = '
'.$json.'
'; break; default: if ($escape) { $value2 = escape_html($value2); } } if ($style || $class) { $value2 = ''.$value2.''; } $html .= '' . $value2 . ''; } $html .= ''; } // finish table and return it $html .= ''; return $html; } function build_table_row($array, $options = []) { // Calculate max columns $cols = 0; foreach ($array as $entry) { $cols = max(safe_count($entry), $cols); } // start table $html = ''; // header row /* $html .= ''; foreach($array[0] as $key => $value) { $html .= ''; } $html .= ''; */ // data rows foreach($array as $key => $entry) { $html .= ''; $html .= ''; $values = array_values((array)$entry); for ($i = 0; $i <= $cols; $i++) { $value = $values[$i]; switch ($options[$key]) { case 'unixtime': $value = format_unixtime($value); break; case 'device': $value = generate_device_link($value); break; } $html .= ''; } $html .= ''; } // finish table and return it $html .= '
' . $key . '
' . $key . '' . $value . '
'; return $html; } /** * Print refresh meta header * * This function print refresh meta header and return status for current page * with refresh time, list and allowed refresh times. * Uses variables $vars['refresh'], $_SESSION['refresh'], $config['page_refresh'] * * @global string $GLOBALS['config']['page_refresh'] * @global string $_SESSION['refresh'] * @param array $vars * @return array $return */ function print_refresh($vars) { if (!$_SESSION['authenticated']) { // Do not print refresh header if not authenticated session, common use in logon page return array('allowed' => FALSE); } $refresh_array = $GLOBALS['config']['wui']['refresh_times']; // Allowed refresh times $refresh_time = 300; // Default page reload time (5 minutes) if (isset($vars['refresh'])) { if (is_numeric($vars['refresh']) && in_array($vars['refresh'], $refresh_array)) { $refresh_time = (int)$vars['refresh']; // Store for SESSION session_set_var('page_refresh', $refresh_time); } // Unset refresh var after unset($GLOBALS['vars']['refresh']); } else if (isset($_SESSION['page_refresh']) && in_array($_SESSION['page_refresh'], $refresh_array)) { $refresh_time = (int)$_SESSION['page_refresh']; } else if (is_numeric($GLOBALS['config']['page_refresh']) && in_array($GLOBALS['config']['page_refresh'], $refresh_array)) { $refresh_time = (int)$GLOBALS['config']['page_refresh']; } // List vars where page refresh full disabled - FIXME move to definitions! $refresh_disabled = $GLOBALS['config']['wui']['refresh_disabled']; $refresh_allowed = TRUE; foreach ($refresh_disabled as $var_test) { $var_count = safe_count($var_test); foreach ($var_test as $key => $value) { if (isset($vars[$key]) && $vars[$key] == $value) { $var_count--; } } if ($var_count === 0) { $refresh_allowed = FALSE; break; } } $return = array('allowed' => $refresh_allowed, 'current' => $refresh_time, 'list' => $refresh_array); if ($refresh_allowed && $refresh_time) { register_html_resource('meta', array('http-equiv' => 'refresh', 'content' => $refresh_time)); //echo(' ' . "\n"); $return['nexttime'] = time() + $refresh_time; // Add unixtime for next refresh } return $return; } /** * This generates an HTML element based on the contents of the $header array, modified by the current request $vars * * @param array $header Array with table header definition including columns and classes. * @param array $vars Array with current selected column ID and/or variables for generate column link * @return string $string */ function generate_table_header($header = [], $vars = []) { // Store current $vars sort variables $sort = $vars['sort']; $sort_order = strtolower($vars['sort_order']); if (!in_array($sort_order, array('asc', 'desc', 'reset'))) { $sort_order = 'asc'; } // Reset current $vars sort variables unset($vars['sort'], $vars['sort_order']); $output = ''; $output .= ' ' . PHP_EOL; //r($header); // Loop each column generating a element foreach($header AS $id => $col) { //if (in_array($id, ['class', 'group', 'style'])) { continue; } // skip html metadata if ($id === 'class' || $id === 'style') { continue; } // skip html data $fields = []; // Empty array for fields if(empty($col) || !is_array($col)) { $col = [ $id => $col ]; } // If col is not an array, make it one if($id == 'state-marker') { $col['class'] = 'state-marker'; } // Hard code handling of state-marker // Loop each field and generate an element foreach ($col as $field_id => $field) { if ($field_id === 'class' || $field_id === 'style' || $field_id == 'subfields') { continue; } // skip html data $header_field = generate_table_header_field($field_id, $field, $vars, $sort, $sort_order); if(strlen($header_field) > 0) { $fields[] = $header_field; } } $output .= ' '; $output .= implode(' / ', $fields); $output .= '' . PHP_EOL; } $output .= ' ' . PHP_EOL; $output .= ' ' . PHP_EOL; return $output; } function generate_table_header_field($field_id, $field, $vars, $sort, $sort_order) { if(empty($field)) { // No label, generate empty column header. $return = ''; } elseif ( is_numeric($field_id) && !is_array($field)) { // Label without id, generate simple column header $return = $field; } else { if(!is_array($field)) { $field = [ 'label' => $field ]; } if(!isset($field['label'])) { $field['label'] = $field[0]; } if($sort == $field_id) { $field['label'] = ''.$field['label'].''; if($sort_order == 'asc') { $new_vars = [ 'sort' => $field_id, 'sort_order' => 'desc' ]; $field['caret'] = ' '; } else { $new_vars = [ 'sort' => NULL, 'sort_order' => NULL ]; $field['caret'] = ' '; } } else { $new_vars = [ 'sort' => $field_id ]; } $return = ''.$field['label'].$field['caret'].''; // Generate slash separated links for subfields if(isset($field['subfields'])) { foreach($field['subfields'] as $subfield_id => $subfield) { //r($subfield); r($subfield_id); $subfields[] = generate_table_header_field($subfield_id, $subfield, $vars, $sort, $sort_order); } $return .= ' ['.implode(" / ", $subfields).']'; } } return $return; } /** * Helper function for generate table header with sort links * This used in other print_* functions * * @param array $cols Array with column IDs, names and column styles * @param array $vars Array with current selected column ID and/or variables for generate column link * @return string $string */ function get_table_header($cols, $vars = array()) { // Always clean sort vars $sort = $vars['sort']; $sort_order = strtolower($vars['sort_order']); if (!in_array($sort_order, array('asc', 'desc', 'reset'))) { $sort_order = 'asc'; } unset($vars['sort'], $vars['sort_order']); if (isset($vars['show_header']) && !$vars['show_header']) { // Do not show any table header if show_header == FALSE $string = ' ' . PHP_EOL; } else { $string = ' ' . PHP_EOL; } $string .= ' ' . PHP_EOL; foreach ($cols as $id => $col) { if (is_array($col)) { $name = $col[0]; $style = ' '.$col[1]; // Column styles/classes } else { $name = $col; $style = ''; } $string .= ' '; if ($name == NULL) { $string .= ''; // Column without Name and without Sort } elseif (is_intnum($id) || str_contains($id, "!")) { $string .= $name; // Column without Sort } elseif (!empty($vars) || $sort) { // Sort order cycle: asc -> desc -> reset if ($sort == $id) { switch ($sort_order) { case 'desc': $name .= '  '; $sort_array = array(); //$vars['sort_order'] = 'reset'; break; case 'reset': //unset($vars['sort'], $vars['sort_order']); $sort_array = array(); break; default: // ASC $name .= '  '; $sort_array = array('sort' => $id, 'sort_order' => 'desc'); //$vars['sort_order'] = 'desc'; } } else{ $sort_array = array('sort' => $id); } $string .= ''.$name.''; // Column now sorted (selected) } else { $string .= $name; // Sorting is not available (if vars empty or FALSE) } $string .= '' . PHP_EOL; } $string .= ' ' . PHP_EOL; $string .= ' ' . PHP_EOL; return $string; } function print_error_permission($text = NULL, $escape = TRUE) { if (empty($text)) { $text = 'You have insufficient permissions to view this page.'; } else if ($escape) { $text = escape_html($text); } echo('
'); print_error('

Permission error

' . PHP_EOL . $text); echo('
'); } /** * Generate html with label group * * @param array $params List of button items * @param array $opt Array with group params and styles * @param bool $escape Escape or not Item text * * @return string Generated html */ function get_label_group($params = [], $opt = [], $escape = TRUE) { $html = ' $param) { // If param is just string, convert to simple group if (is_string($param)) { $param = ['text' => $param, 'event' => 'default']; } //$html_param = ' $param) { // If param is just string, convert to simple group if (is_string($param)) { $param = ['text' => $param, 'event' => 'default']; } $html_param = '
setMarkupEscaped($escape) # escapes markup (HTML) ->setBreaksEnabled(TRUE) # enables automatic line breaks ->text($markdown); return '
'.PHP_EOL.$html.PHP_EOL.'
'; } // Single line (used for messages, eventlogs) $html = $parsedown ->setMarkupEscaped($escape) # escapes markup (HTML) ->setBreaksEnabled(TRUE) # enables automatic line breaks ->line($markdown); //print_vars($html); return '' . $html . ''; } /** * Generate icon html tag * * @param string $icon Icon name in definitions (ie: flag) or by css class (ie: sprite-flag) * @param string $class Additional class(es) for changing main icon view * @param array $attribs Url/link extended attributes (ie data-*, class, style) * * @return string HTML icon tag like or emoji style :flag-us: -> 🇺🇸 */ function get_icon($icon, $class = '', $attribs = []) { global $config; // Passed already html icon tag, return as is if (str_contains_array($icon, [ '<', '>' ])) { return $icon; } $icon = trim(strtolower($icon)); if (isset($config['icon'][$icon])) { // Defined icons $icon = $config['icon'][$icon]; } elseif (!strlen($icon)) { // Empty icon, return empty string return ''; } // Emoji styled icons, ie :flag-us: if (preg_match('/^:[\w\-_]+:$/', $icon)) { // icon-emoji is pseudo class, for styling emoji as other icons return '' . get_icon_emoji($icon) . ''; } // Append glyphicon main class if these icons used if (str_starts($icon, 'glyphicon-')) { $icon = 'glyphicon '.$icon; } if ($class) { // Additional classes $attribs['class'] = array_merge((array)$class, (array)$attribs['class']); } $attribs['class'] = array_merge([ $icon ], (array)$attribs['class']); return ''; } /** * Generate emoji icon (ie html hex entity) * * @param string $emoji Emoji name in definitions (ie: flag-us or :flag-us:) * @param string $type Type of emoji for return (currently only html supported) * @param array $attribs Url/link extended attributes (currently unused) * * @return string Emoji in requested type, for html ie: :flag-us: -> 🇺🇸 */ function get_icon_emoji($emoji, $type = 'html', $attribs = []) { global $config; // Emoji definitions not loaded by default! // Load of first request if (!isset($config['emoji']['zero'])) { include_once $config['install_dir'] . '/includes/definitions/emoji.inc.php'; } $emoji_name = strtolower(trim($emoji, ": \t\n\r\0\x0B")); // Unknown emoji name, return original string if (!isset($config['emoji'][$emoji_name])) { return $emoji; } $type = strtolower($type); $entry = $config['emoji'][$emoji_name]; switch ($type) { case 'unified': case 'non_qualified': $return = escape_html($entry[$type]); break; case 'html': default: // 26A0-FE0F -> ⚠️ $emoji_html = explode('-', escape_html($entry['unified'])); // escaping for prevent pass to definitions html code $return = '&#x' . implode(';&#x', $emoji_html) . ';'; } return $return; } /** * Generate icon html tag with country flag * * @param string $country Country name or code * * @return string HTML icon tag like */ function get_icon_country($country) { global $config; // Unificate country name $country = country_from_code($country); // Find ISO 2 country code (must be first in definitions) $code = strtolower(array_search($country, $config['rewrites']['countries'])); if (empty($code)) { return get_icon('location'); } return ''; } function print_json_status($status, $message = '', $array = []) { if (!in_array($status, [ 'ok', 'failed', 'warning' ])) { $status = 'failed'; } $return = [ 'status' => $status, 'message' => $message ]; if (safe_count($array)) { if (isset($array['message']) && empty($message)) { // prefer not empty message unset($return['message']); } $return = array_merge($return, $array); } header('Content-Type: application/json'); print safe_json_encode($return); } // EOF