form_functions.php
上传用户:gzy2002
上传日期:2010-02-11
资源大小:1785k
文件大小:18k
- <?php
- // +-------------------------------------------------------------+
- // | DeskPRO v [2.0.1 Production]
- // | Copyright (C) 2001 - 2004 Headstart Solutions Limited
- // | Supplied by WTN-WDYL
- // | Nullified by WTN-WDYL
- // | Distribution via WebForum, ForumRU and associated file dumps
- // +-------------------------------------------------------------+
- // | DESKPRO IS NOT FREE SOFTWARE
- // +-------------------------------------------------------------+
- // | License ID : Full Enterprise License =) ...
- // | License Owner : WTN-WDYL Team
- // +-------------------------------------------------------------+
- // | $RCSfile: form_functions.php,v $
- // | $Date: 2004/02/10 01:34:25 $
- // | $Revision: 1.40 $
- // +-------------------------------------------------------------+
- // | File Details:
- // | - Utility functions for HTML form widget generation.
- // +-------------------------------------------------------------+
- error_reporting(E_ALL ^ E_NOTICE);
- #################################################################################################
- # FORM_x FUNCTIONS
- #################################################################################################
- /*****************************************************
- function html_image
- -----DESCRIPTION: -----------------------------------
- creates an image
- -----ARGUMENTS: -------------------------------------
- filename Name of the image on-disk
- alt Alternate text to display for the image
- width Width of the image
- height Height of the image
- javascript JavaScript to include in the <IMG> tag
- -----RETURNS:----------------------------------------
- the html for the form field
- *****************************************************/
- function html_image($name, $alt='', $width='', $height='', $js='') {
- if (!$width or $height) {
- $size = @getimagesize(LOC_IMAGES . $name);
- if (!$width OR $width == 0) {
- $width = $size[0];
- }
- if (!$height OR $height == 0) {
- $height = $size[1];
- }
- }
-
- $html = "<img src="" . constant('LOC_IMAGES') . $name . """;
- if ($alt) {
- $html .= " alt="$alt" title="$alt"";
- }
- if ($width) {
- $html .= " width="$width"";
- }
- if ($height) {
- $html .= " height="$height"";
- }
- if ($js) {
- $html .= " $js";
- }
- $html .= " border="0" />";
- return $html;
- }
- /*****************************************************
- function form_file
- -----DESCRIPTION: -----------------------------------
- creates a file form element
- -----ARGUMENTS: -------------------------------------
- name : makes the name of the file
- attachment.name (ie the name always
- starts with attachment)
- -----RETURNS:----------------------------------------
- the html for the form field
- *****************************************************/
- function form_file($name = '') {
- $name = 'attachment' . $name;
- return "<input type="file" name="$name" />";
- }
- /*****************************************************
- function form_input
- -----DESCRIPTION: -----------------------------------
- creates a file form element
- htmlspecialchars_uni of the value
- -----ARGUMENTS: -------------------------------------
- name : the name of the textfield
- value : the default value for the textfield
- size (DEF=30) : the length of the textfield
- array : if the textfield is part of an array, ie array[fieldname]
- override : if we do not want to htmlspecialchars_uni() the value
- js : any extra code in the <input> (for example js)
- -----RETURNS:----------------------------------------
- the html for the form field
- *****************************************************/
- function form_input($name, $value=NULL, $size="30", $array='', $override = NULL, $extra='') {
- if ($value != NULL) {
- if (!$override) {
- $value = htmlspecialchars_uni($value);
- }
- }
- if ($array) {
- $name = $array . "[" . $name . "]";
- }
- return "<input type="text" name="$name" value="$value" size="$size" $extra >";
- }
- /*****************************************************
- function form_password
- -----DESCRIPTION: -----------------------------------
- creates a file form element
- htmlspecialchars_uni of the value
- questionable use of value, not really secure
- -----ARGUMENTS: -------------------------------------
- name : the name of the password field
- value : the default value for the password field
- size (DEF=30) : the length of the password field
- array : if the textfield is part of an array, ie array[fieldname]
- -----RETURNS:----------------------------------------
- the html for the password field
- *****************************************************/
- function form_password($name, $value="", $size="30", $array='', $override = NULL) {
- if ($value) {
- if (!$override) {
- $value = htmlspecialchars_uni($value);
- }
- }
- if ($array) {
- $name = $array . "[" . $name . "]";
- }
- return "<input type="password" name="$name" value="$value" size="$size">";
- }
- /*****************************************************
- function form_select
- -----DESCRIPTION: -----------------------------------
- - creates a drop down list
- -----ARGUMENTS: -------------------------------------
- name : the name of the select field
- array : the array of data for the select field. Can be associative or not
- to_array : if the select field is part of an array, ie array[fieldname]
- start : the default selected value
- same : makes the value the same as the displayed value, otherwise it would use either the specified
- values if an associatve array or would use incrementing numbers if standard array
- extra : * needs to be looked into, -1 is probably a better value here * creates a "None Selected" row with the value -----
- size : if specified makes the select a multiselect form element and determines the size (vertically)
- top : add some custom code in the <select > element. Generally used for javascript actions
- override : override htmlspecialchars() call
- id : Element ID tag value
- disabled : Show the element as a disabled one
- -----RETURNS:----------------------------------------
- the html for the select field
- *****************************************************/
- function form_select($name, $array, $to_array='', $start='', $same='0', $extra='', $size='', $top='', $override = NULL, $id = NULL, $disabled = NULL) {
- if ($to_array != "") {
- $name = $to_array . "[" . $name . "]";
- }
- if ($size) {
- $name = $name . "[]";
- }
- if ($id) {
- $id = " id="$id"";
- }
- if ($disabled) {
- $disabled = " DISABLED=disabled ";
- $name = $name."_disable";
- }
- if ($size) {
- $html .= "<select name="$name" size="$size" $disabled $top MULTIPLE $id>n";
- } else {
- $html .= "<select name="$name" $disabled $id $top>n";
- }
- if ($extra) {
- $html .= "<option value="-----">None Selected</option>n";
- }
-
- if (is_array($array)) {
- while (list ($key, $val) = each ($array)) {
- if ($same) {
- $key = $val;
- }
- if (!$override) {
- $key = htmlspecialchars_uni($key);
- $val = htmlspecialchars_uni($val);
- }
- if (is_array($start)) {
- if (in_array($key, $start)) {
- $html .= "<option selected=selected value="$key">$val</option>n";
- } else {
- $html .= "<option value="$key">$val</option>n";
- }
- } else {
- if ($key == $start) {
- $html .= "<option selected=selected value="$key">$val</option>n";
- } else {
- $html .= "<option value="$key">$val</option>n";
- }
- }
- }
- }
- $html .= "</select>n";
- return $html;
- }
- /*****************************************************
- function form_radio
- -----DESCRIPTION: -----------------------------------
- - creates radio buttons
- -----ARGUMENTS: -------------------------------------
- name : array of names
- value : array of values
- start : the value that is preselected
- array : if the radio elements are part of an array, ie array[fieldname]
- extra : extra code to go in the <input > fields. Used for js actions
- -----RETURNS:----------------------------------------
- the html for the radio fields
- *****************************************************/
- function form_radio($name, $value, $start='', $array='', $extra='', $override = NULL) {
- if ($array) {
- $name = $array . "[" . $name . "]";
- }
- $num_elements = count($value);
- $form = "<table>";
- for ($idx = 0; $idx < $num_elements; ++$idx) {
- $form .= "<tr><td>$r[normalfont]$value[$idx]:</font></td><td>";
- if (!$override) {
- $value[$idx] = htmlspecialchars_uni($value[$idx]);
- }
- if ($value[$idx] == $start) {
- $form .= "<input type="radio" name="$name" value="$value[$idx]" checked $extra></td></tr>n";
- } else {
- $form .= "<input type="radio" name="$name" value="$value[$idx]" $extra></td></tr>n";
- }
- }
- $form .= "</table>";
- return $form;
- }
- /*****************************************************
- function form_radio_single
- -----DESCRIPTION: -----------------------------------
- - creates a single radio button
- -----ARGUMENTS: -------------------------------------
- name : name
- value : value
- start : the value that is preselected
- extra : extra code to go in the <input > fields. Used for js actions
- -----RETURNS:----------------------------------------
- the html for the radio field
- *****************************************************/
- function form_radio_single($name, $value, $start='', $extra='', $override = NULL) {
- if ($value) {
- if (!$override) {
- $value = htmlspecialchars_uni($value);
- }
- }
- if ($start) {
- return "<input type="radio" name="$name" value="$value" checked $extra />";
- } else {
- return "<input type="radio" name="$name" value="$value"$extra />";
- }
- }
- /*****************************************************
- function form_radio_yn
- -----DESCRIPTION: -----------------------------------
- - creates a yes/no radio button set
- - these can be replaced with a checkbox, but are often more explanative.
- -----ARGUMENTS: -------------------------------------
- name : name
- array : value
- yes : the value that is preselected
- empty : add a 3rd "empty" option, ie neither Yes or No
- extra : extra code to go in the <input > fields. Used for js actions
- -----RETURNS:----------------------------------------
- the html for the radio fields
- *****************************************************/
- function form_radio_yn($name, $array='', $yes='0', $empty='0', $extra='') {
- if ($array) {
- $name = $array . "[" . $name . "]";
- }
- if ($empty) {
- if ($yes == "1") {
- $html .= "<b>";
- $html .= "Either: <input type="radio" name="$name" value="xxxNULLxxx" $extra>n";
- $html .= "Yes: <input type="radio" name="$name" value="1" checked $extra>n";
- $html .= "No: <input type="radio" name="$name" value="0" $extra>n";
- $html .= "</b>";
- } elseif ($yes == "0") {
- $html .= "<b>";
- $html .= "Either: <input type="radio" name="$name" value="xxxNULLxxx" $extra>n";
- $html .= "Yes: <input type="radio" name="$name" value="1" $extra>n";
- $html .= "No: <input type="radio" name="$name" value="0" checked $extra>n";
- $html .= "</b>";
- } else {
- $html .= "<b>";
- $html .= "Either: <input type="radio" name="$name" value="xxxNULLxxx" checked $extra>n";
- $html .= "Yes: <input type="radio" name="$name" value="1" $extra>n";
- $html .= "No: <input type="radio" name="$name" value="0" $extra>n";
- $html .= "</b>";
- }
- } else {
- if ($yes) {
- $html .= "<b>";
- $html .= "Yes: <input type="radio" name="$name" value="1" checked $extra>n";
- $html .= "No: <input type="radio" name="$name" value="0" $extra>n";
- $html .= "</b>";
- } else {
- $html .= "<b>";
- $html .= "Yes: <input type="radio" name="$name" value="1" $extra>n";
- $html .= "No: <input type="radio" name="$name" value="0" checked $extra>n";
- $html .= "</b>";
- }
- }
- return $html;
- }
- ###################### function form_checkbox() #######################
- function form_checkbox($name, $array, $array2, $override = NULL) {
- $name = $name . "[]";
- $num_elements = count($array);
- for ($idx = 0; $idx < $num_elements; ++$idx) {
- if (!$override) {
- $array[$idx] = htmlspecialchars_uni($array[$idx]);
- }
- $form .= "<br /><input type="checkbox" name="$name" value="$array[$idx]" checked>$array2[$idx]";
- }
- return $form;
- }
- ###################### function form_submit() #######################
- function form_submit($value, $name='submit', $override = NULL, $image = NULL) {
- if (!$override) {
- $value = htmlspecialchars_uni($value);
- }
- if ($image) {
- $image = " src="$image"";
- $type = "image";
- } else {
- $type = "submit";
- }
- return "<input type="$type" name="$name" value="$value"$image>";
- }
- ###################### function form_checkbox_single() #######################
- function form_checkbox_single($name, $value, $checked=NULL, $arrayto='', $override = NULL) {
- if (!$override) {
- $value = htmlspecialchars_uni($value);
- }
- if ($arrayto) {
- $name = $arrayto . "[" . $name . "]";
- }
- if ($checked) {
- $form .= "<input type="checkbox" name="$name" value="$value" checked>";
- } else {
- $form .= "<input type="checkbox" name="$name" value="$value">";
- }
- return $form;
- }
- ###################### function form_date() #######################
- /*
- $name : name of the form generated
- $array : if the form should be submitted as part of an array
- $start : prefill with a unix timestamp
- $make_start : prefill with current date
- $empty : add blank options for an unset date
- $yyyymmdd : prefill with a yyyymmdd date
- $return : array of d/m/y only return those found
- */
- function form_date($name, $array='', $start='', $make_start='', $empty='', $yyyymmdd='', $return = array('y', 'm', 'd')) {
- if ($start) {
- $yyyymmdd = date('Y-m-d', $start);
- }
- if ($yyyymmdd) {
- $temp_date = explode('-', $yyyymmdd);
- $start = array();
- $start[day] = $temp_date[2];
- $start[month] = $temp_date[1];
- $start[year] = $temp_date[0];
- } elseif ($make_start == "1") {
- $temp_date = getdate(mktime());
- $start = array();
- $start[day] = $temp_date[mday];
- $start[month] = $temp_date[mon];
- $start[year] = $temp_date[year];
- }
- $day = make_numberarray(1, 31);
- $month = array('1' => 'Jan', '2' => 'Feb', '3' => 'Mar', '4' => 'Apr', '5' => 'May', '6' => 'Jun', '7' => 'Jul', '8' => 'Aug', '9' => 'Sep', '10' => 'Oct', '11' => 'Nov', '12' => 'Dec');
- $year = array('2000', '2001', '2002', '2003', '2004', '2005', '2006', '2007', '2008', '2009', '2010', '2011', '2012', '2013', '2014', '2015', '2016', '2017', '2018', '2019', '2020');
- if ($empty) {
- array_unshift_keys($day, '0', '');
- array_unshift_keys($month, '0', '');
- array_unshift_keys($year, '0', '');
- }
- if (in_array('d', $return)) {
- $name_bit = "d" . $name;
- $html = form_select($name_bit, $day, $array, $start[day], '1');
- }
- if (in_array('m', $return)) {
- $name_bit = "m" . $name;
- $html .= form_select($name_bit, $month, $array, $start[month], '');
- }
- if (in_array('y', $return)) {
- $name_bit = "y" . $name;
- $html .= form_select($name_bit, $year, $array, $start[year], '1');
- }
-
- return $html;
- }
- /*****************************************************
- function form_date_year
- -----DESCRIPTION-------------------------------------
- Emit a drop-down form widget containing years
- ranged from 2000 to 2020, optionally with a
- specific year already selected.
- -----ARGUMENTS---------------------------------------
- name : Element name (in-HTML)
- year : Year to pre-select (default is 2003)
- -----RETURNS:----------------------------------------
- The HTML to generate the widget
- *****************************************************/
- function form_date_year($name, $sel_year = '2003') {
- $year = array('2000', '2001', '2002', '2003', '2004', '2005', '2006', '2007', '2008', '2009', '2010', '2011', '2012', '2013', '2014', '2015', '2016', '2017', '2018', '2019', '2020');
- $html = form_select($name, $year, NULL, $sel_year, 1);
- return $html;
- }
- ###################### function form_time() #######################
- function form_time($name, $make_start=NULL, $twelve='') {
- if ($make_start) {
- $make_start = split(':', $make_start);
- $start_hours = $make_start[0];
- $start_mins = $make_start[1];
- $start_secs = $make_start[2];
- }
- if ($twelve) {
- $hours = make_numberarray(1, 12);
- if ($start_hours > 12) {
- $start_hours -= 12;
- }
- } else {
- $hours = make_numberarray(0, 23);
- }
- $minutes = make_numberarray(0, 59);
- $name_bit = "h" . $name;
- $html = form_select($name_bit, $hours, $array, $start_hours, '1');
-
- $html .= " : ";
- $name_bit = "m" . $name;
- $html .= form_select($name_bit, $minutes, $array, $start_mins, '');
- if ($twelve) {
- $name_bit = "twelve" . $name;
- $html .= form_select($name_bit, array('AM' => 'AM', 'PM' => 'PM'));
- }
- return $html;
- }
- ###################### function form_textarea() #######################
- /* - create a text area
- - the $to_array is used if we want post data to be sent as part of an array
- */
- function form_textarea($name, $cols='30', $rows='5', $value='', $to_array='', $override = NULL, $extra='') {
- if (!$override) {
- $value = htmlspecialchars_uni($value);
- }
- if ($cols == "") $cols = 30;
- if ($rows == "") $rows = 5;
- if ($to_array != "") {
- $name = $to_array . "[" . $name . "]";
- }
- $temp = "<textarea name="$name" cols="$cols" rows="$rows" $extra>$value</textarea>n";
- return $temp;
- }
- ###################### function form_hidden() #######################
- function form_hidden($name, $value, $arrayto='', $override = NULL) {
- if ($arrayto) {
- $name = $arrayto . "[" . $name . "]";
- }
- if (!$override) {
- if (is_string($value)) {
- $value = htmlspecialchars_uni($value);
- }
- }
- return "<input type="hidden" name="$name" value="$value">n";
- }
- ###################### function form_button() #######################
- function form_button($value, $js) {
- return "<input type="button" name="$value" value="$value" $js >";
- }
- ################### function htmlspecialchars_uni($data) ##################
- // This function can be safely run on strings that have already been
- // passed through htmlspecialchars_uni(); it leaves existing "&" style
- // entities alone.
- function htmlentities2($data) {
- $translation_table = get_html_translation_table (HTML_ENTITIES,ENT_QUOTES);
- $translation_table[chr(38)] = '&';
- return preg_replace("/&(?![A-Za-z]{0,4}w{2,3};|#[0-9]{2,3};)/","&" , strtr($data, $translation_table));
- }