honeypot.install 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. <?php
  2. /**
  3. * @file
  4. * Contains install and update functions for Honeypot.
  5. */
  6. use Drupal\Core\Url;
  7. use Drupal\Core\Link;
  8. /**
  9. * Implements hook_schema().
  10. */
  11. function honeypot_schema() {
  12. $schema['honeypot_user'] = [
  13. 'description' => 'Table that stores failed attempts to submit a form.',
  14. 'fields' => [
  15. 'uid' => [
  16. 'description' => 'Foreign key to {users}.uid; uniquely identifies a Drupal user to whom this ACL data applies.',
  17. 'type' => 'int',
  18. 'unsigned' => TRUE,
  19. 'not null' => TRUE,
  20. ],
  21. 'hostname' => [
  22. 'type' => 'varchar',
  23. 'length' => 128,
  24. 'not null' => TRUE,
  25. 'description' => 'Hostname of user that that triggered honeypot.',
  26. ],
  27. 'timestamp' => [
  28. 'description' => 'Date/time when the form submission failed, as Unix timestamp.',
  29. 'type' => 'int',
  30. 'unsigned' => TRUE,
  31. 'not null' => TRUE,
  32. ],
  33. ],
  34. 'indexes' => [
  35. 'uid' => ['uid'],
  36. 'timestamp' => ['timestamp'],
  37. ],
  38. ];
  39. return $schema;
  40. }
  41. /**
  42. * Implements hook_install().
  43. */
  44. function honeypot_install() {
  45. if (PHP_SAPI !== 'cli') {
  46. $config_url = Url::fromUri('base://admin/config/content/honeypot');
  47. drupal_set_message(t(
  48. 'Honeypot installed successfully. Please <a href=":url">configure Honeypot</a> to protect your forms from spam bots.',
  49. [':url' => $config_url->toString()]
  50. ));
  51. }
  52. }
  53. /**
  54. * Implements hook_uninstall().
  55. */
  56. function honeypot_uninstall() {
  57. // Clear the bootstrap cache.
  58. \Drupal::cache('bootstrap')->deleteAll();
  59. \Drupal::configFactory()->getEditable('honeypot.settings.yml')->delete();
  60. }
  61. /**
  62. * Adds the 'hostname' column to the {honeypot_user} table.
  63. */
  64. function honeypot_update_8100() {
  65. $schema = honeypot_schema();
  66. $spec = $schema['honeypot_user']['fields']['hostname'];
  67. $spec['initial'] = '';
  68. \Drupal::database()->schema()->addField('honeypot_user', 'hostname', $spec);
  69. }