<?php
function papa_captcha_show()
{
global $db, $config;
$allnum = $config['ppcap_imnum'];
$realnum = $config['ppcap_imrealnum'];
$unrealnum = $allnum - $realnum;
$db->query("SELECT * FROM " . PREFIX . "_ppcaptcha WHERE `real`='yes' ORDER BY rand() LIMIT ".$realnum);
while($tmp = $db->get_row())
{
$images[] = $tmp;
}
$db->query("SELECT * FROM " . PREFIX . "_ppcaptcha WHERE `real`='no' ORDER BY rand() LIMIT ".$unrealnum);
while($tmp = $db->get_row())
{
$images[] = $tmp;
}
shuffle($images);
// Создаем уникальный javascript для вывода каптчи
$nnnnnn = "\n";
$css .= "<style>
.papacaptcha a img
{
border:solid;
border-color:#999;
border-width:4px;
}
.papacaptcha a img.selected
{
border:solid;
border-color:#F00;
border-width:4px;
}
</style>
<script>
function cmnSet_class( eOn, sClass_name, sInstead ){
if( eOn ){
sClass_name = ( sClass_name.length ) ? sClass_name.replace( /(^\s+|\s+$)/, \"\" ) : \"\";
if( eOn.className.length ){
var sOld = sClass_name;
if( sInstead && sInstead.length ){
sInstead = sInstead.replace( /\s+(\S)/g, \"|$1\" );
if( sOld ){
sOld += \"|\";
}
sOld += sInstead;
}
eOn.className = eOn.className.replace( new RegExp(\"(^|\\s+)(\" + sOld +\")($|\\s+)\", \"g\"), \"$1\" );
}
eOn.className += ( eOn.className.length && sClass_name ? \" \" : \"\" ) + sClass_name;
}
}
</script>
";
$code .= "<script>".$nnnnnn;
$code .= "var images = new Array();".$nnnnnn;
$code .= "var imagessel = new Array();var ppcc;".$nnnnnn;
$i = 0;
while($i<$allnum)
{
$id = $images[$i][id];
$code .= "images[{$i}]='{$id}';".$nnnnnn;
$code .= "imagessel[{$i}]='';".$nnnnnn;
$ppcfun .= "if(imagessel[{$i}]==1) ppcc = ppcc + images[{$i}] + ',';".$nnnnnn;
$i = $i+1;
}
$code .= "function select_image(oImage){".$nnnnnn;
$code .= "if(imagessel[oImage]!=1){".$nnnnnn;
$code .= "imagessel[oImage] = 1;".$nnnnnn;
$code .= "cmnSet_class(document.getElementById('ppc_'+oImage), 'selected','');}".$nnnnnn;
$code .= "else{imagessel[oImage] = 0;cmnSet_class(document.getElementById('ppc_'+oImage), '','selected');}genppccode();}".$nnnnnn;
$code .= "function genppccode(){ppcc='';".$ppcfun."document.getElementById('ppc_field').value = ppcc;}".$nnnnnn;
$code .= "</script>";
$htmlcode .= "<div class='papacaptcha'>";
$htmlcode .= "Пожалуйста, ".$config['ppcap_q']."<br>";
foreach($images as $key=>$value)
{
$htmlcode .= "<a onclick='select_image({$key})'><img src='/uploads/{$value[image]}' height=100 id='ppc_{$key}'></a>";
}
$htmlcode .= "<input type=hidden id=ppc_field name=ppc_field value=''></div>";
//javascript готово, я его так не люблю
return($css.$code.$htmlcode);
}
function papa_captcha_check($code)
{
global $db, $config;
$code = explode(",",$code);
$d = count($code);
unset($code[($d-1)]);
$d = $d - 1;
if($d<>$config['ppcap_imrealnum'])
return false;
$code = implode(", ", $code);
$lal = $db->super_query("SELECT count(*) FROM " . PREFIX . "_ppcaptcha WHERE (`real`='yes' AND id IN ({$code}))");
$lal = $lal['count(*)'];
if($lal<>$config['ppcap_imrealnum'])
return false;
else
return true;
}
?>