honeypot.install 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. }
  60. /**
  61. * Adds the 'hostname' column to the {honeypot_user} table.
  62. */
  63. function honeypot_update_8100() {
  64. $schema = honeypot_schema();
  65. $spec = $schema['honeypot_user']['fields']['hostname'];
  66. $spec['initial'] = '';
  67. \Drupal::database()->schema()->addField('honeypot_user', 'hostname', $spec);
  68. }