honeypot.install 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. <?php
  2. /**
  3. * @file
  4. * Install, update and uninstall functions for the Honeypot module.
  5. */
  6. /**
  7. * Implements 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. 'hostname' => array(
  20. 'type' => 'varchar',
  21. 'length' => 128,
  22. 'not null' => TRUE,
  23. 'description' => 'Hostname of user that that triggered honeypot.',
  24. ),
  25. 'timestamp' => array(
  26. 'description' => 'Date/time when the form submission failed, as Unix timestamp.',
  27. 'type' => 'int',
  28. 'unsigned' => TRUE,
  29. 'not null' => TRUE,
  30. ),
  31. ),
  32. 'indexes' => array(
  33. 'uid' => array('uid'),
  34. 'timestamp' => array('timestamp'),
  35. ),
  36. );
  37. return $schema;
  38. }
  39. /**
  40. * Implements hook_install().
  41. */
  42. function honeypot_install() {
  43. // Create CSS file.
  44. honeypot_create_css(variable_get('honeypot_element_name', 'url'));
  45. if (!drupal_is_cli()) {
  46. $t = get_t();
  47. drupal_set_message($t("Honeypot installed successfully. Please !link to protect your forms from spam bots.", array(
  48. '!link' => l($t('configure Honeypot'), 'admin/config/content/honeypot'),
  49. )));
  50. }
  51. }
  52. /**
  53. * Implements hook_uninstall().
  54. */
  55. function honeypot_uninstall() {
  56. db_delete('variable')
  57. ->condition('name', db_like('honeypot_') . '%', 'LIKE')
  58. ->execute();
  59. $cache_tables = array('variables', 'cache_bootstrap');
  60. foreach ($cache_tables as $table) {
  61. if (db_table_exists($table)) {
  62. cache_clear_all($table, 'cache');
  63. }
  64. }
  65. // Delete 'honeypot' directory from public file directory.
  66. file_unmanaged_delete_recursive('public://honeypot');
  67. }
  68. /**
  69. * Implements hook_update_N().
  70. */
  71. function honeypot_update_7001() {
  72. $ret = array();
  73. // Leaving this in because I had it in version 1.3. Silly me.
  74. return $ret;
  75. }
  76. /**
  77. * Update form names after upgrade from 6.x version.
  78. */
  79. function honeypot_update_7002() {
  80. $map = array(
  81. 'user_register' => 'user_register_form',
  82. 'contact_mail_page' => 'contact_site_form',
  83. 'contact_mail_user' => 'contact_personal_form',
  84. );
  85. foreach ($map as $d6_name => $d7_name) {
  86. $value = variable_get('honeypot_form_' . $d6_name, 0);
  87. if ($value) {
  88. variable_set('honeypot_form_' . $d7_name, $value);
  89. }
  90. variable_del('honeypot_form_' . $d6_name);
  91. }
  92. $comment_form_value = variable_get('honeypot_form_comment_form', 0);
  93. if ($comment_form_value) {
  94. $types = node_type_get_types();
  95. if (!empty($types)) {
  96. foreach ($types as $type) {
  97. $d7_name = 'honeypot_form_comment_node_' . $type->type . '_form';
  98. variable_set($d7_name, $comment_form_value);
  99. }
  100. }
  101. }
  102. variable_del('honeypot_form_comment_form');
  103. }
  104. /**
  105. * Add {honeypot_users} database table if it doesn't exist.
  106. */
  107. function honeypot_update_7003() {
  108. // Make sure the {honeypot_users} table doesn't already exist.
  109. if (!db_table_exists('honeypot_user')) {
  110. $table = array(
  111. 'description' => 'Table that stores failed attempts to submit a form.',
  112. 'fields' => array(
  113. 'uid' => array(
  114. 'description' => 'Foreign key to {users}.uid; uniquely identifies a Drupal user to whom this ACL data applies.',
  115. 'type' => 'int',
  116. 'unsigned' => TRUE,
  117. 'not null' => TRUE,
  118. ),
  119. 'timestamp' => array(
  120. 'description' => 'Date/time when the form submission failed, as Unix timestamp.',
  121. 'type' => 'int',
  122. 'unsigned' => TRUE,
  123. 'not null' => TRUE,
  124. ),
  125. ),
  126. 'indexes' => array(
  127. 'uid' => array('uid'),
  128. 'timestamp' => array('timestamp'),
  129. ),
  130. );
  131. db_create_table('honeypot_user', $table);
  132. }
  133. }
  134. /**
  135. * Create Honeypot CSS file.
  136. */
  137. function honeypot_update_7004() {
  138. drupal_load('module', 'honeypot');
  139. module_load_include('inc', 'honeypot', 'honeypot.admin');
  140. honeypot_create_css(variable_get('honeypot_element_name', 'url'));
  141. }
  142. /**
  143. * Adds the 'hostname' column to the {honeypot_user} table.
  144. */
  145. function honeypot_update_7100() {
  146. $schema = honeypot_schema();
  147. $spec = $schema['honeypot_user']['fields']['hostname'];
  148. $spec['initial'] = '';
  149. db_add_field('honeypot_user', 'hostname', $spec);
  150. }
  151. /**
  152. * Transfer previous honeypot trigger info from {flood} to {honeypot_user}.
  153. */
  154. function honeypot_update_7101() {
  155. // Move all 'honeypot' events, which are honeypot captures for anonymous
  156. // users, to the {honeypot_user}-table, since the latter now supports
  157. // tracking based on ip/hostname for anonymous users.
  158. $query = db_select('flood', 'f')
  159. ->condition('event', 'honeypot');
  160. $query->addExpression('0', 'uid');
  161. $query->addField('f', 'identifier', 'hostname');
  162. $query->addField('f', 'timestamp');
  163. db_insert('honeypot_user')
  164. ->from($query)
  165. ->execute();
  166. // Clean up the flood table by removing our events, since we are no longer
  167. // relying on the flood mechanism to track anonymous honeypot captures.
  168. flood_clear_event('honeypot');
  169. }