cff5172cc52ae2f7050d4fa1b4a92b0df44452a9
[drupal_ipv6_greeter.git] / ipv6_greeter.module
1 <?php
2
3 /**
4  * @file
5  * Standalone module file to handle ipv6_greeter
6  */
7
8 /**
9  * Implementation of hook_perm().
10  *
11  * @return
12  */
13 function ipv6_greeter_perm() {
14   $perms[] = 'administer IPv6 Greeter';
15   return $perms;
16 }
17
18 /**
19  * Implementation of hook_block().
20  *
21  * @param object $op [optional]
22  * @param object $delta [optional]
23  * @return
24  */
25 function ipv6_greeter_block($op = 'list', $delta = 0) {
26   if ($op == 'list') {
27     $blocks[0]['info'] = 'IPv6 Greeter';
28     return $blocks;
29   }
30   elseif ($op == 'view') {
31     $title = variable_get('ipv6_greeter_blocktitle', 'IPv6 Greeter');
32     if (!empty($title)) {
33       // XXX is it OK to use t() here, or we should rather let the user to define
34       // that as a multilanguage variable in $conf['i18n_variables'] ?
35       $block['subject'] = t($title);
36     }
37     $block['content'] = _ipv6_greeter_create_content();
38     $block['cache'] = BLOCK_NO_CACHE;
39
40     return $block;
41   }
42 }
43
44 /**
45  * Implementation of hook_menu().
46  */
47 function ipv6_greeter_menu() {
48   $items = array();
49
50   $items['admin/settings/ipv6_greeter'] = array(
51     'title'            => 'IPv6 Greeter',
52     'description'      => 'Setting for IPv6 Greeter',
53     'page callback'    => 'drupal_get_form',
54     'page arguments'   => array('ipv6_greeter_admin_settings'),
55     'access arguments' => array('administer IPv6 Greeter'),
56     'file'             => 'ipv6_greeter.admin.inc',
57   );
58
59   return $items;
60 }
61
62 /**
63  * Internal function to generate code for ipv6_greeter block
64  *
65  * @return
66  *   String containing HTML code for the block
67  */
68 function _ipv6_greeter_create_content() {
69
70   drupal_add_css((drupal_get_path('module', 'ipv6_greeter') .'/ipv6_greeter.css'));
71
72   $client_ip = ip_address();
73
74   $is_client_ipv6 = filter_var($client_ip, FILTER_VALIDATE_IP, array("flags" => FILTER_FLAG_IPV6));
75   if ($is_client_ipv6) {
76     $content = "<div class=\"ipv6_greeter-ipv6-client\"><p>";
77     $content .= t("You've got IPv6!<br />!client_ip",
78       array('!client_ip' => $client_ip));
79     $content .= "</p></div>";
80   }
81   else {
82     if (variable_get('ipv6_greeter_greetIPv4', '1') == '1') {
83       $ipv6 = l('IPv6', 'http://en.wikipedia.org/wiki/IPv6',
84         array( 'attributes' => array('title' => 'Wikipedia - IPv6'))
85       );
86       $content = "<div class=\"ipv6_greeter-ipv4-client\"><p>";
87       $content .= t("You're not on $ipv6, yet!");
88       $content .= "</p></div>";
89     }
90   }
91
92   return $content;
93 }