From: Banana Date: Wed, 15 Jun 2011 09:30:34 +0000 (+0200) Subject: code cleanup X-Git-Tag: 2.1-alpha-2019-0-29~127 X-Git-Url: http://91.132.146.200/gitweb/?a=commitdiff_plain;h=fc92a8985b2d799ea6687db661c8c28d18d6b6a1;p=insipid.git code cleanup --- diff --git a/lib/Insipid/Main.pm b/lib/Insipid/Main.pm index 724ffda..47d16ef 100755 --- a/lib/Insipid/Main.pm +++ b/lib/Insipid/Main.pm @@ -1,1169 +1,1239 @@ -#!/usr/bin/perl -# -# Copyright (C) 2008 Luke Reeves -# -# 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 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, write to the Free Software -# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 -# USA -# - -package Insipid::Main; - -use warnings; -use strict; - -use vars qw(@ISA @EXPORT @EXPORT_OK); -require Exporter; - -@ISA = qw(Exporter); - -@EXPORT = qw(main); - -use Insipid::Config; -use Insipid::Database; -use Insipid::Bookmarks; -use Insipid::RSS; -use Insipid::JSON; -use Insipid::Sessions; -use Insipid::Snapshots; -use Insipid::Tags; -use Insipid::Util; - -use CGI qw/:standard/; -use CGI::Carp qw(fatalsToBrowser); -use URI::Escape; -use IO::File; -use XML::Parser; -use XML::Writer; -use Date::Format; -use Date::Parse; -use DBI qw/:sql_types/;; -use Digest::MD5 qw(md5 md5_hex); -use MIME::Base64; -use LWP::UserAgent; -use HTTP::Request; -use HTTP::Response; - -my $NL = "
\n"; -my @valid; -my $icount = 0; -my %options; -my $tspec = ""; -my $query = ""; -my $last_page = 0; -my $site_title; - -if(!defined($ENV{SERVER_NAME})) { - $NL = "\n"; -} - -sub main { - - my $username = getconfig('username'); - my $userpass = getconfig('userpass'); - - my $redirect = ''; - my $et = ''; - - # Valid options: - @valid = ('feed_name', 'site_name', 'public_searches', - 'use_rewrite', 'proxy_host', 'proxy_port'); - - # Get the basic options - $site_title = get_option('site_name'); - if($site_title eq '') { - $site_title = 'Insipid Bookmarks'; - } - - # Initialize variables that can be posted and in the URL. - if(defined(url_param('q'))) { - $query = url_param('q'); - } - - if(defined(param('q'))) { - $query = param('q'); - } - - # Check to see if a username and password have been posted - if(defined(param('password')) && defined(param('username'))) { - if( (param('password') eq $userpass) && (param('username') eq $username) ) { - my $rv = login(); - print $rv; - } else { - push(@errors, "Invalid username or password."); - } - } - - # Operations for non-HTML content - - if(defined(url_param('op'))) { - if(url_param('op') eq 'export') { - my $sn = 'n'; - if(defined(param('snapshots'))) { - $sn = 'y'; - } - - do_export($sn); - } - - if(defined(param('op'))) { - if(logged_in() eq 1) { - if(param('op') eq 'logout') { - my $rv = logout(); - print $rv; - } - } - } - - # RSS - if(url_param('op') eq 'rss') { - print "Content-Type: text/xml\r\n\r\n"; - send_rss(); - exit; - } - - # JSON - # JSON Show tags: - if(url_param('op') eq 'json_tags') { - print "Content-Type: application/x-javascript;charset=UTF-8\r\n\r\n"; - send_json_tags(); - exit; - } - # JSON Show bookmarks: - if(url_param('op') eq 'json_posts') { - print "Content-Type: application/x-javascript;charset=UTF-8\r\n\r\n"; - send_json_posts(); - exit; - } - - # Cache - if(url_param('op') eq 'viewsnapshot') { - check_access(); - if(param('md5')) { - show_snapshot(param('md5')); - } - } - } - - # Allow redirections to a bookmark if the user's logged in. - # This allows private bookmarks to not send a referer. - if(logged_in() eq 1) { - if(param('go')) { - my $bid = param('go'); - my $sql = "select url from $tbl_bookmarks - where ($tbl_bookmarks.id = ?)"; - my $sth = $dbh->prepare($sql); - $sth->execute($bid); - my $hr = $sth->fetchrow_hashref; - if(defined($hr->{'url'})) { - print "Cache-Control: private, must-revalidate\n"; - print "Content-Type: text/html; charset=UTF-8\n\n"; - print "{'url'}\">\n"; - exit; - } else { - push(@errors, "Bookmark does not exist."); - } - } - } - - # Add description to the HTML title tag. - if(url_param('tag')) { - $tspec = "/" . url_param('tag'); - $tspec =~ s/ /\+/g; - my $tt = url_param('tag'); - $tt =~ s/ / \+ /g; - $et = sprintf(" - %s", $tt); - } - if($query ne "") { - $et = sprintf(" - search results for \"%s\"", $query); - } - - if(logged_in() eq 1) { - print "Cache-Control: private, must-revalidate\n"; - } - - print "Content-Type: text/html; charset=UTF-8\n\n"; - - - print < - - $site_title$et - - - +#!/usr/bin/perl +# +# Copyright (C) 2008 Luke Reeves +# +# 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 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, write to the Free Software +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 +# USA +# + +package Insipid::Main; + +use warnings; +use strict; + +use vars qw(@ISA @EXPORT @EXPORT_OK); +require Exporter; + +@ISA = qw(Exporter); + +@EXPORT = qw(main); + +use Insipid::Config; +use Insipid::Database; +use Insipid::Bookmarks; +use Insipid::RSS; +use Insipid::JSON; +use Insipid::Sessions; +use Insipid::Snapshots; +use Insipid::Tags; +use Insipid::Util; + +use CGI qw/:standard/; +use CGI::Carp qw(fatalsToBrowser); +use URI::Escape; +use IO::File; +use XML::Parser; +use XML::Writer; +use Date::Format; +use Date::Parse; +use DBI qw/:sql_types/; +use Digest::MD5 qw(md5 md5_hex); +use MIME::Base64; +use LWP::UserAgent; +use HTTP::Request; +use HTTP::Response; + +my $NL = "
\n"; +my @valid; +my $icount = 0; +my %options; +my $tspec = ""; +my $query = ""; +my $last_page = 0; +my $site_title; + +if ( !defined( $ENV{SERVER_NAME} ) ) { + $NL = "\n"; +} + +sub main { + + my $username = getconfig('username'); + my $userpass = getconfig('userpass'); + + my $redirect = ''; + my $et = ''; + + # Valid options: + @valid = ( + 'feed_name', 'site_name', 'public_searches', 'use_rewrite', + 'proxy_host', 'proxy_port' + ); + + # Get the basic options + $site_title = get_option('site_name'); + if ( $site_title eq '' ) { + $site_title = 'Insipid Bookmarks'; + } + + # Initialize variables that can be posted and in the URL. + if ( defined( url_param('q') ) ) { + $query = url_param('q'); + } + + if ( defined( param('q') ) ) { + $query = param('q'); + } + + # Check to see if a username and password have been posted + if ( defined( param('password') ) && defined( param('username') ) ) { + if ( ( param('password') eq $userpass ) + && ( param('username') eq $username ) ) + { + my $rv = login(); + print $rv; + } + else { + push( @errors, "Invalid username or password." ); + } + } + + # Operations for non-HTML content + + if ( defined( url_param('op') ) ) { + if ( url_param('op') eq 'export' ) { + my $sn = 'n'; + if ( defined( param('snapshots') ) ) { + $sn = 'y'; + } + do_export($sn); + } + + if ( defined( param('op') ) ) { + if ( logged_in() eq 1 ) { + if ( param('op') eq 'logout' ) { + my $rv = logout(); + print $rv; + } + } + } + + # RSS + if ( url_param('op') eq 'rss' ) { + print "Content-Type: text/xml\r\n\r\n"; + send_rss(); + exit; + } + + # JSON + # JSON Show tags: + if ( url_param('op') eq 'json_tags' ) { + print + "Content-Type: application/x-javascript;charset=UTF-8\r\n\r\n"; + send_json_tags(); + exit; + } + + # JSON Show bookmarks: + if ( url_param('op') eq 'json_posts' ) { + print + "Content-Type: application/x-javascript;charset=UTF-8\r\n\r\n"; + send_json_posts(); + exit; + } + + # Cache + if ( url_param('op') eq 'viewsnapshot' ) { + check_access(); + if ( param('md5') ) { + show_snapshot( param('md5') ); + } + } + } + + # Allow redirections to a bookmark if the user's logged in. + # This allows private bookmarks to not send a referer. + if ( logged_in() eq 1 ) { + if ( param('go') ) { + my $bid = param('go'); + my $sql = "select url from $tbl_bookmarks + where ($tbl_bookmarks.id = ?)"; + my $sth = $dbh->prepare($sql); + $sth->execute($bid); + my $hr = $sth->fetchrow_hashref; + if ( defined( $hr->{'url'} ) ) { + print "Cache-Control: private, must-revalidate\n"; + print "Content-Type: text/html; charset=UTF-8\n\n"; + print + "{'url'}\">\n"; + exit; + } + else { + push( @errors, "Bookmark does not exist." ); + } + } + } + + # Add description to the HTML title tag. + if ( url_param('tag') ) { + $tspec = "/" . url_param('tag'); + $tspec =~ s/ /\+/g; + my $tt = url_param('tag'); + $tt =~ s/ / \+ /g; + $et = sprintf( " - %s", $tt ); + } + if ( $query ne "" ) { + $et = sprintf( " - search results for \"%s\"", $query ); + } + + if ( logged_in() eq 1 ) { + print "Cache-Control: private, must-revalidate\n"; + } + + print "Content-Type: text/html; charset=UTF-8\n\n"; + + print < + + $site_title$et + + + - - -DOC - - - ###### Operations that don't touch the screen - if(defined(param('op')) && defined(param('id'))) { - if(param('op') eq 'delete_bookmark') { - my $id = param('id'); - delete_bookmark($id); - } - } - - - # If the user just saved a bookmark, redirect them now. - if($redirect ne "") { - print ""; - print ""; - exit; - } - - show_toolbar(); - show_tags(); - - print '
'; - - if(defined(url_param('op'))) { - if(url_param('op') eq 'export') { - if(!defined(param('target'))) { - print "

"; - print "Include Snapshots
"; - print "
"; - } - } - - if(url_param('op') eq 'import') { - check_access(); - if(param('fileupload')) { - do_import(); - } else { - print <This allows you to import either - Insipid or - del.icio.us backups. For del.icio.us, you - must first use their API to export your bookmarks to an XML file. To do this, - access the URL "http://username:password\@del.icio.us/api/posts/all?" - (using your username and password). You can then upload that XML file here. -

-
-
- Import from:
-
- - -
-IFORM - } - } - } - - if(defined(param('op'))) { - if(param('op') eq 'login') { - login_form(); - } - - if((param('op') eq 'add_bookmark') || (param('op') eq 'edit_bookmark') ) { - #check to see if the url is bookmarked, then indicate that this is an edit. - my ($id, $url, $title, $description, $button, - $tags, $extra_params, $access_level, $access_box) = - (-1, "", "", "", "", "", "", 0, ""); - - $access_level = 0; - - if(defined(param('save'))) { - ($url, $title, $description, $tags) = - (param('url'), param('title'), param('description'), param('tags')); - - if(defined(param('access_level'))) { - if(param('access_level') eq 'on') { - $access_level = 1; - } else { - $access_level = 0; - } - } - - if(param('id')) { - update_bookmark(param('id'), $url, $title, $description, $access_level, $tags); - } else { - add_bookmark($url, $title, $description, $access_level, 0, $tags); - if(param('snapshot')) { - if(param('snapshot') eq 'on') { - $id = get_bookmark_id(param('url')); - do_snapshot($id); - } - } - } - - if(param('redirect')) { - if(param('redirect') eq 'on') { - if(@errors eq 0) { - $redirect = $url; - } - } - } - } else { - # Show the form, populating from the database if it's an existing entry. - my $utext = "URL:"; - my $snapshot_params = ""; - $id = "-1"; - - if(defined(param('id'))) { $id = param('id'); } - if(defined(url_param('id'))) { $id = url_param('id'); } - - if($id eq "-1") { - if(defined(param('url'))) { - $id = get_bookmark_id(param('url')); - } - } - - if($id ne -1) { - ($url, $title, $description, $access_level) = get_bookmark($id); - $tags = get_tags($url); - $button = "Save"; - $utext = "URL (already bookmarked):"; - $extra_params = ""; - } else { - # There has to be a nicer way to do this. - if(param('url')) { $url = param('url'); } - if(param('title')) { $title = param('title'); } - if(param('description')) { $description = param('description'); } - $access_level = 1; - $button = "Add"; - $snapshot_params = "Snapshot:\n"; - } - - my $style = "style=\"width:500px\""; - my $redir = "off"; - my $redir_box = ""; - - if(param('redirect')) { - if(param('redirect') eq 'on') { $redir = 'on'; } - if(param('redirect') eq 'true') { $redir = 'on'; } - } - - if($access_level eq 0) { $access_box = ""; } - else { $access_box = "checked=\"true\" "; } - - if($redir eq 'on') { $redir_box = "checked=\"true\""; } - - print < + +DOC + + ###### Operations that don't touch the screen + if ( defined( param('op') ) && defined( param('id') ) ) { + if ( param('op') eq 'delete_bookmark' ) { + my $id = param('id'); + delete_bookmark($id); + } + } + + # If the user just saved a bookmark, redirect them now. + if ( $redirect ne "" ) { + print +""; + print ""; + exit; + } + + show_toolbar(); + show_tags(); + + print '
'; + + if ( defined( url_param('op') ) ) { + if ( url_param('op') eq 'export' ) { + if ( !defined( param('target') ) ) { + print "

"; + print "Include Snapshots
"; + print "
"; + } + } + + if ( url_param('op') eq 'import' ) { + check_access(); + if ( param('fileupload') ) { + do_import(); + } + else { + print <This allows you to import either + Insipid or + del.icio.us backups. For del.icio.us, you + must first use their API to export your bookmarks to an XML file. To do this, + access the URL "http://username:password\@del.icio.us/api/posts/all?" + (using your username and password). You can then upload that XML file here. +

+
+
+ Import from:
+
+ + +
+IFORM + } + } + } + + if ( defined( param('op') ) ) { + if ( param('op') eq 'login' ) { + login_form(); + } + + if ( ( param('op') eq 'add_bookmark' ) + || ( param('op') eq 'edit_bookmark' ) ) + { + + if ( logged_in() ne 1 ) { + push( @errors, + "You have to be logged in to perform that operation." ); + + #return; + } + + #check to see if the url is bookmarked, then indicate that this is an edit. + my ( + $id, $url, $title, + $description, $button, $tags, + $extra_params, $access_level, $access_box + ) = ( -1, "", "", "", "", "", "", 0, "" ); + + $access_level = 0; + + if ( defined( param('save') ) ) { + ( $url, $title, $description, $tags ) = ( + param('url'), param('title'), + param('description'), param('tags') + ); + + if ( defined( param('access_level') ) ) { + if ( param('access_level') eq 'on' ) { + $access_level = 1; + } + else { + $access_level = 0; + } + } + + if ( param('id') ) { + update_bookmark( param('id'), $url, $title, $description, + $access_level, $tags ); + } + else { + add_bookmark( $url, $title, $description, $access_level, 0, + $tags ); + if ( param('snapshot') ) { + if ( param('snapshot') eq 'on' ) { + $id = get_bookmark_id( param('url') ); + do_snapshot($id); + } + } + } + + if ( param('redirect') ) { + if ( param('redirect') eq 'on' ) { + if ( @errors eq 0 ) { + $redirect = $url; + } + } + } + } + else { + + # Show the form, populating from the database if it's an existing entry. + my $utext = "URL:"; + my $snapshot_params = ""; + $id = "-1"; + + if ( defined( param('id') ) ) { $id = param('id'); } + if ( defined( url_param('id') ) ) { $id = url_param('id'); } + + if ( $id eq "-1" ) { + if ( defined( param('url') ) ) { + $id = get_bookmark_id( param('url') ); + } + } + + if ( $id ne -1 ) { + ( $url, $title, $description, $access_level ) = + get_bookmark($id); + $tags = get_tags($url); + $button = "Save"; + $utext = "URL (already bookmarked):"; + $extra_params = ""; + } + else { + + # There has to be a nicer way to do this. + if ( param('url') ) { $url = param('url'); } + if ( param('title') ) { $title = param('title'); } + if ( param('description') ) { + $description = param('description'); + } + $access_level = 1; + $button = "Add"; + $snapshot_params = "Snapshot:\n"; + } + + my $style = "style=\"width:500px\""; + my $redir = "off"; + my $redir_box = ""; + + if ( param('redirect') ) { + if ( param('redirect') eq 'on' ) { $redir = 'on'; } + if ( param('redirect') eq 'true' ) { $redir = 'on'; } + } + + if ( $access_level eq 0 ) { $access_box = ""; } + else { $access_box = "checked=\"true\" "; } + + if ( $redir eq 'on' ) { $redir_box = "checked=\"true\""; } + + print < -
-
- $utext
-
- Title:
-
- Description:
-
- Tags:
-
+
+ + $utext
+
+ Title:
+
+ Description:
+
+ Tags:
+
    - $snapshot_params - Public: - - Return: - - - - $extra_params - -
    -FORM - } - } - } - - # Late redirects. TODO: Get rid of this. - if($redirect ne "") { - print ""; - print ""; - exit; - } - - - if(defined(param('op'))) { - if(logged_in() eq 1) { - if(param('op') eq 'fetchrelated') { - if(defined(param('id'))) { - fetch_related(param('id')); - } - } - - if(param('op') eq 'snapshots') { - show_snapshots(); - print ""; - exit; - } - - if(param('op') eq 'snapshot') { - if(defined(param('id'))) { - do_snapshot(param('id')); - print ""; - exit; - } - } - - if(param('op') eq 'bookmarklets') { - print <This bookmarklet provides a fast way to add your browser's -current page to this Insipid installation. Either drag the -following link to your bookmarks toolbar or right-click on it -and choose "Bookmark This Link..." to create a bookmarklet. -Then when you're on a page you'd like to save, click on your -new "Add to Insipid" button and you'll be brought to a page -that allows you to fill out the tags for the bookmark and save -it. Once you've clicked Save you'll be brought back to the -page.

    -DESC - my $ad = <
  • Add to Insipid
  • "; - print ""; - exit; - } - - # Configuration and management pages - if(param('op') eq 'tags') { - tag_operations(); - print ''; - exit; - } - - if(param('op') eq 'options') { - show_options(); - print ''; - exit; - } - } - } - - foreach (@errors) { - print "
    $_
    "; - } - - show_bookmarks(); - - print "

    "; -} # main - - - -################################################################ - -sub show_options { - # Save options if they were posted. - print "

    "; - if(param('save')) { - my $sql = "update $tbl_options set value=? - where (name = ?)"; - my $sth = $dbh->prepare($sql); - - my %save; - foreach my $p (@valid) { - if(param($p)) { - $save{$p} = param($p); - } - } - - foreach my $k (keys %save) { - $sth->execute($save{$k}, $k); - } - - # The proxy_host can be empty, so check for that. - if(!defined($save{'proxy_host'})) { - $sth->execute('', 'proxy_host'); - } - - print "
    Your options have been saved.
    "; - } - - # Now show em - my $sql = "select name, description, value from $tbl_options"; - my $sth = $dbh->prepare($sql); - $sth->execute(); - - print "
    "; - print ""; - while(my $hr = $sth->fetchrow_hashref) { - print ""; - if($hr->{'name'} eq 'version') { - print ""; - } else { - print ""; - } - print ""; - } - - print ""; - print ""; - print ""; - print "
    $hr->{'description'}$hr->{'value'}{'name'}\" value=\"$hr->{'value'}\" />
    "; -} - -sub show_footer { - my $older = 2; - if(defined(url_param('page'))) { - $older = url_param('page') + 1; - } - - if($last_page eq 0) { - if($query ne "") { - print " | More Results"; - } else { - print " | older"; - } - } -} - -sub do_import { - my $old_fh = select(OUTPUT_HANDLE); - my $cbuffer = ""; my $pcount = 0; - $| = 1; - select($old_fh); - - my ($omd5, $ourl, $otype, $olength, $odate, $sql, - $oadd, $omod, $otags); - my $ispec = ''; - - if($dbtype eq 'mysql') { $ispec = " ignore "; } - - $sql = "insert $ispec into pagecache_references - (md5_parent, md5_child) values(?,?)"; - my $insert_reference = $dbh->prepare($sql); - - $sql = "insert $ispec into pagecache - (md5,url,content_type, - content_length,content,date) - values(?,?,?,?,?,?)"; - my $insert_snapshot = $dbh->prepare($sql); - - $sql = 'update options set value = ? where (name = ?)'; - my $update_option = $dbh->prepare($sql); - - my $parser = new XML::Parser(); - $parser->setHandlers(Start => sub { - my %attr; - my $expat = shift; - my $element = shift; - - while(@_) { - my $att = shift; - my $val = shift; - $attr{$att} = $val; - } - - # netscape stuff - if ( $element eq "A" ) { - $ourl = $attr{'HREF'}; - $oadd = $attr{'ADD_DATE'}; - $omod = $attr{'LAST_MODIFIED'}; - $otags = $attr{'TAGS'}; - $cbuffer = ''; - } - - # A pagecache object - if($element eq "object") { - $omd5 = $attr{'md5'}; - $ourl = $attr{'url'}; - $otype = $attr{'type'}; - $olength = $attr{'length'}; - $odate = $attr{'date'}; - } - - # A pagecache relationship - if($element eq "relationship") { - my $parent = $attr{parent}; - my $child = $attr{child}; - $insert_reference->execute($parent, $child); - } - - # A bookmark - if($element eq "post") { - my $url = $attr{href}; - my $title = $attr{description}; - my $tagvalue = $attr{tag}; - my $datevalue = $attr{time}; - my $access_level; - if(defined($attr{access_level})) { - $access_level = $attr{access_level}; - } else { - $access_level = 1; - } - - my $epoch = str2time($datevalue); - - if($tagvalue eq "system:unfiled") { - $tagvalue = ""; - } - - add_bookmark($url, $title, "", $access_level, - $epoch, $tagvalue, 1); - } - - # Option - if($element eq 'option') { - $update_option->execute( - $attr{value}, $attr{name}); - } - }, Char => sub { - my $expat = shift; - my $chars = shift; - $cbuffer = $cbuffer . $chars; - 1; - }, End => sub { - my $expat = shift; - my $element = shift; - - # - # netscape stuff - # - if($element eq 'A') { - add_bookmark( - $ourl, # $url, - $cbuffer, # $title, - "", - '1', # $access_level, - $omod, # $epoch, - $otags, # $tagvalue - ); - $cbuffer = ''; - } - if ( $element eq 'TITLE' || $element eq 'H1' || $element eq 'DD') { - $cbuffer = ''; - } - - if($element eq 'object') { - - $insert_snapshot->bind_param(1, $omd5); - $insert_snapshot->bind_param(2, $ourl); - $insert_snapshot->bind_param(3, $otype); - $insert_snapshot->bind_param(4, $olength); - - if($dbtype eq "Pg") { - $insert_snapshot->bind_param(5, - decode_base64($cbuffer), SQL_VARBINARY); - } else { - $insert_snapshot->bind_param(5, - decode_base64($cbuffer)); - } - - $insert_snapshot->bind_param(6, $odate); - $insert_snapshot->execute; - - if(!defined($DBI::errstr)) { $pcount++; } - - $cbuffer = ""; - } - }); - - my $xml = ""; - -# BEGIN { -# *CORE::GLOBAL::die = sub { -# print "Some errors were detected. "; -# }; -# } - - if(defined($ENV{SERVER_NAME})) { - my $fh = upload('fileupload'); - while(<$fh>) { - $xml .= $_; - } - - $parser->parse($xml); - } else { - if(!defined($ARGV[0])) { - print "Please specify the filename to import.\n\n"; - exit; - } - - my $fn = $ARGV[0]; - $parser->parsefile($fn); - } - - print "Import finished - $icount bookmarks and $pcount " . - "snapshot objects imported.$NL"; -} - -sub do_export { - my ($snapshots, $islocal) = (@_); - my $writer; - - if(!defined($islocal)) { - print "Content-Type: text/xml;charset=UTF-8\r\n"; - print "Content-Disposition: attachment; filename=bookmarks.xml\r\n\r\n"; - } - - $writer = new XML::Writer(); - - $writer->xmlDecl('UTF-8'); - $writer->startTag('insipid'); - - export_bookmarks($writer); - export_snapshots($writer); - export_options($writer); - - $writer->endTag('insipid'); - $writer->end(); - exit; -} - -sub login_form { - print < -
    -Username:
    -
    -Password:
    -
    - -
    -
    -FORM -} - - -sub show_toolbar { - my $rdata = ""; - if(defined(url_param('tag'))) { - $rdata = url_param('tag'); - $rdata =~ s/ /\+/g; - } - - # Toolbar - print '
    '; - print ''; - print '
    '; - - # Title - print ''; - - if((get_option("public_searches") eq "yes") || (logged_in() eq 1)) { - print "
    "; - print "
    "; - print " "; - print "
    "; - print "
    "; - } else { - print " "; - } - - print "
    "; - print "
    "; - - if(logged_in() eq 1) { - print "options | "; - print "tags | "; - print "import | "; - print "export | "; - print "snapshots | "; - print "logout
    "; - print "add | "; - print "bookmarklets | "; - } - - my $rf; - if(get_option('use_rewrite') eq 'yes') { - $rf = $feed_url . '/' . $rdata; - } else { - $rf = $feed_url . $rdata; - } - - print "RSS feed"; - - if(logged_in() ne 1) { - print " | login"; - } - - print " | help "; - print " | source"; - - print "
    "; -} - -sub delete_bookmark { - my($id) = (@_); - my($sql, $sth, $md5) = ("", "", ""); - - if(logged_in() ne 1) { - push(@errors, "You have to be logged in to perform that operation."); - return; - } - - # Check for cached version to delete. - $sql = "select $tbl_pagecache.md5 from $tbl_pagecache - inner join $tbl_bookmarks on - ($tbl_pagecache.md5 = $tbl_bookmarks.md5) - where ($tbl_bookmarks.id = ?)"; - $sth = $dbh->prepare($sql); - $sth->execute($id); - while(my @r = $sth->fetchrow_array) { - $md5 = $r[0]; - } - - # Drop the tags for the bookmark - $sql = "delete from $tbl_bookmark_tags where (bookmark_id = ?)"; - $sth = $dbh->prepare($sql); - $sth->execute($id); - - # Drop the bookmark. - $sql = "delete from $tbl_bookmarks where (id = ?)"; - $sth = $dbh->prepare($sql); - $sth->execute($id); - - # Delete the cached page. - if($md5 ne "") { delete_snapshot($md5); } -} - -sub show_bookmarks { - my ($subquery, $sql, $sth, @parms, @wheres, @hr); - - # this first query will be used to select from a set, like when a user - # drills in on a specific tag or to get a smaller view of the entire - # dataset (for paging purposes). - - # MySQL and postgres have slightly different syntax here... - if ($dbtype eq 'mysql') { - $sql = "select $tbl_bookmarks.id from $tbl_bookmarks"; - } elsif ($dbtype eq 'Pg') { - $sql = "select $tbl_bookmarks.id, $tbl_bookmarks.date - from $tbl_bookmarks"; - } - - # Limit to tags - if(defined(url_param('tag'))) { - # Join the tag tables only when necessary - - if(url_param('tag') =~ / /) { - my @tags = split(/ /, url_param('tag')); - my $icount = 1; - - foreach(@tags) { - push(@parms, $_); - $sql = "$sql inner join $tbl_bookmark_tags - as bt$icount on - ($tbl_bookmarks.id = - bt$icount.bookmark_id) - inner join $tbl_tags as t$icount on - (t$icount.id = bt$icount.tag_id - and t$icount.name = ?) "; - $icount++; - } - } else { - $sql = "$sql - left join $tbl_bookmark_tags on - ($tbl_bookmarks.id = - $tbl_bookmark_tags.bookmark_id) - inner join $tbl_tags on - ($tbl_tags.id = $tbl_bookmark_tags.tag_id) - where ($tbl_tags.name = ?)"; - push(@parms, url_param('tag')); - } - - } - - # Search - if($query ne "") { - if((get_option("public_searches") eq "yes") || (logged_in() eq 1)) { - my $sparm = $query; - if(length($sparm) > 2) { - $sql = "$sql where ($tbl_bookmarks.title like ?)"; - $sparm =~ s/\%//; - $sparm = "\%$sparm\%"; - push(@parms, $sparm); - } - } - } - - # order - $sql = "$sql order by $tbl_bookmarks.date desc"; - - # paging functionality - $sql = "$sql limit 50"; - - if(defined(url_param('page'))) { - my $offset = ((url_param('page') - 1) * 50); - $sql = "$sql offset $offset"; - } - - $sth = $dbh->prepare($sql); - $sth->execute(@parms); - - $subquery = ""; - if($sth->rows > 0) { - if($sth->rows ne 50) { $last_page = 1; } - - $subquery = " $tbl_bookmarks.id in ("; - - while(@hr = $sth->fetchrow_array) { - $subquery = $subquery . "$hr[0],"; - } - chop($subquery); # Strip off the last delimiter - - $subquery = $subquery . ")"; - } else { - print "

    No bookmarks found.

    "; - return; - } - - @parms = (); - @wheres = (); - - $sql = "select - $tbl_bookmarks.id, - $tbl_bookmarks.title, - $tbl_bookmarks.description, - $tbl_bookmarks.access_level, - $tbl_bookmarks.url, - $tbl_tags.name, - $tbl_bookmarks.date, - $tbl_pagecache.date as cache_date, - $tbl_bookmarks.md5 - from $tbl_bookmarks - left join $tbl_bookmark_tags on - ($tbl_bookmarks.id = $tbl_bookmark_tags.bookmark_id) - left join $tbl_tags on - ($tbl_tags.id = $tbl_bookmark_tags.tag_id) - left join $tbl_pagecache on - ($tbl_bookmarks.md5 = $tbl_pagecache.md5)"; - - # Don't show private marks for non-logged in users - if(logged_in() eq 0) { - push(@wheres, "$tbl_bookmarks.access_level"); - push(@parms, "1"); - } - - my $max = @wheres; - if($max ne 0) { - $sql = "$sql where ("; - my $count = 1; - - foreach (@wheres) { - $sql = "$sql $_ = ?"; - if($count < $max) { - $sql = "$sql and "; - } - $count++; - } - - $sql = "$sql )"; - if($subquery ne "") { $sql = "$sql and $subquery"; } - } else { - if($subquery ne "") { $sql = "$sql where $subquery "; } - } - - # append sort order. - $sql = "$sql order by $tbl_bookmarks.date desc"; - - $sth = $dbh->prepare($sql); - $sth->execute(@parms); - - my %last; - $last{id} = -1; - - print '

      '; - - my $title = ''; - if(defined(url_param('tag'))) { - my $temp = url_param('tag'); - if($temp =~ / /) { - my $count = 0; - foreach(split(/ /, $temp)) { - if($count++ ne 0) { $title = "$title +"; } - $title = "$title $_"; - } - } else { - $title = "$temp"; - } - } else { - $title = 'Most Recent Bookmarks'; - } - - if($query ne '') { - $title = sprintf("Search results for \"%s\"", $query); - } - - print "$title"; - show_footer(); - print '

      '; - - print ""; - print '
      '; - print "
        \n"; - while(@hr = $sth->fetchrow_array) { - if($last{id} eq -1) { - $last{id} = $hr[0]; - $last{title} = $hr[1]; - $last{description} = $hr[2]; - $last{access_level} = $hr[3]; - $last{url} = $hr[4]; - $last{tags} = ""; - $last{timestamp} = $hr[6]; - $last{cachetime} = $hr[7]; - $last{md5} = $hr[8]; - } - - if($hr[0] ne $last{id}) { - # the id changed, so show the last mark. - show_bookmark($last{id}, $last{title}, $last{description}, $last{access_level}, $last{url}, $last{tags}, $last{timestamp}, $last{cachetime}, $last{md5}); - - # Swap the new one in. - $last{id} = $hr[0]; - $last{title} = $hr[1]; - $last{description} = $hr[2]; - $last{access_level} = $hr[3]; - $last{url} = $hr[4]; - $last{tags} = $hr[5]; - $last{timestamp} = $hr[6]; - $last{cachetime} = $hr[7]; - $last{md5} = $hr[8]; - } else { - # Add tag to the current bookmark - if(defined($hr[5])) { - $last{tags} = "$last{tags} $hr[5]"; - } - } - } - - if($last{id} ne -1) { - show_bookmark($last{id}, $last{title}, $last{description}, $last{access_level}, $last{url}, $last{tags}, $last{timestamp}, $last{cachetime}, $last{md5}); - } - - print "
      "; -} - -sub show_bookmark { - my($id, $title, $description, $access_level, $url, - $tags, $timestamp, $cachetime, $md5) = (@_); - - print "
      "; - print "
    • "; - if($access_level eq 0) { - print ""; - print ""; - print "$title"; - print ""; - } else { - print ""; - print $title; - } - - if(logged_in() eq 1) { - if(defined($cachetime)) { - print " - view snapshot"; - } - } - - print "
      "; - - - my $timestr = ""; - if(logged_in() eq 1) { - $timestr = time2str("%Y-%m-%d %T EST", $timestamp, "EST"); - } else { - $timestr = time2str("%Y-%m-%d", $timestamp, "EST"); - } - - print "posted on $timestr "; - - if(defined($tags)) { - print "to "; - my $cur; - - foreach $cur (split(/\ /, $tags)) { - print '' . $cur . ' '; - } - } - - if(logged_in() eq 1) { - my $ex = ""; - - if(url_param('tag')) { $ex = "$ex&tag=" . url_param('tag'); } - if(url_param('page')) { $ex = "$ex&page=" . url_param('page'); } - if($query ne "") { $ex = "$ex&q=" . $query; } - - print " — "; - print "(delete, "; - print "edit"; - if(!defined($cachetime)) { - print ", snapshot"; - } - print ")
      $description
    • \n"; - } - - print "
      \n"; -} - -# Gets the ID for a bookmark if it already exists in the DB. Otherwise, -1. -sub get_bookmark_id { - my ($url) = (@_); - - # Lookup the URL id first. - my $sql = "select $tbl_bookmarks.id from - $tbl_bookmarks where ($tbl_bookmarks.md5 = ?)"; - my $sth = $dbh->prepare($sql); - - $sth->execute(md5_hex($url)); - - if($sth->rows ne 0) { - my @r = $sth->fetchrow_array; - return $r[0]; - } - - return -1; -} - -sub get_bookmark { - my ($id) = (@_); - - my $sql = "select - $tbl_bookmarks.title, - $tbl_bookmarks.description, - $tbl_bookmarks.url, - $tbl_bookmarks.access_level - from $tbl_bookmarks - where ($tbl_bookmarks.id = ?)"; - my $sth = $dbh->prepare($sql); - $sth->execute($id); - my @r = $sth->fetchrow_array; - return ($r[2], $r[0], $r[1], $r[3]); -} - -sub update_bookmark { - my ($id, $url, $title, $description, $access_level, $tags) = (@_); - - if(logged_in() ne 1) { - push(@errors, "You have to be logged in to perform that operation."); - return; - } - - my $sql = "update $tbl_bookmarks - set url = ?, md5 = ?, title = ?, description = ?, - access_level = ? where (id = ?)"; - my $sth = $dbh->prepare($sql); - $sth->execute($url, md5_hex("$url"), $title, $description, - $access_level, $id); - - set_tags($id, $tags); -} - -1; -__END__ + $snapshot_params + Public: + + Return: + + + + $extra_params + + +FORM + } + } + } + + # Late redirects. TODO: Get rid of this. + if ( $redirect ne "" ) { + print +""; + print ""; + exit; + } + + if ( defined( param('op') ) ) { + if ( logged_in() eq 1 ) { + if ( param('op') eq 'fetchrelated' ) { + if ( defined( param('id') ) ) { + fetch_related( param('id') ); + } + } + + if ( param('op') eq 'snapshots' ) { + show_snapshots(); + print ""; + exit; + } + + if ( param('op') eq 'snapshot' ) { + if ( defined( param('id') ) ) { + do_snapshot( param('id') ); + print ""; + exit; + } + } + + if ( param('op') eq 'bookmarklets' ) { + print <This bookmarklet provides a fast way to add your browser's +current page to this Insipid installation. Either drag the +following link to your bookmarks toolbar or right-click on it +and choose "Bookmark This Link..." to create a bookmarklet. +Then when you're on a page you'd like to save, click on your +new "Add to Insipid" button and you'll be brought to a page +that allows you to fill out the tags for the bookmark and save +it. Once you've clicked Save you'll be brought back to the +page.

      +DESC + my $ad = <
    • Add to Insipid
    "; + print ""; + exit; + } + + # Configuration and management pages + if ( param('op') eq 'tags' ) { + tag_operations(); + print ''; + exit; + } + + if ( param('op') eq 'options' ) { + show_options(); + print ''; + exit; + } + } + } + + foreach (@errors) { + print "
    $_
    "; + } + + show_bookmarks(); + + print "

    "; +} # main + +################################################################ + +sub show_options { + + # Save options if they were posted. + print "

    "; + if ( param('save') ) { + my $sql = "update $tbl_options set value=? + where (name = ?)"; + my $sth = $dbh->prepare($sql); + + my %save; + foreach my $p (@valid) { + if ( param($p) ) { + $save{$p} = param($p); + } + } + + foreach my $k ( keys %save ) { + $sth->execute( $save{$k}, $k ); + } + + # The proxy_host can be empty, so check for that. + if ( !defined( $save{'proxy_host'} ) ) { + $sth->execute( '', 'proxy_host' ); + } + + print "
    Your options have been saved.
    "; + } + + # Now show em + my $sql = "select name, description, value from $tbl_options"; + my $sth = $dbh->prepare($sql); + $sth->execute(); + + print "
    "; + print ""; + while ( my $hr = $sth->fetchrow_hashref ) { + print ""; + if ( $hr->{'name'} eq 'version' ) { + print ""; + } + else { + print ""; + } + print ""; + } + + print ""; + print ""; + print ""; + print "
    $hr->{'description'}$hr->{'value'}{'name'}\" value=\"$hr->{'value'}\" />
    "; +} + +sub show_footer { + my $older = 2; + if ( defined( url_param('page') ) ) { + $older = url_param('page') + 1; + } + + if ( $last_page eq 0 ) { + if ( $query ne "" ) { + print " | More Results"; + } + else { + print " | older"; + } + } +} + +sub do_import { + my $old_fh = select(OUTPUT_HANDLE); + my $cbuffer = ""; + my $pcount = 0; + $| = 1; + select($old_fh); + + my ( $omd5, $ourl, $otype, $olength, $odate, $sql, $oadd, $omod, $otags ); + my $ispec = ''; + + if ( $dbtype eq 'mysql' ) { $ispec = " ignore "; } + + $sql = "insert $ispec into pagecache_references + (md5_parent, md5_child) values(?,?)"; + my $insert_reference = $dbh->prepare($sql); + + $sql = "insert $ispec into pagecache + (md5,url,content_type, + content_length,content,date) + values(?,?,?,?,?,?)"; + my $insert_snapshot = $dbh->prepare($sql); + + $sql = 'update options set value = ? where (name = ?)'; + my $update_option = $dbh->prepare($sql); + + my $parser = new XML::Parser(); + $parser->setHandlers( + Start => sub { + my %attr; + my $expat = shift; + my $element = shift; + + while (@_) { + my $att = shift; + my $val = shift; + $attr{$att} = $val; + } + + # netscape stuff + if ( $element eq "A" ) { + $ourl = $attr{'HREF'}; + $oadd = $attr{'ADD_DATE'}; + $omod = $attr{'LAST_MODIFIED'}; + $otags = $attr{'TAGS'}; + $cbuffer = ''; + } + + # A pagecache object + if ( $element eq "object" ) { + $omd5 = $attr{'md5'}; + $ourl = $attr{'url'}; + $otype = $attr{'type'}; + $olength = $attr{'length'}; + $odate = $attr{'date'}; + } + + # A pagecache relationship + if ( $element eq "relationship" ) { + my $parent = $attr{parent}; + my $child = $attr{child}; + $insert_reference->execute( $parent, $child ); + } + + # A bookmark + if ( $element eq "post" ) { + my $url = $attr{href}; + my $title = $attr{description}; + my $tagvalue = $attr{tag}; + my $datevalue = $attr{time}; + my $access_level; + if ( defined( $attr{access_level} ) ) { + $access_level = $attr{access_level}; + } + else { + $access_level = 1; + } + + my $epoch = str2time($datevalue); + + if ( $tagvalue eq "system:unfiled" ) { + $tagvalue = ""; + } + + add_bookmark( $url, $title, "", $access_level, $epoch, + $tagvalue, 1 ); + } + + # Option + if ( $element eq 'option' ) { + $update_option->execute( $attr{value}, $attr{name} ); + } + }, + Char => sub { + my $expat = shift; + my $chars = shift; + $cbuffer = $cbuffer . $chars; + 1; + }, + End => sub { + my $expat = shift; + my $element = shift; + + # + # netscape stuff + # + if ( $element eq 'A' ) { + add_bookmark( + $ourl, # $url, + $cbuffer, # $title, + "", + '1', # $access_level, + $omod, # $epoch, + $otags, # $tagvalue + ); + $cbuffer = ''; + } + if ( $element eq 'TITLE' || $element eq 'H1' || $element eq 'DD' ) { + $cbuffer = ''; + } + + if ( $element eq 'object' ) { + + $insert_snapshot->bind_param( 1, $omd5 ); + $insert_snapshot->bind_param( 2, $ourl ); + $insert_snapshot->bind_param( 3, $otype ); + $insert_snapshot->bind_param( 4, $olength ); + + if ( $dbtype eq "Pg" ) { + $insert_snapshot->bind_param( 5, decode_base64($cbuffer), + SQL_VARBINARY ); + } + else { + $insert_snapshot->bind_param( 5, decode_base64($cbuffer) ); + } + + $insert_snapshot->bind_param( 6, $odate ); + $insert_snapshot->execute; + + if ( !defined($DBI::errstr) ) { $pcount++; } + + $cbuffer = ""; + } + } + ); + + my $xml = ""; + + # BEGIN { + # *CORE::GLOBAL::die = sub { + # print "Some errors were detected. "; + # }; + # } + + if ( defined( $ENV{SERVER_NAME} ) ) { + my $fh = upload('fileupload'); + while (<$fh>) { + $xml .= $_; + } + + $parser->parse($xml); + } + else { + if ( !defined( $ARGV[0] ) ) { + print "Please specify the filename to import.\n\n"; + exit; + } + + my $fn = $ARGV[0]; + $parser->parsefile($fn); + } + + print "Import finished - $icount bookmarks and $pcount " + . "snapshot objects imported.$NL"; +} + +sub do_export { + my ( $snapshots, $islocal ) = (@_); + my $writer; + + if ( !defined($islocal) ) { + print "Content-Type: text/xml;charset=UTF-8\r\n"; + print "Content-Disposition: attachment; filename=bookmarks.xml\r\n\r\n"; + } + + $writer = new XML::Writer(); + + $writer->xmlDecl('UTF-8'); + $writer->startTag('insipid'); + + export_bookmarks($writer); + export_snapshots($writer); + export_options($writer); + + $writer->endTag('insipid'); + $writer->end(); + exit; +} + +sub login_form { + print < +
    +Username:
    +
    +Password:
    +
    + +
    +
    +FORM +} + +sub show_toolbar { + my $rdata = ""; + if ( defined( url_param('tag') ) ) { + $rdata = url_param('tag'); + $rdata =~ s/ /\+/g; + } + + # Toolbar + print '
    '; + print ''; + print '
    '; + + # Title + print ''; + + if ( ( get_option("public_searches") eq "yes" ) || ( logged_in() eq 1 ) ) { + print "
    "; + print "
    "; + print " "; + print "
    "; + print "
    "; + } + else { + print " "; + } + + print "
    "; + print "
    "; + + if ( logged_in() eq 1 ) { + print "options | "; + print "tags | "; + print "import | "; + print "export | "; + print "snapshots | "; + print "logout
    "; + print "add | "; + print "bookmarklets | "; + } + + my $rf; + if ( get_option('use_rewrite') eq 'yes' ) { + $rf = $feed_url . '/' . $rdata; + } + else { + $rf = $feed_url . $rdata; + } + + print "RSS feed"; + + if ( logged_in() ne 1 ) { + print " | login"; + } + + print " | help "; + print " | source"; + + print "
    "; +} + +sub delete_bookmark { + my ($id) = (@_); + my ( $sql, $sth, $md5 ) = ( "", "", "" ); + + if ( logged_in() ne 1 ) { + push( @errors, "You have to be logged in to perform that operation." ); + return; + } + + # Check for cached version to delete. + $sql = "select $tbl_pagecache.md5 from $tbl_pagecache + inner join $tbl_bookmarks on + ($tbl_pagecache.md5 = $tbl_bookmarks.md5) + where ($tbl_bookmarks.id = ?)"; + $sth = $dbh->prepare($sql); + $sth->execute($id); + while ( my @r = $sth->fetchrow_array ) { + $md5 = $r[0]; + } + + # Drop the tags for the bookmark + $sql = "delete from $tbl_bookmark_tags where (bookmark_id = ?)"; + $sth = $dbh->prepare($sql); + $sth->execute($id); + + # Drop the bookmark. + $sql = "delete from $tbl_bookmarks where (id = ?)"; + $sth = $dbh->prepare($sql); + $sth->execute($id); + + # Delete the cached page. + if ( $md5 ne "" ) { delete_snapshot($md5); } +} + +sub show_bookmarks { + my ( $subquery, $sql, $sth, @parms, @wheres, @hr ); + + # this first query will be used to select from a set, like when a user + # drills in on a specific tag or to get a smaller view of the entire + # dataset (for paging purposes). + + # MySQL and postgres have slightly different syntax here... + if ( $dbtype eq 'mysql' ) { + $sql = "select $tbl_bookmarks.id from $tbl_bookmarks"; + } + elsif ( $dbtype eq 'Pg' ) { + $sql = "select $tbl_bookmarks.id, $tbl_bookmarks.date + from $tbl_bookmarks"; + } + + # Limit to tags + if ( defined( url_param('tag') ) ) { + + # Join the tag tables only when necessary + + if ( url_param('tag') =~ / / ) { + my @tags = split( / /, url_param('tag') ); + my $icount = 1; + + foreach (@tags) { + push( @parms, $_ ); + $sql = "$sql inner join $tbl_bookmark_tags + as bt$icount on + ($tbl_bookmarks.id = + bt$icount.bookmark_id) + inner join $tbl_tags as t$icount on + (t$icount.id = bt$icount.tag_id + and t$icount.name = ?) "; + $icount++; + } + } + else { + $sql = "$sql + left join $tbl_bookmark_tags on + ($tbl_bookmarks.id = + $tbl_bookmark_tags.bookmark_id) + inner join $tbl_tags on + ($tbl_tags.id = $tbl_bookmark_tags.tag_id) + where ($tbl_tags.name = ?)"; + push( @parms, url_param('tag') ); + } + + } + + # Search + if ( $query ne "" ) { + if ( ( get_option("public_searches") eq "yes" ) + || ( logged_in() eq 1 ) ) + { + my $sparm = $query; + if ( length($sparm) > 2 ) { + $sql = "$sql where ($tbl_bookmarks.title like ?)"; + $sparm =~ s/\%//; + $sparm = "\%$sparm\%"; + push( @parms, $sparm ); + } + } + } + + # order + $sql = "$sql order by $tbl_bookmarks.date desc"; + + # paging functionality + $sql = "$sql limit 50"; + + if ( defined( url_param('page') ) ) { + my $offset = ( ( url_param('page') - 1 ) * 50 ); + $sql = "$sql offset $offset"; + } + + $sth = $dbh->prepare($sql); + $sth->execute(@parms); + + $subquery = ""; + if ( $sth->rows > 0 ) { + if ( $sth->rows ne 50 ) { $last_page = 1; } + + $subquery = " $tbl_bookmarks.id in ("; + + while ( @hr = $sth->fetchrow_array ) { + $subquery = $subquery . "$hr[0],"; + } + chop($subquery); # Strip off the last delimiter + + $subquery = $subquery . ")"; + } + else { + print "

    No bookmarks found.

    "; + return; + } + + @parms = (); + @wheres = (); + + $sql = "select + $tbl_bookmarks.id, + $tbl_bookmarks.title, + $tbl_bookmarks.description, + $tbl_bookmarks.access_level, + $tbl_bookmarks.url, + $tbl_tags.name, + $tbl_bookmarks.date, + $tbl_pagecache.date as cache_date, + $tbl_bookmarks.md5 + from $tbl_bookmarks + left join $tbl_bookmark_tags on + ($tbl_bookmarks.id = $tbl_bookmark_tags.bookmark_id) + left join $tbl_tags on + ($tbl_tags.id = $tbl_bookmark_tags.tag_id) + left join $tbl_pagecache on + ($tbl_bookmarks.md5 = $tbl_pagecache.md5)"; + + # Don't show private marks for non-logged in users + if ( logged_in() eq 0 ) { + push( @wheres, "$tbl_bookmarks.access_level" ); + push( @parms, "1" ); + } + + my $max = @wheres; + if ( $max ne 0 ) { + $sql = "$sql where ("; + my $count = 1; + + foreach (@wheres) { + $sql = "$sql $_ = ?"; + if ( $count < $max ) { + $sql = "$sql and "; + } + $count++; + } + + $sql = "$sql )"; + if ( $subquery ne "" ) { $sql = "$sql and $subquery"; } + } + else { + if ( $subquery ne "" ) { $sql = "$sql where $subquery "; } + } + + # append sort order. + $sql = "$sql order by $tbl_bookmarks.date desc"; + + $sth = $dbh->prepare($sql); + $sth->execute(@parms); + + my %last; + $last{id} = -1; + + print '

      '; + + my $title = ''; + if ( defined( url_param('tag') ) ) { + my $temp = url_param('tag'); + if ( $temp =~ / / ) { + my $count = 0; + foreach ( split( / /, $temp ) ) { + if ( $count++ ne 0 ) { $title = "$title +"; } + $title = + "$title $_"; + } + } + else { + $title = "$temp"; + } + } + else { + $title = 'Most Recent Bookmarks'; + } + + if ( $query ne '' ) { + $title = sprintf( "Search results for \"%s\"", $query ); + } + + print "$title"; + show_footer(); + print '

      '; + + print ""; + print '
      '; + print "
        \n"; + while ( @hr = $sth->fetchrow_array ) { + if ( $last{id} eq -1 ) { + $last{id} = $hr[0]; + $last{title} = $hr[1]; + $last{description} = $hr[2]; + $last{access_level} = $hr[3]; + $last{url} = $hr[4]; + $last{tags} = ""; + $last{timestamp} = $hr[6]; + $last{cachetime} = $hr[7]; + $last{md5} = $hr[8]; + } + + if ( $hr[0] ne $last{id} ) { + + # the id changed, so show the last mark. + show_bookmark( + $last{id}, $last{title}, $last{description}, + $last{access_level}, $last{url}, $last{tags}, + $last{timestamp}, $last{cachetime}, $last{md5} + ); + + # Swap the new one in. + $last{id} = $hr[0]; + $last{title} = $hr[1]; + $last{description} = $hr[2]; + $last{access_level} = $hr[3]; + $last{url} = $hr[4]; + $last{tags} = $hr[5]; + $last{timestamp} = $hr[6]; + $last{cachetime} = $hr[7]; + $last{md5} = $hr[8]; + } + else { + + # Add tag to the current bookmark + if ( defined( $hr[5] ) ) { + $last{tags} = "$last{tags} $hr[5]"; + } + } + } + + if ( $last{id} ne -1 ) { + show_bookmark( + $last{id}, $last{title}, $last{description}, + $last{access_level}, $last{url}, $last{tags}, + $last{timestamp}, $last{cachetime}, $last{md5} + ); + } + + print "
      "; +} + +sub show_bookmark { + my ( + $id, $title, $description, + $access_level, $url, $tags, + $timestamp, $cachetime, $md5 + ) = (@_); + + print "
      "; + print "
    • "; + if ( $access_level eq 0 ) { + print ""; + print ""; + print "$title"; + print ""; + } + else { + print ""; + print $title; + } + + if ( logged_in() eq 1 ) { + if ( defined($cachetime) ) { + print " - view snapshot"; + } + } + + print "
      "; + + my $timestr = ""; + if ( logged_in() eq 1 ) { + $timestr = time2str( "%Y-%m-%d %T EST", $timestamp, "EST" ); + } + else { + $timestr = time2str( "%Y-%m-%d", $timestamp, "EST" ); + } + + print "posted on $timestr "; + + if ( defined($tags) ) { + print "to "; + my $cur; + + foreach $cur ( split( /\ /, $tags ) ) { + print '' . $cur . ' '; + } + } + + if ( logged_in() eq 1 ) { + my $ex = ""; + + if ( url_param('tag') ) { $ex = "$ex&tag=" . url_param('tag'); } + if ( url_param('page') ) { $ex = "$ex&page=" . url_param('page'); } + if ( $query ne "" ) { $ex = "$ex&q=" . $query; } + + print " — "; + print +"(delete, "; + print +"edit"; + if ( !defined($cachetime) ) { + print +", snapshot"; + } + print +")
      $description
    • \n"; + } + + print "
      \n"; +} + +# Gets the ID for a bookmark if it already exists in the DB. Otherwise, -1. +sub get_bookmark_id { + my ($url) = (@_); + + # Lookup the URL id first. + my $sql = "select $tbl_bookmarks.id from + $tbl_bookmarks where ($tbl_bookmarks.md5 = ?)"; + my $sth = $dbh->prepare($sql); + + $sth->execute( md5_hex($url) ); + + if ( $sth->rows ne 0 ) { + my @r = $sth->fetchrow_array; + return $r[0]; + } + + return -1; +} + +sub get_bookmark { + my ($id) = (@_); + + my $sql = "select + $tbl_bookmarks.title, + $tbl_bookmarks.description, + $tbl_bookmarks.url, + $tbl_bookmarks.access_level + from $tbl_bookmarks + where ($tbl_bookmarks.id = ?)"; + my $sth = $dbh->prepare($sql); + $sth->execute($id); + my @r = $sth->fetchrow_array; + return ( $r[2], $r[0], $r[1], $r[3] ); +} + +sub update_bookmark { + my ( $id, $url, $title, $description, $access_level, $tags ) = (@_); + + if ( logged_in() ne 1 ) { + push( @errors, "You have to be logged in to perform that operation." ); + return; + } + + my $sql = "update $tbl_bookmarks + set url = ?, md5 = ?, title = ?, description = ?, + access_level = ? where (id = ?)"; + my $sth = $dbh->prepare($sql); + $sth->execute( $url, md5_hex("$url"), $title, $description, $access_level, + $id ); + + set_tags( $id, $tags ); +} + +1; +__END__