Exemplo n.º 1
0
function downloadAlbum($album_id)
{
    global $cfg, $db;
    $sid = get('sid');
    $download_id = (int) get('download_id');
    if ($sid) {
        // Download shared file
        header('Expires: Mon, 9 Oct 2000 18:00:00 GMT');
        header('Cache-Control: no-store, no-cache, must-revalidate');
        mysql_query('UPDATE share_download SET
			ip			= "' . mysql_real_escape_string($_SERVER['REMOTE_ADDR']) . '"
			WHERE sid	= BINARY "' . mysql_real_escape_string($sid) . '"
			AND ip		= ""');
        $query = mysql_query('SELECT album_id, download_id
			FROM share_download
			WHERE sid = BINARY "' . mysql_real_escape_string($sid) . '"
			AND ip = "' . mysql_real_escape_string($_SERVER['REMOTE_ADDR']) . '"
			AND expire_time > ' . (int) time());
        $share_download = mysql_fetch_assoc($query);
        if ($share_download == false || $cfg['album_share_download'] == false) {
            message(__FILE__, __LINE__, 'error', '[b]Download failed[/b][br]Authentication failed or share download is disabled');
        }
        $album_id = $share_download['album_id'];
        $download_id = $share_download['download_id'];
        if (cacheGetFile($album_id, $download_id)) {
            authenticate('access_always', true);
        } else {
            authenticate('access_always');
        }
        $download_url = NJB_HOME_URL . 'download.php?action=downloadAlbum&sid=' . $sid;
    } else {
        // Common download
        if (cacheGetFile($album_id, $download_id)) {
            authenticate('access_download', true);
        } else {
            authenticate('access_download');
        }
        if ($cfg['album_download'] == false) {
            message(__FILE__, __LINE__, 'error', '[b]Error[/b][br]Download album disabled');
        }
        $download_url = NJB_HOME_URL . 'download.php?action=downloadAlbum&album_id=' . rawurlencode($album_id) . '&download_id=' . $download_id;
    }
    if ($download_id != -1 && isset($cfg['encode_extension'][$download_id]) == false) {
        message(__FILE__, __LINE__, 'error', '[b]Unsupported input value for[/b][br]download_id');
    }
    $query = mysql_query('SELECT track_id
		FROM track
		WHERE album_id	= "' . mysql_real_escape_string($album_id) . '"');
    if (mysql_num_rows($query) == 1) {
        // By one file downloadTrack()
        $track = mysql_fetch_assoc($query);
        downloadTrack($track['track_id']);
        if ($sid != '') {
            mysql_query('DELETE FROM share_download WHERE sid = BINARY "' . mysql_real_escape_string($sid) . '"');
        } else {
            updateCounter($album_id, NJB_COUNTER_DOWNLOAD);
        }
        exit;
    }
    if ($file = cacheGetFile($album_id, $download_id)) {
        // Download from cache
        $query = mysql_query('SELECT artist_alphabetic, album, year FROM album WHERE album_id = "' . mysql_real_escape_string($album_id) . '"');
        $album = mysql_fetch_assoc($query);
        $filename = $album['artist_alphabetic'] . ' - ';
        $filename .= $album['year'] ? $album['year'] . ' - ' : '';
        $filename .= $album['album'] . '.' . $cfg['download_album_extension'];
        $filename = downloadFilename($filename);
        streamFile($file, $cfg['download_album_mime_type'], 'attachment', $filename);
        if ($sid != '') {
            mysql_query('DELETE FROM share_download WHERE sid = BINARY "' . mysql_real_escape_string($sid) . '"');
        } else {
            updateCounter($album_id, NJB_COUNTER_DOWNLOAD);
        }
        exit;
    }
    ini_set('max_execution_time', 0);
    $query = mysql_query('SELECT artist_alphabetic, album FROM album WHERE album_id = "' . mysql_real_escape_string($album_id) . '"');
    $album = mysql_fetch_assoc($query);
    if ($album == false) {
        message(__FILE__, __LINE__, 'error', '[b]Error[/b][br]album_id not found in database');
    }
    // formattedNavigator
    $nav = array();
    $nav['name'][] = 'Media';
    $nav['url'][] = 'index.php';
    $nav['name'][] = $album['artist_alphabetic'];
    $nav['url'][] = 'index.php?action=view2&artist=' . rawurlencode($album['artist_alphabetic']);
    $nav['name'][] = $album['album'];
    $nav['url'][] = 'index.php?action=view3&album_id=' . $album_id;
    $nav['name'][] = 'Download album';
    require_once 'include/header.inc.php';
    ?>
<table cellspacing="0" cellpadding="0" class="border">
<tr class="header">
	<td class="space"></td>
	<td><?php 
    echo $download_id == -1 ? 'Source' : 'Transcode to ' . html($cfg['encode_name'][$download_id]);
    ?>
</td>
	<td class="textspace"></td>
	<td></td>
	<td class="textspace"></td>
	<td></td>
	<td class="space"></td>
</tr>
<tr class="line"><td colspan="7"></td></tr>
<?php 
    $i = 0;
    $query = mysql_query('SELECT title, artist FROM track WHERE album_id = "' . mysql_real_escape_string($album_id) . '" ORDER BY relative_file');
    while ($track = mysql_fetch_assoc($query)) {
        ?>
<tr class="<?php 
        echo $i++ & 1 ? 'even' : 'odd';
        ?>
">
	<td></td>
	<td><?php 
        echo html($track['artist']);
        ?>
</td>
	<td></td>
	<td><?php 
        echo html($track['title']);
        ?>
</td>
	<td></td>
	<td><span id="status<?php 
        echo $i;
        ?>
"></span></td>
	<td></td>
</tr>
<?php 
    }
    ?>
<tr class="line"><td colspan="7"></td></tr>
<tr class="header">
	<td></td>
	<td colspan="5">Download</td>
	<td></td>
</tr>
<tr class="line"><td colspan="7"></td></tr>
<tr class="odd">
	<td></td>
	<td colspan="3"><span id="text">Create <?php 
    echo $cfg['download_album_extension'];
    ?>
 file</span></td>
	<td></td>
	<td align="center"><span id="icon"></span></td>
	<td></td>
</tr>
</table>
<?php 
    $cfg['footer'] = 'dynamic';
    require 'include/footer.inc.php';
    $cache_dir = cacheGetDir($album_id, $download_id);
    $i = 0;
    $hash_data = '';
    $list = $cache_dir . $album_id . '.txt';
    $list_content = '';
    $query = mysql_query('SELECT track_id, relative_file FROM track WHERE album_id = "' . mysql_real_escape_string($album_id) . '" ORDER BY relative_file');
    while ($track = mysql_fetch_assoc($query)) {
        $i++;
        echo '<script type="text/javascript">document.getElementById(\'status' . $i . '\').innerHTML=\'<img src="' . $cfg['img'] . 'small_animated_progress.gif" alt="" class="small">\';</script>' . "\n";
        @ob_flush();
        flush();
        $source = transcode($track['track_id'], $download_id);
        $pathinfo = pathinfo($source);
        $source_name = $pathinfo['filename'];
        $pathinfo = pathinfo($track['relative_file']);
        $destination_name = $pathinfo['filename'];
        $destination_name = downloadFilename($destination_name, true, true);
        if ($source_name == $destination_name) {
            $destination = $source;
        } else {
            $destination = $cache_dir . $destination_name . '.' . $cfg['encode_extension'][$download_id];
            @copy($source, $destination) or message(__FILE__, __LINE__, 'error', '[b]Failed to copy file[/b][br]From: ' . $source . '[br]To: ' . $destination);
        }
        $pathinfo = pathinfo($destination);
        $hash_data .= $pathinfo['filename'];
        $destination = iconv(NJB_DEFAULT_CHARSET, 'UTF-8', $destination);
        $list_content .= $destination . "\n";
        echo '<script type="text/javascript">document.getElementById(\'status' . $i . '\').innerHTML=\'<img src="' . $cfg['img'] . 'small_check.png" alt="" class="small">\';</script>' . "\n";
        @ob_flush();
        flush();
    }
    if (file_put_contents($list, $list_content) === false) {
        message(__FILE__, __LINE__, 'error', '[b]Failed to write file:[/b][br]' . $list);
    }
    echo '<script type="text/javascript">document.getElementById(\'icon\').innerHTML=\'<img src="' . $cfg['img'] . 'small_animated_progress.gif" alt="" class="small">\';</script>' . "\n";
    @ob_flush();
    flush();
    $hash = md5($hash_data);
    $destination = $cache_dir . $album_id . '_' . $hash . '.' . $cfg['download_album_extension'];
    // Download album
    if (NJB_WINDOWS) {
        $cmd = $cfg['download_album_cmd'];
    } else {
        $cmd = $cfg['download_album_cmd'] . ' 2>&1';
    }
    $cmd = str_replace('%list', escapeCmdArg($list), $cmd);
    $cmd = str_replace('%destination', escapeCmdArg($destination), $cmd);
    $cmd_output = array();
    $cmd_return = 0;
    @exec($cmd, $cmd_output, $cmd_return);
    if ($cmd_return != 0) {
        message(__FILE__, __LINE__, 'error', '[b]Exec error[/b][br][b]Command:[/b] ' . $cmd . '[br][b]System output:[/b] ' . implode('[br]', $cmd_output) . '[br][b]System return code:[/b] ' . $cmd_return);
    }
    if (is_file($destination) == false) {
        message(__FILE__, __LINE__, 'error', '[b]Destination file not created[/b][br]File: ' . $destination . '[br]Command: ' . $cmd);
    }
    cacheUpdateFile($album_id, $download_id, $destination, '', $hash);
    // Cleanup
    @unlink($list);
    recursiveValidate($cache_dir);
    cacheCleanup();
    $download_url .= '&amp;timestamp=' . dechex(time());
    echo '<script type="text/javascript">document.getElementById(\'text\').innerHTML=\'<a href="' . $download_url . '"><img src="' . $cfg['img'] . 'small_download.png" alt="" class="small space">Download ' . $cfg['download_album_extension'] . ' file (' . formattedSize(filesize($destination)) . ')<\\/a>\';</script>' . "\n";
    echo '<script type="text/javascript">document.getElementById(\'icon\').innerHTML=\'\';</script>' . "\n";
    echo '<iframe src="' . $download_url . '" width="0" height="0" scrolling="no" frameborder="0"></iframe>' . "\n";
    $cfg['footer'] = 'close';
    require 'include/footer.inc.php';
}
Exemplo n.º 2
0
function recursiveValidate($dir)
{
    global $cfg, $db;
    $entries = @scandir($dir) or message(__FILE__, __LINE__, 'error', '[b]Failed to open directory:[/b][br]' . $dir);
    foreach ($entries as $entry) {
        if (!in_array($entry, array('.', '..', 'index.php'))) {
            if (is_dir($dir . $entry . '/')) {
                recursiveValidate($dir . $entry . '/');
                if (strlen($entry) != 1) {
                    @rmdir($dir . $entry . '/');
                }
            } else {
                $file = $dir . $entry;
                $relative_file = substr($file, strlen(NJB_HOME_DIR));
                $query = mysql_query('SELECT filesize, filemtime FROM cache
					WHERE relative_file = "' . mysql_real_escape_string($relative_file) . '"');
                $cache = mysql_fetch_assoc($query);
                if (filesize($file) == $cache['filesize'] && filemtimeCompare(filemtime($file), $cache['filemtime'])) {
                    mysql_query('UPDATE cache
						SET updated			= 1
						WHERE relative_file	= "' . mysql_real_escape_string($relative_file) . '"');
                } else {
                    @unlink($file);
                    // File can be in use by another process.
                    // So don't give an error message when the file can't be deleted.
                }
            }
        }
    }
}