<?php
/**
 * COPS (Calibre OPDS PHP Server) class file
 *
 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
 * @author     Sébastien Lucas <sebastien@slucas.fr>
 */

define ("VERSION", "0.6.1");
date_default_timezone_set($config['default_timezone']);
 
function getURLParam ($name, $default = NULL) {
    if (!empty ($_GET) && isset($_GET[$name]) && $_GET[$name] != "") {
        return $_GET[$name];
    }
    return $default;
}

function getCurrentOption ($option) {
    global $config;
    if (isset($_COOKIE[$option])) {
        return $_COOKIE[$option];
    }
    if ($option == "style") {
        return "default";
    }
    
    if (isset($config ["cops_" . $option])) {
        return $config ["cops_" . $option];
    }
    
    return "";
}

function getCurrentCss () {
    return "styles/style-" . getCurrentOption ("style") . ".css";
}

function getUrlWithVersion ($url) {
    return $url . "?v=" . VERSION;
}

function xml2xhtml($xml) {
    return preg_replace_callback('#<(\w+)([^>]*)\s*/>#s', create_function('$m', '
        $xhtml_tags = array("br", "hr", "input", "frame", "img", "area", "link", "col", "base", "basefont", "param");
        return in_array($m[1], $xhtml_tags) ? "<$m[1]$m[2] />" : "<$m[1]$m[2]></$m[1]>";
    '), $xml);
}

function display_xml_error($error)
{
    $return .= str_repeat('-', $error->column) . "^\n";

    switch ($error->level) {
        case LIBXML_ERR_WARNING:
            $return .= "Warning $error->code: ";
            break;
         case LIBXML_ERR_ERROR:
            $return .= "Error $error->code: ";
            break;
        case LIBXML_ERR_FATAL:
            $return .= "Fatal Error $error->code: ";
            break;
    }

    $return .= trim($error->message) .
               "\n  Line: $error->line" .
               "\n  Column: $error->column";

    if ($error->file) {
        $return .= "\n  File: $error->file";
    }

    return "$return\n\n--------------------------------------------\n\n";
}

function are_libxml_errors_ok ()
{
    $errors = libxml_get_errors();
    
    foreach ($errors as $error) {
        if ($error->code == 801) return false;
    }
    return true;
}

function html2xhtml ($html) {
    $doc = new DOMDocument();
    libxml_use_internal_errors(true);
    
    $doc->loadHTML('<html><head><meta http-equiv="content-type" content="text/html; charset=utf-8"></head><body>' . 
                        $html  . '</body></html>'); // Load the HTML
    $output = $doc->saveXML($doc->documentElement); // Transform to an Ansi xml stream
    $output = xml2xhtml($output);
    if (preg_match ('#<html><head><meta http-equiv="content-type" content="text/html; charset=utf-8"></meta></head><body>(.*)</body></html>#ms', $output, $matches)) {
        $output = $matches [1]; // Remove <html><body>
    }
    /* 
    // In case of error with summary, use it to debug
    $errors = libxml_get_errors();

    foreach ($errors as $error) {
        $output .= display_xml_error($error);
    }
    */
    
    if (!are_libxml_errors_ok ()) $output = "HTML code not valid.";
    
    libxml_use_internal_errors(false);
    return $output;
}

/**
 * This method is a direct copy-paste from
 * http://tmont.com/blargh/2010/1/string-format-in-php
 */
function str_format($format) {
    $args = func_get_args();
    $format = array_shift($args);
    
    preg_match_all('/(?=\{)\{(\d+)\}(?!\})/', $format, $matches, PREG_OFFSET_CAPTURE);
    $offset = 0;
    foreach ($matches[1] as $data) {
        $i = $data[0];
        $format = substr_replace($format, @$args[$i], $offset + $data[1] - 1, 2 + strlen($i));
        $offset += strlen(@$args[$i]) - 2 - strlen($i);
    }
    
    return $format;
}

/**
 * This method is based on this page
 * http://www.mind-it.info/2010/02/22/a-simple-approach-to-localization-in-php/
 */
function localize($phrase, $count=-1) {

    if ($count == 0)
        $phrase .= ".none";
    if ($count == 1)
        $phrase .= ".one";
    if ($count > 1)
        $phrase .= ".many";

    /* Static keyword is used to ensure the file is loaded only once */
    static $translations = NULL;
    /* If no instance of $translations has occured load the language file */
    if (is_null($translations)) {
        $lang = "en";
        if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE']))
        {
            $lang = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
        }
        $lang_file_en = NULL;
        $lang_file = 'lang/Localization_' . $lang . '.json';
        if (!file_exists($lang_file)) {
            $lang_file = 'lang/' . 'Localization_en.json';
        }
        elseif ($lang != "en") {
            $lang_file_en = 'lang/' . 'Localization_en.json';
        }
        $lang_file_content = file_get_contents($lang_file);
        /* Load the language file as a JSON object and transform it into an associative array */
        $translations = json_decode($lang_file_content, true);
        if ($lang_file_en)
        {
            $lang_file_content = file_get_contents($lang_file_en);
            $translations_en = json_decode($lang_file_content, true);
            $translations = array_merge ($translations_en, $translations);
        }
    }
    if (array_key_exists ($phrase, $translations)) {
        return $translations[$phrase];
    }
    return $phrase;
}

function addURLParameter($urlParams, $paramName, $paramValue) {
    $start = "";
    if (preg_match ("#^\?(.*)#", $urlParams, $matches)) {
        $start = "?";
        $urlParams = $matches[1];
    }
    $params = array();
    parse_str($urlParams, $params);
    if (empty ($paramValue) && $paramValue != 0) {
        unset ($params[$paramName]);
    } else {
        $params[$paramName] = $paramValue;   
    }
    return $start . http_build_query($params);
}

class Link
{
    const OPDS_THUMBNAIL_TYPE = "http://opds-spec.org/image/thumbnail";
    const OPDS_IMAGE_TYPE = "http://opds-spec.org/image";
    const OPDS_ACQUISITION_TYPE = "http://opds-spec.org/acquisition/open-access";
    const OPDS_NAVIGATION_TYPE = "application/atom+xml;profile=opds-catalog;kind=navigation";
    const OPDS_PAGING_TYPE = "application/atom+xml;profile=opds-catalog;kind=acquisition";
    
    public $href;
    public $type;
    public $rel;
    public $title;
    public $facetGroup;
    public $activeFacet;
    
    public function __construct($phref, $ptype, $prel = NULL, $ptitle = NULL, $pfacetGroup = NULL, $pactiveFacet = FALSE) {
        $this->href = $phref;
        $this->type = $ptype;
        $this->rel = $prel;
        $this->title = $ptitle;
        $this->facetGroup = $pfacetGroup;
        $this->activeFacet = $pactiveFacet;
    }
    
    public function hrefXhtml () {
        return $this->href;
    }
}

class LinkNavigation extends Link
{
    public function __construct($phref, $prel = NULL, $ptitle = NULL) {
        parent::__construct ($phref, Link::OPDS_NAVIGATION_TYPE, $prel, $ptitle);
        if (!preg_match ("#^\?(.*)#", $this->href) && !empty ($this->href)) $this->href = "?" . $this->href;
        if (preg_match ("/(bookdetail|getJSON).php/", $_SERVER["SCRIPT_NAME"])) {
            $this->href = "index.php" . $this->href;
        } else {
            $this->href = $_SERVER["SCRIPT_NAME"] . $this->href;
        }
    }
}

class LinkFacet extends Link
{
    public function __construct($phref, $ptitle = NULL, $pfacetGroup = NULL, $pactiveFacet = FALSE) {
        parent::__construct ($phref, Link::OPDS_PAGING_TYPE, "http://opds-spec.org/facet", $ptitle, $pfacetGroup, $pactiveFacet);
        $this->href = $_SERVER["SCRIPT_NAME"] . $this->href;
    }
}

class Entry
{
    public $title;
    public $id;
    public $content;
    public $contentType;
    public $linkArray;
    public $localUpdated;
    private static $updated = NULL;
    
    public static $icons = array(
        Author::ALL_AUTHORS_ID       => 'images/author.png',
        Serie::ALL_SERIES_ID         => 'images/serie.png',
        Book::ALL_RECENT_BOOKS_ID    => 'images/recent.png',
        Tag::ALL_TAGS_ID             => 'images/tag.png',
        Language::ALL_LANGUAGES_ID   => 'images/language.png',
        "calibre:books$"             => 'images/allbook.png',
        "calibre:books:letter"       => 'images/allbook.png'
    );
    
    public function getUpdatedTime () {
        if (!is_null ($this->localUpdated)) {
            return date (DATE_ATOM, $this->localUpdated);
        }
        if (is_null (self::$updated)) {
            self::$updated = time();
        }
        return date (DATE_ATOM, self::$updated);
    }
    
    public function getContentArray () {
        $navlink = "#";
        foreach ($this->linkArray as $link) { 
            if ($link->type != Link::OPDS_NAVIGATION_TYPE) { continue; }
            
            $navlink = $link->hrefXhtml ();
        }
        return array ( "title" => $this->title, "content" => $this->content, "navlink" => $navlink );
    }
 
    public function __construct($ptitle, $pid, $pcontent, $pcontentType, $plinkArray) {
        global $config;
        $this->title = $ptitle;
        $this->id = $pid;
        $this->content = $pcontent;
        $this->contentType = $pcontentType;
        $this->linkArray = $plinkArray;
        
        if ($config['cops_show_icons'] == 1)
        {
            foreach (self::$icons as $reg => $image)
            {
                if (preg_match ("/" . $reg . "/", $pid)) {
                    array_push ($this->linkArray, new Link (getUrlWithVersion ($image), "image/png", Link::OPDS_THUMBNAIL_TYPE));
                    break;
                }
            }
        }
        
    }
}

class EntryBook extends Entry
{
    public $book;
    
    public function __construct($ptitle, $pid, $pcontent, $pcontentType, $plinkArray, $pbook) {
        parent::__construct ($ptitle, $pid, $pcontent, $pcontentType, $plinkArray);
        $this->book = $pbook;
        $this->localUpdated = $pbook->timestamp;
    }
    
    public function getContentArray () {
        $entry = array ( "title" => $this->title);
        $entry ["book"] = $this->book->getContentArray ();
        return $entry;
    }
    
    public function getCoverThumbnail () {
        foreach ($this->linkArray as $link) {
            if ($link->rel == Link::OPDS_THUMBNAIL_TYPE)
                return $link->hrefXhtml ();
        }
        return null;
    }
    
    public function getCover () {
        foreach ($this->linkArray as $link) {
            if ($link->rel == Link::OPDS_IMAGE_TYPE)
                return $link->hrefXhtml ();
        }
        return null;
    }
}

class Page
{
    public $title;
    public $subtitle = "";
    public $idPage;
    public $idGet;
    public $query;
    public $favicon;
    public $n;
    public $book;
    public $totalNumber = -1;
    public $entryArray = array();
    
    public static function getPage ($pageId, $id, $query, $n)
    {
        switch ($pageId) {
            case Base::PAGE_AUTHORS_FIRST_LETTER :
                return new PageAllAuthorsLetter ($id, $query, $n);
            case Base::PAGE_AUTHORS_STARTING_LETTERS :
                return new PageAuthorsLetter ($id, $query, $n);
            case Base::PAGE_AUTHOR_DETAIL :
                return new PageAuthorDetail ($id, $query, $n);
            case Base::PAGE_ALL_TAGS :
                return new PageAllTags ($id, $query, $n);
            case Base::PAGE_TAG_DETAIL :
                return new PageTagDetail ($id, $query, $n);
            case Base::PAGE_ALL_LANGUAGES :
                return new PageAllLanguages ($id, $query, $n);
            case Base::PAGE_LANGUAGE_DETAIL :
                return new PageLanguageDetail ($id, $query, $n);             
            case Base::PAGE_ALL_SERIES :
                return new PageAllSeries ($id, $query, $n);
            case Base::PAGE_SERIES_STARTING_LETTERS :
                return new PageAllSeriesStartingLetters ($id, $query, $n);
            case Base::PAGE_ALL_BOOKS :
                return new PageAllBooks ($id, $query, $n);
            case Base::PAGE_ALL_BOOKS_LETTER:
                return new PageAllBooksLetter ($id, $query, $n);
            case Base::PAGE_ALL_RECENT_BOOKS :
                return new PageRecentBooks ($id, $query, $n);
            case Base::PAGE_SERIE_DETAIL : 
                return new PageSerieDetail ($id, $query, $n);
            case Base::PAGE_OPENSEARCH_QUERY :
                return new PageQueryResult ($id, $query, $n);
            case Base::PAGE_BOOK_DETAIL :
                return new PageBookDetail ($id, $query, $n);
            case Base::PAGE_ABOUT :
                return new PageAbout ($id, $query, $n);
            case Base::PAGE_CUSTOMIZE : 
                return new PageCustomize ($id, $query, $n);
            default:
                $page = new Page ($id, $query, $n);
                $page->idPage = "cops:catalog";
                return $page;
        }
    }
    
    public function __construct($pid, $pquery, $pn) {
        global $config;
        
        $this->idGet = $pid;
        $this->query = $pquery;
        $this->n = $pn;
        $this->favicon = $config['cops_icon'];
    }
    
    public function InitializeContent () 
    {
        global $config;
        $this->title = $config['cops_title_default'];
        $this->subtitle = $config['cops_subtitle_default'];
        array_push ($this->entryArray, Author::getCount());
        $series = Serie::getCount();
        if (!is_null ($series)) array_push ($this->entryArray, $series);
        $tags = Tag::getCount();
        if (!is_null ($tags)) array_push ($this->entryArray, $tags);
        $languages = Language::getCount();
        if (!is_null ($languages)) array_push ($this->entryArray, $languages);
        $this->entryArray = array_merge ($this->entryArray, Book::getCount());
    }

    public function isPaginated ()
    {
        global $config;
        return (getCurrentOption ("max_item_per_page") != -1 && 
                $this->totalNumber != -1 && 
                $this->totalNumber > getCurrentOption ("max_item_per_page"));
    }
    
    public function getNextLink ()
    {
        global $config;
        $currentUrl = $_SERVER['QUERY_STRING'];
        $currentUrl = preg_replace ("/\&n=.*?$/", "", "?" . $_SERVER['QUERY_STRING']);
        if (($this->n) * getCurrentOption ("max_item_per_page") < $this->totalNumber) {
            return new LinkNavigation ($currentUrl . "&n=" . ($this->n + 1), "next", "Page suivante");
        }
        return NULL;
    }
    
    public function getPrevLink ()
    {
        global $config;
        $currentUrl = $_SERVER['QUERY_STRING'];
        $currentUrl = preg_replace ("/\&n=.*?$/", "", "?" . $_SERVER['QUERY_STRING']);
        if ($this->n > 1) {
            return new LinkNavigation ($currentUrl . "&n=" . ($this->n - 1), "previous", "Page precedente");
        }
        return NULL;
    }
    
    public function getMaxPage ()
    {
        global $config;
        return ceil ($this->totalNumber / getCurrentOption ("max_item_per_page"));
    }
    
    public function containsBook ()
    {
        if (count ($this->entryArray) == 0) return false;
        if (get_class ($this->entryArray [0]) == "EntryBook") return true;
        return false;
    }

}

class PageAllAuthorsLetter extends Page
{
    public function InitializeContent () 
    {
        global $config;
        
        $this->idPage = Author::getEntryIdByLetter ($this->idGet);
        $this->entryArray = Author::getAuthorsByFirstLetter ($this->idGet);
        if ($this->idGet) {
          $this->title = localize("authors.starting").' '.$this->idGet;
        } else {
          $this->title = localize("authors.title");
        }
    }
}

class PageAuthorsLetter extends Page
{
    public function InitializeContent () 
    {
        global $config;
        
        $this->idPage = Author::getEntryIdByLetter ($this->idGet);
        $this->entryArray = Author::getAuthorsByStartingLetter ($this->idGet);
        $this->title = str_format (localize ("splitByLetter.letter"), str_format (localize ("authorword", count ($this->entryArray)), count ($this->entryArray)), $this->idGet);
    }
}

class PageAuthorDetail extends Page
{
    public function InitializeContent () 
    {
        $author = Author::getAuthorById ($this->idGet);
        $this->idPage = $author->getEntryId ();
        $this->title = $author->name;
        list ($this->entryArray, $this->totalNumber) = Book::getBooksByAuthor ($this->idGet, $this->n);
    }
}

class PageAllTags extends Page
{
    public function InitializeContent () 
    {
        $this->title = localize("tags.title");
        $array = Tag::getAllTags();
        asort($array);
        $this->entryArray = $array;
        $this->idPage = Tag::ALL_TAGS_ID;
    }
}

class PageAllLanguages extends Page
{
    public function InitializeContent () 
    {
        $this->title = localize("languages.title");
        $array = Language::getAllLanguages();
        asort($array);
        $this->entryArray = $array;
        $this->idPage = Language::ALL_LANGUAGES_ID;
    }
}

class PageTagDetail extends Page
{
    public function InitializeContent () 
    {
        $tag = Tag::getTagById ($this->idGet);
        $this->idPage = $tag->getEntryId ();
        $this->title = $tag->name;
        list ($this->entryArray, $this->totalNumber) = Book::getBooksByTag ($this->idGet, $this->n);
    }
}

class PageLanguageDetail extends Page
{
    public function InitializeContent () 
    {
        $language = Language::getLanguageById ($this->idGet);
        $this->idPage = $language->getEntryId ();
        $this->title = $language->lang_code;
        list ($this->entryArray, $this->totalNumber) = Book::getBooksByLanguage ($this->idGet, $this->n);
    }
}

class PageAllSeries extends Page
{
    public function InitializeContent () 
    {
        if ($this->idGet) {
          $this->title = str_format (localize ("splitByLetter.letter"), str_format (localize ("series.title")), $this->idGet);
        } else {
          $this->title = localize("series.title");
        }  
        $this->entryArray = Serie::getAllSeries($this->idGet);
        $this->idPage = Serie::getEntryIdByLetters($this->idGet);
    }
}

class PageAllSeriesStartingLetters extends Page
{
    public function InitializeContent () 
    {
        $this->entryArray = Serie::getAllSeriesStartingLetters($this->idGet);
        $this->title = str_format (localize ("splitByLetter.letter"), str_format (localize ("series.title")), $this->idGet);
        $this->idPage = Serie::getEntryIdByLetters($this->idGet);
    }
}

class PageSerieDetail extends Page
{
    public function InitializeContent () 
    {
        $serie = Serie::getSerieById ($this->idGet);
        $this->title = $serie->name;
        list ($this->entryArray, $this->totalNumber) = Book::getBooksBySeries ($this->idGet, $this->n);
        $this->idPage = $serie->getEntryId ();
    }
}

class PageAllBooks extends Page
{
    public function InitializeContent () 
    {
        $this->entryArray = Book::getAllBooks ($this->idGet);
        if ($this->entryArray->count==1) {
          $this->title = $this->idGet;
        } elseif ($this->idGet) {
          $this->title = str_format (localize ("splitByLetter.letter"), str_format (localize ("bookword.title")), $this->idGet);
        } else {
          $this->title = localize ("allbooks.title");
        }  
        $this->idPage = Book::ALL_BOOKS_ID;
    }
}

class PageAllBooksLetter extends Page
{
    public function InitializeContent () 
    {
        list ($this->entryArray, $this->totalNumber) = Book::getBooksByStartingLetter ($this->idGet, $this->n);
        $this->idPage = Book::getEntryIdByLetter ($this->idGet);
        
        $count = $this->totalNumber;
        if ($count == -1)
            $count = count ($this->entryArray);
        
        if ($count==1) {
          $this->title = $this->idGet;
        } else {
          $this->title = str_format (localize ("splitByLetter.letter"), str_format (localize ("bookword", $count), $count), $this->idGet);
        }  
    }
}

class PageRecentBooks extends Page
{
    public function InitializeContent () 
    {
        $this->title = localize ("recent.title");
        $this->entryArray = Book::getAllRecentBooks ();
        $this->idPage = Book::ALL_RECENT_BOOKS_ID;
    }
}

class PageQueryResult extends Page
{
    public function InitializeContent () 
    {
        global $config;
        $this->title = str_format (localize ("search.result"), $this->query);
        $currentPage = getURLParam ("current", NULL);
        
        switch ($currentPage) {
            case Base::PAGE_AUTHORS_FIRST_LETTER :
                $this->entryArray = Author::getAuthorsByStartingLetter ('%' . $this->query);
                break;
            default:
                list ($this->entryArray, $this->totalNumber) = Book::getBooksByQuery ($this->query, $this->n);
        }
    }
}

class PageBookDetail extends Page
{
    public function InitializeContent () 
    {
        $this->book = Book::getBookById ($this->idGet);
        $this->title = $this->book->title;
    }
}

class PageAbout extends Page
{
    public function InitializeContent () 
    {
        $this->title = localize ("about.title");
    }
}

class PageCustomize extends Page
{
    public function InitializeContent () 
    {
        global $config;
        $this->title = localize ("customize.title");
        $this->entryArray = array ();
        
        $use_fancybox = "";
        if (getCurrentOption ("use_fancyapps") == 1) {
            $use_fancybox = "checked='checked'";
        }
        $html_tag_filter = "";
        if (getCurrentOption ("html_tag_filter") == 1) {
            $html_tag_filter = "checked='checked'";
        }
        
        
        $content = "";
        if (!preg_match("/(Kobo|Kindle\/3.0|EBRD1101)/", $_SERVER['HTTP_USER_AGENT'])) {
            $content .= '<select id="style" onchange="updateCookie (this);">';
        }
            foreach (glob ("styles/style-*.css") as $filename) {
                if (preg_match ('/styles\/style-(.*?)\.css/', $filename, $m)) {
                    $filename = $m [1];
                }
                $selected = "";
                if (getCurrentOption ("style") == $filename) {
                    if (!preg_match("/(Kobo|Kindle\/3.0|EBRD1101)/", $_SERVER['HTTP_USER_AGENT'])) {
                        $selected = "selected='selected'";
                    } else {
                        $selected = "checked='checked'";
                    }
                }
                if (!preg_match("/(Kobo|Kindle\/3.0|EBRD1101)/", $_SERVER['HTTP_USER_AGENT'])) {
                    $content .= "<option value='{$filename}' {$selected}>{$filename}</option>";
                } else {
                    $content .= "<input type='radio' onchange='updateCookieFromCheckbox (this);' id='style-{$filename}' name='style' value='{$filename}' {$selected} /><label for='style-{$filename}'> {$filename} </label>";
                }
            }
        if (!preg_match("/(Kobo|Kindle\/3.0|EBRD1101)/", $_SERVER['HTTP_USER_AGENT'])) {
            $content .= '</select>';
        }
        array_push ($this->entryArray, new Entry (localize ("customize.style"), "", 
                                        $content, "text", 
                                        array ()));
        
        $content = '<input type="checkbox" onchange="updateCookieFromCheckbox (this);" id="use_fancyapps" ' . $use_fancybox . ' />';
        array_push ($this->entryArray, new Entry (localize ("customize.fancybox"), "", 
                                        $content, "text", 
                                        array ()));
        $content = '<input type="number" onchange="updateCookie (this);" id="max_item_per_page" value="' . getCurrentOption ("max_item_per_page") . '" min="-1" max="1200" pattern="^[-+]?[0-9]+$" />';
        array_push ($this->entryArray, new Entry (localize ("customize.paging"), "", 
                                        $content, "text", 
                                        array ()));
        $content = '<input type="number" onchange="updateCookie (this);" id="max_books_pages" value="' . getCurrentOption ("max_books_pages") . '" min="-1" max="50" pattern="^[-+]?[0-9]+$" />';
        array_push ($this->entryArray, new Entry (localize ("customize.pages"), "", 
                                        $content, "text", 
                                        array ()));
        $content = '<input type="text" onchange="updateCookie (this);" id="email" value="' . getCurrentOption ("email") . '" />';
        array_push ($this->entryArray, new Entry (localize ("customize.email"), "", 
                                        $content, "text", 
                                        array ()));
        $content = '<input type="checkbox" onchange="updateCookieFromCheckbox (this);" id="html_tag_filter" ' . $html_tag_filter . ' />';
        array_push ($this->entryArray, new Entry (localize ("customize.filter"), "", 
                                        $content, "text", 
                                        array ()));
    }
}


abstract class Base
{
    const PAGE_INDEX = "index";
    const PAGE_AUTHORS_FIRST_LETTER = "AUTH";
    const PAGE_AUTHORS_STARTING_LETTERS = "AUTHST";
    const PAGE_AUTHOR_DETAIL = "AUTHDET";
    const PAGE_ALL_BOOKS = "BOOK";
    const PAGE_ALL_BOOKS_LETTER = "BOOKST";
    const PAGE_ALL_SERIES = "SER";
    const PAGE_SERIES_STARTING_LETTERS = "SERST";
    const PAGE_SERIE_DETAIL = "SERDET";
    const PAGE_OPENSEARCH = "OPEN";
    const PAGE_OPENSEARCH_QUERY = "OPENQ";
    const PAGE_ALL_RECENT_BOOKS = "RECENT";
    const PAGE_ALL_TAGS = "GENR";
    const PAGE_TAG_DETAIL = "GENRDET";
    const PAGE_BOOK_DETAIL = "BOOKDET";
    const PAGE_ABOUT = "ABOUT";
    const PAGE_ALL_LANGUAGES = "LANG";
    const PAGE_LANGUAGE_DETAIL = "LANGDET";   
    const PAGE_CUSTOMIZE = "OPTS"; 

    const COMPATIBILITY_XML_ALDIKO = "aldiko";
    
    private static $db = NULL;

    public static function getFileDirectory () {
        global $config;
        return $config['file_directory'];
    }

    public static function getDbHost () {
        global $config;
        return $config['db_host'];
    }

    public static function getDbUser () {
        global $config;
        return $config['db_user'];
    }

    public static function getDbPasswd () {
        global $config;
        return $config['db_passwd'];
    }

    public static function getDb () {
        global $config;
        if (is_null (self::$db)) {
            try {
                self::$db = new PDO('mysql:host='. self::getDbHost (), self::getDbUser(), self::getDbPasswd());
                self::$db -> exec("set names utf8");
            } catch (Exception $e) {
                header("location: checkconfig.php?err=1");
                exit();
            }
        }
        return self::$db;
    }
    
    public static function clearDb () {
        self::$db = NULL;
    }
    
    public static function executeQuery($query, $columns, $filter, $params, $n) {
        global $config;
        $totalResult = -1;
        
        if (getCurrentOption ("max_item_per_page") != -1 && $n != -1)
        {
            // First check total number of results
            $query_str = str_format ($query, "count(*)", $filter);
            $result = self::getDb ()->prepare ($query_str);
            $result->execute ($params);
            $totalResult = $result->fetchColumn (0);
            
            // Next modify the query and params
            $query .= " limit ".(($n - 1) * getCurrentOption ("max_item_per_page")).", ".getCurrentOption ("max_item_per_page");
        }

        $query_str = str_format($query, $columns, $filter);
        $result = self::getDb ()->prepare($query_str);
        $result->execute ($params);
        return array ($totalResult, $result);
    }

    public static function booksPages ()
    {
        global $config;
        $result = getCurrentOption("max_books_pages");
        if ($result=="") { $result = 10; }
        return $result;
    }
    
    public static function maxItemsPerPage()
    {
        global $config;
        $result = getCurrentOption("max_item_per_page");
        if ($result=="") { $result = 25; }
        return $result;
    }


}