Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: sopac
Fetching contributors…

Cannot retrieve contributors at this time

263 lines (235 sloc) 7.944 kb
<?php
/**
* @file
* Provides integration between Drupal and EZProxy
*/
/**
* Implementatin of hook_menu
*/
function ezproxy_menu() {
$items = array();
$items['ezproxyauth'] = array(
'title' => 'EZProxy Auth',
'page callback' => 'ezproxy_authenticate',
'access arguments' => array('access content'),
'type' => MENU_CALLBACK
);
$items['ezproxylogin'] = array(
'title' => 'EZProxy Login',
'page callback' => 'ezproxy_login',
'access arguments' => array('access content'),
'type' => MENU_CALLBACK
);
$items['ezproxylogout'] = array(
'title' => 'EZProxy Logout',
'page callback' => 'ezproxy_logout',
'access arguments' => array('access content'),
'type' => MENU_CALLBACK
);
$items['admin/settings/ezproxy'] = array(
'title' => 'EZProxy Settings',
'description' => 'Settings for EZProxy',
'page callback' => 'drupal_get_form',
'page arguments' => array('ezproxy_settings'),
'access arguments' => array('administer ezproxy'),
);
$items['admin/settings/ezproxy/list'] = array(
'title' => 'EZProxy Database list',
'description' => 'List of available databases available through EZProxy',
'page callback' => 'ezproxy_database_list',
'access arguments' => array('administer ezproxy'),
);
return $items;
}
/**
* Implementatin of hook_perm
*/
function ezproxy_perm() {
return array('access ezproxy content', 'administer ezproxy');
}
/**
* Used with the external plugin method
* see http://www.oclc.org/support/documentation/ezproxy/usr/external.htm
*
* To use this method copy this line into the users.txt file (part of the ezproxy package)
* ::external=http://yourdrupallib.org/ezproxyauth,post=ezuser=^u&ezpass=^p,valid=+OK
*/
function ezproxy_authenticate() {
$result = db_query("SELECT uid FROM {users} WHERE name = '%s' AND pass = md5('%s') AND status = 1", $_REQUEST['ezuser'], $_REQUEST['ezpass']);
$row = db_fetch_object($result);
if ($row->uid) {
$user = user_load($row->uid);
if (user_access('access ezproxy content', $user)) {
echo "+OK";
}
else {
echo "+FAIL";
}
}
else {
echo "+FAIL";
}
}
/**
* Used with the CGI authentication method
* see http://www.oclc.org/support/documentation/ezproxy/usr/cgi2.htm
*
* To use this method copy this line into the users.txt file (part of the ezproxy package)
* ::CGI=http://example.com/ezproxylogin?url=^U
*/
function ezproxy_login() {
global $user;
global $_ezproxy_url;
$_ezproxy_url = $_REQUEST['url'];
if(module_exists('sopac')){
if ($user->uid && $user->bcode_verified) {
$url = ezproxy_create_ticket($_ezproxy_url);
header("Location:$url\n\n");
}
elseif ($user->uid && !$user->bcode_verified) {
$verify_link = l(t('Please add/verify your Library Card and try again.'), 'user/' . $user->uid);
drupal_set_message(t('You do not have permission to access this content. ').$verify_link, 'error');
return '';
}
}
else {
if ($user->uid && user_access('access ezproxy content')) { //user is already logged in has permission to access ezproxy content
$url = ezproxy_create_ticket($_ezproxy_url);
header("Location:$url\n\n");
}
elseif ($user->uid && !user_access('access ezproxy content')) { //user is already logged in but does NOT have permission to access ezproxy content
drupal_set_message(t('You do not have permission to access ez-proxy content'), 'error');
return '';
}
else {
return drupal_get_form('user_login');
}
}
}
/**
* Implementation of hook form_alter()
* Adds a url field to the form
*/
function ezproxy_form_alter(&$form, $form_state, $form_id) {
global $_ezproxy_url;
if ($form_id == 'user_login') {
$form['ezproxy_url'] = array(
'#type' => 'hidden',
'#value' => $_ezproxy_url,
);
$form['#submit'][] = 'ezproxy_login_submit';
}
}
/**
* Implementation of hook_user
* Used as part of the CGI authentication method
*/
function ezproxy_user($op, &$edit, &$account, $category = NULL) {
if ($op == 'login' && !empty($edit['ezproxy_url'])) {
$_ezproxy_url = $edit['ezproxy_url'];
if(module_exists('sopac')){
if ($user->uid && $user->bcode_verified) {
$url = ezproxy_create_ticket($_ezproxy_url);
header("Location:$url\n\n");
}
elseif ($user->uid && !$user->bcode_verified) {
$verify_link = l(t('Please add/verify your Library Card and try again.'), 'user/' . $user->uid);
drupal_set_message(t('You do not have permission to access this content. ').$verify_link, 'error');
drupal_access_denied();
exit();
}
}
else {
if ($account->uid && user_access('access ezproxy content')) { //user is already logged in has permission to access ezproxy content
$url = ezproxy_create_ticket($_ezproxy_url);
header("Location:$url\n\n");
die();
}
else {
drupal_set_message(t("You do not have permission to access EZProxy content"));
drupal_access_denied();
exit();
}
}
}
}
/**
* Settings for EZproxy
*/
function ezproxy_settings() {
$form = array();
$form['ezproxy_host'] = array(
'#type' => 'textfield',
'#title' => t('EZproxy host'),
'#default_value' => variable_get('ezproxy_host', 'http://ezproxy.example.com'),
'#description' => t('The hostname of your exproxy server'),
);
$form['ezproxy_port'] = array(
'#type' => 'textfield',
'#title' => t('EZproxy port'),
'#default_value' => variable_get('ezproxy_port', '2048'),
'#description' => t('The port used by the exproxy server'),
);
$form['ezproxy_ticket_secret'] = array(
'#type' => 'textfield',
'#title' => t('EZproxy secret (Ticket authentication only)'),
'#default_value' => variable_get('ezproxy_ticket_secret', 'somesecret'),
'#description' => t('NOTE this is only used with the ticket authentication method'),
);
return system_settings_form($form);
}
/**
* Redirects to the standard EZproxy screen
*/
function ezproxy_database_list() {
$ezproxy_host = variable_get('ezproxy_host', 'http://ezproxy.example.com');
$ezproxy_port = variable_get('ezproxy_port', '2048');
header("Location:$ezproxy_host:$ezproxy_port/menu\n\n");
die();
}
/**
* Redirects to the standard EZproxy logout screen
*/
function ezproxy_logout() {
$ezproxy_host = variable_get('ezproxy_host', 'http://ezproxy.example.com');
$ezproxy_port = variable_get('ezproxy_port', '2048');
header("Location:$ezproxy_host:$ezproxy_port/logout\n\n");
die();
}
/**
* Generates a HTML link to the EZProxy database using tickets
* This is used in the same way as the l() function
* except that an ezproxy URL is returned, with the appropriate
* ticket attached.
*
* @param $text the Text to use in the link
* @param $path the URL of the database to connect to
* @param the options array that you would usually pass into the l() function
* @return Decorated URL to use with ezproxy authentication.
*/
function ezproxy_l($text, $path, $options = array()) {
global $user;
if (!user_access('access ezproxy content')) {
return;
}
$url = ezproxy_create_ticket($path);
return l($text, $url, $options);
}
/**
* Generates the URL to use with EZProxy tickets
*
* @param $path the URL of the database to connect to
* @return Decorated URL to use with ezproxy authentication.
*/
function ezproxy_create_ticket($path) {
global $user;
include_once('ezproxyticket.php');
if ($user->uid == 0 ) {
return FALSE;
}
$ezproxy_host = variable_get('ezproxy_host', 'http://ezproxy.example.com');
$ezproxy_port = variable_get('ezproxy_port', '2048');
$ezproxy_ticket_secret = variable_get('ezproxy_ticket_secret', '');
$ezproxy = new EZproxyTicket("$ezproxy_host:$ezproxy_port", $ezproxy_ticket_secret, $user->name);
return $ezproxy->url($path);
}
Jump to Line
Something went wrong with that request. Please try again.