From 70c8a87e158feee2c6f7262eae89bfb6fe0c4c9a Mon Sep 17 00:00:00 2001 From: Cody Cook Date: Sun, 19 May 2024 20:01:13 -0700 Subject: [PATCH] Changes. --- classes/DJ.php | 62 ++++++------ classes/DJs.php | 2 +- classes/Database.php | 2 +- classes/DownloadMix.php | 106 +++++++++++++++++++++ classes/Genre.php | 4 +- classes/Genres.php | 2 +- classes/Mix.php | 40 ++++---- classes/Mixshow.php | 4 +- classes/Mixshows.php | 2 +- classes/Playcount.php | 61 ++++++++++++ classes/Schema.php | 2 +- dj.php | 42 +++++---- djs.php | 54 +++++------ download.php | 17 ++++ genre.php | 8 +- genres.php | 4 +- includes/globals.php | 9 ++ includes/header-security.php | 2 + includes/header.php | 18 +++- includes/navbar.php | 17 ++-- locale/en-US/messages.php | 4 + mix.php | 176 +++++++++++++++++++++++++++-------- mixshow.php | 9 +- mixshows.php | 8 +- tests/DJTest.php | 40 ++++---- 25 files changed, 508 insertions(+), 187 deletions(-) create mode 100644 classes/DownloadMix.php create mode 100644 classes/Playcount.php create mode 100644 download.php create mode 100644 includes/header-security.php diff --git a/classes/DJ.php b/classes/DJ.php index 4c946d9..baefa4f 100644 --- a/classes/DJ.php +++ b/classes/DJ.php @@ -9,7 +9,7 @@ class DJ private string $name = ""; private string $bio = ""; private string $slug = ""; - private string $img = "img/no-image1.png"; + private string $img = "/img/no-image1.png"; private bool $active = false; private bool $claimed = false; private string $email = ""; @@ -17,7 +17,7 @@ class DJ private string $created = ""; private string $updated = ""; - private string $claimed_by = ""; + private string $claimedBy = ""; private $db; private array $mixes = []; @@ -27,22 +27,21 @@ class DJ $this->db = $db; if (ctype_digit((string)$value)) { $this->id = (int)$value; - return $this->load_from_id(); + return $this->loadByID(); } else { $this->slug = $value; - return $this->load_from_slug(); + return $this->loadBySlug(); } } - private function load_from_id(): bool + private function loadByID(): bool { - $socials = []; - $dj = $this->get_dj_by_id($this->id); - return $this->build_dj($dj); + $dj = $this->getDJbyID($this->id); + return $this->buildDJ($dj); } - private function get_dj_by_id() + private function getDJbyID() { $stmt = $this->db->prepare("SELECT * FROM djs WHERE id = ?"); $stmt->bind_param("i", $this->id); @@ -57,7 +56,7 @@ class DJ * @param $dj * @return bool */ - private function build_dj($dj): bool + private function buildDJ($dj): bool { if ($dj) { if (isset($dj['id'])) { @@ -130,7 +129,7 @@ class DJ $this->updated = $dj['lastupdated']; } - $this->load_dj_mixes(); + $this->loadDJMixes(); return true; } else { @@ -138,7 +137,7 @@ class DJ } } - private function load_dj_mixes(): void + private function loadDJMixes(): void { $stmt = $this->db->prepare("SELECT * FROM mix WHERE dj1 = ? OR dj2 = ? OR dj3 = ?"); $stmt->bind_param("iii", $this->id, $this->id, $this->id); @@ -154,14 +153,14 @@ class DJ } - private function load_from_slug(): bool + private function loadBySlug(): bool { $socials = []; - $dj = $this->get_dj_by_slug($this->slug); - return $this->build_dj($dj); + $dj = $this->getDJbySlug($this->slug); + return $this->buildDJ($dj); } - private function get_dj_by_slug($slug) + private function getDJbySlug($slug) { $stmt = $this->db->prepare("SELECT * FROM djs WHERE slug = ?"); $stmt->bind_param("s", $slug); @@ -172,63 +171,70 @@ class DJ return $dj; } - public function get_slug(): string + public function getSlug(): string { return $this->slug; } - public function get_id(): int + public function getID(): int { return $this->id; } - public function get_name(): string + public function getName(): string { return $this->name; } - public function get_bio(): string + public function getBio(): string { return $this->bio; } - public function get_img(): string + public function getImg(): string { return $this->img; } - public function get_active(): bool + public function getActive(): bool { return $this->active; } - public function get_email(): string + public function getEmail(): string { return $this->email; } - public function get_socials(): array + public function getSocials(): array { return $this->socials; } - public function get_created(): string + public function getCreated(): string { return $this->created; } - public function get_updated(): string + public function getUpdated(): string { return $this->updated; } - public function get_claimed(): bool + public function getClaimed(): bool { return $this->claimed; } - public function get_dj_mixes() + public function getDJMixes() { return $this->mixes; } + + public function getClaimedBy() + { + return $this->claimedBy; + } + + } diff --git a/classes/DJs.php b/classes/DJs.php index f5aabe9..ad8a677 100644 --- a/classes/DJs.php +++ b/classes/DJs.php @@ -49,4 +49,4 @@ class DJs $stmt->close(); return $djs; } -} \ No newline at end of file +} diff --git a/classes/Database.php b/classes/Database.php index e41b9fe..304da94 100644 --- a/classes/Database.php +++ b/classes/Database.php @@ -13,4 +13,4 @@ class Database extends mysqli // call the parent constructor with the config file parent::__construct($config['database']['host'], $config['database']['user'], $config['database']['pass'], $config['database']['db'], $config['database']['port'] ?? 3306); } -} \ No newline at end of file +} diff --git a/classes/DownloadMix.php b/classes/DownloadMix.php new file mode 100644 index 0000000..894e70e --- /dev/null +++ b/classes/DownloadMix.php @@ -0,0 +1,106 @@ +db = $db; + $this->mix = $mix; + $this->mix_id = $mix->get_id(); + $this->preDownload(); + } + + private function preDownload() + { + $this->name = $this->mix->get_name(); + $buildDJs = $this->mix->get_djs(); + $this->url = $this->mix->get_url(); + $this->djs = ''; + $djCount = 0; + foreach ($buildDJs as $dj) { + if ($djCount > 0) { + $this->djs .= ', '; + } + $this->djs .= $dj->getName(); + $djCount++; + } + + } + + public function download() + { + $this->loadDownload(); + if (!$this->ready) { + echo "I had a problem downloading the file."; + return; + } else { + if ($this->checkForMixDownloadCount()) { + $this->incrementMixDownloadCount(); + } else { + $this->addMixDownloadCount(); + } + header("Content-Description: File Transfer"); + header("Content-Type: application/octet-stream"); + header("Content-Disposition: attachment; filename=\"" . $this->filename . "\""); + echo $this->content; + } + } + + private function loadDownload() + { + $this->content = file_get_contents($this->url); + $this->filesize = strlen($this->content); + $this->ext = pathinfo(basename($this->url), PATHINFO_EXTENSION); + $this->filename = $this->djs . ' - ' . $this->name . ' (Downloaded from UtahsDJs.com).' . pathinfo(basename($this->url), PATHINFO_EXTENSION); + if ($this->filesize > 0) { + $this->ready = true; + } + } + + private function checkForMixDownloadCount() + { + $stmt = $this->db->prepare("SELECT * FROM mix_meta WHERE attribute = 'downloads' and mix_id = ?"); + $stmt->bind_param('i', $this->mix_id); + $stmt->execute(); + $result = $stmt->get_result(); + $row = $result->fetch_assoc(); + $stmt->close(); + if ($row) { + return true; + } else { + return false; + } + } + + private function incrementMixDownloadCount() + { + $stmt = $this->db->prepare("UPDATE mix_meta SET value = value + 1 WHERE attribute = 'downloads' and mix_id = ?"); + $stmt->bind_param('i', $this->mix_id); + $stmt->execute(); + $stmt->close(); + } + + private function addMixDownloadCount() + { + $stmt = $this->db->prepare("INSERT INTO mix_meta (mix_id, attribute, value) VALUES (?, 'downloads', 1)"); + $stmt->bind_param('i', $this->mix_id); + $stmt->execute(); + $stmt->close(); + } + +} \ No newline at end of file diff --git a/classes/Genre.php b/classes/Genre.php index 5b6a28b..418af91 100644 --- a/classes/Genre.php +++ b/classes/Genre.php @@ -96,7 +96,7 @@ class Genre public function get_img(): string { - return "img/no-image1.png"; + return "/img/no-image1.png"; } public function get_count(): int @@ -123,4 +123,4 @@ class Genre return $mixes; } -} \ No newline at end of file +} diff --git a/classes/Genres.php b/classes/Genres.php index 620a072..43c274d 100644 --- a/classes/Genres.php +++ b/classes/Genres.php @@ -52,4 +52,4 @@ class Genres $stmt->close(); return $genres; } -} \ No newline at end of file +} diff --git a/classes/Mix.php b/classes/Mix.php index 1c4c028..e17012c 100644 --- a/classes/Mix.php +++ b/classes/Mix.php @@ -76,19 +76,8 @@ class Mix $this->name = $mix['title']; $this->slug = $mix['slug']; $this->description = $mix['description']; - if (isset($mix['cover'])) { - - // is this legacy code? - // the code is legacy if it starts with /dj/, - // if it does, prefix with https://www.utahsdjs.com - if (substr($mix['cover'], 0, 5) == "/djs/") { - $mix['cover'] = substr($mix['cover'], 4); - $this->cover = "https://cdn.utahsdjs.com" . $mix['cover']; - } else { - $this->cover = $mix['cover']; - } - } - $this->url = $mix['url']; + $this->cover = $this->legacyFix($mix['cover']); + $this->url = $this->legacyFix($mix['url']); $this->seconds = $mix['seconds']; $this->duration = $this->configure_duration(); $this->download_only = $mix['mediaplayer']; @@ -114,6 +103,15 @@ class Mix return true; } + private function legacyFix(mixed $item) + { + if (str_starts_with($item, "/djs/")) { + return "https://cdn.utahsdjs.com" . substr($item, 4); + } else { + return $item; + } + } + private function configure_duration(): array { $seconds = $this->seconds; @@ -201,14 +199,18 @@ class Mix private function load_by_slug(): bool { $mix = $this->get_mix_by_slug(); - - if ($mix['title'] != "") { - return $this->build_mix($mix); + if ($mix) { + if ($mix['title'] != "") { + return $this->build_mix($mix); + } else { + return false; + } } else { return false; } } + private function get_mix_by_slug() { $stmt = $this->db->prepare("SELECT * FROM mix WHERE slug = ?"); @@ -235,7 +237,7 @@ class Mix return $this->updated; } - public function get_img(): string + public function get_img() { return $this->cover; } @@ -275,7 +277,7 @@ class Mix return $this->url; } - public function get_cover(): string + public function get_cover() { return $this->cover; } @@ -316,5 +318,3 @@ class Mix } } - - diff --git a/classes/Mixshow.php b/classes/Mixshow.php index 0f00d69..96bafd2 100644 --- a/classes/Mixshow.php +++ b/classes/Mixshow.php @@ -11,7 +11,7 @@ class Mixshow private $slug = ""; private $db = null; private $description = ""; - private $cover = "img/no-image1.png"; + private $cover = "/img/no-image1.png"; private $count; private $mixes = []; private $updated; @@ -145,4 +145,4 @@ class Mixshow return $this->updated; } -} \ No newline at end of file +} diff --git a/classes/Mixshows.php b/classes/Mixshows.php index b54bca8..53bfd24 100644 --- a/classes/Mixshows.php +++ b/classes/Mixshows.php @@ -52,4 +52,4 @@ class Mixshows $stmt->close(); return $mixshows; } -} \ No newline at end of file +} diff --git a/classes/Playcount.php b/classes/Playcount.php new file mode 100644 index 0000000..cbfcb0b --- /dev/null +++ b/classes/Playcount.php @@ -0,0 +1,61 @@ +db = $db; + $this->mix_id = $mix; + } + + public function getPlaycount() + { + $sql = "SELECT value FROM mix_meta WHERE mix_id = ? AND attribute = 'playcount'"; + $stmt = $this->db->prepare($sql); + $stmt->execute([$this->mix_id]); + $result = $stmt->get_result(); + $row = $result->fetch_assoc(); + return $row['value']; + } + + public function updatePlaycount(): void + { + if ($this->checkForPlaycount()) { + $this->incrementPlaycount(); + } else { + $this->addPlaycount(); + } + } + + private function checkForPlaycount() + { + $sql = "SELECT meta_id FROM mix_meta WHERE mix_id = ? AND attribute = 'playcount'"; + $stmt = $this->db->prepare($sql); + $stmt->bind_param('i', $this->mix_id); + $stmt->execute(); + $result = $stmt->get_result(); + return $result->fetch_assoc(); + } + + private function incrementPlaycount() + { + $sql = "UPDATE mix_meta SET value = value + 1 WHERE mix_id = ? AND attribute = 'playcount'"; + $stmt = $this->db->prepare($sql); + $stmt->execute([$this->mix_id]); + } + + public function addPlaycount() + { + $sql = "INSERT INTO mix_meta (mix_id, attribute, value) VALUES (?, 'playcount', 1)"; + $stmt = $this->db->prepare($sql); + $stmt->execute([$this->mix_id]); + } + + +} \ No newline at end of file diff --git a/classes/Schema.php b/classes/Schema.php index 82bf0b8..415c15c 100644 --- a/classes/Schema.php +++ b/classes/Schema.php @@ -59,4 +59,4 @@ class Schema */ -} \ No newline at end of file +} diff --git a/dj.php b/dj.php index 256e914..75a9839 100644 --- a/dj.php +++ b/dj.php @@ -2,6 +2,7 @@ require_once 'includes/globals.php'; require_once 'vendor/autoload.php'; + use DJMixHosting\Database; use DJMixHosting\DJ; use DJMixHosting\Genre; @@ -13,11 +14,12 @@ $db = new Database($config); $djFound = false; if (isset($_GET['dj']) && $_GET['dj'] != "") { $dj = new DJ($_GET['dj'], $db); - if ($dj->get_name() != "") { + if ($dj->getName() != "") { $djFound = true; + $title = $dj->getName(); } } -$title = $dj->get_name(); + require_once 'includes/header.php'; ?>
@@ -27,13 +29,15 @@ require_once 'includes/header.php';
+ + \ No newline at end of file diff --git a/download.php b/download.php new file mode 100644 index 0000000..bed4ca9 --- /dev/null +++ b/download.php @@ -0,0 +1,17 @@ +download(); +} else { + header("Location: /"); + exit(); + +} \ No newline at end of file diff --git a/genre.php b/genre.php index 88a49d9..7f75d69 100644 --- a/genre.php +++ b/genre.php @@ -31,7 +31,7 @@ require_once 'includes/header.php';