honeypot.install 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <?php
  2. /**
  3. * @file
  4. * Install, update and uninstall functions for the Honeypot module.
  5. */
  6. /**
  7. * Implements of hook_schema().
  8. */
  9. function honeypot_schema() {
  10. $schema['honeypot_user'] = array(
  11. 'description' => 'Table that stores failed attempts to submit a form.',
  12. 'fields' => array(
  13. 'uid' => array(
  14. 'description' => 'Foreign key to {users}.uid; uniquely identifies a Drupal user to whom this ACL data applies.',
  15. 'type' => 'int',
  16. 'unsigned' => TRUE,
  17. 'not null' => TRUE,
  18. ),
  19. 'timestamp' => array(
  20. 'description' => 'Date/time when the form submission failed, as Unix timestamp.',
  21. 'type' => 'int',
  22. 'unsigned' => TRUE,
  23. 'not null' => TRUE,
  24. ),
  25. ),
  26. 'indexes' => array(
  27. 'uid' => array('uid'),
  28. 'timestamp' => array('timestamp'),
  29. ),
  30. );
  31. return $schema;
  32. }
  33. /**
  34. * Implements hook_install().
  35. */
  36. function honeypot_install() {
  37. // Create CSS file.
  38. honeypot_create_css(variable_get('honeypot_element_name', 'url'));
  39. if (!drupal_is_cli()) {
  40. $t = get_t();
  41. drupal_set_message($t("Honeypot installed successfully. Please !link to protect your forms from spam bots.", array(
  42. '!link' => l($t('configure Honeypot'), 'admin/config/content/honeypot'),
  43. )));
  44. }
  45. }
  46. /**
  47. * Implements hook_uninstall().
  48. */
  49. function honeypot_uninstall() {
  50. db_delete('variable')
  51. ->condition('name', db_like('honeypot_') . '%', 'LIKE')
  52. ->execute();
  53. $cache_tables = array('variables', 'cache_bootstrap');
  54. foreach ($cache_tables as $table) {
  55. if (db_table_exists($table)) {
  56. cache_clear_all($table, 'cache');
  57. }
  58. }
  59. // Delete 'honeypot' directory from public file directory.
  60. file_unmanaged_delete_recursive('public://honeypot');
  61. }
  62. /**
  63. * Implements hook_update_N().
  64. */
  65. function honeypot_update_7001() {
  66. $ret = array();
  67. // Leaving this in because I had it in version 1.3. Silly me.
  68. return $ret;
  69. }
  70. /**
  71. * Update form names after upgrade from 6.x version.
  72. */
  73. function honeypot_update_7002() {
  74. $map = array(
  75. 'user_register' => 'user_register_form',
  76. 'contact_mail_page' => 'contact_site_form',
  77. 'contact_mail_user' => 'contact_personal_form',
  78. );
  79. foreach ($map as $d6_name => $d7_name) {
  80. $value = variable_get('honeypot_form_' . $d6_name, 0);
  81. if ($value) {
  82. variable_set('honeypot_form_' . $d7_name, $value);
  83. }
  84. variable_del('honeypot_form_' . $d6_name);
  85. }
  86. $comment_form_value = variable_get('honeypot_form_comment_form', 0);
  87. if ($comment_form_value) {
  88. $types = node_type_get_types();
  89. if (!empty($types)) {
  90. foreach ($types as $type) {
  91. $d7_name = 'honeypot_form_comment_node_' . $type->type . '_form';
  92. variable_set($d7_name, $comment_form_value);
  93. }
  94. }
  95. }
  96. variable_del('honeypot_form_comment_form');
  97. }
  98. /**
  99. * Add {honeypot_users} database table if it doesn't exist.
  100. */
  101. function honeypot_update_7003() {
  102. // Make sure the {honeypot_users} table doesn't already exist.
  103. if (!db_table_exists('honeypot_user')) {
  104. $table = array(
  105. 'description' => 'Table that stores failed attempts to submit a form.',
  106. 'fields' => array(
  107. 'uid' => array(
  108. 'description' => 'Foreign key to {users}.uid; uniquely identifies a Drupal user to whom this ACL data applies.',
  109. 'type' => 'int',
  110. 'unsigned' => TRUE,
  111. 'not null' => TRUE,
  112. ),
  113. 'timestamp' => array(
  114. 'description' => 'Date/time when the form submission failed, as Unix timestamp.',
  115. 'type' => 'int',
  116. 'unsigned' => TRUE,
  117. 'not null' => TRUE,
  118. ),
  119. ),
  120. 'indexes' => array(
  121. 'uid' => array('uid'),
  122. 'timestamp' => array('timestamp'),
  123. ),
  124. );
  125. db_create_table('honeypot_user', $table);
  126. }
  127. }
  128. /**
  129. * Create Honeypot CSS file.
  130. */
  131. function honeypot_update_7004() {
  132. drupal_load('module', 'honeypot');
  133. module_load_include('inc', 'honeypot', 'honeypot.admin');
  134. honeypot_create_css(variable_get('honeypot_element_name', 'url'));
  135. }