RecaptchaPlugin.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. /**
  3. * StatusNet, the distributed open-source microblogging tool
  4. *
  5. * Plugin to show reCaptcha when a user registers
  6. *
  7. * PHP version 5
  8. *
  9. * LICENCE: This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License as published by
  11. * the Free Software Foundation, either version 3 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. * @category Plugin
  23. * @package StatusNet
  24. * @author Eric Helgeson <erichelgeson@gmail.com>
  25. * @copyright 2009
  26. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  27. * @link http://status.net/
  28. */
  29. if (!defined('STATUSNET') && !defined('LACONICA')) {
  30. exit(1);
  31. }
  32. require_once(INSTALLDIR.'/plugins/Recaptcha/recaptchalib.php');
  33. class RecaptchaPlugin extends Plugin
  34. {
  35. var $private_key;
  36. var $public_key;
  37. var $display_errors;
  38. var $failed;
  39. var $ssl;
  40. function onInitializePlugin()
  41. {
  42. if(!isset($this->private_key)) {
  43. common_log(LOG_ERR, 'Recaptcha: Must specify private_key in config.php');
  44. }
  45. if(!isset($this->public_key)) {
  46. common_log(LOG_ERR, 'Recaptcha: Must specify public_key in config.php');
  47. }
  48. }
  49. function onEndRegistrationFormData($action)
  50. {
  51. $action->elementStart('li');
  52. // TRANS: Field label.
  53. $action->raw('<label for="recaptcha">'._m('Captcha').'</label>');
  54. // AJAX API will fill this div out.
  55. // We're calling that instead of the regular one so we stay compatible
  56. // with application/xml+xhtml output as for mobile.
  57. $action->element('div', array('id' => 'recaptcha'));
  58. $action->elementEnd('li');
  59. $action->recaptchaPluginNeedsOutput = true;
  60. return true;
  61. }
  62. function onEndShowScripts($action)
  63. {
  64. if (isset($action->recaptchaPluginNeedsOutput) && $action->recaptchaPluginNeedsOutput) {
  65. // Load the AJAX API
  66. if (StatusNet::isHTTPS()) {
  67. $url = "https://www.google.com/recaptcha/api/js/recaptcha_ajax.js";
  68. } else {
  69. $url = "http://www.google.com/recaptcha/api/js/recaptcha_ajax.js";
  70. }
  71. $action->script($url);
  72. // And when we're ready, fill out the captcha!
  73. $key = json_encode($this->public_key);
  74. $action->inlinescript("\$(function(){Recaptcha.create($key, 'recaptcha');});");
  75. }
  76. return true;
  77. }
  78. function onStartRegistrationTry($action)
  79. {
  80. $resp = recaptcha_check_answer ($this->private_key,
  81. $_SERVER["REMOTE_ADDR"],
  82. $action->trimmed('recaptcha_challenge_field'),
  83. $action->trimmed('recaptcha_response_field'));
  84. if (!$resp->is_valid) {
  85. if($this->display_errors) {
  86. // TRANS: Error message displayed if there is in error communicating with the
  87. // TRANS: reCAPTCHA server. %s is the error.
  88. $action->showForm(sprintf(_m('(reCAPTCHA error: %s)', $resp->error)));
  89. }
  90. // TRANS: Error message displayed if a provided captcha response does not match.
  91. $action->showForm(_m('Captcha does not match!'));
  92. return false;
  93. }
  94. }
  95. function onPluginVersion(&$versions)
  96. {
  97. $versions[] = array('name' => 'Recaptcha',
  98. 'version' => GNUSOCIAL_VERSION,
  99. 'author' => 'Eric Helgeson',
  100. 'homepage' => 'http://status.net/wiki/Plugin:Recaptcha',
  101. 'rawdescription' =>
  102. // TRANS: Plugin description.
  103. _m('Uses <a href="http://recaptcha.org/">Recaptcha</a> service to add a '.
  104. 'captcha to the registration page.'));
  105. return true;
  106. }
  107. }