breadcrum = $str; } /** * Getter for breadcrum * * @return array Breadcrum. */ public function getBreadcrum() { return $this->breadcrum; } /** * Add an element to breadcrum array. * * @param array $breads Elements to add to breadcrum. * * @return void */ protected function addBreadcrum($breads) { if (empty($breads)) { return; } $this->breadcrum = array_merge($this->breadcrum, $breads); } /** * Setter for label * * @param string $str Label. * * @return void */ public function setLabel(string $str) { $this->label = $str; } /** * Getter for label * * @return array Breadcrum. */ public function getLabel() { return $this->label; } /** * Return units associated to target interval (in seconds). * * @param integer $interval Target interval. * * @return integer Unit. */ public function getTimeUnit($interval) { $units = [ 1, 60, 3600, 86400, 604800, 2592000, 31104000, ]; $size = count($units); for ($i = 0; $i < $size; $i++) { if ($interval < $units[$i]) { if (($i - 1) < 0) { return 1; } return $units[($i - 1)]; } } return $units[-1]; } /** * Builder for breadcrum * * @param array $urls Array of urls to be stored in breadcrum. * @param boolean $add True if breadcrum should be added * instead of overwrite it. * * @return void */ public function prepareBreadcrum( array $urls, bool $add=false ) { $bc = []; $i = 0; foreach ($urls as $url) { if ($url['selected'] == 1) { $class = 'selected'; } else { $class = ''; } $bc[$i] = ''; $bc[$i] .= ''; $bc[$i] .= $url['label']; $bc[$i] .= ''; $bc[$i] .= ''; $i++; } if ($add === true) { $this->addBreadcrum($bc); } else { $this->setBreadcrum($bc); } } /** * To be overwritten. * * @return void */ public function run() { ui_require_css_file('wizard'); // Check access. check_login(); if (! $this->aclMulticheck()) { return; } } /** * Check multiple acl perms. * * @param string $access Access in PM|AR|RR format. Optional. * * @return boolean Alowed or not. */ public function aclMulticheck($access=null, $id_group=0) { global $config; if (isset($access)) { $perms = explode('|', $access); } else { $perms = explode('|', $this->access); } $allowed = false; foreach ($perms as $perm) { $allowed = $allowed || (bool) check_acl( $config['id_user'], $id_group, $perm ); } return $allowed; } /** * Checks if environment is ready, * returns array * icon: icon to be displayed * label: label to be displayed * * @return array With data. **/ public function load() { global $config; // Check access. check_login(); if (! $this->aclMulticheck()) { return false; } return [ 'icon' => $this->icon, 'label' => $this->label, 'url' => $this->url, ]; } /** * Print breadcrum to follow flow. * * @return string Breadcrum HTML code. */ public function printBreadcrum() { return implode( ' / ', $this->breadcrum ); } /** * Prints a header for current wizard. * * @param boolean $return Return HTML or print it. * * @return string HTML code for header. */ public function printHeader(bool $return=false) { $output = $this->printBreadcrum(); if ($return === false) { echo $output; } return $output; } /** * Return formatted html for error handler. * * @param string $message Error mesage. * * @return string */ public static function error($message) { if (is_ajax()) { echo json_encode( [ 'error' => ui_print_error_message($message, '', true), ] ); } else { return ui_print_error_message($message, '', true); } } /** * Print input using functions html lib. * * @param array $data Input definition. * * @return string HTML code for desired input. */ public static function printInput($data) { global $config; include_once $config['homedir'].'/include/functions_html.php'; if (is_array($data) === false) { return ''; } $input_only = true; if (isset($data['input_only']) === true) { $input_only = $data['input_only']; } $input = html_print_input( ($data + ['return' => true]), 'div', $input_only ); if ($input === false) { return ''; } return $input; } /** * Prints a go back button redirecting to main page. * * @param string $url Optional target url. * * @return void */ public static function printGoBackButton($url=null) { if (isset($url) === false) { $url = ui_get_full_url( 'index.php?sec=gservers&sec2=godmode/servers/discovery' ); } $form = [ 'form' => [ 'method' => 'POST', 'action' => $url, ], 'inputs' => [ [ 'class' => 'w100p', 'arguments' => [ 'name' => 'submit', 'label' => __('Go back'), 'type' => 'submit', 'attributes' => 'class="sub cancel"', 'return' => true, ], ], ], ]; self::printForm($form); } /** * Print a block of inputs. * Example, using direct to 'anidate' inputs directly to wrapper: * [ * 'wrapper' => 'div', * 'block_id' => 'example_id', * 'class' => 'your class', * 'direct' => 1, * 'block_content' => [ * [ * 'arguments' => [ * 'label' => __('Sugesstion'), * 'type' => 'button', * 'attributes' => 'class="sub ok btn_sug"', * 'name' => 'option_1', * 'id' => 'option_1', * 'script' => 'change_option1()', * ], * ], * [ * 'arguments' => [ * 'label' => __('Something is not quite right'), * 'type' => 'button', * 'attributes' => 'class="sub ok btn_something"', * 'name' => 'option_2', * 'id' => 'option_2', * 'script' => 'change_option2()', * ], * ], * ], * ]. * * @param array $input Definition of target block to be printed. * @param boolean $return Return as string or direct output. * @param boolean $direct Avoid encapsulation if input print is direct. * * @return string HTML content. */ public static function printBlock( array $input, bool $return=false, bool $direct=false ) { global $config; if ($config['style'] === 'pandora_black') { $text_color = 'style="color: white"'; } $output = ''; if ($input['hidden'] == 1) { $class = ' hidden'; } else { $class = ''; } if (isset($input['class']) === true) { $class = $input['class'].$class; } if (is_array($input['block_content']) === true) { $direct = (bool) $input['direct']; $toggle = (bool) $input['toggle']; if (isset($input['label']) === true) { $output .= ''.$input['label'].''; } // Print independent block of inputs. $output .= '
  • '; if ($input['wrapper']) { $output .= '<'.$input['wrapper'].' id="'.$input['block_id'].'" class="'.$class.'">'; } if (!$direct) { // Avoid encapsulation if input is direct => 1. $output .= ''; } if ($input['wrapper']) { $output .= ''; } $output .= '
  • '; } else { if ($input['arguments']['type'] != 'hidden' && $input['arguments']['type'] != 'hidden_extended' && $input['arguments']['type'] != 'datalist' ) { // Raw content for attach at the start of the input. if (isset($input['surround_start']) === true) { $output .= $input['surround_start']; } if (!$direct) { $output .= '
  • '; } if (isset($input['label']) === true) { $output .= ''; } $output .= self::printInput($input['arguments']); // Allow dynamic content. $output .= $input['extra']; if (!$direct) { $output .= '
  • '; } // Raw content for attach at the end of the input. if (isset($input['surround_end']) === true) { $output .= $input['surround_end']; } } else { $output .= self::printInput($input['arguments']); // Allow dynamic content. $output .= $input['extra']; } } if ($return === false) { echo $output; } return $output; } /** * Print a block of inputs with grid format. * * @param array $input Definition of target block to be printed. * @param boolean $return Return as string or direct output. * * @return string HTML content. */ public static function printBlockAsGrid(array $input, bool $return=false) { $output = ''; if ($input['hidden'] == 1) { $class = ' hidden'; } else { $class = ''; } if (isset($input['class']) === true) { $class = $input['class'].$class; } if (is_array($input['block_content']) === true) { if (empty($input['label']) === false) { $output .= '
    '; $output .= $input['label']; $output .= '
    '; } // Print independent block of inputs. $output .= ''; } else { if ($input['arguments']['type'] != 'hidden' && $input['arguments']['type'] != 'hidden_extended' ) { if ($input['arguments']['inline'] != 'true') { $output .= '
    '; } else { $output .= '
    '; if (!isset($input['extra'])) { $output .= '
    '; } if (isset($input['extra'])) { $output .= '
    '; } } if ($input['arguments']['inline'] == 'true' && isset($input['extra'])) { $output .= '
    '; } $output .= '
    '; $output .= $input['label']; $output .= '
    '; if ($input['arguments']['inline'] == 'true' && isset($input['extra'])) { $output .= '
    '; } if ($input['arguments']['inline'] == 'true' && !isset($input['extra'])) { $output .= '
    '; } if ($input['arguments']['type'] == 'text' || $input['arguments']['type'] == 'text_extended') { $output .= '
    '; $output .= self::printInput($input['arguments']); $output .= '
    '; } else if ($input['arguments']['inline'] == 'true') { if (isset($input['extra'])) { $output .= '
    '; $output .= '
    '; } else { $output .= '
    '; $output .= '
    '; } $output .= self::printInput($input['arguments']); $output .= '
    '; $output .= '
    '; if (isset($input['extra'])) { $output .= '
    '; } } else { $output .= self::printInput($input['arguments']); } // Allow dynamic content. $output .= $input['extra']; $output .= '
    '; } else { $output .= self::printInput($input['arguments']); // Allow dynamic content. $output .= $input['extra']; } } if ($return === false) { echo $output; } return $output; } /** * Print a block of inputs as a list element. * * @param array $input Definition of target block to be printed. * @param boolean $return Return as string or direct output. * * @return string HTML content. */ public static function printBlockAsList(array $input, bool $return=false) { $output = ''; if ($input['hidden'] == 1) { $class = ' hidden'; } else { $class = ''; } if (isset($input['class']) === true) { $class = $input['class'].$class; } if (is_array($input['block_content']) === true) { // Print independent block of inputs. $output .= '
  • '; $output .= '
      '; foreach ($input['block_content'] as $input) { $output .= self::printBlockAsList($input, $return); } $output .= '
  • '; } else { if ($input['arguments']['type'] != 'hidden' && $input['arguments']['type'] != 'hidden_extended' ) { $output .= '
  • '; $output .= ''; $output .= self::printInput($input['arguments']); // Allow dynamic content. $output .= $input['extra']; $output .= '
  • '; } else { $output .= self::printInput($input['arguments']); // Allow dynamic content. $output .= $input['extra']; } } if ($return === false) { echo $output; } return $output; } /** * Print a form. * * @param array $data Definition of target form to be printed. * @param boolean $return Return as string or direct output. * @param boolean $print_white_box Print a white box. * * @return string HTML code. */ public static function printForm( array $data, bool $return=false, bool $print_white_box=false ) { $form = $data['form']; $inputs = $data['inputs']; $rawInputs = $data['rawInputs']; $js = $data['js']; $rawjs = $data['js_block']; $cb_function = $data['cb_function']; $cb_args = $data['cb_args']; $output_head = ''; if (empty($data['pre-content']) === false) { $output_head .= $data['pre-content']; } if (isset($data['form']) === true) { $output_head .= '
    getMessage()); } $output_submit = ''; $output = ''; if ($print_white_box === true) { $output .= '
    '; } $output .= '
      '; foreach ($inputs as $input) { if ($input['arguments']['type'] != 'submit') { $output .= self::printBlock($input, true); } else { $output_submit .= self::printBlock($input, true); } } $output .= '
    '; // There is possible add raw inputs for this form. if (empty($rawInputs) === false) { $output .= $rawInputs; } if ($print_white_box === true) { $output .= '
    '; } $output .= '
      '.$output_submit.'
    '; $output .= html_print_csrf_hidden(true); $output .= '
    '; $output .= ''; if ($rawjs) { $output .= $rawjs; } if ($return === false) { echo $output; } return $output_head.$output; } /** * Print a form as a grid of inputs. * * @param array $data Definition of target form to be printed. * @param boolean $return Return as string or direct output. * * @return string HTML code. */ public static function printFormAsGrid(array $data, bool $return=false) { $form = $data['form']; $rows = $data['rows']; $rawInputs = $data['rawInputs']; $js = $data['js']; $rawjs = $data['js_block']; $cb_function = $data['cb_function']; $cb_args = $data['cb_args']; $output_head = '
    '; if ($return === false) { echo $output_head; } try { if (isset($cb_function) === true) { call_user_func_array( $cb_function, (isset($cb_args) === true) ? $cb_args : [] ); } } catch (Exception $e) { error_log('Error executing wizard callback: ', $e->getMessage()); } $output_submit = ''; $output = ''; $first_block_printed = false; if (is_array($rows)) { foreach ($rows as $row) { if ($row['new_form_block'] == true) { if ($first_block_printed === true) { // If first form block has been placed, then close it before starting a new one. $output .= '
    '; $output .= '
    '; } else { $output .= '
    '; } $first_block_printed = true; } $output .= '
    '; foreach ($row['columns'] as $column) { $width = isset($column['width']) ? 'width: '.$column['width'].';' : 'width: 100%;'; $padding_left = isset($column['padding-left']) ? 'padding-left: '.$column['padding-left'].';' : 'padding-left: 0;'; $padding_right = isset($column['padding-right']) ? 'padding-right: '.$column['padding-right'].';' : 'padding-right: 0;'; $extra_styles = isset($column['style']) ? $column['style'] : ''; $class = isset($column['class']) ? $column['class'] : ''; $output .= '
    '; // Toggle option. foreach ($column['inputs'] as $input) { if (is_array($input)) { if ($input['arguments']['type'] != 'submit') { if ($input['toggle'] === true) { $output .= ui_print_toggle( [ 'name' => (isset($input['toggle_name']) ? $input['toggle_name'] : 'toggle_'.uniqid()), 'title' => $input['toggle_title'], 'id' => $input['toggle_id'], 'hidden_default' => $input['toggle_hidden_default'], 'content' => self::printBlockAsGrid( $input, true ), 'return' => true, 'name' => (isset($input['toggle_name']) ? $input['toggle_name'] : 'toggle_'.uniqid()), 'toggle_class' => $input['toggle_toggle_class'], 'main_class' => $input['toggle_main_class'], 'container_class' => $input['toggle_container_class'], 'img_a' => $input['toggle_img_a'], 'img_b' => $input['toggle_img_b'], 'clean' => (isset($input['toggle_clean']) ? $input['toggle_clean'] : true), ] ); } else { $output .= self::printBlockAsGrid($input, true); } } else { $output_submit .= self::printBlockAsGrid($input, true); } } else { $output .= $input; } } $output .= '
    '; } $output .= '
    '; } } // There is possible add raw inputs for this form. if (empty($rawInputs) === false) { $output .= $rawInputs; } $output .= '
    '; $output .= '
      '.$output_submit.'
    '; $output .= html_print_csrf_hidden(true); $output .= ''; $output .= ''; if ($rawjs) { $output .= $rawjs; } if ($return === false) { echo $output; } return $output_head.$output; } /** * Print a form as a list. * * @param array $data Definition of target form to be printed. * @param boolean $return Return as string or direct output. * * @return string HTML code. */ public static function printFormAsList(array $data, bool $return=false) { $form = $data['form']; $inputs = $data['inputs']; $rawInputs = $data['rawInputs']; $js = $data['js']; $rawjs = $data['js_block']; $cb_function = $data['cb_function']; $cb_args = $data['cb_args']; $output_head = '
    '; if ($return === false) { echo $output_head; } try { if (isset($cb_function) === true) { call_user_func_array( $cb_function, (isset($cb_args) === true) ? $cb_args : [] ); } } catch (Exception $e) { error_log('Error executing wizard callback: ', $e->getMessage()); } $output = '
    '; $output .= '
      '; foreach ($inputs as $input) { if ($input['arguments']['type'] != 'submit') { $output .= self::printBlockAsList($input, true); } else { $output_submit .= self::printBlockAsList($input, true); } } $output .= '
    '; // There is possible add raw inputs for this form. if (empty($rawInputs) === false) { $output .= $rawInputs; } $output .= '
    '; $output .= '
      '.$output_submit.'
    '; $output .= html_print_csrf_hidden(true); $output .= '
    '; $output .= ''; if ($rawjs) { $output .= $rawjs; } if ($return === false) { echo $output; } return $output_head.$output; } /** * Print a big button element (huge image, big text and link). * * @param array $data Element data (link, image...). * * @return void Only prints the element. */ public static function printBigButtonElement($data) { if (isset($data['url']) === false) { $data['url'] = '#'; } ?>

  • '; array_map('self::printBigButtonElement', $list_data); echo ''; } /** * Returns a n-dimensional array (data) into a html tree structure. * * Advanced documentation: * https://www.jqueryscript.net/other/Checkable-Hierarchical-Tree.html * * @param string $target Target DOM id. * @param array $data N-dimensional array. * @param string $onclick Onclick function. * @param string $onchange Onchange function. * * @return string */ public static function printTree( $target, $data, $onclick='', $onchange='' ) { ui_require_css_file('simTree'); ui_require_javascript_file('simTree'); /* * SAMPLE SELECT ALL. * * echo '
    '; * echo ''; * echo __('Please select devices to be monitored'); * echo '
    '; * echo ''; * echo ''; * echo '
    '; * echo '
    '; * * END SAMPLE SELECT ALL. */ $output = ' '; return $output; } }