<?php
require_once 'inc/mysqli.php';

function rus2eng($string) {

    $converter = array(

        'а' => 'a',   'б' => 'b',   'в' => 'v',

        'г' => 'g',   'д' => 'd',   'е' => 'e',

        'ё' => 'e',   'ж' => 'zh',  'з' => 'z',

        'и' => 'i',   'й' => 'y',   'к' => 'k',

        'л' => 'l',   'м' => 'm',   'н' => 'n',

        'о' => 'o',   'п' => 'p',   'р' => 'r',

        'с' => 's',   'т' => 't',   'у' => 'u',

        'ф' => 'f',   'х' => 'h',   'ц' => 'c',

        'ч' => 'ch',  'ш' => 'sh',  'щ' => 'sch',

        'ь' => '',  'ы' => 'y',   'ъ' => '',

        'э' => 'e',   'ю' => 'yu',  'я' => 'ya',



        '50 Cent' =>'50 центов', 'А' => 'A',   'Б' => 'B',   'В' => 'V',

        'Г' => 'G',   'Д' => 'D',   'Е' => 'E',

        'Ё' => 'E',   'Ж' => 'Zh',  'З' => 'Z',

        'И' => 'I',   'Й' => 'Y',   'К' => 'K',

        'Л' => 'L',   'М' => 'M',   'Н' => 'N',

        'О' => 'O',   'П' => 'P',   'Р' => 'R',

        'С' => 'S',   'Т' => 'T',   'У' => 'U',

        'Ф' => 'F',   'Х' => 'H',   'Ц' => 'C',

        'Ч' => 'Ch',  'Ш' => 'Sh',  'Щ' => 'Sch',

        'Ь' => '',  'Ы' => 'Y',   'Ъ' => '',

        'Э' => 'E',   'Ю' => 'Yu',  'Я' => 'Ya', 
		' ' => '_', '#' => '',
		'`' => '', '+' => '_', '&' => 'and');

    return strtr($string, $converter);

}
	function str2url($str) {
		$str = mb_strtolower($str, 'UTF-8');
		$str = preg_replace('~[^-a-zA-Zа-яА-Я0-9_&]+~u', '', $str);
		$str = trim($str, "-");
		return $str;
	}
	$file = base64_decode($_GET['f']);
	$name = $db->getRow('SELECT name_full,filesize,id,store,host,ya_url FROM files WHERE filename = ?s',$file);
	
	if($name['host'] == 'yandex'){
		//$file_url = 'https://getfile.dokpub.com/yandex/get/'.$name['ya_url'];
	function get_stat( $url, $headers ){
		$handle = curl_init();
		curl_setopt( $handle, CURLOPT_URL, $url );
		curl_setopt( $handle, CURLOPT_HTTPHEADER, $headers );
		curl_setopt( $handle, CURLOPT_SSL_VERIFYPEER, false );
		curl_setopt( $handle, CURLOPT_SSL_VERIFYHOST, false );
		curl_setopt( $handle, CURLOPT_RETURNTRANSFER, true );
		$response = curl_exec( $handle );
		$code = curl_getinfo( $handle, CURLINFO_HTTP_CODE );
		return array( "code" => $code, "response" => $response );
	}

	$result = get_stat( "https://cloud-api.yandex.net:443/v1/disk/public/resources/download?public_key=" . urlencode($name['ya_url']), array() );
	
	if( $result["code"] == 200 ){
		$result["response"] = json_decode( $result["response"], true );
		$file_url = $result["response"]["href"];
	}else{
		$file_url = 'https://getfile.dokpub.com/yandex/get/'.$name['ya_url'];
	}
	}else{
		$set = $db->getRow("SELECT `set` FROM `settings` WHERE `name` = 'site_url' LIMIT 1");
		
		$file_url = 'https://'.$set['set'].'mp3/'.$file;
	}
		
	if (ob_get_level()) {
      ob_end_clean();
    }
	
    header("HTTP/1.1 200 OK"); 
	header("Connection: close"); 
	header("Content-Type: audio/mpeg"); 
	header("Accept-Ranges: bytes"); 
    header('Content-Disposition: attachment; filename='.str2url(rus2eng($name['name_full'])).'_('.$_SERVER['HTTP_HOST'].').mp3');
    header('Content-Length:  '.$name['filesize']);

    if ($fd = fopen($file_url, 'rb')) {
      while (!feof($fd)) {
        print fread($fd, $name['filesize']);
      }
      fclose($fd);
    }
	$db->query("UPDATE `files` SET `loads` = `loads` +1 WHERE `id` = ?i", $name['id']);
    exit;
