2012-05-28 08:05:05 +03:00
|
|
|
|
<?php
|
|
|
|
|
/**
|
|
|
|
|
* COPS (Calibre OPDS PHP Server) class file
|
|
|
|
|
*
|
|
|
|
|
* @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
|
|
|
|
|
* @author S<EFBFBD>bastien Lucas <sebastien@slucas.fr>
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
require_once ("base.php");
|
2013-10-10 07:25:04 +03:00
|
|
|
|
|
2012-05-28 08:05:05 +03:00
|
|
|
|
class OPDSRenderer
|
|
|
|
|
{
|
|
|
|
|
private $xmlStream = NULL;
|
|
|
|
|
private $updated = NULL;
|
2013-10-10 07:25:04 +03:00
|
|
|
|
|
2012-05-28 08:05:05 +03:00
|
|
|
|
private function getUpdatedTime () {
|
|
|
|
|
if (is_null ($this->updated)) {
|
|
|
|
|
$this->updated = time();
|
|
|
|
|
}
|
|
|
|
|
return date (DATE_ATOM, $this->updated);
|
|
|
|
|
}
|
2013-10-10 07:25:04 +03:00
|
|
|
|
|
2012-05-28 08:05:05 +03:00
|
|
|
|
private function getXmlStream () {
|
|
|
|
|
if (is_null ($this->xmlStream)) {
|
|
|
|
|
$this->xmlStream = new XMLWriter();
|
|
|
|
|
$this->xmlStream->openMemory();
|
|
|
|
|
$this->xmlStream->setIndent (true);
|
|
|
|
|
}
|
|
|
|
|
return $this->xmlStream;
|
|
|
|
|
}
|
2013-10-10 07:25:04 +03:00
|
|
|
|
|
2012-05-28 08:05:05 +03:00
|
|
|
|
public function getOpenSearch () {
|
2012-05-28 08:06:12 +03:00
|
|
|
|
global $config;
|
2012-05-28 08:05:05 +03:00
|
|
|
|
$xml = new XMLWriter ();
|
|
|
|
|
$xml->openMemory ();
|
|
|
|
|
$xml->setIndent (true);
|
|
|
|
|
$xml->startDocument('1.0','UTF-8');
|
|
|
|
|
$xml->startElement ("OpenSearchDescription");
|
2012-05-28 08:06:12 +03:00
|
|
|
|
$xml->writeAttribute ("xmlns", "http://a9.com/-/spec/opensearch/1.1/");
|
2012-05-28 08:05:05 +03:00
|
|
|
|
$xml->startElement ("ShortName");
|
|
|
|
|
$xml->text ("My catalog");
|
|
|
|
|
$xml->endElement ();
|
2012-09-05 11:40:06 +03:00
|
|
|
|
$xml->startElement ("Description");
|
|
|
|
|
$xml->text ("Search for ebooks");
|
|
|
|
|
$xml->endElement ();
|
2012-05-28 08:05:05 +03:00
|
|
|
|
$xml->startElement ("InputEncoding");
|
|
|
|
|
$xml->text ("UTF-8");
|
|
|
|
|
$xml->endElement ();
|
|
|
|
|
$xml->startElement ("OutputEncoding");
|
|
|
|
|
$xml->text ("UTF-8");
|
|
|
|
|
$xml->endElement ();
|
|
|
|
|
$xml->startElement ("Image");
|
2012-05-28 08:06:12 +03:00
|
|
|
|
$xml->writeAttribute ("type", "image/x-icon");
|
|
|
|
|
$xml->writeAttribute ("width", "16");
|
|
|
|
|
$xml->writeAttribute ("height", "16");
|
2012-12-22 19:03:52 +02:00
|
|
|
|
$xml->text ($config['cops_icon']);
|
2012-05-28 08:05:05 +03:00
|
|
|
|
$xml->endElement ();
|
|
|
|
|
$xml->startElement ("Url");
|
|
|
|
|
$xml->writeAttribute ("type", 'application/atom+xml');
|
2013-04-30 10:42:43 +03:00
|
|
|
|
$urlparam = "?query={searchTerms}";
|
|
|
|
|
if (!is_null (GetUrlParam (DB))) $urlparam = addURLParameter ($urlparam, DB, GetUrlParam (DB));
|
|
|
|
|
$urlparam = str_replace ("%7B", "{", $urlparam);
|
|
|
|
|
$urlparam = str_replace ("%7D", "}", $urlparam);
|
|
|
|
|
$xml->writeAttribute ("template", $config['cops_full_url'] . 'feed.php' . $urlparam);
|
2012-05-28 08:06:12 +03:00
|
|
|
|
$xml->endElement ();
|
|
|
|
|
$xml->startElement ("Query");
|
|
|
|
|
$xml->writeAttribute ("role", "example");
|
|
|
|
|
$xml->writeAttribute ("searchTerms", "robot");
|
2012-05-28 08:05:05 +03:00
|
|
|
|
$xml->endElement ();
|
|
|
|
|
$xml->endElement ();
|
|
|
|
|
$xml->endDocument();
|
|
|
|
|
return $xml->outputMemory(true);
|
|
|
|
|
}
|
2013-10-10 07:25:04 +03:00
|
|
|
|
|
2012-12-22 19:03:52 +02:00
|
|
|
|
private function startXmlDocument ($page) {
|
2012-09-05 11:40:06 +03:00
|
|
|
|
global $config;
|
2012-05-28 08:05:05 +03:00
|
|
|
|
self::getXmlStream ()->startDocument('1.0','UTF-8');
|
|
|
|
|
self::getXmlStream ()->startElement ("feed");
|
|
|
|
|
self::getXmlStream ()->writeAttribute ("xmlns", "http://www.w3.org/2005/Atom");
|
|
|
|
|
self::getXmlStream ()->writeAttribute ("xmlns:xhtml", "http://www.w3.org/1999/xhtml");
|
|
|
|
|
self::getXmlStream ()->writeAttribute ("xmlns:opds", "http://opds-spec.org/2010/catalog");
|
|
|
|
|
self::getXmlStream ()->writeAttribute ("xmlns:opensearch", "http://a9.com/-/spec/opensearch/1.1/");
|
|
|
|
|
self::getXmlStream ()->writeAttribute ("xmlns:dcterms", "http://purl.org/dc/terms/");
|
|
|
|
|
self::getXmlStream ()->startElement ("title");
|
2012-12-22 19:03:52 +02:00
|
|
|
|
self::getXmlStream ()->text ($page->title);
|
2012-05-28 08:05:05 +03:00
|
|
|
|
self::getXmlStream ()->endElement ();
|
2012-12-22 19:23:17 +02:00
|
|
|
|
if ($page->subtitle != "")
|
|
|
|
|
{
|
|
|
|
|
self::getXmlStream ()->startElement ("subtitle");
|
|
|
|
|
self::getXmlStream ()->text ($page->subtitle);
|
|
|
|
|
self::getXmlStream ()->endElement ();
|
|
|
|
|
}
|
2012-05-28 08:05:05 +03:00
|
|
|
|
self::getXmlStream ()->startElement ("id");
|
2012-12-22 19:03:52 +02:00
|
|
|
|
if ($page->idPage)
|
2012-05-28 08:07:49 +03:00
|
|
|
|
{
|
2013-04-30 10:11:04 +03:00
|
|
|
|
$idPage = $page->idPage;
|
2013-10-25 09:07:42 +03:00
|
|
|
|
if (!is_null (GetUrlParam (DB))) $idPage = str_replace ("cops:", "cops:" . GetUrlParam (DB) . ":", $idPage);
|
2013-04-30 10:11:04 +03:00
|
|
|
|
self::getXmlStream ()->text ($idPage);
|
2012-05-28 08:07:49 +03:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
self::getXmlStream ()->text ($_SERVER['REQUEST_URI']);
|
|
|
|
|
}
|
2012-05-28 08:05:05 +03:00
|
|
|
|
self::getXmlStream ()->endElement ();
|
|
|
|
|
self::getXmlStream ()->startElement ("updated");
|
|
|
|
|
self::getXmlStream ()->text (self::getUpdatedTime ());
|
|
|
|
|
self::getXmlStream ()->endElement ();
|
|
|
|
|
self::getXmlStream ()->startElement ("icon");
|
2012-12-22 19:03:52 +02:00
|
|
|
|
self::getXmlStream ()->text ($page->favicon);
|
2012-05-28 08:05:05 +03:00
|
|
|
|
self::getXmlStream ()->endElement ();
|
|
|
|
|
self::getXmlStream ()->startElement ("author");
|
|
|
|
|
self::getXmlStream ()->startElement ("name");
|
2013-10-10 07:25:04 +03:00
|
|
|
|
self::getXmlStream ()->text ($page->authorName);
|
2012-05-28 08:05:05 +03:00
|
|
|
|
self::getXmlStream ()->endElement ();
|
|
|
|
|
self::getXmlStream ()->startElement ("uri");
|
2013-10-10 07:25:04 +03:00
|
|
|
|
self::getXmlStream ()->text ($page->authorUri);
|
2012-05-28 08:05:05 +03:00
|
|
|
|
self::getXmlStream ()->endElement ();
|
|
|
|
|
self::getXmlStream ()->startElement ("email");
|
2013-10-10 07:25:04 +03:00
|
|
|
|
self::getXmlStream ()->text ($page->authorEmail);
|
2012-05-28 08:05:05 +03:00
|
|
|
|
self::getXmlStream ()->endElement ();
|
|
|
|
|
self::getXmlStream ()->endElement ();
|
2012-05-28 08:06:12 +03:00
|
|
|
|
$link = new LinkNavigation ("", "start", "Home");
|
2012-05-28 08:05:05 +03:00
|
|
|
|
self::renderLink ($link);
|
2014-01-19 22:09:11 +02:00
|
|
|
|
$link = new LinkNavigation ("?" . getQueryString (), "self");
|
2012-05-28 08:05:05 +03:00
|
|
|
|
self::renderLink ($link);
|
2013-11-18 16:57:17 +02:00
|
|
|
|
$urlparam = "?";
|
2013-04-30 10:42:43 +03:00
|
|
|
|
if (!is_null (GetUrlParam (DB))) $urlparam = addURLParameter ($urlparam, DB, GetUrlParam (DB));
|
2013-01-11 18:26:26 +02:00
|
|
|
|
if ($config['cops_generate_invalid_opds_stream'] == 0 || preg_match("/(MantanoReader|FBReader)/", $_SERVER['HTTP_USER_AGENT'])) {
|
2012-09-08 16:09:52 +03:00
|
|
|
|
// Good and compliant way of handling search
|
2014-01-07 07:24:06 +02:00
|
|
|
|
$urlparam = addURLParameter ($urlparam, "page", Base::PAGE_OPENSEARCH);
|
2013-04-30 10:42:43 +03:00
|
|
|
|
$link = new Link ("feed.php" . $urlparam, "application/opensearchdescription+xml", "search", "Search here");
|
2012-09-08 16:09:52 +03:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
// Bad way, will be removed when OPDS client are fixed
|
2013-11-18 16:57:17 +02:00
|
|
|
|
$urlparam = addURLParameter ($urlparam, "query", "{searchTerms}");
|
|
|
|
|
$urlparam = str_replace ("%7B", "{", $urlparam);
|
|
|
|
|
$urlparam = str_replace ("%7D", "}", $urlparam);
|
2013-04-30 10:42:43 +03:00
|
|
|
|
$link = new Link ($config['cops_full_url'] . 'feed.php' . $urlparam, "application/atom+xml", "search", "Search here");
|
2012-09-08 16:09:52 +03:00
|
|
|
|
}
|
2012-05-28 08:05:05 +03:00
|
|
|
|
self::renderLink ($link);
|
2013-01-11 16:16:15 +02:00
|
|
|
|
if ($page->containsBook () && !is_null ($config['cops_books_filter']) && count ($config['cops_books_filter']) > 0) {
|
|
|
|
|
$Urlfilter = getURLParam ("tag", "");
|
|
|
|
|
foreach ($config['cops_books_filter'] as $lib => $filter) {
|
2014-01-19 22:09:11 +02:00
|
|
|
|
$link = new LinkFacet ("?" . addURLParameter (getQueryString (), "tag", $filter), $lib, localize ("tagword.title"), $filter == $Urlfilter);
|
2013-01-11 16:16:15 +02:00
|
|
|
|
self::renderLink ($link);
|
|
|
|
|
}
|
|
|
|
|
}
|
2012-05-28 08:05:05 +03:00
|
|
|
|
}
|
2013-10-10 07:25:04 +03:00
|
|
|
|
|
2012-05-28 08:05:05 +03:00
|
|
|
|
private function endXmlDocument () {
|
|
|
|
|
self::getXmlStream ()->endElement ();
|
|
|
|
|
self::getXmlStream ()->endDocument ();
|
|
|
|
|
return self::getXmlStream ()->outputMemory(true);
|
|
|
|
|
}
|
2013-10-10 07:25:04 +03:00
|
|
|
|
|
2012-05-28 08:05:05 +03:00
|
|
|
|
private function renderLink ($link) {
|
|
|
|
|
self::getXmlStream ()->startElement ("link");
|
|
|
|
|
self::getXmlStream ()->writeAttribute ("href", $link->href);
|
|
|
|
|
self::getXmlStream ()->writeAttribute ("type", $link->type);
|
|
|
|
|
if (!is_null ($link->rel)) {
|
|
|
|
|
self::getXmlStream ()->writeAttribute ("rel", $link->rel);
|
|
|
|
|
}
|
|
|
|
|
if (!is_null ($link->title)) {
|
|
|
|
|
self::getXmlStream ()->writeAttribute ("title", $link->title);
|
|
|
|
|
}
|
2013-01-11 16:16:15 +02:00
|
|
|
|
if (!is_null ($link->facetGroup)) {
|
|
|
|
|
self::getXmlStream ()->writeAttribute ("opds:facetGroup", $link->facetGroup);
|
|
|
|
|
}
|
|
|
|
|
if ($link->activeFacet) {
|
|
|
|
|
self::getXmlStream ()->writeAttribute ("opds:activeFacet", "true");
|
|
|
|
|
}
|
2012-05-28 08:05:05 +03:00
|
|
|
|
self::getXmlStream ()->endElement ();
|
|
|
|
|
}
|
|
|
|
|
|
2013-10-10 07:25:04 +03:00
|
|
|
|
|
2012-05-28 08:05:05 +03:00
|
|
|
|
private function renderEntry ($entry) {
|
|
|
|
|
self::getXmlStream ()->startElement ("title");
|
|
|
|
|
self::getXmlStream ()->text ($entry->title);
|
|
|
|
|
self::getXmlStream ()->endElement ();
|
|
|
|
|
self::getXmlStream ()->startElement ("updated");
|
|
|
|
|
self::getXmlStream ()->text (self::getUpdatedTime ());
|
|
|
|
|
self::getXmlStream ()->endElement ();
|
|
|
|
|
self::getXmlStream ()->startElement ("id");
|
|
|
|
|
self::getXmlStream ()->text ($entry->id);
|
|
|
|
|
self::getXmlStream ()->endElement ();
|
|
|
|
|
self::getXmlStream ()->startElement ("content");
|
|
|
|
|
self::getXmlStream ()->writeAttribute ("type", $entry->contentType);
|
|
|
|
|
if ($entry->contentType == "text") {
|
|
|
|
|
self::getXmlStream ()->text ($entry->content);
|
|
|
|
|
} else {
|
|
|
|
|
self::getXmlStream ()->writeRaw ($entry->content);
|
|
|
|
|
}
|
|
|
|
|
self::getXmlStream ()->endElement ();
|
|
|
|
|
foreach ($entry->linkArray as $link) {
|
|
|
|
|
self::renderLink ($link);
|
|
|
|
|
}
|
2013-10-10 07:25:04 +03:00
|
|
|
|
|
2012-05-28 08:05:05 +03:00
|
|
|
|
if (get_class ($entry) != "EntryBook") {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2013-10-10 07:25:04 +03:00
|
|
|
|
|
2012-05-28 08:05:05 +03:00
|
|
|
|
foreach ($entry->book->getAuthors () as $author) {
|
|
|
|
|
self::getXmlStream ()->startElement ("author");
|
|
|
|
|
self::getXmlStream ()->startElement ("name");
|
|
|
|
|
self::getXmlStream ()->text ($author->name);
|
|
|
|
|
self::getXmlStream ()->endElement ();
|
|
|
|
|
self::getXmlStream ()->startElement ("uri");
|
2012-05-28 08:07:49 +03:00
|
|
|
|
self::getXmlStream ()->text ("feed.php" . $author->getUri ());
|
2012-05-28 08:05:05 +03:00
|
|
|
|
self::getXmlStream ()->endElement ();
|
|
|
|
|
self::getXmlStream ()->endElement ();
|
|
|
|
|
}
|
|
|
|
|
foreach ($entry->book->getTags () as $category) {
|
|
|
|
|
self::getXmlStream ()->startElement ("category");
|
2012-06-12 09:27:19 +03:00
|
|
|
|
self::getXmlStream ()->writeAttribute ("term", $category->name);
|
|
|
|
|
self::getXmlStream ()->writeAttribute ("label", $category->name);
|
2012-05-28 08:05:05 +03:00
|
|
|
|
self::getXmlStream ()->endElement ();
|
|
|
|
|
}
|
2013-02-20 22:31:58 +02:00
|
|
|
|
if ($entry->book->getPubDate () != "") {
|
2012-05-28 08:05:05 +03:00
|
|
|
|
self::getXmlStream ()->startElement ("dcterms:issued");
|
|
|
|
|
self::getXmlStream ()->text (date ("Y-m-d", $entry->book->pubdate));
|
|
|
|
|
self::getXmlStream ()->endElement ();
|
2012-05-28 08:07:49 +03:00
|
|
|
|
self::getXmlStream ()->startElement ("published");
|
|
|
|
|
self::getXmlStream ()->text (date ("Y-m-d", $entry->book->pubdate) . "T08:08:08Z");
|
|
|
|
|
self::getXmlStream ()->endElement ();
|
2012-05-28 08:05:05 +03:00
|
|
|
|
}
|
2013-10-10 07:25:04 +03:00
|
|
|
|
|
2012-12-23 16:10:07 +02:00
|
|
|
|
$lang = $entry->book->getLanguages ();
|
2013-03-23 10:36:10 +02:00
|
|
|
|
if (!empty ($lang)) {
|
2012-12-23 16:10:07 +02:00
|
|
|
|
self::getXmlStream ()->startElement ("dcterms:language");
|
|
|
|
|
self::getXmlStream ()->text ($lang);
|
|
|
|
|
self::getXmlStream ()->endElement ();
|
|
|
|
|
}
|
2012-05-28 08:05:05 +03:00
|
|
|
|
|
|
|
|
|
}
|
2013-10-10 07:25:04 +03:00
|
|
|
|
|
2012-05-28 08:05:05 +03:00
|
|
|
|
public function render ($page) {
|
2012-09-18 16:39:22 +03:00
|
|
|
|
global $config;
|
2012-12-22 19:03:52 +02:00
|
|
|
|
self::startXmlDocument ($page);
|
2012-09-21 15:19:11 +03:00
|
|
|
|
if ($page->isPaginated ())
|
2012-05-28 08:06:12 +03:00
|
|
|
|
{
|
|
|
|
|
self::getXmlStream ()->startElement ("opensearch:totalResults");
|
2012-09-18 16:39:22 +03:00
|
|
|
|
self::getXmlStream ()->text ($page->totalNumber);
|
2012-05-28 08:06:12 +03:00
|
|
|
|
self::getXmlStream ()->endElement ();
|
|
|
|
|
self::getXmlStream ()->startElement ("opensearch:itemsPerPage");
|
2012-09-18 16:39:22 +03:00
|
|
|
|
self::getXmlStream ()->text ($config['cops_max_item_per_page']);
|
2012-05-28 08:06:12 +03:00
|
|
|
|
self::getXmlStream ()->endElement ();
|
|
|
|
|
self::getXmlStream ()->startElement ("opensearch:startIndex");
|
2012-09-18 16:39:22 +03:00
|
|
|
|
self::getXmlStream ()->text (($page->n - 1) * $config['cops_max_item_per_page'] + 1);
|
2012-05-28 08:06:12 +03:00
|
|
|
|
self::getXmlStream ()->endElement ();
|
2012-09-21 15:19:11 +03:00
|
|
|
|
$prevLink = $page->getPrevLink ();
|
|
|
|
|
$nextLink = $page->getNextLink ();
|
|
|
|
|
if (!is_null ($prevLink)) {
|
|
|
|
|
self::renderLink ($prevLink);
|
2012-09-18 16:39:22 +03:00
|
|
|
|
}
|
2012-09-21 15:19:11 +03:00
|
|
|
|
if (!is_null ($nextLink)) {
|
|
|
|
|
self::renderLink ($nextLink);
|
2012-09-18 16:39:22 +03:00
|
|
|
|
}
|
2012-05-28 08:06:12 +03:00
|
|
|
|
}
|
2012-05-28 08:05:05 +03:00
|
|
|
|
foreach ($page->entryArray as $entry) {
|
|
|
|
|
self::getXmlStream ()->startElement ("entry");
|
|
|
|
|
self::renderEntry ($entry);
|
|
|
|
|
self::getXmlStream ()->endElement ();
|
|
|
|
|
}
|
|
|
|
|
return self::endXmlDocument ();
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-10-10 07:25:04 +03:00
|
|
|
|
|