Project

General

Profile

Paste
Download (5.21 KB) Statistics
| Branch: | Revision:

root / drupal7 / sites / all / modules / honeypot / honeypot.install @ 4019484b

1
<?php
2

    
3
/**
4
 * @file
5
 * Install, update and uninstall functions for the Honeypot module.
6
 */
7

    
8
/**
9
 * Implements hook_schema().
10
 */
11
function honeypot_schema() {
12
  $schema['honeypot_user'] = array(
13
    'description' => 'Table that stores failed attempts to submit a form.',
14
    'fields' => array(
15
      'uid' => array(
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' => array(
22
        'type' => 'varchar',
23
        'length' => 128,
24
        'not null' => TRUE,
25
        'description' => 'Hostname of user that that triggered honeypot.',
26
      ),
27
      'timestamp' => array(
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' => array(
35
      'uid' => array('uid'),
36
      'timestamp' => array('timestamp'),
37
    ),
38
  );
39
  return $schema;
40
}
41

    
42
/**
43
 * Implements hook_install().
44
 */
45
function honeypot_install() {
46
  // Create CSS file.
47
  honeypot_create_css(variable_get('honeypot_element_name', 'url'));
48

    
49
  if (!drupal_is_cli()) {
50
    $t = get_t();
51
    drupal_set_message($t("Honeypot installed successfully. Please !link to protect your forms from spam bots.", array(
52
      '!link' => l($t('configure Honeypot'), 'admin/config/content/honeypot'),
53
    )));
54
  }
55
}
56

    
57
/**
58
 * Implements hook_uninstall().
59
 */
60
function honeypot_uninstall() {
61
  db_delete('variable')
62
    ->condition('name', db_like('honeypot_') . '%', 'LIKE')
63
    ->execute();
64
  $cache_tables = array('variables', 'cache_bootstrap');
65
  foreach ($cache_tables as $table) {
66
    if (db_table_exists($table)) {
67
      cache_clear_all($table, 'cache');
68
    }
69
  }
70

    
71
  // Delete 'honeypot' directory from files directory.
72
  drupal_load('module', 'honeypot');
73
  file_unmanaged_delete_recursive(honeypot_file_default_scheme() . '://honeypot');
74
}
75

    
76
/**
77
 * Implements hook_update_N().
78
 */
79
function honeypot_update_7001() {
80
  $ret = array();
81

    
82
  // Leaving this in because I had it in version 1.3. Silly me.
83
  return $ret;
84
}
85

    
86
/**
87
 * Update form names after upgrade from 6.x version.
88
 */
89
function honeypot_update_7002() {
90
  $map = array(
91
    'user_register' => 'user_register_form',
92
    'contact_mail_page' => 'contact_site_form',
93
    'contact_mail_user' => 'contact_personal_form',
94
  );
95
  foreach ($map as $d6_name => $d7_name) {
96
    $value = variable_get('honeypot_form_' . $d6_name, 0);
97
    if ($value) {
98
      variable_set('honeypot_form_' . $d7_name, $value);
99
    }
100
    variable_del('honeypot_form_' . $d6_name);
101
  }
102

    
103
  $comment_form_value = variable_get('honeypot_form_comment_form', 0);
104
  if ($comment_form_value) {
105
    $types = node_type_get_types();
106
    if (!empty($types)) {
107
      foreach ($types as $type) {
108
        $d7_name = 'honeypot_form_comment_node_' . $type->type . '_form';
109
        variable_set($d7_name, $comment_form_value);
110
      }
111
    }
112
  }
113
  variable_del('honeypot_form_comment_form');
114
}
115

    
116
/**
117
 * Add {honeypot_users} database table if it doesn't exist.
118
 */
119
function honeypot_update_7003() {
120
  // Make sure the {honeypot_users} table doesn't already exist.
121
  if (!db_table_exists('honeypot_user')) {
122
    $table = array(
123
      'description' => 'Table that stores failed attempts to submit a form.',
124
      'fields' => array(
125
        'uid' => array(
126
          'description' => 'Foreign key to {users}.uid; uniquely identifies a Drupal user to whom this ACL data applies.',
127
          'type' => 'int',
128
          'unsigned' => TRUE,
129
          'not null' => TRUE,
130
        ),
131
        'timestamp' => array(
132
          'description' => 'Date/time when the form submission failed, as Unix timestamp.',
133
          'type' => 'int',
134
          'unsigned' => TRUE,
135
          'not null' => TRUE,
136
        ),
137
      ),
138
      'indexes' => array(
139
        'uid' => array('uid'),
140
        'timestamp' => array('timestamp'),
141
      ),
142
    );
143

    
144
    db_create_table('honeypot_user', $table);
145
  }
146
}
147

    
148
/**
149
 * Create Honeypot CSS file.
150
 */
151
function honeypot_update_7004() {
152
  drupal_load('module', 'honeypot');
153
  module_load_include('inc', 'honeypot', 'honeypot.admin');
154
  honeypot_create_css(variable_get('honeypot_element_name', 'url'));
155
}
156

    
157
/**
158
 * Adds the 'hostname' column to the {honeypot_user} table.
159
 */
160
function honeypot_update_7100() {
161
  $schema = honeypot_schema();
162
  $spec = $schema['honeypot_user']['fields']['hostname'];
163
  $spec['initial'] = '';
164
  db_add_field('honeypot_user', 'hostname', $spec);
165
}
166

    
167
/**
168
 * Transfer previous honeypot trigger info from {flood} to {honeypot_user}.
169
 */
170
function honeypot_update_7101() {
171
  // Move all 'honeypot' events, which are honeypot captures for anonymous
172
  // users, to the {honeypot_user}-table, since the latter now supports
173
  // tracking based on ip/hostname for anonymous users.
174
  $query = db_select('flood', 'f')
175
    ->condition('event', 'honeypot');
176
  $query->addExpression('0', 'uid');
177
  $query->addField('f', 'identifier', 'hostname');
178
  $query->addField('f', 'timestamp');
179
  db_insert('honeypot_user')
180
    ->from($query)
181
    ->execute();
182

    
183
  // Clean up the flood table by removing our events, since we are no longer
184
  // relying on the flood mechanism to track anonymous honeypot captures.
185
  flood_clear_event('honeypot');
186
}