#!/usr/bin/perl use CGI qw(:standard); #name used to identify cookie $cookiename = "klink_info"; #username tab password, stored in cookie, checked on every screen $logininfo = "username\tpassword"; #rawfile generated $datafile = "/home/myuser/public_html/bookmarks/bookmarks"; #static HTML file used to display bookmarks after every transaction $outfile = "/home/myuser/public_html/bookmarks/bookmarks.html"; #static HTML file that just has bookmarks in "blank" category #(this category sorts first on the big list, and is useful for #"hot" bookmarks) $outhotfile = "/home/myuser/public_html/home/hotlinks.html"; #Location and name of this script $mainurl = "http://www.sitename.com/bookmarks/klink.cgi"; #Location and name of the output file $outurl = "http://www.sitename.com/bookmarks/bookmarks.html"; $userinfo = cookie($cookiename); $screen = param("screen"); $dologin = param("dologin"); if($dologin eq "on" || $dologin eq "off") { if($dologin eq "on") { $userinfo = param("username")."\t".param("password"); } else { $userinfo = ""; } $packed_cookie = cookie( -NAME => $cookiename, -VALUE => $userinfo, -EXPIRES => "+2y" ); print header(-COOKIE => $packed_cookie); } else { print header(); } print getTop(); if($userinfo eq $logininfo) { if($screen eq "formadd" || $screen eq "formedit") { if($screen eq "formadd") { getUsedcat(); $titler = "Add New"; $targeter = "doadd"; }else { getUsedcat(param("id")); $titler = "Edit"; $targeter = "doedit"; } print<<__EOQ__;

Klink - $titler

URL:

Title:

Note:

Category:

__EOQ__ } if($screen eq "doadd") { $url = param("url"); $name = param("name"); $note = param("note"); $cat = param("cat"); $id = time; addRecord($id,$cat,$url,$name,$note); print "Added Bookmark $name"; printoutPage(); printoutHot(); print<<__EOQ__; __EOQ__ } if($screen eq "doedit") { $url = param("url"); $name = param("name"); $note = param("note"); $cat = param("cat"); $id = param("id"); deleteRecord($id); addRecord($id,$cat,$url,$name,$note); print "Updated Bookmark $name"; printoutPage(); printoutHot(); print<<__EOQ__; __EOQ__ } if($screen eq "editmode" || $screen eq "") { printoutPage("editmode"); } if($screen eq "formdelete"){ $id=param("id"); getUsedcat($id); print<<__EOQ__;
Confirm Delete:
$oldtitle ($oldurl)
Confirm / Cancel
__EOQ__ } if($screen eq "dodelete") { $id = param("id"); deleteRecord($id); printoutPage(); printoutHot(); print<<__EOQ__; __EOQ__ } } else { print <<__EOQ__
K/link Login
Username:
Password:
__EOQ__ } sub getUsedcat { ($findid) = @_; open(READ,$datafile) || print "error, cannot read cats, $!";; while(defined($next=)){ ($readid,$cat,@etc) = split(/\t/,$next); $usedcat{$cat} = "on"; if($readid eq $findid) { ($oldid,$oldcat,$oldurl,$oldname,$oldnote) = split(/\t/,$next); } } } sub printoutHot { open(READ,$datafile) || print "couldn't open"; open(WRITE,"> $outhotfile"); while(defined($next=)){ chomp $next; ($id,$cat,$url,$title,$desc) = split(/\t/,$next); if($cat eq "") { $thisun = <<__EOQ__; $title __EOQ__ if($desc ne "") { $thisun .= " - $desc"; } $thisun .= "
\n"; print WRITE $thisun; } } close WRITE; close READ; } sub printoutPage { ($type)=@_; if($type eq "editmode") { $handle = *STDOUT; } else { open(OUT,"> $outfile") || print "Can't write out: $!"; $handle = *OUT; $headtop = getTop(); print $handle $headtop; } print $handle <<__EOQ__; K/link __EOQ__ $rmsg = param("rmsg"); if($rmsg ne "") { $rmsg = " Reload "; } if($type eq "editmode") { print $handle <<__EOQ__; - Edit Mode [[View Mode$rmsg]] [[Decookie]] __EOQ__ } else { print $handle <<__EOQ__; [[Edit Mode]] __EOQ__ } print $handle <<__EOQ__; [[Add New]]
__EOQ__ open(READ,$datafile) || print "couldn't open"; while(defined($next=)){ chomp $next; ($id,$cat,@etc) = split(/\t/,$next); $mark{$id} = $next; $cats{$cat} .= "$id\t"; } foreach $cat (sort keys %cats) { print $handle "$cat
"; @entries = split(/\t/,$cats{$cat}); foreach $entry (sort @entries) { $line = $mark{$entry}; ($id,$cat,$url,$title,$desc) = split(/\t/,$line); if($type eq "editmode") { print $handle <<__EOQ__; [X] $title [peek] __EOQ__ } else { print $handle <<__EOQ__;       $title __EOQ__ } if($desc ne "") { print $handle " - $desc"; } print $handle "
\n"; } } if($type eq ""){ close OUT; } } sub deleteRecord { ($killid) = @_; open(READ,$datafile) ; open(WRITE, "> $datafile.temp"); while(defined($next=)){ chomp $next; ($thisid,@rest) = split(/\t/,$next); if($thisid ne $killid) { print WRITE "$next\n"; } else { open(DEL,">> $datafile.killed"); print DEL "$next\n"; close DEL; } } close READ; close WRITE; print `cp $datafile.temp $datafile`; } sub addRecord{ my($id,$cat,$url,$name,$note) = @_; open(WRITE,">> $datafile") || print "Couldn't open bookmarks: $!"; print WRITE "$id\t$cat\t$url\t$name\t$note\n"; close WRITE; } sub getTop{ return<<__EOQ__; K/link __EOQ__ }