/* ################################################## # # Filename..........: $RCSfile: Form.class,v $ # Original Author...: Anthony L. Awtrey # Version...........: $Revision: 0.1 $ # Last Modified By..: $Author: aawtrey $ # Last Modified.....: $Date: 2006/09/21 18:15:56 $ # # Copyright 2006 Anthony Awtrey # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA # */ /* * This class provides methods to build HTML forms. */ class Form { var $action; var $method; var $hidden = array(); var $input = array(); var $submit = array(); var $reset; /* * Class initialization */ function Form($action='index.php', $method='GET') { global $_SERVER; global $_SESSION; global $_REQUEST; global $_POST; global $_GET; $this->action = $action; $this->method = $method; } /* * Add a hidden form input */ function hidden($name,$value) { $c = count($this->hidden); $this->hidden[$c]['name'] = $name; $this->hidden[$c]['value'] = $value; } /* * Add a text form input */ function text($label,$name,$value,$size='30',$maxlength='') { $c = count($this->input); $this->input[$c]['type'] = 'text'; $this->input[$c]['label'] = $label; $this->input[$c]['name'] = $name; $this->input[$c]['value'] = $value; $this->input[$c]['size'] = $size; if ( $maxlength == '' ) { $this->input[$c]['maxlength'] = $size; } else { $this->input[$c]['maxlength'] = $maxlength; } } /* * Add a password form input */ function password($label,$name,$value,$size='30',$maxlength='') { $c = count($this->input); $this->input[$c]['type'] = 'password'; $this->input[$c]['label'] = $label; $this->input[$c]['name'] = $name; $this->input[$c]['value'] = $value; $this->input[$c]['size'] = $size; if ( $maxlength == '' ) { $this->input[$c]['maxlength'] = $size; } else { $this->input[$c]['maxlength'] = $maxlength; } } /* * Add a file form input */ function file($label,$name,$value,$size='30',$maxlength='') { $c = count($this->input); $this->input[$c]['type'] = 'file'; $this->input[$c]['label'] = $label; $this->input[$c]['name'] = $name; $this->input[$c]['value'] = $value; $this->input[$c]['size'] = $size; if ( $maxlength == '' ) { $this->input[$c]['maxlength'] = $size; } else { $this->input[$c]['maxlength'] = $maxlength; } } /* * Add a textarea form input */ function textarea($label,$name,$value,$rows='5',$cols='60') { $c = count($this->input); $this->input[$c]['type'] = 'textarea'; $this->input[$c]['label'] = $label; $this->input[$c]['name'] = $name; $this->input[$c]['value'] = $value; $this->input[$c]['rows'] = $rows; $this->input[$c]['cols'] = $cols; } /* * Add a select form input * $options must be a key/value array */ function select($label,$name,$options,$selected='',$size='1') { $c = count($this->input); $this->input[$c]['type'] = 'select'; $this->input[$c]['label'] = $label; $this->input[$c]['name'] = $name; $this->input[$c]['options'] = $options; $this->input[$c]['selected'] = $selected; $this->input[$c]['size'] = $size; } /* * Add a captcha form input. This is an input test used to prevent spam. */ function captcha() { $c = count($this->input); $this->input[$c]['type'] = 'captcha'; } /* * Add a submit button */ function submit($name,$value) { $c = count($this->submit); $this->submit[$c]['name'] = $name; $this->submit[$c]['value'] = $value; } /* * Add a reset button */ function reset($name) { $this->reset = $name; } /* * Generate a captcha image for the form and set it to the session */ function generateCaptcha() { $image = imagecreate(100, 30); $white = imagecolorallocate($image, 0xFF, 0xFF, 0xFF); $gray = imagecolorallocate($image, 0xC0, 0xC0, 0xC0); $darkgray = imagecolorallocate($image, 0x50, 0x50, 0x50); srand((double)microtime()*1000000); for ($i = 0; $i < 30; $i++) { $x1 = rand(0,100); $y1 = rand(0,30); $x2 = rand(0,100); $y2 = rand(0,30); imageline($image, $x1, $y1, $x2, $y2 , $gray); } for ($i = 0; $i < 5; $i++) { $cnum[$i] = rand(0,9); } for ($i = 0; $i < 5; $i++) { $fnt = rand(3,5); $x = $x + rand(12 , 20); $y = rand(7 , 12); imagestring($image, $fnt, $x, $y, $cnum[$i] , $darkgray); } $digit = "$cnum[0]$cnum[1]$cnum[2]$cnum[3]$cnum[4]"; $_SESSION['inCaptcha'] = $digit; header('Content-type: image/png'); imagepng($image); imagedestroy($image); exit; } /* * Render the defined form into HTML */ function render() { $output = "
\n"; return $output; } } ?>