PageRenderTime 43ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/extensions/ConfirmEdit/FancyCaptcha.class.php

https://github.com/ChuguluGames/mediawiki-svn
PHP | 249 lines | 172 code | 29 blank | 48 comment | 29 complexity | 190162d862f56f09f09ee40dcc864609 MD5 | raw file
  1. <?php
  2. class FancyCaptcha extends SimpleCaptcha {
  3. /**
  4. * Check if the submitted form matches the captcha session data provided
  5. * by the plugin when the form was generated.
  6. *
  7. * @param string $answer
  8. * @param array $info
  9. * @return bool
  10. */
  11. function keyMatch( $answer, $info ) {
  12. global $wgCaptchaSecret;
  13. $digest = $wgCaptchaSecret . $info['salt'] . $answer . $wgCaptchaSecret . $info['salt'];
  14. $answerHash = substr( md5( $digest ), 0, 16 );
  15. if ( $answerHash == $info['hash'] ) {
  16. wfDebug( "FancyCaptcha: answer hash matches expected {$info['hash']}\n" );
  17. return true;
  18. } else {
  19. wfDebug( "FancyCaptcha: answer hashes to $answerHash, expected {$info['hash']}\n" );
  20. return false;
  21. }
  22. }
  23. function addCaptchaAPI( &$resultArr ) {
  24. $info = $this->pickImage();
  25. if ( !$info ) {
  26. $resultArr['captcha']['error'] = 'Out of images';
  27. return;
  28. }
  29. $index = $this->storeCaptcha( $info );
  30. $title = SpecialPage::getTitleFor( 'Captcha', 'image' );
  31. $resultArr['captcha']['type'] = 'image';
  32. $resultArr['captcha']['mime'] = 'image/png';
  33. $resultArr['captcha']['id'] = $index;
  34. $resultArr['captcha']['url'] = $title->getLocalUrl( 'wpCaptchaId=' . urlencode( $index ) );
  35. }
  36. /**
  37. * Insert the captcha prompt into the edit form.
  38. */
  39. function getForm() {
  40. $info = $this->pickImage();
  41. if ( !$info ) {
  42. throw new MWException( "Ran out of captcha images" );
  43. }
  44. // Generate a random key for use of this captcha image in this session.
  45. // This is needed so multiple edits in separate tabs or windows can
  46. // go through without extra pain.
  47. $index = $this->storeCaptcha( $info );
  48. wfDebug( "Captcha id $index using hash ${info['hash']}, salt ${info['salt']}.\n" );
  49. $title = SpecialPage::getTitleFor( 'Captcha', 'image' );
  50. return "<p>" .
  51. Xml::element( 'img', array(
  52. 'src' => $title->getLocalUrl( 'wpCaptchaId=' . urlencode( $index ) ),
  53. 'width' => $info['width'],
  54. 'height' => $info['height'],
  55. 'alt' => '' ) ) .
  56. "</p>\n" .
  57. Xml::element( 'input', array(
  58. 'type' => 'hidden',
  59. 'name' => 'wpCaptchaId',
  60. 'id' => 'wpCaptchaId',
  61. 'value' => $index ) ) .
  62. "<p>" .
  63. Xml::element( 'input', array(
  64. 'name' => 'wpCaptchaWord',
  65. 'id' => 'wpCaptchaWord',
  66. 'tabindex' => 1 ) ) . // tab in before the edit textarea
  67. "</p>\n";
  68. }
  69. /**
  70. * Select a previously generated captcha image from the queue.
  71. * @fixme subject to race conditions if lots of files vanish
  72. * @return mixed tuple of (salt key, text hash) or false if no image to find
  73. */
  74. function pickImage() {
  75. global $wgCaptchaDirectory, $wgCaptchaDirectoryLevels;
  76. return $this->pickImageDir(
  77. $wgCaptchaDirectory,
  78. $wgCaptchaDirectoryLevels );
  79. }
  80. function pickImageDir( $directory, $levels ) {
  81. if ( $levels ) {
  82. $dirs = array();
  83. // Check which subdirs are actually present...
  84. $dir = opendir( $directory );
  85. while ( false !== ( $entry = readdir( $dir ) ) ) {
  86. if ( ctype_xdigit( $entry ) && strlen( $entry ) == 1 ) {
  87. $dirs[] = $entry;
  88. }
  89. }
  90. closedir( $dir );
  91. $place = mt_rand( 0, count( $dirs ) - 1 );
  92. // In case all dirs are not filled,
  93. // cycle through next digits...
  94. for ( $j = 0; $j < count( $dirs ); $j++ ) {
  95. $char = $dirs[( $place + $j ) % count( $dirs )];
  96. $return = $this->pickImageDir( "$directory/$char", $levels - 1 );
  97. if ( $return ) {
  98. return $return;
  99. }
  100. }
  101. // Didn't find any images in this directory... empty?
  102. return false;
  103. } else {
  104. return $this->pickImageFromDir( $directory );
  105. }
  106. }
  107. function pickImageFromDir( $directory ) {
  108. if ( !is_dir( $directory ) ) {
  109. return false;
  110. }
  111. $n = mt_rand( 0, $this->countFiles( $directory ) - 1 );
  112. $dir = opendir( $directory );
  113. $count = 0;
  114. $entry = readdir( $dir );
  115. $pick = false;
  116. while ( false !== $entry ) {
  117. $entry = readdir( $dir );
  118. if ( preg_match( '/^image_([0-9a-f]+)_([0-9a-f]+)\\.png$/', $entry, $matches ) ) {
  119. $size = getimagesize( "$directory/$entry" );
  120. $pick = array(
  121. 'salt' => $matches[1],
  122. 'hash' => $matches[2],
  123. 'width' => $size[0],
  124. 'height' => $size[1],
  125. 'viewed' => false,
  126. );
  127. if ( $count++ == $n ) {
  128. break;
  129. }
  130. }
  131. }
  132. closedir( $dir );
  133. return $pick;
  134. }
  135. /**
  136. * Count the number of files in a directory.
  137. * @return int
  138. */
  139. function countFiles( $dirname ) {
  140. $dir = opendir( $dirname );
  141. $count = 0;
  142. while ( false !== ( $entry = readdir( $dir ) ) ) {
  143. if ( $entry != '.' && $entry != '..' ) {
  144. $count++;
  145. }
  146. }
  147. closedir( $dir );
  148. return $count;
  149. }
  150. function showImage() {
  151. global $wgOut;
  152. $wgOut->disable();
  153. $info = $this->retrieveCaptcha();
  154. if ( $info ) {
  155. /*
  156. // Be a little less restrictive for now; in at least some circumstances,
  157. // Konqueror tries to reload the image even if you haven't navigated
  158. // away from the page.
  159. if( $info['viewed'] ) {
  160. wfHttpError( 403, 'Access Forbidden', "Can't view captcha image a second time." );
  161. return false;
  162. }
  163. */
  164. $info['viewed'] = wfTimestamp();
  165. $this->storeCaptcha( $info );
  166. $salt = $info['salt'];
  167. $hash = $info['hash'];
  168. $file = $this->imagePath( $salt, $hash );
  169. if ( file_exists( $file ) ) {
  170. global $IP;
  171. require_once "$IP/includes/StreamFile.php";
  172. header( "Cache-Control: private, s-maxage=0, max-age=3600" );
  173. wfStreamFile( $file );
  174. return true;
  175. }
  176. }
  177. wfHttpError( 500, 'Internal Error', 'Requested bogus captcha image' );
  178. return false;
  179. }
  180. function imagePath( $salt, $hash ) {
  181. global $wgCaptchaDirectory, $wgCaptchaDirectoryLevels;
  182. $file = $wgCaptchaDirectory;
  183. $file .= DIRECTORY_SEPARATOR;
  184. for ( $i = 0; $i < $wgCaptchaDirectoryLevels; $i++ ) {
  185. $file .= $hash { $i } ;
  186. $file .= DIRECTORY_SEPARATOR;
  187. }
  188. $file .= "image_{$salt}_{$hash}.png";
  189. return $file;
  190. }
  191. /**
  192. * Show a message asking the user to enter a captcha on edit
  193. * The result will be treated as wiki text
  194. *
  195. * @param $action Action being performed
  196. * @return string
  197. */
  198. function getMessage( $action ) {
  199. $name = 'fancycaptcha-' . $action;
  200. $text = wfMsg( $name );
  201. # Obtain a more tailored message, if possible, otherwise, fall back to
  202. # the default for edits
  203. return wfEmptyMsg( $name, $text ) ? wfMsg( 'fancycaptcha-edit' ) : $text;
  204. }
  205. /**
  206. * Delete a solved captcha image, if $wgCaptchaDeleteOnSolve is true.
  207. */
  208. function passCaptcha() {
  209. global $wgCaptchaDeleteOnSolve;
  210. $info = $this->retrieveCaptcha(); // get the captcha info before it gets deleted
  211. $pass = parent::passCaptcha();
  212. if ( $pass && $wgCaptchaDeleteOnSolve ) {
  213. $filename = $this->imagePath( $info['salt'], $info['hash'] );
  214. if ( file_exists( $filename ) ) {
  215. unlink( $filename );
  216. }
  217. }
  218. return $pass;
  219. }
  220. }