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