<?php

#
# The Interactive M'Cheyne Bible calendar
#
# Copyright 2002-2012 Ben Edgington
#
# Version:
#   3.92(27/03/2016) - Update for import_request_variables obsolescence
#   3.91(16/09/2012) - Fixes for leap year handling
#   3.9 (21/02/2010) - Rewrite and simplify cookie code
#   3.8 (27/06/2009) - Add option to use mobile BibleGateway
#   3.7 (26/02/2006) - Add select date control
#                      Change cookie path from "/" to "/mcheyne/"
#   3.6 (04/10/2004) - Turn off cacheing - see note below
#   3.5 (29/06/2004) - Add RSS links and fix typo
#   3.4 (16/04/2004) - Minor changes to improve accessibility
#   3.3 (14/04/2004) - Double-buffering of compressed output
#   3.2 (16/06/2003) - Obtain Bible and Calendar options from the
#                      server (ie. they are no longer hardcoded here)
#   3.1 (13/01/2003) - Remove local links when accessed via AvantGo to
#                      stop it downloading my entire site every day.
#   3.0 (08/01/2003) - Use new M'Cheyne data server
#   2.2 (30/10/2002) - Don't rely on register_globals for input parameters
#   2.1 (24/10/2002) - Add links to RSV and NRSV
#   2.0 (22/10/2002) - Complete rewrite in PHP; MySQL backend
#   1.1 (19/08/2002) - Add links to the ESV
#   1.0 (14/05/2002) - Original version in Perl
#

# Include the M'Cheyne Server interface class
include "data_bpe.inc";

# Use output buffering to allow compression and content-length generation,
# but we double-buffer to workaround problems finding the buffer-length of
# compressed data in some PHP versions
ob_start();
ob_start('ob_gzhandler');

# Detect whether page is being accessed by AvantGo
$avantgo = strstr($_SERVER['HTTP_USER_AGENT'], 'AvantGo');

#### Acceptable values and defaults for the parameters ####

$cal_default = 'classic';

$bible_default = 'niv';

$tzs = array (    -12 => 'GMT-12', -11 => 'GMT-11', -10 => 'GMT-10',
   -9 => 'GMT-9',  -8 => 'GMT-8',   -7 => 'GMT-7',   -6 => 'GMT-6',
   -5 => 'GMT-5',  -4 => 'GMT-4',   -3 => 'GMT-3',   -2 => 'GMT-2',
   -1 => 'GMT-1',   0 => 'GMT',      1 => 'GMT+1',    2 => 'GMT+2',
    3 => 'GMT+3',   4 => 'GMT+4',    5 => 'GMT+5',    6 => 'GMT+6',
    7 => 'GMT+7',   8 => 'GMT+8',    9 => 'GMT+9',   10 => 'GMT+10',
   11 => 'GMT+11', 12 => 'GMT+12'
);
$tz_default = 0;

$skins = array (
    'green' => 'Green',
    'slate' => 'Slate',
    'rich' => 'Rich',
    'grey_l' => 'Grey Left',
    'grey_r' => 'Grey Right',
    'pastel' => 'Pastel',
    'plain' => 'Plain',
    'none' => 'None'
);
$skin_default = 'green';

#### Useful date stuff
$months = array ('January'   => 0,
                 'February'  => 31,
                 'March'     => 59,
                 'April'     => 90,
                 'May'       => 120,
                 'June'      => 151,
                 'July'      => 181,
                 'August'    => 212,
                 'September' => 243,
                 'October'   => 273,
                 'November'  => 304,
                 'December'  => 334);

#### Explicitly deal with request variables ####

# Ie. don't rely on register_globals behaviour.  It may be turned off.
# This can be tested by inserting "php_flag register_globals off"
# in the directory .htaccess file.

# 160327 - the following is obsolete
#import_request_variables("gp", "req_");
extract($_GET, EXTR_PREFIX_ALL, "req");
extract($_POST, EXTR_PREFIX_ALL, "req");
$bible = $req_bible;
$cal = $req_cal;
$tz = $req_tz;
$skin = $req_skin;
$hide_navig = $req_hide_navig;
$hide_prefs = $req_hide_prefs;
$hide_blurb = $req_hide_blurb;
$day = $req_day;
$mobile = $req_mobile;

if ($req_prev) {
    $day = ($day == 0) ? 364 : $day - 1;
} else if ($req_next) {
    $day = ($day == 364) ? 0 : $day + 1;
} else if ($req_today) {
    $day = -1;
} else if ($req_go) {
    $day = $req_month + $req_mday;
}

#### Deal with any returned cookie ####

if (isset($_COOKIE['MCHEYNEPREFS'])) {
    $cookie = explode(':',$_COOKIE['MCHEYNEPREFS']);
    # PUT and GET variables take precedence
    if (!isset($bible)) { $bible = $cookie[0]; }
    if (!isset($cal)) { $cal = $cookie[1]; }
    if (!isset($tz)) { $tz = $cookie[2]; }
    if (!isset($skin)) { $skin = $cookie[3]; }
    if (!isset($hide_navig)) { $hide_navig = $cookie[4]; }
    if (!isset($hide_prefs)) { $hide_prefs = $cookie[5]; }
    if (!isset($hide_blurb)) { $hide_blurb = $cookie[6]; }
    if (!isset($mobile)) { $mobile = $cookie[7]; }
}


#### Check and set parameters ####

# Timezone.  Casting it as an integer gives most flexibility.
if (!(isset($tz) && settype($tz,"integer") && valid($tz,$tzs))) {
    $tz = $tz_default;
}

# Calendar version
if (!isset($cal)) { $cal = $cal_default; }
if ($cal == 'classic') {
    $title_1 = 'Family';
    $title_2 = 'Secret';
} else {
    $title_1 = 'Family';
    $title_2 = 'Private';
}

# Bible version
if (!isset($bible)) { $bible = $bible_default; }

# Skin
if (!(isset($skin) && valid($skin,$skins))) { $skin = $skin_default; }

# Display options
$show_navig = (isset($hide_navig) && $hide_navig == 't') ? FALSE : TRUE;
$show_prefs = (isset($hide_prefs) && $hide_prefs == 't') ? FALSE : TRUE;
$show_blurb = (isset($hide_blurb) && $hide_blurb == 't') ? FALSE : TRUE;

$hide_navig = $show_navig ? '' : 't';
$hide_prefs = $show_prefs ? '' : 't';
$hide_blurb = $show_blurb ? '' : 't';

$path = $_SERVER['PHP_SELF'];

# Use mobile BibleGateway?
$use_mobile = (isset($mobile) && $mobile == 't') ? TRUE : FALSE;
$mobile = $use_mobile ? 't' : '';

#### Get data from the M'Cheyne server ####

# This will validate $day or return current $day taking into account $tz
$mc = new McheyneData($bible, $cal, $tz, $day, $mobile);

# Check server version is new enough
if ($mc->version < 2.3) {
    $error = "<p><b>Error!</b> The server version ({$mc->version}) is too old.  It must be version 2.3 or newer.</p>";
}

# In case either of these were invalid use the values from the server
$bible = $mc->bible;
$cal = $mc->cal;

$day = $mc->day;

$leap = (date('Y')%4 == 0 && $day >=59);
$t = getdate(strtotime("1 January") + 24*60*60*($day+$leap));
$t_mday = $t['mday'];
$t_month = $t['month'];
$date = $t_mday.' '.$t_month;

$quote = $mc->quote;

$ref_1 = $mc->refs[0];
$ref_2 = $mc->refs[1];
$ref_3 = $mc->refs[2];
$ref_4 = $mc->refs[3];

$url_1 = $mc->urls[0];
$url_2 = $mc->urls[1];
$url_3 = $mc->urls[2];
$url_4 = $mc->urls[3];


#### Functions ####

function valid($value,$values) {

    return array_key_exists($value,$values);

}

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=ISO-8859-1" />
<title>Interactive M'Cheyne Bible Calendar</title>
<link rel="alternate" type="application/rss+xml" href="rss_feed.php" title="RSS feed for the M'Cheyne Bible calendar"/>
<style type="text/css">

<?php if ($skin != 'none') { ?>
@import url(skin_<?php echo $skin; ?>.css);
<?php } ?>

.mcheyne { text-align: center; }
.navigate table { margin-left: auto; margin-right: auto; }

.button { font-style: italic; }

.quote {
	text-align: center;
        font-style: italic;

}

select { display: inline; }

<?php if ($show_prefs && ($skin=='grey_l' || $skin=='grey_r')) { ?>
.mcheyne { width: 75%; }
<?php } else { ?>
.mcheyne { width: 100%; }
<?php } ?>

.left { float: left; }
.right { float: right; }
.clear { clear: both; }

#xml a img { border: 0; }

</style>
</head>

<body>

<!-- Main block -->

<?php
if ($error) {
    print $error;
    exit;
}
?>

<div class="mcheyne">
<h1>M'Cheyne readings for <?php echo $date; ?></h1>

<p class="quote"><?php echo $quote; ?></p>

<div class="spacer"></div>

<div class="refblock">
<div class="refs1">
<span class="title"><?php echo $title_1; ?><span class="nsonly">: </span></span>
<span class="refer"><a href="<?php echo $url_1; ?>"><?php echo $ref_1; ?></a><span class="nsonly">, </span></span>
<span class="refer"><a href="<?php echo $url_2; ?>"><?php echo $ref_2; ?></a></span>
</div>
<div class="spacer"></div>
<div class="refs2">
<span class="title"><?php echo $title_2; ?><span class="nsonly">: </span></span>
<span class="refer"><a href="<?php echo $url_3; ?>"><?php echo $ref_3; ?></a><span class="nsonly">, </span></span>
<span class="refer"><a href="<?php echo $url_4; ?>"><?php echo $ref_4; ?></a></span>
</div>
</div>

<div class="spacer"></div>

<div class="clear"></div>

<!-- Navigate block -->

<?php if ($show_navig) { ?>

<div class="navigate">

<form method="post" action="<?php echo $path; ?>">
<input type="hidden" name="hide_navig" value="<?php echo $hide_navig; ?>" />
<input type="hidden" name="hide_prefs" value="<?php echo $hide_prefs; ?>" />
<input type="hidden" name="hide_blurb" value="<?php echo $hide_blurb; ?>" />
<input type="hidden" name="mobile" value="<?php echo $mobile; ?>" />
<input type="hidden" name="day" value="<?php echo $day; ?>" />
<input type="hidden" name="tz" value="<?php echo $tz; ?>" />
<input type="hidden" name="bible" value="<?php echo $bible; ?>" />
<input type="hidden" name="cal" value="<?php echo $cal; ?>" />
<input type="hidden" name="skin" value="<?php echo $skin; ?>" />
<table summary="This table contains buttons linking to the previous and next day's readings."><tr>
<td class="button" align="left">
<p>
<input type="submit" name="prev" value="Prev" />
</p>
</td>

<td class="button" align="center">
<p>
<input type="submit" name="today" value="Today" />
</p>
</td>

<td class="button" align="right">
<form method="post" action="<?php echo $path; ?>">
<p>
<input type="submit" name="next" value="Next" />
</p>
</td>

</tr>
<tr>
<td class="button" align="center" colspan="3">
<select id="mday" name="mday">
<?php
    for ($i=1; $i<=31; $i++) {
        $j = $i-1;
        echo "<option value=\"$j\"";
        if ($i==$t_mday) {echo " selected=\"selected\"";}
        echo ">$i</option>\n";
    }
?>
</select>
<select id="month" name="month">
<?php
    foreach ($months as $name => $days) {
        echo "<option value=\"$days\"";
        if ($name==$t_month) {echo " selected=\"selected\"";}
        echo ">$name</option>\n";
    }
?>
</select>
<input type="submit" name="go" value="Go" />
</td>
</tr>
</table>
</form>

</div>

<?php } ?>

</div>

<!-- Customise block -->

<?php if ($show_prefs) { ?>

<hr class="nsonly" />

<div class="prefs">

<form method="post" action="<?php echo $path; ?>">

<div class="pref-1">
<div class="item">
<label for="bible" title="Choose which Bible version to link to.">Bible</label>
<select id="bible" name="bible">
<?php
    select_box($bible,$mc->bibles);
?>
</select>
</div>

<div class="item">
<label for="cal" title="Choose whether to use M&apos;Cheyne&apos;s original version, or Carson&apos;s slightly modified version.">Calendar</label>
<select id="cal" name="cal">
<?php
    select_box($cal,$mc->cals);
?>
</select>
</div>

<div class="item">
<label for="tz" title="Select which timezone you are in so that the readings change at midnight for you.">Timezone</label>
<select id="tz" name="tz">
<?php
    select_box($tz,$tzs);
?>
</select>
</div>

<div class="item">
<label for="skin" title="Select one of the background themes available.">Skin</label>
<select id="skin" name="skin">
<?php
    select_box($skin,$skins);
?>
</select>
</div>

</div>


<div class="pref-2">
<div class="item">
<input type="checkbox" name="hide_navig" value="t" id="hide-navig" <?php if ($hide_navig) { echo "checked=\"checked\"";} ?> /> <label for="hide-navig" title="Hide the controls for moving from day to day." >Hide navigation</label>
</div>
<div class="item">
<input type="checkbox" name="hide_prefs" value="t" id="hide-prefs" <?php if ($hide_prefs) { echo "checked=\"checked\"";} ?> /> <label for="hide-prefs" title="Hide this panel.">Hide preferences</label>
</div>
<div class="item">
<input type="checkbox" name="hide_blurb" value="t" id="hide-blurb" <?php if ($hide_blurb) { echo "checked=\"checked\"";} ?> /> <label for="hide-blurb" title="Hide the information panel.">Hide blurb</label>
</div>
<div class="item">
<input type="checkbox" name="mobile" value="t" id="mobile" <?php if ($use_mobile) { echo "checked=\"checked\"";} ?> /> <label for="mobile" title="Link to the mobile versions where appropriate (BibleGateway, ESV).">Mobile Versions</label>
</div>
<div class="apply">
<input type="submit" name="Apply" value="Apply" />
</div>
</div>
</form>

<?php if (!$avantgo) { ?>

<p><a href="nocookies.php">No cookies</a>? Then <a href="<?php
echo "${path}?tz=$tz&amp;cal=$cal&amp;bible=$bible&amp;skin=$skin&amp;hide_navig=$hide_navig&amp;hide_prefs=$hide_prefs&amp;hide_blurb=$hide_blurb&amp;mobile=$mobile";
?>">bookmark this link</a>.</p>

<?php } ?>

</div>

<?php } ?>

<div class="clear-skin-grey"></div>

<!-- Text block -->

<?php if ($show_blurb && !$avantgo) { ?>

<hr class="nsonly" />

<div class="blurb">

<p>These readings are according to Robert Murray M'Cheyne's daily
Bible reading calendar.  Click the references to see the Bible texts.</p>

<p>See my <a href="index.html">M'Cheyne calendar pages</a> for more information about the calendar and other ways to access it, including RSS options.</p>

<p>Please <a href="/mail.php">contact me</a> with any comments, suggestions or bug reports.</p>

</div>

<?php } ?>

<div class="copy">

<hr class="nsonly" />

<?php if (!$show_prefs) { ?>

<div class="right">

<form method="post" action="<?php echo $path; ?>">

<input type="hidden" name="hide_navig" value="<?php echo $hide_navig; ?>" />
<input type="hidden" name="hide_prefs" value="" />
<input type="hidden" name="hide_blurb" value="<?php echo $hide_blurb; ?>" />
<input type="hidden" name="tz" value="<?php echo $tz; ?>" />
<input type="hidden" name="bible" value="<?php echo $bible; ?>" />
<input type="hidden" name="cal" value="<?php echo $cal; ?>" />
<input type="hidden" name="skin" value="<?php echo $skin; ?>" />
<input type="hidden" name="day" value="<?php echo $day; ?>" />
<input type="submit" name="Apply" value="Preferences" />

</form>

</div>

<?php } ?>

<div class="left">

<?php
# Reduce off-page links when AvantGo comes along
if ($avantgo) {
     echo "<p>&copy; Copyright 2002-2009 Ben Edgington.</p>";
} else {
     echo "<p>&copy; Copyright 2002-2009 <a href=\"/\">Ben Edgington</a>.</p>";
     echo "<p id=\"xml\"><a type=\"application/rss+xml\" href=\"rss_feed.php\"><img src=\"/gifs/xml.gif\" alt=\"XML\"/></a></p>";
}
?>

</div>

<div class="spacer"></div>

</div>

</body></html>
<?php

# End inner (possibly compressed) output buffer
ob_end_flush();

#### Generate HTTP headers ####

# Save parameters as cookie
# New path is "/mcheyne".  Expires one year hence
setcookie('MCHEYNEPREFS', "$bible:$cal:$tz:$skin:$hide_navig:$hide_prefs:$hide_blurb:$mobile", time()+86400*365, '/mcheyne/', $_SERVER['HTTP_HOST']);

# For simplicity, don't allow any cacheing
# (I reckon Etags etc. don't gain us anything here)
header('Expires: 0');

# Content-Length
header('Content-Length: ' . ob_get_length());

# Output the page
ob_end_flush();


function select_box($value,$values) {

    foreach ($values as $name => $label) {
        echo "<option value=\"$name\"";
        if ($name==$value) {echo " selected=\"selected\"";}
        echo ">$label</option>\n";
    }

}
?>