<?php
//====================================================================//
//                                                                    //
//                   Form Processor                                   //
//                               By dark2k1(dark2k1.com)              //
//                                                                    //
// Adding Fields:                                                     //
// $form->add_text_field(Label, Max Chars, Value, Required)           //
// $form->add_textarea(Label, Max Chars, Rows, Cols, Value, Required) //
//                                                                    //
//====================================================================//

extract($_GET);
extract($_POST);

class form {
	function set_option($option, $value) {
	$this->options[$option] = $value;
	}

	function add_text_field($label, $max_length, $value, $required) {
	$count = count($this->fields);

	$this->fields[$count]['label'] = $label;
	$this->fields[$count]['type'] = 'text';
	$this->fields[$count]['max_length'] = $max_length;
	$this->fields[$count]['value'] = $value;
	$this->fields[$count]['required'] = $required;
	}

	function add_textarea($label, $max_length, $rows, $cols, $value, $required) {
	$count = count($this->fields);

	$this->fields[]['label'] = $label;
	$this->fields[$count]['type'] = 'textarea';
	$this->fields[$count]['max_length'] = $max_length;
	$this->fields[$count]['rows'] = $rows;
	$this->fields[$count]['cols'] = $cols;
	$this->fields[$count]['value'] = $value;
	$this->fields[$count]['required'] = $required;
	}

	function generate($title) {
	global $func, $PHP_SELF;

		if($func != 'process') {
		$form = "
		<center><h3>$title</h3></center>

		<form action='$PHP_SELF' method='post'>
		<center>
		<table width='80%'>
		<tr><td colspan='2'>* = required field</td></tr>";

			for($x = 0; $x < count($this->fields); $x++) {
				if($this->fields[$x]['required']) {
				$required = '*';
				} else {
				$required = '';
				}

				if($this->fields[$x]['type'] == 'text') {
					if($this->fields[$x]['max_length'] > 0) {
					$max_length = " maxlength='" . $this->fields[$x]['max_length'] . "'";
					}

				$form .= "<tr><td>" . $this->fields[$x]['label'] . "$required</td><td><input type='text' name='fields[$x]' value='" . $this->fields[$x]['value'] . "' $max_length></td></tr>";
				} elseif($this->fields[$x]['type'] == 'textarea') {
					if($this->fields[$x]['max_length'] > 0) {
					$max_length = " maxlength='" . $this->fields[$x]['max_length'] . "'";
					}

				$form .= "<tr><td>" . $this->fields[$x]['label'] . "$required</td><td><textarea rows='" . $this->fields[$x]['rows'] . "' cols='" . $this->fields[$x]['columns'] . "' name='fields[$x]' $max_length>" . $this->fields[$x]['value'] . "</textarea></td></tr>";
				}
			}

		$form .= "
		<tr><td colspan='2' align='center'><input type='hidden' name='func' value='process'><input type='submit'></td></tr>
		</table>
		</center>
		</form>";

		echo $form;
		return $form;
		} else {
		$this->process();
		}
	}

	function process() {
	global $fields, $REMOTE_ADDR, $HTTP_REFERER;

	$data = "
Date/Time: " . date('Y-m-d H:i:s') . "
IP: $REMOTE_ADDR
Referer Address: $HTTP_REFERER
";

		for($x = 0; $x < count($this->fields); $x++) {
			if($this->fields[$x]['required'] && empty($fields[$x])) {
			echo "<center><b>Please go back and fill out all required fields</b></center>";

			$data = '';

			break;
			} elseif($this->fields[$x]['max_length'] < strlen($fields[$x])) {
			echo "<center><b>The max length for the " . $this->fields[$x]['label'] . " field is " . $this->fields[$x]['max_length'] . " characters.</b></center>";

			$data = '';

			break;
			} else {
				if($this->options['save_mode'] == 'text') {
				$data .= $this->fields[$x]['label'] . ':' . $fields[$x] . "
";
				} elseif($this->options['save_mode'] == 'html') {
				$data .= "<font color='#660000'>" . $this->fields[$x]['label'] . "</font>:<font color='#006600'>" . $fields[$x] . "</font><br>";
				} 
			}
		}

		if(!empty($data)) {
			if(!$open = fopen($this->options['save_to'], 'a+')) {
			echo 'Failed opening file<br>';
			}

			if(!fwrite($open, $data)) {
			echo 'Failed writing to file<br>';
			}

			if(!fclose($open)) {
			echo 'Failed closing file<br>';
			}

		echo "<center><b>Thanks for your submission</b></center>";
		}			
	}
}

$form = new form();

$form->set_option('save_to', 'text.html');
$form->set_option('save_mode', 'text');

$form->add_text_field('RS Name', '50', '', 1);
$form->add_text_field('Combat Level', '10', '', 1);
$form->add_text_field('Email', '150', '', 1);
$form->add_text_field('AIM', '50', '', 0);

$form->generate('Join');

?>
Name