$captcha = array();
switch($_GET["action"]):
case 'createcaptcha' :
$uniqid = array( uniqid().'0', uniqid().'1', uniqid().'2', uniqid().'3', uniqid().'6', uniqid().'5', uniqid().'7' );
$captcha["Q"] = $uniqid[0];
$captcha["A"] = $uniqid[1];
$randNumber = rand( 0, 2 );
switch ( $randNumber ) {
case 0 : {
$num1 = rand(0,5);
$num2 = rand(0,4);
$num3 = $num1 + $num2;
$text = 'blank '.$num1.' blank plus blank '.$num2.' blank equal blank ';
$captcha["Ans"] = $num3;
$captcha[$uniqid[1]] = "image:::".$text.":::".$uniqid[1].":::"."13";
$captcha[$uniqid[0]] = "text:::"."Resolve this operation.".":::".$uniqid[0].":::"."17";
echo $randNumber.'/+/'.$captcha[$uniqid[0]].'/+/'.$captcha[$uniqid[1]].'/+/'.$captcha["Ans"];
} break;
case 1 : {
$char1 = "candle ";
$char2 = "bubble ";
$char3 = "beetle ";
$text = '';
$num1 = 0;
$num2 = 0;
$num3 = 0;
for($i = 0;$i<9;$i++) {
switch (rand(1,3)) {
case 1 : {
$num1 ++;
$text .= $char1;
} break;
case 2 : {
$num2 ++;
$text .= $char2;
} break;
case 3 : {
$num3 ++;
$text .= $char3;
} break;
}
}
$captcha[$uniqid[1]] = "image:::".$text.":::".$uniqid[1].":::"."0";
if(rand(0,1)) {
$captcha[$uniqid[0]] = "image:::".$char1.":::".$uniqid[0].":::"."0";
$captcha["Ans"] = $num1;
} else if (rand(0,1)) {
$captcha[$uniqid[0]] = "image:::".$char2.":::".$uniqid[0].":::"."0";
$captcha["Ans"] = $num2;
} else {
$captcha[$uniqid[0]] = "image:::".$char3.":::".$uniqid[0].":::"."0";
$captcha["Ans"] = $num3;
}
echo $randNumber.'/+/'.$captcha[$uniqid[0]].'/+/'.$captcha[$uniqid[1]].'/+/'.$captcha["Ans"];
} break;
case 2 : {
$numbers = array( "smile1", "smile2", "smile3", "smile4", "smile5", "smile6" );
shuffle ( $numbers );
array_splice( $numbers, -2, 2, array( "smile0" ) );
shuffle ( $numbers );
$i = 1;
foreach ( $numbers as $num ) {
$text .= $num.' blank ';
if( $num == "smile0" )
$captcha["Ans"] = $i;
$i++;
}
$captcha[$captcha["Q"]] = "image:::"."smile0 ".":::".$captcha["Q"].":::"."0";
$captcha[$captcha["A"]] = "image:::".$text.":::".$captcha["A"].":::"."0";
echo $randNumber.'/+/'.$captcha[$captcha["Q"]].'/+/'.$captcha[$captcha["A"]].'/+/'.$captcha["Ans"];
} break;
}
break;
case 'createimage' :
$arr = explode( ':::', urldecode($_GET["img"]));
if ($arr[0] == 'image')
createimgimg($arr[1],$arr[2],$arr[3]);
else
createtextimg($arr[1],$arr[2],$arr[3]);
break;
case 'resetimage' :
unlink($captcha);
break;
case 'checkAnswer' :
$answer = $_POST["ans"];
$ans1 = $_GET["ans1"];
$ans2 = $_GET["ans2"];
$ans3 = $_GET["ans3"];
if( ( $ans1==$answer && $ans2=='' && $ans3=='undefined' ) || ( $ans1=='' && $ans2==$answer && $ans3=='undefined' ) || ( $ans1=='' && $ans2=='' && $ans3=='ans4'.$answer ) ) {
send_contact($_POST["name"], $_POST["mail"], $_POST["subject"], $_POST["message"]);
echo "Right";
} else {
echo "Wrong";
}
break;
endswitch;
function createimgimg( $img, $filename, $num ){
$allimg = explode ( ' ', $img );
array_pop($allimg) ;
$im = imagecreate(360, 60);
// White background and blue text
$bg = imagecolorallocate($im, 0x88, 0x88, 0x88);
$textcolor = imagecolorallocate($im, 0, 0, 255);
$i = 0;
foreach ( $allimg as $value ) {
if ($value!="blank") {
$src = imagecreatefromjpeg('./captcha_image/'.$value.'.jpg');
imagecopymerge($im, $src, $i, $num, 0, 0, imagesx ($src), imagesy ($src),100);
}
$i += 40;
}
imagejpeg($im,"",100);
imagedestroy($im);
}
function createtextimg($text,$filename, $fontsize ) {
$im = imagecreate(60, 30);
$bg = imagecolorallocate($im, 0x88, 0x88, 0x88);
$textcolor = imagecolorallocate($im, 0xee, 0xee, 0xee);
$font = './trebuc.ttf';
imagettftext($im, $fontsize, 0, 4, 23, $textcolor, $font, $text);
var_dump($im);
exit();
imagejpeg($im,"",100);
imagedestroy($im);
}
function send_contact($sender_name, $sender_email, $subject, $message) {
include_once('config.inc.php');
// Send email only if email is confirmed
if ( EMAIL_CONFIRMED ) {
include_once('class.phpmailer.php');
// Filter Mail Components
$subject = stripslashes(trim($subject));
$content = file_get_contents('./contact_content.tpl', true);
$header_tpl = ''.$sender_name.' '
.'<'.$sender_email.'>';
$header = str_replace('xxx', $header_tpl, CONTENT);
$str_search = array('{sendername}', '{senderemail}', '{sitename}', '{subject}', '{header}', '{message}');
$str_replace = array($sender_name, $sender_email, SITE_URL, $subject, $header, $message);
$subject = str_replace($str_search, $str_replace, SUBJECT);
$message = str_replace($str_search, $str_replace, $content);
// Set Host to connect to
$mail = new PHPMailer();
$mail->isSMTP();
$mail->Host = HOSTNAME_MTA_PUBLISH;
// Sender and Return Address
$mail->setFrom(SENDER_EMAIL, SENDER_NAME);
$mail->addReplyTo($sender_email, $sender_name);
$mail->addAddress(RECEIVER_EMAIL, RECEIVER_NAME.' '.RECEIVER_LASTNAME);
// set intialize of mailer
$mail->CharSet = 'UTF-8';
$mail->Encoding = '7bit';
// set reciever
$mail->Subject = $subject;
$mail->msgHTML($message);
// Actually send the mail
$mail->send();
}
}
?>