downloader/VinylDownload.class.php

116 lines
2.9 KiB
PHP
Raw Normal View History

2017-02-01 23:20:47 +01:00
<?php
class VinylDownload {
const MAXDOWNLOADS = 3;
const TABLE = 'vinyl-codes';
public $database, $file, $code;
private function db_connect(){
$db = new SQLite3( $this->database );
if (!$db) die ($error);
return $db;
}
public function query_code($code) {
$db = $this->db_connect();
$c = SQLite3::escapeString ( $code ) ;
$q = "SELECT * FROM '" . self::TABLE . "' WHERE code='" . $c . "'" ;
$result = @$db->query( $q );
$row = @$result->fetchArray();
$db->close();
2017-03-29 10:47:35 +02:00
/* row == false when no row was found */
2017-02-01 23:20:47 +01:00
return $row;
}
private function increase_count_in_db() {
$db = @$this->db_connect();
$db->exec( "UPDATE '" . self::TABLE . "' SET count = count + 1 WHERE code='" . $this->code . "'" );
$db->close();
}
public function reached_max_dl( $count ) {
$max = false;
$count += 1;
if ( $count > self::MAXDOWNLOADS ) $max = true;
return $max;
}
public function gen_response( $c , $f ){
$this->code = $c;
$content = "Code ungültig oder abgelaufen.";
2017-03-29 10:47:35 +02:00
/* row == false when no row was found */
2017-02-01 23:20:47 +01:00
if ( $row = $this->query_code($this->code) ) {
// max count not reached
if ( false === $this->reached_max_dl( $row['count'] ) ) {
$content ='
2017-03-02 22:13:56 +01:00
<p>Du hast noch <strong>' . ( self::MAXDOWNLOADS - $row['count'] - 1 ) .'</strong> Downloads zur Verf&uuml;gung bis der Code ung&uuml;ltig wird.
<div id="1">
2017-02-01 23:20:47 +01:00
<form method=POST id="dl-trigger" action="https://www.rainervonvielen.de/digital-download"><p>
<input type="hidden" value="'. $this->code . '" name="c">
<input type="hidden" value="'. $f . '" name="sel_format">
<input type="submit" class ="btn middle" value="Start">
2017-02-01 23:20:47 +01:00
</form>
</div>
';
}
}
return $content;
}
public function dl_trigger($format) {
// Start Download
switch ($format) {
case 'mp3':
$file = $this->mp3_file;
$filename = "Rainer_von_Vielen-Überall_Chaos_2017-MP3.zip";
2017-02-01 23:20:47 +01:00
break;
case 'wav':
$file = $this->wav_file;
$filename = "Rainer_von_Vielen-Überall_Chaos_2017-WAV.zip";
2017-02-01 23:20:47 +01:00
break;
}
$this->downloader( $file, $filename );
$this->increase_count_in_db();
}
private function downloader( $file, $filename ){
// Force the browser to start the download automatically
// Variables:
// $file = real name of actual download file on the server
// $filename = new name of local download file - this is what the visitor's file will actually be called when he/she saves it
ob_start();
header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment; filename=".$filename."");
2017-02-01 23:20:47 +01:00
header("Content-Description: File Transfer");
header("Content-Length: " .(string)(filesize($file)) );
header("Cache-Control: public, must-revalidate");
header("Pragma: public");
header("Content-Transfer-Encoding: binary\n");
ob_end_clean();
readfile($file);
}
}
?>