From bfc4775e06354f1fd8a97ff28ebb00d640b85988 Mon Sep 17 00:00:00 2001 From: donncha Date: Thu, 14 Jul 2005 13:42:18 +0000 Subject: first stab at using WP-hashcash to protect the reg page. git-svn-id: http://svn.automattic.com/wordpress-mu/trunk@24 7be80a69-a1ef-0310-a953-fb0f7c49ff36 --- wp-inst/wp-newblog.inc.php | 654 +++++++++++++++++++++++++++++++++++++++++++++ wp-inst/wp-newblog.php | 7 +- 2 files changed, 659 insertions(+), 2 deletions(-) create mode 100644 wp-inst/wp-newblog.inc.php diff --git a/wp-inst/wp-newblog.inc.php b/wp-inst/wp-newblog.inc.php new file mode 100644 index 0000000..8f91bd2 --- /dev/null +++ b/wp-inst/wp-newblog.inc.php @@ -0,0 +1,654 @@ +>5]|=0x80<<((len)%32);x[(((len+64)>>>9)<<4)+14]=len;var a=1732584193;var b=-271733879;var c=-1732584194;var d=271733878;for(var i=0;i>16)+(y>>16)+(lsw>>16);return(msw<<16)|(lsw&0xFFFF);}' . $s; + $bits [] = $p . 'function ' . $names[16] . '(num,cnt){return(num<>>(32-cnt));}' . $s; + $bits [] = $p . 'function ' . $names[7] . '(str){var bin=Array();var mask=(1<<8)-1;for(var i=0;i>5]|=(str.charCodeAt(i/8)&mask)<<(i%32);return bin;}' . $s; + $bits [] = $p . 'function ' . $names[5] . '(' . $names[15] . '){var ' . $names[14] . '="0123456789abcdef";var str="";for(var i=0;i<' . $names[15] . '.length*4;i++){str+=' . $names[14] . '.charAt((' . $names[15] . '[i>>2]>>((i%4)*8+4))&0xF)+' . $names[14] . '.charAt((' . $names[15] . '[i>>2]>>((i%4)*8))&0xF);}return str;}' . $s; + + return $bits; +} + +/** + * Takes: <> + * Returns: the hashcash special code, based on the session or ip + */ +function hashcash_special_code(){ + if(HASHCASH_PER_USER) { + $key = strip_tags(session_id()); + + if(!$key){ + $key = $_SERVER['REMOTE_ADDR']; + } + + return md5($key . ABSPATH . $_SERVER['HTTP_USER_AGENT'] . date("F j, Y, g a")); + } else { + if(get_bloginfo('version') < 1.5){ + return md5(ABSPATH . get_bloginfo('version') . HASHCASH_PER_USER_RAND); + } else { + return md5(ABSPATH . get_bloginfo('version') . get_option('wp_hashcash_rand')); + } + } +} + +/** + * Takes: <> + * Returns: the hashcash special field value + */ +function hashcash_field_value(){ + global $posts; + return $posts[0]->ID * strlen(ABSPATH); +} + +/** + * Takes: String name of function + * Returns: Javascript to compute field value + */ +function hashcash_field_value_js($val_name){ + $js = 'function ' . $val_name . '(){'; + + $type = rand(0, 5); + switch($type){ + /* Addition of n times of field value / n, + modulus */ + case 0: + $eax = hashcash_random_string(rand(8,10)); + $val = hashcash_field_value(); + $inc = rand(1, $val - 1); + $n = floor($val / $inc); + $r = $val % $inc; + + $js .= "var $eax = $inc; "; + for($i = 0; $i < $n - 1; $i++){ + $js .= "$eax += $inc; "; + } + + $js .= "$eax += $r; "; + $js .= "return $eax; "; + + break; + + /* Conversion from binary */ + case 1: + $eax = hashcash_random_string(rand(8,10)); + $ebx = hashcash_random_string(rand(8,10)); + $ecx = hashcash_random_string(rand(8,10)); + $val = hashcash_field_value(); + $binval = strrev(base_convert($val, 10, 2)); + + $js .= "var $eax = \"$binval\"; "; + $js .= "var $ebx = 0; "; + $js .= "var $ecx = 0; "; + $js .= "while($ecx < $eax.length){ "; + $js .= "if($eax.charAt($ecx) == \"1\") { "; + $js .= "$ebx += Math.pow(2, $ecx); "; + $js .= "} "; + $js .= "$ecx++; "; + $js .= "} "; + $js .= "return $ebx; "; + + break; + + /* Multiplication of square roots */ + case 2: + $val = hashcash_field_value(); + $sqrt = floor(sqrt($val)); + $r = $val - ($sqrt * $sqrt); + $js .= "return $sqrt * $sqrt + $r; "; + break; + + /* Closest sum up to n */ + case 3: + $val = hashcash_field_value(); + $n = floor((sqrt(8*$val+1)-1)/2); + $sum = $n * ($n + 1) / 2; + $r = $val - $sum; + $eax = hashcash_random_string(rand(8,10)); + + $js .= "var $eax = $r; "; + for($i = 0; $i <= $n; $i++){ + $js .= "$eax += $i; "; + } + $js .= "return $eax; "; + break; + + /* Closest sum up to n #2 */ + case 4: + $val = hashcash_field_value(); + $n = floor((sqrt(8*$val+1)-1)/2); + $sum = $n * ($n + 1) / 2; + $r = $val - $sum; + + $js .= "return $r "; + for($i = 0; $i <= $n; $i++){ + $js .= "+ $i "; + } + $js .= ";"; + break; + + /* Closest sum up to n #3 */ + case 5: + $val = hashcash_field_value(); + $n = floor((sqrt(8*$val+1)-1)/2); + $sum = $n * ($n + 1) / 2; + $r = $val - $sum; + $eax = hashcash_random_string(rand(8,10)); + + $js .= "var $eax = $r; var i; "; + $js .= "for(i = 0; i <= $n; i++){ "; + $js .= "$eax += i; "; + $js .= "} "; + $js .= "return $eax; "; + break; + } + + $js .= "} "; + return $js; +} + +/** + * Takes: An array matching the form + * Returns: The form code, with input elements disabled + */ +function hashcash_disable_callback($matches){ + $text = $matches[0]; + return preg_replace('/]*?id="(submit|author|email|url)")/si', '', '

Protected by WP-Hashcash.

' . "\n" . '', $text); +} + +/** + * Takes: An array matching the form + * Returns: The form code, with a noscript attribution + */ +function hashcash_script_callback($matches){ + $text = $matches[0]; + return str_replace('

WP-Hashcash by Elliott Back protects you from spam. Please enable javascript and reload this page to add your comment.

' . "\n" . '