| 1 |
<?php
|
| 2 |
// $Id: validateage.module,v 1.5 2008/03/04 06:47:29 gwen Exp $
|
| 3 |
|
| 4 |
/* TODO FormAPI image buttons are now supported.
|
| 5 |
FormAPI now offers the 'image_button' element type, allowing developers to
|
| 6 |
use icons or other custom images in place of traditional HTML submit buttons.
|
| 7 |
|
| 8 |
$form['my_image_button'] = array(
|
| 9 |
'#type' => 'image_button',
|
| 10 |
'#title' => t('My button'),
|
| 11 |
'#return_value' => 'my_data',
|
| 12 |
'#src' => 'my/image/path.jpg',
|
| 13 |
); */
|
| 14 |
|
| 15 |
/* TODO hook_user('view')
|
| 16 |
The return value of hook_user('view') has changed, to match the process that
|
| 17 |
nodes use for rendering. Modules should add their custom HTML to
|
| 18 |
$account->content element. Further, this HTML should be in the form that
|
| 19 |
drupal_render() recognizes. */
|
| 20 |
|
| 21 |
/* TODO New user_mail_tokens() method may be useful.
|
| 22 |
user.module now provides a user_mail_tokens() function to return an array
|
| 23 |
of the tokens available for the email notification messages it sends when
|
| 24 |
accounts are created, activated, blocked, etc. Contributed modules that
|
| 25 |
wish to make use of the same tokens for their own needs are encouraged
|
| 26 |
to use this function. */
|
| 27 |
|
| 28 |
/* TODO
|
| 29 |
There is a new hook_watchdog in core. This means that contributed modules
|
| 30 |
can implement hook_watchdog to log Drupal events to custom destinations.
|
| 31 |
Two core modules are included, dblog.module (formerly known as watchdog.module),
|
| 32 |
and syslog.module. Other modules in contrib include an emaillog.module,
|
| 33 |
included in the logging_alerts module. See syslog or emaillog for an
|
| 34 |
example on how to implement hook_watchdog.
|
| 35 |
function example_watchdog($log = array()) {
|
| 36 |
if ($log['severity'] == WATCHDOG_ALERT) {
|
| 37 |
mysms_send($log['user']->uid,
|
| 38 |
$log['type'],
|
| 39 |
$log['message'],
|
| 40 |
$log['variables'],
|
| 41 |
$log['severity'],
|
| 42 |
$log['referer'],
|
| 43 |
$log['ip'],
|
| 44 |
format_date($log['timestamp']));
|
| 45 |
}
|
| 46 |
} */
|
| 47 |
|
| 48 |
/* TODO Implement the hook_theme registry. Combine all theme registry entries
|
| 49 |
into one hook_theme function in each corresponding module file.
|
| 50 |
function validateage_theme() {
|
| 51 |
return array(
|
| 52 |
);
|
| 53 |
} */
|
| 54 |
|
| 55 |
/**
|
| 56 |
* @file
|
| 57 |
* This is the main module file
|
| 58 |
*
|
| 59 |
* This module requires that a user's stated age during registration is at least that set by the administrator.
|
| 60 |
*/
|
| 61 |
|
| 62 |
/**
|
| 63 |
* Implementation of hook_help()
|
| 64 |
*
|
| 65 |
* Display help and module information
|
| 66 |
* @param section which section of the site we're displaying help
|
| 67 |
* @return help text for section
|
| 68 |
*/
|
| 69 |
function validateage_help($path, $arg) {
|
| 70 |
$output = '';
|
| 71 |
switch ($path) {
|
| 72 |
case "admin/help#validateage":
|
| 73 |
$output = '<p>Validates user as over an adminstrator set age when registering. Module requires that ';
|
| 74 |
$output .= 'a mandatory date field be created using the profile module. The name of that field and the ';
|
| 75 |
$output .= 'minimum age must be entered in the Minimum Registration Age settings.</p>';
|
| 76 |
break;
|
| 77 |
}
|
| 78 |
return $output;
|
| 79 |
}
|
| 80 |
|
| 81 |
/**
|
| 82 |
* Valid permissions for this module
|
| 83 |
*
|
| 84 |
* @return $array
|
| 85 |
* An array of valid permissions for the validateage module
|
| 86 |
*/
|
| 87 |
function validateage_perm() {
|
| 88 |
return array('administer age validation');
|
| 89 |
}
|
| 90 |
|
| 91 |
/*
|
| 92 |
* Implementation of hook_menu()
|
| 93 |
*/
|
| 94 |
function validateage_menu() {
|
| 95 |
$items = array();
|
| 96 |
$items['admin/user/validateage'] = array(
|
| 97 |
'title' => 'Minimum Registration Age',
|
| 98 |
'description' => 'Set the minimum age for user registration',
|
| 99 |
'page callback' => 'drupal_get_form',
|
| 100 |
'page arguments' => array('validateage_admin'),
|
| 101 |
'access arguments' => array('administer age validation'),
|
| 102 |
'type' => MENU_NORMAL_ITEM,
|
| 103 |
);
|
| 104 |
return $items;
|
| 105 |
}
|
| 106 |
|
| 107 |
/*
|
| 108 |
* Implementation of hook_user
|
| 109 |
*/
|
| 110 |
function validateage_user($op, &$edit, &$account, $category = NULL) {
|
| 111 |
switch ($op) {
|
| 112 |
case 'delete':
|
| 113 |
validateage_birthday_delete($account->uid);
|
| 114 |
break;
|
| 115 |
case 'insert':
|
| 116 |
validateage_birthday_update($account->uid, $edit['validateage_dob']['month'], $edit['validateage_dob']['day'], $edit['validateage_dob']['year']);
|
| 117 |
break;
|
| 118 |
case 'register':
|
| 119 |
$form = array();
|
| 120 |
$form['validateage_dob'] = array(
|
| 121 |
'#type' => 'date',
|
| 122 |
'#title' => t('Date of Birth'),
|
| 123 |
'#description' => t('You must be at least @age to join @site', array('@age' => variable_get('validateage_age', 18), '@site' => variable_get('sitename', ''))),
|
| 124 |
);
|
| 125 |
return $form;
|
| 126 |
break;
|
| 127 |
case 'validate':
|
| 128 |
$minimum_age = variable_get('validateage_age', '18');
|
| 129 |
$birthyear = $edit['validateage_dob']['year'];
|
| 130 |
$birthmonth = $edit['validateage_dob']['month'];
|
| 131 |
$birthdate = $edit['validateage_dob']['day'];
|
| 132 |
$birthday = implode("-", array($birthyear, $birthmonth, $birthdate));
|
| 133 |
$age = _validateage_get_age($birthday);
|
| 134 |
if ($age < $minimum_age) {
|
| 135 |
form_set_error('registration', variable_get('validateage_fail_message', 'Sorry, we are unable to accept your registration at this time.'));
|
| 136 |
}
|
| 137 |
elseif (_validateage_check_birthday($birthday)) {
|
| 138 |
drupal_set_message(t('Happy Birthday!'));
|
| 139 |
}
|
| 140 |
break;
|
| 141 |
}
|
| 142 |
}
|
| 143 |
|
| 144 |
/*
|
| 145 |
* This function defines the settings on the admin page
|
| 146 |
*/
|
| 147 |
function validateage_admin() {
|
| 148 |
$form['validateage_age'] = array(
|
| 149 |
'#type' => 'textfield',
|
| 150 |
'#title' => t('Age'),
|
| 151 |
'#default_value' => variable_get('validateage_age', '18'),
|
| 152 |
'#size' => 3,
|
| 153 |
'#maxlength' => 3,
|
| 154 |
'#description' => t('The minimum age required to register for the site.'),
|
| 155 |
'#element_validate' => array('validateage_is_number'),
|
| 156 |
);
|
| 157 |
$form['validateage_fail_message'] = array(
|
| 158 |
'#type' => 'textarea',
|
| 159 |
'#title' => t('Validation Failure Message'),
|
| 160 |
'#default_value' => variable_get('validateage_fail_message', 'Sorry, we are unable to accept your registration at this time.'),
|
| 161 |
'#description' => t('The message displayed if the user is underage.'),
|
| 162 |
);
|
| 163 |
return system_settings_form($form);
|
| 164 |
}
|
| 165 |
|
| 166 |
/*
|
| 167 |
* This function is used to validate the age field in the settings form
|
| 168 |
*/
|
| 169 |
function validateage_is_number(&$form) {
|
| 170 |
if (! is_numeric($form['#value'])) {
|
| 171 |
form_set_error("validateage", t('Age is not a number.'));
|
| 172 |
}
|
| 173 |
elseif ($form['#value'] < 0) {
|
| 174 |
form_set_error("validateage", t('Age must be 0 or greater.'));
|
| 175 |
}
|
| 176 |
}
|
| 177 |
|
| 178 |
/**
|
| 179 |
* This function deletes the birthday record from the db
|
| 180 |
*/
|
| 181 |
function validateage_birthday_delete($uid) {
|
| 182 |
$query = "DELETE FROM {validateage} WHERE uid = %d";
|
| 183 |
$args = array($uid);
|
| 184 |
db_query($query, $args);
|
| 185 |
}
|
| 186 |
|
| 187 |
/**
|
| 188 |
* This function updates the birthday in the db
|
| 189 |
*/
|
| 190 |
function validateage_birthday_update($uid, $month, $day, $year) {
|
| 191 |
$query = "SELECT COUNT(*) FROM {validateage} WHERE uid = %d";
|
| 192 |
$args = array($uid);
|
| 193 |
// check if there's a record for user already
|
| 194 |
if (db_result(db_query($query, $args))) {
|
| 195 |
$query = "UPDATE {validateage} SET month = %d, $day = %d, $year = %d WHERE uid = %d";
|
| 196 |
$args = array($month, $day, $year, $uid);
|
| 197 |
}
|
| 198 |
else {
|
| 199 |
$query = "INSERT INTO {validateage} (uid, month, day, year) VALUES (%d, %d, %d, %d)";
|
| 200 |
$args = array($uid, $month, $day, $year);
|
| 201 |
}
|
| 202 |
db_query($query, $args);
|
| 203 |
}
|
| 204 |
|
| 205 |
/*
|
| 206 |
* This function gets a user's age
|
| 207 |
*/
|
| 208 |
function _validateage_get_age($birthday) {
|
| 209 |
list($year, $month, $day) = explode("-", $birthday);
|
| 210 |
$year_diff = date("Y") - $year;
|
| 211 |
$month_diff = date("m") - $month;
|
| 212 |
$day_diff = date("d") - $day;
|
| 213 |
if ($month_diff < 0) {
|
| 214 |
$year_diff--;
|
| 215 |
}
|
| 216 |
elseif (($month_diff==0) && ($day_diff < 0)) {
|
| 217 |
$year_diff--;
|
| 218 |
}
|
| 219 |
return $year_diff;
|
| 220 |
}
|
| 221 |
|
| 222 |
/*
|
| 223 |
* This function checks if it's a user's birthday or not
|
| 224 |
*/
|
| 225 |
function _validateage_check_birthday($birthday) {
|
| 226 |
list($year, $month, $day) = explode("-", $birthday);
|
| 227 |
if ((date("m") == $month) && (date("d") == $day)) {
|
| 228 |
return 1;
|
| 229 |
}
|
| 230 |
else {
|
| 231 |
return 0;
|
| 232 |
}
|
| 233 |
}
|