Projet

Général

Profil

Paste
Télécharger (5,17 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / honeypot / honeypot.install @ bc175c27

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
  file_unmanaged_delete_recursive(honeypot_file_default_scheme() . '://honeypot');
73
}
74

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

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

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

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

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

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

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

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

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

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