Check-in; adding features like mixshow and google analytics.

This commit is contained in:
Cody Cook 2024-05-02 19:21:24 -07:00
commit 8288ebc67a
50 changed files with 1492 additions and 483 deletions

71
classes/CustomCDN.php Normal file
View file

@ -0,0 +1,71 @@
<?php
class CustomCDN
{
private $space;
private $region;
private $storageType;
private $accessKey;
private $secretKey;
private $public = false;
public function __construct($space, $region, $storageType, $accessKey, $secretKey)
{
$this->space = $space;
$this->region = $region;
$this->storageType = $storageType;
$this->accessKey = $accessKey;
$this->secretKey = $secretKey;
}
public function uploadFile($filePath, $spacePath)
{
$file = basename($filePath);
$date = gmdate('D, d M Y H:i:s T');
if ($this->public) {
$acl = "x-amz-acl:public-read";
} else {
$acl = "x-amz-acl:private";
}
$contentType = $this->getContentType($filePath);
$storageClass = "x-amz-storage-class:{$this->storageType}";
$stringToSign = "PUT\n\n{$contentType}\n{$date}\n{$acl}\n{$storageClass}\n/{$this->space}{$spacePath}{$file}";
$signature = base64_encode(hash_hmac('sha1', $stringToSign, $this->secretKey, true));
$headers = ["Host: {$this->space}.{$this->region}.digitaloceanspaces.com", "Date: {$date}", "Content-Type: {$contentType}", "{$storageClass}", "{$acl}", "Authorization: AWS {$this->accessKey}:{$signature}"];
$ch = curl_init("https://{$this->space}.{$this->region}.digitaloceanspaces.com{$spacePath}{$file}");
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_PUT, true);
curl_setopt($ch, CURLOPT_INFILE, fopen($filePath, 'r'));
curl_setopt($ch, CURLOPT_INFILESIZE, filesize($filePath));
$response = curl_exec($ch);
$error = curl_error($ch);
curl_close($ch);
return $error ? $error : $response;
}
private function getContentType($filePath)
{
$finfo = finfo_open(FILEINFO_MIME_TYPE);
$mimeType = finfo_file($finfo, $filePath);
finfo_close($finfo);
return $mimeType ?: 'application/octet-stream';
}
public function setPublic()
{
$this->public = true;
}
public function setPrivate()
{
$this->public = false;
}
}
?>

View file

@ -17,6 +17,7 @@ class DJ
private string $updated = "";
private string $claimed_by = "";
private mysqli $db;
private array $mixes = [];
public function __construct($value, $db)
@ -118,8 +119,6 @@ class DJ
if (isset($dj['claimed_by']) && $dj['claimed_by'] != null) {
// TODO: pull some quick data on the user who claimed this DJ
$this->claimed = true;
}
if (isset($dj['created'])) {
@ -129,6 +128,8 @@ class DJ
$this->updated = $dj['lastupdated'];
}
$this->load_dj_mixes();
return true;
} else {
return false;
@ -208,5 +209,24 @@ class DJ
return $this->claimed;
}
private function load_dj_mixes(): void
{
$stmt = $this->db->prepare("SELECT * FROM mix WHERE dj1 = ? OR dj2 = ? OR dj3 = ?");
$stmt->bind_param("iii", $this->id, $this->id, $this->id);
$stmt->execute();
$result = $stmt->get_result();
$mixes = [];
while ($mix = $result->fetch_assoc()) {
$mixes[] = $mix;
}
$stmt->close();
$this->mixes = $mixes;
}
public function get_dj_mixes()
{
return $this->mixes;
}
}

View file

@ -3,12 +3,13 @@
class Genre
{
private $id = -1 ;
private $id = -1;
private $enabled = "";
private $count = 0;
private $name = "";
private $slug = "";
private $db = "";
private $mixes = [];
public function __construct($value, $db)
{
@ -22,6 +23,7 @@ class Genre
return $this->load_by_slug();
}
}
private function load_by_id(): bool
{
$genre = $this->get_genre_by_id();
@ -30,6 +32,7 @@ class Genre
$this->count = $genre['count'];
$this->name = $genre['name'];
$this->slug = $genre['slug'];
$this->mixes = $this->load_mixes();
return true;
} else {
return false;
@ -56,6 +59,7 @@ class Genre
$this->enabled = $genre['enabled'];
$this->count = $genre['count'];
$this->name = $genre['name'];
$this->mixes = $this->load_mixes();
return true;
}
return false;
@ -98,4 +102,23 @@ class Genre
return $this->count;
}
public function get_mixes(): array
{
return $this->mixes;
}
private function load_mixes(): array
{
$stmt = $this->db->prepare("SELECT mix_id FROM mix_meta WHERE attribute = 'genre' AND value = ?");
$stmt->bind_param("i", $this->id);
$stmt->execute();
$result = $stmt->get_result();
$mixes = [];
while ($row = $result->fetch_assoc()) {
$mixes[] = $row['mix_id'];
}
$stmt->close();
return $mixes;
}
}

View file

@ -7,26 +7,32 @@ class Mix
private $enabled = false;
private $name = "";
private $slug = "";
private $genre = [];
private $db = null;
private $description = "";
private $cover = "";
private $url = "";
private $seconds = 0;
private $mediaplayer = false;
private $download_only = true;
private $djs = [];
private $genres = [];
private $recorded;
private $downloads = 0;
private $created;
private $updated;
private $playcount = 0;
private $tracklist = [];
private $loadDJs = true;
private $related_mixes = [];
private $duration = [];
private $mixshow = [];
public function __construct($value, $db)
public function __construct($value, $db, $loadDJs = true)
{
$this->db = $db;
// echo the type of value
if (ctype_digit((string)$value)) {
$this->id = (int)$value;
return $this->load_by_id();
@ -40,7 +46,7 @@ class Mix
private function load_by_id(): bool
{
$mix = $this->get_mix_by_id();
if ($mix && $mix['name'] != "") {
if ($mix && $mix['title'] != "") {
return $this->build_mix($mix);
} else {
return false;
@ -58,122 +64,6 @@ class Mix
return $mix;
}
private function get_mix_genres()
{
$stmt = $this->db->prepare("SELECT * FROM mix_meta WHERE attribute = 'genre' AND mix_id = ?");
$stmt->bind_param("i", $this->id);
$stmt->execute();
$result = $stmt->get_result();
$genres = $result->fetch_all(MYSQLI_ASSOC);
$stmt->close();
return $genres;
}
private function get_playcount()
{
$stmt = $this->db->prepare("SELECT value FROM mix_meta WHERE attribute = 'playcount' AND mix_id = ?");
$stmt->bind_param("i", $this->id);
$stmt->execute();
$result = $stmt->get_result();
$genres = $result->fetch_all(MYSQLI_ASSOC);
$stmt->close();
return $genres;
}
private function load_by_slug(): bool
{
$mix = $this->get_mix_by_slug();
if ($mix['title'] != "") {
return $this->build_mix($mix);
} else {
return false;
}
}
private function get_mix_by_slug()
{
$stmt = $this->db->prepare("SELECT * FROM mix WHERE slug = ?");
$stmt->bind_param("s", $this->slug);
$stmt->execute();
$result = $stmt->get_result();
$mix = $result->fetch_assoc();
$stmt->close();
return $mix;
}
private function load_mix_genres()
{
$genres = $this->get_mix_genres();
if (count($genres) == 0) {
$this->genres = [];
} else {
// iterate through the genres; add each of the rest to the array
$this->genres = [];
require_once 'Genre.php';
foreach ($genres as $genre) {
$genre = new Genre($genre['value'], $this->db);
if ($genre->get_id() != -1) {
$this->genres[] = ['id' => $genre->get_id(), 'name' => $genre->get_name(), 'slug' => $genre->get_slug()];
}
}
}
}
private function evaluate_tracklist(): array
{
if (empty($this->tracklist)){
return [];
} else {
// if the first item in the array is also an array, then return it
if (is_array($this->tracklist[0]['value'])){
return $this->tracklist[0]['value'];
} else {
return explode("\n", $this->tracklist[0]['value']);
}
}
}
private function load_mix_tracklist()
{
$stmt = $this->db->prepare("SELECT value FROM mix_meta WHERE attribute = 'tracklist' AND mix_id = ?");
$stmt->bind_param("i", $this->id);
$stmt->execute();
$result = $stmt->get_result();
$tracklist = $result->fetch_all(MYSQLI_ASSOC);
$stmt->close();
return $tracklist;
}
public function get_img(): string
{
return $this->cover;
}
public function get_id(): int
{
return $this->id;
}
public function get_name(): string
{
return $this->name;
}
public function get_slug(): string
{
return $this->slug;
}
public function get_djs(){
return $this->djs;
}
/**
* @param $mix
* @return true
@ -198,29 +88,157 @@ class Mix
}
$this->url = $mix['url'];
$this->seconds = $mix['seconds'];
$this->mediaplayer = $mix['mediaplayer'];
$this->duration = $this->configure_duration();
$this->download_only = $mix['mediaplayer'];
$this->recorded = $mix['recorded'];
$this->created = $mix['created'];
$this->updated = $mix['lastupdated'];
$this->enabled = $mix['pending'];
require 'DJ.php';
$this->djs[] = new DJ($mix['dj1'], $this->db);
if ($mix['dj2'] != null)
$this->djs[] = new DJ($mix['dj2'], $this->db);
if ($mix['dj3'] != null)
$this->djs[] = new DJ($mix['dj3'], $this->db);
if ($this->loadDJs) {
require_once 'DJ.php';
$this->djs[] = new DJ($mix['dj1'], $this->db);
if ($mix['dj2'] != null) $this->djs[] = new DJ($mix['dj2'], $this->db);
if ($mix['dj3'] != null) $this->djs[] = new DJ($mix['dj3'], $this->db);
// delete any nulls from the array
$this->djs = array_filter($this->djs);
$this->djs = array_filter($this->djs);
}
$this->genre = $this->get_mix_genres();
$this->playcount = $this->get_playcount();
$this->tracklist = $this->load_mix_tracklist();
$this->load_mix_meta();
$this->tracklist = $this->evaluate_tracklist();
return true;
}
private function configure_duration(): array
{
$seconds = $this->seconds;
$hours = floor($seconds / 3600);
$minutes = floor(($seconds / 60) % 60);
$seconds = $seconds % 60;
// for 't', we need to show it as 01:02:03
if ($hours < 10) {
$hours0 = "0" . $hours;
} else {
$hours0 = $hours;
}
if ($minutes < 10) {
$minutes0 = "0" . $minutes;
} else {
$minutes0 = $minutes;
}
if ($seconds < 10) {
$seconds0 = "0" . $seconds;
} else {
$seconds0 = $seconds;
}
// if hours is 0, we don't need to show it
$time = $hours > 0 ? $hours0 . ":" . $minutes0 . ":" . $seconds0 : $minutes0 . ":" . $seconds0;
return ['h' => $hours, 'm' => $minutes, 's' => $seconds, 't' => $time, 'S' => $this->seconds];
}
private function load_mix_meta(): void
{
$stmt = $this->db->prepare("SELECT attribute,value FROM mix_meta WHERE mix_id = ?");
$stmt->bind_param("i", $this->id);
$stmt->execute();
$result = $stmt->get_result();
$meta = $result->fetch_all(MYSQLI_ASSOC);
$stmt->close();
foreach ($meta as $key => $value) {
if ($value['attribute'] == "genre") {
$this->genres[] = $value['value'];
unset($meta[$key]);
}
if ($value['attribute'] == "related") {
$this->related_mixes[] = $value['value'];
unset($meta[$key]);
}
if ($value['attribute'] == "playcount") {
$this->playcount = $value['value'];
unset($meta[$key]);
}
if ($value['attribute'] == "downloads") {
$this->downloads = $value['value'];
unset($meta[$key]);
}
if ($value['attribute'] == "tracklist") {
$this->tracklist = $value['value'];
unset($meta[$key]);
}
if ($value['attribute'] == "mixshow") {
$this->mixshow[] = $value['value'];
unset($meta[$key]);
}
}
}
private function evaluate_tracklist()
{
if (empty($this->tracklist)) {
return [];
} else {
// if the first item in the array is also an array, then return it
if (is_array($this->tracklist)) {
return $this->tracklist;
} else {
return explode("\n", (string)$this->tracklist);
}
}
}
private function load_by_slug(): bool
{
$mix = $this->get_mix_by_slug();
if ($mix['title'] != "") {
return $this->build_mix($mix);
} else {
return false;
}
}
private function get_mix_by_slug()
{
$stmt = $this->db->prepare("SELECT * FROM mix WHERE slug = ?");
$stmt->bind_param("s", $this->slug);
$stmt->execute();
$result = $stmt->get_result();
$mix = $result->fetch_assoc();
$stmt->close();
return $mix;
}
public function get_recorded()
{
return $this->recorded;
}
public function get_created()
{
return $this->created;
}
public function get_updated()
{
return $this->updated;
}
public function get_img(): string
{
return $this->cover;
}
public function get_djs()
{
return $this->djs;
}
public function get_description()
{
return $this->description;
@ -231,5 +249,66 @@ class Mix
return $this->tracklist;
}
public function get_genres()
{
return $this->genres;
}
public function get_seconds(): int
{
return $this->seconds;
}
public function is_download_only(): bool
{
return $this->download_only;
}
public function get_url(): string
{
return $this->url;
}
public function get_cover(): string
{
return $this->cover;
}
public function get_downloads(): int
{
return $this->downloads;
}
public function get_plays(): int
{
return $this->playcount;
}
public function get_id(): int
{
return $this->id;
}
public function get_name(): string
{
return $this->name;
}
public function get_slug(): string
{
return $this->slug;
}
public function get_duration(): array
{
return $this->duration;
}
public function get_mixshow(): array
{
return $this->mixshow;
}
}

134
classes/Mixshow.php Normal file
View file

@ -0,0 +1,134 @@
<?php
class Mixshow
{
private $id = -1;
private $enabled = false;
private $name = "";
private $slug = "";
private $db = null;
private $description = "";
private $cover = "";
private $count;
private $mixes = [];
public function __construct($value, $db)
{
$this->db = $db;
if (ctype_digit((string)$value)) {
$this->id = (int)$value;
return $this->load_by_id();
} else {
$this->slug = $value;
return $this->load_by_slug();
}
}
private function load_by_id()
{
$mixshow = $this->get_mixshow_by_id();
if ($mixshow && $mixshow['title'] != "") {
return $this->build_mixshow($mixshow);
} else {
return false;
}
}
private function get_mixshow_by_id()
{
$stmt = $this->db->prepare("SELECT * FROM shows WHERE id = ?");
$stmt->bind_param("i", $this->id);
$stmt->execute();
$result = $stmt->get_result();
return $result->fetch_assoc();
}
private function build_mixshow($mixshow)
{
$this->id = $mixshow['id'];
$this->name = $mixshow['name'];
$this->slug = $mixshow['slug'];
$this->description = $mixshow['description'];
$this->cover = $mixshow['cover'];
$this->enabled = $mixshow['enabled'];
$this->count = $mixshow['count'];
$this->load_mixes();
return true;
}
private function load_mixes()
{
$stmt = $this->db->prepare("SELECT value FROM mix_meta WHERE value = ?");
$stmt->bind_param("i", $this->id);
$stmt->execute();
$result = $stmt->get_result();
$this->mixes = [];
while ($mix = $result->fetch_assoc()) {
$this->mixes[] = $mix;
}
}
private function load_by_slug()
{
$mixshow = $this->get_mixshow_by_slug();
if ($mixshow && $mixshow['title'] != "") {
return $this->build_mixshow($mixshow);
} else {
return false;
}
}
private function get_mixshow_by_slug()
{
$stmt = $this->db->prepare("SELECT * FROM shows WHERE slug = ?");
$stmt->bind_param("s", $this->slug);
$stmt->execute();
$result = $stmt->get_result();
return $result->fetch_assoc();
}
public function get_id()
{
return $this->id;
}
public function get_name()
{
return $this->name;
}
public function get_slug()
{
return $this->slug;
}
public function get_description()
{
return $this->description;
}
public function get_cover()
{
return $this->cover;
}
public function get_enabled()
{
return $this->enabled;
}
public function get_count()
{
return $this->count;
}
public function get_mixes()
{
return $this->mixes;
}
}

8
classes/S3.php Normal file
View file

@ -0,0 +1,8 @@
<?php
require_once 'vendor/autoload.php';
Class S3 extends Aws\S3\S3Client{
}