'. t("This module adds to any user with permission 'send invitation voucher' possibility send to unregistered users a voucher, required for registration at the site.") .'

'; break; } return $output; } /** * Implementation of hook_perm * Defines new permission at standard permissions list */ function invvoucher_perm() { return array('send invitation voucher'); } /** * Implementation of hook_menu * Registers menu items and hooks for it */ function invvoucher_menu() { $items = array(); $items['admin/user/invvoucher'] = array( 'title' => t('Invitation voucher'), 'description' => t('Administer how and where Invitation Voucher are used.'), 'page callback' => 'drupal_get_form', 'page arguments' => array('invvoucher_admin_settings'), 'access arguments' => array('administer site configuration'), 'type' => MENU_NORMAL_ITEM ); $items['invvoucher'] = array( 'title' => t('Invite a friend!'), 'page callback' => 'drupal_get_form', 'page arguments' => array('invvoucher_send_form'), 'access arguments' => array('Invite'), 'type' => MENU_NORMAL_ITEM ); $items['invvoucher/delete'] = array( 'page callback' => 'invvoucher_delete', 'access arguments' => array('administer site configuration'), 'type' => MENU_CALLBACK, ); return $items; } /** * Admin Settings form */ function invvoucher_admin_settings() { global $base_url; $form['email_settings'] = array( '#type' => 'fieldset', '#title' => t('Email settings'), '#collapsible' => TRUE, '#collapsed' => FALSE, ); /* $form['email_settings']['invvoucher_subject'] = array( '#type' => 'textfield', '#title' => t('Subject'), '#default_value' => variable_get('invvoucher_subject', t("You've been invited")), '#size' => 40, '#maxlength' => 64, '#description' => t('Type the subject of the invitation email.'), '#required' => TRUE, ); */ $form['email_settings']['invvoucher_default_mail_template'] = array( '#type' => 'textarea', '#title' => t('Mail template'), '#default_value' => invvoucher_get_mail_template(), '#required' => TRUE, '#rows' =>10, '#description' => t('Use the following placeholders: @site, @homepage, @join_link, @message, @inviter, @code, @expired.'), ); $form['invvoucher_register_URL'] = array( '#type' => 'textfield', '#title' => t('Registration page URL'), '#default_value' => variable_get('invvoucher_register_URL', $base_url), '#required' => FALSE, '#description' => t('Place here your \'Create account page\' URL'), ); return system_settings_form($form); } /** * Create Invitation Voucher send form */ function invvoucher_send_form() { /* $form['invvoucher_settings'] = array( '#type' => 'fieldset', '#title' => t('Invitation Voucher settings'), '#collapsible' => TRUE, '#collapsed' => FALSE, ); */ /* $form['invvoucher_settings']['invvoucher_quantity'] = array( '#type' => 'select', '#title' => t('Voucher quantity'), '#default_value' => variable_get('invvoucher_quantity', 1), '#options' => array(1 => 1, 2 => 2, 3 => 3, 5 => 5, 10 => 10, 20 => 20, -1 => t('Unlimited')), '#description' => t('How much users can use this voucher'), '#multiple' => FALSE, '#required' => TRUE, ); if ( user_access('administer site configuration') ) { $form['invvoucher_settings']['invvoucher_code'] = array( '#type' => 'textfield', '#title' => t('Your own voucher word'), '#default_value' => variable_get('invvoucher_code',''), '#required' => FALSE, '#description' => t("If you plan create a voucher youself, something like 'friends', 'invitors2008', etc. place it here"), ); } */ $form['invvoucher_recipient'] = array( '#type' => 'textfield', '#title' => t('Email'), '#default_value' => variable_get('invvoucher_recipient',''), '#required' => TRUE, '#description' => t('Email address of the person you are inviting.'), ); $form['invvoucher_settings']['invvoucher_expiration_date'] = array( '#type' => 'textfield', '#title' => t('Expiration date'), '#default_value' => variable_get('invvoucher_expiration_date',date("Y-m-d", 30*86400 + time())), '#required' => TRUE, '#description' => t(''), ); $form['invvoucher_submit'] = array( '#type' => 'submit', '#value' => t('Send invite'), ); // Show list of sent invitations $form['invvoucher_sent'] = array( '#type' => 'fieldset', '#title' => t('Sent invitations'), '#collapsible' => TRUE, '#collapsed' => TRUE, ); /** *Display list of sent invitations */ global $user; $uid = $user->uid; if (user_access('administer site configuration')) $condition = '1'; else $condition = "t1.owner_uid=$uid"; $query = db_query( "SELECT t1.* , t2.uid, t3.name AS owner_name FROM `invvoucher` AS t1 LEFT JOIN (`invvoucher_users` AS t2) ON ( t1.id = t2.voucher_id ) LEFT JOIN ( `users` AS t3 ) ON ( t1.owner_uid = t3.uid ) WHERE $condition"); // select all availble invitation vouchers if ($query && db_affected_rows()) { $voucher_data = array(); while ($row = db_fetch_array($query)) { $voucher_data[] = $row; } /* * Move vouchers data and users who registered with it to arrays */ $users = array(); $vouchers = array(); foreach ($voucher_data as $key=>$value) { $id = $value['id']; $vouchers[ $id ] = $value; if ($value['uid']) $users[ $id ][] = $value['uid']; } /* * Form table rows */ $items = array(); foreach ($vouchers as $key=>$value) { if ( empty( $users[$key] ) ) $registered = t('No'); else { $registered='Yes, as '; $i=1; foreach($users[$key] as $k=>$v) { if ( $i++>=($users[$key]) ) $comma = ''; else $comma = ','; $registered ='' . 'Yes, as '. '' . $v . '' . ''; } } $code = $value['code']; $sent_to = $value['email']; $quantity = ($value['reg_count']==-1)?t('unlimited'):$value['reg_count']; $expired = date("Y-m-d", $value['date_exp']); // color expired date with red if ( $value['date_exp'] < time() ) $expired = "
".$expired."
"; if (user_access('send invitation voucher')) { $delete_link=l(t('delete'), 'invvoucher/delete/'.urlencode($value['id'])); $owner = $value['owner_name']; // echo '
';
// 				print_r($value);
// 				echo '
'; } else { $owner = ''; $delete_link=''; } $items[] = array($code, $sent_to, $expired, $registered, $delete_link); } } /* * Display the table with vouchers information */ if (user_access('send invitation voucher')) { $owner_label = t('Owner'); } else $owner_label=''; if ( count($items)>0 ) { $header = array(t('Code'), t('Sent to'), t('Expires on'),t('Have they logged in yet?'),''); } else { $header = array (' '); $items[] = array ( t("You haven't invited anyone yet. What are you waiting for?") ); } $table = theme('table', $header, $items, array('id' => 'invvoucher_sent_table')); $form['invvoucher_sent']['invvoucher_sent_table'] = array( '#type' => 'markup', '#value' => $table, ); return $form; } /** * Send email with code */ function invvoucher_send_invite($op, $user_name = '[username]', $email = '[recipient-email]', $code = 'registration-code', $message = '[your message]', $expired='[days left]', $quantity='[possible registrations]'){ global $base_url, $user; $inviter = $user_name; $default_from = variable_get('site_mail', ini_get('sendmail_from')); $subject = $inviter . ' has invited you to ModelFor.Us'; $template = invvoucher_get_mail_template(); $site = variable_get('site_name', t('Drupal')); $join_link = variable_get('invvoucher_register_URL', $base_url); $homepage = $base_url; if ($quantity == -1){ $quantity = t("any number of"); } $body = t($template, array('@site' => $site, '@join_link' => $join_link, '@homepage' => $homepage, '@message' => $message, '@inviter' => $inviter, '@code' => $code, '@expired' => date("Y-m-d",$expired), '@quantity'=> $quantity)); // Bundle up the variables into a structured array for altering. $message = array( 'id' => 'invite-email', 'to' => $email, 'from' => isset($from) ? $from : $default_from, 'language' => $language, 'params' => $params, 'subject' => $subject, 'body' => $body ); // Build the default headers $headers = array( 'MIME-Version' => '1.0', 'Content-Type' => 'text/plain; charset=UTF-8; format=flowed; delsp=yes', 'Content-Transfer-Encoding' => '8Bit', 'X-Mailer' => 'Drupal' ); if ($default_from) { // To prevent e-mail from looking like spam, the addresses in the Sender and // Return-Path headers should have a domain authorized to use the originating // SMTP server. Errors-To is redundant, but shouldn't hurt. $headers['From'] = $headers['Reply-To'] = $headers['Sender'] = $headers['Return-Path'] = $headers['Errors-To'] = $default_from; } if ($from) { $headers['From'] = $headers['Reply-To'] = $from; } $message['headers'] = $headers; // Concatenate and wrap the e-mail body. //$message['body'] = is_array($message['body']) ? drupal_wrap_mail(implode("\n\n", $message['body'])) : drupal_wrap_mail($message['body']); // Optionally send e-mail. $message['result'] = drupal_mail_send($message); // Log errors if (!$message['result']) { watchdog('mail', 'Error sending e-mail (from %from to %to).', array('%from' => $message['from'], '%to' => $message['to']), WATCHDOG_ERROR); drupal_set_message(t('Unable to send e-mail. Please contact the site admin, if the problem persists.'), 'error'); } return $message['result']; } /** * Send form submit handler */ function invvoucher_send_form_submit($form_id, $edit) { global $user; $errors =0; $email = $edit['values']['invvoucher_recipient']; $expiration_date = $edit['values']['invvoucher_expiration_date']; $message = trim($edit['values']['message']); $quantity = '1'; if ( !valid_email_address($email) ) { drupal_set_message( t('You must enter a valid email address'), 'error' ); $errors++; } /* * Check handmade voucher */ if ( !empty($edit['values']['invvoucher_code'] ) ) { if ( user_access('administer site configuration') ) { $code = $edit['values']['invvoucher_code']; $r = db_query("SELECT COUNT(*) FROM {invvoucher} WHERE `code` = '%s'", $code); if ( db_result($r)!=0 ) { $errors++; drupal_set_message('Such voucher is already created at the system.', 'error'); } } } else $code = invvoucher_create(); if (!$code) { $errors++; } /* * Check date expiration */ if (!$expired = datestr2unixtime($expiration_date)) { $errors++; } else // invitation expires at 23.59, not at 00.00 of the same day! $expired += 60*60*23+60*59; if (!$errors) if ( invvoucher_send_invite('mail', $user->name, $email, $code, $message, $expired, $quantity) ) { global $user; $uid = $user->uid; db_query("INSERT INTO {invvoucher} (code, reg_count, date_sent, date_exp, email, owner_uid) VALUES ('%s', '%d', '%d', '%d', '%s', '%d')", $code, $quantity, time(), $expired, $email, $uid); // notify other modules $args = array('inviter' => $user); module_invoke_all('invvoucher', 'invvoucher', $args); drupal_set_message(t('Your invitation has been sent to ').$email); } } /** * Default mail template */ function invvoucher_get_mail_template() { $template = t("Dear friend, @inviter has invited you to join @site [@homepage]. 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 code: @number. You can register @quantity user(s) with this code at our site before @expired ---------- @message"); return variable_get('invvoucher_default_mail_template', $template); } /** * Create voucher */ function invvoucher_create() { do { $reg_code=''; for ($i=1; $i<=10; $i++) $reg_code.=rand(0,9); $r = db_query("SELECT COUNT(*) FROM {invvoucher} WHERE code = '%s'", $reg_code); } while ( (int)db_result($r) > 0 ); return $reg_code; } /** * Validate voucher CHECKED */ function invvoucher_validate ($code) { @$r = db_fetch_array( db_query("SELECT * FROM {invvoucher} WHERE code = '%s'", $code) ); if ( !empty($r)) { if ( $r['date_exp'] array()) + $form['#submit']; if (module_hook('invvoucher', 'invvoucherchallenge')) { call_user_func_array('invvoucher_invvoucherchallenge', array(&$form, &$_SESSION['invvoucher'])); //check only if Code field is filled if ($_POST['invvoucher_response']) if (invvoucher_validate ($_POST['invvoucher_response'])) $_SESSION['invvoucher_correct'] = $_POST['invvoucher_response']; } } } /** * Additional registration form elements */ function invvoucher_invvoucherchallenge(&$form, &$invvoucher) { $form['invvoucher_response'] = array ( '#type' => 'textfield', '#title' => t('Invite code'), '#description' => t('This site is invite only. Enter your invite code here.'), '#weight' => 3, '#required' => TRUE, '#validate' => array('_invvoucher_validate' => array()), ); } /** * Decrement of voucher code quantity */ function invvoucher_decrement($invvoucher_word=0) { $quantity = db_fetch_array( db_query("SELECT reg_count FROM {invvoucher} WHERE id = %d", $invvoucher_word) ); if ($quantity['reg_count'] > 0) db_query("UPDATE {invvoucher} SET reg_count = %d WHERE id = '%s'", $quantity['reg_count']-1, $invvoucher_word); } /** * Convert date string to UNIX timestamp with validation */ function datestr2unixtime($strDate,$timeDefer=0) { if (preg_match('/^([0-9]{4})-([0-9]{1,2})-([0-9]{1,2})$/', $strDate)) { $dateArr = explode('-', $strDate); if(checkdate($dateArr[1], $dateArr[2], $dateArr[0])) { return mktime(0, 0, 0, $dateArr[1], $dateArr[2], $dateArr[0])-($timeDefer*3600); } else { drupal_set_message( t('Incorrect date'),'error'); return false; } } else { drupal_set_message( t('Incorrect date formatting'),'error'); return false; } } /** * Remember email of person, registered with this voucher in table invvoucher_users */ function invvoucher_add_registered_email($id='', $uid='') { if ($id && $uid) db_query( "INSERT INTO invvoucher_users (voucher_id, code, uid) VALUES ('%d', '%s','%s')", $id, $_POST['invvoucher_response'], $uid ); } /* * delete any appearance of voucher at database. For admins only */ function invvoucher_delete($code=0) { global $user; if ( empty($code) ) { drupal_goto('invvoucher'); return 0; } if ( !user_access('administer site configuration') ) { drupal_goto('invvoucher'); return 0; } $code = urldecode($code); db_query("DELETE FROM invvoucher_users WHERE voucher_id = '%d'", $code); $result = db_query("DELETE FROM invvoucher WHERE id = '%d'", $code); if ( !empty( $result ) ) drupal_set_message(t('Invitation has been deleted')); else watchdog('invvoucher', t('Detected malicious attempt to delete an invitation voucher.'), WATCHDOG_WARNING, l(t('view'), 'user/'. $user->uid)); drupal_goto('invvoucher'); }