3 if ( defined( 'MEDIAWIKI' ) ) {
5 global $wgCaptchaDirectory;
6 $wgCaptchaDirectory = "$wgUploadDirectory/captcha"; // bad default :D
8 global $wgCaptchaSecret;
9 $wgCaptchaSecret = "CHANGE_THIS_SECRET!";
12 class FancyCaptcha extends SimpleCaptcha {
14 global $wgRequest, $wgCaptchaSecret;
16 if( !isset( $_SESSION['ceAnswerVar'] ) ) {
17 wfDebug( "FancyCaptcha: no session captcha key set, this is new visitor.\n" );
21 $var = @$_SESSION['ceAnswerVar'];
22 $salt = @$_SESSION['captchaSalt'];
23 $hash = @$_SESSION['captchaHash'];
25 $answer = $wgRequest->getVal( $var );
26 $digest = $wgCaptchaSecret . $salt . $answer . $wgCaptchaSecret . $salt;
27 $answerHash = substr( md5( $digest ), 0, 16 );
29 if( $answerHash == $hash ) {
30 wfDebug( "FancyCaptcha: answer hash matches expected $hash\n" );
33 wfDebug( "FancyCaptcha: answer hashes to $answerHash, expected $hash\n" );
39 * Insert the captcha prompt into the edit form.
41 function formCallback( &$out ) {
42 $dest = 'wpCaptchaWord' . mt_rand();
44 $img = $this->pickImage();
46 die( "out of captcha images; this shouldn't happen" );
49 $_SESSION['ceAnswerVar'] = $dest;
50 $_SESSION['captchaHash'] = $img['hash'];
51 $_SESSION['captchaSalt'] = $img['salt'];
52 $_SESSION['captchaViewed'] = false;
53 wfDebug( "Picked captcha with hash ${img['hash']}, salt ${img['salt']}.\n" );
55 $title = Title::makeTitle( NS_SPECIAL, 'Captcha/image' );
57 $out->addWikiText( wfMsg( "captcha-short" ) );
58 $out->addHTML( "<p>" .
59 wfElement( 'img', array(
60 'src' => $title->getLocalUrl(),
61 'width' => $img['width'],
62 'height' => $img['height'],
65 "<p><input name=\"$dest\" id=\"$dest\" tabindex=\"1\" /></p>" );
69 * Select a previously generated captcha image from the queue.
70 * @fixme subject to race conditions if lots of files vanish
71 * @return mixed tuple of (salt key, text hash) or false if no image to find
73 function pickImage() {
74 global $wgCaptchaDirectory;
75 $pick = mt_rand( 0, $this->countFiles( $wgCaptchaDirectory ) );
76 $dir = opendir( $wgCaptchaDirectory );
78 $n = mt_rand( 0, 16 );
81 $entry = readdir( $dir );
83 while( false !== $entry ) {
84 $entry = readdir( $dir );
85 if( preg_match( '/^image_([0-9a-f]+)_([0-9a-f]+)\\.png$/', $entry, $matches ) ) {
86 $size = getimagesize( "$wgCaptchaDirectory/$entry" );
88 'salt' => $matches[1],
89 'hash' => $matches[2],
93 if( $count++ == $n ) {
103 * Count the number of files in a directory.
106 function countFiles( $dirname ) {
107 $dir = opendir( $dirname );
109 while( false !== ($entry = readdir( $dir ) ) ) {
110 if( $dir != '.' && $dir != '..' ) {
118 function showImage() {
121 if( !empty( $_SESSION['captchaViewed'] ) ) {
122 wfHttpError( 403, 'Access Forbidden', "Can't view captcha image a second time." );
125 $_SESSION['captchaViewed'] = wfTimestamp();
127 if( isset( $_SESSION['captchaSalt'] ) ) {
128 $salt = $_SESSION['captchaSalt'];
129 if( isset( $_SESSION['captchaHash'] ) ) {
130 $hash = $_SESSION['captchaHash'];
132 global $wgCaptchaDirectory;
133 $file = $wgCaptchaDirectory . DIRECTORY_SEPARATOR . "image_{$salt}_{$hash}.png";
134 if( file_exists( $file ) ) {
135 header( 'Content-type: image/png' );
140 wfHttpError( 500, 'Internal Error', 'Requested bogus captcha image' );
145 } # End invocation guard