";
echo "";
echo "";
echo "";
echo "";
/*
echo "
";
$wpdb->flush();
*/
}
/*
-----------------------------------------------------------------------------
Making random seed
-----------------------------------------------------------------------------
*/
function make_seed()
{
list($usec, $sec) = explode(' ', microtime());
return (float) $sec + ((float) $usec * 100000);
}
function s_get_fontpath()
{
global $_GET;
$s_plugin_path = $_GET["plugin"];
$s_nix_path = str_replace("\\", "/", $s_plugin_path);
$s_path = getcwd();
$s_path = str_replace("\\", "/", $s_path);
$s_pos = strrpos($s_path, "wp-admin");
$s_path = substr($s_path, 0, $s_pos);
$s_path.= "wp-content/plugins/";
$s_pos = strrpos($s_nix_path, "/");
$s_nix_path = substr($s_nix_path, 0, $s_pos);
$s_path.= $s_nix_path;
$s_lastfont="";
if ( $s_dirID = opendir($s_path) )
{
while ( false !== ($s_file = readdir( $s_dirID )) )
{
if ($s_file != "." && $s_file != "..")
{
#echo "$s_file\n";
if ( strtolower(substr($s_file, -4, 4)) == ".ttf" )
$s_lastfont=$s_file;
}
}
closedir($s_dirID);
}
$s_path.="/".$s_lastfont;
/*
$h=fopen("c:/log.txt","w");
fwrite($h,"PATH: ".$s_path);
fclose($h);
*/
return $s_path;
}
/*
-----------------------------------------------------------------------------
This function will install the needed tables if they does not exists.
If the data table exists, it will be empty.
-----------------------------------------------------------------------------
*/
function s_captcha_install()
{
global $table_prefix, $wpdb, $_GET;
$s_table_data = $table_prefix . "s_captcha_data";
$s_table_conf = $table_prefix . "s_captcha_config";
$s_font_path = s_get_fontpath();
if($wpdb->get_var("show tables like '$s_table_data'") != $s_table_data)
{
require_once(ABSPATH . 'wp-admin/upgrade-functions.php');
$sql = 'CREATE TABLE '.$s_table_data.' ('
. ' captcha_id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT, '
. ' captcha_str VARCHAR(32) NOT NULL,'
. ' PRIMARY KEY (captcha_id),'
. ' INDEX (captcha_str)'
. ' )';
dbDelta($sql);
} // if data table not exists
else
{
$delete = 'DELETE FROM '.$s_table_data;
$results = $wpdb->query( $delete );
}
if($wpdb->get_var("show tables like '$s_table_conf'") != $s_table_conf)
{
require_once(ABSPATH . 'wp-admin/upgrade-functions.php');
$sql = 'CREATE TABLE '.$s_table_conf.' ('
. ' cap_installed TINYINT(1) NOT NULL, '
. ' cap_use_grid TINYINT(1), '
. ' cap_use_background TINYINT(1), '
. ' cap_use_random TINYINT(1), '
. ' cap_ttf_path VARCHAR(250), '
. ' cap_ttf_size INT, '
. ' cap_ttf_X INT, '
. ' cap_ttf_Y INT, '
. ' cap_X_size INT, '
. ' cap_Y_size INT, '
. ' cap_rotation INT, '
. ' cap_paper_color_R INT, '
. ' cap_paper_color_G INT, '
. ' cap_paper_color_B INT, '
. ' cap_font_color_R INT, '
. ' cap_font_color_G INT, '
. ' cap_font_color_B INT, '
. ' cap_grid_color_R INT, '
. ' cap_grid_color_G INT, '
. ' cap_grid_color_B INT, '
. ' cap_grid_spacing INT, '
. ' cap_background_path VARCHAR(250), '
. ' PRIMARY KEY (cap_installed) '
. ' )';
dbDelta($sql);
$insert = "insert into ".$s_table_conf." VALUES (0, 1, 0, 1, '".$s_font_path."', 15, 5, 35, 115, 40, 10, 200, 200, 200, 100, 100, 100, 150, 150, 150, 5, '')";
$results = $wpdb->query( $insert );
} // if config table not exists
}
/*
-----------------------------------------------------------------------------
Cleaning data table when the plugin will be deactivated.
-----------------------------------------------------------------------------
*/
function s_captcha_deactivate()
{
global $table_prefix, $wpdb, $_GET;
$s_table_data = $table_prefix . "s_captcha_data";
$delete = 'DELETE FROM '.$s_table_data;
$results = $wpdb->query( $delete );
}
/*
-----------------------------------------------------------------------------
Error message in an image
-----------------------------------------------------------------------------
*/
function s_captcha_errimage($errtitle,$errmsg)
{
$IMG_X = 150;
$IMG_Y = 30;
$image = imagecreate($IMG_X,$IMG_Y);
$paper = imagecolorallocate( $image, 222,0,0 );
$pen = imagecolorallocate( $image, 255, 255, 255 );
imagestring($image, 3, 5, 4, $errtitle, $pen);
imagestring($image, 1, 5, 18, $errmsg, $pen);
$corners[0]=2; $corners[1]=2;
$corners[2]=$IMG_X-3; $corners[3]=2;
$corners[4]=$IMG_X-3; $corners[5]=$IMG_Y-3;
$corners[6]=2; $corners[7]=$IMG_Y-3;
imagepolygon($image,$corners,4,$pen);
header("Content-type: image/jpg");
imagejpeg($image);
}
/*
-----------------------------------------------------------------------------
Creating the image
-----------------------------------------------------------------------------
*/
function s_captcha_showimage()
{
global $table_prefix, $wpdb, $_GET;
$s_table_conf = $table_prefix . "s_captcha_config";
$s_table_data = $table_prefix . "s_captcha_data";
$is_installed = $wpdb->get_var("SELECT cap_installed FROM ".$s_table_conf." limit 1");
if ($is_installed)
{
$select = 'SELECT * FROM '.$s_table_conf.' LIMIT 1';
$row = $wpdb->get_row($select, ARRAY_A, 0);
if (!file_exists($row['cap_ttf_path']))
{
s_captcha_errimage("Font not foud","Set SCaptcha under Options!");
die();
}
if ($row['cap_use_random'])
{
srand(make_seed());
$RandomWord="";
while ( strlen($RandomWord)!=6 )
{
$ChrCode=0;
$IsNumber = mt_rand(0,1);
if ($IsNumber)
$ChrCode = mt_rand(50,57); # All numbers without zero and one
else
$ChrCode = mt_rand(65,90);
$RandomWord .= chr( $ChrCode );
}
if (isset($_GET['demo']))
if ($_GET['demo']=="2")
$RandomWord="WWWWWW";
$SecretWord=md5($RandomWord);
}
$image = imagecreate($row['cap_X_size'],$row['cap_Y_size']);
$paper = imagecolorallocate( $image,
$row['cap_paper_color_R'],
$row['cap_paper_color_G'],
$row['cap_paper_color_B'] );
$font = imagecolorallocate( $image,
$row['cap_font_color_R'],
$row['cap_font_color_G'],
$row['cap_font_color_B'] );
$grid = imagecolorallocate( $image,
$row['cap_grid_color_R'],
$row['cap_grid_color_G'],
$row['cap_grid_color_B'] );
if ($row['cap_use_grid'])
{
for($i=$row['cap_grid_spacing'];$i<$row['cap_Y_size'];$i+=$row['cap_grid_spacing'])
{
imageline ( $image, 0, $i, $row['cap_X_size']-1, $i, $grid );
}
for($i=$row['cap_grid_spacing'];$i<$row['cap_X_size'];$i+=$row['cap_grid_spacing'])
{
imageline ( $image, $i, 0, $i, $row['cap_Y_size']-1, $grid );
}
}
imagettftext($image,
$row['cap_ttf_size'],
$row['cap_rotation'],
$row['cap_ttf_X'],
$row['cap_ttf_Y'],
$font,
$row['cap_ttf_path'],
$RandomWord);
if (!isset($_GET['demo']))
{
$insert = 'insert into '.$s_table_data.' (captcha_str) values (\''.$SecretWord.'\')';
$results = $wpdb->query( $insert );
}
header("Content-type: image/jpg");
imagejpeg($image);
}
else
{
s_captcha_errimage("Not installed","Set SCaptcha under Options!");
}
}
/*
-----------------------------------------------------------------------------
Examinig user's answer
-----------------------------------------------------------------------------
*/
function s_captcha_comment_post($comment_ID)
{
global $table_prefix, $wpdb, $_GET, $_POST;
$s_table_data = $table_prefix . "s_captcha_data";
$EnteredWord = strtoupper(trim($_POST["s_captcha"]));
$EncodedWord = md5($EnteredWord);
$post_ID=-1;
$mytable=$table_prefix."silvers_captcha";
$select = "SELECT captcha_id FROM ".$s_table_data." WHERE captcha_str='".$EncodedWord."' LIMIT 1";
if (($captcha_id = $wpdb->get_var( $select )) == null )
{
// reading comment's post_ID if the comment is approved and showing
$select = "SELECT comment_approved FROM ".$table_prefix."comments WHERE comment_ID=".$comment_ID." LIMIT 1";
$comment_approved = $wpdb->get_var( $select );
$select = "SELECT comment_post_ID FROM ".$table_prefix."comments WHERE comment_ID=".$comment_ID." LIMIT 1";
$post_ID = $wpdb->get_var( $select );
if ( ( $post_ID == null ) || ( $comment_approved == null ) )
{
$wpdb->bail("Error establishing a database connection
".
"Comment ID: ".$comment_ID."
".
"Comment approved: ".$comment_approved."
".
"Silver's captcha plugin can not ".
"read the approval of the comment.
");
die();
}
else
{
// updating post's count
$select = "SELECT comment_count from ".$table_prefix."posts WHERE ID=".$post_ID;
if (($comment_count = $wpdb->get_var( $select )) == null )
{
$wpdb->bail("Error establishing a database connection
".
"Silver's captcha plugin can not ".
"update posts table. (Stage 1)
");
die();
}
else
{
$comment_count--;
$update="UPDATE ".$table_prefix."posts SET comment_count=".$comment_count." WHERE ID=".$post_ID." LIMIT 1";
$result = $wpdb->query( $update );
if (!$result)
{
$wpdb->bail("Error establishing a database connection
".
"Silver's captcha plugin can not ".
"update posts table. (Stage 2)
");
die();
}
}
}
// deleting comment
$delete="DELETE FROM ".$table_prefix."comments WHERE comment_ID=".$comment_ID;
$result = $wpdb->query( $delete );
if (!$result)
{
$wpdb->bail("Error establishing a database connection
".
"Silver's captcha plugin can not ".
"delete from the database. (Stage 1)
");
die();
}
}
else
{
$delete="DELETE FROM ".$s_table_data." where captcha_id=".$captcha_id;
$result = $wpdb->query( $delete );
if (!$result)
{
$wpdb->bail("Error establishing a database connection
".
"Silver's captcha plugin can not ".
"delete from the database. (Stage 2)
");
die();
}
}
}
/*
=============================================================================
Hanging our ACTIONS into hooks of WordPress
=============================================================================
*/
if (function_exists("add_action"))
{
// if this plugin was called from the WordPress
add_action('activate_'.$_GET["plugin"],'s_captcha_install');
add_action('deactivate_'.$_GET["plugin"],'s_captcha_deactivate');
add_action('admin_menu', 's_captcha_adminmenu');
add_action('comment_post', 's_captcha_comment_post');
}
else
{
// if this plugin was called as an image resource
require_once("../../../wp-config.php");
require_once("../../../wp-includes/wp-db.php");
$wpdb=new wpdb(DB_USER, DB_PASSWORD, DB_NAME, DB_HOST);
s_captcha_showimage();
}
?>
XML-RPC server accepts POST requests only.