Wednesday 14 February 2007

PHP: Form with smarty + captcha

Form composition with smarty + captcha


/*
Usage:
$ff=array(
array('text', 'field_name', 'label', array('class'=>'area'), array('postlabel'=>'ddd', 'validate'=>'empty')),
array('separator', 'td_class', 'text here'),
array('options', 'field_name2', 'label', array('options'=>array(1=>2)), array('postlabel'=>'ddd', 'check'=>'empty')),
array('submit', '', '', array('value'=>'Sign up')),
array('hidden', 'hid', '', array('value'=>'blah'))
);
sf_init($ff);

$smarty->assign(array('f2'=>$ff, 'in'=>$_REQUEST));
$sm->register_function('smarty_form', 'smarty_form');
{smarty_form fields=$f2 in=$in table_attr="class=tabt cellpadding=0 cellspacing=0" form_attr="method=post" colspan_class="separator"}

*/

function sf_init(&$ff) {
$ft=array('type', 'name', 'label', 'fp', 'gp'); // make readable array
foreach ($ff as $fd) {
$fdh=array(); foreach ($ft as $i=>$t) {if (isset($fd[$i])) $fdh[$t]=$fd[$i];}
$f2[]=$fdh;
}
$ff=$f2;
}
function iname($f2, $fd) {
foreach ($f2 as $i=>$f) {if ($f['name']==$fd) $fi=$i;}
return (isset($fi) ? $fi : -1);
}
function vfields($f2) {
$fr=array();
foreach ($f2 as $f) {
if (!in_array($f['type'], array('separator', 'submit')) && $f['name']!='captcha') $fr[]=$f['name'];
}
return $fr;
}

function smarty_form($p, &$smarty) {
$hidden=array();
foreach ($p['fields'] as $i=>$_fd) {$fd=&$p['fields'][$i];
// start values
$fd['fp']['name']=$fd['name']; // for form tags
$fd['fp']['type']=$fd['type'];
if (isset($p['in'][$fd['name']])) { // in-value
$par=(in_array($fd['type'], array('options', 'radios', 'checkboxes')) ? 'selected' : 'value');
$fd['fp'][$par]=$p['in'][$fd['name']];
}
// generate fields
if (in_array($fd['type'], array('options', 'radios', 'checkboxes'))) {
require_once(SMARTY_DIR."plugins/function.html_$fd[type].php");
if (isset($p['in'][$fd['name']])) $fd['fp']['selected']=$p['in'][$fd['name']];
$fields_t[$fd['name']]=call_user_func("smarty_function_html_$fd[type]", $fd['fp'], $smarty);
} elseif (!in_array($fd['type'], array('separator', 'hidden'))) {
$fields_t[$fd['name']]=_field_gen($fd['fp'], $fd['type']);
}
// data for table, hidden fields
if ($fd['type']=='hidden') $hidden[]=_field_gen($fd['fp'], $fd['type']);
elseif ($fd['type']=='separator') $data[][]=$fd['label'];
else $data[]=array(
$fd['label'].(isset($fd['gp']['check']) ? ' *' : ''),
$fields_t[$fd['name']].(isset($fd['gp']['plabel']) ? $fd['gp']['plabel'] : '')
);
}

$js=_jstext($p['fields']); // javascript-check
return $js.'<form '.$p['form_attr'].($js ? ' onsubmit="return validate_form(this)"' : '').'>'.
"\n".implode("\n", $hidden)."\n"._tablefm($data, &$p).'</form>';
}
function _tablefm($data, $p) {
$res[]='<table '.$p['table_attr'].'>';
$cols=0; foreach ($data as $line) {$c=count($line); if ($c>$cols) $cols=$c;}
foreach ($data as $line) {
$res[]='<tr>';
if (count($line)==1) $res[]="<td class=\"$p[colspan_class]\" colspan=$cols>$line[0]</td>";
else {foreach ($line as $y=>$v) $res[]="<td>$v</td>";}
$res[]='</tr>';
}
$res[]='</table>';
return implode("\n", $res);
}
function _jstext($fields_h) {
foreach ($fields_h as $fd) {
if (!isset($fd['gp']['check'])) continue;
$rs['name'][]=$fd['name'];
$rs['label'][]=$fd['label'];
$rs['check'][]=$fd['gp']['check'];
}
if (!$rs) return '';
return '
<script>
v_names = new Array("'.implode('", "', $rs['name']).'");
v_checks = new Array("'.implode('", "', $rs['check']).'");
v_labels = new Array("'.implode('", "', $rs['label']).'");

function validate_form(obj) {
errors="";
for (i=0; i<obj.elements.length; i++) {
for (y=0; y<v_names.length; y++) {
if (obj.elements[i].name==v_names[y]) errors+=_validate(v_checks[y], obj.elements[i], v_labels[y]);
}
}
if (errors) {alert(errors); return false;}
else return true;
}
function _validate(type, obj, label) {
res="";
if (type=="email") {
if (obj.value.indexOf(".")>2 && obj.value.indexOf("@") > 0) res="";
else res="E-mail address is not valid\n";
} else if (type=="empty") {
if (obj.value=="" || obj.value==null) res="Field \""+label+"\" is empty\n";
}
return res;
}
</script>
';
}
function _field_gen($params, $type='input') {
if ($type=='textarea') {
foreach ($params as $k=>$v) {if ($k!='value') $pp="$k=\"$v\"";}
return '<textarea '.implode(' ', $pp).'>'.(isset($params['value']) ? $params['value'] : '').'</textarea>';
} else {
foreach ($params as $k=>$v) {$pp[]="$k=\"$v\"";}
return '<input '.implode(' ', $pp).'>';
}
}


/*
Usage:
image.php:
$captcha=array(140, 34, 'britanic.ttf', 25, 0, 0, 0, array(255,255,255), array(0,0,50));
captcha();
form.php:
<form method="POST"><img src="img.php"><input type="text" name="captcha"><input type="submit"></form>
if ($_POST && check_captcha()) echo "ok";

*/

function captcha() {
global $captcha;
list($box_w, $box_h, $font, $font_size, $font_angle, $margin_left, $margin_top, $color_bg, $color_text)=$captcha;
$pass=''; $chm=rand(0,1);
$ch=array(str_split('aeiouy'), str_split('bcdfjhgkmnprstvxz'));
while (strlen($pass)<6) {$pass.=$ch[$chm][array_rand($ch[$chm])]; $chm=($chm ? 0:1);}
session_start();
$_SESSION['captcha'] = $pass;
$image = ImageCreatetruecolor($box_w,$box_h); // create the image resource
$color_bg = ImageColorAllocate($image, $color_bg[0], $color_bg[1], $color_bg[2]); // set colors
$color_text = ImageColorAllocate($image, $color_text[0], $color_text[1], $color_text[2]);
imagefill($image, 0, 0, $color_bg); // set background
imagettftext($image, $font_size, $font_angle, $margin_left, $font_size + $margin_top, $color_text, $font, $pass); // set text
header("Content-Type: image/jpg"); // or image/jpg
imagejpeg($image); // created image
imagedestroy($image);
}
function check_captcha() {
session_start();
if (isset($_SESSION['captcha']) && isset($_REQUEST['captcha']) && $_SESSION['captcha']==$_REQUEST['captcha']) {
unset($_SESSION['captcha']);
return 1;
}
return 0;
}