#!/usr/local/bin/perl $my_url='http://sydney.colorado.edu/~dixonj/restricted/cdlist.cgi'; $database_file='/home/sydney/dixonj/cdlist/joncd2'; $tmpfile='/home/sydney/dixonj/cdlist/cdtemp'; #BEGIN { # unshift(@INC,'/home/obelisk/dixonj/CGI.pm-2.36'); # } use CGI; use Fcntl ':flock'; $query = new CGI; print $query->header; print $query->start_html(-title=>'CD List Searcher', -author=>'dixonj@colorado.edu'); if (!$query->param('Action')) { print "

Welcome to Jon's CD Database\n"; print "

"; } else { my($act)=$query->param('Action'); if ( $act =~ /search/ ) { &do_search($query) } elsif ( $act =~ /add/ ) { &do_add($query) } elsif ( $act =~ /modify/ ) { &do_modify($query) } elsif ( $act =~ /delete/ ) { &do_delete($query) } else { print "That is not a valid option.
" } } print $query->end_html; # subroutines # print_search sub print_search { my($q) = @_; print "Type search terms:", "

", $q->textarea(-name=>'searchkeys', -rows=>3,-cols=>50), "

", $q->em("Search for:"), $q->checkbox_group(-name=>'searchtype', -values=>['piece','title','artist'], -default=>'piece', -labels=>{ 'piece'=>'Partial piece names', 'title'=>'Partial title names', 'artist'=>'Artist name' } ), "

", $q->checkbox(-name=>'intersection', -label=>'AND search terms together'); } # do_search sub do_search { my ($q) = @_; # if ($q->param('dosearch')) { my($search,@keywords,@hits,$entries,$i, $keycount,$filename,$count,$take_intersection); local (@title,@artist,@piece,@count); @keywords = split(/[\s,]+/,$q->param('searchkeys')); unless (@keywords) { print "No keywords specified!\n"; return; } $search= join(' ', $q->param('searchtype')); unless ($search) { print "Please specify the type of search to perform!\n"; return; } $take_intersection++ if $q->param('intersection'); $entries = &lookup($take_intersection,$search,@keywords); if (not $entries) { print "No matches found for @keywords.\n"; return;} for ($i=0; $i < $entries; $i++) {$hits[$i]=$i} if ($q->param('sort')=~/score/) { @hits = sort { $count[$b]<=>$count[$a] || $b <=> $a; } @hits; } elsif ($q->param('sort')=~/title/) {@hits = sort bytitle @hits } else { @hits = sort byartist @hits } # Start an ordered list print("$entries matches were found for @keywords.\n"); print "

    \n"; $i= 0; foreach $num (@hits){ $i++; print <(Match $i of $entries)
    Title: $title[$num]
    Artist: $artist[$num]
    Tracks: $piece[$num]

    END } print "


\n"; print "Click
here to return to main menu." } else { print "

Search Form

"; print $q->start_form; &print_search($q); print $q->hidden('Action','search'); print "

", "Sort results by:", $q->popup_menu(-name=>'sort', -values=>['title','artist']); print "

", $q->reset, $q->submit(-name=>'dosearch',-value=>'Search'), $q->end_form; } } # end do_search sub bytitle { $title[$a] cmp $title[$b] } sub byartist { $artist[$a] cmp $artist[$b] } sub readrecord{ my($i)= 0; my ($title,$artist,$piece,$number); my ($intrack) = 0; while(){ if (/^\s*$/ && $title) {return ($title,$artist,$piece,$number)} chomp; s/~ +/~/g; ($key,$val)= split('~'); if (!$val && $intrack) {$piece .= $key ; $key = ""} else { $intrack = 0} # if ($val) {$val =~ s/ +/ /g;} if($key=~/TITLE/) { $title= $val; } elsif($key=~/PERFORMER/) { $artist= $val; } elsif($key=~/TRACKS/) { $piece= $val; $intrack = 1} elsif($key=~/NUMBER/) { $number = $val;} } if ($number) {return ($title,$artist,$piece,$number);} else {return "";} }# end readrecord sub lookup{ my($take_intersection, $search, @keywords)= @_; my ($temptitle,$tempartist,$temppiece); my ($i) = 0; open (DBIN,$database_file) || webdie ("Cannot open $database_file: $! \n"); flock (DBIN,LOCK_EX); ($temptitle,$tempartist,$temppiece,$tempnum) = &readrecord; while ( $temptitle || $tempartist || $temppiece || $tempnum ) { #while ( ($temptitle,$tempartist,$temppiece) = &readrecord ) { $hit=0; foreach $keyword (@keywords){ if( ($search =~ /title/ && $temptitle =~ /$keyword/i) || ($search =~ /artist/ && $tempartist =~ /$keyword/i) || ($search =~ /piece/ && $temppiece =~ /$keyword/i) ) { $hit++; } } if($take_intersection){ $keycount=scalar(@keywords); if ($hit eq $keycount) { $score[$i]=$hit; $title[$i]=$temptitle; $artist[$i]=$tempartist; $cdnum[$i]=$tempnum; $piece[$i++]=$temppiece; } } elsif ($hit) { $score[$i]=$hit; $title[$i]=$temptitle; $artist[$i]=$tempartist; $cdnum[$i]=$tempnum; $piece[$i++]=$temppiece; } ($temptitle,$tempartist,$temppiece, $tempnum) = &readrecord; } flock (DBIN,LOCK_UN); close (DBIN); return($i); } # end lookup sub add_form { my ($q,$title,$artist,$piece) = @_; print "

Title: ",$q->textfield(-name=>'addtitle', -rows=>1,-cols=>50, -default=>$title); print "

Artist: ",$q->textfield(-name=>'addartist', -rows=>1,-cols=>50, -default=>$artist); print "

Tracks: ",$q->textarea(-name=>'addpiece', -rows=>5,-cols=>60, -default=>$piece); } # end add_form sub do_add { my ($q) = @_; if ($q->param('doadd')) { my ($title) = $q->param('addtitle'); my ($artist) = $q->param('addartist'); my ($piece) = $q->param('addpiece'); $piece =~ s/\n//g; $piece =~ s/\cM//g; open (DBIN,$database_file) || webdie ("Cannot open $database_file: $! \n"); flock (DBIN,LOCK_EX); my ($maxnum)=0; while ( my ($temptitle,$tempartist,$temppiece,$tempnum) = &readrecord ) { # while ( $temptitle || $tempartist || $temppiece ||$tempnum ) { if (!$tempnum) {last} if ($tempnum>$maxnum) {$maxnum=$tempnum} if (($temptitle eq $title) && ($tempartist eq $artist) && ($temppiece eq $piece) ) { print "This CD is already in the database"; flock (DBIN,LOCK_UN); close (DBIN); return; } # ($temptitle,$tempartist,$temppiece,$tempnum) = &readrecord; } flock (DBIN,LOCK_UN); close (DBIN); $maxnum+=1; open (DBOUT,">>$database_file") || webdie ("Cannot open $database_file: $! \n"); flock (DBOUT,LOCK_EX); print DBOUT "\nNUMBER~ $maxnum\n"; print DBOUT "TITLE~ $title\n"; print DBOUT "PERFORMER~ $artist\n"; print DBOUT "TRACKS~ $piece\n"; flock (DBOUT,LOCK_UN); close (DBOUT); print "CD added to database. "; print "Click here to return to main menu." } else { print "

Add a New Entry

"; print $q->start_form; &add_form($q); print $q->hidden('Action','add'); print "

", $q->reset, $q->submit(-name=>'doadd',-value=>'Add CD'), $q->end_form; } } # end do_add sub do_modify { my ($q)=@_; if ($q->param('modify_act')) { my ($title) = $q->param('addtitle'); my ($artist) = $q->param('addartist'); my ($piece) = $q->param('addpiece'); my ($cdnum) =$q->param('cdnum'); $piece =~ s/\n//g; $piece =~ s/\cM//g; open (DBIN,$database_file) || webdie ("Cannot open $database_file: $! \n"); flock (DBIN,LOCK_EX); open (DBOUT,">$tmpfile") || webdie ("Cannot open $tmpfile :$! \n"); flock (DBOUT,LOCK_EX); while( ($temptitle,$tempartist,$temppiece,$tempnum) = &readrecord) { if (!$tempnum) {last} if ($tempnum == $cdnum) { print DBOUT "\nNUMBER~ $cdnum\n"; print DBOUT "TITLE~ $title\n"; print DBOUT "PERFORMER~ $artist\n"; print DBOUT "TRACKS~ $piece\n"; } else { print DBOUT "\nNUMBER~ $tempnum\n"; print DBOUT "TITLE~ $temptitle\n"; print DBOUT "PERFORMER~ $tempartist\n"; print DBOUT "TRACKS~ $temppiece\n"; } # ($temptitle,$tempartist,$temppiece) = &readrecord; } flock (DBIN,LOCK_UN); close (DBIN); flock (DBOUT,LOCK_UN); close (DBOUT); webdie("Problem in rename : $!") unless rename $tmpfile, $database_file; chmod 0666, $database_file || webdie("Problem in chmod : $!"); print "Changes recorded in database. "; print "Click here to return to main menu." } elsif ($q->param('modify2')) { print "

Make Modifications

\n"; print $q->start_form; my($number) = $q->param('modnum'); open (DBIN,$database_file) || webdie ("Cannot open $database_file: $! \n"); flock (DBIN,LOCK_EX); my ($temptitle,$tempartist,$temppiece,$tempnum); while( ($temptitle,$tempartist,$temppiece,$tempnum) = &readrecord) { if (!$tempnum) {last} if ($tempnum == $number) {last} } flock (DBIN,LOCK_UN); close (DBIN); &add_form($q,$temptitle,$tempartist,$temppiece); print $q->hidden('cdnum',"$number"); print $q->hidden('Action','modify'); print "

", $q->reset, $q->submit(-name=>'modify_act',-value=>'Change'), $q->end_form; } elsif ($q->param('modify1')) { print "

Choose Item to Modify

\n"; my($search,@keywords,@hits,$entries,$i, $keycount,$filename,$count,$take_intersection); local (@title,@artist,@piece,@count); @keywords = split(/[\s,]+/,$q->param('searchkeys')); unless (@keywords) { print "No keywords specified!\n"; return; } $search= join(' ', $q->param('searchtype')); unless ($search) { print "Please specify the type of search to perform!\n"; return; } $take_intersection++ if $q->param('intersection'); $entries = &lookup($take_intersection,$search,@keywords); if (not $entries) { print "No matches found for @keywords.\n"; return;} for ($i=0; $i < $entries; $i++) {$hits[$i]=$i} if ($q->param('sort')=~/score/) { @hits = sort { $count[$b]<=>$count[$a] || $b <=> $a; } @hits; } elsif ($q->param('sort')=~/title/) {@hits = sort bytitle @hits } else { @hits = sort byartist @hits } print("$entries matches were found for @keywords.\n"); print "
    \n"; print $query->start_form; print "\n"; $i= 0; foreach $num (@hits){ $i++; print < END } print "
    Title: $title[$num] Artist: $artist[$num]
    Tracks: $piece[$num]
    \n"; print $query->hidden('Action','modify'); print $query->submit(-name=>'modify2',-value=>'Modify this item'); print $query->end_form; print $query->start_form(-action=>$my_url); print $query->submit(-name=>'modify_abort',-value=>'Abort Modification'); print $query->end_form; } else { print "

    Modify an Entry

    "; print $q->start_form; &print_search($q); print $q->hidden('Action','modify'); print "

    ", $q->reset, $q->submit(-name=>'modify1',-value=>'Search'), $q->end_form; } } # end sub do_modify sub do_delete { my ($q)=@_; if ($q->param('delete_act')) { my ($title) = $q->param('addtitle'); my ($artist) = $q->param('addartist'); my ($piece) = $q->param('addpiece'); my ($cdnum) =$q->param('modnum'); $piece =~ s/\n//g; $piece =~ s/\cM//g; open (DBIN,$database_file) || webdie ("Cannot open $database_file: $! \n"); flock (DBIN,LOCK_EX); open (DBOUT,">$tmpfile") || webdie ("Cannot open $tmpfile :$! \n"); flock (DBOUT,LOCK_EX); while( ($temptitle,$tempartist,$temppiece,$tempnum) = &readrecord) { if (!$tempnum) {last} if ($tempnum != $cdnum) { print DBOUT "\nNUMBER~ $tempnum\n"; print DBOUT "TITLE~ $temptitle\n"; print DBOUT "PERFORMER~ $tempartist\n"; print DBOUT "TRACKS~ $temppiece\n"; } # ($temptitle,$tempartist,$temppiece) = &readrecord; } flock (DBIN,LOCK_UN); close (DBIN); flock (DBOUT,LOCK_UN); close (DBOUT); webdie("Problem in rename : $!") unless rename $tmpfile, $database_file; chmod 0666, $database_file || webdie("Problem in chmod : $!"); print "Item deleted from database. "; print "Click here to return to main menu." } elsif ($q->param('delete1')) { print "

    Choose Item to Delete

    \n"; my($search,@keywords,@hits,$entries,$i, $keycount,$filename,$count,$take_intersection); local (@title,@artist,@piece,@count); @keywords = split(/[\s,]+/,$q->param('searchkeys')); unless (@keywords) { print "No keywords specified!\n"; return; } $search= join(' ', $q->param('searchtype')); unless ($search) { print "Please specify the type of search to perform!\n"; return; } $take_intersection++ if $q->param('intersection'); $entries = &lookup($take_intersection,$search,@keywords); if (not $entries) { print "No matches found for @keywords.\n"; return;} for ($i=0; $i < $entries; $i++) {$hits[$i]=$i} if ($q->param('sort')=~/score/) { @hits = sort { $count[$b]<=>$count[$a] || $b <=> $a; } @hits; } elsif ($q->param('sort')=~/title/) {@hits = sort bytitle @hits } else { @hits = sort byartist @hits } print("$entries matches were found for @keywords.\n"); print "
      \n"; print $query->start_form; print "\n"; $i= 0; foreach $num (@hits){ $i++; print < END } print "
      Title: $title[$num] Artist: $artist[$num]
      Tracks: $piece[$num]
      \n"; print $query->hidden('Action','delete'); print $query->submit(-name=>'delete_act',-value=>'Delete this item'); print $query->end_form; print $query->start_form(-action=>$my_url); print $query->submit(-name=>'delete_abort',-value=>'Abort Deletion'); print $query->end_form; } else { print "

      Search for Entry to Delete

      "; print $q->start_form; &print_search($q); print $q->hidden('Action','delete'); print "

      ", $q->reset, $q->submit(-name=>'delete1',-value=>'Search'), $q->end_form; } } # end sub do_delete sub webdie { my ($txt) =@_; print $txt; print $query->end_html; die ($txt); } #end webdie

      Make your own free website on Tripod.com