Completed
Pull Request — master (#233)
by
unknown
05:24
created

PageQueryResult::doSearchByCategory()   F

Complexity

Conditions 18
Paths 298

Size

Total Lines 79
Code Lines 54

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 61
CRAP Score 18.0334
Metric Value
dl 0
loc 79
ccs 61
cts 64
cp 0.9531
rs 3.8594
cc 18
eloc 54
nc 298
nop 0
crap 18.0334

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * COPS (Calibre OPDS PHP Server) class file
4
 *
5
 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
6
 * @author     S�bastien Lucas <[email protected]>
7
 */
8
9
define ("VERSION", "1.0.0RC4");
10
define ("DB", "db");     // url parameter for the selected database
11
define ("VL", "vl");     // url parameter for the selected virtual library
12
date_default_timezone_set($config['default_timezone']);
13
14
require_once('virtuallib.php');
15
16
17
function useServerSideRendering () {
18 3
    global $config;
19 3
    return preg_match("/" . $config['cops_server_side_render'] . "/", $_SERVER['HTTP_USER_AGENT']);
20
}
21
22
function serverSideRender ($data) {
23
    // Get the templates
24 2
    $theme = getCurrentTemplate ();
25 2
    $header = file_get_contents('templates/' . $theme . '/header.html');
26 2
    $footer = file_get_contents('templates/' . $theme . '/footer.html');
27 2
    $main = file_get_contents('templates/' . $theme . '/main.html');
28 2
    $bookdetail = file_get_contents('templates/' . $theme . '/bookdetail.html');
29 2
    $page = file_get_contents('templates/' . $theme . '/page.html');
30
31
    // Generate the function for the template
32 2
    $template = new doT ();
33 2
    $dot = $template->template ($page, array ("bookdetail" => $bookdetail,
34 2
                                              "header" => $header,
35 2
                                              "footer" => $footer,
36 2
                                              "main" => $main));
37
    // If there is a syntax error in the function created
38
    // $dot will be equal to FALSE
39 2
    if (!$dot) {
40
        return FALSE;
41
    }
42
    // Execute the template
43 2
    if (!empty ($data)) {
44
        return $dot ($data);
45
    }
46
47 2
    return NULL;
48
}
49
50
function getQueryString () {
51 18
    if ( isset($_SERVER['QUERY_STRING']) ) {
52 16
        return $_SERVER['QUERY_STRING'];
53
    }
54 2
    return "";
55
}
56
57
function notFound () {
58
    header($_SERVER["SERVER_PROTOCOL"]." 404 Not Found");
59
    header("Status: 404 Not Found");
60
61
    $_SERVER['REDIRECT_STATUS'] = 404;
62
}
63
64
function getURLParam ($name, $default = NULL) {
65 101
    if (!empty ($_GET) && isset($_GET[$name]) && $_GET[$name] != "") {
66 24
        return $_GET[$name];
67
    }
68 101
    return $default;
69
}
70
71
function getCurrentOption ($option) {
72 87
    global $config;
73 87
    if (isset($_COOKIE[$option])) {
74 2
        if (isset($config ["cops_" . $option]) && is_array ($config ["cops_" . $option])) {
75
            return explode (",", $_COOKIE[$option]);
76
        } else {
77 2
            return $_COOKIE[$option];
78
        }
79
    }
80 85
    if ($option == "style") {
81 2
        return "default";
82
    }
83
84 85
    if (isset($config ["cops_" . $option])) {
85 85
        return $config ["cops_" . $option];
86
    }
87
88
    return "";
89
}
90
91
function getCurrentCss () {
92 2
    return "templates/" . getCurrentTemplate () . "/styles/style-" . getCurrentOption ("style") . ".css";
93
}
94
95
function getCurrentTemplate () {
96 4
    return getCurrentOption ("template");
97
}
98
99
function getUrlWithVersion ($url) {
100 50
    return $url . "?v=" . VERSION;
101
}
102
103
function xml2xhtml($xml) {
104 35
    return preg_replace_callback('#<(\w+)([^>]*)\s*/>#s', create_function('$m', '
105
        $xhtml_tags = array("br", "hr", "input", "frame", "img", "area", "link", "col", "base", "basefont", "param");
106
        return in_array($m[1], $xhtml_tags) ? "<$m[1]$m[2] />" : "<$m[1]$m[2]></$m[1]>";
107 35
    '), $xml);
108
}
109
110
function display_xml_error($error)
111
{
112
    $return = "";
113
    $return .= str_repeat('-', $error->column) . "^\n";
114
115
    switch ($error->level) {
116
        case LIBXML_ERR_WARNING:
117
            $return .= "Warning $error->code: ";
118
            break;
119
         case LIBXML_ERR_ERROR:
120
            $return .= "Error $error->code: ";
121
            break;
122
        case LIBXML_ERR_FATAL:
123
            $return .= "Fatal Error $error->code: ";
124
            break;
125
    }
126
127
    $return .= trim($error->message) .
128
               "\n  Line: $error->line" .
129
               "\n  Column: $error->column";
130
131
    if ($error->file) {
132
        $return .= "\n  File: $error->file";
133
    }
134
135
    return "$return\n\n--------------------------------------------\n\n";
136
}
137
138
function are_libxml_errors_ok ()
139
{
140 35
    $errors = libxml_get_errors();
141
142 35
    foreach ($errors as $error) {
143
        if ($error->code == 801) return false;
144 35
    }
145 35
    return true;
146
}
147
148
function html2xhtml ($html) {
149 35
    $doc = new DOMDocument();
150 35
    libxml_use_internal_errors(true);
151
152 35
    $doc->loadHTML('<html><head><meta http-equiv="content-type" content="text/html; charset=utf-8"></head><body>' .
153 35
                        $html  . '</body></html>'); // Load the HTML
154 35
    $output = $doc->saveXML($doc->documentElement); // Transform to an Ansi xml stream
155 35
    $output = xml2xhtml($output);
156 35
    if (preg_match ('#<html><head><meta http-equiv="content-type" content="text/html; charset=utf-8"></meta></head><body>(.*)</body></html>#ms', $output, $matches)) {
157 35
        $output = $matches [1]; // Remove <html><body>
158 35
    }
159
    /*
160
    // In case of error with summary, use it to debug
161
    $errors = libxml_get_errors();
162
163
    foreach ($errors as $error) {
164
        $output .= display_xml_error($error);
165
    }
166
    */
167
168 35
    if (!are_libxml_errors_ok ()) $output = "HTML code not valid.";
169
170 35
    libxml_use_internal_errors(false);
171 35
    return $output;
172
}
173
174
/**
175
 * This method is a direct copy-paste from
176
 * http://tmont.com/blargh/2010/1/string-format-in-php
177
 */
178
function str_format($format) {
0 ignored issues
show
Unused Code introduced by
The parameter $format is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
179 94
    $args = func_get_args();
180 94
    $format = array_shift($args);
181
182 94
    preg_match_all('/(?=\{)\{(\d+)\}(?!\})/', $format, $matches, PREG_OFFSET_CAPTURE);
183 94
    $offset = 0;
184 94
    foreach ($matches[1] as $data) {
185 94
        $i = $data[0];
186 94
        $format = substr_replace($format, @$args[$i], $offset + $data[1] - 1, 2 + strlen($i));
187 94
        $offset += strlen(@$args[$i]) - 2 - strlen($i);
188 94
    }
189
190 94
    return $format;
191
}
192
193
/**
194
 * Get all accepted languages from the browser and put them in a sorted array
195
 * languages id are normalized : fr-fr -> fr_FR
196
 * @return array of languages
197
 */
198
function getAcceptLanguages() {
199 16
    $langs = array();
200
201 16
    if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
202
        // break up string into pieces (languages and q factors)
203 16
        $accept = $_SERVER['HTTP_ACCEPT_LANGUAGE'];
204 16
        if (preg_match('/^(\w{2})-\w{2}$/', $accept, $matches)) {
205
            // Special fix for IE11 which send fr-FR and nothing else
206 3
            $accept = $accept . "," . $matches[1] . ";q=0.8";
207 3
        }
208 16
        preg_match_all('/([a-z]{1,8}(-[a-z]{1,8})?)\s*(;\s*q\s*=\s*(1|0\.[0-9]+))?/i', $accept, $lang_parse);
209
210 16
        if (count($lang_parse[1])) {
211 16
            $langs = array();
212 16
            foreach ($lang_parse[1] as $lang) {
213
                // Format the language code (not standard among browsers)
214 16
                if (strlen($lang) == 5) {
215 11
                    $lang = str_replace("-", "_", $lang);
216 11
                    $splitted = preg_split("/_/", $lang);
217 11
                    $lang = $splitted[0] . "_" . strtoupper($splitted[1]);
218 11
                }
219 16
                array_push($langs, $lang);
220 16
            }
221
            // create a list like "en" => 0.8
222 16
            $langs = array_combine($langs, $lang_parse[4]);
223
224
            // set default to 1 for any without q factor
225 16
            foreach ($langs as $lang => $val) {
226 16
                if ($val === '') $langs[$lang] = 1;
227 16
            }
228
229
            // sort list based on value
230 16
            arsort($langs, SORT_NUMERIC);
231 16
        }
232 16
    }
233
234 16
    return $langs;
235
}
236
237
/**
238
 * Find the best translation file possible based on the accepted languages
239
 * @return array of language and language file
240
 */
241
function getLangAndTranslationFile() {
242 17
    global $config;
243 17
    $langs = array();
244 17
    $lang = "en";
245 17
    if (!empty($config['cops_language'])) {
246
        $lang = $config['cops_language'];
247
    }
248 17
    elseif (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
249 16
        $langs = getAcceptLanguages();
250 16
    }
251
    //echo var_dump($langs);
252 17
    $lang_file = NULL;
253 17
    foreach ($langs as $language => $val) {
254 16
        $temp_file = dirname(__FILE__). '/lang/Localization_' . $language . '.json';
255 16
        if (file_exists($temp_file)) {
256 16
            $lang = $language;
257 16
            $lang_file = $temp_file;
258 16
            break;
259
        }
260 17
    }
261 17
    if (empty ($lang_file)) {
262 3
        $lang_file = dirname(__FILE__). '/lang/Localization_' . $lang . '.json';
263 3
    }
264 17
    return array($lang, $lang_file);
265
}
266
267
/**
268
 * This method is based on this page
269
 * http://www.mind-it.info/2010/02/22/a-simple-approach-to-localization-in-php/
270
 */
271
function localize($phrase, $count=-1, $reset=false) {
272 114
    global $config;
273 114
    if ($count == 0)
274 114
        $phrase .= ".none";
275 114
    if ($count == 1)
276 114
        $phrase .= ".one";
277 114
    if ($count > 1)
278 114
        $phrase .= ".many";
279
280
    /* Static keyword is used to ensure the file is loaded only once */
281 114
    static $translations = NULL;
282 114
    if ($reset) {
283 16
        $translations = NULL;
284 16
    }
285
    /* If no instance of $translations has occured load the language file */
286 114
    if (is_null($translations)) {
287 17
        $lang_file_en = NULL;
288 17
        list ($lang, $lang_file) = getLangAndTranslationFile();
289 17
        if ($lang != "en") {
290 1
            $lang_file_en = dirname(__FILE__). '/lang/' . 'Localization_en.json';
291 1
        }
292
293 17
        $lang_file_content = file_get_contents($lang_file);
294
        /* Load the language file as a JSON object and transform it into an associative array */
295 17
        $translations = json_decode($lang_file_content, true);
296
297
        /* Clean the array of all unfinished translations */
298 17
        foreach (array_keys ($translations) as $key) {
299 17
            if (preg_match ("/^##TODO##/", $key)) {
300 1
                unset ($translations [$key]);
301 1
            }
302 17
        }
303
        if ($lang_file_en)
0 ignored issues
show
Bug Best Practice introduced by
The expression $lang_file_en of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
304 17
        {
305 1
            $lang_file_content = file_get_contents($lang_file_en);
306 1
            $translations_en = json_decode($lang_file_content, true);
307 1
            $translations = array_merge ($translations_en, $translations);
308 1
        }
309 17
    }
310 114
    if (array_key_exists ($phrase, $translations)) {
311 114
        return $translations[$phrase];
312
    }
313 1
    return $phrase;
314
}
315
316
function addURLParameter($urlParams, $paramName, $paramValue) {
317 58
    if (empty ($urlParams)) {
318 48
        $urlParams = "";
319 48
    }
320 58
    $start = "";
321 58
    if (preg_match ("#^\?(.*)#", $urlParams, $matches)) {
322 15
        $start = "?";
323 15
        $urlParams = $matches[1];
324 15
    }
325 58
    $params = array();
326 58
    parse_str($urlParams, $params);
327 58
    if (empty ($paramValue) && $paramValue != 0) {
328
        unset ($params[$paramName]);
329
    } else {
330 58
        $params[$paramName] = $paramValue;
331
    }
332 58
    return $start . http_build_query($params);
333
}
334
335
function useNormAndUp () {
336 107
    global $config;
337 107
    return $config ['cops_normalized_search'] == "1";
338
}
339
340
function normalizeUtf8String( $s) {
341 8
    include_once 'transliteration.php';
342 8
    return _transliteration_process($s);
343
}
344
345
function normAndUp ($s) {
346 7
    return mb_strtoupper (normalizeUtf8String($s), 'UTF-8');
347
}
348
349
class Link
350
{
351
    const OPDS_THUMBNAIL_TYPE = "http://opds-spec.org/image/thumbnail";
352
    const OPDS_IMAGE_TYPE = "http://opds-spec.org/image";
353
    const OPDS_ACQUISITION_TYPE = "http://opds-spec.org/acquisition";
354
    const OPDS_NAVIGATION_TYPE = "application/atom+xml;profile=opds-catalog;kind=navigation";
355
    const OPDS_PAGING_TYPE = "application/atom+xml;profile=opds-catalog;kind=acquisition";
356
357
    public $href;
358
    public $type;
359
    public $rel;
360
    public $title;
361
    public $facetGroup;
362
    public $activeFacet;
363
364 96
    public function __construct($phref, $ptype, $prel = NULL, $ptitle = NULL, $pfacetGroup = NULL, $pactiveFacet = FALSE) {
365 96
        $this->href = $phref;
366 96
        $this->type = $ptype;
367 96
        $this->rel = $prel;
368 96
        $this->title = $ptitle;
369 96
        $this->facetGroup = $pfacetGroup;
370 96
        $this->activeFacet = $pactiveFacet;
371 96
    }
372
373 10
    public function hrefXhtml () {
374 10
        return $this->href;
375
    }
376
}
377
378
class LinkNavigation extends Link
379
{
380 95
    public function __construct($phref, $prel = NULL, $ptitle = NULL) {
381 95
        parent::__construct ($phref, Link::OPDS_NAVIGATION_TYPE, $prel, $ptitle);
382 95
        if (!is_null (GetUrlParam (DB))) {
383 1
        	$this->href = addURLParameter ($this->href, DB, GetUrlParam (DB));
384 1
        	$this->href = addURLParameter ($this->href, VL, GetUrlParam (VL, 0));
385 1
        }
386 95
        if (!preg_match ("#^\?(.*)#", $this->href) && !empty ($this->href)) $this->href = "?" . $this->href;
387 95
        if (preg_match ("/(bookdetail|getJSON).php/", $_SERVER["SCRIPT_NAME"])) {
388
            $this->href = "index.php" . $this->href;
389
        } else {
390 95
            $this->href = $_SERVER["SCRIPT_NAME"] . $this->href;
391
        }
392 95
    }
393
}
394
395
class LinkFacet extends Link
396
{
397 1
    public function __construct($phref, $ptitle = NULL, $pfacetGroup = NULL, $pactiveFacet = FALSE) {
398 1
        parent::__construct ($phref, Link::OPDS_PAGING_TYPE, "http://opds-spec.org/facet", $ptitle, $pfacetGroup, $pactiveFacet);
399 1
        if (!is_null (GetUrlParam (DB))) {
400
        	$this->href = addURLParameter ($this->href, DB, GetUrlParam (DB));
401
        	$this->href = addURLParameter ($this->href, VL, GetUrlParam (VL, 0));
402
        }
403 1
        $this->href = $_SERVER["SCRIPT_NAME"] . $this->href;
404 1
    }
405
}
406
407
class Entry
408
{
409
    public $title;
410
    public $id;
411
    public $content;
412
    public $numberOfElement;
413
    public $contentType;
414
    public $linkArray;
415
    public $localUpdated;
416
    public $className;
417
    private static $updated = NULL;
418
419
    public static $icons = array(
420
        Author::ALL_AUTHORS_ID       => 'images/author.png',
421
        Serie::ALL_SERIES_ID         => 'images/serie.png',
422
        Book::ALL_RECENT_BOOKS_ID    => 'images/recent.png',
423
        Tag::ALL_TAGS_ID             => 'images/tag.png',
424
        Language::ALL_LANGUAGES_ID   => 'images/language.png',
425
        CustomColumn::ALL_CUSTOMS_ID => 'images/tag.png',
426
        "cops:books$"             => 'images/allbook.png',
427
        "cops:books:letter"       => 'images/allbook.png',
428
        Publisher::ALL_PUBLISHERS_ID => 'images/publisher.png'
429
    );
430
431
    public function getUpdatedTime () {
432
        if (!is_null ($this->localUpdated)) {
433
            return date (DATE_ATOM, $this->localUpdated);
434
        }
435
        if (is_null (self::$updated)) {
436
            self::$updated = time();
437
        }
438
        return date (DATE_ATOM, self::$updated);
439
    }
440
441 7
    public function getNavLink () {
442 7
        foreach ($this->linkArray as $link) {
443 7
            if ($link->type != Link::OPDS_NAVIGATION_TYPE) { continue; }
444
445 7
            return $link->hrefXhtml ();
446
        }
447
        return "#";
448
    }
449
450 89
    public function __construct($ptitle, $pid, $pcontent, $pcontentType, $plinkArray, $pclass = "", $pcount = 0) {
451 89
        global $config;
452 89
        $this->title = $ptitle;
453 89
        $this->id = $pid;
454 89
        $this->content = $pcontent;
455 89
        $this->contentType = $pcontentType;
456 89
        $this->linkArray = $plinkArray;
457 89
        $this->className = $pclass;
458 89
        $this->numberOfElement = $pcount;
459
460 89
        if ($config['cops_show_icons'] == 1)
461 89
        {
462 89
            foreach (self::$icons as $reg => $image)
463
            {
464 89
                if (preg_match ("/" . $reg . "/", $pid)) {
465 50
                    array_push ($this->linkArray, new Link (getUrlWithVersion ($image), "image/png", Link::OPDS_THUMBNAIL_TYPE));
466 50
                    break;
467
                }
468 89
            }
469 89
        }
470
471 89
        if (!is_null (GetUrlParam (DB))) $this->id = str_replace ("cops:", "cops:" . GetUrlParam (DB) . ":" . GetUrlParam (VL,0) . ":", $this->id);
472 89
    }
473
}
474
475
class EntryBook extends Entry
476
{
477
    public $book;
478
479 39
    public function __construct($ptitle, $pid, $pcontent, $pcontentType, $plinkArray, $pbook) {
480 39
        parent::__construct ($ptitle, $pid, $pcontent, $pcontentType, $plinkArray);
481 39
        $this->book = $pbook;
482 39
        $this->localUpdated = $pbook->timestamp;
483 39
    }
484
485
    public function getCoverThumbnail () {
486
        foreach ($this->linkArray as $link) {
487
            if ($link->rel == Link::OPDS_THUMBNAIL_TYPE)
488
                return $link->hrefXhtml ();
489
        }
490
        return null;
491
    }
492
493
    public function getCover () {
494
        foreach ($this->linkArray as $link) {
495
            if ($link->rel == Link::OPDS_IMAGE_TYPE)
496
                return $link->hrefXhtml ();
497
        }
498
        return null;
499
    }
500
}
501
502
class Page
503
{
504
    public $title;
505
    public $subtitle = "";
506
    public $authorName = "";
507
    public $authorUri = "";
508
    public $authorEmail = "";
509
    public $idPage;
510
    public $idGet;
511
    public $query;
512
    public $favicon;
513
    public $n;
514
    public $book;
515
    public $totalNumber = -1;
516
    public $entryArray = array();
517
518 81
    public static function getPage ($pageId, $id, $query, $n)
519
    {
520
        switch ($pageId) {
521 81
            case Base::PAGE_ALL_AUTHORS :
522 3
                return new PageAllAuthors ($id, $query, $n);
523 78
            case Base::PAGE_AUTHORS_FIRST_LETTER :
524 1
                return new PageAllAuthorsLetter ($id, $query, $n);
525 77
            case Base::PAGE_AUTHOR_DETAIL :
526 7
                return new PageAuthorDetail ($id, $query, $n);
527 70
            case Base::PAGE_ALL_TAGS :
528 2
                return new PageAllTags ($id, $query, $n);
529 68
            case Base::PAGE_TAG_DETAIL :
530 1
                return new PageTagDetail ($id, $query, $n);
531 67
            case Base::PAGE_ALL_LANGUAGES :
532 2
                return new PageAllLanguages ($id, $query, $n);
533 65
            case Base::PAGE_LANGUAGE_DETAIL :
534 1
                return new PageLanguageDetail ($id, $query, $n);
535 64
            case Base::PAGE_ALL_CUSTOMS :
536 3
                return new PageAllCustoms ($id, $query, $n);
537 61
            case Base::PAGE_CUSTOM_DETAIL :
538 3
                return new PageCustomDetail ($id, $query, $n);
539 58
            case Base::PAGE_ALL_RATINGS :
540 1
                return new PageAllRating ($id, $query, $n);
541 57
            case Base::PAGE_RATING_DETAIL :
542 1
                return new PageRatingDetail ($id, $query, $n);
543 56
            case Base::PAGE_ALL_SERIES :
544 2
                return new PageAllSeries ($id, $query, $n);
545 54
            case Base::PAGE_ALL_BOOKS :
546 3
                return new PageAllBooks ($id, $query, $n);
547 51
            case Base::PAGE_ALL_BOOKS_LETTER:
548 1
                return new PageAllBooksLetter ($id, $query, $n);
549 50
            case Base::PAGE_ALL_RECENT_BOOKS :
550 4
                return new PageRecentBooks ($id, $query, $n);
551 46
            case Base::PAGE_SERIE_DETAIL :
552 1
                return new PageSerieDetail ($id, $query, $n);
553 45
            case Base::PAGE_OPENSEARCH_QUERY :
554 31
                return new PageQueryResult ($id, $query, $n);
555 14
            case Base::PAGE_BOOK_DETAIL :
556 1
                return new PageBookDetail ($id, $query, $n);
557 13
            case Base::PAGE_ALL_PUBLISHERS:
558 2
                return new PageAllPublishers ($id, $query, $n);
559 11
            case Base::PAGE_PUBLISHER_DETAIL :
560 1
                return new PagePublisherDetail ($id, $query, $n);
561 10
            case Base::PAGE_ABOUT :
562
                return new PageAbout ($id, $query, $n);
563 10
            case Base::PAGE_CUSTOMIZE :
564
                return new PageCustomize ($id, $query, $n);
565 10
            default:
566 10
                $page = new Page ($id, $query, $n);
567 10
                $page->idPage = "cops:catalog";
568 10
                return $page;
569 10
        }
570
    }
571
572 81
    public function __construct($pid, $pquery, $pn) {
573 81
        global $config;
574
575 81
        $this->idGet = $pid;
576 81
        $this->query = $pquery;
577 81
        $this->n = $pn;
578 81
        $this->favicon = $config['cops_icon'];
579 81
        $this->authorName = empty($config['cops_author_name']) ? utf8_encode('S�bastien Lucas') : $config['cops_author_name'];
580 81
        $this->authorUri = empty($config['cops_author_uri']) ? 'http://blog.slucas.fr' : $config['cops_author_uri'];
581 81
        $this->authorEmail = empty($config['cops_author_email']) ? '[email protected]' : $config['cops_author_email'];
582 81
    }
583
584 10
    public function InitializeContent ()
585
    {
586 10
        global $config;
587 10
        $this->title = $config['cops_title_default'];
588 10
        $this->subtitle = $config['cops_subtitle_default'];
589 10
        if (Base::noDatabaseSelected ()) {
590 2
            $i = 0;
591 2
            foreach (Base::getDbNameList () as $key) {
592 2
            	if (VirtualLib::isVLEnabled()) {
593
            		// Virtual Libraries are enabled show each virtual library as one database
594
            		$nBooks = Book::getBookCount ($i);
595
            		$j = 0;
596
            		foreach (VirtualLib::getVLNameList($i) as $vlName) {
597
            			array_push ($this->entryArray, new Entry (VirtualLib::getDisplayName($key, $vlName),
598
            								"cops:{$i}:catalog",
599
            								str_format (localize ("bookword", $nBooks), $nBooks), "text",
600
            								array ( new LinkNavigation ("?" . DB . "={$i}&" . VL . "={$j}")), "", $nBooks));
601
            			$j++;
602
            		}
603
            	} else {
604
            		// Virtual Libraries are enabled show each virtual library as one database
605 2
            		$nBooks = Book::getBookCount ($i);
606 2
	                array_push ($this->entryArray, new Entry ($key, "cops:{$i}:catalog",
607 2
	                                        str_format (localize ("bookword", $nBooks), $nBooks), "text",
608 2
	                                        array ( new LinkNavigation ("?" . DB . "={$i}")), "", $nBooks));
609
            	}
610 2
                $i++;
611 2
                Base::clearDb ();
612 2
            }
613 2
        } else {
614 8
            if (!in_array (PageQueryResult::SCOPE_AUTHOR, getCurrentOption ('ignored_categories'))) {
615 7
                array_push ($this->entryArray, Author::getCount());
616 7
            }
617 8
            if (!in_array (PageQueryResult::SCOPE_SERIES, getCurrentOption ('ignored_categories'))) {
618 7
                $series = Serie::getCount();
619 7
                if (!is_null ($series)) array_push ($this->entryArray, $series);
620 7
            }
621 8
            if (!in_array (PageQueryResult::SCOPE_PUBLISHER, getCurrentOption ('ignored_categories'))) {
622 7
                $publisher = Publisher::getCount();
623 7
                if (!is_null ($publisher)) array_push ($this->entryArray, $publisher);
624 7
            }
625 8
            if (!in_array (PageQueryResult::SCOPE_TAG, getCurrentOption ('ignored_categories'))) {
626 7
                $tags = Tag::getCount();
627 7
                if (!is_null ($tags)) array_push ($this->entryArray, $tags);
628 7
            }
629 8
            if (!in_array (PageQueryResult::SCOPE_RATING, getCurrentOption ('ignored_categories'))) {
630 8
                $rating = Rating::getCount();
631 8
                if (!is_null ($rating)) array_push ($this->entryArray, $rating);
632 8
            }
633 8
            if (!in_array ("language", getCurrentOption ('ignored_categories'))) {
634 7
                $languages = Language::getCount();
635 7
                if (!is_null ($languages)) array_push ($this->entryArray, $languages);
636 7
            }
637 8
            foreach ($config['cops_calibre_custom_column'] as $lookup) {
638 4
                $customId = CustomColumn::getCustomId ($lookup);
639 4
                if (!is_null ($customId)) {
640 4
                    array_push ($this->entryArray, CustomColumn::getCount($customId));
641 4
                }
642 8
            }
643 8
            $this->entryArray = array_merge ($this->entryArray, Book::getCount());
644
645 8
            if (Base::isDatabaseArray ()) $this->title =  Base::getDbName ();
646
        }
647 10
    }
648
649 17
    public function isPaginated ()
650
    {
651 17
        return (getCurrentOption ("max_item_per_page") != -1 &&
652 17
                $this->totalNumber != -1 &&
653 17
                $this->totalNumber > getCurrentOption ("max_item_per_page"));
654
    }
655
656 2
    public function getNextLink ()
657
    {
658 2
        $currentUrl = preg_replace ("/\&n=.*?$/", "", "?" . getQueryString ());
659 2
        if (($this->n) * getCurrentOption ("max_item_per_page") < $this->totalNumber) {
660 1
            return new LinkNavigation ($currentUrl . "&n=" . ($this->n + 1), "next", localize ("paging.next.alternate"));
661
        }
662 1
        return NULL;
663
    }
664
665 2
    public function getPrevLink ()
666
    {
667 2
        $currentUrl = preg_replace ("/\&n=.*?$/", "", "?" . getQueryString ());
668 2
        if ($this->n > 1) {
669 1
            return new LinkNavigation ($currentUrl . "&n=" . ($this->n - 1), "previous", localize ("paging.previous.alternate"));
670
        }
671 2
        return NULL;
672
    }
673
674 2
    public function getMaxPage ()
675
    {
676 2
        return ceil ($this->totalNumber / getCurrentOption ("max_item_per_page"));
677
    }
678
679 70
    public function containsBook ()
680
    {
681 70
        if (count ($this->entryArray) == 0) return false;
682 68
        if (get_class ($this->entryArray [0]) == "EntryBook") return true;
683 46
        return false;
684
    }
685
}
686
687
class PageAllAuthors extends Page
688
{
689 3
    public function InitializeContent ()
690
    {
691 3
        $this->title = localize("authors.title");
692 3
        if (getCurrentOption ("author_split_first_letter") == 1) {
693 2
            $this->entryArray = Author::getAllAuthorsByFirstLetter();
694 2
        }
695
        else {
696 1
            $this->entryArray = Author::getAllAuthors();
697
        }
698 3
        $this->idPage = Author::ALL_AUTHORS_ID;
699 3
    }
700
}
701
702
class PageAllAuthorsLetter extends Page
703
{
704 1
    public function InitializeContent ()
705
    {
706 1
        $this->idPage = Author::getEntryIdByLetter ($this->idGet);
707 1
        $this->entryArray = Author::getAuthorsByStartingLetter ($this->idGet);
708 1
        $this->title = str_format (localize ("splitByLetter.letter"), str_format (localize ("authorword", count ($this->entryArray)), count ($this->entryArray)), $this->idGet);
709 1
    }
710
}
711
712
class PageAuthorDetail extends Page
713
{
714 7
    public function InitializeContent ()
715
    {
716 7
        $author = Author::getAuthorById ($this->idGet);
717 7
        $this->idPage = $author->getEntryId ();
718 7
        $this->title = $author->name;
719 7
        list ($this->entryArray, $this->totalNumber) = Book::getBooksByAuthor ($this->idGet, $this->n);
720 7
    }
721
}
722
723
class PageAllPublishers extends Page
724
{
725 2
    public function InitializeContent ()
726
    {
727 2
        $this->title = localize("publishers.title");
728 2
        $this->entryArray = Publisher::getAllPublishers();
729 2
        $this->idPage = Publisher::ALL_PUBLISHERS_ID;
730 2
    }
731
}
732
733
class PagePublisherDetail extends Page
734
{
735 1
    public function InitializeContent ()
736
    {
737 1
        $publisher = Publisher::getPublisherById ($this->idGet);
738 1
        $this->title = $publisher->name;
739 1
        list ($this->entryArray, $this->totalNumber) = Book::getBooksByPublisher ($this->idGet, $this->n);
740 1
        $this->idPage = $publisher->getEntryId ();
741 1
    }
742
}
743
744
class PageAllTags extends Page
745
{
746 2
    public function InitializeContent ()
747
    {
748 2
        $this->title = localize("tags.title");
749 2
        $this->entryArray = Tag::getAllTags();
750 2
        $this->idPage = Tag::ALL_TAGS_ID;
751 2
    }
752
}
753
754
class PageAllLanguages extends Page
755
{
756 2
    public function InitializeContent ()
757
    {
758 2
        $this->title = localize("languages.title");
759 2
        $this->entryArray = Language::getAllLanguages();
760 2
        $this->idPage = Language::ALL_LANGUAGES_ID;
761 2
    }
762
}
763
764
class PageCustomDetail extends Page
765
{
766 3
    public function InitializeContent ()
767
    {
768 3
        $customId = getURLParam ("custom", NULL);
769 3
        $custom = CustomColumn::getCustomById ($customId, $this->idGet);
770 3
        $this->idPage = $custom->getEntryId ();
771 3
        $this->title = $custom->name;
772 3
        list ($this->entryArray, $this->totalNumber) = Book::getBooksByCustom ($customId, $this->idGet, $this->n);
773 3
    }
774
}
775
776
class PageAllCustoms extends Page
777
{
778 3
    public function InitializeContent ()
779
    {
780 3
        $customId = getURLParam ("custom", NULL);
781 3
        $this->title = CustomColumn::getAllTitle ($customId);
782 3
        $this->entryArray = CustomColumn::getAllCustoms($customId);
783 3
        $this->idPage = CustomColumn::getAllCustomsId ($customId);
784 3
    }
785
}
786
787
class PageTagDetail extends Page
788
{
789 1
    public function InitializeContent ()
790
    {
791 1
        $tag = Tag::getTagById ($this->idGet);
792 1
        $this->idPage = $tag->getEntryId ();
793 1
        $this->title = $tag->name;
794 1
        list ($this->entryArray, $this->totalNumber) = Book::getBooksByTag ($this->idGet, $this->n);
795 1
    }
796
}
797
798
class PageLanguageDetail extends Page
799
{
800 1
    public function InitializeContent ()
801
    {
802 1
        $language = Language::getLanguageById ($this->idGet);
803 1
        $this->idPage = $language->getEntryId ();
804 1
        $this->title = $language->lang_code;
805 1
        list ($this->entryArray, $this->totalNumber) = Book::getBooksByLanguage ($this->idGet, $this->n);
806 1
    }
807
}
808
809
class PageAllSeries extends Page
810
{
811 2
    public function InitializeContent ()
812
    {
813 2
        $this->title = localize("series.title");
814 2
        $this->entryArray = Serie::getAllSeries();
815 2
        $this->idPage = Serie::ALL_SERIES_ID;
816 2
    }
817
}
818
819
class PageSerieDetail extends Page
820
{
821 1
    public function InitializeContent ()
822
    {
823 1
        $serie = Serie::getSerieById ($this->idGet);
824 1
        $this->title = $serie->name;
825 1
        list ($this->entryArray, $this->totalNumber) = Book::getBooksBySeries ($this->idGet, $this->n);
826 1
        $this->idPage = $serie->getEntryId ();
827 1
    }
828
}
829
830
class PageAllRating extends Page
831
{
832 1
    public function InitializeContent ()
833
    {
834 1
        $this->title = localize("ratings.title");
835 1
        $this->entryArray = Rating::getAllRatings();
836 1
        $this->idPage = Rating::ALL_RATING_ID;
837 1
    }
838
}
839
840
class PageRatingDetail extends Page
841
{
842 1
    public function InitializeContent ()
843
    {
844 1
        $rating = Rating::getRatingById ($this->idGet);
845 1
        $this->idPage = $rating->getEntryId ();
846 1
        $this->title =str_format (localize ("ratingword", $rating->name/2), $rating->name/2);
847 1
        list ($this->entryArray, $this->totalNumber) = Book::getBooksByRating ($this->idGet, $this->n);
848 1
    }
849
}
850
851
class PageAllBooks extends Page
852
{
853 3
    public function InitializeContent ()
854
    {
855 3
        $this->title = localize ("allbooks.title");
856 3
        if (getCurrentOption ("titles_split_first_letter") == 1) {
857 2
            $this->entryArray = Book::getAllBooks();
858 2
        }
859
        else {
860 1
            list ($this->entryArray, $this->totalNumber) = Book::getBooks ($this->n);
861
        }
862 3
        $this->idPage = Book::ALL_BOOKS_ID;
863 3
    }
864
}
865
866
class PageAllBooksLetter extends Page
867
{
868 1
    public function InitializeContent ()
869
    {
870 1
        list ($this->entryArray, $this->totalNumber) = Book::getBooksByStartingLetter ($this->idGet, $this->n);
871 1
        $this->idPage = Book::getEntryIdByLetter ($this->idGet);
872
873 1
        $count = $this->totalNumber;
874 1
        if ($count == -1)
875 1
            $count = count ($this->entryArray);
876
877 1
        $this->title = str_format (localize ("splitByLetter.letter"), str_format (localize ("bookword", $count), $count), $this->idGet);
878 1
    }
879
}
880
881
class PageRecentBooks extends Page
882
{
883 4
    public function InitializeContent ()
884
    {
885 4
        $this->title = localize ("recent.title");
886 4
        $this->entryArray = Book::getAllRecentBooks ();
887 4
        $this->idPage = Book::ALL_RECENT_BOOKS_ID;
888 4
    }
889
}
890
891
class PageQueryResult extends Page
892
{
893
    const SCOPE_TAG = "tag";
894
    const SCOPE_RATING = "rating";
895
    const SCOPE_SERIES = "series";
896
    const SCOPE_AUTHOR = "author";
897
    const SCOPE_BOOK = "book";
898
    const SCOPE_PUBLISHER = "publisher";
899
900 24
    private function useTypeahead () {
901 24
        return !is_null (getURLParam ("search"));
902
    }
903
904 29
    private function searchByScope ($scope, $limit = FALSE) {
905 29
        $n = $this->n;
906 29
        $numberPerPage = NULL;
907 29
        $queryNormedAndUp = $this->query;
908 29
        if (useNormAndUp ()) {
909 7
            $queryNormedAndUp = normAndUp ($this->query);
910 7
        }
911 29
        if ($limit) {
912 22
            $n = 1;
913 22
            $numberPerPage = 5;
914 22
        }
915
        switch ($scope) {
916 29
            case self::SCOPE_BOOK :
917 23
                $array = Book::getBooksByStartingLetter ('%' . $queryNormedAndUp, $n, NULL, $numberPerPage);
918 23
                break;
919 28
            case self::SCOPE_AUTHOR :
920 23
                $array = Author::getAuthorsForSearch ('%' . $queryNormedAndUp);
921 23
                break;
922 25
            case self::SCOPE_SERIES :
923 22
                $array = Serie::getAllSeriesByQuery ($queryNormedAndUp);
924 22
                break;
925 24
            case self::SCOPE_TAG :
926 23
                $array = Tag::getAllTagsByQuery ($queryNormedAndUp, $n, NULL, $numberPerPage);
927 23
                break;
928 23
            case self::SCOPE_PUBLISHER :
929 23
                $array = Publisher::getAllPublishersByQuery ($queryNormedAndUp);
930 23
                break;
931
            default:
932
                $array = Book::getBooksByQuery (
933
                    array ("all" => "%" . $queryNormedAndUp . "%"), $n);
934
        }
935
936 29
        return $array;
937
    }
938
939 22
    public function doSearchByCategory () {
940 22
        $database = GetUrlParam (DB);
941 22
        $virtualLib = getURLParam(VL, 0);
942 22
        $out = array ();
943 22
        $pagequery = Base::PAGE_OPENSEARCH_QUERY;
944 22
        $dbArray = array ("");
945 22
        $d = $database;
946 22
        $query = $this->query;
947
        // Special case when no databases were chosen, we search on all databases
948 22
        if (Base::noDatabaseSelected ()) {
949 1
            $dbArray = Base::getDbNameList ();
950 1
            $d = 0;
951 1
        }
952 22
        $vl = $virtualLib;
953 22
        $vlArray = VirtualLib::getVLNameList($d);
954 22
        $vlArray = array($vlArray[$vl]);
955
        
956 22
        foreach ($dbArray as $dbKey) {
957 22
        	if (Base::noDatabaseSelected () && VirtualLib::isVLEnabled()) {
958
        		// If virtual libraries are enabled, but no Database is selected, 
959
        		// then iterate over all virtual libraries
960
        		$vlArray = VirtualLib::getVLNameList($d);
961
        		$vl = 0;
962
        	}
963 22
        	foreach ($vlArray as $vlKey) {	
964 22
	            if (Base::noDatabaseSelected ()) {
965 1
	                array_push ($this->entryArray, new Entry (VirtualLib::getDisplayName($dbKey, $vlKey), 
966 1
	                						DB . ":query:{$d}:{$vl}",
967 1
	                                        " ", "text",
968 1
	                                        array ( new LinkNavigation ("?" . DB . "={$d}&" . VL . "={$vl}")), "tt-header"));
969 1
	                Base::getDb ($d);
970
	                // TODO: set current virtual library in Base Or VirtualLib
971 1
	            }
972 22
	            foreach (array (PageQueryResult::SCOPE_BOOK,
973 22
	                            PageQueryResult::SCOPE_AUTHOR,
974 22
	                            PageQueryResult::SCOPE_SERIES,
975 22
	                            PageQueryResult::SCOPE_TAG,
976 22
	                            PageQueryResult::SCOPE_PUBLISHER) as $key) {
977 22
	                if (in_array($key, getCurrentOption ('ignored_categories'))) {
978 3
	                    continue;
979
	                }
980 22
	                $array = $this->searchByScope ($key, TRUE);
981
	
982 22
	                $i = 0;
983 22
	                if (count ($array) == 2 && is_array ($array [0])) {
984 22
	                    $total = $array [1];
985 22
	                    $array = $array [0];
986 22
	                } else {
987 22
	                    $total = count($array);
988
	                }
989 22
	                if ($total > 0) {
990
	                    // Comment to help the perl i18n script
991
	                    // str_format (localize("bookword", count($array))
992
	                    // str_format (localize("authorword", count($array))
993
	                    // str_format (localize("seriesword", count($array))
994
	                    // str_format (localize("tagword", count($array))
995
	                    // str_format (localize("publisherword", count($array))
996 21
	                    array_push ($this->entryArray, new Entry (str_format (localize ("search.result.{$key}"), $this->query), DB . ":query:{$d}:{$key}:{$vl}",
997 21
	                                        str_format (localize("{$key}word", $total), $total), "text",
998 21
	                                        array ( new LinkNavigation ("?page={$pagequery}&query={$query}&db={$d}&vl={$vl}&scope={$key}")),
999 21
	                                        Base::noDatabaseSelected () ? "" : "tt-header", $total));
1000 21
	                }
1001 22
	                if (!Base::noDatabaseSelected () && $this->useTypeahead ()) {
1002 6
	                    foreach ($array as $entry) {
1003 6
	                        array_push ($this->entryArray, $entry);
1004 6
	                        $i++;
1005 6
	                        if ($i > 4) { break; };
1006 6
	                    }
1007 6
	                }
1008 22
	            }
1009 22
	            $vl++;
1010 22
        	}
1011 22
            $d++;
1012 22
            if (Base::noDatabaseSelected ()) {
1013 1
                Base::clearDb ();
1014 1
            }
1015 22
        }
1016 22
        return $out;
1017
    }
1018
1019 31
    public function InitializeContent ()
1020
    {
1021 31
        $scope = getURLParam ("scope");
1022 31
        if (empty ($scope)) {
1023 24
            $this->title = str_format (localize ("search.result"), $this->query);
1024 24
        } else {
1025
            // Comment to help the perl i18n script
1026
            // str_format (localize ("search.result.author"), $this->query)
1027
            // str_format (localize ("search.result.tag"), $this->query)
1028
            // str_format (localize ("search.result.series"), $this->query)
1029
            // str_format (localize ("search.result.book"), $this->query)
1030
            // str_format (localize ("search.result.publisher"), $this->query)
1031 7
            $this->title = str_format (localize ("search.result.{$scope}"), $this->query);
1032
        }
1033
1034 31
        $crit = "%" . $this->query . "%";
1035
1036
        // Special case when we are doing a search and no database is selected
1037 31
        if (Base::noDatabaseSelected () && !$this->useTypeahead ()) {
1038 2
            $i = 0;
1039 2
            foreach (Base::getDbNameList () as $key) {
1040 2
                Base::clearDb ();
1041 2
                list ($array, $totalNumber) = Book::getBooksByQuery (array ("all" => $crit), 1, $i, 1);
0 ignored issues
show
Unused Code introduced by
The assignment to $array is unused. Consider omitting it like so list($first,,$third).

This checks looks for assignemnts to variables using the list(...) function, where not all assigned variables are subsequently used.

Consider the following code example.

<?php

function returnThreeValues() {
    return array('a', 'b', 'c');
}

list($a, $b, $c) = returnThreeValues();

print $a . " - " . $c;

Only the variables $a and $c are used. There was no need to assign $b.

Instead, the list call could have been.

list($a,, $c) = returnThreeValues();
Loading history...
1042 2
                array_push ($this->entryArray, new Entry ($key, DB . ":query:{$i}",
1043 2
                                        str_format (localize ("bookword", $totalNumber), $totalNumber), "text",
1044 2
                                        array ( new LinkNavigation ("?" . DB . "={$i}&page=9&query=" . $this->query)), "", $totalNumber));
1045 2
                $i++;
1046 2
            }
1047 2
            return;
1048
        }
1049 29
        if (empty ($scope)) {
1050 22
            $this->doSearchByCategory ();
1051 22
            return;
1052
        }
1053
1054 7
        $array = $this->searchByScope ($scope);
1055 7
        if (count ($array) == 2 && is_array ($array [0])) {
1056 2
            list ($this->entryArray, $this->totalNumber) = $array;
1057 2
        } else {
1058 5
            $this->entryArray = $array;
1059
        }
1060 7
    }
1061
}
1062
1063
class PageBookDetail extends Page
1064
{
1065 1
    public function InitializeContent ()
1066
    {
1067 1
        $this->book = Book::getBookById ($this->idGet);
1068 1
        $this->title = $this->book->title;
1069 1
    }
1070
}
1071
1072
class PageAbout extends Page
1073
{
1074
    public function InitializeContent ()
1075
    {
1076
        $this->title = localize ("about.title");
1077
    }
1078
}
1079
1080
class PageCustomize extends Page
1081
{
1082
    private function isChecked ($key, $testedValue = 1) {
1083
        $value = getCurrentOption ($key);
1084
        if (is_array ($value)) {
1085
            if (in_array ($testedValue, $value)) {
1086
                return "checked='checked'";
1087
            }
1088
        } else {
1089
            if ($value == $testedValue) {
1090
                return "checked='checked'";
1091
            }
1092
        }
1093
        return "";
1094
    }
1095
1096
    private function isSelected ($key, $value) {
1097
        if (getCurrentOption ($key) == $value) {
1098
            return "selected='selected'";
1099
        }
1100
        return "";
1101
    }
1102
1103
    private function getStyleList () {
1104
        $result = array ();
1105
        foreach (glob ("templates/" . getCurrentTemplate () . "/styles/style-*.css") as $filename) {
1106
            if (preg_match ('/styles\/style-(.*?)\.css/', $filename, $m)) {
1107
                array_push ($result, $m [1]);
1108
            }
1109
        }
1110
        return $result;
1111
    }
1112
1113
    public function InitializeContent ()
1114
    {
1115
        $this->title = localize ("customize.title");
1116
        $this->entryArray = array ();
1117
1118
        $ignoredBaseArray = array (PageQueryResult::SCOPE_AUTHOR,
1119
                                   PageQueryResult::SCOPE_TAG,
1120
                                   PageQueryResult::SCOPE_SERIES,
1121
                                   PageQueryResult::SCOPE_PUBLISHER,
1122
                                   PageQueryResult::SCOPE_RATING,
1123
                                   "language");
1124
1125
        $content = "";
1126
        array_push ($this->entryArray, new Entry ("Template", "",
1127
                                        "<span style='cursor: pointer;' onclick='$.cookie(\"template\", \"bootstrap\", { expires: 365 });window.location=$(\".headleft\").attr(\"href\");'>Click to switch to Bootstrap</span>", "text",
1128
                                        array ()));
1129
        if (!preg_match("/(Kobo|Kindle\/3.0|EBRD1101)/", $_SERVER['HTTP_USER_AGENT'])) {
1130
            $content .= '<select id="style" onchange="updateCookie (this);">';
1131
            foreach ($this-> getStyleList () as $filename) {
1132
                $content .= "<option value='{$filename}' " . $this->isSelected ("style", $filename) . ">{$filename}</option>";
1133
            }
1134
            $content .= '</select>';
1135
        } else {
1136
            foreach ($this-> getStyleList () as $filename) {
1137
                $content .= "<input type='radio' onchange='updateCookieFromCheckbox (this);' id='style-{$filename}' name='style' value='{$filename}' " . $this->isChecked ("style", $filename) . " /><label for='style-{$filename}'> {$filename} </label>";
1138
            }
1139
        }
1140
        array_push ($this->entryArray, new Entry (localize ("customize.style"), "",
1141
                                        $content, "text",
1142
                                        array ()));
1143
        if (!useServerSideRendering ()) {
1144
            $content = '<input type="checkbox" onchange="updateCookieFromCheckbox (this);" id="use_fancyapps" ' . $this->isChecked ("use_fancyapps") . ' />';
1145
            array_push ($this->entryArray, new Entry (localize ("customize.fancybox"), "",
1146
                                            $content, "text",
1147
                                            array ()));
1148
        }
1149
        $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]+$" />';
1150
        array_push ($this->entryArray, new Entry (localize ("customize.paging"), "",
1151
                                        $content, "text",
1152
                                        array ()));
1153
        $content = '<input type="text" onchange="updateCookie (this);" id="email" value="' . getCurrentOption ("email") . '" />';
1154
        array_push ($this->entryArray, new Entry (localize ("customize.email"), "",
1155
                                        $content, "text",
1156
                                        array ()));
1157
        $content = '<input type="checkbox" onchange="updateCookieFromCheckbox (this);" id="html_tag_filter" ' . $this->isChecked ("html_tag_filter") . ' />';
1158
        array_push ($this->entryArray, new Entry (localize ("customize.filter"), "",
1159
                                        $content, "text",
1160
                                        array ()));
1161
        $content = "";
1162
        foreach ($ignoredBaseArray as $key) {
1163
            $keyPlural = preg_replace ('/(ss)$/', 's', $key . "s");
1164
            $content .=  '<input type="checkbox" name="ignored_categories[]" onchange="updateCookieFromCheckboxGroup (this);" id="ignored_categories_' . $key . '" ' . $this->isChecked ("ignored_categories", $key) . ' > ' . localize ("{$keyPlural}.title") . '</input> ';
1165
        }
1166
1167
        array_push ($this->entryArray, new Entry (localize ("customize.ignored"), "",
1168
                                        $content, "text",
1169
                                        array ()));
1170
    }
1171
}
1172
1173
1174
abstract class Base
1175
{
1176
    const PAGE_INDEX = "index";
1177
    const PAGE_ALL_AUTHORS = "1";
1178
    const PAGE_AUTHORS_FIRST_LETTER = "2";
1179
    const PAGE_AUTHOR_DETAIL = "3";
1180
    const PAGE_ALL_BOOKS = "4";
1181
    const PAGE_ALL_BOOKS_LETTER = "5";
1182
    const PAGE_ALL_SERIES = "6";
1183
    const PAGE_SERIE_DETAIL = "7";
1184
    const PAGE_OPENSEARCH = "8";
1185
    const PAGE_OPENSEARCH_QUERY = "9";
1186
    const PAGE_ALL_RECENT_BOOKS = "10";
1187
    const PAGE_ALL_TAGS = "11";
1188
    const PAGE_TAG_DETAIL = "12";
1189
    const PAGE_BOOK_DETAIL = "13";
1190
    const PAGE_ALL_CUSTOMS = "14";
1191
    const PAGE_CUSTOM_DETAIL = "15";
1192
    const PAGE_ABOUT = "16";
1193
    const PAGE_ALL_LANGUAGES = "17";
1194
    const PAGE_LANGUAGE_DETAIL = "18";
1195
    const PAGE_CUSTOMIZE = "19";
1196
    const PAGE_ALL_PUBLISHERS = "20";
1197
    const PAGE_PUBLISHER_DETAIL = "21";
1198
    const PAGE_ALL_RATINGS = "22";
1199
    const PAGE_RATING_DETAIL = "23";
1200
1201
    const COMPATIBILITY_XML_ALDIKO = "aldiko";
1202
    
1203
    const SQL_SETTING = 'SELECT val FROM preferences WHERE key = "{0}"';
1204
1205
    private static $db = NULL;
1206
1207 108
    public static function isDatabaseArray () {
1208 108
        global $config;
1209 108
        return is_array ($config['calibre_directory']);
1210
    }
1211
    
1212 46
    public static function isMultipleDatabaseEnabled () {
1213 46
    	return (self::isDatabaseArray () || VirtualLib::isVLEnabled());
1214
    }
1215
1216 46
    public static function useAbsolutePath () {
1217 46
        global $config;
1218 46
        $path = self::getDbDirectory();
1219 46
        return preg_match ('/^\//', $path) || // Linux /
1220 46
               preg_match ('/^\w\:/', $path); // Windows X:
1221
    }
1222
1223 45
    public static function noDatabaseSelected () {
1224 45
        return self::isMultipleDatabaseEnabled() && is_null (GetUrlParam (DB));
1225
    }
1226
1227 4
    public static function getDbList () {
1228 4
        global $config;
1229 4
        if (self::isDatabaseArray ()) {
1230 4
            return $config['calibre_directory'];
1231
        } else {
1232 1
            return array ("" => $config['calibre_directory']);
1233
        }
1234
    }
1235
1236 5
    public static function getDbNameList () {
1237 5
        global $config;
1238 5
        if (self::isDatabaseArray ()) {
1239 5
            return array_keys ($config['calibre_directory']);
1240
        } else {
1241
            return array ("");
1242
        }
1243
    }
1244
1245 1
    public static function getDbName ($database = NULL) {
1246 1
        global $config;
1247 1
        if (self::isDatabaseArray ()) {
1248 1
            if (is_null ($database)) $database = GetUrlParam (DB, 0);
1249 1
            if (!is_null($database) && !preg_match('/^\d+$/', $database)) {
1250
                return self::error ($database);
1251
            }
1252 1
            $array = array_keys ($config['calibre_directory']);
1253 1
            return  $array[$database];
1254
        }
1255
        return "";
1256
    }
1257
1258 90
    public static function getDbDirectory ($database = NULL) {
1259 90
        global $config;
1260 90
        if (self::isDatabaseArray ()) {
1261 9
            if (is_null ($database)) $database = GetUrlParam (DB, 0);
1262 9
            if (!is_null($database) && !preg_match('/^\d+$/', $database)) {
1263
                return self::error ($database);
1264
            }
1265 9
            $array = array_values ($config['calibre_directory']);
1266 9
            return  $array[$database];
1267
        }
1268 81
        return $config['calibre_directory'];
1269
    }
1270
1271
1272 61
    public static function getDbFileName ($database = NULL) {
1273 61
        return self::getDbDirectory ($database) .'metadata.db';
1274
    }
1275
1276 2
    private static function error ($database) {
1277 2
        if (php_sapi_name() != "cli") {
1278
            header("location: checkconfig.php?err=1");
1279
        }
1280 2
        throw new Exception("Database <{$database}> not found.");
1281
    }
1282
1283 126
    public static function getDb ($database = NULL) {
1284 126
        if (is_null (self::$db)) {
1285
            try {
1286 61
                if (is_readable (self::getDbFileName ($database))) {
1287 60
                    self::$db = new PDO('sqlite:'. self::getDbFileName ($database));
1288 60
                    if (useNormAndUp ()) {
1289 7
                        self::$db->sqliteCreateFunction ('normAndUp', 'normAndUp', 1);
1290 7
                    }
1291 60
                } else {
1292 2
                    self::error ($database);
1293
                }
1294 61
            } catch (Exception $e) {
1295 2
                self::error ($database);
1296
            }
1297 60
        }
1298 125
        return self::$db;
1299
    }
1300
1301 4
    public static function checkDatabaseAvailability () {
1302 4
        if (self::noDatabaseSelected ()) {
1303 3
            for ($i = 0; $i < count (self::getDbList ()); $i++) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
1304 3
                self::getDb ($i);
1305 2
                self::clearDb ();
1306 2
            }
1307 1
        } else {
1308 1
            self::getDb ();
1309
        }
1310 2
        return true;
1311
    }
1312
1313 58
    public static function clearDb () {
1314 58
        self::$db = NULL;
1315 58
    }
1316
1317 35
    public static function executeQuerySingle ($query, $database = NULL) {
1318 35
        return self::getDb ($database)->query($query)->fetchColumn();
1319
    }
1320
1321 8
    public static function getCountGeneric($table, $id, $pageId, $numberOfString = NULL) {
1322 8
        if (!$numberOfString) {
1323 7
            $numberOfString = $table . ".alphabetical";
1324 7
        }
1325 8
        $count = self::executeQuerySingle ('select count(*) from ' . $table);
1326 8
        if ($count == 0) return NULL;
1327 8
        $entry = new Entry (localize($table . ".title"), $id,
1328 8
            str_format (localize($numberOfString, $count), $count), "text",
1329 8
            array ( new LinkNavigation ("?page=".$pageId)), "", $count);
1330 8
        return $entry;
1331
    }
1332
1333 35
    public static function getEntryArrayWithBookNumber ($query, $columns, $params, $category) {
1334 35
        list (, $result) = self::executeQuery ($query, $columns, "", $params, -1);
1335 35
        $entryArray = array();
1336 35
        while ($post = $result->fetchObject ())
1337
        {
1338 25
            $instance = new $category ($post);
1339 25
            if (property_exists($post, "sort")) {
1340 17
                $title = $post->sort;
1341 17
            } else {
1342 8
                $title = $post->name;
1343
            }
1344 25
            array_push ($entryArray, new Entry ($title, $instance->getEntryId (),
1345 25
                str_format (localize("bookword", $post->count), $post->count), "text",
1346 25
                array ( new LinkNavigation ($instance->getUri ())), "", $post->count));
1347 25
        }
1348 35
        return $entryArray;
1349
    }
1350
1351 73
    public static function executeQuery($query, $columns, $filter, $params, $n, $database = NULL, $numberPerPage = NULL) {
1352 73
        $totalResult = -1;
1353
1354 73
        if (useNormAndUp ()) {
1355 7
            $query = preg_replace("/upper/", "normAndUp", $query);
1356 7
            $columns = preg_replace("/upper/", "normAndUp", $columns);
1357 7
        }
1358
1359 73
        if (is_null ($numberPerPage)) {
1360 71
            $numberPerPage = getCurrentOption ("max_item_per_page");
1361 71
        }
1362
1363 73
        if ($numberPerPage != -1 && $n != -1)
1364 73
        {
1365
            // First check total number of results
1366 28
            $result = self::getDb ($database)->prepare (str_format ($query, "count(*)", $filter));
1367 28
            $result->execute ($params);
1368 28
            $totalResult = $result->fetchColumn ();
1369
1370
            // Next modify the query and params
1371 28
            $query .= " limit ?, ?";
1372 28
            array_push ($params, ($n - 1) * $numberPerPage, $numberPerPage);
1373 28
        }
1374
1375 73
        $result = self::getDb ($database)->prepare(str_format ($query, $columns, $filter));
1376 73
        $result->execute ($params);
1377 73
        return array ($totalResult, $result);
1378
    }
1379
1380
    /**
1381
     * Gets a calibre setting from the database.
1382
     * 
1383
     * @param string $name Name of the property
1384
     * @param int $database Database to query from
1385
     * @return string Value of the property
1386
     */
1387 22
    public static function getCalibreSetting($name, $database = NULL) {
1388 22
    	$query = str_format(self::SQL_SETTING, $name);
1389 22
    	return self::executeQuerySingle($query, $database);
1390
    }
1391
}
1392