PageRenderTime 69ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/Croogo/View/Helper/RecaptchaHelper.php

http://github.com/croogo/croogo
PHP | 126 lines | 81 code | 19 blank | 26 comment | 23 complexity | a1220220e76c73ef089ad5d27aac313b MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. App::uses('AppHelper', 'View/Helper');
  3. /**
  4. * @package Croogo.Croogo.View.Helper
  5. * @link http://bakery.cakephp.org/articles/view/recaptcha-component-helper-for-cakephp
  6. */
  7. class RecaptchaHelper extends AppHelper {
  8. public $helpers = array('Form');
  9. public function display_form($output_method = 'return', $error = null, $use_ssl = false) {
  10. $this->Form->unlockField('recaptcha_challenge_field');
  11. $this->Form->unlockField('recaptcha_response_field');
  12. $data = $this->__form(Configure::read("Recaptcha.pubKey"),$error,$use_ssl);
  13. if ($output_method == "echo")
  14. echo $data;
  15. else
  16. return $data;
  17. }
  18. public function hide_mail($email = '',$output_method = 'return') {
  19. $data = $this->recaptcha_mailhide_html(Configure::read('Recaptcha.pubKey'), Configure::read('Recaptcha.privateKey'), $email);
  20. if ($output_method == "echo")
  21. echo $data;
  22. else
  23. return $data;
  24. }
  25. /**
  26. * Gets the challenge HTML (javascript and non-javascript version).
  27. * This is called from the browser, and the resulting reCAPTCHA HTML widget
  28. * is embedded within the HTML form it was called from.
  29. * @param string $pubkey A public key for reCAPTCHA
  30. * @param string $error The error given by reCAPTCHA (optional, default is null)
  31. * @param boolean $use_ssl Should the request be made over ssl? (optional, default is false)
  32. * @return string - The HTML to be embedded in the user's form.
  33. */
  34. private function __form($pubkey, $error = null, $use_ssl = false) {
  35. if ($pubkey == null || $pubkey == '') {
  36. die ("To use reCAPTCHA you must get an API key from <a href='http://recaptcha.net/api/getkey'>http://recaptcha.net/api/getkey</a>");
  37. }
  38. if ($use_ssl) {
  39. $server = Configure::read('Recaptcha.apiSecureServer');
  40. } else {
  41. $server = Configure::read('Recaptcha.apiServer');
  42. }
  43. $errorpart = "";
  44. if ($error) {
  45. $errorpart = "&amp;error=" . $error;
  46. }
  47. return '<script type="text/javascript" src="'. $server . '/challenge?k=' . $pubkey . $errorpart . '"></script>
  48. <noscript>
  49. <iframe src="'. $server . '/noscript?k=' . $pubkey . $errorpart . '" height="300" width="500" frameborder="0"></iframe><br/>
  50. <textarea name="recaptcha_challenge_field" rows="3" cols="40"></textarea>
  51. <input type="hidden" name="recaptcha_response_field" value="manual_challenge"/>
  52. <input type="hidden" name="recaptcha_response_field" value="manual_challenge"/>
  53. </noscript>';
  54. }
  55. /* Mailhide related code */
  56. protected function _recaptcha_aes_encrypt($val,$ky) {
  57. if (! function_exists ("mcrypt_encrypt")) {
  58. die ("To use reCAPTCHA Mailhide, you need to have the mcrypt php module installed.");
  59. }
  60. $mode=MCRYPT_MODE_CBC;
  61. $enc=MCRYPT_RIJNDAEL_128;
  62. $val=$this->_recaptcha_aes_pad($val);
  63. return mcrypt_encrypt($enc, $ky, $val, $mode, "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0");
  64. }
  65. protected function _recaptcha_mailhide_urlbase64($x) {
  66. return strtr(base64_encode ($x), '+/', '-_');
  67. }
  68. /* gets the reCAPTCHA Mailhide url for a given email, public key and private key */
  69. public function recaptcha_mailhide_url($pubkey, $privkey, $email) {
  70. if ($pubkey == '' || $pubkey == null || $privkey == "" || $privkey == null) {
  71. die ("To use reCAPTCHA Mailhide, you have to sign up for a public and private key, " .
  72. "you can do so at <a href='http://mailhide.recaptcha.net/apikey'>http://mailhide.recaptcha.net/apikey</a>");
  73. }
  74. $ky = pack('H*', $privkey);
  75. $cryptmail = $this->_recaptcha_aes_encrypt ($email, $ky);
  76. return "http://mailhide.recaptcha.net/d?k=" . $pubkey . "&c=" . $this->_recaptcha_mailhide_urlbase64 ($cryptmail);
  77. }
  78. /**
  79. * gets the parts of the email to expose to the user.
  80. * eg, given johndoe@example,com return ["john", "example.com"].
  81. * the email is then displayed as john...@example.com
  82. */
  83. protected function _recaptcha_mailhide_email_parts($email) {
  84. $arr = preg_split("/@/", $email );
  85. if (strlen ($arr[0]) <= 4) {
  86. $arr[0] = substr ($arr[0], 0, 1);
  87. } elseif (strlen ($arr[0]) <= 6) {
  88. $arr[0] = substr ($arr[0], 0, 3);
  89. } else {
  90. $arr[0] = substr ($arr[0], 0, 4);
  91. }
  92. return $arr;
  93. }
  94. /**
  95. * Gets html to display an email address given a public an private key.
  96. * to get a key, go to:
  97. *
  98. * http://mailhide.recaptcha.net/apikey
  99. */
  100. public function recaptcha_mailhide_html($pubkey, $privkey, $email) {
  101. $emailparts = $this->_recaptcha_mailhide_email_parts ($email);
  102. $url = $this->recaptcha_mailhide_url ($pubkey, $privkey, $email);
  103. return htmlentities($emailparts[0]) . "<a href='" . htmlentities ($url) .
  104. "' onclick=\"window.open('" . htmlentities ($url) . "', '', 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width=500,height=300'); return false;\" title=\"Reveal this e-mail address\">...</a>@" . htmlentities ($emailparts [1]);
  105. }
  106. }