$chapter){
/* first store the chapter's full folder name */
$chapter_list[$key]['folder']=$chapter;
/* then parse the chapter name and number */
parse_chapter_info($chapter,$chapter_list[$key]['name'],$chapter_list[$key]['number']);
}
}
/* This function builds a list of the pages
in the currently selected chapter. It is global
just like the chapter list. */
function get_page_list($chapter){
global $chapter_list,$page_list;
/* all pages are files with the prefix "page-"
followed by alphabetically correct numbers that
ensure the pages will be sorted into the correct
order. */
$page_list=list_files($chapter_list[$chapter]['folder'],'/page-.*\.'.PAGE_FILE_TYPE.'/');
sort($page_list);
}
/* This function examines a chapter folder name and
extracts the chapter number and the chapter title */
function parse_chapter_info($file,&$name,&$number){
/* strip the prefix and substitute spaces for
underscores to generate the chapter name */
$name=str_replace('_',' ',substr($file,4));
/* convert the prefix into an integer to
generate the chapter number */
$number=(int)substr($file,0,3);
}
/* This function returns a string with the URL for
a specified page and chapter */
function page_link($chapter,$page=0){
return(sprintf('%s?chapter=%s&page=%s',$_SERVER['SCRIPT_NAME'],urlencode($chapter),urlencode($page)));
}
/* This function imports the chapter variable and
ensures that it has a legal numeric value */
function sanity_check_chapter(&$chapter){
if(isset($_REQUEST['chapter'])) $chapter=(int)$_REQUEST['chapter'];
if(!is_numeric($chapter)) unset($chapter);
}
/* This function imports the page number variable
and ensures that it has a legal numeric value */
function sanity_check_page(&$page){
if(isset($_REQUEST['page'])) $page=(int)$_REQUEST['page'];
if(!is_numeric($page)) $page=0;
}
/* This function returns a link to the next page, or an empty string if this is the last page */
function next_page_url($page, $chapter){
global $chapter_list,$page_list;
/* pre-calculate the last page and chapter */
$last_chapter=sizeof($chapter_list)-1;
$last_page=sizeof($page_list)-1;
if($page<$last_page){
/* when looking at any page except the last
page, the link points to $page+1 */
return page_link($chapter,$page+1);
}else{
/* when looking at the last page... */
if($chapter<$last_chapter){
/* when looking at the last page of any
chapter except the last one, the link
points to the first page of the next
chapter */
return page_link($chapter+1,0);
}else{
/* when looking at the last page of the last
chapter, the link is not a link at all */
return '';
}
}
}
/*-----------------------------------------------------*/
/* Output Functions */
/* This function outputs a list of thumbnail images
that link to the first page of each chapter */
function list_thumbnails(){
global $chapter_list;
foreach($chapter_list as $chapter_ID=>$chapter){
printf(' '."\n"
,page_link($chapter_ID)
,$chapter['folder']
,THUMB_FILE_TYPE
,$chapter['number']
,$chapter['name']
,$chapter['number']
,$chapter['name'])
;
}
}
/* this function outputs the index navigation */
function output_index_navigation(){
?>
';
$link_prev='';
$link_prev_disabled='';
$link_next='';
$link_next_disabled='';
/* now the previous-page link */
if($page>0){
/* when looking at any page but the first,
the link points to $page-1 */
printf('%s'."\n",page_link($chapter,$page-1),$link_prev);
}else{
/* when looking at the first page... */
if($chapter>0){
/* when looking at the first page of any
chapter other than the first one, the
link points to the last page of the
previous chapter */
printf('%s'."\n",page_link($chapter-1,-1),$link_prev);
}else{
/* when looking at the first page of the
first chapter, the link is not a link
at all */
printf('%s'."\n",$link_prev_disabled);
}
}
/* The index link is easy. It never changes. */
printf('%s'."\n",$_SERVER['SCRIPT_NAME'],$link_index);
/* output the next-page link */
if($page<$last_page){
/* when looking at any page except the last
page, the link points to $page+1 */
printf('%s'."\n",page_link($chapter,$page+1),$link_next);
}else{
/* when looking at the last page... */
if($chapter<$last_chapter){
/* when looking at the last page of any
chapter except the last one, the link
points to the first page of the next
chapter */
printf('%s'."\n",page_link($chapter+1,0),$link_next);
}else{
/* when looking at the last page of the last
chapter, the link is not a link at all */
printf('%s'."\n",$link_next_disabled);
}
}
printf(' '."\n");
printf(''."\n");
}
/* This function outputs the body of the page */
function show_page($chapter,$page){
global $chapter_list,$page_list;
/* pre-calculate the last page */
$last=sizeof($page_list)-1;
/* if $page is -1 then select the
last page in the chapter. (this
is used by the "previous" page
navigation link) */
if($page==-1) $page=$last;
/* read the alt text */
$alt_file_name=sprintf('./%s/alt.txt',$chapter_list[$chapter]['folder']);
if(is_readable($alt_file_name)){
$alt_file_data=file($alt_file_name);
$alt=htmlentities(trim(str_replace("\n",'',strip_tags($alt_file_data[1+$page]))),ENT_QUOTES);
}else{
$alt=sprintf('Page %s',$page);
}
/* I think this looks better centered. */
printf('
'."\n");
}
/* This function outputs a simplified header for
the index page */
function show_index_header(){
printf(''."\n");
/* output the title */
printf("%s\n",PAGE_TITLE);
printf(''."\n");
printf(''."\n");
printf(''."\n");
}
/* This function outputs the html headers and the
title, and opens the html body tag */
function show_page_header($chapter,$page){
global $chapter_list;
printf(''."\n");
/* output the title including chapter name and page number */
printf("%s - %s - Page %s\n",PAGE_TITLE,$chapter_list[$chapter]['name'],$page);
/* output the favorites icon */
printf(''."\n");
/* generate the prefetch headers for the next page */
generate_prefetch_link($chapter,$page);
printf(''."\n");
}
function generate_prefetch_link($chapter,$page){
global $chapter_list,$page_list;
$last=sizeof($page_list)-1;
$last_chapter=sizeof($chapter_list)-1;
if($page<$last){
/* if we are looking at anything other than the
last page, we output a header that
instructs browsers to prefetch the next page */
printf(''."\n",$chapter_list[$chapter]['folder'],$page_list[$page+1]);
}else{
/* if we are looking at the last page of a series */
if($chapter<$last_chapter){
/* if we are on the last page of any chapter
other than the last one, we output a
header instructing the browser to prefetch
the first page of the next chapter. */
/* it is neccisary to list the pages of the next
chapter because we only already have the pages
of the current chapter in memory */
$next_chapter_page_list=list_files($chapter_list[$chapter+1]['folder'],'/page-.*\.'.PAGE_FILE_TYPE.'/');
sort($next_chapter_page_list);
/* and here we output the link it would not
really be an unreasonable assumption to
assume that every chapter will have at least
one page, but I hate making assuptions, even
reasonable ones :) */
if(sizeof($next_chapter_page_list)){
printf(''."\n",$chapter_list[$chapter+1]['folder'],$next_chapter_page_list[0]);
}
/* and then we discard the list of the next
chapter's pages from memory, as we will not
need it any further */
unset($next_chapter_page_list);
}
}
}
/* This function outputs the html footers and
the copyright information */
function show_page_footer(){
printf('
Once upon a time, there was a Hamster named Bob. He was the eldest of 144 siblings, all of whom were also named Bob (yes, even his sisters). He grew up a happy hamster child, starred in a few obscure computer games, started a revolution and founded his own country, saved the world from unspeakable evil once or twice, and finally decided to settle down and get a real job. Sound mundane? Well, nothing stays simple long in Bob's life.
New chapters are uploaded on a strict schedule of whenever-I-feel-like-it.