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

Contents of /contributions/modules/register_country/register_country.module

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


Revision 1.2 - (show annotations) (download) (as text)
Mon Oct 22 15:49:20 2007 UTC (2 years, 1 month ago) by nancyw
Branch: MAIN
CVS Tags: DRUPAL-5--1-0, HEAD
Branch point for: DRUPAL-5, DRUPAL-6--1
Changes since 1.1: +2 -1 lines
File MIME type: text/x-php
*** empty log message ***
1 <?php
2 // $Id: register_country.module,v 1.1 2007/07/18 21:30:23 nancyw Exp $
3
4 /**
5 * @file
6 * Allows the admin to select which countries (based on IP address) can register for this site.
7 */
8
9 /**
10 * Implementation of hook_help().
11 * The first case adds a little help text.
12 * The second case adds some submission guidelines on the create content page.
13 */
14 function register_country_help($section) {
15 switch ($section) {
16
17 case 'admin/help#register_country':
18 $output = '<p>'. t('The site administrator can limit registration at this site to specific countries. The country is determined by the IP address of the user. There is no specific access permission for this module; it uses \'administer site configuration\'.') .'</p>';
19 return $output;
20 }
21 }
22
23 /**
24 * Implementation of hook_menu().
25 * Add a menu item to the Administer >> Site building menu for displaying the register_country.
26 */
27 function register_country_menu($may_cache) {
28 $items = array();
29
30 if ($may_cache) {
31 $items[] = array(
32 'path' => 'admin/settings/register_country',
33 'title' => t('Register country settings'),
34 'description' => t('Limit registration to specific countries based on IP address.'),
35 'callback' => 'drupal_get_form',
36 'callback arguments' => array('register_country_settings_form'),
37 'access' => user_access('administer site configuration'),
38 );
39 }
40 return $items;
41 }
42
43 /**
44 * Define a form to control the settings.
45 */
46 function register_country_settings_form() {
47 // Pull in our form's style sheet.
48 drupal_add_css(drupal_get_path('module', 'register_country') .'/register_country.css');
49
50 $_default_settings = array(
51 // Try to use the site's "access denied" page,if it exists, for a default.
52 'error_page' => variable_get('site_403', 'node'),
53 'countries' => array('US'),
54 );
55 $register_country_settings = variable_get('register_country_settings', $_default_settings);
56
57 $form['error_page'] = array(
58 '#type' => 'textfield',
59 '#title' => t('Registration denied page'),
60 // '#suffix' => '</div>',
61 '#description' => t("This the address of the page to which the user will be redirected if they are not within one of the selected countries."),
62 '#weight' => -5,
63 '#default_value' => $register_country_settings['error_page'],
64 );
65
66 // Countries Fieldset
67 $form['countries'] = array(
68 '#type' => 'fieldset',
69 '#title' => t('Select countries'),
70 '#weight' => -1,
71 '#collapsible' => FALSE,
72 '#collapsed' => FALSE,
73 '#prefix' => '<div class="register_country_countries_section">',
74 '#suffix' => '',
75 );
76
77 // TODO: fix formatting problem when country name splits to a new line.
78 // Do a loop to get all the countries we know about.
79 $results = db_query("SELECT country_code2 AS code, country_name AS name FROM {iso3166}");
80 while ($cntry = db_fetch_object($results)) {
81 $form['countries']['country-'. $cntry->code] = array('#type' => 'checkbox',
82 '#title' => t($cntry->name),
83 '#default_value' => in_array($cntry->code, $register_country_settings['countries']),
84 '#prefix' => "\n".'<div class="register_country_country">',
85 '#suffix' => '</div>',
86 );
87 } /* end while */
88
89 $form['update']['attach'] = array(
90 '#type' => 'submit',
91 '#value' => t('Update'),
92 '#prefix' => '</div><div class="clear-block"></div>',
93 '#weight' => 3,
94 );
95
96 return $form;
97 }
98
99 /**
100 * Implementation of hook_form_submit().
101 * Save the settings values.
102 */
103 function register_country_settings_form_submit($form_id, $form_values) {
104 // We have to loop through all the form values to find the countries that are selected.
105 $countries = array();
106 foreach ($form_values as $key => $value) {
107 if (substr($key, 0, 8) == 'country-') {
108 // Checkbox values are boolean.
109 if ($value) { $countries[] = substr($key, 8, 2); }
110 } /* end country- check */
111 } /* end foreach */
112 $register_country_settings = array(
113 'error_page' => $form_values['error_page'],
114 'countries' => $countries,
115 );
116 variable_set('register_country_settings', $register_country_settings);
117
118 drupal_set_message(t('Configuration has been updated. '), 'notice');
119 }
120
121 /**
122 * Implementation of hook_user().
123 * Checks the user's IP Address on registration.
124 * If they are from one of the specified countries, allow registration to continue.
125 * If not, then redirect to a designated page.
126 */
127 function register_country_user($op, &$edit, &$account, $category = NULL){
128 $register_country_settings = variable_get('register_country_settings', array());
129 switch ($op) {
130 case 'register':
131 $addr = $_SERVER['REMOTE_ADDR'];
132 $country = ip2cc_get_country($addr);
133
134 // If $country is Null, then it wasn't found in the table.
135 // This won't happen with my patch to ip2cc.
136 if ($country->country_code2) {
137
138 // See if it's an allowed country code.
139 if (in_array($country->country_code2, $register_country_settings['countries'])) { /* okay */ }
140 // No, go to the error page.
141 else { drupal_goto($register_country_settings['error_page']); }
142
143 } /* end if country */
144 else {
145 drupal_set_message("Your IP address was not found in our country look up table.");
146 drupal_goto($register_country_settings['error_page']);
147 }
148 break; /* end of register */
149
150 } /* end of switch */
151 return array();
152 }

  ViewVC Help
Powered by ViewVC 1.1.2