More maintenance for ConfirmEdit extension.
[toast/cookiecaptcha.git] / FancyCaptcha.class.php
index 0d74116557d05f517c400ae5dc8961158e0f397b..c6ee03c6ddf76b155bc8c1cd8c88a2162c7cf276 100644 (file)
@@ -44,7 +44,7 @@ class FancyCaptcha extends SimpleCaptcha {
        function getForm() {
                $info = $this->pickImage();
                if ( !$info ) {
-                       die( "out of captcha images; this shouldn't happen" );
+                       throw new MWException( "Ran out of captcha images" );
                }
 
                // Generate a random key for use of this captcha image in this session.
@@ -57,21 +57,28 @@ class FancyCaptcha extends SimpleCaptcha {
                $title = SpecialPage::getTitleFor( 'Captcha', 'image' );
 
                return "<p>" .
-                       Xml::element( 'img', array(
+                       Html::element( 'img', array(
                                'src'    => $title->getLocalUrl( 'wpCaptchaId=' . urlencode( $index ) ),
                                'width'  => $info['width'],
                                'height' => $info['height'],
                                'alt'    => '' ) ) .
                        "</p>\n" .
-                       Xml::element( 'input', array(
+                       Html::element( 'input', array(
                                'type'  => 'hidden',
                                'name'  => 'wpCaptchaId',
                                'id'    => 'wpCaptchaId',
                                'value' => $index ) ) .
-                       "<p>" .
-                       Xml::element( 'input', array(
+                       '<p>' .
+                       Html::element( 'label', array(
+                               'for' => 'wpCaptchaWord',
+                       ), parent::getMessage( 'label' ) . wfMessage( 'colon-separator' )->text() ) .
+                       Html::element( 'input', array(
                                'name' => 'wpCaptchaWord',
                                'id'   => 'wpCaptchaWord',
+                               'type' => 'text',
+                               'autocorrect' => 'off',
+                               'autocapitalize' => 'off',
+                               'required' => 'required',
                                'tabindex' => 1 ) ) . // tab in before the edit textarea
                        "</p>\n";
        }
@@ -94,6 +101,9 @@ class FancyCaptcha extends SimpleCaptcha {
 
                        // Check which subdirs are actually present...
                        $dir = opendir( $directory );
+                       if ( !$dir ) {
+                               return false;
+                       }
                        while ( false !== ( $entry = readdir( $dir ) ) ) {
                                if ( ctype_xdigit( $entry ) && strlen( $entry ) == 1 ) {
                                        $dirs[] = $entry;
@@ -151,6 +161,7 @@ class FancyCaptcha extends SimpleCaptcha {
 
        /**
         * Count the number of files in a directory.
+        * @param $dirname
         * @return int
         */
        function countFiles( $dirname ) {
@@ -166,23 +177,14 @@ class FancyCaptcha extends SimpleCaptcha {
        }
 
        function showImage() {
-               global $wgOut, $wgRequest;
+               global $wgOut;
 
                $wgOut->disable();
 
                $info = $this->retrieveCaptcha();
                if ( $info ) {
-                       /*
-                       // Be a little less restrictive for now; in at least some circumstances,
-                       // Konqueror tries to reload the image even if you haven't navigated
-                       // away from the page.
-                       if( $info['viewed'] ) {
-                               wfHttpError( 403, 'Access Forbidden', "Can't view captcha image a second time." );
-                               return false;
-                       }
-                       */
-
-                       $info['viewed'] = wfTimestamp();
+                       $timestamp = new MWTimestamp();
+                       $info['viewed'] = $timestamp->getTimestamp();
                        $this->storeCaptcha( $info );
 
                        $salt = $info['salt'];
@@ -193,7 +195,7 @@ class FancyCaptcha extends SimpleCaptcha {
                                global $IP;
                                require_once "$IP/includes/StreamFile.php";
                                header( "Cache-Control: private, s-maxage=0, max-age=3600" );
-                               wfStreamFile( $file );
+                               StreamFile::stream( $file );
                                return true;
                        }
                }
@@ -217,14 +219,33 @@ class FancyCaptcha extends SimpleCaptcha {
         * Show a message asking the user to enter a captcha on edit
         * The result will be treated as wiki text
         *
-        * @param $action Action being performed
+        * @param $action string Action being performed
         * @return string
         */
        function getMessage( $action ) {
                $name = 'fancycaptcha-' . $action;
-               $text = wfMsg( $name );
+               $text = wfMessage( $name )->text();
                # Obtain a more tailored message, if possible, otherwise, fall back to
                # the default for edits
-               return wfEmptyMsg( $name, $text ) ? wfMsg( 'fancycaptcha-edit' ) : $text;
+               return wfMessage( $name, $text )->isDisabled() ? wfMessage( 'fancycaptcha-edit' )->text() : $text;
+       }
+
+       /**
+        * Delete a solved captcha image, if $wgCaptchaDeleteOnSolve is true.
+        */
+       function passCaptcha() {
+               global $wgCaptchaDeleteOnSolve;
+
+               $info = $this->retrieveCaptcha(); // get the captcha info before it gets deleted
+               $pass = parent::passCaptcha();
+
+               if ( $pass && $wgCaptchaDeleteOnSolve ) {
+                       $filename = $this->imagePath( $info['salt'], $info['hash'] );
+                       if ( file_exists( $filename ) ) {
+                               unlink( $filename );
+                       }
+               }
+
+               return $pass;
        }
 }