<? class Main
{
    const httpUserAgent = 'Mozilla/5.0 Windows NT 6.1; Win64; x64 AppleWebKit/537.36 KHTML, like Gecko Chrome/61.0.3163.91 Safari/537.36';
    const referer = 'http://m.vk.com/audio';

    static function fwrite_stream($fp, $string)
    {
        if (is_resource($fp) === false) {
            return false;
        }
        for ($written = 0; $written < strlen($string); $written += $fwrite) {
            $fwrite = fwrite($fp, substr($string, $written));
            if ($fwrite === false) return $written;
        }
        return $written;
    }

    static function fileLocation($file = null, $error = false)
    {
        header('HTTP/1.1 302 Moved Temporarily');
        header('Date: ' . gmdate('D, d M Y H:i:s', time()) . ' GMT');
        header('Content-Type: text/html; charset=UTF-8');
        if ($error === false) {
            header('Location: ' . FILES_SERVER . 'audio/' . $file);
        } else {
            header('Location: ' . FILES_SERVER . 'audio/no_file.mp3');
        }
        exit();
    }

    static public function uploadFile($path, $urlFile, $referer)
    {
        if (is_readable($path) === false) {
            $fp = fopen($path, 'wb'); // Открываем файл
            $ch = curl_init($urlFile);
            curl_setopt($ch, CURLOPT_USERAGENT, self::httpUserAgent);
            curl_setopt($ch, CURLOPT_FILE, $fp); // Передаём дескриптор файла, туда сбросится результат
            curl_setopt($ch, CURLOPT_HEADER, 0);
            curl_setopt($ch, CURLOPT_REFERER, $referer);// Передаём откуда пришел юзер
            curl_setopt($ch, CURLOPT_COOKIEFILE, coreAudio . 'cookie/cookieUserVk.cook'); // Задаем куки юзера
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
       /*     ##########PROXY##########
            curl_setopt($ch, CURLOPT_PROXY, '89.191.235.127:65234');
            curl_setopt($ch, CURLOPT_PROXYUSERPWD, 'Kalitinua:W9e0DoM');
            curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5);
       */     ########END-PROXY########
            curl_exec($ch);
            curl_close($ch);
            fclose($fp);
        }
        return is_readable($path);
    }

    static public function readUrlFile($urlFile, $referer, $filename)
    {
        $ch = curl_init($urlFile);
        curl_setopt($ch, CURLOPT_USERAGENT, self::httpUserAgent);
        curl_setopt($ch, CURLOPT_REFERER, $referer);// Передаём откуда пришел юзер
        curl_setopt($ch, CURLOPT_COOKIEFILE, coreAudio . 'cookie/cookieUserVk.cook'); // Задаем куки юзера
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
    /*    ##########PROXY##########
        curl_setopt($ch, CURLOPT_PROXY, '89.191.235.127:65234');
        curl_setopt($ch, CURLOPT_PROXYUSERPWD, 'Kalitinua:W9e0DoM');
    */   curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5);
        ########END-PROXY########
        header('HTTP/1.1 200 Ok');
        header('Accept-Ranges: bytes');
        header('Content-Description: File Transfer');
        header('Content-Type: Content-Type: audio/mpeg');
        header('Content-Disposition: attachment; filename="' . $filename . '"');
        header('Content-Transfer-Encoding: binary');
        header('Expires: 0');
        header('Cache-Control: must-revalidate');
        header('Pragma: public');
        curl_exec($ch);
        curl_close($ch);
        exit();
    }
} ?>