Files
packtpub-browser/index.php
2018-07-03 10:38:29 +02:00

220 lines
8.7 KiB
PHP

<?php
include 'vendor/autoload.php';
set_time_limit(-1);
$db = new SQLite3('data.db');
if (isset($_GET['category']) && strlen($_GET['category'])) {
$stmt = $db->prepare('SELECT b.*, group_concat(a.name, "|") as author_name, s.id as star FROM book as b LEFT JOIN author_book as ab ON b.id = ab.book_id LEFT JOIN author AS a ON ab.author_id = a.id LEFT JOIN star AS s on s.book_id = b.id WHERE category_slug = :category_slug GROUP BY b.id ');
$stmt->bindValue(':category_slug', $_GET['category'], SQLITE3_TEXT);
} elseif (isset($_GET['search']) && isset($_GET['search_title']) && !empty($_GET['search_title'])) {
$stmt = $db->prepare('SELECT b.*, group_concat(a.name, "|") as author_name, s.id as star FROM book as b LEFT JOIN author_book as ab ON b.id = ab.book_id LEFT JOIN author AS a ON ab.author_id = a.id LEFT JOIN star AS s on s.book_id = b.id WHERE title like :search GROUP BY b.id ');
$stmt->bindValue(':search', '%' . $_GET['search_title'] . '%', SQLITE3_TEXT);
} elseif (isset($_GET['author']) && !empty($_GET['author'])) {
$stmt = $db->prepare('SELECT b.*, group_concat(a.name, "|") as author_name, s.id as star FROM book as b LEFT JOIN author_book as ab ON b.id = ab.book_id LEFT JOIN author AS a ON ab.author_id = a.id LEFT JOIN star AS s on s.book_id = b.id WHERE a.name = :author GROUP BY b.id ');
$stmt->bindValue(':author', $_GET['author'], SQLITE3_TEXT);
} elseif (isset($_GET['book']) && !empty($_GET['book'])) {
$stmt = $db->prepare('SELECT * FROM chapter WHERE book_id = :book');
$stmt->bindValue(':book', $_GET['book'], SQLITE3_INTEGER);
$result = $stmt->execute();
$chaptersArray = [];
while ($chapter = $result->fetchArray(SQLITE3_ASSOC)) {
if (!$chapter['parent_id']) {
$chaptersArray[$chapter['id']] = ['name' => $chapter['name'], 'order' => $chapter['chapter_number'], 'subchapter' => []];
} else {
$chaptersArray[$chapter['parent_id']]['subchapter'][$chapter['chapter_number']] = ['id' => $chapter['id'], 'name' => $chapter['name']];
}
}
$stmt = $db->prepare('SELECT b.*, group_concat(a.name, "|") as author_name, s.id as star FROM book as b LEFT JOIN author_book as ab ON b.id = ab.book_id LEFT JOIN author AS a ON ab.author_id = a.id LEFT JOIN star AS s on s.book_id = b.id WHERE b.id = :book GROUP BY b.id ');
$stmt->bindValue(':book', $_GET['book'], SQLITE3_INTEGER);
} elseif (isset($_GET['star']) && !empty($_GET['star'])) {
$stmt = $db->prepare('SELECT * FROM star WHERE book_id = :book');
$stmt->bindValue(':book', $_GET['star'], SQLITE3_INTEGER);
$result = $stmt->execute();
if ($result->fetchArray(SQLITE3_ASSOC) === false) {
$insertStmt = $db->prepare('INSERT INTO star (book_id, user_id) VALUES (:book_id, :user_id) ');
$insertStmt->bindValue(':book_id', $_GET['star'], SQLITE3_INTEGER);
$insertStmt->bindValue(':user_id', 1, SQLITE3_INTEGER);
$insertStmt->execute();
} else {
$stmt = $db->prepare('DELETE FROM star WHERE book_id = :book');
$stmt->bindValue(':book', $_GET['star'], SQLITE3_INTEGER);
$result = $stmt->execute();
}
if (isset($_SERVER['HTTP_REFERER'])) {
header('Location: ' . $_SERVER['HTTP_REFERER']);
} else {
header('Location: /');
}
} else {
$stmt = $db->prepare('SELECT b.*, group_concat(a.name, "|") as author_name, s.id as star FROM book as b LEFT JOIN author_book as ab ON b.id = ab.book_id LEFT JOIN author AS a ON ab.author_id = a.id LEFT JOIN star AS s on s.book_id = b.id GROUP BY b.id');
}
$result = $stmt->execute();
?>
<style>
html, body, div, span, applet, object, iframe,
h1, h2, h3, h4, h5, h6, p, blockquote, pre,
a, abbr, acronym, address, big, cite, code,
del, dfn, em, img, ins, kbd, q, s, samp,
small, strike, strong, sub, sup, tt, var,
b, u, i, center,
dl, dt, dd, ol, ul, li,
fieldset, form, label, legend,
table, caption, tbody, tfoot, thead, tr, th, td,
article, aside, canvas, details, embed,
figure, figcaption, footer, header, hgroup,
menu, nav, output, ruby, section, summary,
time, mark, audio, video {
margin: 0;
padding: 0;
border: 0;
font-size: 100%;
font: inherit;
vertical-align: baseline;
}
/* HTML5 display-role reset for older browsers */
article, aside, details, figcaption, figure,
footer, header, hgroup, menu, nav, section {
display: block;
}
body {
line-height: 1;
}
ol, ul {
list-style: none;
}
blockquote, q {
quotes: none;
}
blockquote:before, blockquote:after,
q:before, q:after {
content: '';
content: none;
}
table {
border-collapse: collapse;
border-spacing: 0;
}
a {
text-decoration: none;
color: #0070de;
}
a:hover {
color: #58abfc;
}
tbody tr:nth-child(2n) {
background: rgba(0, 0, 0, 0.02);
}
tbody tr:hover {
background: rgba(0, 0, 0, 0.04);
}
body {
font-family: arial;
font-size: 0.9em;
line-height: 16px;
}
td {
padding: 2px 15px;
}
form {
margin: 15px 50px;
}
form a {
margin: 0 10px;
}
input[type="text"] {
padding: 5px 15px;
}
button {
padding: 4px 15px;
}
.star {
color: #e4e4e4;
}
.star.on, .star:hover {
color: gold;
text-shadow: 1px 1px 1px #000;
}
.star.off {
}
</style>
<?php
echo "<form method='get'><input type='text' name='search_title' value='" . ($_GET['search_title'] ?? '') . "' /><button type='submit' name='search'>Search</button><a href='/'>Clear</a></form>";
echo '<table style="width: 100%">';
print('<thead><tr> <th>nid</th><th></th> <th>Title</th> <th>Category</th> <th style="width: 300px;">Author</th> <th>Publish date</th> <th>Pages</th> <th>Votes</th> <th>Rating</th> <th>Pdf</th> <th>Epub</th> <th>Mobi</th> <th>Code</th> </tr></thead> <tbody>');
while ($resultArray = $result->fetchArray(SQLITE3_ASSOC)) {
$pdfUrl = 'books/' . $resultArray['nid'] . '/' . $resultArray['nid'] . '.pdf';
$epubUrl = 'books/' . $resultArray['nid'] . '/' . $resultArray['nid'] . '.epub';
$mobiUrl = 'books/' . $resultArray['nid'] . '/' . $resultArray['nid'] . '.mobi';
$codeUrl = '';
if (preg_match('#\d+#', $resultArray['code'], $found)) {
$codeId = $found[0];
$codeUrl = 'books/' . $codeId . '/' . $codeId . '.zip';
}
$authorsArray = explode('|', $resultArray['author_name']);
$authors = [];
foreach ($authorsArray as $author) {
$authors[] = '<a href="?author=' . $author . '">' . $author . '</a>';
}
printf('<tr> <td><a href="http://packtpub.com%s">%d</a></td> <td><a class="star %s" href="?star=%d">%s</a></td> <td><a href="?book=%d">%s</a></td> <td><a href="?category=%s">%s</a></td> <td>%s</td> <td>%s</td> <td>%s</td> <td>%d</td> <td>%.1f</td> <td>%s</td> <td>%s</td><td>%s</td><td>%s</td> </tr>',
$resultArray['url'], $resultArray['nid'], $resultArray['star'] ? 'on' : 'off', $resultArray['id'], $resultArray['star'] ? '&#9733' : '&#9734', $resultArray['id'], $resultArray['title'], $resultArray['category_slug'], $resultArray['category'], implode(', ', $authors), $resultArray['datepublished'], $resultArray['numberofpages'], $resultArray['reviewCount'], $resultArray['ratingValue'],
is_file($pdfUrl) ? "<a href='{$pdfUrl}'>pdf</a>" : (strlen($resultArray['pdf']) ? 'nie pobrano' : ''),
is_file($epubUrl) ? "<a href='{$epubUrl}'>epub</a>" : (strlen($resultArray['epub']) ? 'nie pobrano' : ''),
is_file($mobiUrl) ? "<a href='{$mobiUrl}'>mobi</a>" : (strlen($resultArray['mobi']) ? 'nie pobrano' : ''),
is_file($codeUrl) ? "<a href='{$codeUrl}'>zip</a>" : (strlen($resultArray['code']) ? 'nie pobrano' : '')
);
if (isset($chaptersArray) && $chaptersArray != []) {
$table = '<table class="toc">';
foreach ($chaptersArray as $chapter) {
$table .= '<tr class="chapter"><td colspan="2">' . $chapter['name'] . '</td></tr>';
foreach ($chapter['subchapter'] as $subchapter) {
$table .= '<tr class="subchapter"><td></td><td>' . $subchapter['name'] . '</td></tr>';
}
}
$table .= '</table>';
echo $table;
}
// if ($resultArray) {
// $x = $resultArray;
// }
}
echo '</tbody></table>';