dj_mix_hosting_software/classes/DownloadMix.php
Cody Cook 70c8a87e15
All checks were successful
SonarQube Scan / SonarQube Trigger (push) Successful in 30s
Changes.
2024-05-19 20:01:13 -07:00

106 lines
3.0 KiB
PHP

<?php
namespace DJMixHosting;
class DownloadMix
{
private $db;
private $mix;
private $ready = false;
private $name;
private $djs;
private $filename;
private $url;
private $mix_id;
private $content;
private $filesize = 0;
private $ext;
public function __construct($mix, $db)
{
$this->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();
}
}