From e368ae6dd0805753ff74c93b29307dc9d8c6712c Mon Sep 17 00:00:00 2001 From: rurban Date: Sun, 30 Oct 2005 14:20:42 +0000 Subject: [PATCH] move Captcha specific vars and methods into a Captcha object randomize Captcha chars positions and angles (smoothly) git-svn-id: svn://svn.code.sf.net/p/phpwiki/code/trunk@4944 96ab9672-09ca-45d6-a79d-3d69d39ca109 --- lib/Captcha.php | 230 ++++++++++++++++++++++++++++------------------- lib/editpage.php | 96 +++++++++----------- lib/main.php | 8 +- 3 files changed, 186 insertions(+), 148 deletions(-) diff --git a/lib/Captcha.php b/lib/Captcha.php index 90d62f643..911d11ff4 100644 --- a/lib/Captcha.php +++ b/lib/Captcha.php @@ -1,10 +1,11 @@ + by Gavin M. Roy Modified by Benjamin Drieu - 2005 for PhpWiki get_captcha_random_word() contributed by Dan Frankowski 2005 for PhpWiki + objectified and randomized 2005 by Reini Urban This File is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -21,105 +22,154 @@ rcs_id('$Id: Captcha.php,v 1.3 2005-10-29 07:37:56 rurban Exp $'); Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ +class Captcha { -function get_captcha_word () { - if (USE_CAPTCHA_RANDOM_WORD) - return get_captcha_dictionary_word(); - else - return get_captcha_random_word(); -} + function Captcha($meta = array(), $width = 250, $height = 80) { + $this->meta =& $meta; + $this->width = $width; + $this->height = $height; + $this->length = 8; + $this->failed_msg = _("Typed in verification word mismatch ... are you a bot?"); + $this->request =& $GLOBALS['request']; + } -function get_captcha_dictionary_word () { - // Load In the Word List - $fp = fopen(FindFile("lib/captcha/dictionary"), "r"); - while ( !feof($fp) ) - $text[] = Trim(fgets($fp, 1024)); - fclose($fp); - - // Pick a Word - $word = ""; - while ( strlen(Trim($word)) == 0 ) { - $x = rand(0, Count($text)); - return $text[$x]; + function captchaword() { + if ( ! $this->request->getSessionVar('captchaword')) { + $this->request->setSessionVar('captchaword', $this->get_word()); + } + return $this->request->getSessionVar('captchaword'); } -} -/* by Dan Frankowski. - */ -function get_captcha_random_word () { - // Pick a few random letters or numbers - $word = ""; - // Don't use 1 <=> l or 0 (for o), because they're hard to read - $letters = "abcdefghijkmnopqrstuvwxyzABCDEFGHIJKMNOPQRSTUVWXYZ23456789"; - $letter_len = strlen($letters); - for ($i=0; $i<4; $i++) { - $word .= $letters[mt_rand(0, $letter_len-1)]; + function Failed () { + if ($this->request->getSessionVar('captcha_ok') == true) + return false; + + if ( ! array_key_exists ( 'captcha_input', $this->meta ) + or ($this->request->getSessionVar('captchaword') + and ($this->request->getSessionVar('captchaword') != $this->meta['captcha_input']))) + return true; + + $this->request->setSessionVar('captcha_ok', true); + return false; } - return $word; -} -// Draw the Spiral -function spiral( &$im, $origin_x = 100, $origin_y = 100, $r = 0, $g = 0, $b = 0 ) { - $theta = 1; - $thetac = 6; - $radius = 15; - $circles = 10; - $points = 35; - $lcolor = imagecolorallocate( $im, $r, $g, $b ); - for( $i = 0; $i < ( $circles * $points ) - 1; $i++ ) { - $theta = $theta + $thetac; - $rad = $radius * ( $i / $points ); - $x = ( $rad * cos( $theta ) ) + $origin_x; - $y = ( $rad * sin( $theta ) ) + $origin_y; - $theta = $theta + $thetac; - $rad1 = $radius * ( ( $i + 1 ) / $points ); - $x1 = ( $rad1 * cos( $theta ) ) + $origin_x; - $y1 = ( $rad1 * sin( $theta ) ) + $origin_y; - imageline( $im, $x, $y, $x1, $y1, $lcolor ); - $theta = $theta - $thetac; + function getFormElements() { + $el = array(); + if (! $this->request->getSessionVar('captcha_ok')) { + $el['CAPTCHA_INPUT'] + = HTML::input(array('type' => 'text', + 'class' => 'wikitext', + 'id' => 'edit:captcha_input', + 'name' => 'edit[captcha_input]', + 'size' => $this->length + 2, + 'maxlength' => 256)); + $url = WikiURL("", array("action"=>"captcha","id"=>time()), false); + $el['CAPTCHA_IMAGE'] = "\"captcha\""; + $el['CAPTCHA_LABEL'] = ''; + } + return $el; + } + + function get_word () { + if (USE_CAPTCHA_RANDOM_WORD) + return get_dictionary_word(); + else + return rand_ascii_readable($this->length); // lib/stdlib.php } -} -function captcha_image ( $word ) { - $width = 250; - $height = 80; + function get_dictionary_word () { + // Load In the Word List + $fp = fopen(findfile("lib/captcha/dictionary"), "r"); + while ( !feof($fp) ) + $text[] = trim(fgets($fp, 1024)); + fclose($fp); + + // Pick a Word + $word = ""; + better_srand(); + while ( strlen(trim($word)) == 0 ) { + if (function_exists('mt_rand')) + $x = mt_rand(0, count($text)); + else + $x = rand(0, count($text)); + return $text[$x]; + } + } + + // Draw the Spiral + function spiral( &$im, $origin_x = 100, $origin_y = 100, $r = 0, $g = 0, $b = 0 ) { + $theta = 1; + $thetac = 6; + $radius = 15; + $circles = 10; + $points = 35; + $lcolor = imagecolorallocate( $im, $r, $g, $b ); + for( $i = 0; $i < ( $circles * $points ) - 1; $i++ ) { + $theta = $theta + $thetac; + $rad = $radius * ( $i / $points ); + $x = ( $rad * cos( $theta ) ) + $origin_x; + $y = ( $rad * sin( $theta ) ) + $origin_y; + $theta = $theta + $thetac; + $rad1 = $radius * ( ( $i + 1 ) / $points ); + $x1 = ( $rad1 * cos( $theta ) ) + $origin_x; + $y1 = ( $rad1 * sin( $theta ) ) + $origin_y; + imageline( $im, $x, $y, $x1, $y1, $lcolor ); + $theta = $theta - $thetac; + } + } + + function image ( $word ) { + $width =& $this->width; + $height =& $this->height; - // Create the Image - $jpg = ImageCreate($width,$height); - $bg = ImageColorAllocate($jpg,255,255,255); - $tx = ImageColorAllocate($jpg,185,140,140); - ImageFilledRectangle($jpg,0,0,$width,$height,$bg); - - $x = rand(0, $width); - $y = rand(0, $height); - spiral($jpg, $x, $y, 225, 190, 190); - - $angle = rand(-25, 25); - $size = rand(14,20); - if ( $angle >= 0 ) - $y = rand(50,$height-20); - else - $y = rand(25, 50); - $x = rand(10, $width-100); - - imagettftext($jpg, $size, $angle, $x, $y, $tx, - realpath(FindFile("lib/captcha/Vera.ttf")), - $word); - - $x = rand(0, 280); - $y = rand(0, 115); - spiral($jpg, $x, $y, 255,190,190); - - imageline($jpg, 0,0,$width-1,0,$tx); - imageline($jpg, 0,0,0,$height-1,$tx); - imageline($jpg, 0,$height-1,$width-1,$height-1,$tx); - imageline($jpg, $width-1,0,$width-1,$height-1,$tx); - - header("Content-type: image/jpeg"); - ImageJpeg($jpg); + // Create the Image + $jpg = ImageCreate($width,$height); + $bg = ImageColorAllocate($jpg,255,255,255); + $tx = ImageColorAllocate($jpg,185,140,140); + ImageFilledRectangle($jpg,0,0,$width,$height,$bg); + + $x = rand(0, $width); + $y = rand(0, $height); + $this->spiral($jpg, $x, $y, $width-25, 190, 190); + + $x = rand(10, 30); + $y = rand(50, $height-20); //50-60 + + // randomize the chars + for ($i=0; $i < strlen($word); $i++) { + $angle += rand(-5, 5); + if ( $angle > 25 ) $angle = 15; + elseif ( $angle < -25 ) $angle = -15; + $size = rand(14, 20); + $y += rand(-10, 10); + if ( $y < 10 ) $y = 11; + elseif ( $y > $height-10 ) $y = $height-11; + $x += rand($size, $size*2); + imagettftext($jpg, $size, $angle, $x, $y, $tx, + realpath(findfile("lib/captcha/Vera.ttf")), + $word[$i]); + } + + $x = rand(0, $width+30); + $y = rand(0, $height+35); // 115 + $this->spiral($jpg, $x, $y, 255,190,190); + + imageline($jpg, 0,0,$width-1,0,$tx); + imageline($jpg, 0,0,0,$height-1,$tx); + imageline($jpg, 0,$height-1,$width-1,$height-1,$tx); + imageline($jpg, $width-1,0,$width-1,$height-1,$tx); + + //TODO: JPEG or PNG? + header("Content-type: image/jpeg"); + ImageJpeg($jpg); + } + } // $Log: not supported by cvs2svn $ +// Revision 1.3 2005/10/29 07:37:56 rurban +// USE_CAPTCHA_RANDOM_WORD by Dan Frankowski +// // Local Variables: // mode: php diff --git a/lib/editpage.php b/lib/editpage.php index 7c81bdd49..7172ff1dc 100644 --- a/lib/editpage.php +++ b/lib/editpage.php @@ -1,23 +1,8 @@ time()); $this->tokens = array(); - + + if (ENABLE_WYSIWYG) { + require_once("lib/WysiwygEdit.php"); + if (USE_TINYMCE) $backend = "tinymce"; + elseif (USE_HTMLAREA3) $backend = "htmlarea3"; + elseif (USE_HTMLAREA2) $backend = "htmlarea2"; + else trigger_error("undefined WysiwygEdit backend", E_USER_ERROR); + + require_once("lib/WysiwygEdit/$backend.php"); + $class = "WysiwygEdit_$backend"; + $this->WysiwygEdit = new $class(); + } + if (ENABLE_CAPTCHA) { + require_once('lib/Captcha.php'); + $this->Captcha = new Captcha($this->meta); + } + $version = $request->getArg('version'); if ($version !== false) { $this->selected = $this->page->getRevision($version); @@ -95,9 +96,9 @@ class PageEditor $tokens['PAGE_LOCKED_MESSAGE'] = $this->getLockedMessage(); } elseif ($this->request->getArg('save_and_redirect_to') != "") { - if (ENABLE_CAPTCHA && $this->captchaFailed()) { + if (ENABLE_CAPTCHA && $this->Captcha->Failed()) { $this->tokens['PAGE_LOCKED_MESSAGE'] = - HTML::p(HTML::h1(_("Typed in verification word mismatch ... are you a bot?"))); + HTML::p(HTML::h1($this->Captcha->failed_msg)); } elseif ( $this->savePage()) { // noreturn @@ -107,9 +108,9 @@ class PageEditor $saveFailed = true; } elseif ($this->editaction == 'save') { - if (ENABLE_CAPTCHA && $this->captchaFailed()) { + if (ENABLE_CAPTCHA && $this->Captcha->Failed()) { $this->tokens['PAGE_LOCKED_MESSAGE'] = - HTML::p(HTML::h1(_("Typed in verification word mismatch ... are you a bot?"))); + HTML::p(HTML::h1($this->Captcha->failed_msg)); } elseif ($this->savePage()) { return true; // Page saved. @@ -179,9 +180,9 @@ class PageEditor $title = new FormattedText ($title_fs, $pagelink); // not for dumphtml or viewsource - if (ENABLE_WYSIWYG and $template == 'editpage') { - $WikiTheme->addMoreHeaders(Edit_WYSIWYG_Head()); - //$tokens['PAGE_SOURCE'] = Edit_WYSIWYG_ConvertBefore($this->_content); + if (ENABLE_WYSIWYG and $template == 'editpage') { + $WikiTheme->addMoreHeaders($this->WysiwygEdit->Head()); + //$tokens['PAGE_SOURCE'] = $WysiwygEdit->ConvertBefore($this->_content); } $template = Template($template, $this->tokens); GeneratePage($template, $title, $rev); @@ -283,7 +284,7 @@ class PageEditor (admin plugins) */ // look at the errorstack - $errors = $GLOBALS['ErrorManager']->_postponed_errors; + $errors = $GLOBALS['ErrorManager']->_postponed_errors; $warnings = $GLOBALS['ErrorManager']->getPostponedErrorsAsHTML(); $GLOBALS['ErrorManager']->_postponed_errors = $errors; @@ -314,19 +315,6 @@ class PageEditor return true; } - function captchaFailed () { - if ($this->request->getSessionVar('captcha_ok') == true) - return false; - - if ( ! array_key_exists ( 'captcha_input', $this->meta ) - or ($this->request->getSessionVar('captchaword') - and ($this->request->getSessionVar('captchaword') != $this->meta['captcha_input']))) - return true; - - $this->request->setSessionVar('captcha_ok', true); - return false; - } - function isConcurrentUpdate () { assert($this->current->getVersion() >= $this->_currentVersion); return $this->current->getVersion() != $this->_currentVersion; @@ -450,7 +438,7 @@ class PageEditor // possibly convert HTMLAREA content back to Wiki markup function getContent () { if (ENABLE_WYSIWYG) { - $xml_output = Edit_WYSIWYG_ConvertAfter($this->_content); + $xml_output = $this->WysiwygEdit->ConvertAfter($this->_content); //$this->_content = join("", $xml_output->_content); return $this->_content; } else { @@ -523,9 +511,10 @@ class PageEditor */ if (isBrowserNS4()) $textarea->setAttr('wrap', 'virtual'); - if (ENABLE_WYSIWYG) - return Edit_WYSIWYG_Textarea($textarea, $this->_wikicontent, $textarea->getAttr('name')); - else + if (ENABLE_WYSIWYG) { + return $this->WysiwygEdit->Textarea($textarea, $this->_wikicontent, + $textarea->getAttr('name')); + } else return $textarea; } @@ -542,20 +531,9 @@ class PageEditor $el['HIDDEN_INPUTS'] = HiddenInputs($h); $el['EDIT_TEXTAREA'] = $this->getTextArea(); - if ( ENABLE_CAPTCHA && ! $request->getSessionVar('captchaword')) { - $request->setSessionVar('captchaword', get_captcha_word()); - } - if ( ENABLE_CAPTCHA && ! $request->getSessionVar('captcha_ok')) { - $el['CAPTCHA_INPUT'] - = HTML::input(array('type' => 'text', - 'class' => 'wikitext', - 'id' => 'edit:captcha_input', - 'name' => 'edit[captcha_input]', - 'size' => 20, - 'maxlength' => 256)); - $el['CAPTCHA_IMAGE'] = 'captcha'; - $el['CAPTCHA_LABEL'] = ''; - } + if ( ENABLE_CAPTCHA ) { + $el = array_merge($el, $this->Captcha->getFormElements()); + } $el['SUMMARY_INPUT'] = HTML::input(array('type' => 'text', 'class' => 'wikitext', @@ -780,6 +758,7 @@ extends PageEditor PrintXML($template); return true; } + function getConflictMessage () { $message = HTML(HTML::p(fmt("Some of the changes could not automatically be combined. Please look for sections beginning with '%s', and ending with '%s'. You will need to edit those sections by hand before you click Save.", "<<<<<<<", @@ -791,6 +770,11 @@ extends PageEditor /** $Log: not supported by cvs2svn $ + Revision 1.99 2005/10/29 08:21:58 rurban + ENABLE_SPAMBLOCKLIST: + Check for links to blocked external tld domains in new edits, against + multi.surbl.org and bl.spamcop.net. + Revision 1.98 2005/10/10 19:37:04 rurban change USE_HTMLAREA to ENABLE WYSIWYG, add NUM_SPAM_LINKS=20 diff --git a/lib/main.php b/lib/main.php index 37cfe2ebf..03cb8ecf0 100644 --- a/lib/main.php +++ b/lib/main.php @@ -1,5 +1,5 @@ getSessionVar('captchaword')); + $captcha = new Captcha(); + $captcha->image ( $captcha->captchaword() ); } } @@ -1272,6 +1273,9 @@ if (!defined('PHPWIKI_NOMAIN') or !PHPWIKI_NOMAIN) // $Log: not supported by cvs2svn $ +// Revision 1.218 2005/10/29 14:18:06 rurban +// fix typo +// // Revision 1.217 2005/09/18 12:44:00 rurban // novatrope patch to let only _AUTHENTICATED view pages // -- 2.45.0