SimpleCaptchaPlugin.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. <?php
  2. /*
  3. * GNU Social - a federating social network
  4. * Copyright (C) 2014, Free Software Foundation, Inc.
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU Affero General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU Affero General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Affero General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. if (!defined('GNUSOCIAL')) { exit(1); }
  20. /**
  21. * @package Plugin
  22. * @maintainer Mikael Nordfeldth <mmn@hethane.se>
  23. */
  24. class SimpleCaptchaPlugin extends Plugin
  25. {
  26. public function initialize()
  27. {
  28. // This probably needs some work. For example with IPv6 you can easily generate new IPs...
  29. $client_ip = common_client_ip();
  30. $this->client_ip = $client_ip[0] ?: $client_ip[1]; // [0] is proxy, [1] should be the real IP
  31. }
  32. public function onEndRegistrationFormData(Action $action)
  33. {
  34. $action->elementStart('li');
  35. // TRANS: Field label.
  36. $action->input('simplecaptcha', _m('Captcha'), null,
  37. // TRANS: The instruction box for our simple captcha plugin
  38. sprintf(_m('Copy this to the textbox: "%s"'), $this->getCaptchaText()),
  39. // TRANS: Placeholder in the text box
  40. /* name=id */ null, /* required */ true, ['placeholder'=>_m('Prove that you are sentient.')]);
  41. $action->elementEnd('li');
  42. return true;
  43. }
  44. protected function getCaptchaText()
  45. {
  46. return common_config('site', 'name');
  47. }
  48. public function onStartRegistrationTry(Action $action)
  49. {
  50. if ($action->arg('simplecaptcha') !== $this->getCaptchaText()) {
  51. common_log(LOG_INFO, 'Stopped non-sentient registration of nickname '._ve($action->trimmed('nickname')).' from IP: '._ve($this->client_ip));
  52. throw new ClientException(_m('Captcha does not match!'));
  53. }
  54. return true;
  55. }
  56. public function onPluginVersion(array &$versions)
  57. {
  58. $versions[] = array('name' => 'Simple Captcha',
  59. 'version' => GNUSOCIAL_VERSION,
  60. 'author' => 'Mikael Nordfeldth',
  61. 'homepage' => 'https://gnu.io/social',
  62. 'rawdescription' =>
  63. // TRANS: Plugin description.
  64. _m('A simple captcha to get rid of spambots.'));
  65. return true;
  66. }
  67. }