/[drupal]/contributions/modules/ip2cc/ip2cc.module
ViewVC logotype

Contents of /contributions/modules/ip2cc/ip2cc.module

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.15 - (show annotations) (download) (as text)
Thu Oct 25 16:53:36 2007 UTC (2 years, 1 month ago) by sugree
Branch: MAIN
CVS Tags: HEAD
Changes since 1.14: +19 -1 lines
File MIME type: text/x-php
merged json patch
1 <?php
2 /* $Id: ip2cc.module,v 1.10.2.7 2007/10/25 16:39:30 sugree Exp $ */
3
4 /*
5 * "This 'work' uses the IP-to-Country Database
6 * provided by WebHosting.Info (http://www.webhosting.info),
7 * available from http://ip-to-country.webhosting.info."
8 */
9
10 /**
11 * Implementation of hook_help().
12 */
13 function ip2cc_help($section = 'admin/help#ip2cc') {
14 switch ($section) {
15 case 'admin/modules#description':
16 return t('Adds support for ip-to-country.com for other modules.');
17 break;
18 case 'admin/settings/ip2cc':
19 $output = t('Here you can configure the ip2cc module.');
20 break;
21 }
22 return $output;
23 }
24
25 /**
26 * Implementation of hook_perm
27 */
28 function ip2cc_perm() {
29 return array('view IP address', 'view country', 'view network name');
30 }
31
32 /**
33 * Implementation of hook_menu().
34 */
35 function ip2cc_menu($may_cache) {
36 $items = array();
37 $access = user_access('administer site configuration');
38
39 if ($may_cache) {
40 $items[] = array(
41 'path' => 'ip2cc/get_country/json',
42 'title' => t('Get country as JSON'),
43 'callback' => 'ip2cc_get_country_json',
44 'access' => user_access('access content'),
45 'type' => MENU_CALLBACK,
46 );
47
48 $items[] = array(
49 'path' => 'admin/settings/ip2cc',
50 'title' => t('IP to Country'),
51 'description' => t('Administer IP to Country.'),
52 'callback' => 'drupal_get_form',
53 'callback arguments' => array('ip2cc_admin_settings'),
54 'access' => $access,
55 'type' => MENU_NORMAL_ITEM,
56 );
57
58 $items[] = array('path' => 'admin/settings/ip2cc/manage',
59 'title' => t('Manage settings'),
60 'access' => $access,
61 'weight' => -10,
62 'type' => MENU_DEFAULT_LOCAL_TASK
63 );
64 $items[] = array('path' => 'admin/settings/ip2cc/update',
65 'title' => t('Update'),
66 'callback' => 'ip2cc_update',
67 'access' => $access,
68 'weight' => -9,
69 'type' => MENU_LOCAL_TASK
70 );
71 }
72
73 return $items;
74 }
75
76 /**
77 * Implementation of hook_settings().
78 */
79 function ip2cc_admin_settings() {
80 $form['ip2cc_placement'] = array(
81 '#type' => 'fieldset',
82 '#title' => t('Where to show the flag'),
83 );
84 $form['ip2cc_placement']['ip2cc_node_types'] = array(
85 '#type' => 'select',
86 '#title' => t('Node types to display flag for'),
87 '#default_value' => variable_get('ip2cc_node_types', array()),
88 '#options' => node_get_types('names'),
89 '#multiple' => TRUE,
90 '#description' => t('Flag will display on the node pages themselves (as dictated by the theme).<br/>To display flag on main feed pages, check the box below.'),
91 );
92 $form['ip2cc_placement']['ip2cc_node'] = array(
93 '#type' => 'checkbox',
94 '#title' => t('Display in node feeds (optional)'),
95 '#return_value' => '1',
96 '#default_value' => variable_get('ip2cc_node',0),
97 );
98 $form['ip2cc_placement']['ip2cc_comment'] = array(
99 '#type' => 'checkbox',
100 '#title' => t('Display in comment feeds (optional)'),
101 '#return_value' => '1',
102 '#default_value' => variable_get('ip2cc_comment',0),
103 );
104
105 return system_settings_form($form);
106 }
107
108 function ip2cc_update() {
109 return drupal_get_form('ip2cc_update_form');
110 }
111
112 function ip2cc_update_form() {
113 $form = array();
114 $form['ip2cc_update_current'] = array(
115 '#type' => 'fieldset',
116 '#title' => t('Information'),
117 );
118 $form['ip2cc_update_current']['current'] = array(
119 '#type' => 'textfield',
120 '#title' => t('Amount: of entries'),
121 '#default_value' => db_result(db_query('SELECT COUNT(*) FROM {ip2cc}')),
122 '#disabled' => TRUE,
123 );
124 $form['ip2cc_update_detail'] = array(
125 '#type' => 'fieldset',
126 '#title' => t('Update details'),
127 );
128 $form['ip2cc_update_detail']['start'] = array(
129 '#type' => 'textfield',
130 '#title' => t('Start entry'),
131 '#default_value' => '1',
132 );
133 $form['ip2cc_update_detail']['end'] = array(
134 '#type' => 'textfield',
135 '#title' => t('End entry'),
136 '#default_value' => _ip2cc_batch_count("ipdb.mysql"),
137 );
138 $form['ip2cc_update_detail']['flush'] = array(
139 '#type' => 'checkbox',
140 '#title' => t('Flush?'),
141 '#return_value' => '1',
142 '#default_value' => '0',
143 );
144 $form['ip2cc_update_detail']['submit'] = array(
145 '#type' => 'submit',
146 '#value' => t('Submit'),
147 );
148 return $form;
149 }
150
151 function ip2cc_update_form_submit($form_id, $form_values) {
152 if ($form_values['flush']) {
153 db_query("DELETE FROM {ip2cc}");
154 }
155 _ip2cc_batch_insert("ipdb.mysql", (int)$form_values['start'], (int)$form_values['end']);
156 drupal_set_message(t("Successfully updated entry %start to %end", array('%start' => $form_values['start'], '%end' => $form_values['end'])));
157 }
158
159 /**
160 * Implementation of hook_nodeapi().
161 */
162 function ip2cc_nodeapi(&$node, $op, $teaser = NULL, $page = NULL) {
163 switch ($op) {
164 case 'load':
165 return db_fetch_array(db_query("SELECT hostname FROM {ip2cc_node} WHERE nid = %d", $node->nid));
166 break;
167
168 case 'insert':
169 case 'update':
170 ip2cc_set_node($node);
171 break;
172
173 case 'delete':
174 db_query("DELETE FROM {ip2cc_node} WHERE nid = %d", $node->nid);
175 break;
176 }
177 }
178
179 /**
180 * Implementation of hook_update().
181 */
182 function ip2cc_set_node($node) {
183 $node_count = db_result(db_query("SELECT COUNT(hostname) FROM {ip2cc_node} WHERE nid = %d", $node->nid));
184 if ($node_count == 0) {
185 db_query("INSERT INTO {ip2cc_node} (nid, hostname) VALUES (%d, '%s')", $node->nid, $_SERVER['REMOTE_ADDR']);
186 }
187 else {
188 db_query("UPDATE {ip2cc_node} SET hostname = '%s' WHERE nid = %d", $_SERVER['REMOTE_ADDR'], $node->nid);
189 }
190 }
191
192 /**
193 * Implementation of hook_link().
194 */
195 function ip2cc_link($type, $node = 0, $main = 0) {
196 $links = array();
197
198 $node_type = in_array($node->type, variable_get('ip2cc_node_types', array()), TRUE);
199 if ($type == 'node' && variable_get('ip2cc_node', '0') == '1' && $node_type && isset($node->hostname) && !empty($node->hostname)) {
200 if ($output = theme('ip2cc_ip_flag', $node->hostname)) {
201 $links['ip2cc_info'] = array(
202 'title' => $output,
203 'html' => TRUE,
204 );
205 }
206 }
207 else if ($type == 'comment' && variable_get('ip2cc_comment', '0') == '1') {
208 $result = db_query('SELECT c.hostname FROM {comments} c WHERE c.cid = %d', $node->cid);
209 $comment = db_fetch_object($result);
210 if ($output = theme('ip2cc_ip_flag', $comment->hostname)) {
211 $links['ip2cc_info'] = array(
212 'title' => $output,
213 'html' => TRUE,
214 );
215 }
216 }
217
218 return $links;
219 }
220
221 function ip2cc_get_country($addr) {
222 $ip = _ip2cc_inet_aton($addr);
223 $result = db_query("SELECT i.*,c.* FROM {ip2cc} i LEFT JOIN {iso3166} c ON i.country_code = c.country_code2 WHERE ip_from <= %s AND ip_to >= %s",$ip,$ip);
224 while ($co = db_fetch_object($result)) {
225 $co->ip = $addr;
226 return $co;
227 }
228 return NULL;
229 }
230
231 function ip2cc_get_country_json() {
232 $addr = arg(3);
233 if (empty($addr)) {
234 $addr = $_SERVER['REMOTE_ADDR'];
235 }
236 $country = ip2cc_get_country($addr);
237 print drupal_to_js($country);
238 exit;
239 }
240
241 function theme_ip2cc_flag($co, $addr = '') {
242 if (!user_access('view country')) {
243 return "";
244 }
245
246 if (isset($co)) {
247 $cc = strtolower($co->country_code2);
248 if (empty($cc)) {
249 $cc = "unknown";
250 }
251 $title = "$cc $co->country_name";
252 if (user_access('view IP address')) {
253 $title .= " $co->ip";
254 }
255 if (user_access('view network name')) {
256 $title .= " $co->net_name";
257 }
258 } else {
259 $cc = 'unknown';
260 $title = (empty($addr))?'unknown':$addr;
261 }
262 $path = drupal_get_path('module','ip2cc') . "/flags/$cc.png";
263 $attribs = array('width' => 14, height => 14);
264 return theme('image',$path,$cc,$title,$attribs,FALSE);
265 }
266
267 function theme_ip2cc_ip_flag($addr) {
268 $country = module_invoke('ip2cc', 'get_country', $addr);
269 if ($output = theme('ip2cc_flag', $country, $addr)) {
270 $output = "<span>$output</span>";
271 }
272 return $output;
273 }
274
275 function theme_ip2cc_ip_flag_long($addr) {
276 $country = module_invoke('ip2cc', 'get_country', $addr);
277 if ($output = theme('ip2cc_flag', $country, $addr)) {
278 $output = "<span>$output";
279 if (user_access('view IP address')) {
280 $output .= "$country->ip";
281 }
282 $output .= "</span>";
283 }
284 return $output;
285 }
286
287 function _ip2cc_inet_aton($a) {
288 $ip = gethostbyname($a);
289 return sprintf("%u", ip2long($ip));
290 }
291
292 function _ip2cc_batch_count($filename) {
293 $count = 0;
294 if ($fd = fopen(drupal_get_path('module','ip2cc').'/'.$filename, 'r')) {
295 while (!feof($fd)) {
296 $line = fgets($fd);
297 $count++;
298 }
299 fclose($fd);
300 }
301 return $count;
302 }
303
304 function _ip2cc_batch_insert($filename,$start=0,$end=-1) {
305 if ($fd = fopen(drupal_get_path('module','ip2cc').'/'.$filename, 'r')) {
306 for($i=0; $i<$start; $i++) {
307 $line = fgets($fd);
308 }
309 $i = $start;
310 while (($i <= $end || $end == -1) && !feof($fd)) {
311 $line = substr(rtrim(fgets($fd)),0,-1);
312 db_query($line);
313 $line = NULL;
314 $i++;
315 }
316 fclose($fd);
317 }
318 }
319
320 ?>

  ViewVC Help
Powered by ViewVC 1.1.2