| 1 |
<?php
|
| 2 |
//$Id: invvoucher.module,v 1.2 2008/09/17 21:39:00 dearanton Exp $
|
| 3 |
|
| 4 |
/**
|
| 5 |
* With this module admin can send to friends invitation vouchers. With it, and only with, user could register at site.
|
| 6 |
*/
|
| 7 |
|
| 8 |
/**
|
| 9 |
* Help
|
| 10 |
*/
|
| 11 |
function invvoucher_help($section = 'admin/modules#description') {
|
| 12 |
$output = '';
|
| 13 |
|
| 14 |
switch ($section) {
|
| 15 |
case 'admin/help#invvoucher':
|
| 16 |
$output .= '<p>'. t('This module adds to administer possibility send to unregistered users invitation voucher.') .'</p>';
|
| 17 |
break;
|
| 18 |
}
|
| 19 |
return $output;
|
| 20 |
}
|
| 21 |
|
| 22 |
/**
|
| 23 |
* Plugin permissions
|
| 24 |
*/
|
| 25 |
function invvoucher_perm() {
|
| 26 |
return array('send invitation voucher');
|
| 27 |
}
|
| 28 |
|
| 29 |
/**
|
| 30 |
* Menu items
|
| 31 |
*/
|
| 32 |
function invvoucher_menu($may_cache) {
|
| 33 |
$items = array();
|
| 34 |
if ($may_cache) {
|
| 35 |
$items[] = array(
|
| 36 |
'path' => 'admin/user/invvoucher',
|
| 37 |
'title' => t('Invitation voucher'),
|
| 38 |
'description' => t('Administer how and where Invitation Voucher are used.'),
|
| 39 |
'callback' => 'drupal_get_form',
|
| 40 |
'callback arguments' => array('invvoucher_admin_settings'),
|
| 41 |
'access' => user_access('administer site configuration'),
|
| 42 |
'type' => MENU_NORMAL_ITEM
|
| 43 |
);
|
| 44 |
|
| 45 |
$items[] = array(
|
| 46 |
'path' => 'invvoucher',
|
| 47 |
'title' => t('Send invitation voucher'),
|
| 48 |
'callback' => 'drupal_get_form',
|
| 49 |
'callback arguments' => array('invvoucher_send_form'),
|
| 50 |
'access' => user_access('send invitation voucher'),
|
| 51 |
'type' => MENU_NORMAL_ITEM
|
| 52 |
);
|
| 53 |
|
| 54 |
$items[] = array(
|
| 55 |
'path' => 'invvoucher/delete',
|
| 56 |
'callback' => 'invvoucher_delete',
|
| 57 |
'access' => user_access('administer site configuration'),
|
| 58 |
'type' => MENU_CALLBACK,
|
| 59 |
);
|
| 60 |
}
|
| 61 |
return $items;
|
| 62 |
}
|
| 63 |
|
| 64 |
/**
|
| 65 |
* Admin settings
|
| 66 |
*/
|
| 67 |
function invvoucher_admin_settings() {
|
| 68 |
global $base_url;
|
| 69 |
|
| 70 |
$form['email_settings'] = array(
|
| 71 |
'#type' => 'fieldset',
|
| 72 |
'#title' => t('Email settings'),
|
| 73 |
'#collapsible' => TRUE,
|
| 74 |
'#collapsed' => FALSE,
|
| 75 |
);
|
| 76 |
|
| 77 |
$form['email_settings']['invvoucher_subject'] = array(
|
| 78 |
'#type' => 'textfield',
|
| 79 |
'#title' => t('Subject'),
|
| 80 |
'#default_value' => variable_get('invvoucher_subject', t("You've been invited")),
|
| 81 |
'#size' => 40,
|
| 82 |
'#maxlength' => 64,
|
| 83 |
'#description' => t('Type the subject of the invitation email.'),
|
| 84 |
'#required' => TRUE,
|
| 85 |
);
|
| 86 |
|
| 87 |
$form['email_settings']['invvoucher_default_mail_template'] = array(
|
| 88 |
'#type' => 'textarea',
|
| 89 |
'#title' => t('Mail template'),
|
| 90 |
'#default_value' => invvoucher_get_mail_template(),
|
| 91 |
'#required' => TRUE,
|
| 92 |
'#rows' =>10,
|
| 93 |
'#description' => t('Use the following placeholders: @site, @homepage, @join_link, @message, @inviter, @code, @expired.'),
|
| 94 |
);
|
| 95 |
|
| 96 |
$form['invvoucher_register_URL'] = array(
|
| 97 |
'#type' => 'textfield',
|
| 98 |
'#title' => t('Registration page URL'),
|
| 99 |
'#default_value' => variable_get('invvoucher_register_URL', $base_url),
|
| 100 |
'#required' => FALSE,
|
| 101 |
'#description' => t('Place here your \'Create account page\' URL'),
|
| 102 |
);
|
| 103 |
return system_settings_form($form);
|
| 104 |
}
|
| 105 |
|
| 106 |
/**
|
| 107 |
* Send invitation voucher form
|
| 108 |
*/
|
| 109 |
function invvoucher_send_form() {
|
| 110 |
|
| 111 |
$form['invvoucher_settings'] = array(
|
| 112 |
'#type' => 'fieldset',
|
| 113 |
'#title' => t('Invitation Voucher settings'),
|
| 114 |
'#collapsible' => TRUE,
|
| 115 |
'#collapsed' => FALSE,
|
| 116 |
);
|
| 117 |
|
| 118 |
$form['invvoucher_settings']['invvoucher_expiration_date'] = array(
|
| 119 |
'#type' => 'textfield',
|
| 120 |
'#title' => t('date of expiration'),
|
| 121 |
'#default_value' => variable_get('invvoucher_expiration_date',date("Y-m-d")),
|
| 122 |
'#required' => TRUE,
|
| 123 |
'#description' => t('Date, when your voucher will expire (YYYY-MM-DD)'),
|
| 124 |
);
|
| 125 |
|
| 126 |
$form['invvoucher_settings']['invvoucher_quantity'] = array(
|
| 127 |
'#type' => 'select',
|
| 128 |
'#title' => t('Voucher quantity'),
|
| 129 |
'#default_value' => variable_get('invvoucher_quantity', 1),
|
| 130 |
'#options' => array(1 => 1, 2 => 2, 3 => 3, 5 => 5, 10 => 10, 20 => 20, -1 => t('Unlimited')),
|
| 131 |
'#description' => t('How much users can use this voucher'),
|
| 132 |
'#multiple' => FALSE,
|
| 133 |
'#required' => TRUE,
|
| 134 |
);
|
| 135 |
|
| 136 |
$form['invvoucher_recipient'] = array(
|
| 137 |
'#type' => 'textfield',
|
| 138 |
'#title' => t('Email of invited person'),
|
| 139 |
'#default_value' => variable_get('invvoucher_recipient',''),
|
| 140 |
'#required' => TRUE,
|
| 141 |
'#description' => t('Email'),
|
| 142 |
);
|
| 143 |
|
| 144 |
$form['invvoucher_code'] = array(
|
| 145 |
'#type' => 'submit',
|
| 146 |
'#value' => t('Send voucher'),
|
| 147 |
);
|
| 148 |
|
| 149 |
// Show list of sent invitations
|
| 150 |
$form['invvoucher_sent'] = array(
|
| 151 |
'#type' => 'fieldset',
|
| 152 |
'#title' => t('Sent invitation vouchers'),
|
| 153 |
'#collapsible' => TRUE,
|
| 154 |
'#collapsed' => TRUE,
|
| 155 |
);
|
| 156 |
|
| 157 |
if ( user_access('administer site configuration') ) {
|
| 158 |
$result = db_query('SELECT uid, name FROM {users} WHERE 1');
|
| 159 |
$users = array();
|
| 160 |
while ( $row = mysql_fetch_array($result) )
|
| 161 |
$users[ $row['uid'] ] = $row['name'];
|
| 162 |
|
| 163 |
global $user;
|
| 164 |
|
| 165 |
$form['invvoucher_sent']['filter'] = array(
|
| 166 |
'#type' => 'select',
|
| 167 |
'#title' => t('Filter'),
|
| 168 |
'#default_value' => $user->uid,
|
| 169 |
'#options' => $users,
|
| 170 |
'#description' => t('Choose voucher owners to view'),
|
| 171 |
'#multiple' => FALSE,
|
| 172 |
'#required' => FALSE,
|
| 173 |
);
|
| 174 |
}
|
| 175 |
|
| 176 |
/**
|
| 177 |
*Display list of sent and registerd with them users
|
| 178 |
*/
|
| 179 |
// select all availble invitation vouchers
|
| 180 |
$query = db_query("SELECT * FROM {invvoucher} ORDER BY date_exp DESC");
|
| 181 |
//if (!empty($query)) {
|
| 182 |
while ($row = mysql_fetch_array($query)) {
|
| 183 |
$name = '';
|
| 184 |
$email = '';
|
| 185 |
$created = '';
|
| 186 |
if (user_access('administer site configuration'))
|
| 187 |
$delete_link=l(t('delete'), 'invvoucher/delete/'.urlencode($row['code']));
|
| 188 |
else
|
| 189 |
$delete_link='';
|
| 190 |
// look for users registered with each voucher
|
| 191 |
$subquery = db_query("SELECT t1.*, t2.mail, t2.created FROM {users} AS t2, {invvoucher_users} AS t1 WHERE t1.code = '%s' AND t2.name = t1.uid", $row['code']);
|
| 192 |
while ( $sub_row = mysql_fetch_array($subquery) ) {
|
| 193 |
$name.=$sub_row['uid'].'<BR>';
|
| 194 |
$email.=$sub_row['mail'].'<BR>';
|
| 195 |
$created.=date("Y-m-d",$sub_row['created']).'<BR>';
|
| 196 |
}
|
| 197 |
if ( empty( $name ) ) {
|
| 198 |
$name = t('No registration yet');
|
| 199 |
$email = '-';
|
| 200 |
$created = '-';
|
| 201 |
}
|
| 202 |
$expired = date("Y-m-d", $row['date_exp']);
|
| 203 |
// color expired date with red
|
| 204 |
if ( $row['date_exp'] < time() )
|
| 205 |
$expired = "<div style='color : red'>".$expired."</div>";
|
| 206 |
|
| 207 |
$items[] = array($row['code'], $name, $email, $created,($row['reg_count']!=-1)?$row['reg_count']:t('Unlimited'), $expired, $delete_link);
|
| 208 |
}
|
| 209 |
// format results as table
|
| 210 |
if ( count($items)>0 ) {
|
| 211 |
$header = array(t('Voucher'), t('Nickname(s)'),t('Email(s)'), t('Registered'), t('Quantity'), t('Voucher expires'),'');
|
| 212 |
$table = theme('table', $header, $items, array('id' => 'invvoucher_sent_table'));
|
| 213 |
}
|
| 214 |
//}
|
| 215 |
else {
|
| 216 |
$header = array (' ');
|
| 217 |
$items[] = array ( t("No invitation vouchers was sent") );
|
| 218 |
$table = theme('table', $header, $items, array('id' => 'invvoucher_sent_table'));
|
| 219 |
}
|
| 220 |
|
| 221 |
$form['invvoucher_sent']['invvoucher_sent_table'/*invvouchers'*/] = array(
|
| 222 |
'#type' => 'markup',
|
| 223 |
'#value' => $table,
|
| 224 |
);
|
| 225 |
|
| 226 |
return $form;
|
| 227 |
}
|
| 228 |
|
| 229 |
/**
|
| 230 |
* Send email with code
|
| 231 |
*/
|
| 232 |
function invvoucher_send_invite($op, $user_name = "[username]", $email ="[recipient-email]", $code = "registration-code", $message = '[your message]', $expired='[days left]', $quantity='[possible registrations]'){
|
| 233 |
global $base_url, $user;
|
| 234 |
|
| 235 |
$subject = variable_get('invvoucher_subject', t("You've been invited"));
|
| 236 |
$template = invvoucher_get_mail_template();
|
| 237 |
$site = variable_get('site_name', t('Drupal'));
|
| 238 |
$join_link = variable_get('invvoucher_register_URL', $base_url);
|
| 239 |
$homepage = $base_url;
|
| 240 |
$inviter = $user_name;
|
| 241 |
if ($quantity == -1)
|
| 242 |
$quantity = t("any number of");
|
| 243 |
|
| 244 |
$body = t($template, array('@site' => $site, '@join_link' => $join_link, '@homepage' => $homepage, '@message' => $message, '@inviter' => $inviter, '@number' => $code, '@expired' => date("Y-m-d",$expired), '@quantity'=> $quantity));
|
| 245 |
|
| 246 |
switch ($op){
|
| 247 |
case "mail":
|
| 248 |
if (!($success = drupal_mail('invite-mail', $email, $subject, wordwrap($body, 72), $from))) {
|
| 249 |
static $error_shown = FALSE;
|
| 250 |
if (!$error_shown) {
|
| 251 |
drupal_set_message(t('Problems occurred sending the invitation(s). Please contact the site administrator.'), 'error');
|
| 252 |
$error_shown = TRUE;
|
| 253 |
}
|
| 254 |
watchdog('invite', t("Failed sending invitation. To: @email From: @from", array('@email' => $email, '@from' => $from)));
|
| 255 |
}
|
| 256 |
return $success;
|
| 257 |
break;
|
| 258 |
}
|
| 259 |
}
|
| 260 |
|
| 261 |
/**
|
| 262 |
* Send form submit handler
|
| 263 |
*/
|
| 264 |
function invvoucher_send_form_submit($form_id, $edit) {
|
| 265 |
|
| 266 |
global $user;
|
| 267 |
$errors =0;
|
| 268 |
$email = $edit['invvoucher_recipient'];
|
| 269 |
if (!valid_email_address($email)) {
|
| 270 |
drupal_set_message( t('Incorrect email'),'error');
|
| 271 |
$errors++;
|
| 272 |
}
|
| 273 |
$code = invvoucher_create();
|
| 274 |
$message = trim($edit['message']);
|
| 275 |
$quantity = $edit['invvoucher_quantity'];
|
| 276 |
|
| 277 |
if (!$expired = datestr2unixtime($edit['invvoucher_expiration_date'])) {
|
| 278 |
$errors++;
|
| 279 |
}
|
| 280 |
else
|
| 281 |
// invitation expires at 23.59, not at 00.00 of the same day!
|
| 282 |
$expired += 60*60*23+60*59;
|
| 283 |
if (!$errors)
|
| 284 |
if (invvoucher_send_invite('mail', $user->name, $email, $code, $message, $expired, $quantity)) {
|
| 285 |
db_query("INSERT INTO {invvoucher} (code, reg_count, date_sent, date_exp, email) VALUES ('%s', '%d', '%d', '%d', '%s')",
|
| 286 |
$code, $quantity, time(), $expired, $email);
|
| 287 |
// notify other modules
|
| 288 |
$args = array('inviter' => $user);
|
| 289 |
module_invoke_all('invvoucher', 'invvoucher', $args);
|
| 290 |
drupal_set_message(t('Invitation voucher was sent to ').$email);
|
| 291 |
}
|
| 292 |
}
|
| 293 |
|
| 294 |
/**
|
| 295 |
* Default mail template
|
| 296 |
*/
|
| 297 |
function invvoucher_get_mail_template() {
|
| 298 |
|
| 299 |
$template = t("Dear friend, @inviter has invited you to join @site [@homepage].
|
| 300 |
|
| 301 |
To become a member of @site, click this link @join_link or paste it into the address bar of your browser and enter in registration form folowing number: @number.
|
| 302 |
|
| 303 |
You can register @quantity user(s) with this number at our site before @expired
|
| 304 |
|
| 305 |
----------
|
| 306 |
|
| 307 |
@message");
|
| 308 |
|
| 309 |
return variable_get('invvoucher_default_mail_template', $template);
|
| 310 |
}
|
| 311 |
|
| 312 |
/**
|
| 313 |
* Create voucher
|
| 314 |
*/
|
| 315 |
function invvoucher_create() {
|
| 316 |
do {
|
| 317 |
$regcode='';
|
| 318 |
for ($i=1; $i<=10; $i++)
|
| 319 |
$reg_code.=rand(0,9);
|
| 320 |
$r = db_query("SELECT COUNT(*) FROM {invvoucher} WHERE code = '%s'", $reg_code);
|
| 321 |
} while ((int)db_result($r) > 0);
|
| 322 |
return $reg_code;
|
| 323 |
}
|
| 324 |
|
| 325 |
/**
|
| 326 |
* Validate voucher
|
| 327 |
*/
|
| 328 |
function invvoucher_validate ($code) {
|
| 329 |
$r = mysql_fetch_array( db_query("SELECT * FROM {invvoucher} WHERE code = '%s'", $code) );
|
| 330 |
if ( !empty($r)) {
|
| 331 |
if ( $r[3]<time()) {// exp_date
|
| 332 |
form_set_error('invvoucher_response', t('The code you entered has expired.'));
|
| 333 |
return 0;
|
| 334 |
}
|
| 335 |
if ($r[1]==0) { //quantity
|
| 336 |
form_set_error('invvoucher_response', t('The code you entered can be used no more.'));
|
| 337 |
return 0;
|
| 338 |
}
|
| 339 |
if ( $r[0] == $code)
|
| 340 |
return 1;
|
| 341 |
}
|
| 342 |
form_set_error('invvoucher_response', t('The code you entered is incorrect.'));
|
| 343 |
return 0;
|
| 344 |
}
|
| 345 |
|
| 346 |
/**
|
| 347 |
* Override registration form
|
| 348 |
*/
|
| 349 |
function invvoucher_form_alter($form_id, &$form) {
|
| 350 |
if ($form_id == 'user_register') {
|
| 351 |
$form['#submit'] = array('invvoucher_submit' => array()) + $form['#submit'];
|
| 352 |
if (module_hook('invvoucher', 'invvoucherchallenge')) {
|
| 353 |
call_user_func_array('invvoucher_invvoucherchallenge', array(&$form, &$_SESSION['invvoucher']));
|
| 354 |
//check only if Code field is filled
|
| 355 |
if ($_POST['invvoucher_response'])
|
| 356 |
if (invvoucher_validate ($_POST['invvoucher_response'])) {
|
| 357 |
$_SESSION['invvoucher_correct'] = $_POST['invvoucher_response'];
|
| 358 |
}
|
| 359 |
}
|
| 360 |
}
|
| 361 |
}
|
| 362 |
|
| 363 |
/**
|
| 364 |
* Additional registration form elements
|
| 365 |
*/
|
| 366 |
function invvoucher_invvoucherchallenge(&$form, &$invvoucher) {
|
| 367 |
$form['invvoucher_response'] = array (
|
| 368 |
'#type' => 'textfield',
|
| 369 |
'#title' => t('Registration code'),
|
| 370 |
'#description' => t('Enter here your registration code from email'),
|
| 371 |
'#weight' => 0,
|
| 372 |
'#required' => TRUE,
|
| 373 |
'#validate' => array('_invvoucher_validate' => array()),
|
| 374 |
);
|
| 375 |
}
|
| 376 |
|
| 377 |
/**
|
| 378 |
* Submit form handler
|
| 379 |
*/
|
| 380 |
function invvoucher_submit() {
|
| 381 |
if($_SESSION['invvoucher_correct']) {
|
| 382 |
invvoucher_add_registered_email();
|
| 383 |
invvoucher_decrement($_POST['invvoucher_response']);
|
| 384 |
unset($_SESSION['invvoucher_correct']);
|
| 385 |
unset($_SESSION['invvoucher']);
|
| 386 |
}
|
| 387 |
}
|
| 388 |
|
| 389 |
function invvoucher_invvouchervalidate(&$invvoucher_word, &$correct) {
|
| 390 |
if ( invvoucher_validate($invvoucher_word) ) {
|
| 391 |
$correct = TRUE;
|
| 392 |
}
|
| 393 |
else {
|
| 394 |
$correct = FALSE;
|
| 395 |
form_set_error('invvoucher_response', t('The code you entered is incorrect.'));
|
| 396 |
}
|
| 397 |
}
|
| 398 |
|
| 399 |
function _invvoucher_validate($invvoucher_response) {
|
| 400 |
if ($_SESSION['invvoucher_correct']) {
|
| 401 |
return TRUE;
|
| 402 |
}
|
| 403 |
if (is_array($invvoucher_response)) {
|
| 404 |
$invvoucher_response = $invvoucher_response['#value'];
|
| 405 |
}
|
| 406 |
global $user;
|
| 407 |
if (module_hook('invvoucher', 'invvouchervalidate')) {
|
| 408 |
call_user_func_array('invvoucher_invvouchervalidate', array(&$invvoucher_response, &$_SESSION['invvoucher_correct']));
|
| 409 |
}
|
| 410 |
return $_SESSION['invvoucher_correct'];
|
| 411 |
}
|
| 412 |
|
| 413 |
/**
|
| 414 |
* Decrement of voucher code quantity
|
| 415 |
*/
|
| 416 |
function invvoucher_decrement($invvoucher_word) {
|
| 417 |
$quantity = mysql_fetch_array( db_query("SELECT reg_count FROM {invvoucher} WHERE code = '%s'", $invvoucher_word) );
|
| 418 |
if ($quantity['reg_count']!=0)
|
| 419 |
db_query("UPDATE {invvoucher} SET reg_count = %d WHERE code = '%s' AND reg_count <> -1", $quantity['reg_count']-1, $invvoucher_word);
|
| 420 |
}
|
| 421 |
|
| 422 |
/**
|
| 423 |
* Convert date string to UNIX timestamp with validation
|
| 424 |
*/
|
| 425 |
function datestr2unixtime($strDate,$timeDefer=0) {
|
| 426 |
if (preg_match('/^([0-9]{4})-([0-9]{1,2})-([0-9]{1,2})$/', $strDate)) {
|
| 427 |
$dateArr = explode('-', $strDate);
|
| 428 |
if(checkdate($dateArr[1], $dateArr[2], $dateArr[0])) {
|
| 429 |
return mktime(0, 0, 0, $dateArr[1], $dateArr[2], $dateArr[0])-($timeDefer*3600);
|
| 430 |
} else {
|
| 431 |
drupal_set_message( t('Incorrect date'),'error');
|
| 432 |
return false;
|
| 433 |
}
|
| 434 |
} else {
|
| 435 |
drupal_set_message( t('Incorrect date formatting'),'error');
|
| 436 |
return false;
|
| 437 |
}
|
| 438 |
}
|
| 439 |
|
| 440 |
/**
|
| 441 |
* Remember email of person, registered with this voucher in table invvoucher_users
|
| 442 |
*/
|
| 443 |
function invvoucher_add_registered_email() {
|
| 444 |
db_query( "INSERT INTO {invvoucher_users} (code, uid) VALUES ('%s','%s')", $_POST['invvoucher_response'], $_POST['name'] );
|
| 445 |
}
|
| 446 |
|
| 447 |
function invvoucher_delete($code) {
|
| 448 |
global $user;
|
| 449 |
$code = urldecode($code);
|
| 450 |
db_query("DELETE FROM {invvoucher_users} WHERE code = '%s'", $code);
|
| 451 |
$result = db_query("DELETE FROM {invvoucher} WHERE code = '%s'", $code);
|
| 452 |
if ( !empty( $result ) )
|
| 453 |
drupal_set_message(t('Voucher ').$code.t(' was successfully deleted.'));
|
| 454 |
else
|
| 455 |
watchdog('invvoucher', t('Detected malicious attempt to delete an invitation voucher.'), WATCHDOG_WARNING, l(t('view'), 'user/'. $user->uid));
|
| 456 |
drupal_goto('invvoucher');
|
| 457 |
}
|