Adding elements to $wgSpecialPages must be done before SpecialPage::initList()
[toast/cookiecaptcha.git] / FancyCaptcha.php
index ec867e8739d57b850b80b71d1898306e3bc1aaa9..c21afb8c854775c82add2f4b2f6b60467e145501 100644 (file)
@@ -1,4 +1,29 @@
 <?php
 <?php
+/**
+ * Experimental image-based captcha plugin, using images generated by an
+ * external tool.
+ *
+ * Copyright (C) 2005, 2006 Brion Vibber <brion@pobox.com>
+ * http://www.mediawiki.org/
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ * http://www.gnu.org/copyleft/gpl.html
+ *
+ * @package MediaWiki
+ * @subpackage Extensions
+ */
 
 if ( defined( 'MEDIAWIKI' ) ) {
 
 
 if ( defined( 'MEDIAWIKI' ) ) {
 
@@ -10,27 +35,26 @@ $wgCaptchaSecret = "CHANGE_THIS_SECRET!";
 
 
 class FancyCaptcha extends SimpleCaptcha {
 
 
 class FancyCaptcha extends SimpleCaptcha {
-       function keyMatch() {
-               global $wgRequest, $wgCaptchaSecret;
-               
-               if( !isset( $_SESSION['ceAnswerVar'] ) ) {
-                       wfDebug( "FancyCaptcha: no session captcha key set, this is new visitor.\n" );
-                       return false;
-               }
-               
-               $var  = @$_SESSION['ceAnswerVar'];
-               $salt = @$_SESSION['captchaSalt'];
-               $hash = @$_SESSION['captchaHash'];
+       /**
+        * Check if the submitted form matches the captcha session data provided
+        * by the plugin when the form was generated.
+        *
+        * @param WebRequest $request
+        * @param array $info
+        * @return bool
+        */
+       function keyMatch( $request, $info ) {
+               global $wgCaptchaSecret;
                
                
-               $answer = $wgRequest->getVal( $var );
-               $digest = $wgCaptchaSecret . $salt . $answer . $wgCaptchaSecret . $salt;
+               $answer = $request->getVal( 'wpCaptchaWord' );
+               $digest = $wgCaptchaSecret . $info['salt'] . $answer . $wgCaptchaSecret . $info['salt'];
                $answerHash = substr( md5( $digest ), 0, 16 );
                
                $answerHash = substr( md5( $digest ), 0, 16 );
                
-               if( $answerHash == $hash ) {
-                       wfDebug( "FancyCaptcha: answer hash matches expected $hash\n" );
+               if( $answerHash == $info['hash'] ) {
+                       wfDebug( "FancyCaptcha: answer hash matches expected {$info['hash']}\n" );
                        return true;
                } else {
                        return true;
                } else {
-                       wfDebug( "FancyCaptcha: answer hashes to $answerHash, expected $hash\n" );
+                       wfDebug( "FancyCaptcha: answer hashes to $answerHash, expected {$info['hash']}\n" );
                        return false;
                }
        }
                        return false;
                }
        }
@@ -38,31 +62,39 @@ class FancyCaptcha extends SimpleCaptcha {
        /**
         * Insert the captcha prompt into the edit form.
         */
        /**
         * Insert the captcha prompt into the edit form.
         */
-       function formCallback( &$out ) {
-               $dest = 'wpCaptchaWord' . mt_rand();
-               
-               $img = $this->pickImage();
-               if( !$img ) {
+       function getForm() {
+               $info = $this->pickImage();
+               if( !$info ) {
                        die( "out of captcha images; this shouldn't happen" );
                }
                
                        die( "out of captcha images; this shouldn't happen" );
                }
                
-               $_SESSION['ceAnswerVar'] = $dest;
-               $_SESSION['captchaHash'] = $img['hash'];
-               $_SESSION['captchaSalt'] = $img['salt'];
-               $_SESSION['captchaViewed'] = false;
-               wfDebug( "Picked captcha with hash ${img['hash']}, salt ${img['salt']}.\n" );
+               // Generate a random key for use of this captcha image in this session.
+               // This is needed so multiple edits in separate tabs or windows can
+               // go through without extra pain.
+               $index = $this->storeCaptcha( $info );
+               
+               wfDebug( "Captcha id $index using hash ${info['hash']}, salt ${info['salt']}.\n" );
                
                $title = Title::makeTitle( NS_SPECIAL, 'Captcha/image' );
                
                
                $title = Title::makeTitle( NS_SPECIAL, 'Captcha/image' );
                
-               $out->addWikiText( wfMsg( "captcha-short" ) );  
-               $out->addHTML( "<p>" . 
+               return "<p>" . 
                        wfElement( 'img', array(
                        wfElement( 'img', array(
-                               'src'    => $title->getLocalUrl(),
-                               'width'  => $img['width'],
-                               'height' => $img['height'],
+                               'src'    => $title->getLocalUrl( 'wpCaptchaId=' . urlencode( $index ) ),
+                               'width'  => $info['width'],
+                               'height' => $info['height'],
                                'alt'    => '' ) ) .
                        "</p>\n" .
                                'alt'    => '' ) ) .
                        "</p>\n" .
-                       "<p><input name=\"$dest\" id=\"$dest\" tabindex=\"1\" /></p>" );
+                       wfElement( 'input', array(
+                               'type'  => 'hidden',
+                               'name'  => 'wpCaptchaId',
+                               'id'    => 'wpCaptchaId',
+                               'value' => $index ) ) .
+                       "<p>" .
+                       wfElement( 'input', array(
+                               'name' => 'wpCaptchaWord',
+                               'id'   => 'wpCaptchaWord',
+                               'tabindex' => 1 ) ) . // tab in before the edit textarea
+                       "</p>\n";
        }
        
        /**
        }
        
        /**
@@ -72,10 +104,9 @@ class FancyCaptcha extends SimpleCaptcha {
         */
        function pickImage() {
                global $wgCaptchaDirectory;
         */
        function pickImage() {
                global $wgCaptchaDirectory;
-               $pick = mt_rand( 0, $this->countFiles( $wgCaptchaDirectory ) );
+               $n = mt_rand( 0, $this->countFiles( $wgCaptchaDirectory ) );
                $dir = opendir( $wgCaptchaDirectory );
                
                $dir = opendir( $wgCaptchaDirectory );
                
-               $n = mt_rand( 0, 16 );
                $count = 0;
                
                $entry = readdir( $dir );
                $count = 0;
                
                $entry = readdir( $dir );
@@ -88,7 +119,8 @@ class FancyCaptcha extends SimpleCaptcha {
                                        'salt' => $matches[1],
                                        'hash' => $matches[2],
                                        'width' => $size[0],
                                        'salt' => $matches[1],
                                        'hash' => $matches[2],
                                        'width' => $size[0],
-                                       'height' => $size[1]
+                                       'height' => $size[1],
+                                       'viewed' => false,
                                );
                                if( $count++ == $n ) {
                                        break;
                                );
                                if( $count++ == $n ) {
                                        break;
@@ -116,29 +148,33 @@ class FancyCaptcha extends SimpleCaptcha {
        }
        
        function showImage() {
        }
        
        function showImage() {
-               global $wgOut;
+               global $wgOut, $wgRequest;
+               global $wgCaptchaDirectory;
+               
                $wgOut->disable();
                $wgOut->disable();
-               if( !empty( $_SESSION['captchaViewed'] ) ) {
-                       wfHttpError( 403, 'Access Forbidden', "Can't view captcha image a second time." );
-                       return false;
-               }
-               $_SESSION['captchaViewed'] = wfTimestamp();
                
                
-               if( isset( $_SESSION['captchaSalt'] ) ) {
-                       $salt = $_SESSION['captchaSalt'];
-                       if( isset( $_SESSION['captchaHash'] ) ) {
-                               $hash = $_SESSION['captchaHash'];
-                               
-                               global $wgCaptchaDirectory;
-                               $file = $wgCaptchaDirectory . DIRECTORY_SEPARATOR . "image_{$salt}_{$hash}.png";
-                               if( file_exists( $file ) ) {
-                                       header( 'Content-type: image/png' );
-                                       readfile( $file );
-                               }
+               $info = $this->retrieveCaptcha();
+               if( $info ) {
+                       if( $info['viewed'] ) {
+                               wfHttpError( 403, 'Access Forbidden', "Can't view captcha image a second time." );
+                               return false;
+                       }
+                       
+                       $info['viewed'] = wfTimestamp();
+                       $this->storeCaptcha( $info );
+                       
+                       $salt = $info['salt'];
+                       $hash = $info['hash'];
+                       $file = $wgCaptchaDirectory . DIRECTORY_SEPARATOR . "image_{$salt}_{$hash}.png";
+                       
+                       if( file_exists( $file ) ) {
+                               header( 'Content-type: image/png' );
+                               readfile( $file );
+                               return true;
                        }
                        }
-               } else {
-                       wfHttpError( 500, 'Internal Error', 'Requested bogus captcha image' );
                }
                }
+               wfHttpError( 500, 'Internal Error', 'Requested bogus captcha image' );
+               return false;
        }
 }
 
        }
 }