-
Notifications
You must be signed in to change notification settings - Fork 0
/
geoip_tokens.rules.inc
145 lines (138 loc) · 3.4 KB
/
geoip_tokens.rules.inc
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
<?php
/**
* @file
* GeoIP Token support code for the Rules module.
*/
/**
* Implements hook_rules_data_info().
*/
function geoip_tokens_rules_data_info() {
return array(
'geoip_tokens' => array(
'label' => 'geoip token',
'type' => 'geoip_tokens',
'token type' => 'geoip_tokens',
'ui class' => 'RulesDataUIText',
'property info' => _geoip_tokens_tokens_info(),
),
);
}
/**
* Helper function to declare the token type to rules.
*/
function _geoip_tokens_tokens_info() {
return array(
'country_code' => array(
'label' => t('Country code'),
'description' => t('Country code (two uppercase letters) detected by current IP.'),
'type' => 'text',
),
'timezone' => array(
'label' => t('Timezone'),
'description' => t('Timezone name detected by current IP'),
'type' => 'text',
),
'latitude' => array(
'label' => t('Latitude'),
'description' => t('Latitude of current position detected by IP'),
'type' => 'decimal',
),
'longitude' => array(
'label' => t('Longitude'),
'description' => t('Longitude of current position detected by IP'),
'type' => 'decimal',
),
);
}
/**
* Implements hook_rules_condition_info().
*
* Condition to compare if the detected country is contained in a list of
* selected countries.
*/
function geoip_tokens_rules_condition_info() {
return array(
'geoip_tokens_rules_condition_country_compare' => array(
'label' => t('Check for user country detected by IP'),
'parameter' => array(
'Countries' => array(
'label' => t('Country'),
'type' => 'list<text>',
'restriction' => 'input',
'options list' => 'geoip_tokens_countries_list',
),
),
'group' => t('GeoIP Tokens'),
),
'geoip_tokens_rules_condition_is_european_union' => array(
'label' => t('Check if detected country is in the European Union'),
'group' => t('GeoIP Tokens'),
),
);
}
/**
* Checks if the detected country matches to one in the list.
*
* @param array|string $countries
* List of countries to check against.
*
* @return bool
* TRUE if the country code is in the list.
*/
function geoip_tokens_rules_condition_country_compare($countries) {
if (!is_array($countries)) {
$countries = explode('\n', strtoupper($countries));
}
$current_country = geoip_tokens_get_geoip_info('country_code');
return in_array($current_country['country_code'], $countries);
}
/**
* Checks if the detected country is a member of the European Union.
*/
function geoip_tokens_rules_condition_is_european_union() {
$current_country = geoip_tokens_get_geoip_info('country_code');
$members = geoip_tokens_eu_member_list();
return in_array($current_country['country_code'], $members);
}
/**
* Helper function to provide an options list for rules UI.
*/
function geoip_tokens_countries_list() {
include_once BACKDROP_ROOT . '/core/includes/locale.inc';
return country_get_list();
}
/**
* Helper function to provide a list of member countries.
*/
function geoip_tokens_eu_member_list() {
$member_countries = array(
'AT',
'BE',
'BG',
'CY',
'CZ',
'DE',
'DK',
'EE',
'ES',
'FI',
'FR',
'GR',
'HR',
'HU',
'IE',
'IT',
'LT',
'LU',
'LV',
'MT',
'NL',
'PL',
'PT',
'RO',
'SE',
'SI',
'SK',
);
return $member_countries;
}