r76547 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r76546‎ | r76547 | r76548 >
Date:23:16, 11 November 2010
Author:awjrichards
Status:deferred
Tags:
Comment:
Added support to recaptcha library for curl and using an http proxy
Modified paths:
  • /trunk/extensions/DonationInterface/payflowpro_gateway/extras/recaptcha/recaptcha-php/recaptchalib.php (modified) (history)
  • /trunk/extensions/DonationInterface/payflowpro_gateway/extras/recaptcha/recaptcha.php (modified) (history)

Diff [purge]

Index: trunk/extensions/DonationInterface/payflowpro_gateway/extras/recaptcha/recaptcha-php/recaptchalib.php
@@ -1,4 +1,14 @@
22 <?php
 3+/**
 4+ * This is a modified PHP library that handles calling reCaptcha
 5+ *
 6+ * This is /different/ from the original PHP API for reCAPTCHA.
 7+ * This version supports cURL and using an http proxy for network
 8+ * communications and takes some settings set by MediaWiki.
 9+ *
 10+ * See below for original license and other info.
 11+ */
 12+
313 /*
414 * This is a PHP library that handles calling reCAPTCHA.
515 * - Documentation and latest version
@@ -40,6 +50,19 @@
4151 define( "RECAPTCHA_VERIFY_SERVER", "www.google.com" );
4252
4353 /**
 54+ * Proxy settings
 55+ */
 56+define( "RECAPTCHA_USE_HTTP_PROXY", $wgPayflowRecaptchaUseHTTPProxy );
 57+define( "RECAPTCHA_HTTP_PROXY", $wgPayflowRecaptchaHTTPProxy );
 58+
 59+/**
 60+ * Other reCAPTCHA settings
 61+ */
 62+define( "RECAPTCHA_TIMEOUT", $wgPayflowRecaptchaTimeout );
 63+define( "RECAPTCHA_PROTOCOL", $wgProto ); //http or https
 64+define( "RECAPTCHA_RETRY_LIMIT", $wgPayflowRecaptchaComsRetryLimit );
 65+
 66+/**
4467 * Encodes the given data into a query string format
4568 * @param $data - array of string elements to be encoded
4669 * @return string - encoded request
@@ -57,7 +80,7 @@
5881
5982
6083 /**
61 - * Submits an HTTP POST to a reCAPTCHA server
 84+ * Wrapper to submit an HTTP POST to a reCAPTCHA server
6285 * @param string $host
6386 * @param string $path
6487 * @param array $data
@@ -68,32 +91,104 @@
6992
7093 $req = _recaptcha_qsencode ( $data );
7194
72 - $http_request = "POST $path HTTP/1.0\r\n";
73 - $http_request .= "Host: $host\r\n";
74 - $http_request .= "Content-Type: application/x-www-form-urlencoded;\r\n";
75 - $http_request .= "Content-Length: " . strlen( $req ) . "\r\n";
76 - $http_request .= "User-Agent: reCAPTCHA/PHP\r\n";
77 - $http_request .= "\r\n";
78 - $http_request .= $req;
79 -
80 - $response = '';
81 - if ( false == ( $fs = @fsockopen( $host, $port, $errno, $errstr, 10 ) ) ) {
82 - die ( 'Could not open socket' );
 95+ if ( !extension_loaded( 'curl' ) ) {
 96+ $response = _recaptcha_http_post_fsock( $host, $path, $req, $port );
 97+ } else {
 98+ $response = _recaptcha_http_post_curl( $host, $path, $req, $port );
8399 }
84 -
85 - fwrite( $fs, $http_request );
86 -
87 - while ( !feof( $fs ) )
88 - $response .= fgets( $fs, 1160 ); // One TCP-IP packet
89 - fclose( $fs );
 100+
90101 $response = explode( "\r\n\r\n", $response, 2 );
91102
92103 return $response;
93104 }
94105
 106+/**
 107+ * Submits an HTTP POST to a reCAPTCHA server using fsockopen()
 108+ * @param $host
 109+ * @param $path
 110+ * @param $data
 111+ * @param int $port
 112+ * @return array response
 113+ */
 114+function _recaptcha_http_post_fsock( $host, $path, $data, $port = 80 ) {
 115+
 116+ $http_request = "POST $path HTTP/1.0\r\n";
 117+ $http_request .= "Host: $host\r\n";
 118+ $http_request .= "Content-Type: application/x-www-form-urlencoded;\r\n";
 119+ $http_request .= "Content-Length: " . strlen( $req ) . "\r\n";
 120+ $http_request .= "User-Agent: reCAPTCHA/PHP\r\n";
 121+ $http_request .= "\r\n";
 122+ $http_request .= $req;
95123
 124+ $response = '';
 125+ if ( false == ( $fs = @fsockopen( $host, $port, $errno, $errstr, 10 ) ) ) {
 126+ die ( 'Could not open socket' );
 127+ }
96128
 129+ fwrite( $fs, $http_request );
 130+
 131+ while ( !feof( $fs ) )
 132+ $response .= fgets( $fs, 1160 ); // One TCP-IP packet
 133+ fclose( $fs );
 134+
 135+ return $response;
 136+}
 137+
97138 /**
 139+ * Submits an HTTP POST to a reCAPTCHA server using cURL
 140+ * @param $host
 141+ * @param $path
 142+ * @param $data
 143+ * @param int $port
 144+ * @return array response
 145+ */
 146+function _recaptcha_http_post_curl( $host, $path, $data, $port = 80 ) {
 147+ $url = RECAPTCHA_PROTOCOL . "://" . $host . ":" . $port;
 148+
 149+ $ch = curl_init( $url );
 150+ curl_setopt( $ch, CURLOPT_POST, true );
 151+ curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
 152+ curl_setopt( $ch, CURLOPT_TIMEOUT, RECAPTCHA_TIMEOUT );
 153+ curl_setopt( $ch, CURLOPT_USERAGENT, 'reCAPTCHA/PHP' );
 154+ curl_setopt($curl, CURLOPT_POSTFIELDS, $data );
 155+ curl_setopt($curl, CURLOPT_HEADER, true );
 156+ curl_setopt($curl, CURLOPT_HTTPHEADER, array( "Host: " . $host ) );
 157+
 158+ // set proxy settings if necessary
 159+ if ( RECAPTCHA_USE_HTTP_PROXY ) {
 160+ curl_setopt( $ch, CURLOPT_HTTPPROXYTUNNEL, 1 );
 161+ curl_setopt( $ch, CURLOPT_PROXY, RECAPTCHA_HTTP_PROXY );
 162+ }
 163+
 164+ // try up to three times
 165+ for ( $i = 0; $i < RECAPTCHA_RETRY_LIMIT; $i++ ) {
 166+ wfDebug( 'payflowpro_gateway', 'Preparing to communicate with reCaptcha.' );
 167+ $response = curl_exec( $ch );
 168+ wfDebug( 'payflowpro_gateway', "Finished communicating with reCaptcha." );
 169+ if ( $response ) {
 170+ wfDebug( 'payflowpro_gateway', 'Response from reCaptcha: ' . $response );
 171+ break;
 172+ }
 173+ }
 174+
 175+ /**
 176+ * This is a nasty hack to pretend like things worked when they really didn't
 177+ *
 178+ * Doing this per instructions from the fundraisers to accept a trxn when we
 179+ * are unsuccesful communicating with reCaptcha.
 180+ *
 181+ * This sets $response to a message that will fool reCaptcha (on our end) into thinking
 182+ * the user entered the correct values.
 183+ */
 184+ if ( !$response ) {
 185+ wfDebug( 'payflowpro_gateway', 'Failed communicating with reCaptcha: ' . curl_error( $ch ) );
 186+ $response = "true\r\n\r\ntrue";
 187+ }
 188+
 189+ return $response;
 190+}
 191+
 192+/**
98193 * Gets the challenge HTML (javascript and non-javascript version).
99194 * This is called from the browser, and the resulting reCAPTCHA HTML widget
100195 * is embedded within the HTML form it was called from.
Index: trunk/extensions/DonationInterface/payflowpro_gateway/extras/recaptcha/recaptcha.php
@@ -26,6 +26,23 @@
2727 $wgPayflowRecaptchaPublicKey = '';
2828 $wgPayflowRecaptchaPrivateKey = '';
2929
 30+// Timeout (in seconds) for communicating with reCatpcha
 31+$wgPayflowRecaptchaTimeout = 2;
 32+
 33+/**
 34+ * HTTP Proxy settings
 35+ *
 36+ * Default to settings in PayflowPro Gateway
 37+ */
 38+$wgPayflowRecaptchaUseHTTPProxy = $wgPayflowGatewayUseHTTPProxy;
 39+$wgPayflowRecaptchaHTTPProxy = $wgPayflowGatewayHTTPProxy;
 40+
 41+/**
 42+ * The # of times to retry communicating with reCaptcha if communication fails
 43+ * @var int
 44+ */
 45+$wgPayflowRecaptchaComsRetryLimit = 3;
 46+
3047 $dir = dirname( __FILE__ ) . "/";
3148 $wgAutoloadClasses['PayflowProGateway_Extras_ReCaptcha'] = $dir . "recaptcha.body.php";
3249

Status & tagging log