]> ToastFreeware Gitweb - toast/cookiecaptcha.git/blobdiff - ConfirmEdit_body.php
The EditPage object is not passed by reference in the EditFilter hooks
[toast/cookiecaptcha.git] / ConfirmEdit_body.php
index e905640cf23b8bafb119fecba564e462a397effc..991bd6331c4415b56b2bb2dd62a5f5453a022847 100644 (file)
@@ -15,11 +15,11 @@ class ConfirmEditHooks {
                return $wgCaptcha;
        }
 
-       static function confirmEdit( &$editPage, $newtext, $section ) {
+       static function confirmEdit( $editPage, $newtext, $section ) {
                return self::getInstance()->confirmEdit( $editPage, $newtext, $section );
        }
 
-       static function confirmEditMerged( &$editPage, $newtext ) {
+       static function confirmEditMerged( $editPage, $newtext ) {
                return self::getInstance()->confirmEditMerged( $editPage, $newtext );
        }
        
@@ -54,7 +54,8 @@ class CaptchaSpecialPage extends UnlistedSpecialPage {
                $instance = ConfirmEditHooks::getInstance();
                switch( $par ) {
                case "image":
-                       return $instance->showImage();
+                       if( method_exists($instance,'showImage') )
+                               return $instance->showImage();
                case "help":
                default:
                        return $instance->showHelp();
@@ -83,6 +84,7 @@ class SimpleCaptcha {
                $captcha = $this->getCaptcha();
                $index = $this->storeCaptcha( $captcha );
                $resultArr['captcha']['type'] = 'simple';
+               $resultArr['captcha']['mime'] = 'text/plain';
                $resultArr['captcha']['id'] = $index;
                $resultArr['captcha']['question'] = $captcha['question'];
        }
@@ -101,12 +103,12 @@ class SimpleCaptcha {
                $index = $this->storeCaptcha( $captcha );
 
                return "<p><label for=\"wpCaptchaWord\">{$captcha['question']}</label> = " .
-                       wfElement( 'input', array(
+                       Xml::element( 'input', array(
                                'name' => 'wpCaptchaWord',
                                'id'   => 'wpCaptchaWord',
                                'tabindex' => 1 ) ) . // tab in before the edit textarea
                        "</p>\n" .
-                       wfElement( 'input', array(
+                       Xml::element( 'input', array(
                                'type'  => 'hidden',
                                'name'  => 'wpCaptchaId',
                                'id'    => 'wpCaptchaId',
@@ -144,8 +146,12 @@ class SimpleCaptcha {
         * @return bool true to keep running callbacks
         */
        function injectUserCreate( &$template ) {
-               global $wgCaptchaTriggers, $wgOut;
+               global $wgCaptchaTriggers, $wgOut, $wgUser;
                if( $wgCaptchaTriggers['createaccount'] ) {
+                       if( $wgUser->isAllowed( 'skipcaptcha' ) ) {
+                               wfDebug( "ConfirmEdit: user group allows skipping captcha on account creation\n" );
+                               return true;
+                       }
                        $template->set( 'header',
                                "<div class='captcha'>" .
                                $wgOut->parse( $this->getMessage( 'createaccount' ) ) .
@@ -203,8 +209,24 @@ class SimpleCaptcha {
         * @access private
         */
        function isBadLoginTriggered() {
-               global $wgMemc;
-               return intval( $wgMemc->get( $this->badLoginKey() ) ) > 0;
+               global $wgMemc, $wgCaptchaBadLoginAttempts;
+               return intval( $wgMemc->get( $this->badLoginKey() ) ) >= $wgCaptchaBadLoginAttempts;
+       }
+       
+       /**
+        * Check if the IP is allowed to skip captchas
+        */
+       function isIPWhitelisted() {
+               global $wgCaptchaWhitelistIP;
+               if( $wgCaptchaWhitelistIP ) {
+                       $ip = wfGetIp();
+                       foreach ( $wgCaptchaWhitelistIP as $range ) {
+                               if ( IP::isInRange( $ip, $range ) ) {
+                                       return true;
+                               }
+                       }
+               }
+               return false;
        }
        
        /**
@@ -262,15 +284,8 @@ class SimpleCaptcha {
                        wfDebug( "ConfirmEdit: user group allows skipping captcha\n" );
                        return false;
                }
-               global $wgCaptchaWhitelistIP;
-               if( !empty( $wgCaptchaWhitelistIP ) ) {
-                       $ip = wfGetIp();
-                       foreach ( $wgCaptchaWhitelistIP as $range ) {
-                               if ( IP::isInRange( $ip, $range ) ) {
-                                       return false;
-                               }
-                       }
-               }
+               if( $this->isIPWhitelisted() )
+                       return false;
 
 
                global $wgEmailAuthentication, $ceAllowConfirmedEmail;
@@ -313,8 +328,8 @@ class SimpleCaptcha {
                        } else {
                                // Get link changes in the slowest way known to man
                                $oldtext = $this->loadText( $editPage, $section );
-                               $oldLinks = $this->findLinks( $oldtext );
-                               $newLinks = $this->findLinks( $newtext );
+                               $oldLinks = $this->findLinks( $editPage, $oldtext );
+                               $newLinks = $this->findLinks( $editPage, $newtext );
                        }
 
                        $unknownLinks = array_filter( $newLinks, array( &$this, 'filterLink' ) );
@@ -334,7 +349,7 @@ class SimpleCaptcha {
                }
 
                global $wgCaptchaRegexes;
-               if( !empty( $wgCaptchaRegexes ) ) {
+               if( $wgCaptchaRegexes ) {
                        // Custom regex checks
                        $oldtext = $this->loadText( $editPage, $section );
 
@@ -407,7 +422,7 @@ class SimpleCaptcha {
                        //$regex = 'http://+[a-z0-9_\-.]*(' . implode( '|', $lines ) . ')';
                        //return '/' . str_replace( '/', '\/', preg_replace('|\\\*/|', '/', $regex) ) . '/Si';
                        $regexes = '';
-                       $regexStart = '/http:\/\/+[a-z0-9_\-.]*(';
+                       $regexStart = '/^https?:\/\/+[a-z0-9_\-.]*(';
                        $regexEnd = ')/Si';
                        $regexMax = 4096;
                        $build = false;
@@ -452,7 +467,7 @@ class SimpleCaptcha {
         * Backend function for confirmEdit() and confirmEditAPI()
         * @return bool false if the CAPTCHA is rejected, true otherwise
         */
-       private function doConfirmEdit( &$editPage, $newtext, $section, $merged = false ) {
+       private function doConfirmEdit( $editPage, $newtext, $section, $merged = false ) {
                if( $this->shouldCheck( $editPage, $newtext, $section, $merged ) ) {
                        if( $this->passCaptcha() ) {
                                return true;
@@ -473,9 +488,8 @@ class SimpleCaptcha {
         * @param bool $merged
         * @return bool true to continue saving, false to abort and show a captcha form
         */
-       function confirmEdit( &$editPage, $newtext, $section, $merged = false ) {
-               global $wgTitle;
-               if( is_null( $wgTitle ) ) {
+       function confirmEdit( $editPage, $newtext, $section, $merged = false ) {
+               if( defined('MW_API') ) {
                        # API mode
                        # The CAPTCHA was already checked and approved 
                        return true;
@@ -492,7 +506,7 @@ class SimpleCaptcha {
         * @param EditPage $editPage
         * @param string $newtext
         */
-       function confirmEditMerged( &$editPage, $newtext ) {
+       function confirmEditMerged( $editPage, $newtext ) {
                return $this->confirmEdit( $editPage, $newtext, false, true );
        }
        
@@ -512,8 +526,15 @@ class SimpleCaptcha {
         * @return bool true to continue, false to abort user creation
         */
        function confirmUserCreate( $u, &$message ) {
-               global $wgCaptchaTriggers;
+               global $wgCaptchaTriggers, $wgUser;
                if( $wgCaptchaTriggers['createaccount'] ) {
+                       if( $wgUser->isAllowed( 'skipcaptcha' ) ) {
+                               wfDebug( "ConfirmEdit: user group allows skipping captcha on account creation\n" );
+                               return true;
+                       }
+                       if( $this->isIPWhitelisted() )
+                               return true;
+                               
                        $this->trigger = "new account '" . $u->getName() . "'";
                        if( !$this->passCaptcha() ) {
                                $message = wfMsg( 'captcha-createaccount-fail' );
@@ -531,6 +552,9 @@ class SimpleCaptcha {
         */
        function confirmUserLogin( $u, $pass, &$retval ) {
                if( $this->isBadLoginTriggered() ) {
+                       if( $this->isIPWhitelisted() )
+                               return true;
+                       
                        $this->trigger = "post-badlogin login '" . $u->getName() . "'";
                        if( !$this->passCaptcha() ) {
                                $message = wfMsg( 'captcha-badlogin-fail' );
@@ -638,12 +662,12 @@ class SimpleCaptcha {
         * @param string $text
         * @return array of strings
         */
-       function findLinks( $text ) {
-               global $wgParser, $wgTitle, $wgUser;
+       function findLinks( &$editpage, $text ) {
+               global $wgParser, $wgUser;
 
                $options = new ParserOptions();
-               $text = $wgParser->preSaveTransform( $text, $wgTitle, $wgUser, $options );
-               $out = $wgParser->parse( $text, $wgTitle, $options );
+               $text = $wgParser->preSaveTransform( $text, $editpage->mTitle, $wgUser, $options );
+               $out = $wgParser->parse( $text, $editpage->mTitle, $options );
 
                return array_keys( $out->getExternalLinks() );
        }