-
- /**
- * Select a previously generated captcha image from the queue.
- * @return mixed tuple of (salt key, text hash) or false if no image to find
- */
- protected function pickImage() {
- global $wgCaptchaDirectoryLevels;
-
- $lockouts = 0; // number of times another process claimed a file before this one
- $baseDir = $this->getBackend()->getRootStoragePath() . '/captcha-render';
- return $this->pickImageDir( $baseDir, $wgCaptchaDirectoryLevels, $lockouts );
- }
-
- /**
- * @param $directory string
- * @param $levels integer
- * @param $lockouts integer
- * @return Array|bool
- */
- protected function pickImageDir( $directory, $levels, &$lockouts ) {
- global $wgMemc;
-
- if ( $levels <= 0 ) { // $directory has regular files
- return $this->pickImageFromDir( $directory, $lockouts );
- }
-
- $backend = $this->getBackend();
-
- $key = "fancycaptcha:dirlist:{$backend->getWikiId()}:" . sha1( $directory );
- $dirs = $wgMemc->get( $key ); // check cache
- if ( !is_array( $dirs ) ) { // cache miss
- $dirs = array(); // subdirs actually present...
- foreach ( $backend->getTopDirectoryList( array( 'dir' => $directory ) ) as $entry ) {
- if ( ctype_xdigit( $entry ) && strlen( $entry ) == 1 ) {
- $dirs[] = $entry;
- }
- }
- wfDebug( "Cache miss for $directory subdirectory listing.\n" );
- $wgMemc->set( $key, $dirs, 86400 );
- }
-
- if ( !count( $dirs ) ) {
- // Remove this directory if empty so callers don't keep looking here
- $backend->clean( array( 'dir' => $directory ) );
- return false; // none found
- }
-
- $place = mt_rand( 0, count( $dirs ) - 1 ); // pick a random subdir
- // In case all dirs are not filled, cycle through next digits...
- for ( $j = 0; $j < count( $dirs ); $j++ ) {
- $char = $dirs[( $place + $j ) % count( $dirs )];
- $info = $this->pickImageDir( "$directory/$char", $levels - 1, $lockouts );
- if ( $info ) {
- return $info; // found a captcha
- } else {
- wfDebug( "Could not find captcha in $directory.\n" );
- $wgMemc->delete( $key ); // files changed on disk?
- }
- }
-
- return false; // didn't find any images in this directory... empty?
- }
-
- /**
- * @param $directory string
- * @param $lockouts integer
- * @return Array|bool
- */
- protected function pickImageFromDir( $directory, &$lockouts ) {
- global $wgMemc;
-
- $backend = $this->getBackend();
-
- $key = "fancycaptcha:filelist:{$backend->getWikiId()}:" . sha1( $directory );
- $files = $wgMemc->get( $key ); // check cache
- if ( !is_array( $files ) ) { // cache miss
- $files = array(); // captcha files
- foreach ( $backend->getTopFileList( array( 'dir' => $directory ) ) as $entry ) {
- $files[] = $entry;
- if ( count( $files ) >= 500 ) { // sanity
- wfDebug( 'Skipping some captchas; $wgCaptchaDirectoryLevels set too low?.' );
- break;
- }
- }
- $wgMemc->set( $key, $files, 86400 );
- wfDebug( "Cache miss for $directory captcha listing.\n" );
- }
-
- if ( !count( $files ) ) {
- // Remove this directory if empty so callers don't keep looking here
- $backend->clean( array( 'dir' => $directory ) );
- return false;
- }
-
- $info = $this->pickImageFromList( $directory, $files, $lockouts );
- if ( !$info ) {
- wfDebug( "Could not find captcha in $directory.\n" );
- $wgMemc->delete( $key ); // files changed on disk?
- }
-
- return $info;
- }
-
- /**
- * @param $directory string
- * @param $files array
- * @param $lockouts integer
- * @return boolean
- */
- protected function pickImageFromList( $directory, array $files, &$lockouts ) {
- global $wgMemc, $wgCaptchaDeleteOnSolve;
-
- if ( !count( $files ) ) {
- return false; // none found
- }
-
- $backend = $this->getBackend();
- $place = mt_rand( 0, count( $files ) - 1 ); // pick a random file
- $misses = 0; // number of files in listing that don't actually exist
- for ( $j = 0; $j < count( $files ); $j++ ) {
- $entry = $files[( $place + $j ) % count( $files )];
- if ( preg_match( '/^image_([0-9a-f]+)_([0-9a-f]+)\\.png$/', $entry, $matches ) ) {
- if ( $wgCaptchaDeleteOnSolve ) { // captcha will be deleted when solved
- $key = "fancycaptcha:filelock:{$backend->getWikiId()}:" . sha1( $entry );
- // Try to claim this captcha for 10 minutes (for the user to solve)...
- if ( ++$lockouts <= 10 && !$wgMemc->add( $key, '1', 600 ) ) {
- continue; // could not acquire (skip it to avoid race conditions)
- }
- }
- $fsFile = $backend->getLocalReference( array( 'src' => "$directory/$entry" ) );
- if ( !$fsFile || !$fsFile->exists() ) {
- if ( ++$misses >= 5 ) { // too many files in the listing don't exist
- break; // listing cache too stale? break out so it will be cleared
- }
- continue; // try next file
- }
- $size = getimagesize( $fsFile->getPath() );
- return array(
- 'salt' => $matches[1],
- 'hash' => $matches[2],
- 'width' => $size[0],
- 'height' => $size[1],
- 'viewed' => false,
- );
- }
- }
-
- return false; // none found
- }
-
- function showImage() {
- global $wgOut;
-
- $wgOut->disable();
-
- $info = $this->retrieveCaptcha();
- if ( $info ) {
- $timestamp = new MWTimestamp();
- $info['viewed'] = $timestamp->getTimestamp();
- $this->storeCaptcha( $info );
-
- $salt = $info['salt'];
- $hash = $info['hash'];
-
- return $this->getBackend()->streamFile( array(
- 'src' => $this->imagePath( $salt, $hash ),
- 'headers' => array( "Cache-Control: private, s-maxage=0, max-age=3600" )
- ) )->isOK();
- }
-
- wfHttpError( 500, 'Internal Error', 'Requested bogus captcha image' );
- return false;
- }
-
- /**
- * @param $salt string
- * @param $hash string
- * @return string
- */
- public function imagePath( $salt, $hash ) {
- global $wgCaptchaDirectoryLevels;
-
- $file = $this->getBackend()->getRootStoragePath() . '/captcha-render/';
- for ( $i = 0; $i < $wgCaptchaDirectoryLevels; $i++ ) {
- $file .= $hash{ $i } . '/';
- }
- $file .= "image_{$salt}_{$hash}.png";
-
- return $file;
- }
-
- /**
- * @param $basename string
- * @return Array (salt, hash)
- * @throws MWException
- */
- public function hashFromImageName( $basename ) {
- if ( preg_match( '/^image_([0-9a-f]+)_([0-9a-f]+)\\.png$/', $basename, $matches ) ) {
- return array( $matches[1], $matches[2] );
- } else {
- throw new MWException( "Invalid filename '$basename'.\n" );
- }
- }