t('Relational Matrix'),
'description' => t('Browse RCSA relational matrix.'),
'page callback' => 'rcsa_show_matrix',
'access arguments' => array('administer content types')
);
// Registration and login pages.
$items['rcsa/login'] = array(
'title' => 'User account',
'page callback' => 'rcsa_page',
'access callback' => TRUE,
'type' => MENU_CALLBACK,
);
$items['admin/sync'] = array(
'title' => t('Sync IMIS'),
'description' => t(''),
'page callback' => 'rcsa_sync_imis',
'access arguments' => array('administer content types')
);
return $items;
}
/*
* Implementing hook_cron
*/
function rcsa_cron() {
if($connection = rcsa_mssql_connect()) {
drupal_set_message('Connected to IMIS','status');
//Reading the RCSA_Members table and inserting into Drupal
//rcsa_synchronize($connection);
} else {
drupal_set_message('Could not connect to IMIS','error');
watchdog('system', 'Could not connect to IMIS DB.', array(), WATCHDOG_ERROR);
}
//rcsa_mssql_disconnect($sqlconnect);
rcsa_mssql_disconnect($connection);
}
function rcsa_sync_imis() {
if($connection = rcsa_mssql_connect()) {
drupal_set_message('Connected to IMIS','status');
//Reading the RCSA_Members table and inserting into Drupal
rcsa_synchronize($connection);
} else {
drupal_set_message('Could not connect to IMIS','error');
watchdog('system', 'Could not connect to IMIS DB.', array(), WATCHDOG_ERROR);
}
}
/**
* Implementation of hook_form_alter().
*/
/*function rcsa_form_alter(&$form, $form_state, $form_id) {
if($form_id == 'user_login' || $form_id == 'user_login_block') {
$form['#action'] = '/rcsa_login';
//$form['#validate'] = 'rcsa_login_validator';
$form['name']['#title'] = t('Member Number');
//$form['#submit'] = 'user_login_submit';
}
}*/
function portal($area_name, $area_id) {
drupal_goto($area_name."/".$area_id);
}
/**
* Implementation of hook_perm().
*/
function rcsa_perm() {
return array('administer RCSA');
}
/**
* Implementation of hook_nodeapi().
*/
/*function rcsa_nodeapi(&$node, $op, $teaser, $page) {
switch ($op) {
case 'load':
$output .= 'Cons';
return $output;
break;
case 'insert':
$body = $node->body;
// Copying over the node body is optional.
$body = isset($nat_config['body'][$node->type]) ? $body : '';
// Add node title as terms.
$terms = _nat_add_terms($nat_config['types'][$node->type], $node->title, $body, $node->taxonomy, $node->nat['related'], $node->nat['synonyms']);
// Save node-term association in the NAT table.
_nat_save_association($node->nid, $terms);
break;
case 'update':
$body = $node->body;
// Copying over the node body is optional.
$body = isset($nat_config['body'][$node->type]) ? $body : '';
// Update node title in term(s).
$terms = nat_get_terms($node->nid);
_nat_update_terms($terms, $node->title, $body, $node->taxonomy, $node->nat['related'], $node->nat['synonyms']);
break;
case 'delete':
// Deleting the associated term when a node is deleted is optional.
if (isset($nat_config['delete'][$node->type])) {
_nat_delete_terms($node->nid);
}
// Delete node-term association from the NAT table.
_nat_delete_association($node->nid);
break;
}
}*/
/**
* Implementation of hook_link_alter().
*/
//function rcsa_link_alter(&$links, $node) {}
/**
* Menu callback: RCSA matrix.
*/
function rcsa_show_matrix() {
if (user_access('administer RCSA')) {
//drupal_set_message("Matrix",'status');
$matrix_sql = db_query("SELECT w.portal,t.name FROM {wp_matrix} w LEFT JOIN {term_data} t ON t.tid = w.cid ORDER BY w.portal ASC");
//if($matrix = db_fetch_object($matrix_sql)) {
$output .= '
';
while($matrix = db_fetch_object($matrix_sql)) {
$output .= '| ' . $matrix->portal . ' | ' . $matrix->name . ' |
';
}
$output .= '
';
//}
return $output;
} else {
return $output;
}
}
/*
* A function that returns category IDs for the given portal entry as $cname option filtered by nodes
* existing in this category and the portal entry
*/
function rcsa_items($cname) {
$cat_query = db_query("SELECT DISTINCT m.cid,m.portal,td.name,n.nid FROM {content_field_category} c LEFT JOIN {wp_matrix} m ON m.cid = c.field_category_value LEFT JOIN {term_data} td ON td.tid = m.cid LEFT JOIN {node} n ON n.vid = c.vid LEFT JOIN {content_field_archive} a ON a.vid = n.vid WHERE n.type = '%s' AND n.status = '%d' AND a.field_archive_value IS NULL AND td.vid = '%d' ORDER BY m.cid ASC", 'page', 1, 11);
while($category = db_fetch_array($cat_query)) {
$node = node_load($category['nid']);
if (node_access('view', $node)) {
$cat[] = $category['cid']; // Term IDs for Categories
$portal[] = $category['portal']; // portal name
$category_name[] = $category['name'];
}
}
//$port = array_combine($portal,$cat);
$i = 0;
foreach($cat as $value) {
switch($portal[$i]) {
case $cname:
$filter[$cname][$category_name[$i]] = $value;
break;
}
$i++;
}
//return $port;
return $filter[$cname];
}
/*session_start();
session_register("SESSION");
$_SESSION[temp_ID] = $_POST[username];
#$header = "Location: **DATAREMOVED**";
$data = array('LOGIN' => $_POST[username], 'PASSWORD' => $_POST[password], 'PWORD' => 'YES', 'return' => '0');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, '**DATAREMOVED**');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
$output = curl_exec($ch);
if (ereg("activesection=home", $output)) {
$_SESSION[ID] = $_SESSION[temp_ID];
unset($_SESSION[temp_ID]);
echo "ID=".$_SESSION[ID];
} else {
echo "Incorrext Username/Password";
}*/
/*
* Creating custom login form and block
*/
function rcsa_login_block() {
$form = array(
'#action' => 'rcsa/login',
'#id' => 'rcsa-login-form',
'#validate' => rcsa_login_default_validators(),
//'#submit' => array('rcsa_login_submit'),
);
$form['name'] = array('#type' => 'textfield',
'#title' => t('Username'),
'#maxlength' => USERNAME_MAX_LENGTH,
'#size' => 15,
'#required' => TRUE,
);
$form['pass'] = array('#type' => 'password',
'#title' => t('Password'),
'#maxlength' => 60,
'#size' => 15,
'#required' => TRUE,
);
$form['submit'] = array('#type' => 'submit',
'#value' => t('Log in'),
);
$items = array();
if (variable_get('user_register', 1)) {
$items[] = l(t('Create new account'), 'user/register', array('attributes' => array('title' => t('Create a new user account.'))));
}
$items[] = l(t('Request new password'), 'user/password', array('attributes' => array('title' => t('Request new password via e-mail.'))));
$form['links'] = array('#value' => theme('item_list', $items));
return $form;
}
function rcsa_block($op = 'list', $delta = 0, $edit = array()) {
global $user;
if ($op == 'list') {
$blocks[0]['info'] = t('RCSA Member Login');
// Not worth caching.
$blocks[0]['cache'] = BLOCK_NO_CACHE;
return $blocks;
} else if($op == 'view') {
$block = array();
switch ($delta) {
case 0:
// For usability's sake, avoid showing two login forms on one page.
if (!$user->uid && !(arg(0) == 'rcsa' && arg(1) == 'login')) {
$block['subject'] = t('Member login');
$block['content'] = drupal_get_form('rcsa_login_block');
}
return $block;
}
}
}
/*
* A function to validate the custom login form
*/
function rcsa_login_default_validators() {
return array('rcsa_login_name_validate', 'rcsa_login_authenticate_validate');
}
function rcsa_login_name_validate($form, &$form_state) {
if (isset($form_state['values']['name'])) {
imis_auth($form_state,0);
}
}
function imis_auth($form_state, $trig) {
switch($trig) {
case '0':
break;
case '1':
$form_state['values'] = $form_state;
break;
}
//authenticating the user agains IMIS
session_start();
session_register("SESSION");
$_SESSION[temp_ID] = $form_state['values']['name'];
$data = array('LOGIN' => $form_state['values']['name'], 'PASSWORD' => $form_state['values']['pass'], 'PWORD' => 'YES', 'return' => '0');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, '**DATAREMOVED**');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
$output1 = curl_exec($ch);
if (ereg("activesection=home", $output1)) {
$_SESSION[ID] = $_SESSION[temp_ID];
unset($_SESSION[temp_ID]);
echo "ID=".$_SESSION[ID];
$user = rcsa_authenticate($form_state['values']);
//drupal_execute('user_login', $form_state);
//global $user;
if($user->uid > 0) {
$savepass_query = db_query("UPDATE users SET pass = '%s' WHERE name = '%s'", md5($form_state['values']['pass']), $form_state['values']['name']);
$savepass_query2 = db_query("INSERT INTO profile_values (value, fid, uid) VALUES ('%s','%d','%d') WHERE uid = '%d'", $form_state['values']['pass'], 21, $user->uid, $user->uid);
} else {
drupal_set_message('No user ID','error');
}
} else {
drupal_execute('user_login', $form_state);
//form_set_error('name', t('The username %name is not recognised, or your password is incorrect.', array('%name' => $form_state['values']['name'])));
}
}
function rcsa_authenticate($form_values = array()) {
global $user;
// Name and pass keys are required.
if (!empty($form_values['name']) && $account = user_load(array('name' => $form_values['name'], 'status' => 1))) {
$user = $account;
user_authenticate_finalize($form_values);
drupal_goto('/user/' . $user->uid);
return $user;
}
}
function rcsa_login_authenticate_validate($form, &$form_state) {
//
}
/*function rcsa_login_member() {
if(isset($_POST)) {
foreach($_POST as $key => $value) {
$output .= $key . ' => ' . $value . '
';
}
return $output;
}
}
*/
/**
* Access callback for path rcsa/login.
*
* Displays user profile if user is logged in, or login form for anonymous
* users.
*/
function rcsa_page() {
global $user;
if ($user->uid) {
menu_set_active_item('user/'. $user->uid);
return menu_execute_active_handler();
}
else {
return drupal_get_form('rcsa_login');
//return rcsa_login_name_validate();
}
}
function rcsa_login(&$form_state) {
global $user;
// If we are already logged on, go to the user page instead.
if ($user->uid) {
drupal_goto('user/'. $user->uid);
}
if($form_state && $form_state['post']['form_id'] == 'rcsa_login_block') {
foreach($form_state['post'] as $k => $v) {
drupal_set_message($k . '=>' . $v,'status');
}
return imis_auth($form_state['post'], 1);
}
// Display login form:
$form['name'] = array('#type' => 'textfield',
'#title' => t('Member ID'),
'#size' => 60,
'#maxlength' => USERNAME_MAX_LENGTH,
'#required' => TRUE,
'#attributes' => array('tabindex' => '1'),
);
//$form['name']['#description'] = t('Enter your @s username.', array('@s' => variable_get('site_name', 'Drupal')));
$form['pass'] = array('#type' => 'password',
'#title' => t('Password'),
//'#description' => t('Enter the password that accompanies your username.'),
'#required' => TRUE,
'#attributes' => array('tabindex' => '2'),
);
$form['#validate'] = rcsa_login_default_validators();
$form['submit'] = array('#type' => 'submit', '#value' => t('Log in'), '#weight' => 2, '#attributes' => array('tabindex' => '3'));
return $form;
}
function rcsa_mssql_connect() {
/*$dsn="**DATAREMOVED**";
$username="**DATAREMOVED**";
$password="**DATAREMOVED**";
$sqlconnect=odbc_connect($dsn,$username,$password);*/
$myServer = "**DATAREMOVED**";
$myUser = "**DATAREMOVED**";
$myPass = "**DATAREMOVED**";
$myDB = "**DATAREMOVED**";
$dbhandle = mssql_connect($myServer, $myUser, $myPass) or die("Couldn't connect to SQL Server on $myServer");
$selected = mssql_select_db($myDB, $dbhandle) or die("Couldn't open database $myDB");
//return $sqlconnect;
return $dbhandle;
}
function rcsa_mssql_disconnect($conn) {
mssql_close($conn);
//odbc_close($conn);
}
function rcsa_synchronize($conn) {
$ignore = FALSE; // Use this to disable the user import (false) or enable it (true)
if($ignore === TRUE) return;
//getting IMIS data
$query = "SELECT * FROM RCSAMembers WHERE ID <> '0' AND (MEMBER_TYPE = 'Individual' OR MEMBER_TYPE = 'Corporate' OR MEMBER_TYPE = 'Affiliate') ORDER BY ID ASC";
$result = mssql_query($query);
//drupal_set_message($result,'status');
//drupal_set_message(mssql_num_rows($result),'status');
//return;
//getting Drupal Users data
$stuff = db_query("SELECT DISTINCT(u.uid), u.name FROM {users} u LEFT JOIN {users_roles} r ON u.uid = r.rid AND (r.rid != '%d' OR r.rid != '%d')", 3, 4);
while($user_sql = db_fetch_array($stuff)) {
$users[$user_sql['uid']] = $user_sql['name'];
}
/*
while($user_sql = db_fetch_array(db_query("SELECT u.uid, u.name FROM {users} u LEFT JOIN {users_roles} r on u.uid = r.rid WHERE r.rid != '%d' OR r.rid != '%d'", 3, 4))) {
$users[$user_sql['uid']] = $user_sql['name'];
}
*/
while($row = mssql_fetch_array($result)) {
//drupal_set_message($row,'status');
if($row['STATUS'] == 'A') { $status = 1; } else { $status = 0; }
// Process stuff that is common to NEW and EXISTING users
// determine user roles
switch($row['MEMBER_TYPE']) {
case 'Individual':
$role = '2'; // authenticated user
break;
case 'Corporate':
$role = '7'; // Corporate
break;
case 'Affiliate':
$role = '5'; // Affiliate
break;
}
// Address profile values
$array = array(
'profile_company' => $row['COMPANY'],
'profile_address1' => $row['ADDRESS_1'],
'profile_address2' => $row['ADDRESS_2'],
'profile_suburb' => $row['CITY'],
'profile_state' => $row['STATE_PROVINCE'],
'profile_pcode' => $row['ZIP'],
'profile_country' => $row['COUNTRY']
);
$category = 'Address';
$account = user_save($user, $array, $category);
// Contact information
$array = array(
'profile_phone' => $row['WORK_PHONE'],
'profile_fax' => $row['FAX'],
'profile_website' => $row['WEBSITE']
);
$category = 'Contact Information';
$account = user_save($user, $array, $category);
// Personal Details profile values
$array = array(
'profile_fname' => $row['FIRST_NAME'],
'profile_lname' => $row['LAST_NAME'],
'profile_customer' => $row['ID'],
'profile_mtype' => $row['MEMBER_TYPE']
);
$category = 'Personal Details';
$account = user_save($user, $array, $category);
// determine if the user in new or existing
if($uid = array_search($row['ID'],$users)) {
// then update
drupal_set_message('User ' . $row['ID'] . ' updated','status');
// Saving main user data first
$user = user_load($uid); // Existing user - update one
$array = array(
'mail' => $row['EMAIL'],
'init' => $row['EMAIL'],
'status' => $status
);
if(!$account = user_save($user, $array)) {
drupal_set_message('User update error ' . $row['ID'],'error');
watchdog('cron', 'User update error ' . $row['ID'], array(), WATCHDOG_ERROR);
}
$uid = $user->uid;
//$user = $account;
// Update user roles
if($query = db_query("UPDATE LOW_PRIORITY users_roles SET rid = '%d' WHERE uid = '%d'", $role, $uid)) {
drupal_set_message("Role " . $role . " updated",'status');
}
} else {
//insert new user
drupal_set_message('New user ' . $row['ID'],'status');
$temp_pass = 'rcsalogin'.$row['ID'];
// saving user to the database
// Saving main user data first
$user = ''; // No existing user - create a new one
$array = array(
'name' => $row['ID'],
'pass' => $temp_pass,
'mail' => $row['EMAIL'],
'created' => strtotime($row['JOIN_DATE']),
'status' => '1',
'init' => $row['EMAIL'],
'timezone' => '10800'
);
if(!$account = user_save($user, $array)) {
drupal_set_message('User save error ' . $row['ID'],'error');
}
$uid = $account->uid;
$user = $account;
// System profile values
$array = array(
'imis_id' => $row['ID']
);
$category = 'System';
$account = user_save($user, $array, $category);
// Saving user's role
/* if($query = db_query("INSERT LOW_PRIORITY INTO users_roles SET uid = '%d', rid = '%d'", $uid, $role)) {
drupal_set_message("Role " . $role . " updated",'status');
}
*/
/* if($insert_user_query = db_query("
REPLACE DELAYED INTO
users
SET
name = '%s',
pass = '%s',
mail = '%s',
mod = '%d',
sort = '%d',
threshold = '%d',
created = '%d',
status = '%d',
timezone = '%d',
init = '%s',
timezone_id = '%d',
",
$row['ID'], // name
md5($temp_pass), // pass
$row['EMAIL'], // e-mail
0, // mod
0, // sort
0, // treshold
strtotime($row['JOIN_DATE']), // UNIX timestamp for Created
$status, // User status 1 if active, 0 if disabled/blocked
10800, // Default timezone for Australia
$row['EMAIL'], // init
1 // timezone_id
) && (
$isert_user_role_query = db_query("
REPLACE DELAYED INTO
users_roles
SET
uid = ,
rid = ,
")
)
) {
drupal_set_message("Insert Query Ok","status");
} else {
drupal_set_message("Insert Query Failure for member: ".$row['ID'],"error");
}
*/
}
/* //processing data here
$temp_pass = 'rcsalogin'.$row['ID'];
if($update_user_query = db_query("
REPLACE DELAYED INTO
users
SET
name = '%s',
pass = '%s',
mail = '%s',
mod = '%d',
sort = '%d',
threshold = '%d',
created = '%d',
status = '%d',
timezone = '%d',
init = '%s',
timezone_id = '%d',
",
$row['ID'], // name
md5($temp_pass), // pass
$row['EMAIL'], // e-mail
0, // mod
0, // sort
0, // treshold
strtotime($row['JOIN_DATE']), // UNIX timestamp for Created
$status, // User status 1 if active, 0 if disabled/blocked
10800, // Default timezone for Australia
$row['EMAIL'], // init
1 // timezone_id
)
) {
drupal_set_message("Query Ok","status");
} else {
drupal_set_message("Query Failure","error");
}
//drupal_set_message(strtotime($row['JOIN_DATE']),'status');
*/
}
drupal_set_message('Users import complete: '.mssql_num_rows($result),'status');
watchdog('cron','User import from IMIS complete',array());
}