/**
 * hiweb_tpl {html_select_time} function plugin
 * Type:     function<br>
 * Name:     html_select_time<br>
 * Purpose:  Prints the dropdowns for time selection
 *
 * @link     http://www.smurty.net/manual/en/language.function.html.select.time.php {html_select_time}
 *           (hiweb_tpl online manual)
 * @author   Roberto Berto <*****@*****.**>
 * @author   Monte Ohrt <monte AT ohrt DOT com>
 *
 * @param array $params parameters
 *
 * @return string
 * @uses     smurty_make_timestamp()
 */
function smurty_function_html_select_time($params)
{
    $prefix = "Time_";
    $field_array = null;
    $field_separator = "\n";
    $option_separator = "\n";
    $time = null;
    $display_hours = true;
    $display_minutes = true;
    $display_seconds = true;
    $display_meridian = true;
    $hour_format = '%02d';
    $hour_value_format = '%02d';
    $minute_format = '%02d';
    $minute_value_format = '%02d';
    $second_format = '%02d';
    $second_value_format = '%02d';
    $hour_size = null;
    $minute_size = null;
    $second_size = null;
    $meridian_size = null;
    $all_empty = null;
    $hour_empty = null;
    $minute_empty = null;
    $second_empty = null;
    $meridian_empty = null;
    $all_id = null;
    $hour_id = null;
    $minute_id = null;
    $second_id = null;
    $meridian_id = null;
    $use_24_hours = true;
    $minute_interval = 1;
    $second_interval = 1;
    $extra_attrs = '';
    $all_extra = null;
    $hour_extra = null;
    $minute_extra = null;
    $second_extra = null;
    $meridian_extra = null;
    foreach ($params as $_key => $_value) {
        switch ($_key) {
            case 'time':
                if (!is_array($_value) && $_value !== null) {
                    $time = smurty_make_timestamp($_value);
                }
                break;
            case 'prefix':
            case 'field_array':
            case 'field_separator':
            case 'option_separator':
            case 'all_extra':
            case 'hour_extra':
            case 'minute_extra':
            case 'second_extra':
            case 'meridian_extra':
            case 'all_empty':
            case 'hour_empty':
            case 'minute_empty':
            case 'second_empty':
            case 'meridian_empty':
            case 'all_id':
            case 'hour_id':
            case 'minute_id':
            case 'second_id':
            case 'meridian_id':
            case 'hour_format':
            case 'hour_value_format':
            case 'minute_format':
            case 'minute_value_format':
            case 'second_format':
            case 'second_value_format':
                ${$_key} = (string) $_value;
                break;
            case 'display_hours':
            case 'display_minutes':
            case 'display_seconds':
            case 'display_meridian':
            case 'use_24_hours':
                ${$_key} = (bool) $_value;
                break;
            case 'minute_interval':
            case 'second_interval':
            case 'hour_size':
            case 'minute_size':
            case 'second_size':
            case 'meridian_size':
                ${$_key} = (int) $_value;
                break;
            default:
                if (!is_array($_value)) {
                    $extra_attrs .= ' ' . $_key . '="' . smurty_function_escape_special_chars($_value) . '"';
                } else {
                    trigger_error("html_select_date: extra attribute '{$_key}' cannot be an array", E_USER_NOTICE);
                }
                break;
        }
    }
    if (isset($params['time']) && is_array($params['time'])) {
        if (isset($params['time'][$prefix . 'Hour'])) {
            // $_REQUEST[$field_array] given
            foreach (array('H' => 'Hour', 'i' => 'Minute', 's' => 'Second') as $_elementKey => $_elementName) {
                $_variableName = '_' . strtolower($_elementName);
                ${$_variableName} = isset($params['time'][$prefix . $_elementName]) ? $params['time'][$prefix . $_elementName] : date($_elementKey);
            }
            $_meridian = isset($params['time'][$prefix . 'Meridian']) ? ' ' . $params['time'][$prefix . 'Meridian'] : '';
            $time = strtotime($_hour . ':' . $_minute . ':' . $_second . $_meridian);
            list($_hour, $_minute, $_second) = $time = explode('-', date('H-i-s', $time));
        } elseif (isset($params['time'][$field_array][$prefix . 'Hour'])) {
            // $_REQUEST given
            foreach (array('H' => 'Hour', 'i' => 'Minute', 's' => 'Second') as $_elementKey => $_elementName) {
                $_variableName = '_' . strtolower($_elementName);
                ${$_variableName} = isset($params['time'][$field_array][$prefix . $_elementName]) ? $params['time'][$field_array][$prefix . $_elementName] : date($_elementKey);
            }
            $_meridian = isset($params['time'][$field_array][$prefix . 'Meridian']) ? ' ' . $params['time'][$field_array][$prefix . 'Meridian'] : '';
            $time = strtotime($_hour . ':' . $_minute . ':' . $_second . $_meridian);
            list($_hour, $_minute, $_second) = $time = explode('-', date('H-i-s', $time));
        } else {
            // no date found, use NOW
            list($_year, $_month, $_day) = $time = explode('-', date('Y-m-d'));
        }
    } elseif ($time === null) {
        if (array_key_exists('time', $params)) {
            $_hour = $_minute = $_second = $time = null;
        } else {
            list($_hour, $_minute, $_second) = $time = explode('-', date('H-i-s'));
        }
    } else {
        list($_hour, $_minute, $_second) = $time = explode('-', date('H-i-s', $time));
    }
    // generate hour <select>
    if ($display_hours) {
        $_html_hours = '';
        $_extra = '';
        $_name = $field_array ? $field_array . '[' . $prefix . 'Hour]' : $prefix . 'Hour';
        if ($all_extra) {
            $_extra .= ' ' . $all_extra;
        }
        if ($hour_extra) {
            $_extra .= ' ' . $hour_extra;
        }
        $_html_hours = '<select name="' . $_name . '"';
        if ($hour_id !== null || $all_id !== null) {
            $_html_hours .= ' id="' . smurty_function_escape_special_chars($hour_id !== null ? $hour_id ? $hour_id : $_name : ($all_id ? $all_id . $_name : $_name)) . '"';
        }
        if ($hour_size) {
            $_html_hours .= ' size="' . $hour_size . '"';
        }
        $_html_hours .= $_extra . $extra_attrs . '>' . $option_separator;
        if (isset($hour_empty) || isset($all_empty)) {
            $_html_hours .= '<option value="">' . (isset($hour_empty) ? $hour_empty : $all_empty) . '</option>' . $option_separator;
        }
        $start = $use_24_hours ? 0 : 1;
        $end = $use_24_hours ? 23 : 12;
        for ($i = $start; $i <= $end; $i++) {
            $_val = sprintf('%02d', $i);
            $_text = $hour_format == '%02d' ? $_val : sprintf($hour_format, $i);
            $_value = $hour_value_format == '%02d' ? $_val : sprintf($hour_value_format, $i);
            if (!$use_24_hours) {
                $_hour12 = $_hour == 0 ? 12 : ($_hour <= 12 ? $_hour : $_hour - 12);
            }
            $selected = $_hour !== null ? $use_24_hours ? $_hour == $_val : $_hour12 == $_val : null;
            $_html_hours .= '<option value="' . $_value . '"' . ($selected ? ' selected="selected"' : '') . '>' . $_text . '</option>' . $option_separator;
        }
        $_html_hours .= '</select>';
    }
    // generate minute <select>
    if ($display_minutes) {
        $_html_minutes = '';
        $_extra = '';
        $_name = $field_array ? $field_array . '[' . $prefix . 'Minute]' : $prefix . 'Minute';
        if ($all_extra) {
            $_extra .= ' ' . $all_extra;
        }
        if ($minute_extra) {
            $_extra .= ' ' . $minute_extra;
        }
        $_html_minutes = '<select name="' . $_name . '"';
        if ($minute_id !== null || $all_id !== null) {
            $_html_minutes .= ' id="' . smurty_function_escape_special_chars($minute_id !== null ? $minute_id ? $minute_id : $_name : ($all_id ? $all_id . $_name : $_name)) . '"';
        }
        if ($minute_size) {
            $_html_minutes .= ' size="' . $minute_size . '"';
        }
        $_html_minutes .= $_extra . $extra_attrs . '>' . $option_separator;
        if (isset($minute_empty) || isset($all_empty)) {
            $_html_minutes .= '<option value="">' . (isset($minute_empty) ? $minute_empty : $all_empty) . '</option>' . $option_separator;
        }
        $selected = $_minute !== null ? $_minute - $_minute % $minute_interval : null;
        for ($i = 0; $i <= 59; $i += $minute_interval) {
            $_val = sprintf('%02d', $i);
            $_text = $minute_format == '%02d' ? $_val : sprintf($minute_format, $i);
            $_value = $minute_value_format == '%02d' ? $_val : sprintf($minute_value_format, $i);
            $_html_minutes .= '<option value="' . $_value . '"' . ($selected === $i ? ' selected="selected"' : '') . '>' . $_text . '</option>' . $option_separator;
        }
        $_html_minutes .= '</select>';
    }
    // generate second <select>
    if ($display_seconds) {
        $_html_seconds = '';
        $_extra = '';
        $_name = $field_array ? $field_array . '[' . $prefix . 'Second]' : $prefix . 'Second';
        if ($all_extra) {
            $_extra .= ' ' . $all_extra;
        }
        if ($second_extra) {
            $_extra .= ' ' . $second_extra;
        }
        $_html_seconds = '<select name="' . $_name . '"';
        if ($second_id !== null || $all_id !== null) {
            $_html_seconds .= ' id="' . smurty_function_escape_special_chars($second_id !== null ? $second_id ? $second_id : $_name : ($all_id ? $all_id . $_name : $_name)) . '"';
        }
        if ($second_size) {
            $_html_seconds .= ' size="' . $second_size . '"';
        }
        $_html_seconds .= $_extra . $extra_attrs . '>' . $option_separator;
        if (isset($second_empty) || isset($all_empty)) {
            $_html_seconds .= '<option value="">' . (isset($second_empty) ? $second_empty : $all_empty) . '</option>' . $option_separator;
        }
        $selected = $_second !== null ? $_second - $_second % $second_interval : null;
        for ($i = 0; $i <= 59; $i += $second_interval) {
            $_val = sprintf('%02d', $i);
            $_text = $second_format == '%02d' ? $_val : sprintf($second_format, $i);
            $_value = $second_value_format == '%02d' ? $_val : sprintf($second_value_format, $i);
            $_html_seconds .= '<option value="' . $_value . '"' . ($selected === $i ? ' selected="selected"' : '') . '>' . $_text . '</option>' . $option_separator;
        }
        $_html_seconds .= '</select>';
    }
    // generate meridian <select>
    if ($display_meridian && !$use_24_hours) {
        $_html_meridian = '';
        $_extra = '';
        $_name = $field_array ? $field_array . '[' . $prefix . 'Meridian]' : $prefix . 'Meridian';
        if ($all_extra) {
            $_extra .= ' ' . $all_extra;
        }
        if ($meridian_extra) {
            $_extra .= ' ' . $meridian_extra;
        }
        $_html_meridian = '<select name="' . $_name . '"';
        if ($meridian_id !== null || $all_id !== null) {
            $_html_meridian .= ' id="' . smurty_function_escape_special_chars($meridian_id !== null ? $meridian_id ? $meridian_id : $_name : ($all_id ? $all_id . $_name : $_name)) . '"';
        }
        if ($meridian_size) {
            $_html_meridian .= ' size="' . $meridian_size . '"';
        }
        $_html_meridian .= $_extra . $extra_attrs . '>' . $option_separator;
        if (isset($meridian_empty) || isset($all_empty)) {
            $_html_meridian .= '<option value="">' . (isset($meridian_empty) ? $meridian_empty : $all_empty) . '</option>' . $option_separator;
        }
        $_html_meridian .= '<option value="am"' . ($_hour > 0 && $_hour < 12 ? ' selected="selected"' : '') . '>AM</option>' . $option_separator . '<option value="pm"' . ($_hour < 12 ? '' : ' selected="selected"') . '>PM</option>' . $option_separator . '</select>';
    }
    $_html = '';
    foreach (array('_html_hours', '_html_minutes', '_html_seconds', '_html_meridian') as $k) {
        if (isset(${$k})) {
            if ($_html) {
                $_html .= $field_separator;
            }
            $_html .= ${$k};
        }
    }
    return $_html;
}
function smurty_function_html_checkboxes_output($name, $value, $output, $selected, $extra, $separator, $labels, $label_ids, $escape = true)
{
    $_output = '';
    if (is_object($value)) {
        if (method_exists($value, "__toString")) {
            $value = (string) $value->__toString();
        } else {
            trigger_error("html_options: value is an object of class '" . get_class($value) . "' without __toString() method", E_USER_NOTICE);
            return '';
        }
    } else {
        $value = (string) $value;
    }
    if (is_object($output)) {
        if (method_exists($output, "__toString")) {
            $output = (string) $output->__toString();
        } else {
            trigger_error("html_options: output is an object of class '" . get_class($output) . "' without __toString() method", E_USER_NOTICE);
            return '';
        }
    } else {
        $output = (string) $output;
    }
    if ($labels) {
        if ($label_ids) {
            $_id = smurty_function_escape_special_chars(preg_replace('![^\\w\\-\\.]!' . hiweb_tpl::$_UTF8_MODIFIER, '_', $name . '_' . $value));
            $_output .= '<label for="' . $_id . '">';
        } else {
            $_output .= '<label>';
        }
    }
    $name = smurty_function_escape_special_chars($name);
    $value = smurty_function_escape_special_chars($value);
    if ($escape) {
        $output = smurty_function_escape_special_chars($output);
    }
    $_output .= '<input type="checkbox" name="' . $name . '[]" value="' . $value . '"';
    if ($labels && $label_ids) {
        $_output .= ' id="' . $_id . '"';
    }
    if (is_array($selected)) {
        if (isset($selected[$value])) {
            $_output .= ' checked="checked"';
        }
    } elseif ($value === $selected) {
        $_output .= ' checked="checked"';
    }
    $_output .= $extra . ' />' . $output;
    if ($labels) {
        $_output .= '</label>';
    }
    $_output .= $separator;
    return $_output;
}
function smurty_function_html_options_optgroup($key, $values, $selected, $id, $class, &$idx)
{
    $optgroup_html = '<optgroup label="' . smurty_function_escape_special_chars($key) . '">' . "\n";
    foreach ($values as $key => $value) {
        $optgroup_html .= smurty_function_html_options_optoutput($key, $value, $selected, $id, $class, $idx);
    }
    $optgroup_html .= "</optgroup>\n";
    return $optgroup_html;
}
/**
 * hiweb_tpl {html_select_date} plugin
 * Type:     function<br>
 * Name:     html_select_date<br>
 * Purpose:  Prints the dropdowns for date selection.
 * ChangeLog:
 * <pre>
 *            - 1.0 initial release
 *            - 1.1 added support for +/- N syntax for begin
 *              and end year values. (Monte)
 *            - 1.2 added support for yyyy-mm-dd syntax for
 *              time value. (Jan Rosier)
 *            - 1.3 added support for choosing format for
 *              month values (Gary Loescher)
 *            - 1.3.1 added support for choosing format for
 *              day values (Marcus Bointon)
 *            - 1.3.2 support negative timestamps, force year
 *              dropdown to include given date unless explicitly set (Monte)
 *            - 1.3.4 fix behaviour of 0000-00-00 00:00:00 dates to match that
 *              of 0000-00-00 dates (cybot, boots)
 *            - 2.0 complete rewrite for performance,
 *              added attributes month_names, *_id
 * </pre>
 *
 * @link     http://www.smurty.net/manual/en/language.function.html.select.date.php {html_select_date}
 *           (hiweb_tpl online manual)
 * @version  2.0
 * @author   Andrei Zmievski
 * @author   Monte Ohrt <monte at ohrt dot com>
 * @author   Rodney Rehm
 *
 * @param array $params parameters
 *
 * @return string
 */
function smurty_function_html_select_date($params)
{
    // generate timestamps used for month names only
    static $_month_timestamps = null;
    static $_current_year = null;
    if ($_month_timestamps === null) {
        $_current_year = date('Y');
        $_month_timestamps = array();
        for ($i = 1; $i <= 12; $i++) {
            $_month_timestamps[$i] = mktime(0, 0, 0, $i, 1, 2000);
        }
    }
    /* Default values. */
    $prefix = "Date_";
    $start_year = null;
    $end_year = null;
    $display_days = true;
    $display_months = true;
    $display_years = true;
    $month_format = "%B";
    /* Write months as numbers by default  GL */
    $month_value_format = "%m";
    $day_format = "%02d";
    /* Write day values using this format MB */
    $day_value_format = "%d";
    $year_as_text = false;
    /* Display years in reverse order? Ie. 2000,1999,.... */
    $reverse_years = false;
    /* Should the select boxes be part of an array when returned from PHP?
       e.g. setting it to "birthday", would create "birthday[Day]",
       "birthday[Month]" & "birthday[Year]". Can be combined with prefix */
    $field_array = null;
    /* <select size>'s of the different <select> tags.
       If not set, uses default dropdown. */
    $day_size = null;
    $month_size = null;
    $year_size = null;
    /* Unparsed attributes common to *ALL* the <select>/<input> tags.
       An example might be in the template: all_extra ='class ="foo"'. */
    $all_extra = null;
    /* Separate attributes for the tags. */
    $day_extra = null;
    $month_extra = null;
    $year_extra = null;
    /* Order in which to display the fields.
       "D" -> day, "M" -> month, "Y" -> year. */
    $field_order = 'MDY';
    /* String printed between the different fields. */
    $field_separator = "\n";
    $option_separator = "\n";
    $time = null;
    // $all_empty = null;
    // $day_empty = null;
    // $month_empty = null;
    // $year_empty = null;
    $extra_attrs = '';
    $all_id = null;
    $day_id = null;
    $month_id = null;
    $year_id = null;
    foreach ($params as $_key => $_value) {
        switch ($_key) {
            case 'time':
                if (!is_array($_value) && $_value !== null) {
                    $time = smurty_make_timestamp($_value);
                }
                break;
            case 'month_names':
                if (is_array($_value) && count($_value) == 12) {
                    ${$_key} = $_value;
                } else {
                    trigger_error("html_select_date: month_names must be an array of 12 strings", E_USER_NOTICE);
                }
                break;
            case 'prefix':
            case 'field_array':
            case 'start_year':
            case 'end_year':
            case 'day_format':
            case 'day_value_format':
            case 'month_format':
            case 'month_value_format':
            case 'day_size':
            case 'month_size':
            case 'year_size':
            case 'all_extra':
            case 'day_extra':
            case 'month_extra':
            case 'year_extra':
            case 'field_order':
            case 'field_separator':
            case 'option_separator':
            case 'all_empty':
            case 'month_empty':
            case 'day_empty':
            case 'year_empty':
            case 'all_id':
            case 'month_id':
            case 'day_id':
            case 'year_id':
                ${$_key} = (string) $_value;
                break;
            case 'display_days':
            case 'display_months':
            case 'display_years':
            case 'year_as_text':
            case 'reverse_years':
                ${$_key} = (bool) $_value;
                break;
            default:
                if (!is_array($_value)) {
                    $extra_attrs .= ' ' . $_key . '="' . smurty_function_escape_special_chars($_value) . '"';
                } else {
                    trigger_error("html_select_date: extra attribute '{$_key}' cannot be an array", E_USER_NOTICE);
                }
                break;
        }
    }
    // Note: date() is faster than strftime()
    // Note: explode(date()) is faster than date() date() date()
    if (isset($params['time']) && is_array($params['time'])) {
        if (isset($params['time'][$prefix . 'Year'])) {
            // $_REQUEST[$field_array] given
            foreach (array('Y' => 'Year', 'm' => 'Month', 'd' => 'Day') as $_elementKey => $_elementName) {
                $_variableName = '_' . strtolower($_elementName);
                ${$_variableName} = isset($params['time'][$prefix . $_elementName]) ? $params['time'][$prefix . $_elementName] : date($_elementKey);
            }
        } elseif (isset($params['time'][$field_array][$prefix . 'Year'])) {
            // $_REQUEST given
            foreach (array('Y' => 'Year', 'm' => 'Month', 'd' => 'Day') as $_elementKey => $_elementName) {
                $_variableName = '_' . strtolower($_elementName);
                ${$_variableName} = isset($params['time'][$field_array][$prefix . $_elementName]) ? $params['time'][$field_array][$prefix . $_elementName] : date($_elementKey);
            }
        } else {
            // no date found, use NOW
            list($_year, $_month, $_day) = $time = explode('-', date('Y-m-d'));
        }
    } elseif ($time === null) {
        if (array_key_exists('time', $params)) {
            $_year = $_month = $_day = $time = null;
        } else {
            list($_year, $_month, $_day) = $time = explode('-', date('Y-m-d'));
        }
    } else {
        list($_year, $_month, $_day) = $time = explode('-', date('Y-m-d', $time));
    }
    // make syntax "+N" or "-N" work with $start_year and $end_year
    // Note preg_match('!^(\+|\-)\s*(\d+)$!', $end_year, $match) is slower than trim+substr
    foreach (array('start', 'end') as $key) {
        $key .= '_year';
        $t = ${$key};
        if ($t === null) {
            ${$key} = (int) $_current_year;
        } elseif ($t[0] == '+') {
            ${$key} = (int) ($_current_year + (int) trim(substr($t, 1)));
        } elseif ($t[0] == '-') {
            ${$key} = (int) ($_current_year - (int) trim(substr($t, 1)));
        } else {
            ${$key} = (int) ${$key};
        }
    }
    // flip for ascending or descending
    if ($start_year > $end_year && !$reverse_years || $start_year < $end_year && $reverse_years) {
        $t = $end_year;
        $end_year = $start_year;
        $start_year = $t;
    }
    // generate year <select> or <input>
    if ($display_years) {
        $_extra = '';
        $_name = $field_array ? $field_array . '[' . $prefix . 'Year]' : $prefix . 'Year';
        if ($all_extra) {
            $_extra .= ' ' . $all_extra;
        }
        if ($year_extra) {
            $_extra .= ' ' . $year_extra;
        }
        if ($year_as_text) {
            $_html_years = '<input type="text" name="' . $_name . '" value="' . $_year . '" size="4" maxlength="4"' . $_extra . $extra_attrs . ' />';
        } else {
            $_html_years = '<select name="' . $_name . '"';
            if ($year_id !== null || $all_id !== null) {
                $_html_years .= ' id="' . smurty_function_escape_special_chars($year_id !== null ? $year_id ? $year_id : $_name : ($all_id ? $all_id . $_name : $_name)) . '"';
            }
            if ($year_size) {
                $_html_years .= ' size="' . $year_size . '"';
            }
            $_html_years .= $_extra . $extra_attrs . '>' . $option_separator;
            if (isset($year_empty) || isset($all_empty)) {
                $_html_years .= '<option value="">' . (isset($year_empty) ? $year_empty : $all_empty) . '</option>' . $option_separator;
            }
            $op = $start_year > $end_year ? -1 : 1;
            for ($i = $start_year; $op > 0 ? $i <= $end_year : $i >= $end_year; $i += $op) {
                $_html_years .= '<option value="' . $i . '"' . ($_year == $i ? ' selected="selected"' : '') . '>' . $i . '</option>' . $option_separator;
            }
            $_html_years .= '</select>';
        }
    }
    // generate month <select> or <input>
    if ($display_months) {
        $_extra = '';
        $_name = $field_array ? $field_array . '[' . $prefix . 'Month]' : $prefix . 'Month';
        if ($all_extra) {
            $_extra .= ' ' . $all_extra;
        }
        if ($month_extra) {
            $_extra .= ' ' . $month_extra;
        }
        $_html_months = '<select name="' . $_name . '"';
        if ($month_id !== null || $all_id !== null) {
            $_html_months .= ' id="' . smurty_function_escape_special_chars($month_id !== null ? $month_id ? $month_id : $_name : ($all_id ? $all_id . $_name : $_name)) . '"';
        }
        if ($month_size) {
            $_html_months .= ' size="' . $month_size . '"';
        }
        $_html_months .= $_extra . $extra_attrs . '>' . $option_separator;
        if (isset($month_empty) || isset($all_empty)) {
            $_html_months .= '<option value="">' . (isset($month_empty) ? $month_empty : $all_empty) . '</option>' . $option_separator;
        }
        for ($i = 1; $i <= 12; $i++) {
            $_val = sprintf('%02d', $i);
            $_text = isset($month_names) ? smurty_function_escape_special_chars($month_names[$i]) : ($month_format == "%m" ? $_val : strftime($month_format, $_month_timestamps[$i]));
            $_value = $month_value_format == "%m" ? $_val : strftime($month_value_format, $_month_timestamps[$i]);
            $_html_months .= '<option value="' . $_value . '"' . ($_val == $_month ? ' selected="selected"' : '') . '>' . $_text . '</option>' . $option_separator;
        }
        $_html_months .= '</select>';
    }
    // generate day <select> or <input>
    if ($display_days) {
        $_extra = '';
        $_name = $field_array ? $field_array . '[' . $prefix . 'Day]' : $prefix . 'Day';
        if ($all_extra) {
            $_extra .= ' ' . $all_extra;
        }
        if ($day_extra) {
            $_extra .= ' ' . $day_extra;
        }
        $_html_days = '<select name="' . $_name . '"';
        if ($day_id !== null || $all_id !== null) {
            $_html_days .= ' id="' . smurty_function_escape_special_chars($day_id !== null ? $day_id ? $day_id : $_name : ($all_id ? $all_id . $_name : $_name)) . '"';
        }
        if ($day_size) {
            $_html_days .= ' size="' . $day_size . '"';
        }
        $_html_days .= $_extra . $extra_attrs . '>' . $option_separator;
        if (isset($day_empty) || isset($all_empty)) {
            $_html_days .= '<option value="">' . (isset($day_empty) ? $day_empty : $all_empty) . '</option>' . $option_separator;
        }
        for ($i = 1; $i <= 31; $i++) {
            $_val = sprintf('%02d', $i);
            $_text = $day_format == '%02d' ? $_val : sprintf($day_format, $i);
            $_value = $day_value_format == '%02d' ? $_val : sprintf($day_value_format, $i);
            $_html_days .= '<option value="' . $_value . '"' . ($_val == $_day ? ' selected="selected"' : '') . '>' . $_text . '</option>' . $option_separator;
        }
        $_html_days .= '</select>';
    }
    // order the fields for output
    $_html = '';
    for ($i = 0; $i <= 2; $i++) {
        switch ($field_order[$i]) {
            case 'Y':
            case 'y':
                if (isset($_html_years)) {
                    if ($_html) {
                        $_html .= $field_separator;
                    }
                    $_html .= $_html_years;
                }
                break;
            case 'm':
            case 'M':
                if (isset($_html_months)) {
                    if ($_html) {
                        $_html .= $field_separator;
                    }
                    $_html .= $_html_months;
                }
                break;
            case 'd':
            case 'D':
                if (isset($_html_days)) {
                    if ($_html) {
                        $_html .= $field_separator;
                    }
                    $_html .= $_html_days;
                }
                break;
        }
    }
    return $_html;
}
/**
 * hiweb_tpl {html_image} function plugin
 * Type:     function<br>
 * Name:     html_image<br>
 * Date:     Feb 24, 2003<br>
 * Purpose:  format HTML tags for the image<br>
 * Examples: {html_image file="/images/masthead.gif"}<br>
 * Output:   <img src="/images/masthead.gif" width=400 height=23><br>
 * Params:
 * <pre>
 * - file        - (required) - file (and path) of image
 * - height      - (optional) - image height (default actual height)
 * - width       - (optional) - image width (default actual width)
 * - basedir     - (optional) - base directory for absolute paths, default is environment variable DOCUMENT_ROOT
 * - path_prefix - prefix for path output (optional, default empty)
 * </pre>
 *
 * @link    http://www.smurty.net/manual/en/language.function.html.image.php {html_image}
 *          (hiweb_tpl online manual)
 * @author  Monte Ohrt <monte at ohrt dot com>
 * @author  credits to Duda <*****@*****.**>
 * @version 1.0
 *
 * @param array                    $params   parameters
 * @param Smurty_Internal_Template $template template object
 *
 * @throws SmurtyException
 * @return string
 * @uses    smurty_function_escape_special_chars()
 */
function smurty_function_html_image($params, $template)
{
    require_once HIWEB_TPL_PLUGINS_DIR . 'shared.escape_special_chars.php';
    $alt = '';
    $file = '';
    $height = '';
    $width = '';
    $extra = '';
    $prefix = '';
    $suffix = '';
    $path_prefix = '';
    $basedir = isset($_SERVER['DOCUMENT_ROOT']) ? $_SERVER['DOCUMENT_ROOT'] : '';
    foreach ($params as $_key => $_val) {
        switch ($_key) {
            case 'file':
            case 'height':
            case 'width':
            case 'dpi':
            case 'path_prefix':
            case 'basedir':
                ${$_key} = $_val;
                break;
            case 'alt':
                if (!is_array($_val)) {
                    ${$_key} = smurty_function_escape_special_chars($_val);
                } else {
                    throw new SmurtyException("html_image: extra attribute '{$_key}' cannot be an array", E_USER_NOTICE);
                }
                break;
            case 'link':
            case 'href':
                $prefix = '<a href="' . $_val . '">';
                $suffix = '</a>';
                break;
            default:
                if (!is_array($_val)) {
                    $extra .= ' ' . $_key . '="' . smurty_function_escape_special_chars($_val) . '"';
                } else {
                    throw new SmurtyException("html_image: extra attribute '{$_key}' cannot be an array", E_USER_NOTICE);
                }
                break;
        }
    }
    if (empty($file)) {
        trigger_error("html_image: missing 'file' parameter", E_USER_NOTICE);
        return;
    }
    if ($file[0] == '/') {
        $_image_path = $basedir . $file;
    } else {
        $_image_path = $file;
    }
    // strip file protocol
    if (stripos($params['file'], 'file://') === 0) {
        $params['file'] = substr($params['file'], 7);
    }
    $protocol = strpos($params['file'], '://');
    if ($protocol !== false) {
        $protocol = strtolower(substr($params['file'], 0, $protocol));
    }
    if (isset($template->smurty->security_policy)) {
        if ($protocol) {
            // remote resource (or php stream, …)
            if (!$template->smurty->security_policy->isTrustedUri($params['file'])) {
                return;
            }
        } else {
            // local file
            if (!$template->smurty->security_policy->isTrustedResourceDir($_image_path)) {
                return;
            }
        }
    }
    if (!isset($params['width']) || !isset($params['height'])) {
        // FIXME: (rodneyrehm) getimagesize() loads the complete file off a remote resource, use custom [jpg,png,gif]header reader!
        if (!($_image_data = @getimagesize($_image_path))) {
            if (!file_exists($_image_path)) {
                trigger_error("html_image: unable to find '{$_image_path}'", E_USER_NOTICE);
                return;
            } elseif (!is_readable($_image_path)) {
                trigger_error("html_image: unable to read '{$_image_path}'", E_USER_NOTICE);
                return;
            } else {
                trigger_error("html_image: '{$_image_path}' is not a valid image file", E_USER_NOTICE);
                return;
            }
        }
        if (!isset($params['width'])) {
            $width = $_image_data[0];
        }
        if (!isset($params['height'])) {
            $height = $_image_data[1];
        }
    }
    if (isset($params['dpi'])) {
        if (strstr($_SERVER['HTTP_USER_AGENT'], 'Mac')) {
            // FIXME: (rodneyrehm) wrong dpi assumption
            // don't know who thought this up… even if it was true in 1998, it's definitely wrong in 2011.
            $dpi_default = 72;
        } else {
            $dpi_default = 96;
        }
        $_resize = $dpi_default / $params['dpi'];
        $width = round($width * $_resize);
        $height = round($height * $_resize);
    }
    return $prefix . '<img src="' . $path_prefix . $file . '" alt="' . $alt . '" width="' . $width . '" height="' . $height . '"' . $extra . ' />' . $suffix;
}