-#!/usr/bin/perl\r
-#\r
-# Copyright (C) 2008 Luke Reeves\r
-#\r
-# This program is free software; you can redistribute it and/or modify\r
-# it under the terms of the GNU General Public License as published by\r
-# the Free Software Foundation; either version 2 of the License, or\r
-# (at your option) any later version.\r
-#\r
-# This program is distributed in the hope that it will be useful,\r
-# but WITHOUT ANY WARRANTY; without even the implied warranty of\r
-# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\r
-# GNU General Public License for more details.\r
-#\r
-# You should have received a copy of the GNU General Public License\r
-# along with this program; if not, write to the Free Software\r
-# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307\r
-# USA\r
-#\r
-\r
-package Insipid::Bookmarks;\r
-\r
-use strict;\r
-use warnings;\r
-\r
-use vars qw(@ISA @EXPORT @EXPORT_OK $icount $duplicates);\r
-use Insipid::Config;\r
-use Insipid::Database;\r
-use Insipid::Schemas;\r
-use Insipid::Sessions;\r
-use Insipid::Tags;\r
-use Insipid::Util;\r
-use DBI qw/:sql_types/;;\r
-use Date::Format;\r
-use Date::Parse;\r
-use CGI qw/:standard/;\r
-use CGI::Carp qw(fatalsToBrowser);\r
-use Digest::MD5 qw(md5 md5_hex);\r
-\r
-require Exporter;\r
-\r
-@ISA = qw(Exporter);\r
-\r
-@EXPORT = qw(\r
-add_bookmark\r
-export_bookmarks\r
-get_bookmark_id_by_url\r
-$icount\r
-$duplicates\r
-);\r
-\r
-sub get_bookmark_id_by_url {\r
- my ($url) = (@_);\r
- my $sql = "select $tbl_bookmarks.id from $tbl_bookmarks \r
- where ($tbl_bookmarks.url = ?)";\r
- my $sth = $dbh->prepare($sql);\r
- $sth->execute($url);\r
-\r
- my @r = $sth->fetchrow_array;\r
- return $r[0];\r
-}\r
-\r
-sub add_bookmark {\r
- my ($url, $title, $description, $access_level, $epoch, $tags) = (@_);\r
- my ($sql, $sth);\r
-\r
- if(logged_in() ne 1) {\r
- push(@errors, 'You have to be logged in to perform ' .\r
- 'that operation.');\r
- return;\r
- }\r
-\r
- my $md5 = md5_hex($url);\r
-\r
- # Check for duplicate\r
- $sql = "select title from $tbl_bookmarks where (md5 = ?)";\r
- $sth = $dbh->prepare($sql);\r
- $sth->execute($md5);\r
- \r
- if($sth->rows ne 0) {\r
- $duplicates++;\r
- return;\r
- }\r
-\r
- $sql = "INSERT INTO $tbl_bookmarks \r
- (url, md5, title, description, access_level, date) \r
- VALUES (?, ?, ?, ?, ?, ?)";\r
-\r
- if($epoch eq 0) { $epoch = time; }\r
- $sth = $dbh->prepare($sql);\r
-\r
- $sth->execute($url, $md5, $title, $description, $access_level, $epoch)\r
- or die $DBI::errstr;\r
- \r
- $icount++;\r
-\r
- set_tags(get_bookmark_id_by_url($url), $tags);\r
-}\r
-\r
-sub export_bookmarks {\r
- my ($writer) = (@_);\r
-\r
- my ($sql, $sth, $last_id);\r
-\r
- $writer->startTag("posts");\r
-\r
- $sql = "select \r
- $tbl_bookmarks.id, $tbl_bookmarks.title, \r
- $tbl_bookmarks.date, $tbl_bookmarks.access_level, \r
- $tbl_bookmarks.url, $tbl_tags.name\r
- from $tbl_bookmarks\r
- left join $tbl_bookmark_tags on\r
- ($tbl_bookmarks.id = $tbl_bookmark_tags.bookmark_id)\r
- left join $tbl_tags on\r
- ($tbl_bookmark_tags.tag_id = $tbl_tags.id)";\r
-\r
- $sth = $dbh->prepare($sql);\r
- $sth->execute();\r
-\r
- my ($url, $title);\r
- my $tags = "";\r
-\r
- my %last;\r
- $last_id = -1;\r
- my $current = 0;\r
- my $max = $sth->rows;\r
-\r
- # There HAS to be a better way to do this horrible looping for tags.\r
- while(my $hr = $sth->fetchrow_hashref) {\r
- $current++;\r
-\r
- # For the first bookmark\r
- if($last_id eq -1) {\r
- $last_id = $hr->{'id'};\r
- $last{title} = $hr->{'title'};\r
- $last{url} = $hr->{'url'};\r
- $last{tags} = "";\r
- $last{timestamp} = $hr->{'date'};\r
- $last{access_level} = $hr->{'access_level'};\r
- } \r
- \r
- #if(($hr->{'id'} ne $last_id) || ($current eq $max)) {\r
- if($hr->{'id'} ne $last_id) {\r
- # the id changed, so show the last mark.\r
- #my $url = sanitize_html($last{'url'});\r
- my $url = $last{'url'};\r
- my $title = $last{'title'};\r
- #my $title = sanitize_html($last{'title'});\r
- #$title =~ s/"/"/gi;\r
- if(defined($last{tags})) {\r
- if($last{tags} eq "") {\r
- $last{tags} = "system:unfiled"; \r
- }\r
- } else {\r
- $last{tags} = "system:unfiled";\r
- }\r
- \r
- if($last{url} ne "") {\r
- my $tstr = time2str("%Y-%m-%dT%TZ", $last{timestamp}, "GMT");\r
- $writer->emptyTag('post',\r
- 'access_level' => $last{access_level},\r
- 'href' => $url,\r
- 'description' => $title,\r
- 'tag' => $last{tags},\r
- 'time' => $tstr);\r
- }\r
-\r
- # Swap the new one in.\r
- $last_id = $hr->{'id'};\r
- $last{title} = $hr->{'title'};\r
- $last{url} = $hr->{'url'};\r
- $last{tags} = $hr->{'name'};\r
- $last{timestamp} = $hr->{'date'};\r
- $last{access_level} = $hr->{'access_level'};\r
- } else {\r
- # Add tag to the current bookmark\r
- if($hr->{'name'}) {\r
- $last{tags} = "$last{tags} $hr->{'name'}";\r
- }\r
- }\r
- }\r
- \r
- if($last{'url'}) {\r
- #$url = sanitize_html($last{'url'});\r
- #$title = sanitize_html($last{'title'});\r
- #$title =~ s/"/"/gi;\r
- \r
- $url = $last{'url'};\r
- $title = $last{'title'};\r
- \r
- if(defined($last{tags})) {\r
- if($last{tags} eq "") {\r
- $last{tags} = "system:unfiled"; \r
- }\r
- } else {\r
- $last{tags} = "system:unfiled";\r
- }\r
- \r
- if($last{url} ne "") {\r
- my $tstr = time2str("%Y-%m-%dT%TZ", $last{timestamp}, "GMT");\r
- $writer->emptyTag('post',\r
- 'access_level' => $last{access_level},\r
- 'href' => $url,\r
- 'description' => $title,\r
- 'tag' => $last{tags},\r
- 'time' => $tstr);\r
- }\r
- }\r
-\r
- $writer->endTag("posts");\r
-}\r
-\r
-1;\r
-__END__\r
+#!/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::Bookmarks;
+
+use strict;
+use warnings;
+
+use vars qw(@ISA @EXPORT @EXPORT_OK $icount $duplicates);
+use Insipid::Config;
+use Insipid::Database;
+use Insipid::Schemas;
+use Insipid::Sessions;
+use Insipid::Tags;
+use Insipid::Util;
+use DBI qw/:sql_types/;;
+use Date::Format;
+use Date::Parse;
+use CGI qw/:standard/;
+use CGI::Carp qw(fatalsToBrowser);
+use Digest::MD5 qw(md5 md5_hex);
+
+require Exporter;
+
+@ISA = qw(Exporter);
+
+@EXPORT = qw(
+add_bookmark
+export_bookmarks
+get_bookmark_id_by_url
+$icount
+$duplicates
+);
+
+sub get_bookmark_id_by_url {
+ my ($url) = (@_);
+ my $sql = "select $tbl_bookmarks.id from $tbl_bookmarks
+ where ($tbl_bookmarks.url = ?)";
+ my $sth = $dbh->prepare($sql);
+ $sth->execute($url);
+
+ my @r = $sth->fetchrow_array;
+ return $r[0];
+}
+
+sub add_bookmark {
+ my ($url, $title, $description, $access_level, $epoch, $tags) = (@_);
+ my ($sql, $sth);
+
+ check_access();
+
+ my $md5 = md5_hex($url);
+
+ # Check for duplicate
+ $sql = "select title from $tbl_bookmarks where (md5 = ?)";
+ $sth = $dbh->prepare($sql);
+ $sth->execute($md5);
+
+ if($sth->rows ne 0) {
+ $duplicates++;
+ return;
+ }
+
+ $sql = "INSERT INTO $tbl_bookmarks
+ (url, md5, title, description, access_level, date)
+ VALUES (?, ?, ?, ?, ?, ?)";
+
+ if($epoch eq 0) { $epoch = time; }
+ $sth = $dbh->prepare($sql);
+
+ $sth->execute($url, $md5, $title, $description, $access_level, $epoch)
+ or die $DBI::errstr;
+
+ $icount++;
+
+ set_tags(get_bookmark_id_by_url($url), $tags);
+}
+
+sub export_bookmarks {
+ my ($writer) = (@_);
+
+ my ($sql, $sth, $last_id);
+
+ $writer->startTag("posts");
+
+ $sql = "select
+ $tbl_bookmarks.id, $tbl_bookmarks.title,
+ $tbl_bookmarks.date, $tbl_bookmarks.access_level,
+ $tbl_bookmarks.url, $tbl_tags.name
+ from $tbl_bookmarks
+ left join $tbl_bookmark_tags on
+ ($tbl_bookmarks.id = $tbl_bookmark_tags.bookmark_id)
+ left join $tbl_tags on
+ ($tbl_bookmark_tags.tag_id = $tbl_tags.id)";
+
+ $sth = $dbh->prepare($sql);
+ $sth->execute();
+
+ my ($url, $title);
+ my $tags = "";
+
+ my %last;
+ $last_id = -1;
+ my $current = 0;
+ my $max = $sth->rows;
+
+ # There HAS to be a better way to do this horrible looping for tags.
+ while(my $hr = $sth->fetchrow_hashref) {
+ $current++;
+
+ # For the first bookmark
+ if($last_id eq -1) {
+ $last_id = $hr->{'id'};
+ $last{title} = $hr->{'title'};
+ $last{url} = $hr->{'url'};
+ $last{tags} = "";
+ $last{timestamp} = $hr->{'date'};
+ $last{access_level} = $hr->{'access_level'};
+ }
+
+ #if(($hr->{'id'} ne $last_id) || ($current eq $max)) {
+ if($hr->{'id'} ne $last_id) {
+ # the id changed, so show the last mark.
+ #my $url = sanitize_html($last{'url'});
+ my $url = $last{'url'};
+ my $title = $last{'title'};
+ #my $title = sanitize_html($last{'title'});
+ #$title =~ s/"/"/gi;
+ if(defined($last{tags})) {
+ if($last{tags} eq "") {
+ $last{tags} = "system:unfiled";
+ }
+ } else {
+ $last{tags} = "system:unfiled";
+ }
+
+ if($last{url} ne "") {
+ my $tstr = time2str("%Y-%m-%dT%TZ", $last{timestamp}, "GMT");
+ $writer->emptyTag('post',
+ 'access_level' => $last{access_level},
+ 'href' => $url,
+ 'description' => $title,
+ 'tag' => $last{tags},
+ 'time' => $tstr);
+ }
+
+ # Swap the new one in.
+ $last_id = $hr->{'id'};
+ $last{title} = $hr->{'title'};
+ $last{url} = $hr->{'url'};
+ $last{tags} = $hr->{'name'};
+ $last{timestamp} = $hr->{'date'};
+ $last{access_level} = $hr->{'access_level'};
+ } else {
+ # Add tag to the current bookmark
+ if($hr->{'name'}) {
+ $last{tags} = "$last{tags} $hr->{'name'}";
+ }
+ }
+ }
+
+ if($last{'url'}) {
+ #$url = sanitize_html($last{'url'});
+ #$title = sanitize_html($last{'title'});
+ #$title =~ s/"/"/gi;
+
+ $url = $last{'url'};
+ $title = $last{'title'};
+
+ if(defined($last{tags})) {
+ if($last{tags} eq "") {
+ $last{tags} = "system:unfiled";
+ }
+ } else {
+ $last{tags} = "system:unfiled";
+ }
+
+ if($last{url} ne "") {
+ my $tstr = time2str("%Y-%m-%dT%TZ", $last{timestamp}, "GMT");
+ $writer->emptyTag('post',
+ 'access_level' => $last{access_level},
+ 'href' => $url,
+ 'description' => $title,
+ 'tag' => $last{tags},
+ 'time' => $tstr);
+ }
+ }
+
+ $writer->endTag("posts");
+}
+
+1;
+__END__
-#!/usr/bin/perl\r
-#\r
-# Copyright (C) 2008 Luke Reeves\r
-#\r
-# This program is free software; you can redistribute it and/or modify\r
-# it under the terms of the GNU General Public License as published by\r
-# the Free Software Foundation; either version 2 of the License, or\r
-# (at your option) any later version.\r
-#\r
-# This program is distributed in the hope that it will be useful,\r
-# but WITHOUT ANY WARRANTY; without even the implied warranty of\r
-# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\r
-# GNU General Public License for more details.\r
-#\r
-# You should have received a copy of the GNU General Public License\r
-# along with this program; if not, write to the Free Software\r
-# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307\r
-# USA\r
-#\r
-\r
-package Insipid::Util;\r
-\r
-use strict;\r
-use warnings;\r
-\r
-use vars qw(@ISA @EXPORT @EXPORT_OK);\r
-\r
-use Insipid::Sessions;\r
-use Insipid::Config;\r
-\r
-require Exporter;\r
-\r
-@ISA = qw(Exporter);\r
-\r
-@EXPORT = qw(\r
-ims_time\r
-sanitize_html\r
-check_access\r
-);\r
-\r
-@EXPORT_OK = qw();\r
-\r
-my @DoW = qw(Sun Mon Tue Wed Thu Fri Sat);\r
-my @MoY = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec);\r
-\r
-# TODO: If content is already sent, add the error to @errors instead of dying\r
-sub check_access {\r
- if(logged_in() ne 1) {\r
- print "Content-Type: text/plain\r\n\r\n";\r
- print "You have to be logged in to perform that operation.";\r
- exit;\r
- }\r
-}\r
-\r
-sub sanitize_html {\r
- my ($orig) = (@_);\r
-\r
- $orig =~ s/</</gi;\r
- $orig =~ s/>/>/gi;\r
-\r
- $orig =~ s/&/&/gi;\r
- $orig =~ s/&/&/gi;\r
-\r
- return $orig;\r
-}\r
-\r
-\r
-# From http::date\r
-sub ims_time {\r
- my ($time) = (@_);\r
- my ($sec, $min, $hour, $mday, $mon, $year, $wday) = gmtime($time);\r
- return sprintf("%s, %02d %s %04d %02d:%02d:%02d GMT",\r
- $DoW[$wday],\r
- $mday, $MoY[$mon], $year+1900,\r
- $hour, $min, $sec);\r
-}\r
-\r
-1;\r
-__END__\r
+#!/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::Util;
+
+use strict;
+use warnings;
+
+use vars qw(@ISA @EXPORT @EXPORT_OK);
+
+use Insipid::Sessions;
+use Insipid::Config;
+
+require Exporter;
+
+@ISA = qw(Exporter);
+
+@EXPORT = qw(
+ims_time
+sanitize_html
+check_access
+);
+
+@EXPORT_OK = qw();
+
+my @DoW = qw(Sun Mon Tue Wed Thu Fri Sat);
+my @MoY = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec);
+
+# TODO: If content is already sent, add the error to @errors instead of dying
+sub check_access {
+ if(logged_in() ne 1) {
+ #print "Content-Type: text/plain\r\n\r\n";
+ print "You have to be logged in to perform that operation.";
+ exit;
+ }
+}
+
+sub sanitize_html {
+ my ($orig) = (@_);
+
+ $orig =~ s/</</gi;
+ $orig =~ s/>/>/gi;
+
+ $orig =~ s/&/&/gi;
+ $orig =~ s/&/&/gi;
+
+ return $orig;
+}
+
+
+# From http::date
+sub ims_time {
+ my ($time) = (@_);
+ my ($sec, $min, $hour, $mday, $mon, $year, $wday) = gmtime($time);
+ return sprintf("%s, %02d %s %04d %02d:%02d:%02d GMT",
+ $DoW[$wday],
+ $mday, $MoY[$mon], $year+1900,
+ $hour, $min, $sec);
+}
+
+1;
+__END__