SimpleCaptchaPlugin.php 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. const PLUGIN_VERSION = '2.0.0';
  27. public function initialize()
  28. {
  29. // This probably needs some work. For example with IPv6 you can easily generate new IPs...
  30. $client_ip = common_client_ip();
  31. $this->client_ip = $client_ip[0] ?: $client_ip[1]; // [0] is proxy, [1] should be the real IP
  32. }
  33. public function onEndRegistrationFormData(Action $action)
  34. {
  35. $action->elementStart('li');
  36. // TRANS: Field label.
  37. $action->input('simplecaptcha', _m('Captcha'), null,
  38. // TRANS: The instruction box for our simple captcha plugin
  39. sprintf(_m('Copy this to the textbox: "%s"'), $this->getCaptchaText()),
  40. // TRANS: Placeholder in the text box
  41. /* name=id */ null, /* required */ true, ['placeholder'=>_m('Prove that you are sentient.')]);
  42. $action->elementEnd('li');
  43. return true;
  44. }
  45. protected function getCaptchaText()
  46. {
  47. return common_config('site', 'name');
  48. }
  49. public function onStartRegistrationTry(Action $action)
  50. {
  51. if ($action->arg('simplecaptcha') !== $this->getCaptchaText()) {
  52. common_log(LOG_INFO, 'Stopped non-sentient registration of nickname '._ve($action->trimmed('nickname')).' from IP: '._ve($this->client_ip));
  53. throw new ClientException(_m('Captcha does not match!'));
  54. }
  55. return true;
  56. }
  57. public function onPluginVersion(array &$versions): bool
  58. {
  59. $versions[] = array('name' => 'Simple Captcha',
  60. 'version' => self::PLUGIN_VERSION,
  61. 'author' => 'Mikael Nordfeldth',
  62. 'homepage' => 'https://gnu.io/social',
  63. 'rawdescription' =>
  64. // TRANS: Plugin description.
  65. _m('A simple captcha to get rid of spambots.'));
  66. return true;
  67. }
  68. }