5 # Copyright (c) 2003 Ivan Kohler
7 # This program is free software; you can redistribute it and/or modify it under
8 # the same terms as Perl itself.
11 use subs qw(suidsetup pick_server);
13 use CGI::Carp qw(fatalsToBrowser);
14 use Digest::MD5 qw(md5_hex);
20 # pull in configuration
21 use vars qw($dsn $user $pass $mp3path $port);
22 use vars qw($override_query $fileno_query);
23 use vars qw(%extension2type);
24 require "/etc/iceplex.conf";
26 #detect .pls or .m3u filetype
27 $0 =~ /\.(pls|m3u)$/i or die "must be named with .pls or .m3u extension";
28 my $extension = lc($1);
31 my $dbh = DBI->connect($dsn, $user, $pass) or die $DBI::errstr;
34 my $server = pick_server();
36 #get customer & user from client browser
38 $cgi->param('customer') =~ /^(\d{1,10})$/ or die 'illegal customer numer';
39 my $customer = sprintf("%6d", $1); #!! how many digits in customer #?
40 $cgi->param('user') =~ /^(\d{1,10})$/ or die 'illegal user numer';
41 my $user = sprintf("%4d", $1); #!! how many digits in user #?
45 if ( $override_query ) {
46 my $sth = $dbh->prepare( $override_query ) or die $dbh->errstr;
47 $sth->execute($customer, $user) or die $sth->errstr;
48 my $row = $sth->fetchrow_arrayref;
49 $fileno = $row->[0] if $row;
51 if ( !length($fileno) && $fileno_query ) {
52 my $sth = $dbh->prepare( $fileno_query ) or die $dbh->errstr;
53 $sth->execute($customer, $user) or die $sth->errstr;
54 my $row = $sth->fetchrow_arrayref;
55 die "No files for user $user of customer $customer" unless $row;
58 $fileno = '000' unless length($fileno); #default
61 my $filename = "$mp3path/$customer-$user-$fileno.mp3";
63 #disconnect from database
66 #generate a mountpoint
67 md5_hex($cgi->remote_host. $$. time. int(rand(4294967296))) =~ /^(\w+)$/;
70 #signal encoder to start streaming to mountpoint & wait for confirmation
71 warn "ssh $server yashout $filename $mountpoint $port\n";
72 ssh($server, 'yashout', $filename, $mountpoint, $port );
74 #send file back to client browser
76 print $cgi->header( -type => $extension2type{$extension} );
77 print "[playlist]\nNumberOfEntries=1\nFile1=" if $extension eq 'pls';
78 print "http://$server:$port/$mountpoint\n";
85 $ENV{'PATH'} ='/usr/local/bin:/usr/bin:/usr/ucb:/bin';
86 $ENV{'SHELL'} = '/bin/sh';
87 $ENV{'IFS'} = " \t\n";
90 $ENV{'BASH_ENV'} = '';
92 $< = $>; #correct uid for spawned ssh process
96 my $sth = $dbh->prepare(
97 "SELECT servername FROM iceplex_servers ".
98 "WHERE status = 'online' ".
99 "ORDER BY listeners ASC LIMIT 1"
100 ) or die $dbh->errstr;
101 $sth->execute() or die $sth->errstr;
102 my $row = $sth->fetchrow_arrayref;
103 die "No servers online!" unless $row;