| 1 |
<?php
|
| 2 |
// $Id: og_reg_codes.module,v 1.4 2008/11/09 23:02:44 marcp Exp $
|
| 3 |
/**
|
| 4 |
* @file
|
| 5 |
* Provides registration codes for organic groups.
|
| 6 |
*/
|
| 7 |
|
| 8 |
/**
|
| 9 |
* Implementation of hook_help
|
| 10 |
*
|
| 11 |
*/
|
| 12 |
function og_reg_codes_help($path, $arg) {
|
| 13 |
global $user;
|
| 14 |
|
| 15 |
switch ($path) {
|
| 16 |
case 'admin/og/og-reg-codes':
|
| 17 |
$output = '<p>'. t('Settings for Organic Groups registration codes. A new user will be auto-approved as member of a group when they supply a group code at registration. Users may also visit the "join a group" page to join a group using a code.');
|
| 18 |
$output .= '</p><p>'. t('You may combine this feature with the usual use of groups in the registration form.'). '</p>';
|
| 19 |
return $output;
|
| 20 |
case 'og/use-code':
|
| 21 |
$output = '<p>'. t('You may check a group code by entering it below.');
|
| 22 |
if ($user->uid) {
|
| 23 |
$output .= ' '. t('You may also join a group by submitting its code.');
|
| 24 |
}
|
| 25 |
return $output. '</p>';
|
| 26 |
|
| 27 |
}
|
| 28 |
}
|
| 29 |
|
| 30 |
/**
|
| 31 |
* Implementation of hook_menu
|
| 32 |
*
|
| 33 |
*/
|
| 34 |
function og_reg_codes_menu() {
|
| 35 |
$items = array();
|
| 36 |
|
| 37 |
$items['admin/og/og-reg-codes'] = array(
|
| 38 |
'title' => 'OG registration codes',
|
| 39 |
'description' => 'Configure group registration codes.',
|
| 40 |
'page callback' => 'drupal_get_form',
|
| 41 |
'page arguments' => array('og_reg_codes_set'),
|
| 42 |
'access callback' => 'user_access',
|
| 43 |
'access arguments' => array('administer site configuration'),
|
| 44 |
);
|
| 45 |
$items['og/use-code'] = array(
|
| 46 |
'title' => 'Use group code',
|
| 47 |
'page callback' => 'drupal_get_form',
|
| 48 |
'page arguments' => array('og_reg_codes_join'),
|
| 49 |
'access callback' => 'user_is_logged_in',
|
| 50 |
'weight' => 10,
|
| 51 |
'type' => MENU_LOCAL_TASK,
|
| 52 |
);
|
| 53 |
|
| 54 |
return $items;
|
| 55 |
}
|
| 56 |
|
| 57 |
/**
|
| 58 |
* Implementation of hook_form_alter.
|
| 59 |
*/
|
| 60 |
function og_reg_codes_form_alter(&$form, &$form_state, $form_id) {
|
| 61 |
if ($form_id == 'og_invite_form') {
|
| 62 |
$code = og_reg_codes_get_code($form['gid']['#value']);
|
| 63 |
$form['pmessage']['#default_value'] .= strtr(_og_reg_codes_message(), array('%reg_code' => $code)). "\n\n";
|
| 64 |
}
|
| 65 |
else if ($form_id == 'user_register') {
|
| 66 |
if (!isset($form['og_register'])) {
|
| 67 |
$form['og_register'] = array('#type' => 'fieldset', '#title' => t('Groups'));
|
| 68 |
}
|
| 69 |
$form['og_register']['og_reg_code'] = array(
|
| 70 |
'#type' => 'textfield',
|
| 71 |
'#title' => t('Group registration code'),
|
| 72 |
'#required' => variable_get('og_reg_code_required', TRUE),
|
| 73 |
'#description' => t('Enter the group registration code that you received from a current group member or site administrator.'),
|
| 74 |
'#suffix' => t('Before registering, you can <a href="!url">check your group code</a>.', array('!url' => url('og/use-code')))
|
| 75 |
);
|
| 76 |
}
|
| 77 |
}
|
| 78 |
|
| 79 |
/**
|
| 80 |
* Implementation of hook_user
|
| 81 |
*
|
| 82 |
*/
|
| 83 |
function og_reg_codes_user($op, &$edit, &$account, $category = NULL) {
|
| 84 |
|
| 85 |
if (!module_exists('og')) {
|
| 86 |
return;
|
| 87 |
}
|
| 88 |
global $user;
|
| 89 |
|
| 90 |
switch ($op) {
|
| 91 |
case 'validate':
|
| 92 |
if (isset($edit['og_reg_code'])) { //only present during registration
|
| 93 |
if (variable_get('og_reg_code_required', TRUE) || $edit['og_reg_code'] != '') {
|
| 94 |
if (!og_reg_codes_valid_code($edit['og_reg_code'])) {
|
| 95 |
form_set_error('og_reg_code', "Not a valid group registration code.");
|
| 96 |
}
|
| 97 |
}
|
| 98 |
}
|
| 99 |
break;
|
| 100 |
case 'insert':
|
| 101 |
if (isset($edit['og_reg_code']) && og_reg_codes_valid_code($edit['og_reg_code'])) { //only present during registration
|
| 102 |
$gid = og_reg_codes_get_gid($edit['og_reg_code']);
|
| 103 |
$return = og_subscribe_user($gid, $account);
|
| 104 |
if (!empty($return['message'])) {
|
| 105 |
drupal_set_message($return['message']);
|
| 106 |
}
|
| 107 |
$rid = variable_get('og_reg_codes_role', DRUPAL_AUTHENTICATED_RID);
|
| 108 |
if (!in_array($rid, array(DRUPAL_ANONYMOUS_RID, DRUPAL_AUTHENTICATED_RID))) {
|
| 109 |
db_query('INSERT INTO {users_roles} (uid, rid) VALUES (%d, %d)', $account->uid, $rid);
|
| 110 |
}
|
| 111 |
}
|
| 112 |
break;
|
| 113 |
case 'view':
|
| 114 |
// only show to self and admins
|
| 115 |
if ($account->uid == $user->uid || user_access('administer organic groups')) {
|
| 116 |
if ($account->og_groups) {
|
| 117 |
$nids = $rows = $open_groups = array();
|
| 118 |
foreach ($account->og_groups as $key => $val) {
|
| 119 |
$nids[] = $key;
|
| 120 |
}
|
| 121 |
if (count($nids)) {
|
| 122 |
$result = db_query("SELECT nid FROM {og} WHERE nid in (". implode(',', $nids).") AND (og_selective = %d OR og_selective = %d)", OG_OPEN, OG_MODERATED);
|
| 123 |
while ($r = db_fetch_array($result)) {
|
| 124 |
$open_groups[] = $r['nid'];
|
| 125 |
}
|
| 126 |
}
|
| 127 |
if (count($open_groups)) {
|
| 128 |
foreach ($open_groups as $key) {
|
| 129 |
$val = $account->og_groups[$key];
|
| 130 |
$rows[] = array(
|
| 131 |
l($val['title'], "node/$key") . theme('og_format_subscriber_status', $val),
|
| 132 |
og_reg_codes_get_code($key),
|
| 133 |
);
|
| 134 |
}
|
| 135 |
if (count($rows)) {
|
| 136 |
$header = array(t('Group'), t('Registration code'));
|
| 137 |
|
| 138 |
$account->content['summary']['og_registration_codes'] = array(
|
| 139 |
'#type' => 'item',
|
| 140 |
'#title' => t('Registration codes'),
|
| 141 |
'#value' => theme('table', $header, $rows),
|
| 142 |
'#description' => t('You may share these codes to enable others to easily join your groups'),
|
| 143 |
'#attributes' => array('class' => 'og_registration_codes'),
|
| 144 |
// Only show list of groups to self and admins. TOOD: Make this configurable or doable via Views.
|
| 145 |
'#access' => TRUE,
|
| 146 |
'#weight' => 1
|
| 147 |
);
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
}
|
| 152 |
}
|
| 153 |
}
|
| 154 |
}
|
| 155 |
break;
|
| 156 |
}
|
| 157 |
}
|
| 158 |
|
| 159 |
/**
|
| 160 |
* Menu callback function for settings form
|
| 161 |
*
|
| 162 |
*/
|
| 163 |
function og_reg_codes_set() {
|
| 164 |
|
| 165 |
$form['og_reg_code_required'] = array(
|
| 166 |
'#type' => 'checkbox',
|
| 167 |
'#title' => t('Is a registration code required for new users to register at this site?.'),
|
| 168 |
'#default_value' => variable_get('og_reg_code_required', TRUE),
|
| 169 |
'#description' => t("If this is checked, no user will be able to register without a code at /user/register/"),
|
| 170 |
);
|
| 171 |
|
| 172 |
$roles = user_roles(TRUE);
|
| 173 |
$roles[DRUPAL_AUTHENTICATED_RID] = '<'. t('None') .'>';
|
| 174 |
|
| 175 |
$form['og_reg_codes_role'] = array(
|
| 176 |
'#type' => 'select',
|
| 177 |
'#title' => t('Role for new registrants'),
|
| 178 |
'#default_value' => variable_get('og_reg_codes_role', DRUPAL_AUTHENTICATED_RID),
|
| 179 |
'#options' => $roles,
|
| 180 |
'#description' => t('Pick an additional user role that will be assigned to all new users registering with a valid group registration code.'),
|
| 181 |
);
|
| 182 |
|
| 183 |
$form['og_reg_codes_message'] = array(
|
| 184 |
'#type' => 'textarea',
|
| 185 |
'#title' => t('Text to be added to group invitation e-mails'),
|
| 186 |
'#default_value' => _og_reg_codes_message(),
|
| 187 |
'#required' => TRUE,
|
| 188 |
'#description' => t("The token '%reg_code' must be included and will be replaced by the group's registration code."),
|
| 189 |
);
|
| 190 |
|
| 191 |
$date = format_date(_og_reg_codes_get_salt('timestamp'));
|
| 192 |
|
| 193 |
$form['reset_salt'] = array(
|
| 194 |
'#type' => 'submit',
|
| 195 |
'#value' => t('Reset codes'),
|
| 196 |
'#suffix' => '<p>'.t("All existing codes will be invalidated. The last date that new codes were initiated was !date", array('!date' => $date)).'</p>',
|
| 197 |
);
|
| 198 |
|
| 199 |
return system_settings_form($form);
|
| 200 |
}
|
| 201 |
|
| 202 |
function og_reg_codes_set_validate($form, &$form_state) {
|
| 203 |
$op = isset($form_state['values']['op']) ? $form_state['values']['op'] : '';
|
| 204 |
|
| 205 |
if ($op == t('Save configuration') && !strstr($form_state['values']['og_reg_codes_message'], '%reg_code')) {
|
| 206 |
form_set_error('og_reg_codes_message', t("You must include in your message the token '%reg_code'"));
|
| 207 |
}
|
| 208 |
}
|
| 209 |
|
| 210 |
function og_reg_codes_set_submit($form, &$form_state) {
|
| 211 |
if ($form_state['values']['op'] == t('Reset codes')) {
|
| 212 |
variable_del('og_reg_codes_salt');
|
| 213 |
drupal_set_message(t('All registration codes have been reset and given new values.'));
|
| 214 |
}
|
| 215 |
else {
|
| 216 |
unset($form_state['values']['reset_salt']);
|
| 217 |
system_settings_form_submit($form, $form_state['values']);
|
| 218 |
}
|
| 219 |
}
|
| 220 |
|
| 221 |
/**
|
| 222 |
* Menu callback function for "check group" and "join a group" page.
|
| 223 |
*/
|
| 224 |
function og_reg_codes_join() {
|
| 225 |
global $user;
|
| 226 |
|
| 227 |
$form['og_reg_code'] = array(
|
| 228 |
'#type' => 'textfield',
|
| 229 |
'#title' => t('Group registration code'),
|
| 230 |
'#required' => TRUE,
|
| 231 |
'#size' => 25,
|
| 232 |
);
|
| 233 |
|
| 234 |
$form['#after_build'] = array('og_reg_codes_add_preview');
|
| 235 |
|
| 236 |
$form['preview'] = array('#type' => 'button', '#value' => t('Check code'));
|
| 237 |
|
| 238 |
if ($user->uid > 0) {
|
| 239 |
$form['submit'] = array('#type' => 'submit', '#value' => t('Submit and join') );
|
| 240 |
}
|
| 241 |
return $form;
|
| 242 |
}
|
| 243 |
|
| 244 |
function og_reg_codes_add_preview($form, &$form_state) {
|
| 245 |
$op = isset($form['#post']['op']) ? $form['#post']['op'] : '';
|
| 246 |
if ($op == t('Check code')) {
|
| 247 |
drupal_validate_form($form['form_id']['#value'], $form, $form_state);
|
| 248 |
if (!form_get_errors()) {
|
| 249 |
$gid = og_reg_codes_get_gid($form['og_reg_code']['#value']);
|
| 250 |
$node = node_load(array('nid' => $gid));
|
| 251 |
$preview = theme('og_reg_codes_join_preview', $node);
|
| 252 |
$form['#prefix'] = isset($form['#prefix']) ? $preview . $form['#prefix'] : $preview;
|
| 253 |
}
|
| 254 |
}
|
| 255 |
return $form;
|
| 256 |
}
|
| 257 |
|
| 258 |
function og_reg_codes_join_validate($form, &$form_state) {
|
| 259 |
if (!og_reg_codes_valid_code($form_state['values']['og_reg_code'])) {
|
| 260 |
form_set_error('og_reg_code', "Not a valid group registration code.");
|
| 261 |
}
|
| 262 |
}
|
| 263 |
|
| 264 |
function og_reg_codes_join_submit($form, &$form_state) {
|
| 265 |
global $user;
|
| 266 |
|
| 267 |
if ($user->uid) {
|
| 268 |
$gid = og_reg_codes_get_gid($form_state['values']['og_reg_code']);
|
| 269 |
$return = og_subscribe_user($gid, $user);
|
| 270 |
drupal_set_message($return['message']);
|
| 271 |
}
|
| 272 |
else {
|
| 273 |
drupal_access_denied();
|
| 274 |
}
|
| 275 |
}
|
| 276 |
|
| 277 |
function theme_og_reg_codes_join_preview($node) {
|
| 278 |
$output = '<div class="preview">';
|
| 279 |
$output .= '<h3>'. t('By submitting this code, you will join this group:') .'</h3>';
|
| 280 |
$output .= node_view($node, TRUE, FALSE, FALSE);
|
| 281 |
$output .= "</div>\n";
|
| 282 |
|
| 283 |
return $output;
|
| 284 |
}
|
| 285 |
|
| 286 |
/**
|
| 287 |
* Implementation of hook_theme().
|
| 288 |
*/
|
| 289 |
function og_reg_codes_theme() {
|
| 290 |
return array(
|
| 291 |
'og_reg_codes_join_preview' => array(
|
| 292 |
'arguments' => array('node' => NULL),
|
| 293 |
),
|
| 294 |
);
|
| 295 |
}
|
| 296 |
|
| 297 |
/**
|
| 298 |
* @name og_reg_codes utility
|
| 299 |
* @{
|
| 300 |
* Utility functions for this module
|
| 301 |
*/
|
| 302 |
|
| 303 |
function _og_reg_codes_message() {
|
| 304 |
return variable_get('og_reg_codes_message', t("To join this group, use the registration code %reg_code"));
|
| 305 |
}
|
| 306 |
|
| 307 |
/**
|
| 308 |
* Returns the gid part of a registration code, or 0 if the code is
|
| 309 |
* not properly formatted.
|
| 310 |
*/
|
| 311 |
function og_reg_codes_get_gid($code) {
|
| 312 |
$parts = explode('-', $code);
|
| 313 |
if (isset($parts[1]) && is_numeric($parts[1])) {
|
| 314 |
return $parts[1];
|
| 315 |
}
|
| 316 |
return 0;
|
| 317 |
}
|
| 318 |
|
| 319 |
/**
|
| 320 |
* Return a salt to generate the registration code hash, or the
|
| 321 |
* timestamp corresponding to the last time the salt was set.
|
| 322 |
*/
|
| 323 |
function _og_reg_codes_get_salt($op = 'salt') {
|
| 324 |
|
| 325 |
$salt = variable_get('og_reg_codes_salt', FALSE);
|
| 326 |
if (!$salt) {
|
| 327 |
$salt = uniqid(mt_rand(), TRUE);
|
| 328 |
variable_set('og_reg_codes_salt', $salt);
|
| 329 |
variable_set('og_reg_codes_salt_timestamp', time());
|
| 330 |
}
|
| 331 |
if ($op == 'salt') {
|
| 332 |
return $salt;
|
| 333 |
}
|
| 334 |
elseif ($op == 'timestamp') {
|
| 335 |
return variable_get('og_reg_codes_salt_timestamp', 0);
|
| 336 |
}
|
| 337 |
}
|
| 338 |
|
| 339 |
/**
|
| 340 |
* Returns a registration code given a gid.
|
| 341 |
* API function; no validation is done on the gid.
|
| 342 |
*/
|
| 343 |
function og_reg_codes_get_code($gid) {
|
| 344 |
|
| 345 |
$hash = strtoupper(substr(md5($gid. _og_reg_codes_get_salt()), 11, 6));
|
| 346 |
return $hash. '-'. $gid;
|
| 347 |
}
|
| 348 |
|
| 349 |
/**
|
| 350 |
* Returns TRUE if the code has a valid hash, and if it corresponds to a
|
| 351 |
* group that is open or moderated. Returns FALSE otherwise.
|
| 352 |
*/
|
| 353 |
function og_reg_codes_valid_code($code) {
|
| 354 |
$code = strtoupper(trim($code));
|
| 355 |
$gid = og_reg_codes_get_gid($code);
|
| 356 |
if ($gid && og_reg_codes_get_code($gid) == $code) {
|
| 357 |
$group = node_load($gid);
|
| 358 |
if (!empty($group) && isset($group->og_selective)) {
|
| 359 |
if ($group->og_selective == OG_OPEN || $group->og_selective == OG_MODERATED) {
|
| 360 |
return TRUE;
|
| 361 |
}
|
| 362 |
}
|
| 363 |
}
|
| 364 |
return FALSE;
|
| 365 |
}
|
| 366 |
|
| 367 |
/**
|
| 368 |
* @} End of "og_reg_codes utility".
|
| 369 |
*/
|