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

/app/code/community/Fontis/Recaptcha/Helper/Data.php

https://bitbucket.org/acidel/buykoala
PHP | 178 lines | 92 code | 19 blank | 67 comment | 28 complexity | 60776da03571a72de80a45d6f56b1fc6 MD5 | raw file
  1. <?php
  2. /**
  3. * Fontis Recaptcha Extension
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the Open Software License (OSL 3.0)
  8. * that is bundled with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://opensource.org/licenses/osl-3.0.php
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@magentocommerce.com so we can send you a copy immediately.
  14. *
  15. * This code has been adopted from the reCAPTCHA module available at:
  16. * http://www.google.com/recaptcha
  17. * The original reCAPTCHA module was written by:
  18. * Mike Crawford
  19. * Ben Maurer
  20. *
  21. * @category Fontis
  22. * @package Fontis_Recaptcha
  23. * @author Denis Margetic
  24. * @author Chris Norton
  25. * @copyright Copyright (c) 2011 Fontis Pty. Ltd. (http://www.fontis.com.au)
  26. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  27. */
  28. class Fontis_Recaptcha_Helper_Data extends Mage_Core_Helper_Abstract
  29. {
  30. const RECAPTCHA_API_SERVER_HOST = "www.google.com";
  31. const RECAPTCHA_API_SERVER_PATH = "/recaptcha/api";
  32. const RECAPTCHA_API_SECURE_SERVER = "https://www.google.com/recaptcha/api";
  33. /**
  34. * Encodes the given data into a query string format
  35. * @param $data - array of string elements to be encoded
  36. * @return string - encoded request
  37. */
  38. function _recaptcha_qsencode ($data)
  39. {
  40. $req = "";
  41. foreach ( $data as $key => $value )
  42. $req .= $key . '=' . urlencode( stripslashes($value) ) . '&';
  43. // Cut the last '&'
  44. $req=substr($req,0,strlen($req)-1);
  45. return $req;
  46. }
  47. /**
  48. * Submits an HTTP POST to a reCAPTCHA server
  49. * @param string $host
  50. * @param string $path
  51. * @param array $data
  52. * @param int port
  53. * @return array response
  54. */
  55. function _recaptcha_http_post($host, $path, $data, $port = 80)
  56. {
  57. $req = $this->_recaptcha_qsencode ($data);
  58. $http_request = "POST $path HTTP/1.0\r\n";
  59. $http_request .= "Host: $host\r\n";
  60. $http_request .= "Content-Type: application/x-www-form-urlencoded;\r\n";
  61. $http_request .= "Content-Length: " . strlen($req) . "\r\n";
  62. $http_request .= "User-Agent: reCAPTCHA/PHP\r\n";
  63. $http_request .= "\r\n";
  64. $http_request .= $req;
  65. $response = '';
  66. if( false == ( $fs = @fsockopen($host, $port, $errno, $errstr, 10) ) ) {
  67. die ('Could not open socket');
  68. }
  69. fwrite($fs, $http_request);
  70. while ( !feof($fs) )
  71. $response .= fgets($fs, 1160); // One TCP-IP packet
  72. fclose($fs);
  73. $response = explode("\r\n\r\n", $response, 2);
  74. return $response;
  75. }
  76. /**
  77. * Gets the challenge HTML (javascript and non-javascript version).
  78. * This is called from the browser, and the resulting reCAPTCHA HTML widget
  79. * is embedded within the HTML form it was called from.
  80. * @param string $pubkey A public key for reCAPTCHA
  81. * @param string $error The error given by reCAPTCHA (optional, default is null)
  82. * @param boolean $use_ssl Should the request be made over ssl? (optional, default is false)
  83. * @return string - The HTML to be embedded in the user's form.
  84. */
  85. function recaptcha_get_html ($pubkey, $error = null, $use_ssl = false)
  86. {
  87. if ($pubkey == null || $pubkey == '') {
  88. die ("To use reCAPTCHA you must get an API key from <a href='http://recaptcha.net/api/getkey'>http://recaptcha.net/api/getkey</a>");
  89. }
  90. if ($use_ssl) {
  91. $server = self::RECAPTCHA_API_SECURE_SERVER;
  92. } else {
  93. $server = 'http://' . self::RECAPTCHA_API_SERVER_HOST . self::RECAPTCHA_API_SERVER_PATH;
  94. }
  95. $errorpart = "";
  96. if ($error) {
  97. $errorpart = "&amp;error=" . $error;
  98. }
  99. return '<script type="text/javascript" src="'. $server . '/challenge?k=' . $pubkey . $errorpart . '"></script>
  100. <noscript>
  101. <iframe src="'. $server . '/noscript?k=' . $pubkey . $errorpart . '" height="300" width="500" frameborder="0"></iframe><br/>
  102. <textarea name="recaptcha_challenge_field" rows="3" cols="40"></textarea>
  103. <input type="hidden" name="recaptcha_response_field" value="manual_challenge"/>
  104. </noscript>';
  105. }
  106. /**
  107. * Calls an HTTP POST function to verify if the user's guess was correct
  108. * @param string $privkey
  109. * @param string $remoteip
  110. * @param string $challenge
  111. * @param string $response
  112. * @param array $extra_params an array of extra variables to post to the server
  113. * @return ReCaptchaResponse
  114. */
  115. function recaptcha_check_answer ($privkey, $remoteip, $challenge, $response, $extra_params = array())
  116. {
  117. if ($privkey == null || $privkey == '') {
  118. die ("To use reCAPTCHA you must get an API key from <a href='http://recaptcha.net/api/getkey'>http://recaptcha.net/api/getkey</a>");
  119. }
  120. if ($remoteip == null || $remoteip == '') {
  121. die ("For security reasons, you must pass the remote ip to reCAPTCHA");
  122. }
  123. //discard spam submissions
  124. if ($challenge == null || strlen($challenge) == 0 || $response == null || strlen($response) == 0) {
  125. return false;
  126. }
  127. $response = $this->_recaptcha_http_post (self::RECAPTCHA_API_SERVER_HOST, self::RECAPTCHA_API_SERVER_PATH . "/verify", array ( 'privatekey' => $privkey,
  128. 'remoteip' => $remoteip,
  129. 'challenge' => $challenge,
  130. 'response' => $response
  131. ) + $extra_params
  132. );
  133. $answers = explode ("\n", $response [1]);
  134. if (trim ($answers [0]) == 'true') {
  135. return true;
  136. }
  137. return false;
  138. }
  139. /**
  140. * gets a URL where the user can sign up for reCAPTCHA. If your application
  141. * has a configuration page where you enter a key, you should provide a link
  142. * using this function.
  143. * @param string $domain The domain where the page is hosted
  144. * @param string $appname The name of your application
  145. */
  146. function recaptcha_get_signup_url ($domain = null, $appname = null)
  147. {
  148. return "http://recaptcha.net/api/getkey?" . $this->_recaptcha_qsencode (array ('domain' => $domain, 'app' => $appname));
  149. }
  150. function _recaptcha_aes_pad($val)
  151. {
  152. $block_size = 16;
  153. $numpad = $block_size - (strlen ($val) % $block_size);
  154. return str_pad($val, strlen ($val) + $numpad, chr($numpad));
  155. }
  156. }
  157. ?>