{$title}. {$location}: {$publisher}, {$pubdate}."; } elseif ($type == 'Music') { return "{$author_sort}, {$title}. {$publisher}, {$pubdate}."; } else { return "{$author_sort}, director. {$title}. {$publisher}, {$pubdate}."; } } /** * Generate APA citation */ function generate_apa_citation($type, $author_sort, $title, $publisher, $pubdate, $creator) { $name_parts = explode(', ', $author_sort); $last_name = $name_parts[0] ?? ''; $first_initial = isset($name_parts[1]) ? mb_substr($name_parts[1], 0, 1) : ''; if ($type == 'Text') { return "{$last_name}, {$first_initial}. ({$pubdate}). {$title}. {$publisher}."; } elseif ($type == 'Music') { return "{$creator}. ({$pubdate}). {$title} [Album]. {$publisher}."; } else { return "{$last_name}, {$first_initial}. (Director). ({$pubdate}). {$title} [Film]. {$publisher}."; } } /** * Generate MLA citation */ function generate_mla_citation($type, $author_sort, $title, $publisher, $pubdate) { if ($type == 'Text' || $type == 'Music') { return "{$author_sort}. {$title}. {$publisher}, {$pubdate}."; } else { return "{$author_sort}, director. {$title}. {$publisher}, {$pubdate}."; } } // Initialize variables $ItemID = null; $db = null; $error_message = null; // Data arrays to store results $book_data = []; $identifiers = []; $languages = []; $tags = []; $similar_items = []; try { // Validate and sanitize ItemID - CRITICAL for SQL injection prevention if (!isset($_GET["itemid"]) || !is_numeric($_GET["itemid"])) { throw new Exception('Invalid item ID'); } $ItemID = (int)$_GET["itemid"]; if ($ItemID <= 0) { throw new Exception('Invalid item ID'); } // Check if database exists if (!file_exists('metadata.sqlite')) { throw new Exception('Database file not found'); } // Establish database connection $db = new SQLite3('metadata.sqlite'); if (!$db) { throw new Exception('Unable to open database'); } $db->busyTimeout(5000); // Pull data from books table - Using parameterized query for safety $stmt = $db->prepare(" SELECT id, title, date(timestamp) as created, author_sort, strftime('%Y',pubdate) AS pubyear, date(last_modified) as modified FROM books WHERE id = :itemid "); $stmt->bindValue(':itemid', $ItemID, SQLITE3_INTEGER); $result = $stmt->execute(); if ($result && $row = $result->fetchArray(SQLITE3_ASSOC)) { $book_data = $row; } else { throw new Exception('Item not found'); } // Pull author data $stmt = $db->prepare(" SELECT name FROM authors INNER JOIN books_authors_link ON books_authors_link.author = authors.id WHERE books_authors_link.book = :itemid "); $stmt->bindValue(':itemid', $ItemID, SQLITE3_INTEGER); $result = $stmt->execute(); if ($result && $row = $result->fetchArray(SQLITE3_ASSOC)) { $book_data['creator'] = $row['name']; } // Pull summary/comments $stmt = $db->prepare("SELECT text FROM comments WHERE book = :itemid"); $stmt->bindValue(':itemid', $ItemID, SQLITE3_INTEGER); $result = $stmt->execute(); if ($result && $row = $result->fetchArray(SQLITE3_ASSOC)) { $book_data['summary'] = $row['text']; } // Pull publisher $stmt = $db->prepare(" SELECT name FROM publishers INNER JOIN books_publishers_link ON books_publishers_link.publisher = publishers.id WHERE books_publishers_link.book = :itemid "); $stmt->bindValue(':itemid', $ItemID, SQLITE3_INTEGER); $result = $stmt->execute(); if ($result && $row = $result->fetchArray(SQLITE3_ASSOC)) { $book_data['publisher'] = $row['name']; } // Pull tags (excluding infopump internal tags) $stmt = $db->prepare(" SELECT name FROM tags INNER JOIN books_tags_link ON books_tags_link.tag = tags.id WHERE tags.name NOT LIKE 'infopump%' AND books_tags_link.book = :itemid "); $stmt->bindValue(':itemid', $ItemID, SQLITE3_INTEGER); $result = $stmt->execute(); while ($result && $row = $result->fetchArray(SQLITE3_ASSOC)) { $tags[] = $row['name']; } // Pull identifiers $stmt = $db->prepare("SELECT type, val FROM identifiers WHERE book = :itemid"); $stmt->bindValue(':itemid', $ItemID, SQLITE3_INTEGER); $result = $stmt->execute(); while ($result && $row = $result->fetchArray(SQLITE3_ASSOC)) { $identifiers[] = $row; } // Pull languages $stmt = $db->prepare(" SELECT languages.lang_code AS lang_code FROM languages INNER JOIN books_languages_link ON books_languages_link.lang_code = languages.id WHERE book = :itemid ORDER BY books_languages_link.item_order "); $stmt->bindValue(':itemid', $ItemID, SQLITE3_INTEGER); $result = $stmt->execute(); while ($result && $row = $result->fetchArray(SQLITE3_ASSOC)) { $languages[] = $row['lang_code']; } // Pull type $stmt = $db->prepare(" SELECT custom_column_1.value AS itemtype FROM books_custom_column_1_link INNER JOIN custom_column_1 ON custom_column_1.id = books_custom_column_1_link.value WHERE books_custom_column_1_link.book = :itemid "); $stmt->bindValue(':itemid', $ItemID, SQLITE3_INTEGER); $result = $stmt->execute(); if ($result && $row = $result->fetchArray(SQLITE3_ASSOC)) { $book_data['type'] = mb_convert_case($row['itemtype'], MB_CASE_TITLE, "UTF-8"); } // Pull series information $stmt = $db->prepare(" SELECT series.name AS series, books.series_index AS seriesindex FROM series INNER JOIN books_series_link ON books_series_link.series = series.id INNER JOIN books ON books.id = books_series_link.book WHERE books_series_link.book = :itemid "); $stmt->bindValue(':itemid', $ItemID, SQLITE3_INTEGER); $result = $stmt->execute(); if ($result && $row = $result->fetchArray(SQLITE3_ASSOC)) { $book_data['series'] = $row['series']; $book_data['series_index'] = $row['seriesindex']; } // Pull publisher location $stmt = $db->prepare(" SELECT custom_column_2.value AS publoc FROM books INNER JOIN books_custom_column_2_link ON books_custom_column_2_link.book = books.id INNER JOIN custom_column_2 ON custom_column_2.id = books_custom_column_2_link.value WHERE books.id = :itemid "); $stmt->bindValue(':itemid', $ItemID, SQLITE3_INTEGER); $result = $stmt->execute(); if ($result && $row = $result->fetchArray(SQLITE3_ASSOC)) { $book_data['publisher_location'] = $row['publoc']; } // Pull subtype $stmt = $db->prepare(" SELECT custom_column_3.value AS subtype FROM books INNER JOIN books_custom_column_3_link ON books_custom_column_3_link.book = books.id INNER JOIN custom_column_3 ON custom_column_3.id = books_custom_column_3_link.value WHERE books.id = :itemid "); $stmt->bindValue(':itemid', $ItemID, SQLITE3_INTEGER); $result = $stmt->execute(); if ($result && $row = $result->fetchArray(SQLITE3_ASSOC)) { $book_data['subtype'] = $row['subtype']; } // Get similar items based on tags if (count($tags) >= 2) { // Get two random tags for similarity $random_tags = array_rand(array_flip($tags), min(2, count($tags))); if (!is_array($random_tags)) { $random_tags = [$random_tags]; } $tag_placeholders = implode(',', array_fill(0, count($random_tags), '?')); $stmt = $db->prepare(" SELECT DISTINCT books.id, title, author_sort FROM books INNER JOIN books_tags_link ON books_tags_link.book = books.id INNER JOIN tags ON tags.id = books_tags_link.tag WHERE tags.name IN ($tag_placeholders) AND books.id != :itemid LIMIT 4 "); foreach ($random_tags as $index => $tag) { $stmt->bindValue($index + 1, $tag, SQLITE3_TEXT); } $stmt->bindValue(':itemid', $ItemID, SQLITE3_INTEGER); $result = $stmt->execute(); while ($result && $row = $result->fetchArray(SQLITE3_ASSOC)) { $similar_items[] = $row; } } } catch (Exception $e) { error_log('Error in itemrecord.php: ' . $e->getMessage()); $error_message = 'Unable to load item details. Please try again later.'; } // Helper function to get identifier URL function get_identifier_url($type) { $urls = [ 'google' => 'https://books.google.com/books?id=', 'isbn' => 'https://www.librarything.com/isbn/', 'oclc' => 'https://worldcat.org/title/', 'tmdb' => 'https://www.themoviedb.org/movie/' ]; return $urls[$type] ?? '#'; } // Check if image exists $image_path = "images/{$ItemID}.jpg"; if (!file_exists($image_path)) { $image_path = "images/placeholder.jpg"; } ?>
Note: You may need to slightly edit citations for final use. Citations are based upon available data. Refer to your format guides for proper bibliographic citations.
Copy Chicago citation