#!/usr/bin/perl
# Copyright (C) 2001 Thomax Kaulmann
# 
# 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 2, 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, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.


use CGI;
use DBI;
use ORA;

local %in;
#local $Hquery;
#$Hquery = new CGI();
#print $Hquery->header('text/html');
undef(%in);



                  ##       ##     ##
                  ##              ##
  #####   ####   #####    ###    #####
 ##  ##  ##  ##   ##       ##     ##
 ##  ##  ######   ##       ##     ##
  #####  ##       ## ##    ##     ## ##
     ##   ####     ###    ####     ###
 #####
sub getit
{
	local $st, $ret1, $ret2;

	$db = OMA::opendb();
	$st=$db->prepare("select ora_page,location from ora_conn where item_sid=$in{'item'}");
	$st->execute();
	($ret1,$ret2) = $st->fetchrow();
	$st->finish();
	return($ret1,$ret2);
}



                ###       ##                              ##
                 ##       ##                              ##
  ####   ##  ##  #####   #####  ## ###   ####     ####   #####
 ##      ##  ##  ##  ##   ##     ### ##     ##   ##  ##   ##
  ####   ##  ##  ##  ##   ##     ##      #####   ##       ##
     ##  ##  ##  ##  ##   ## ##  ##     ##  ##   ##  ##   ## ##
 #####    ##### ## ###     ###  ####     ### ##   ####     ###

sub subtract
{
	local $query, $cookie, $items, $old;
	$query = new CGI;

	($in{'it'},$in{'base'}) = getit() if($in{'item'} && !$in{'it'});

	$items = $query->cookie('oraItem');
	$items =~ s/$in{'it'}=$in{'base'}://;

	$x = $query->cookie('oraItem').":$in{'it'}";
	$cookie = $query->cookie(	-name=>'oraItem',
								-value=>$items,
								-expires=>'+730d'
								);

  print $query->header(-cookie=>$cookie);
  print "<body onload=\"location.href='$ORA::CGI_BINS/ora-cook?op=look'\" bgcolor=#ff661e>";


}


           ###     ###
            ##      ##
 ####    #####   #####
    ##  ##  ##  ##  ##
 #####  ##  ##  ##  ##
##  ##  ##  ##  ##  ##
 ### ##  ### ##  ### ##

sub add
{
	local $query, $cookie, $items, $old;
	$query = new CGI;

	($in{'it'},$in{'base'}) = getit() if($in{'item'} && !$in{'it'});

	$old = $query->cookie('oraItem');
	$items = "$in{'it'}=$in{'base'}";
	if(length($old) > 0) {
		$items .= ":$old";
	}

	$x = $query->cookie('oraItem').":$in{'it'}";
	$cookie = $query->cookie(	-name=>'oraItem',
								-value=>$items,
								-expires=>'+730d'
								);

  print $query->header(-cookie=>$cookie);
  print "<body onload=\"location.href='$ORA::CGI_BINS/ora-cook?op=look'\" bgcolor=#ff661e>";
}


          ###
           ##
  ####     ##     ####   ####   ## ###
 ##  ##    ##    ##  ##     ##   ### ##
 ##        ##    ######  #####   ##
 ##  ##    ##    ##     ##  ##   ##
  ####    ####    ####   ### ## ####

sub clear
{
	local $cookie, $query;

	$query = new CGI;
	$cookie = $query->cookie(
								-name=>'oraItem',
								-value=>':EOL',
	);

  print $query->header(-cookie=>$cookie);
  print "<body onload=\"location.href='$ORA::CGI_BINS/ora-cook?op=look'\" bgcolor=#ff661e>";

}


          ###
           ##
## ###     ##    ####    ##  ##
 ##  ##    ##       ##   ##  ##
 ##  ##    ##    #####   ##  ##
 #####     ##   ##  ##    #####
 ##       ####   ### ##      ##
####                     #####
sub play
{
	local ($query, $cookie, @it, $ret, %hashrow, $file);
	local $base, $id;
	local $pex;


	$query = new CGI;
	undef(@items);
	undef(%hashrow);

	$db = ORA::opendb();


	@items = split(/:/,$query->cookie('oraItem'));
#	print "Content-type: text/html\n\n<pre>";
#	print "$items[0]\n";

	($id,$base) = split(/=/,$items[0]);
	$ret = $db->prepare("SELECT * from ora_page where ID=$id AND location='$base'");
	$ret->execute();
	$ret->bind_columns(\(@hashrow{@{$st->{NAME_lc}}}));
	$ret->fetchrow();
	$ret->finish();

	if($hashrow{audio} =~ /\.rm$/) {
		print $query->header('audio/x-pn-realaudio');
	}
	else {
		print $query->header('audio/x-mpegurl');
	}
	for($i=0; $i <= $#items; $i++) {
		if(($items[$i] ne "EOL")) {
			($id,$base) = split(/=/,$items[$i]);
			$ret = $db->prepare("SELECT * from ora_page where ID=$id AND location='$base'");
			$ret->execute();
			$ret->bind_columns(\(@hashrow{@{$st->{NAME_lc}}}));
			$ret->fetchrow();
			$ret->finish();
            if($hashrow{audio} =~ /\.rm$/) {
                print "pnm://$base/$ORA::ORADATA/$id/$hashrow{audio}\n";
            }
            else {
                print "http://$base:8000/file/$ORA::ORADATA/$id/$hashrow{audio}\n";
            }
		}
	}
	print "\n";
}



  ###                   ###
   ##                    ##
   ##     ####    ####   ##  ##
   ##    ##  ##  ##  ##  ## ##
   ##    ##  ##  ##  ##  ####
   ##    ##  ##  ##  ##  ## ##
  ####    ####    ####  ###  ##

sub look
{
	local ($query, $cookie, @it, $ret, %hashrow, $file);
	local $id, $base;

	$query = new CGI;
	undef(@items);
	undef(%hashrow);

	$db = ORA::opendb();

	print "<html><head></head>$ORA::ORA_BODY3<font face=\"heveltica,verdana,arial\">";

	print "<b><img align=top src=http://$ORA::OMAHOST/img/ora.png>[ora]</b> <nobr><a href=$ORA::CGI_BINS/ora-cook?op=clear>clear all</a>\n";
	print "<table cellspacing=0 colspan=0>";
	@items = split(/:/,$query->cookie('oraItem'));
	for($i=0; $i <= $#items; $i++) {
		if($items[$i] ne "EOL" && $items[$i] ne "") {
			($id,$base) = split(/=/,$items[$i]);
			$ret = $db->prepare("SELECT * from ora_page where ID=$id AND location='$base'");
			$ret->execute();
			$ret->bind_columns(\(@hashrow{@{$ret->{NAME_lc}}}));
			$ret->fetchrow();
			print "<tr><td valign=top><a href=http://$ORA::HOST$ORA::CGI_BINS/ora-cook?op=sub&it=$id&base=$base><b>-</b></a></td><td><font size=-1><a target=f2 href=http://$ORA::HOST$ORA::CGI_BINS/ora-parse?id=$id&base=$base>$hashrow{dc__title}</a></td></tr>\n";
			$ret->finish();
		}
	}
	print "</table></font></body></html>";
}


##   ##            ##            ######         ##   ##
### ###           ####             ##           ###  ##
#######          ##  ##            ##           #### ##
#######          ##  ##            ##           ## ####
## # ##          ######            ##           ##  ###
##   ##          ##  ##            ##           ##   ##
##   ##          ##  ##          ######         ##   ##

CGI::ReadParse();
play() if($in{'op'} eq "play");
add() if($in{'op'} eq "add");
clear() if($in{'op'} eq "clear");
look() if($in{'op'} eq "look");
subtract() if($in{'op'} eq "sub");

