<?php

// defines if this script requires to be logged in
define( "PRIVATE", false );

# CALL COMMON LIBRARIES

require_once('./lib/inc-common.php');
require_once(FS_PATH."/etc/locale/".LANG_PACK."/inc-batch.php");

// Don't try to subvert me!
if (BATCH_DOWNLOAD == 'f') {

	header ("Location: index.php\n\n");
	
}

set_time_limit (BATCH_TIME_LIMIT);

if (strtolower($_REQUEST['do']) == 'download') {
	bundleMe ($_REQUEST['val']);
} else {
	header ("Location: index.php\n\n");
}

function bundleMe ($val) {
	
	GLOBAL $dbconn, $NETJUKE_SESSION_VARS;
	
	$total_size = 0;
	
	if ($val != '') {
	
		$id = split(",",$val);

    	$trans = get_html_translation_table (HTML_ENTITIES);
    	$trans = array_flip ($trans);
    	$data = strtr($data,$trans);
	
		foreach ($id as $this_id) {
      
	    $sql = "SELECT tr.location, tr.time, tr.name, ar.name, al.name  "
         . " from netjuke_tracks tr, netjuke_artists ar, netjuke_albums al "
         . " where tr.id = $this_id and ar.id = tr.ar_id and al.id = tr.al_id ";
      
      	$dbrs = $dbconn->Execute($sql);
      
      	$tr_location  = separatorCleanup($dbrs->fields[0]);
      	$tr_time      = floor($dbrs->fields[1]);
      	$tr_name      = strtr(format_for_display($dbrs->fields[2]),$trans);
      	$ar_name      = strtr(format_for_display($dbrs->fields[3]),$trans);
      	$al_name      = strtr(format_for_display($dbrs->fields[4]),$trans);
      
      	# Append web path value if location still doesn't contain ://
      	# Copes with the fact that the streaming server could be a relative path (eg: music/)
      	# instead of a protocol+hostname combination (http://streaming.host.dom)
      	#if (!strstr($tr_location,"://")) $tr_location  = WEB_PATH."/".$tr_location;
      
      	$tr_location = separatorCleanup($tr_location);   
        	
		$files .= "\"".rawurldecode($tr_location)."\" ";
       	// update the track's download count.
       	// Is normally done accurately in the dispatcher, but it is now optional...
       	$dbconn->Execute( "update netjuke_tracks set dl_cnt = dl_cnt + 1 where id = ".$this_id." " );
        
		$dbrs->Close();
		
		$total_size += filesize (MUSIC_DIR . '/' . $trlocation);
		if (($total_size / (1024 * 1024)) >= BATCH_SIZE_LIMIT) {
		
			exit;
			
		}

      }
		
	} else {
	
		alert (PLAYER_NOID);
    	exit;

	}
	
	if ($NETJUKE_SESSION_VARS["batch_type"] == '' )
	{
	
	// if user has not set batch_type, set one for them
	
		if (BATCH_TAR_PATH != "") 
		{ 
			$NETJUKE_SESSION_VARS["batch_type"] = 'TAR' ; 
		} else if (BATCH_ZIP_PATH != "") 
		{ 
			$NETJUKE_SESSION_VARS["batch_type"] = 'ZIP' ; 
		} else if (BATCH_SIT_PATH != "") 
		{ 
			$NETJUKE_SESSION_VARS["batch_type"] = 'SIT' ; 
		}

	  netjuke_session('update');
	
	}
	
	// Build the archive
	switch ($NETJUKE_SESSION_VARS["batch_type"]) {
		
		case 'TAR':
		
			$command = BATCH_TAR_PATH . " c -C " . MUSIC_DIR . " " . $files;
			$mime = "application/x-tar";
			$ext = "tar";
			
			break;
		
		case 'ZIP':
	
			$command = BATCH_ZIP_PATH . " -q -0 - " . $files;
			$mime = "application/zip";
			$ext = "zip";
			chdir (MUSIC_DIR);	// change working directory -- zip uses relative paths
			
			break;
		
		case 'SIT':
		
			$tfile = tempnam ("/tmp", "netjuke-");
			if ($tfile == FALSE) {
			
				javascript ("alert('".TMPNAM_FAILURE."');");
				exit;
				
			}
			
			unlink ($tfile);	// Remove file -- stuff stubbornly will not overwrite it
			
			$command = BATCH_SIT_PATH . " -l=0 -q --name=" . $tfile . " " . $files;
			$ext = "sit";
			$mime = "application/sit";
			chdir (MUSIC_DIR);	// change working directory -- zip uses relative paths
	
			break;
			
		default:
		
			header ("Location: prefs.php?do=edit\n\n");
			exit;
			
			break;
			
	}
	
	header("Content-type: ".$mime);
	header("Content-Disposition: attachment; filename=netjuke-".substr(time(),-7).".".$ext );
	
	if ($NETJUKE_SESSION_VARS["batch_type"] == "SIT") {
	
		exec ($command);
		
		$val = @readfile ($tfile);
		if ($val == FALSE) {
		
			javascript ("alert('".TMPNAM_FAILURE."');");
			exit;
			
		}
		
	} else {
  		
		passthru ($command);
		
	}

  	exit;
}

?>
