<?php
class form_validation
{
var $error = array();
var $dropdown_select = 'Select';
var $allow_html = false;
function select_post_fix($values, $lables, $selected)
{
if($selected == '')
{
$disp = "<option value=\"\" selected>".trim($this->dropdown_select)."</option>\n";
}
if(!empty($values))
{
$val = explode(",", $values);
$val_ok = 1;
}
$lab = explode(",", $lables);
//
for($x = 0; $x<=count($lab); $x++)
{
$value = $lab[$x];
if($val_ok = 1)
{
$value = $val[$x];
}
if(!empty($lab[$x]))
{
if($value == $selected)
{
$disp .= "<option value=\"".trim($value)."\" selected>".trim($lab[$x])."</option>\n";
}
else
{
$disp .= "<option value=\"".trim($value)."\">".trim($lab[$x])."</option>\n";
}
unset($value);
}
}
return $disp;
}
//
function unset_post()
{
for($x = 0; $x<=count($_POST); $x++)
{
unset($_POST[$x]);
}
}
function clean_fields($field)
{
if(is_array($field))
{
foreach($field as $key => $value)
{
$value = addslashes($value);
$value = trim($value);
$value = str_replace(",", "&#8218;", $value);
if($this->allow_html == false)
{
$value = strip_tags($value);
}
$return[$key] = $value;
}
return $return;
}
else
{
$field = addslashes($field);
$field = trim($field);
$field = str_replace(",", "&#8218;", $field);
if($this->allow_html == false)
{
$field = strip_tags($field);
}
return $field;
}
}
function check($field, $lbl)
{
if(trim($field) == "")
{
$this->error[] = "Please check <span class=\"fielderror\">".ucwords($lbl)."</span> and try again.";
}
}
function show_error()
{
$array = $this->error;
$str = '<div class="error_message">';
foreach($array as $key => $msg)
{
if(!empty($msg))
$str .= '<div>'.$msg.'</div>';
}
$str .= '</div>';
return $str;
}
function checkbox($group, $values, $lables, $selected)
{
$values = explode(",", $values);
$lables =  explode(",", $lables);
$selected = explode(",", $selected);
$str = '<table border="0" cellspacing="0" cellpadding="0">';
for($x = 0; $x < sizeof($values); $x++)
{
if(!empty($values[$x]))
{
if(in_array($values[$x], $selected))
{
$str .= "<tr><td><input type=\"checkbox\" name=\"$group\" value=\"$values[$x]\" checked /></td><td class=\"".$this->classname."\">$lables[$x]</td></tr>\n";
}
else
{
$str .= "<tr><td><input type=\"checkbox\" name=\"$group\" value=\"$values[$x]\" /></td><td class=\"".$this->classname."\" align=\"left\">$lables[$x]</td></tr>\n";
}
}
}
$str .= "</table>";
return $str;
}
function radio($group, $values, $lables, $selected)
{
$values = explode(",", $values);
$lables =  explode(",", $lables);
$selected = explode(",", $selected);
for($x = 0; $x < sizeof($values); $x++)
{
if(!empty($values[$x]))
{
if(in_array($values[$x], $selected))
{
$str .= "<input type=\"radio\" name=\"$group\" value=\"$values[$x]\" checked /> $lables[$x]<br />\n";
}
else
{
$str .= "<input type=\"radio\" name=\"$group\" value=\"$values[$x]\" /> $lables[$x]<br />\n";
}
}
}
return $str;
}
//
function cache_form()
{
$this->kill_form();
foreach($_POST as $key => $value)
{
$_SESSION['form'][$key] = $value;
}
}
//
function get_form()
{
if(is_array($_SESSION['form']))
{
foreach($_SESSION['form'] as $key => $value)
{
$array[$key] = $value;
}
}
return $array;
}
//
function kill_form()
{
$_SESSION['form'] = "";
}
//
function recaptchaHeaderCode()
{
return '<script src="//www.google.com/recaptcha/api.js"></script>';
}
//
function recaptchaShowCaptcha()
{
return '<div class="g-recaptcha" data-sitekey="6LdLaicTAAAAAOB1vQ3f_HM5waY3h3kMzcFcpLKN" class="required"></div><div style="display:none;" class="validation-advice" id="captchaErr">Please tick/answer the captcha above.</div>';
}
//
function recaptchaConfirm($str)
{
$url = 'https://www.google.com/recaptcha/api/siteverify';
$fields = array(
'secret' => '6LdLaicTAAAAAEdlfdH9t3NpcVKm5QzFlym_141J',
'response' => $str
);
$fields_string = '';
foreach($fields as $key=>$value){$fields_string .= $key.'='.$value.'&';}
rtrim($fields_string, '&');
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_POST, count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
curl_setopt($ch,CURLOPT_RETURNTRANSFER, true);
$json = curl_exec($ch);
curl_close($ch);
$json = json_decode($json);
return ($json->success) ? true : false;
}
//
}
$form = new form_validation();
?>