<?php

/*
 * This script serves up readings from the ESV.  It takes a
 * single parameter "passage=XXX" where XXX is the Bible text
 * to display.  In future there might be some style-related
 * options.
 *
 * The script performs three tasks:
 * (1) Retrieve the XML version of the text from the ESV's SOAP server.
 * (2) Convert the XML to XHTML by means of an XSLT script.
 * (3) Output the XHTML.  This is formatted with a CSS2 stylesheet.
 *
 * Version 1.0 BPE 29/07/2003
 */

$do_soap = true;
$do_xslt = true;
$do_out = '';

$debug = false;

//$format = (isset($_GET['Format'])) ? $_GET['Format'] : 'normal';
$format = 'normal';

if(isset($_GET['passage']) && $_GET['passage']) {
    $passage = $_GET['passage'];
} else {
    $passage = 'John 3:16';
}

$tmp_fo = tempnam('/tmp','ESV');
$tmp_pdf = tempnam('/tmp','ESV');

// -1- Retrieve the passage ------------------------------------ //

if ($do_soap) {

    require_once('../../lib/nusoap.php');

    $key = 'TEST';
    include('../../inc/esv_key.inc'); // defines $key

    $server = 'http://www.gnpcb.org/esv/share/soap/';

    $soapclient = new soapclient($server);

    // Do proxy config if necessary
    include('../../inc/proxy.inc'); // defines $proxy*
    if ($proxy) {
        $soapclient->setHTTPProxy(
                                  $proxyhost,
                                  $proxyport,
                                  $proxyusername=$proxyusername,
                                  $proxypassword=$proxypassword
                                 );
    }

    if ($format != 'pdf') {
        $esvparams = array(
                           'output-format'=>'crossway-xml-1.0',
                           'include-doctype'=>'false',
                           // 'include-word-ids'=>'true'
                           );
    } else {
        $esvparams = array(
                           'output-format'=>'crossway-xml-1.0',
                           'include-doctype'=>'false',
                           'base-element'=>'paragraph',
                           // 'include-word-ids'=>'true'
                           );
    }

    $parameters = array(
                        'key'=>$key,
                        'passage'=>$passage,
                        'options'=>$esvparams
                        );

    $soapclient->debug_flag=true;

    $xml = $soapclient->call('doPassageQuery',$parameters);

    if ($debug) echo 'Debug log: <pre>'.$soapclient->debug_str.'</pre>';

    if ($do_out=='save') {
        $fp = fopen("/tmp/test.xml", "w");
        fwrite($fp, $xml);
    }

} else {

    $xml = implode('',file('/tmp/test.xml'));

}

// -2- Convert from XML to XHTML ------------------------------- //

if ($xml && $do_xslt) {

    // I can't get the ESV DTD to work, so define the entities here
    $doctype = <<<EOT
<!DOCTYPE crossway-bible [
<!ENTITY	ldblquot  "&#8220;">
<!ENTITY	rdblquot  "&#8221;">
<!ENTITY	lquot     "&#8216;">
<!ENTITY	rquot     "&#8217;">
<!ENTITY	apos      "'">
<!ENTITY	emdash    "&#8212;">
<!ENTITY	endash    "&#8211;">
<!ENTITY	ellipsis  "&#xa0;.&#xa0;.&#xa0;.">
<!ENTITY	ellipsis4 ".&#xa0;.&#xa0;.&#xa0;.">
]>
EOT;

    switch ($format) {
    case 'notes':
        $xsl = implode('',file('notes.xsl'));
        $style = 'notes.css';
        break;
    case 'pdf':
        $xsl = implode('',file('esv2fo.xsl'));
        $style = '';
        break;
    default:
        $xsl = implode('',file('esv.xsl'));
        $style = 'grey.css';
    }

    $xml = $doctype.preg_replace('/<!DOCTYPE.*>/','',$xml);
    $arguments = array(
                       '/_xml' => $xml,
                       '/_xsl' => $xsl
                       );

    // Indicate the stylesheet to be used
    $xsltparams = array( 'style' => $style );

    $x = xslt_create();
    $out = xslt_process(
                         $x,
                         'arg:/_xml',
                         'arg:/_xsl',
                         NULL,
                         $arguments,
                         $xsltparams
                         );
    xslt_free($x);

    if ($format=='pdf') {

        //$tmp_fo = '/tmp/esv1.fo';
        //$tmp_pdf = '/tmp/esv1.pdf';

        $fp = fopen($tmp_fo, 'w');
        fwrite($fp, $out);

        $res = array();
        $ret = 0;

//        exec("JAVACMD=/home/ben/java/j2sdk1.4.2/bin/java /home/ben/bin/fop -fo $tmp_fo -pdf $tmp_pdf", $res, $ret );
        exec("/home/ben/bin/fop -fo $tmp_fo -pdf $tmp_pdf", $res, $ret );

        if ($ret==0) {
            header("Content-Type: application/pdf; charset=UTF-8");
            $fd = fopen($tmp_pdf, "rb");
            $pdf = fread ($fd, filesize($tmp_pdf));
            // header("Content-Type: text/html; charset=UTF-8");
            $out = $pdf;
        } else {
            header("Content-Type: text/html; charset=UTF-8");
            $out = '<html><pre>'.join("\n",$res).'</pre></html>';
        }

    } else {
        header("Content-Type: text/html; charset=UTF-8");
    }

} else {

    header("Content-Type: text/html; charset=UTF-8");

    if (!$xml) {
        $out = '<p>There was a problem getting the passage in XML</p>';
    } else {
        $out = '';
    }

}

// Anglicise the text: but for my local server only: it would violate
// copyright to do this for public access.

if (preg_match('/\.local$/',$_SERVER['SERVER_NAME'])) {
    include('anglicise.inc');
}


// -3- Output the XHTML ---------------------------------------- //

switch($do_out) {
 case 'debug1':
     print '<pre>'.htmlspecialchars($out).'</pre>';
     break;
 case 'debug2':
     print '<pre>'.htmlspecialchars($xml).'</pre>';
     print '<hr />';
     print '<pre>'.htmlspecialchars($out).'</pre>';
     break;
 default:
     print $out;
}


if ($format=='pdf') {
    @unlink($tmp_fo);
    @unlink($tmp_pdf);
}

?>