-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathip_blocking.install
181 lines (165 loc) · 4.67 KB
/
ip_blocking.install
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
<?php
/**
* @file
* Install, update and uninstall functions for the IP address blocking module.
*/
/**
* Implements hook_requirements().
*/
function ip_blocking_requirements($phase) {
$requirements = array();
$t = get_t();
if ($phase == 'install') {
if (module_exists('ban_ip')) {
$requirements['ban_ip']['description'] = $t('Please uninstall module "Ban IP" then try again.');
$requirements['ban_ip']['severity'] = REQUIREMENT_ERROR;
}
if (db_table_exists('blocked_ips')) {
db_rename_table('blocked_ips', 'blocked_ips_d7');
$requirements['table_exists'] = array(
'title' => $t('DB table already exists'),
'description' => $t('DB table named "blocked_ips" already exists.'),
'value' => $t('An existing table named "blocked_ips" has been renamed to "blocked_ips_d7"'),
'severity' => REQUIREMENT_WARNING,
);
}
}
if ($phase == 'runtime') {
if (db_table_exists('blocked_ips')) {
$result = db_query('SELECT iid FROM {blocked_ips}');
$count = $result->rowCount();
if ($count > 0) {
$url = url('admin/config/people/ip-blocking');
$requirements['ip_blocking'] = array(
'title' => t('IP address blocking'),
'value' => t('@count IP-address(es) blocked. <a href="@url">Manage blocked IPs</a>',
array('@count' => $count, '@url' => $url)),
'severity' => REQUIREMENT_INFO,
);
}
}
if (db_table_exists('blocked_ips_d7')) {
$table = url('admin/config/people/ip-blocking/orphaned_table');
$requirements['blocked_ips_d7'] = array(
'title' => $t('Database orphaned table'),
'value' => $t("A table called 'blocked_ips_d7' inherited from Drupal 7 can be deleted if you don't need it, or you can import data from <a href='@table'>this table</a>.",
array('@table' => $table)),
'severity' => REQUIREMENT_INFO,
);
}
}
return $requirements;
}
/**
* Implements hook_schema().
*/
function ip_blocking_schema() {
$schema = array();
$schema['blocked_ips'] = array(
'description' => 'Stores blocked IP addresses.',
'fields' => array(
'iid' => array(
'description' => 'Primary Key: unique ID for IP addresses.',
'type' => 'serial',
'unsigned' => TRUE,
'not null' => TRUE,
),
'ip' => array(
'description' => 'IP address',
'type' => 'varchar',
'length' => 40,
'not null' => TRUE,
'default' => '',
),
'reason' => array(
'description' => 'Reason of blocking.',
'type' => 'varchar',
'length' => '255',
'not null' => FALSE,
),
'uid' => array(
'description' => 'User identifier.',
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
),
'time' => array(
'description' => 'Unix timestamp of when IP blocked.',
'type' => 'int',
'not null' => TRUE,
'default' => 0,
),
'type' => array(
'description' => 'Type of event',
'type' => 'varchar',
'length' => 15,
'not null' => FALSE,
'default' => '',
),
),
'indexes' => array(
'ip' => array('ip'),
),
'primary key' => array('iid'),
);
return $schema;
}
/**
* Add new fields to database.
*/
function ip_blocking_update_1000() {
$fields = array(
'reason' => array(
'description' => 'Reason of blocking.',
'type' => 'varchar',
'length' => '255',
'not null' => FALSE,
),
'uid' => array(
'description' => 'User identifier.',
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
),
'time' => array(
'type' => 'int',
'not null' => TRUE,
'default' => 0,
'description' => 'Unix timestamp of when IP blocked.',
),
);
foreach($fields as $field_name => $schema) {
if (!db_field_exists('blocked_ips', $field_name)) {
db_add_field('blocked_ips', $field_name, $schema);
}
}
}
/**
* Add and init new variable.
*/
function ip_blocking_update_1001() {
$config = config('ip_blocking.settings');
$config->set('return_404', 0);
$config->save();
}
/**
* 1) add and init new variable;
* 2) add new column to table.
*/
function ip_blocking_update_1002() {
$config = config('ip_blocking.settings');
$config->set('number_of_items', 50);
$config->save();
if (!db_field_exists('blocked_ips', 'type')) {
$spec = array(
'description' => 'Type of event',
'type' => 'varchar',
'length' => 15,
'not null' => FALSE,
'default' => '',
);
db_add_field('blocked_ips', 'type', $spec);
}
}