#!/usr/bin/perl
###############################################################################
# #
# IPFire.org - A linux based firewall #
# Copyright (C) 2007 Michael Tremer & Christian Schmidt #
# #
# This program is free software: you can redistribute it and/or modify #
# it under the terms of the GNU General Public License as published by #
# the Free Software Foundation, either version 3 of the License, or #
# (at your option) any later version. #
# #
# This program is distributed in the hope that it will be useful, #
# but WITHOUT ANY WARRANTY; without even the implied warranty of #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
# GNU General Public License for more details. #
# #
# You should have received a copy of the GNU General Public License #
# along with this program. If not, see . #
# #
###############################################################################
use strict;
# enable only the following on debugging purpose
#use warnings;
#use CGI::Carp 'fatalsToBrowser';
require '/var/ipfire/general-functions.pl';
require "${General::swroot}/lang.pl";
require "${General::swroot}/header.pl";
my %color = ();
my %mainsettings = ();
my %mpfiresettings = ();
my %checked = ();
my $message = '0';
my $errormessage = "";
&General::readhash("${General::swroot}/main/settings", \%mainsettings);
&General::readhash("/srv/web/ipfire/html/themes/".$mainsettings{'THEME'}."/include/colors.txt", \%color);
&Header::showhttpheaders();
sub refreshpage{&Header::openbox( 'Waiting', 1, "" );print "
$Lang::tr{'pagerefresh'}";&Header::closebox();}
$mpfiresettings{'PAGE'} = "1";
open(DATEI, "<${General::swroot}/mpfire/db/mpd.db") || die "No Database found";
my @songdb = ;
close(DATEI);
my @artist; my @album; my @genre; my @year; my $linecount = 0; my @currentsong = ""; my %songs;
foreach (@songdb){
if ( $_ =~ /mtime: / ){
$songs{$currentsong[1]}="$currentsong[2]|$currentsong[3]|$currentsong[4]|$currentsong[5]|$currentsong[6]|$currentsong[7]|$currentsong[8]|$currentsong[9]";
push(@artist,$currentsong[4]);
push(@album,$currentsong[6]);
push(@year,$currentsong[8]);
push(@genre,$currentsong[9]);
@currentsong = "";
}
elsif ( $_ =~ /key: / || $_ =~ /file: / || $_ =~ /Time: / || $_ =~ /Artist: / || $_ =~ /Title: / || $_ =~ /Album: / || $_ =~ /Track: / || $_ =~ /Date: / || $_ =~ /Genre: / ){
my @temp = split(/: /,$_);
push(@currentsong,$temp[1]);
}
else {
next;
}
}
my %hash = map{ $_, 1 }@artist;
@artist = sort keys %hash;
my %hash = map{ $_, 1 }@album;
@album = sort keys %hash;
my %hash = map{ $_, 1 }@year;
@year = sort keys %hash;
my %hash = map{ $_, 1 }@genre;
@genre = sort keys %hash;
my $artistcount = $#artist+1;
my $albumcount = $#album+1;
my $yearcount = $#year+1;
my $genrecount = $#genre+1;
&Header::getcgihash(\%mpfiresettings);
&Header::openpage($Lang::tr{'mpfire'}, 1, "");
&Header::openbigbox('100%', 'left', '', $errormessage);
############################################################################################################################
######################################## Scanne Verzeichnisse nach Mp3 Dateien #############################################
if ( $mpfiresettings{'ACTION'} eq "scan" )
{
delete $mpfiresettings{'__CGI__'};delete $mpfiresettings{'x'};delete $mpfiresettings{'y'};delete $mpfiresettings{'PAGE'};
&General::writehash("${General::swroot}/mpfire/settings", \%mpfiresettings);
open(DATEI, "<${General::swroot}/mpfire/mpd.conf") || die "Datei nicht gefunden";
my @Zeilen = ;
close(DATEI);
open(DATEI, ">${General::swroot}/mpfire/mpd.conf") || die "Datei nicht gefunden";
foreach (@Zeilen){
if ( $_ =~ /music_directory/){print DATEI "music_directory \"".$mpfiresettings{'MUSICDIR'}."\"\n";}
else {print DATEI $_;}
}
close(DATEI);
$message=system("/usr/local/bin/mpfirectrl scan");
refreshpage();
}
elsif ( $mpfiresettings{'ACTION'} eq ">" ){$message=system("/usr/local/bin/mpfirectrl","play","\"$mpfiresettings{'FILE'}\"");}
elsif ( $mpfiresettings{'ACTION'} eq "x" ){$message=system("/usr/local/bin/mpfirectrl stop");}
elsif ( $mpfiresettings{'ACTION'} eq "|>" ){$message=system("/usr/local/bin/mpfirectrl toggle");}
elsif ( $mpfiresettings{'ACTION'} eq "<<" ){$message=system("/usr/local/bin/mpfirectrl prev");}
elsif ( $mpfiresettings{'ACTION'} eq ">>" ){$message=system("/usr/local/bin/mpfirectrl next");}
elsif ( $mpfiresettings{'ACTION'} eq "+" ){$message=system("/usr/local/bin/mpfirectrl volup 5");}
elsif ( $mpfiresettings{'ACTION'} eq "-" ){$message=system("/usr/local/bin/mpfirectrl voldown 5");}
elsif ( $mpfiresettings{'ACTION'} eq "++" ){$message=system("/usr/local/bin/mpfirectrl volup 10");}
elsif ( $mpfiresettings{'ACTION'} eq "--" ){$message=system("/usr/local/bin/mpfirectrl voldown 10");}
elsif ( $mpfiresettings{'ACTION'} eq "playweb" ){$message=system("/usr/local/bin/mpfirectrl","playweb","\"$mpfiresettings{'FILE'}\"");}
elsif ( $mpfiresettings{'ACTION'} eq "playlist" ){$message=system("/usr/local/bin/mpfirectrl playlist");}
elsif ( $mpfiresettings{'ACTION'} eq "emptyplaylist" ){$message=system("/usr/local/bin/mpfirectrl clearplaylist");}
elsif ( $mpfiresettings{'ACTION'} eq "addtoplaylist" ){$message=system("/usr/local/bin/mpfirectrl","playadd","\"$mpfiresettings{'FILE'}\"");}
elsif ( $mpfiresettings{'ACTION'} eq "playall" ){$message=system("/usr/local/bin/mpfirectrl playall");}
elsif ( $mpfiresettings{'ACTION'} eq "playalbum" )
{
my @temp = ""; my @song = ""; my @select = split(/\|/,$mpfiresettings{'album'});
foreach (keys(%songs)){
@song = split(/\|/,$songs{$_});$song[4] =~ s/\W/ /g;
foreach (@select){
$_ =~ s/\W/ /g;
if ( $song[4] =~ /$_/ ){push(@temp,$song[0]);}
}
}
open(DATEI, ">${General::swroot}/mpfire/playlist.m3u") || die "Could not add playlist";
print DATEI @temp;
close(DATEI);
$message=system("/usr/local/bin/mpfirectrl playlist");
}
elsif ( $mpfiresettings{'ACTION'} eq "playartist" )
{
my @temp = ""; my @song = ""; my @select = split(/\|/,$mpfiresettings{'artist'});
foreach (keys(%songs)){
@song = split(/\|/,$songs{$_});$song[2] =~ s/\W/ /g;
foreach (@select){
$_ =~ s/\W/ /g;
if ( $song[2] =~ /$_/ ){push(@temp,$song[0]);}
}
}
open(DATEI, ">${General::swroot}/mpfire/playlist.m3u") || die "Could not add playlist";
print DATEI @temp;
close(DATEI);
$message=system("/usr/local/bin/mpfirectrl playlist");
}
elsif ( $mpfiresettings{'ACTION'} eq "playyear" )
{
my @temp = ""; my @song = ""; my @select = split(/\|/,$mpfiresettings{'year'});
foreach (keys(%songs)){
@song = split(/\|/,$songs{$_});$song[6] =~ s/\W/ /g;
foreach (@select){
$_ =~ s/\W/ /g;
if ( $song[6] =~ /$_/ ){push(@temp,$song[0]);}
}
}
open(DATEI, ">${General::swroot}/mpfire/playlist.m3u") || die "Could not add playlist";
print DATEI @temp;
close(DATEI);
$message=system("/usr/local/bin/mpfirectrl playlist");
}
elsif ( $mpfiresettings{'ACTION'} eq "playgenre" )
{
my @temp = ""; my @song = ""; my @select = split(/\|/,$mpfiresettings{'genre'});
foreach (keys(%songs)){
@song = split(/\|/,$songs{$_});$song[7] =~ s/\W/ /g;
foreach (@select){
$_ =~ s/\W/ /g;
if ( $song[7] =~ /$_/ ){push(@temp,$song[0]);}
}
}
open(DATEI, ">${General::swroot}/mpfire/playlist.m3u") || die "Could not add playlist";
print DATEI @temp;
close(DATEI);
$message=system("/usr/local/bin/mpfirectrl playlist");
}
elsif ( $mpfiresettings{'SHOWLIST'} ){delete $mpfiresettings{'__CGI__'};delete $mpfiresettings{'x'};delete $mpfiresettings{'y'};delete $mpfiresettings{'PAGE'};&General::writehash("${General::swroot}/mpfire/settings", \%mpfiresettings);refreshpage();}
############################################################################################################################
################################### Aufbau der HTML Seite fr globale Sambaeinstellungen ####################################
$mpfiresettings{'MUSICDIR'} = "/";
&General::readhash("${General::swroot}/mpfire/settings", \%mpfiresettings);
############################################################################################################################
########################################### rekursiv nach neuen Mp3s Scannen ##############################################รค
if ( $message ne '0' ) { print "An Error occured while launching the command"; }
elsif ( $message ne "" && $message ne '0' ) { print "$message"; }
&Header::openbox('100%', 'center', $Lang::tr{'mpfire scanning'});
print <
END
;
&Header::closebox();
my $song = qx(/usr/local/bin/mpfirectrl song);
if ( $song eq "" ){$song = "None";}
my $Volume = `/usr/local/bin/mpfirectrl volume`;
$Volume=~s//
/g;
my $stats = `mpc stats | tail -4`;
$stats=~s/\\/
/g
&Header::openbox('100%', 'center', $Lang::tr{'mpfire controls'});
print <
|
END
;
my $countsongs=`/usr/local/bin/mpfirectrl stats`;
print " ".$countsongs."
|
";
print <
|
|
|
|
|
END
;
if ( $mpfiresettings{'SHOWLIST'} eq "on" ){print" | ";}
else { print"
| ";}
print <
|
|
|
$Volume |
$stats |
END
;
&Header::closebox();
&Header::openbox('100%', 'center', $Lang::tr{'quick playlist'});
print "";
if ( $#songdb eq '-1' ) {print "$Lang::tr{'artist'} | $Lang::tr{'album'} |
";}
else {print "$Lang::tr{'artist'} - ".$artistcount." | $Lang::tr{'album'} - ".$albumcount." |
";}
print <
|
|
END
;
if ( $#songdb eq '-1' ) {print "$Lang::tr{'year'} | $Lang::tr{'genre'} |
";}
else {print "$Lang::tr{'year'} - ".$yearcount." | $Lang::tr{'genre'} - ".$genrecount." |
";}
print <
|
|
END
;
&Header::closebox();
if ( $mpfiresettings{'SHOWLIST'} eq "on" ){
&Header::openbox('100%', 'center', $Lang::tr{'mpfire songs'});
print <
$Lang::tr{'Existing Files'} |
$Lang::tr{'Pages'}
|
|
$Lang::tr{'artist'} $Lang::tr{'title'} |
$Lang::tr{'number'} |
$Lang::tr{'album'} |
$Lang::tr{'year'} |
$Lang::tr{'genre'} |
$Lang::tr{'length'} |
END
;
my $lines=0;my $i=0;my $begin;my $end;
if ( $mpfiresettings{'PAGE'} eq 'all' ){
$begin=0;
$end=keys(%songs);
}
else{
$begin=(($mpfiresettings{'PAGE'}-1) * 100);
$end=(($mpfiresettings{'PAGE'} * 100)-1);
}
foreach (keys(%songs)){
if (!($i >= $begin && $i <= $end)){
# print $begin."->".$i."<-".$end."\n";
$i++;next;}
my @song = split(/\|/,$songs{$_});
my $minutes = sprintf ("%.0f", $song[1] / 60 );
my $seconds = $song[1] % 60;
if ($lines % 2) {print "";} else {print "
";}
print <
$song[2] $song[3] |
$song[5] |
$song[4] |
$song[6] |
$song[7] |
$minutes:$seconds |
END
;
$lines++;
$i++;
}
print "
";
&Header::closebox();
}
&Header::openbox('100%', 'center', $Lang::tr{'mpfire playlist'});
my @playlist = `mpc playlist`;
print <
$Lang::tr{'current playlist'} |
|
|
|
END
;
&Header::closebox();
&Header::openbox('100%', 'center', $Lang::tr{'mpfire webradio'});
open(DATEI, "<${General::swroot}/mpfire/webradio") || die "Could not open playlist";
my @webradio = ;
close(DATEI);
print <
$Lang::tr{'webradio playlist'} |
Stream | |
END
;
foreach (@webradio){
my @stream = split(/\|/,$_);
print <$stream[1] |
|
END
;
}
print "";
&Header::closebox();
&Header::closebigbox();
&Header::closepage();