<?php
  
/*
   *   Off Campus Link checker for Innovative Catalog
   *   James Muir (muir.29@osu.edu) Systems Developer/Engineer, Ohio State University Libraries
   *   http://library.osu.edu/opensource/
   *   Last Updated 4/2/2008
   *   Automatically adds proxy url to the link if the user is off campus
   *   Copyright (C)2008 [James Muir]
   *   This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version.
   *   This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
   *   You should have received a copy of the GNU General Public License along with this program; if not, see <http://www.gnu.org/licenses>.  
   */ 

  // Set to true to enable debugging. Should be set to false for production.
  
$debug false;
  
  
$resource $_GET['resource'];
  
$record $_GET['record'];
  
$out_debug "";
  
$remote_ip=@$REMOTE_ADDR;
  
  
  function 
check_ip($ip) {
    
$campus_ips = array(
    
'140.254'
    
'128.146'
    
'164.107'
    
);
    
$on false;
    foreach (
$campus_ips as $ipn){
      if(
substr($ip,0,strlen($ipn)) == $ipn$on true;
    }
  return 
$on;
  }
  
  function 
check_open($record){
    
$ch curl_init();
    
curl_setopt($chCURLOPT_URL"http://library.ohio-state.edu/search?/.".$record."/.".$record."/1%2C1%2C1%2CB/marc~".$record);
    
curl_setopt($chCURLOPT_HEADER0);
    
curl_setopt($chCURLOPT_RETURNTRANSFER,1);
    
$result = @curl_exec($ch);
    
curl_close($ch);
   
    
$marc_num "945";
    
$open_codes = array(
      
"opensc"
      
"openkb",
      
"opengov"
      
);
    foreach(
$open_codes as $scode){
      
$marc_line "$marc_num    $scode";
      if (
strpos($result,$marc_line)) {  
        return 
true;
      }
    }
    return 
false;
  }
  
// First check to see if they are on campus, if so point straight to the resource
  
if(check_ip($remote_ip)) { 
    
$out_debug .= "You are on campus!<br>";
    
$goto $resource;
  }
  
// Check if record is open according to criteria above
  
else if(check_open($record)){
    
$out_debug .= "This record is open!<br>";
    
$goto $resource;
  }
  
// If it makes it this far, then the user is off campus trying to access a restricted resource, so it adds the proxy string
  
else { 
   
$out_debug .= "You are not on campus!<br>";
   
$goto 'http://proxy.lib.ohio-state.edu/login?url=' $resource;
   }

  if(!
$debug) {
    
header('Location: '.$goto);
  }
  else {
    echo 
"Debug information for Off Campus Validator:<br><br>".$out_debug."Resulting Link: <a href='$goto'>$goto</a>";      
  }   
?>