<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">
<html>
<head>
  <title>ISBN Analyzer</title>
  <style type="text/css">
  BODY { background: white; }
  h1 {text-align : center;}
  </style>
<script type="text/javascript">
<!-- // bibres.js  version 1.1
     // (c) Robert D. Cameron and Serban Tatu, November 2000. 
     // GNU General Public License, Version 2 applies.
var BibP_BaseURL;
var BibP_nocitehost = typeof(BibP_citehost) == "undefined";
function BibP_SetBaseURL (server) {
  BibP_BaseURL = server + "bibp1.0/resolve?" +
  (BibP_nocitehost ? "usin=" : "citehost="+ BibP_citehost+ "&usin=")}
BibP_SetBaseURL(BibP_nocitehost ? "http://usin.org/" :BibP_citehost);
function BibP_onMouseOver () {
  window.status = "bibp:" + this.href.substring(BibP_BaseURL.length); 
  return true}
function BibP_onMouseOut () {window.status = "";  return true}
function BibP_ProcessLink(L, srchKey) {
  var spot = L.href.indexOf(srchKey);
  if (spot != -1) {
    L.href = BibP_BaseURL + L.href.substring(spot + srchKey.length);
    L.onmouseover = BibP_onMouseOver;
    L.onmouseout = BibP_onMouseOut}}
var BibP_Icon = new Image ();      // To test for local bibhost icon.
function BibP_onIcon () {
  if (BibP_Icon.height!=0) {
    var oldBase = BibP_BaseURL;
    BibP_SetBaseURL("http://bibhost/");
    for (var i = 0; i < document.links.length; i++) 
      BibP_ProcessLink(document.links[i], oldBase)}}
function BibP_onLoad () {
  for (var i = 0; i < document.links.length; i++)
    BibP_ProcessLink(document.links[i], "bibp:")
  BibP_Icon.onload = BibP_onIcon;  // Now test for bibhost.
  BibP_Icon.src = "http://bibhost/bibp1.0/bibpicon.jpg"}
if (typeof(navigator.bibpSupport) == "undefined") {
  window.onload = BibP_onLoad}
// -->
</script>
<?php
//  ISBN-analyzer.php  - ISBN Analysis Service
//  Version 1.0
//  Robert D. Cameron, January 12, 2001.
//  Copyright (c) 2001,  GNU Public License, Version 2, applies.

$server_root getcwd();  
while (
basename($server_root) != 'usin.org') {
  
$server_root dirname($server_root);
}
require(
"$server_root/scripts/utilities/ISBN-ISSN.php");

function 
error_repair_and_report($clean) {
  if (
strlen($clean) == 9) {
    echo(
"<p>This string is missing a digit.</p>\n");
    echo(
"<h3>Error Repair</h3>\n");
    echo(
"<p>Attempting error correction by single digit insertion.\n");
  }
  elseif (
strlen($clean) == 10) {
    echo(
"<p>This string has an error in at least one position.</p>\n");
    echo(
"<h3>Error Repair</h3>\n");
    echo(
"<p>Attempting error correction by single character " .
         
"replacement or pairwise transposition.\n");
  }
  else {
    echo(
"<p>This string has an extraneous character.</p>\n");
    echo(
"<h3>Error Repair</h3>\n");
    echo(
"<p>Attempting error correction by single character deletion.\n");
  }
  echo(
"</p>\n");
  
$corrections generate_ISBN_corrections($clean);
  foreach (
$corrections as $newISBN => $dummy) {
    if (
is_assigned($newISBN))
      
$correction_text .= '<li><a href="ISBN-analyzer.php?usin=' 
                           
$newISBN '">' $newISBN "</a></li>\n";
  }
  if (
strlen($correction_text) == 0) {
    echo(
"<p>Sorry, no correction is possible.\n</p>");
  }
  else {
    echo(
"<p>The following valid ISBNs have been generated.</p>\n");
    echo(
"<ul>\n$correction_text</ul>\n");
  }
}

function 
analyze_and_report($original$clean) {
  echo(
"<h1>ISBN Analysis for $original</h1>\n");
  echo(
"<h3>Checksum</h3>\n");
  echo(
"<p>The string is ISBN-checksum correct!</p>\n");
  
$canon canonical_ISBN($clean);
  
$codes split("-"$canon);
  if (
sizeof($codes) == 4) {
    if ((
strlen($original) > 10) && 
        (
str_replace("x""X"$original) != $canon)) {
      echo(
"<p><strong>Hyphenation error</strong>: hyphenation ignored.</p>\n");
    }
    echo(
"<h3>Canonical Form</h3>\n");
    if (
$canon == $original) {
      echo(
"The ISBN as specified is already in USIN canonical form!\n");
    }
    else {
      echo(
'The USIN canonical form for this ISBN is <a href="bibp:ISBN/' .
           
$canon '">' $canon "</a>.\n");
    }
    echo(
"<h3>ISBN Structure</h3>");
    echo(
"<dl>\n");
    echo(
"<dt>Country/Group Code: " $codes[0] . "</dt>\n");
    echo(
"<dd>" country_group_name($clean) .  "</dd>\n");
    echo(
"<dt>Publisher Code: " $codes[1] . "</dt>\n");
    echo(
"<dt>Book Code: " $codes[2] . "</dt>\n");
    echo(
"<dt>Check Digit: " $codes[3] . "</dt>\n");
    echo(
"</dl>\n");
    echo(
'<p>Try the bibp link: <a href="bibp:ISBN/' $canon .
         
'">bibp:ISBN/' .  $canon "</a>!</p>\n");
  }
  else {
    echo(
"<h3>Unassigned</h3>\n");
    echo(
"<p>This ISBN is in an unassigned range!</p>");
    echo(
"<dl>\n");
    echo(
"<dt>Country/Group Code: " $codes[0] . "</dt>\n");
    echo(
"<dd>" country_group_name($clean) .  "</dd>\n");
    echo(
"<dt>Publisher/Book Code: " $codes[1] . "</dt>\n");
    echo(
"<dd>unassigned</dd>\n");
    echo(
"<dt>Check Digit: " $codes[2] . "</dt>\n");
    echo(
"</dl>\n");
  }
}
?>
</head> 
<body>
<?php
if (strlen($usin) != 0$ISBN_proto $usin;
elseif (
$ISBN_by_POST$ISBN_proto $ISBN_by_POST;
if (
$ISBN_proto) {
  
$clean ISN_clean($ISBN_proto);
  
$class ISBN_classifier($clean);
  if (
$class == "bookland") {
    echo(
"<h1>Bookland EAN</h1>\n");
    echo(
"<p>This string appears to be an ISBN encoded as a Bookland EAN.\n");
    
$ISBN_proto substr($ISBN_proto3strlen($ISBN_proto) - 4);
    
$ISBN_proto .= make_checkdigit(mod11_checksum($ISBN_proto10));
    echo(
"Computing ISBN string for analysis: <strong>$ISBN_proto</strong></p>\n");
    
$clean ISN_clean($ISBN_proto);
    
$class ISBN_classifier($clean);
  }
  if (
$class == "checksumOK"analyze_and_report($ISBN_proto$clean);
  else {
    echo(
"<h1>Error in ISBN</h1>\n");
    echo(
"<p>ISBN string under analysis: <strong>$clean</strong></p>\n");
    if (
$class == "single_error"error_repair_and_report($clean);
    else {
      echo(
"<p>This string ");
      if (
$class == "short") echo("is too short to be an ISBN.\n");
      elseif (
$class == "short/invalid"
        echo(
"is too short to be an ISBN and contains invalid characters.\n");
      elseif (
$class == "long") echo("is too long to be an ISBN.\n");
      elseif (
$class == "long/invalid"
        echo(
"is too long to be an ISBN and contains invalid characters.\n");
      elseif (
$class == "invalid"
        echo(
"contains two or more invalid ISBN characters.\n");
      echo(
"There are too many errors for error correction to be attempted.</p>\n");
    }
    echo(
"<p>An ISBN consists of 9 significant digits and a check digit.\n");
    echo(
"The check digit may be 'X' or 'x' to represent a checksum of 10.\n");
    echo(
"Optional embedded hyphens should reflect the internal structure consisting " .
         
"of country/group code, publisher code, book code and check digit.\n");
    echo(
"</p>");
  }
}
?>
<hr>
<form enctype="multipart/form-data" 
      action="http://usin.org/scripts/ISBN-analyzer.php" 
      method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="10000">
<H3>ISBN Syntax Analyzer</h3>
<p>
Enter an ISBN protostring for analysis.  
<input name="ISBN_by_POST" type="text" size="20">
</p>
<input TYPE="submit" VALUE="Analyze!">
</form>

</body>
</html>