Test Setup Failed
Push — master ( 9c8496...b157c5 )
by Sébastien
02:29
created

lib/JSON_renderer.php (2 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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 1
require_once dirname(__FILE__) . '/../base.php';
10
11
class JSONRenderer
12
{
13
    /**
14
     * @param Book $book
15
     * @return array
16
     */
17 4
    public static function getBookContentArray ($book) {
18 4
        global $config;
19 4
        $i = 0;
20 4
        $preferedData = array ();
21 4
        foreach ($config['cops_prefered_format'] as $format)
22
        {
23 4
            if ($i == 2) { break; }
24 4
            if ($data = $book->getDataFormat ($format)) {
25 4
                $i++;
26 4
                array_push ($preferedData, array ("url" => $data->getHtmlLink (),
27 4
                  "viewUrl" => $data->getViewHtmlLink (), "name" => $format));
28
            }
29
        }
30
31 4
        $publisher = $book->getPublisher();
32 4
        if (is_null ($publisher)) {
33
            $pn = "";
34
            $pu = "";
35
        } else {
36 4
            $pn = $publisher->name;
37 4
            $link = new LinkNavigation ($publisher->getUri ());
38 4
            $pu = $link->hrefXhtml ();
39
        }
40
41 4
        $serie = $book->getSerie ();
42 4
        if (is_null ($serie)) {
43 2
            $sn = "";
44 2
            $scn = "";
45 2
            $su = "";
46
        } else {
47 2
            $sn = $serie->name;
48 2
            $scn = str_format (localize ("content.series.data"), $book->seriesIndex, $serie->name);
49 2
            $link = new LinkNavigation ($serie->getUri ());
50 2
            $su = $link->hrefXhtml ();
51
        }
52 4
        $cc = $book->getCustomColumnValues($config['cops_calibre_custom_column_list'], true);
53
54 4
        return array ("id" => $book->id,
55 4
                      "hasCover" => $book->hasCover,
56 4
                      "preferedData" => $preferedData,
57 4
                      "rating" => $book->getRating (),
58 4
                      "publisherName" => $pn,
59 4
                      "publisherurl" => $pu,
60 4
                      "pubDate" => $book->getPubDate (),
61 4
                      "languagesName" => $book->getLanguages (),
62 4
                      "authorsName" => $book->getAuthorsName (),
63 4
                      "tagsName" => $book->getTagsName (),
64 4
                      "seriesName" => $sn,
65 4
                      "seriesIndex" => $book->seriesIndex,
66 4
                      "seriesCompleteName" => $scn,
67 4
                      "seriesurl" => $su,
68 4
                      "customcolumns_list" => $cc);
69
    }
70
71
    /**
72
     * @param Book $book
73
     * @return array
74
     */
75 1
    public static function getFullBookContentArray ($book) {
76 1
        global $config;
77 1
        $out = self::getBookContentArray ($book);
78 1
        $database = GetUrlParam (DB);
79
80 1
        $out ["coverurl"] = Data::getLink ($book, "jpg", "image/jpeg", Link::OPDS_IMAGE_TYPE, "cover.jpg", NULL)->hrefXhtml ();
81 1
        $out ["thumbnailurl"] = Data::getLink ($book, "jpg", "image/jpeg", Link::OPDS_THUMBNAIL_TYPE, "cover.jpg", NULL, NULL, $config['cops_html_thumbnail_height'] * 2)->hrefXhtml ();
82 1
        $out ["content"] = $book->getComment (false);
83 1
        $out ["datas"] = array ();
84 1
        $dataKindle = $book->GetMostInterestingDataToSendToKindle ();
85 1
        foreach ($book->getDatas() as $data) {
86 1
            $tab = array ("id" => $data->id,
87 1
                "format" => $data->format, 
88 1
                "url" => $data->getHtmlLink (),
89
                "viewUrl" => $data->getViewHtmlLink (),
90 1
                "mail" => 0, 
91 1
                "readerUrl" => "");
92
            if (!empty ($config['cops_mail_configuration']) && !is_null ($dataKindle) && $data->id == $dataKindle->id) {
93 1
                $tab ["mail"] = 1;
94
            }
95 1
            if ($data->format == "EPUB") {
96 1
                $tab ["readerUrl"] = "epubreader.php?data={$data->id}&db={$database}";
97 1
            }
98 1
            array_push ($out ["datas"], $tab);
99
        }
100 1
        $out ["authors"] = array ();
101 1 View Code Duplication
        foreach ($book->getAuthors () as $author) {
102 1
            $link = new LinkNavigation ($author->getUri ());
103 1
            array_push ($out ["authors"], array ("name" => $author->name, "url" => $link->hrefXhtml ()));
104
        }
105 1
        $out ["tags"] = array ();
106 View Code Duplication
        foreach ($book->getTags () as $tag) {
107 1
            $link = new LinkNavigation ($tag->getUri ());
108
            array_push ($out ["tags"], array ("name" => $tag->name, "url" => $link->hrefXhtml ()));
109
        }
110
        $out ["customcolumns_preview"] = $book->getCustomColumnValues($config['cops_calibre_custom_column_preview'], true);
111
112
        return $out;
113
    }
114
115
    public static function getContentArray ($entry) {
116
        if ($entry instanceof EntryBook) {
117
            $out = array ( "title" => $entry->title);
118
            $out ["book"] = self::getBookContentArray ($entry->book);
119 7
            return $out;
120 7
        }
121 7
        return array ( "title" => $entry->title, "content" => $entry->content, "navlink" => $entry->getNavLink (), "number" => $entry->numberOfElement );
122 7
    }
123 2
124
    public static function getContentArrayTypeahead ($page) {
125 7
        $out = array ();
126 6
        foreach ($page->entryArray as $entry) {
127
            if ($entry instanceof EntryBook) {
128 7
                array_push ($out, array ("class" => $entry->className, "title" => $entry->title, "navlink" => $entry->book->getDetailUrl ()));
129
            } else {
130
                if (empty ($entry->className) xor Base::noDatabaseSelected ()) {
131
                    array_push ($out, array ("class" => $entry->className, "title" => $entry->title, "navlink" => $entry->getNavLink ()));
132 7
                } else {
133
                    array_push ($out, array ("class" => $entry->className, "title" => $entry->content, "navlink" => $entry->getNavLink ()));
134
                }
135 1
            }
136 1
        }
137 1
        return $out;
138
    }
139 1
140 1
    public static function addCompleteArray ($in) {
141 1
        global $config;
142 1
        $out = $in;
143 1
144 1
        $out ["c"] = array ("version" => VERSION, "i18n" => array (
145 1
                           "coverAlt" => localize("i18n.coversection"),
146 1
                           "authorsTitle" => localize("authors.title"),
147 1
                           "bookwordTitle" => localize("bookword.title"),
148 1
                           "tagsTitle" => localize("tags.title"),
149 1
                           "seriesTitle" => localize("series.title"),
150 1
                           "customizeTitle" => localize ("customize.title"),
151 1
                           "aboutTitle" => localize ("about.title"),
152 1
                           "previousAlt" => localize ("paging.previous.alternate"),
153 1
                           "nextAlt" => localize ("paging.next.alternate"),
154 1
                           "searchAlt" => localize ("search.alternate"),
155 1
                           "sortAlt" => localize ("sort.alternate"),
156 1
                           "homeAlt" => localize ("home.alternate"),
157 1
                           "cogAlt" => localize ("cog.alternate"),
158 1
                           "permalinkAlt" => localize ("permalink.alternate"),
159 1
                           "publisherName" => localize("publisher.name"),
160 1
                           "pubdateTitle" => localize("pubdate.title"),
161
                           "languagesTitle" => localize("language.title"),
162 1
                           "contentTitle" => localize("content.summary"),
163 1
                           "filterClearAll" => localize("filter.clearall"),
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal filterClearAll does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
Coding Style Comprehensibility introduced by
The string literal filter.clearall does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
164 1
                           "sortorderAsc" => localize("search.sortorder.asc"),
165
                           "sortorderDesc" => localize("search.sortorder.desc"),
166 1
                           "customizeEmail" => localize("customize.email")),
167 1
                       "url" => array (
168 1
                           "detailUrl" => "index.php?page=13&id={0}&db={1}",
169 1
                           "coverUrl" => "fetch.php?id={0}&db={1}",
170 1
                           "thumbnailUrl" => "fetch.php?height=" . $config['cops_html_thumbnail_height'] . "&id={0}&db={1}"),
171 1
                       "config" => array (
172 1
                           "use_fancyapps" => $config ["cops_use_fancyapps"],
173 1
                           "max_item_per_page" => $config['cops_max_item_per_page'],
174 1
                           "kindleHack"        => "",
175
                           "server_side_rendering" => useServerSideRendering (),
176 1
                           "html_tag_filter" => $config['cops_html_tag_filter']));
177 1
        if ($config['cops_thumbnail_handling'] == "1") {
178
            $out ["c"]["url"]["thumbnailUrl"] = $out ["c"]["url"]["coverUrl"];
179 1
        } else if (!empty ($config['cops_thumbnail_handling'])) {
180
            $out ["c"]["url"]["thumbnailUrl"] = $config['cops_thumbnail_handling'];
181
        }
182 7
        if (preg_match("/./", $_SERVER['HTTP_USER_AGENT'])) {
183 7
          $out ["c"]["config"]["kindleHack"] = 'style="text-decoration: none !important;"';
184 7
        }
185 7
        return $out;
186 7
    }
187 7
188 7
    public static function getJson ($complete = false) {
189 7
        global $config;
190
        $page = getURLParam ("page", Base::PAGE_INDEX);
191 7
        $query = getURLParam ("query");
192 7
        $search = getURLParam ("search");
193
        $qid = getURLParam ("id");
194 7
        $n = getURLParam ("n", "1");
195 7
        $database = GetUrlParam (DB);
196
197
        $currentPage = Page::getPage ($page, $qid, $query, $n);
198
        $currentPage->InitializeContent ();
199
200
        if ($search) {
201
            return self::getContentArrayTypeahead ($currentPage);
202
        }
203
204
        $out = array ( "title" => $currentPage->title);
205
        $entries = array ();
206
        foreach ($currentPage->entryArray as $entry) {
207
            array_push ($entries, self::getContentArray ($entry));
208
        }
209
        if (!is_null ($currentPage->book)) {
210
            $out ["book"] = self::getFullBookContentArray ($currentPage->book);
211
        }
212
        $out ["databaseId"] = GetUrlParam (DB, "");
213
        $out ["databaseName"] = Base::getDbName ();
214
        if ($out ["databaseId"] == "") {
215
            $out ["databaseName"] = "";
216
        }
217
        $out ["fullTitle"] = $out ["title"];
218
        if ($out ["databaseId"] != "" && $out ["databaseName"] != $out ["fullTitle"]) {
219
            $out ["fullTitle"] = $out ["databaseName"] . " > " . $out ["fullTitle"];
220
        }
221
        $out ["page"] = $page;
222
        $out ["multipleDatabase"] = Base::isMultipleDatabaseEnabled () ? 1 : 0;
223
        $out ["entries"] = $entries;
224
        $out ["isPaginated"] = 0;
225
        if ($currentPage->isPaginated ()) {
226
            $prevLink = $currentPage->getPrevLink ();
227
            $nextLink = $currentPage->getNextLink ();
228
            $out ["isPaginated"] = 1;
229
            $out ["prevLink"] = "";
230
            if (!is_null ($prevLink)) {
231
                $out ["prevLink"] = $prevLink->hrefXhtml ();
232
            }
233
            $out ["nextLink"] = "";
234
            if (!is_null ($nextLink)) {
235
                $out ["nextLink"] = $nextLink->hrefXhtml ();
236
            }
237
            $out ["maxPage"] = $currentPage->getMaxPage ();
238
            $out ["currentPage"] = $currentPage->n;
239
        }
240
        if (!is_null (getURLParam ("complete")) || $complete) {
241
            $out = self::addCompleteArray ($out);
242
       }
243
244
        $out ["containsBook"] = 0;
245
        if ($currentPage->containsBook ()) {
246
            $out ["containsBook"] = 1;
247
        }
248
249
        $out["abouturl"] = "index.php" . addURLParameter ("?page=" . Base::PAGE_ABOUT, DB, $database);
250
251
        if ($page == Base::PAGE_ABOUT) {
252
            $temp = preg_replace ("/\<h1\>About COPS\<\/h1\>/", "<h1>About COPS " . VERSION . "</h1>", file_get_contents('about.html'));
253
            $out ["fullhtml"] = $temp;
254
        }
255
256
        $out ["homeurl"] = "index.php";
257
        if ($page != Base::PAGE_INDEX && !is_null ($database)) $out ["homeurl"] = $out ["homeurl"] .  "?" . addURLParameter ("", DB, $database);
258
259
        return $out;
260
    }
261
}